Update src/lora_helper.py
Browse files- src/lora_helper.py +159 -147
src/lora_helper.py
CHANGED
|
@@ -4,7 +4,7 @@ import re
|
|
| 4 |
import torch
|
| 5 |
from .layers_cache import MultiDoubleStreamBlockLoraProcessor, MultiSingleStreamBlockLoraProcessor
|
| 6 |
|
| 7 |
-
device = "cuda"
|
| 8 |
|
| 9 |
def load_safetensors(path):
|
| 10 |
tensors = {}
|
|
@@ -29,168 +29,180 @@ def load_checkpoint(local_path):
|
|
| 29 |
return checkpoint
|
| 30 |
|
| 31 |
def update_model_with_lora(checkpoint, lora_weights, transformer, cond_size, device="cpu"):
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
for key, value in checkpoint.items():
|
| 46 |
-
# Match based on the layer index in the key (assuming the key contains layer index)
|
| 47 |
if re.search(r'\.(\d+)\.', key):
|
| 48 |
checkpoint_layer_index = int(re.search(r'\.(\d+)\.', key).group(1))
|
| 49 |
if checkpoint_layer_index == layer_index and key.startswith("transformer_blocks"):
|
| 50 |
-
lora_state_dicts[key] = value
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
for key, value in checkpoint.items():
|
| 72 |
-
# Match based on the layer index in the key (assuming the key contains layer index)
|
| 73 |
if re.search(r'\.(\d+)\.', key):
|
| 74 |
checkpoint_layer_index = int(re.search(r'\.(\d+)\.', key).group(1))
|
| 75 |
if checkpoint_layer_index == layer_index and key.startswith("single_transformer_blocks"):
|
| 76 |
-
lora_state_dicts[key] = value
|
| 77 |
-
|
| 78 |
-
lora_attn_procs[name] = MultiSingleStreamBlockLoraProcessor(
|
| 79 |
-
dim=3072, ranks=ranks, network_alphas=ranks, lora_weights=lora_weights, device=device, dtype=torch.bfloat16, cond_width=cond_size, cond_height=cond_size, n_loras=number
|
| 80 |
-
)
|
| 81 |
-
# Load the weights from the checkpoint dictionary into the corresponding layers
|
| 82 |
-
for n in range(number):
|
| 83 |
-
lora_attn_procs[name].q_loras[n].down.weight.data = lora_state_dicts.get(f'{name}.q_loras.{n}.down.weight', None)
|
| 84 |
-
lora_attn_procs[name].q_loras[n].up.weight.data = lora_state_dicts.get(f'{name}.q_loras.{n}.up.weight', None)
|
| 85 |
-
lora_attn_procs[name].k_loras[n].down.weight.data = lora_state_dicts.get(f'{name}.k_loras.{n}.down.weight', None)
|
| 86 |
-
lora_attn_procs[name].k_loras[n].up.weight.data = lora_state_dicts.get(f'{name}.k_loras.{n}.up.weight', None)
|
| 87 |
-
lora_attn_procs[name].v_loras[n].down.weight.data = lora_state_dicts.get(f'{name}.v_loras.{n}.down.weight', None)
|
| 88 |
-
lora_attn_procs[name].v_loras[n].up.weight.data = lora_state_dicts.get(f'{name}.v_loras.{n}.up.weight', None)
|
| 89 |
-
lora_attn_procs[name].to(device)
|
| 90 |
-
else:
|
| 91 |
-
lora_attn_procs[name] = FluxAttnProcessor2_0()
|
| 92 |
-
|
| 93 |
-
transformer.set_attn_processor(lora_attn_procs)
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
def update_model_with_multi_lora(checkpoints, lora_weights, transformer, cond_size):
|
| 97 |
-
ck_number = len(checkpoints)
|
| 98 |
-
cond_lora_number = [len(ls) for ls in lora_weights]
|
| 99 |
-
cond_number = sum(cond_lora_number)
|
| 100 |
-
ranks = [get_lora_rank(checkpoint) for checkpoint in checkpoints]
|
| 101 |
-
multi_lora_weight = []
|
| 102 |
-
for ls in lora_weights:
|
| 103 |
-
for n in ls:
|
| 104 |
-
multi_lora_weight.append(n)
|
| 105 |
-
|
| 106 |
-
lora_attn_procs = {}
|
| 107 |
-
double_blocks_idx = list(range(19))
|
| 108 |
-
single_blocks_idx = list(range(38))
|
| 109 |
-
for name, attn_processor in transformer.attn_processors.items():
|
| 110 |
-
match = re.search(r'\.(\d+)\.', name)
|
| 111 |
-
if match:
|
| 112 |
-
layer_index = int(match.group(1))
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
lora_attn_procs[name].proj_loras[num].down.weight.data = lora_state_dicts[idx].get(f'{name}.proj_loras.{n}.down.weight', None)
|
| 139 |
-
lora_attn_procs[name].proj_loras[num].up.weight.data = lora_state_dicts[idx].get(f'{name}.proj_loras.{n}.up.weight', None)
|
| 140 |
-
lora_attn_procs[name].to(device)
|
| 141 |
-
num += 1
|
| 142 |
-
|
| 143 |
-
elif name.startswith("single_transformer_blocks") and layer_index in single_blocks_idx:
|
| 144 |
-
|
| 145 |
-
lora_state_dicts = [{} for _ in range(ck_number)]
|
| 146 |
-
for idx, checkpoint in enumerate(checkpoints):
|
| 147 |
-
for key, value in checkpoint.items():
|
| 148 |
-
# Match based on the layer index in the key (assuming the key contains layer index)
|
| 149 |
-
if re.search(r'\.(\d+)\.', key):
|
| 150 |
-
checkpoint_layer_index = int(re.search(r'\.(\d+)\.', key).group(1))
|
| 151 |
-
if checkpoint_layer_index == layer_index and key.startswith("single_transformer_blocks"):
|
| 152 |
-
lora_state_dicts[idx][key] = value
|
| 153 |
-
|
| 154 |
-
lora_attn_procs[name] = MultiSingleStreamBlockLoraProcessor(
|
| 155 |
-
dim=3072, ranks=ranks, network_alphas=ranks, lora_weights=multi_lora_weight, device=device, dtype=torch.bfloat16, cond_width=cond_size, cond_height=cond_size, n_loras=cond_number
|
| 156 |
-
)
|
| 157 |
-
# Load the weights from the checkpoint dictionary into the corresponding layers
|
| 158 |
-
num = 0
|
| 159 |
-
for idx in range(ck_number):
|
| 160 |
-
for n in range(cond_lora_number[idx]):
|
| 161 |
-
lora_attn_procs[name].q_loras[num].down.weight.data = lora_state_dicts[idx].get(f'{name}.q_loras.{n}.down.weight', None)
|
| 162 |
-
lora_attn_procs[name].q_loras[num].up.weight.data = lora_state_dicts[idx].get(f'{name}.q_loras.{n}.up.weight', None)
|
| 163 |
-
lora_attn_procs[name].k_loras[num].down.weight.data = lora_state_dicts[idx].get(f'{name}.k_loras.{n}.down.weight', None)
|
| 164 |
-
lora_attn_procs[name].k_loras[num].up.weight.data = lora_state_dicts[idx].get(f'{name}.k_loras.{n}.up.weight', None)
|
| 165 |
-
lora_attn_procs[name].v_loras[num].down.weight.data = lora_state_dicts[idx].get(f'{name}.v_loras.{n}.down.weight', None)
|
| 166 |
-
lora_attn_procs[name].v_loras[num].up.weight.data = lora_state_dicts[idx].get(f'{name}.v_loras.{n}.up.weight', None)
|
| 167 |
-
lora_attn_procs[name].to(device)
|
| 168 |
-
num += 1
|
| 169 |
-
|
| 170 |
-
else:
|
| 171 |
-
lora_attn_procs[name] = FluxAttnProcessor2_0()
|
| 172 |
-
|
| 173 |
-
transformer.set_attn_processor(lora_attn_procs)
|
| 174 |
|
|
|
|
| 175 |
|
| 176 |
-
def set_single_lora(transformer, local_path, lora_weights=[], cond_size=512):
|
| 177 |
checkpoint = load_checkpoint(local_path)
|
| 178 |
-
update_model_with_lora(checkpoint, lora_weights, transformer, cond_size)
|
| 179 |
-
|
| 180 |
-
def set_multi_lora(transformer, local_paths, lora_weights=[[]], cond_size=512):
|
| 181 |
checkpoints = [load_checkpoint(local_path) for local_path in local_paths]
|
| 182 |
-
update_model_with_multi_lora(checkpoints, lora_weights, transformer, cond_size)
|
| 183 |
|
| 184 |
def unset_lora(transformer):
|
| 185 |
lora_attn_procs = {}
|
| 186 |
for name, attn_processor in transformer.attn_processors.items():
|
| 187 |
lora_attn_procs[name] = FluxAttnProcessor2_0()
|
| 188 |
-
transformer.set_attn_processor(lora_attn_procs)
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
'''
|
| 192 |
-
unset_lora(pipe.transformer)
|
| 193 |
-
lora_path = "./lora.safetensors"
|
| 194 |
-
lora_weights = [1, 1]
|
| 195 |
-
set_lora(pipe.transformer, local_path=lora_path, lora_weights=lora_weights, cond_size=512)
|
| 196 |
-
'''
|
|
|
|
| 4 |
import torch
|
| 5 |
from .layers_cache import MultiDoubleStreamBlockLoraProcessor, MultiSingleStreamBlockLoraProcessor
|
| 6 |
|
| 7 |
+
# 移除全局 device = "cuda",改为通过参数传递
|
| 8 |
|
| 9 |
def load_safetensors(path):
|
| 10 |
tensors = {}
|
|
|
|
| 29 |
return checkpoint
|
| 30 |
|
| 31 |
def update_model_with_lora(checkpoint, lora_weights, transformer, cond_size, device="cpu"):
|
| 32 |
+
number = len(lora_weights)
|
| 33 |
+
ranks = [get_lora_rank(checkpoint) for _ in range(number)]
|
| 34 |
+
lora_attn_procs = {}
|
| 35 |
+
double_blocks_idx = list(range(19))
|
| 36 |
+
single_blocks_idx = list(range(38))
|
| 37 |
+
for name, attn_processor in transformer.attn_processors.items():
|
| 38 |
+
match = re.search(r'\.(\d+)\.', name)
|
| 39 |
+
if match:
|
| 40 |
+
layer_index = int(match.group(1))
|
| 41 |
+
|
| 42 |
+
if name.startswith("transformer_blocks") and layer_index in double_blocks_idx:
|
| 43 |
+
lora_state_dicts = {}
|
| 44 |
+
for key, value in checkpoint.items():
|
| 45 |
+
if re.search(r'\.(\d+)\.', key):
|
| 46 |
+
checkpoint_layer_index = int(re.search(r'\.(\d+)\.', key).group(1))
|
| 47 |
+
if checkpoint_layer_index == layer_index and key.startswith("transformer_blocks"):
|
| 48 |
+
lora_state_dicts[key] = value
|
| 49 |
+
|
| 50 |
+
lora_attn_procs[name] = MultiDoubleStreamBlockLoraProcessor(
|
| 51 |
+
dim=3072,
|
| 52 |
+
ranks=ranks,
|
| 53 |
+
network_alphas=ranks,
|
| 54 |
+
lora_weights=lora_weights,
|
| 55 |
+
device=device, # 使用传入的 device 参数
|
| 56 |
+
dtype=torch.bfloat16,
|
| 57 |
+
cond_width=cond_size,
|
| 58 |
+
cond_height=cond_size,
|
| 59 |
+
n_loras=number
|
| 60 |
+
)
|
| 61 |
|
| 62 |
+
# Load weights and move to specified device
|
| 63 |
+
for n in range(number):
|
| 64 |
+
lora_attn_procs[name].q_loras[n].down.weight.data = lora_state_dicts.get(f'{name}.q_loras.{n}.down.weight', None)
|
| 65 |
+
lora_attn_procs[name].q_loras[n].up.weight.data = lora_state_dicts.get(f'{name}.q_loras.{n}.up.weight', None)
|
| 66 |
+
lora_attn_procs[name].k_loras[n].down.weight.data = lora_state_dicts.get(f'{name}.k_loras.{n}.down.weight', None)
|
| 67 |
+
lora_attn_procs[name].k_loras[n].up.weight.data = lora_state_dicts.get(f'{name}.k_loras.{n}.up.weight', None)
|
| 68 |
+
lora_attn_procs[name].v_loras[n].down.weight.data = lora_state_dicts.get(f'{name}.v_loras.{n}.down.weight', None)
|
| 69 |
+
lora_attn_procs[name].v_loras[n].up.weight.data = lora_state_dicts.get(f'{name}.v_loras.{n}.up.weight', None)
|
| 70 |
+
lora_attn_procs[name].proj_loras[n].down.weight.data = lora_state_dicts.get(f'{name}.proj_loras.{n}.down.weight', None)
|
| 71 |
+
lora_attn_procs[name].proj_loras[n].up.weight.data = lora_state_dicts.get(f'{name}.proj_loras.{n}.up.weight', None)
|
| 72 |
+
lora_attn_procs[name].to(device) # 使用传入的 device
|
| 73 |
+
|
| 74 |
+
elif name.startswith("single_transformer_blocks") and layer_index in single_blocks_idx:
|
| 75 |
+
lora_state_dicts = {}
|
| 76 |
+
for key, value in checkpoint.items():
|
| 77 |
+
if re.search(r'\.(\d+)\.', key):
|
| 78 |
+
checkpoint_layer_index = int(re.search(r'\.(\d+)\.', key).group(1))
|
| 79 |
+
if checkpoint_layer_index == layer_index and key.startswith("single_transformer_blocks"):
|
| 80 |
+
lora_state_dicts[key] = value
|
| 81 |
+
|
| 82 |
+
lora_attn_procs[name] = MultiSingleStreamBlockLoraProcessor(
|
| 83 |
+
dim=3072,
|
| 84 |
+
ranks=ranks,
|
| 85 |
+
network_alphas=ranks,
|
| 86 |
+
lora_weights=lora_weights,
|
| 87 |
+
device=device, # 使用传入的 device 参数
|
| 88 |
+
dtype=torch.bfloat16,
|
| 89 |
+
cond_width=cond_size,
|
| 90 |
+
cond_height=cond_size,
|
| 91 |
+
n_loras=number
|
| 92 |
+
)
|
| 93 |
+
# Load weights and move to specified device
|
| 94 |
+
for n in range(number):
|
| 95 |
+
lora_attn_procs[name].q_loras[n].down.weight.data = lora_state_dicts.get(f'{name}.q_loras.{n}.down.weight', None)
|
| 96 |
+
lora_attn_procs[name].q_loras[n].up.weight.data = lora_state_dicts.get(f'{name}.q_loras.{n}.up.weight', None)
|
| 97 |
+
lora_attn_procs[name].k_loras[n].down.weight.data = lora_state_dicts.get(f'{name}.k_loras.{n}.down.weight', None)
|
| 98 |
+
lora_attn_procs[name].k_loras[n].up.weight.data = lora_state_dicts.get(f'{name}.k_loras.{n}.up.weight', None)
|
| 99 |
+
lora_attn_procs[name].v_loras[n].down.weight.data = lora_state_dicts.get(f'{name}.v_loras.{n}.down.weight', None)
|
| 100 |
+
lora_attn_procs[name].v_loras[n].up.weight.data = lora_state_dicts.get(f'{name}.v_loras.{n}.up.weight', None)
|
| 101 |
+
lora_attn_procs[name].to(device) # 使用传入的 device
|
| 102 |
+
else:
|
| 103 |
+
lora_attn_procs[name] = FluxAttnProcessor2_0()
|
| 104 |
+
|
| 105 |
+
transformer.set_attn_processor(lora_attn_procs)
|
| 106 |
+
|
| 107 |
+
def update_model_with_multi_lora(checkpoints, lora_weights, transformer, cond_size, device="cpu"): # 顺便更新此函数
|
| 108 |
+
ck_number = len(checkpoints)
|
| 109 |
+
cond_lora_number = [len(ls) for ls in lora_weights]
|
| 110 |
+
cond_number = sum(cond_lora_number)
|
| 111 |
+
ranks = [get_lora_rank(checkpoint) for checkpoint in checkpoints]
|
| 112 |
+
multi_lora_weight = []
|
| 113 |
+
for ls in lora_weights:
|
| 114 |
+
for n in ls:
|
| 115 |
+
multi_lora_weight.append(n)
|
| 116 |
+
|
| 117 |
+
lora_attn_procs = {}
|
| 118 |
+
double_blocks_idx = list(range(19))
|
| 119 |
+
single_blocks_idx = list(range(38))
|
| 120 |
+
for name, attn_processor in transformer.attn_processors.items():
|
| 121 |
+
match = re.search(r'\.(\d+)\.', name)
|
| 122 |
+
if match:
|
| 123 |
+
layer_index = int(match.group(1))
|
| 124 |
+
|
| 125 |
+
if name.startswith("transformer_blocks") and layer_index in double_blocks_idx:
|
| 126 |
+
lora_state_dicts = [{} for _ in range(ck_number)]
|
| 127 |
+
for idx, checkpoint in enumerate(checkpoints):
|
| 128 |
for key, value in checkpoint.items():
|
|
|
|
| 129 |
if re.search(r'\.(\d+)\.', key):
|
| 130 |
checkpoint_layer_index = int(re.search(r'\.(\d+)\.', key).group(1))
|
| 131 |
if checkpoint_layer_index == layer_index and key.startswith("transformer_blocks"):
|
| 132 |
+
lora_state_dicts[idx][key] = value
|
| 133 |
+
|
| 134 |
+
lora_attn_procs[name] = MultiDoubleStreamBlockLoraProcessor(
|
| 135 |
+
dim=3072,
|
| 136 |
+
ranks=ranks,
|
| 137 |
+
network_alphas=ranks,
|
| 138 |
+
lora_weights=multi_lora_weight,
|
| 139 |
+
device=device, # 使用传入的 device 参数
|
| 140 |
+
dtype=torch.bfloat16,
|
| 141 |
+
cond_width=cond_size,
|
| 142 |
+
cond_height=cond_size,
|
| 143 |
+
n_loras=cond_number
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
num = 0
|
| 147 |
+
for idx in range(ck_number):
|
| 148 |
+
for n in range(cond_lora_number[idx]):
|
| 149 |
+
lora_attn_procs[name].q_loras[num].down.weight.data = lora_state_dicts[idx].get(f'{name}.q_loras.{n}.down.weight', None)
|
| 150 |
+
lora_attn_procs[name].q_loras[num].up.weight.data = lora_state_dicts[idx].get(f'{name}.q_loras.{n}.up.weight', None)
|
| 151 |
+
lora_attn_procs[name].k_loras[num].down.weight.data = lora_state_dicts[idx].get(f'{name}.k_loras.{n}.down.weight', None)
|
| 152 |
+
lora_attn_procs[name].k_loras[num].up.weight.data = lora_state_dicts[idx].get(f'{name}.k_loras.{n}.up.weight', None)
|
| 153 |
+
lora_attn_procs[name].v_loras[num].down.weight.data = lora_state_dicts[idx].get(f'{name}.v_loras.{n}.down.weight', None)
|
| 154 |
+
lora_attn_procs[name].v_loras[num].up.weight.data = lora_state_dicts[idx].get(f'{name}.v_loras.{n}.up.weight', None)
|
| 155 |
+
lora_attn_procs[name].proj_loras[num].down.weight.data = lora_state_dicts[idx].get(f'{name}.proj_loras.{n}.down.weight', None)
|
| 156 |
+
lora_attn_procs[name].proj_loras[num].up.weight.data = lora_state_dicts[idx].get(f'{name}.proj_loras.{n}.up.weight', None)
|
| 157 |
+
lora_attn_procs[name].to(device) # 使用传入的 device
|
| 158 |
+
num += 1
|
| 159 |
+
|
| 160 |
+
elif name.startswith("single_transformer_blocks") and layer_index in single_blocks_idx:
|
| 161 |
+
lora_state_dicts = [{} for _ in range(ck_number)]
|
| 162 |
+
for idx, checkpoint in enumerate(checkpoints):
|
| 163 |
for key, value in checkpoint.items():
|
|
|
|
| 164 |
if re.search(r'\.(\d+)\.', key):
|
| 165 |
checkpoint_layer_index = int(re.search(r'\.(\d+)\.', key).group(1))
|
| 166 |
if checkpoint_layer_index == layer_index and key.startswith("single_transformer_blocks"):
|
| 167 |
+
lora_state_dicts[idx][key] = value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
+
lora_attn_procs[name] = MultiSingleStreamBlockLoraProcessor(
|
| 170 |
+
dim=3072,
|
| 171 |
+
ranks=ranks,
|
| 172 |
+
network_alphas=ranks,
|
| 173 |
+
lora_weights=multi_lora_weight,
|
| 174 |
+
device=device, # 使用传入的 device 参数
|
| 175 |
+
dtype=torch.bfloat16,
|
| 176 |
+
cond_width=cond_size,
|
| 177 |
+
cond_height=cond_size,
|
| 178 |
+
n_loras=cond_number
|
| 179 |
+
)
|
| 180 |
+
num = 0
|
| 181 |
+
for idx in range(ck_number):
|
| 182 |
+
for n in range(cond_lora_number[idx]):
|
| 183 |
+
lora_attn_procs[name].q_loras[num].down.weight.data = lora_state_dicts[idx].get(f'{name}.q_loras.{n}.down.weight', None)
|
| 184 |
+
lora_attn_procs[name].q_loras[num].up.weight.data = lora_state_dicts[idx].get(f'{name}.q_loras.{n}.up.weight', None)
|
| 185 |
+
lora_attn_procs[name].k_loras[num].down.weight.data = lora_state_dicts[idx].get(f'{name}.k_loras.{n}.down.weight', None)
|
| 186 |
+
lora_attn_procs[name].k_loras[num].up.weight.data = lora_state_dicts[idx].get(f'{name}.k_loras.{n}.up.weight', None)
|
| 187 |
+
lora_attn_procs[name].v_loras[num].down.weight.data = lora_state_dicts[idx].get(f'{name}.v_loras.{n}.down.weight', None)
|
| 188 |
+
lora_attn_procs[name].v_loras[num].up.weight.data = lora_state_dicts[idx].get(f'{name}.v_loras.{n}.up.weight', None)
|
| 189 |
+
lora_attn_procs[name].to(device) # 使用传入的 device
|
| 190 |
+
num += 1
|
| 191 |
+
else:
|
| 192 |
+
lora_attn_procs[name] = FluxAttnProcessor2_0()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
+
transformer.set_attn_processor(lora_attn_procs)
|
| 195 |
|
| 196 |
+
def set_single_lora(transformer, local_path, lora_weights=[], cond_size=512, device="cpu"):
|
| 197 |
checkpoint = load_checkpoint(local_path)
|
| 198 |
+
update_model_with_lora(checkpoint, lora_weights, transformer, cond_size, device=device)
|
| 199 |
+
|
| 200 |
+
def set_multi_lora(transformer, local_paths, lora_weights=[[]], cond_size=512, device="cpu"): # 顺便更新此函数
|
| 201 |
checkpoints = [load_checkpoint(local_path) for local_path in local_paths]
|
| 202 |
+
update_model_with_multi_lora(checkpoints, lora_weights, transformer, cond_size, device=device)
|
| 203 |
|
| 204 |
def unset_lora(transformer):
|
| 205 |
lora_attn_procs = {}
|
| 206 |
for name, attn_processor in transformer.attn_processors.items():
|
| 207 |
lora_attn_procs[name] = FluxAttnProcessor2_0()
|
| 208 |
+
transformer.set_attn_processor(lora_attn_procs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|