-
Notifications
You must be signed in to change notification settings - Fork 152
/
batch_hcp_convert.py
429 lines (386 loc) · 15.1 KB
/
batch_hcp_convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import os
import math
import argparse
from typing import List
from collections import defaultdict
from hcpdiff.ckpt_manager import auto_manager
class LoraConverter(object):
com_name_unet = [
"down_blocks",
"up_blocks",
"mid_block",
"transformer_blocks",
"to_q",
"to_k",
"to_v",
"to_out",
"proj_in",
"proj_out",
"input_blocks",
"middle_block",
"output_blocks",
]
com_name_TE = ["self_attn", "q_proj", "v_proj", "k_proj", "out_proj", "text_model"]
prefix_unet = "lora_unet_"
prefix_TE = "lora_te_"
def __init__(self):
self.com_name_unet_tmp = [x.replace("_", "%") for x in self.com_name_unet]
self.com_name_TE_tmp = [x.replace("_", "%") for x in self.com_name_TE]
def convert_from_webui(
self, state, network_type="lora", auto_scale_alpha=False, sdxl=False
):
assert network_type in ["lora", "plugin"]
if not sdxl:
sd_unet = self.convert_from_webui_(
state,
network_type=network_type,
prefix=self.prefix_unet,
com_name=self.com_name_unet,
com_name_tmp=self.com_name_unet_tmp,
)
sd_te = self.convert_from_webui_(
state,
network_type=network_type,
prefix=self.prefix_TE,
com_name=self.com_name_TE,
com_name_tmp=self.com_name_TE_tmp,
)
else:
sd_unet = self.convert_from_webui_xl_unet_(
state,
network_type=network_type,
prefix=self.prefix_unet,
com_name=self.com_name_unet,
com_name_tmp=self.com_name_unet_tmp,
)
sd_te = self.convert_from_webui_xl_te_(
state,
network_type=network_type,
prefix=self.prefix_TE_xl_clip_B,
com_name=self.com_name_TE,
com_name_tmp=self.com_name_TE_tmp,
)
sd_te2 = self.convert_from_webui_xl_te_(
state,
network_type=network_type,
prefix=self.prefix_TE_xl_clip_bigG,
com_name=self.com_name_TE,
com_name_tmp=self.com_name_TE_tmp,
)
sd_te.update(sd_te2)
if auto_scale_alpha and network_type == "lora":
sd_unet = self.alpha_scale_from_webui(sd_unet)
sd_te = self.alpha_scale_from_webui(sd_te)
return {network_type: sd_te}, {network_type: sd_unet}
def convert_to_webui(
self, sd_unet, sd_te, network_type="lora", auto_scale_alpha=False, sdxl=False
):
assert network_type in ["lora", "plugin"]
sd_unet = self.convert_to_webui_(
sd_unet, network_type=network_type, prefix=self.prefix_unet
)
if sdxl:
sd_te = self.convert_to_webui_xl_(
sd_te, network_type=network_type, prefix=self.prefix_TE
)
else:
sd_te = self.convert_to_webui_(
sd_te, network_type=network_type, prefix=self.prefix_TE
)
sd_unet.update(sd_te)
if auto_scale_alpha and network_type == "lora":
sd_unet = self.alpha_scale_to_webui(sd_unet)
return sd_unet
def convert_from_webui_(self, state, network_type, prefix, com_name, com_name_tmp):
state = {k: v for k, v in state.items() if k.startswith(prefix)}
prefix_len = len(prefix)
sd_covert = {}
for k, v in state.items():
model_k, lora_k = k[prefix_len:].split(".", 1)
model_k = (
self.replace_all(model_k, com_name, com_name_tmp)
.replace("_", ".")
.replace("%", "_")
)
if lora_k == "alpha" or network_type == "plugin":
sd_covert[f"{model_k}.___.{lora_k}"] = v
else:
sd_covert[f"{model_k}.___.layer.{lora_k}"] = v
return sd_covert
def convert_to_webui_(self, state, network_type, prefix):
sd_covert = {}
for k, v in state.items():
if network_type == "plugin" or "alpha" in k or "scale" in k:
separator = ".___."
else:
separator = ".___.layer."
model_k, lora_k = k.split(separator, 1)
sd_covert[f"{prefix}{model_k.replace('.', '_')}.{lora_k}"] = v
return sd_covert
def convert_to_webui_xl_(self, state, network_type, prefix):
sd_convert = {}
for k, v in state.items():
if network_type == "plugin" or "alpha" in k or "scale" in k:
separator = ".___."
else:
separator = ".___.layer."
model_k, lora_k = k.split(separator, 1)
new_k = f"{prefix}{model_k.replace('.', '_')}.{lora_k}"
if "clip" in new_k:
new_k = (
new_k.replace("_clip_B", "1")
if "clip_B" in new_k
else new_k.replace("_clip_bigG", "2")
)
sd_convert[new_k] = v
return sd_convert
def convert_from_webui_xl_te_(
self, state, network_type, prefix, com_name, com_name_tmp
):
state = {k: v for k, v in state.items() if k.startswith(prefix)}
sd_covert = {}
prefix_len = len(prefix)
for k, v in state.items():
model_k, lora_k = k[prefix_len:].split(".", 1)
model_k = (
self.replace_all(model_k, com_name, com_name_tmp)
.replace("_", ".")
.replace("%", "_")
)
if prefix == "lora_te1_":
model_k = f"clip_B.{model_k}"
else:
model_k = f"clip_bigG.{model_k}"
if lora_k == "alpha" or network_type == "plugin":
sd_covert[f"{model_k}.___.{lora_k}"] = v
else:
sd_covert[f"{model_k}.___.layer.{lora_k}"] = v
return sd_covert
def convert_from_webui_xl_unet_(
self, state, network_type, prefix, com_name, com_name_tmp
):
# Down:
# 4 -> 1, 0 4 = 1 + 3 * 1 + 0
# 5 -> 1, 1 5 = 1 + 3 * 1 + 1
# 7 -> 2, 0 7 = 1 + 3 * 2 + 0
# 8 -> 2, 1 8 = 1 + 3 * 2 + 1
# Up
# 0 -> 0, 0 0 = 0 * 3 + 0
# 1 -> 0, 1 1 = 0 * 3 + 1
# 2 -> 0, 2 2 = 0 * 3 + 2
# 3 -> 1, 0 3 = 1 * 3 + 0
# 4 -> 1, 1 4 = 1 * 3 + 1
# 5 -> 1, 2 5 = 1 * 3 + 2
down = {
"4": [1, 0],
"5": [1, 1],
"7": [2, 0],
"8": [2, 1],
}
up = {
"0": [0, 0],
"1": [0, 1],
"2": [0, 2],
"3": [1, 0],
"4": [1, 1],
"5": [1, 2],
}
import re
m = []
def match(key, regex_text):
regex = re.compile(regex_text)
r = re.match(regex, key)
if not r:
return False
m.clear()
m.extend(r.groups())
return True
state = {k: v for k, v in state.items() if k.startswith(prefix)}
sd_covert = {}
prefix_len = len(prefix)
for k, v in state.items():
model_k, lora_k = k[prefix_len:].split(".", 1)
model_k = (
self.replace_all(model_k, com_name, com_name_tmp)
.replace("_", ".")
.replace("%", "_")
)
if match(model_k, r"input_blocks.(\d+).1.(.+)"):
new_k = (
f"down_blocks.{down[m[0]][0]}.attentions" f".{down[m[0]][1]}.{m[1]}"
)
elif match(model_k, r"middle_block.1.(.+)"):
new_k = f"mid_block.attentions.0.{m[0]}"
pass
elif match(model_k, r"output_blocks.(\d+).(\d+).(.+)"):
new_k = f"up_blocks.{up[m[0]][0]}.attentions" f".{up[m[0]][1]}.{m[2]}"
else:
raise NotImplementedError
if lora_k == "alpha" or network_type == "plugin":
sd_covert[f"{new_k}.___.{lora_k}"] = v
else:
sd_covert[f"{new_k}.___.layer.{lora_k}"] = v
return sd_covert
@staticmethod
def replace_all(data: str, srcs: List[str], dsts: List[str]):
for src, dst in zip(srcs, dsts):
data = data.replace(src, dst)
return data
@staticmethod
def alpha_scale_from_webui(state):
# Apply to "lora_down" and "lora_up" respectively to prevent overflow
for k, v in state.items():
if "lora_up" in k:
state[k] = v * math.sqrt(v.shape[1])
elif "lora_down" in k:
state[k] = v * math.sqrt(v.shape[0])
return state
@staticmethod
def alpha_scale_to_webui(state):
for k, v in state.items():
if "lora_up" in k:
state[k] = v * math.sqrt(v.shape[1])
elif "lora_down" in k:
state[k] = v * math.sqrt(v.shape[0])
return state
def save_and_print_path(sd, path):
os.makedirs(args.dump_path, exist_ok=True)
ckpt_manager._save_ckpt(sd, save_path=path)
print("Saved to:", path)
def get_unet_te_pairs(lora_path):
file_pairs = defaultdict(lambda: {"TE": None, "unet": None})
for filename in os.listdir(lora_path):
if filename.endswith(".safetensors"):
parts = os.path.splitext(filename)[0].split("-")
prefix, name = parts[0], "-".join(parts[1:])
if "text_encoder" in prefix:
file_pairs[name]["TE"] = os.path.join(args.lora_path, filename)
elif "unet" in prefix:
file_pairs[name]["unet"] = os.path.join(args.lora_path, filename)
return file_pairs
def get_network_type(sd_unet, sd_te):
if "lora" in sd_unet.keys() and "lora" in sd_te.keys():
return "lora"
elif "plugin" in sd_unet.keys() and "plugin" in sd_te.keys():
return "plugin"
else:
return None
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert LoRA models.")
parser.add_argument(
"--lora_path",
required=True,
type=str,
help="Path to the LoRA or folder containing LoRA models.",
)
parser.add_argument(
"--lora_path_TE", type=str, help="Path to the HCP Text Encoder LoRA."
)
parser.add_argument(
"--dump_path",
required=True,
type=str,
help="Path to save the converted state dict.",
)
parser.add_argument(
"--from_webui", action="store_true", help="Convert from webui format."
)
parser.add_argument(
"--save_network_type", type=str, help="Specify the network type for conversion."
)
parser.add_argument(
"--to_webui", action="store_true", help="Convert to webui format."
)
parser.add_argument(
"--output_prefix", default="", type=str, help="Prefix for output filenames."
)
parser.add_argument(
"--auto_scale_alpha", action="store_true", help="Automatically scale alpha."
)
parser.add_argument("--sdxl", action="store_true", help="Enable SDXL conversion.")
args = parser.parse_args()
converter = LoraConverter()
if os.path.isdir(args.lora_path):
ckpt_manager = auto_manager(".safetensors")()
if args.from_webui:
for filename in os.listdir(args.lora_path):
if filename.endswith(".safetensors"):
file_path = os.path.join(args.lora_path, filename)
print(f"Converting {file_path}")
state = ckpt_manager.load_ckpt(file_path)
sd_te, sd_unet = converter.convert_from_webui(
state,
network_type=args.save_network_type,
auto_scale_alpha=args.auto_scale_alpha,
sdxl=args.sdxl,
)
TE_path = os.path.join(args.dump_path, "text_encoder-" + filename)
unet_path = os.path.join(args.dump_path, "unet-" + filename)
save_and_print_path(sd_te, TE_path)
save_and_print_path(sd_unet, unet_path)
elif args.to_webui:
file_pairs = get_unet_te_pairs(args.lora_path)
for name, paths in file_pairs.items():
if paths["TE"] and paths["unet"]:
sd_unet = ckpt_manager.load_ckpt(paths["unet"])
sd_te = ckpt_manager.load_ckpt(paths["TE"])
network_type = get_network_type(sd_unet, sd_te)
if network_type is None:
print("no saved lora/lycoris found, skip")
continue
print(
f'Converting pair: {paths["TE"]} and {paths["unet"]}'
f' with key "{network_type}"'
)
state = converter.convert_to_webui(
sd_unet[network_type],
sd_te[network_type],
network_type=network_type,
auto_scale_alpha=args.auto_scale_alpha,
sdxl=args.sdxl,
)
output_path = os.path.join(
args.dump_path, f"{args.output_prefix}-{name}.safetensors"
)
save_and_print_path(state, output_path)
else:
print("Converting LoRA model")
ckpt_manager = auto_manager(args.lora_path)()
if args.from_webui:
state = ckpt_manager.load_ckpt(args.lora_path)
sd_te, sd_unet = converter.convert_from_webui(
state,
network_type=args.save_network_type,
auto_scale_alpha=args.auto_scale_alpha,
sdxl=args.sdxl,
)
TE_path = os.path.join(
args.dump_path, "text_encoder-" + os.path.basename(args.lora_path)
)
unet_path = os.path.join(
args.dump_path, "unet-" + os.path.basename(args.lora_path)
)
save_and_print_path(sd_te, TE_path)
save_and_print_path(sd_unet, unet_path)
elif args.to_webui:
sd_unet = ckpt_manager.load_ckpt(args.lora_path)
sd_te = ckpt_manager.load_ckpt(args.lora_path_TE)
network_type = get_network_type(sd_unet, sd_te)
if network_type is None:
print("no saved lora/lycoris found, terminating")
exit(1)
print(f'Converting with key "{network_type}"')
state = converter.convert_to_webui(
sd_unet[network_type],
sd_te[network_type],
network_type=network_type,
auto_scale_alpha=args.auto_scale_alpha,
sdxl=args.sdxl,
)
lora_name = os.path.basename(args.lora_path)
if "-" in lora_name:
lora_name = "-".join(lora_name.split("-")[1:])
output_path = os.path.join(
args.dump_path, args.output_prefix + "-" + lora_name
)
save_and_print_path(state, output_path)