@@ -140,16 +140,50 @@ def _get_lora_layer_values(layer_dict: dict[str, torch.Tensor], alpha: float | N
140140
141141
142142def _group_by_layer (state_dict : Dict [str , torch .Tensor ]) -> dict [str , dict [str , torch .Tensor ]]:
143- """Groups the keys in the state dict by layer."""
143+ """Groups the keys in the state dict by layer.
144+
145+ Z-Image LoRAs have keys like:
146+ - diffusion_model.layers.17.attention.to_k.alpha
147+ - diffusion_model.layers.17.attention.to_k.dora_scale
148+ - diffusion_model.layers.17.attention.to_k.lora_down.weight
149+ - diffusion_model.layers.17.attention.to_k.lora_up.weight
150+
151+ We need to group these by the full layer path (e.g., diffusion_model.layers.17.attention.to_k)
152+ and extract the suffix (alpha, dora_scale, lora_down.weight, lora_up.weight).
153+ """
144154 layer_dict : dict [str , dict [str , torch .Tensor ]] = {}
155+
156+ # Known suffixes that indicate the end of a layer name
157+ known_suffixes = [
158+ ".lora_A.weight" ,
159+ ".lora_B.weight" ,
160+ ".lora_down.weight" ,
161+ ".lora_up.weight" ,
162+ ".dora_scale" ,
163+ ".alpha" ,
164+ ]
165+
145166 for key in state_dict :
146167 if not isinstance (key , str ):
147168 continue
148- # Split the 'lora_A.weight' or 'lora_B.weight' suffix from the layer name.
149- parts = key .rsplit ("." , maxsplit = 2 )
150- layer_name = parts [0 ]
151- key_name = "." .join (parts [1 :])
169+
170+ # Try to find a known suffix
171+ layer_name = None
172+ key_name = None
173+ for suffix in known_suffixes :
174+ if key .endswith (suffix ):
175+ layer_name = key [: - len (suffix )]
176+ key_name = suffix [1 :] # Remove leading dot
177+ break
178+
179+ if layer_name is None :
180+ # Fallback to original logic for unknown formats
181+ parts = key .rsplit ("." , maxsplit = 2 )
182+ layer_name = parts [0 ]
183+ key_name = "." .join (parts [1 :])
184+
152185 if layer_name not in layer_dict :
153186 layer_dict [layer_name ] = {}
154187 layer_dict [layer_name ][key_name ] = state_dict [key ]
188+
155189 return layer_dict
0 commit comments