11# For using the diffusers format weights
22# Based on the original ComfyUI function +
3- # https://github.com/PixArt-alpha/PixArt-alpha /blob/master /tools/convert_pixart_alpha_to_diffusers .py
3+ # https://github.com/NVlabs/Sana /blob/main /tools/convert_sana_to_diffusers .py
44import torch
55
6- conversion_map_ms = [ # for multi_scale_train (MS)
7- # Resolution
8- ("csize_embedder.mlp.0.weight" , "adaln_single.emb.resolution_embedder.linear_1.weight" ),
9- ("csize_embedder.mlp.0.bias" , "adaln_single.emb.resolution_embedder.linear_1.bias" ),
10- ("csize_embedder.mlp.2.weight" , "adaln_single.emb.resolution_embedder.linear_2.weight" ),
11- ("csize_embedder.mlp.2.bias" , "adaln_single.emb.resolution_embedder.linear_2.bias" ),
12- # Aspect ratio
13- ("ar_embedder.mlp.0.weight" , "adaln_single.emb.aspect_ratio_embedder.linear_1.weight" ),
14- ("ar_embedder.mlp.0.bias" , "adaln_single.emb.aspect_ratio_embedder.linear_1.bias" ),
15- ("ar_embedder.mlp.2.weight" , "adaln_single.emb.aspect_ratio_embedder.linear_2.weight" ),
16- ("ar_embedder.mlp.2.bias" , "adaln_single.emb.aspect_ratio_embedder.linear_2.bias" ),
17- ]
186
197def get_depth (state_dict ):
208 return sum (key .endswith ('.attn1.to_k.bias' ) for key in state_dict .keys ())
@@ -30,7 +18,7 @@ def get_lora_depth(state_dict):
3018 return cnt
3119
3220def get_conversion_map (state_dict ):
33- conversion_map = [ # main SD conversion map (PixArt reference, HF Diffusers)
21+ conversion_map = [ # main SD conversion map (Sana reference, HF Diffusers)
3422 # Patch embeddings
3523 ("x_embedder.proj.weight" , "pos_embed.proj.weight" ),
3624 ("x_embedder.proj.bias" , "pos_embed.proj.bias" ),
@@ -82,10 +70,7 @@ def find_prefix(state_dict, target_key):
8270 return prefix
8371
8472def convert_state_dict (state_dict ):
85- if "adaln_single.emb.resolution_embedder.linear_1.weight" in state_dict .keys ():
86- cmap = get_conversion_map (state_dict ) + conversion_map_ms
87- else :
88- cmap = get_conversion_map (state_dict )
73+ cmap = get_conversion_map (state_dict )
8974
9075 missing = [k for k ,v in cmap if v not in state_dict ]
9176 new_state_dict = {k : state_dict [v ] for k ,v in cmap if k not in missing }
@@ -109,16 +94,17 @@ def convert_state_dict(state_dict):
10994 matched += [key ('q' ), key ('k' ), key ('v' )]
11095
11196 if len (matched ) < len (state_dict ):
112- print (f"PixArt : UNET conversion has leftover keys! ({ len (matched )} vs { len (state_dict )} )" )
97+ print (f"Sana : UNET conversion has leftover keys! ({ len (matched )} vs { len (state_dict )} )" )
11398 print (list ( set (state_dict .keys ()) - set (matched ) ))
11499
115100 if len (missing ) > 0 :
116- print (f"PixArt : UNET conversion has missing keys!" )
101+ print (f"Sana : UNET conversion has missing keys!" )
117102 print (missing )
118103
119104 return new_state_dict
120105
121106# Same as above but for LoRA weights:
107+ # TODO: Not used yet, need to support LoRA for Sana
122108def convert_lora_state_dict (state_dict , peft = True ):
123109 # koyha
124110 rep_ak = lambda x : x .replace (".weight" , ".lora_down.weight" )
@@ -137,18 +123,18 @@ def convert_lora_state_dict(state_dict, peft=True):
137123 rep_pp = lambda x : x .replace ("." , "_" )[:- 7 ] + ".alpha"
138124
139125 prefix = "lora_transformer_"
140- t5_marker = "lora_te_encoder"
141- t5_keys = []
126+ gemma_marker = "lora_te_encoder"
127+ gemma_keys = []
142128 for key in list (state_dict .keys ()):
143129 if key .startswith (prefix ):
144130 state_dict [key [len (prefix ):]] = state_dict .pop (key )
145- elif t5_marker in key :
146- t5_keys .append (state_dict .pop (key ))
147- if len (t5_keys ) > 0 :
148- print (f"Text Encoder not supported for PixArt LoRA, ignoring { len (t5_keys )} keys" )
131+ elif gemma_marker in key :
132+ gemma_keys .append (state_dict .pop (key ))
133+ if len (gemma_keys ) > 0 :
134+ print (f"Text Encoder not supported for Sana LoRA, ignoring { len (gemma_keys )} keys" )
149135
150136 cmap = []
151- cmap_unet = get_conversion_map (state_dict ) + conversion_map_ms # todo: 512 model
137+ cmap_unet = get_conversion_map (state_dict ) # todo: 512 model
152138 for k , v in cmap_unet :
153139 if v .endswith (".weight" ):
154140 cmap .append ((rep_ak (k ), rep_ap (v )))
@@ -213,11 +199,11 @@ def convert_lora_state_dict(state_dict, peft=True):
213199 pass
214200
215201 if len (matched ) < len (state_dict ):
216- print (f"PixArt : LoRA conversion has leftover keys! ({ len (matched )} vs { len (state_dict )} )" )
202+ print (f"Sana : LoRA conversion has leftover keys! ({ len (matched )} vs { len (state_dict )} )" )
217203 print (list ( set (state_dict .keys ()) - set (matched ) ))
218204
219205 if len (missing ) > 0 :
220- print (f"PixArt : LoRA conversion has missing keys! (probably)" )
206+ print (f"Sana : LoRA conversion has missing keys! (probably)" )
221207 print (missing )
222208
223209 return new_state_dict
0 commit comments