hashu786 commited on
Commit
b9fcecb
·
verified ·
1 Parent(s): d60046f

Upload 2 files

Browse files

added convert script and test lora

Files changed (2) hide show
  1. convert_lora.py +164 -0
  2. wf_0400.safetensors +3 -0
convert_lora.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import os
3
+ from collections import defaultdict
4
+ from pathlib import Path
5
+
6
+ import torch
7
+ from safetensors.torch import load_file, save_file
8
+
9
+
10
+ def convert_diffusers_to_hunyuan_video_lora(diffusers_state_dict):
11
+ converted_state_dict = {k: diffusers_state_dict.pop(k) for k in list(diffusers_state_dict.keys())}
12
+
13
+ TRANSFORMER_KEYS_RENAME_DICT = {
14
+ "img_in": "x_embedder",
15
+ "time_in.mlp.0": "time_text_embed.timestep_embedder.linear_1",
16
+ "time_in.mlp.2": "time_text_embed.timestep_embedder.linear_2",
17
+ "guidance_in.mlp.0": "time_text_embed.guidance_embedder.linear_1",
18
+ "guidance_in.mlp.2": "time_text_embed.guidance_embedder.linear_2",
19
+ "vector_in.in_layer": "time_text_embed.text_embedder.linear_1",
20
+ "vector_in.out_layer": "time_text_embed.text_embedder.linear_2",
21
+ ".double_blocks": ".transformer_blocks",
22
+ ".single_blocks": ".single_transformer_blocks",
23
+ "img_attn_q_norm": "attn.norm_q",
24
+ "img_attn_k_norm": "attn.norm_k",
25
+ "img_attn_proj": "attn.to_out.0",
26
+ "txt_attn_q_norm": "attn.norm_added_q",
27
+ "txt_attn_k_norm": "attn.norm_added_k",
28
+ "txt_attn_proj": "attn.to_add_out",
29
+ "img_mod.linear": "norm1.linear",
30
+ "img_norm1": "norm1.norm",
31
+ "img_norm2": "norm2",
32
+ "txt_mlp": "ff_context",
33
+ "img_mlp": "ff",
34
+ "txt_mod.linear": "norm1_context.linear",
35
+ "txt_norm1": "norm1.norm",
36
+ "txt_norm2": "norm2_context",
37
+ "modulation.linear": "norm.linear",
38
+ "pre_norm": "norm.norm",
39
+ "final_layer.norm_final": "norm_out.norm",
40
+ "final_layer.linear": "proj_out",
41
+ # "linear2": "proj_out",
42
+ "fc1": "net.0.proj",
43
+ "fc2": "net.2",
44
+ "input_embedder": "proj_in",
45
+ # txt_in
46
+ "individual_token_refiner.blocks": "token_refiner.refiner_blocks",
47
+ "final_layer.adaLN_modulation.1": "norm_out.linear",
48
+ # "t_embedder.mlp.0": "time_text_embed.timestep_embedder.linear_1",
49
+ # "t_embedder.mlp.2": "time_text_embed.timestep_embedder.linear_2",
50
+ "c_embedder": "time_text_embed.text_embedder",
51
+ "txt_in": "context_embedder",
52
+ # "mlp": "ff",
53
+ }
54
+
55
+ TRANSFORMER_KEYS_RENAME_DICT_REVERSE = {v: k for k, v in TRANSFORMER_KEYS_RENAME_DICT.items()}
56
+
57
+ for key in list(converted_state_dict.keys()):
58
+ if "norm_out.linear" in key:
59
+ weight = converted_state_dict.pop(key)
60
+ scale, shift = weight.chunk(2, dim=0)
61
+ new_weight = torch.cat([shift, scale], dim=0)
62
+ converted_state_dict[key] = new_weight
63
+
64
+ if "to_q" in key:
65
+ if "single_transformer_blocks" in key:
66
+ to_q = converted_state_dict.pop(key)
67
+ to_k = converted_state_dict.pop(key.replace("to_q", "to_k"))
68
+ to_v = converted_state_dict.pop(key.replace("to_q", "to_v"))
69
+ to_out = converted_state_dict.pop(key.replace("attn.to_q", "proj_mlp"))
70
+ rename_attn_key = "linear1"
71
+ if "lora_A" in key:
72
+ converted_state_dict[key.replace("attn.to_q", rename_attn_key)] = to_q
73
+ else:
74
+ qkv_mlp = torch.cat([to_q, to_k, to_v, to_out], dim=0)
75
+ converted_state_dict[key.replace("attn.to_q", rename_attn_key)] = qkv_mlp
76
+ else:
77
+ to_q = converted_state_dict.pop(key)
78
+ to_k = converted_state_dict.pop(key.replace("to_q", "to_k"))
79
+ to_v = converted_state_dict.pop(key.replace("to_q", "to_v"))
80
+ if "token_refiner" in key:
81
+ rename_attn_key = "self_attn_qkv"
82
+ if "lora_A" in key:
83
+ converted_state_dict[key.replace("attn.to_q", rename_attn_key)] = to_q
84
+ else:
85
+ qkv = torch.cat([to_q, to_k, to_v], dim=0)
86
+ converted_state_dict[key.replace("attn.to_q", rename_attn_key)] = qkv
87
+ else:
88
+ rename_attn_key = "img_attn_qkv"
89
+ if "lora_A" in key:
90
+ converted_state_dict[key.replace("attn.to_q", rename_attn_key)] = to_q
91
+ else:
92
+ qkv = torch.cat([to_q, to_k, to_v], dim=0)
93
+ converted_state_dict[key.replace("attn.to_q", rename_attn_key)] = qkv
94
+
95
+ if "add_q_proj" in key:
96
+ to_q = converted_state_dict.pop(key)
97
+ to_k = converted_state_dict.pop(key.replace("add_q_proj", "add_k_proj"))
98
+ to_v = converted_state_dict.pop(key.replace("add_q_proj", "add_v_proj"))
99
+ rename_attn_key = "txt_attn_qkv"
100
+ if "lora_A" in key:
101
+ converted_state_dict[key.replace("attn.add_q_proj", rename_attn_key)] = to_q
102
+ else:
103
+ qkv = torch.cat([to_q, to_k, to_v], dim=0)
104
+ converted_state_dict[key.replace("attn.add_q_proj", rename_attn_key)] = qkv
105
+
106
+ for key in list(converted_state_dict.keys()):
107
+ new_key = key[:]
108
+ if "token_refiner" in key and "attn.to_out.0" in new_key:
109
+ new_key = new_key.replace("attn.to_out.0", "self_attn_proj")
110
+ if "token_refiner" in key and "ff" in new_key:
111
+ new_key = new_key.replace("ff", "mlp")
112
+ if "token_refiner" in key and "norm_out.linear" in new_key:
113
+ new_key = new_key.replace("norm_out.linear", "adaLN_modulation.1")
114
+ if "context_embedder" in key and "time_text_embed.text_embedder.linear_1" in new_key:
115
+ new_key = new_key.replace("time_text_embed.text_embedder.linear_1", "c_embedder.linear_1")
116
+ if "context_embedder" in key and "time_text_embed.text_embedder.linear_2" in new_key:
117
+ new_key = new_key.replace("time_text_embed.text_embedder.linear_2", "c_embedder.linear_2")
118
+ if "context_embedder" in key and "time_text_embed.timestep_embedder.linear_1" in new_key:
119
+ new_key = new_key.replace("time_text_embed.timestep_embedder.linear_1", "t_embedder.mlp.0")
120
+ if "context_embedder" in key and "time_text_embed.timestep_embedder.linear_2" in new_key:
121
+ new_key = new_key.replace("time_text_embed.timestep_embedder.linear_2", "t_embedder.mlp.2")
122
+ if "single_transformer_blocks" in key and "proj_out" in new_key:
123
+ new_key = new_key.replace("proj_out", "linear2")
124
+ for replace_key, rename_key in TRANSFORMER_KEYS_RENAME_DICT_REVERSE.items():
125
+ new_key = new_key.replace(replace_key, rename_key)
126
+ converted_state_dict[new_key] = converted_state_dict.pop(key)
127
+
128
+ # Remove "transformer." prefix
129
+ for key in list(converted_state_dict.keys()):
130
+ if key.startswith("transformer."):
131
+ converted_state_dict[key[len("transformer."):]] = converted_state_dict.pop(key)
132
+
133
+ # Add back "diffusion_model." prefix
134
+ for key in list(converted_state_dict.keys()):
135
+ converted_state_dict[f"diffusion_model.{key}"] = converted_state_dict.pop(key)
136
+
137
+ return converted_state_dict
138
+
139
+
140
+ def get_args():
141
+ parser = argparse.ArgumentParser()
142
+ parser.add_argument("--ckpt_path", type=str, required=True)
143
+ parser.add_argument("--output_path_or_name", type=str, required=True)
144
+ return parser.parse_args()
145
+
146
+
147
+ if __name__ == "__main__":
148
+ args = get_args()
149
+
150
+ if args.ckpt_path.endswith(".pt"):
151
+ diffusers_state_dict = torch.load(args.ckpt_path, map_location="cpu", weights_only=True)
152
+ elif args.ckpt_path.endswith(".safetensors"):
153
+ diffusers_state_dict = load_file(args.ckpt_path)
154
+
155
+ original_format_state_dict = convert_diffusers_to_hunyuan_video_lora(diffusers_state_dict)
156
+
157
+ output_path_or_name = Path(args.output_path_or_name)
158
+ if output_path_or_name.as_posix().endswith(".safetensors"):
159
+ os.makedirs(output_path_or_name.parent, exist_ok=True)
160
+ save_file(original_format_state_dict, output_path_or_name)
161
+ else:
162
+ os.makedirs(output_path_or_name, exist_ok=True)
163
+ output_path_or_name = output_path_or_name / "pytorch_lora_weights.safetensors"
164
+ save_file(original_format_state_dict, output_path_or_name)
wf_0400.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:474982b3da41876ed4a28e252b4b58ea69e558f4019accef5c962e9876926a3b
3
+ size 374925440