MCPcopy Create free account
hub / github.com/MeiGen-AI/MultiTalk / standardize_lora_key_format

Function standardize_lora_key_format

wan/utils/utils.py:182–329  ·  view source on GitHub ↗
(lora_sd)

Source from the content-addressed store, hash-verified

180 return (sd, metadata) if return_metadata else sd
181
182def standardize_lora_key_format(lora_sd):
183 new_sd = {}
184 for k, v in lora_sd.items():
185 # Diffusers format
186 if k.startswith('transformer.'):
187 k = k.replace('transformer.', 'diffusion_model.')
188 if k.startswith('pipe.dit.'): #unianimate-dit/diffsynth
189 k = k.replace('pipe.dit.', 'diffusion_model.')
190
191 # Fun LoRA format
192 if k.startswith('lora_unet__'):
193 # Split into main path and weight type parts
194 parts = k.split('.')
195 main_part = parts[0] # e.g. lora_unet__blocks_0_cross_attn_k
196 weight_type = '.'.join(parts[1:]) if len(parts) > 1 else None # e.g. lora_down.weight
197
198 # Process the main part - convert from underscore to dot format
199 if 'blocks_' in main_part:
200 # Extract components
201 components = main_part[len('lora_unet__'):].split('_')
202
203 # Start with diffusion_model
204 new_key = "diffusion_model"
205
206 # Add blocks.N
207 if components[0] == 'blocks':
208 new_key += f".blocks.{components[1]}"
209
210 # Handle different module types
211 idx = 2
212 if idx < len(components):
213 if components[idx] == 'self' and idx+1 < len(components) and components[idx+1] == 'attn':
214 new_key += ".self_attn"
215 idx += 2
216 elif components[idx] == 'cross' and idx+1 < len(components) and components[idx+1] == 'attn':
217 new_key += ".cross_attn"
218 idx += 2
219 elif components[idx] == 'ffn':
220 new_key += ".ffn"
221 idx += 1
222
223 # Add the component (k, q, v, o) and handle img suffix
224 if idx < len(components):
225 component = components[idx]
226 idx += 1
227
228 # Check for img suffix
229 if idx < len(components) and components[idx] == 'img':
230 component += '_img'
231 idx += 1
232
233 new_key += f".{component}"
234
235 # Handle weight type - this is the critical fix
236 if weight_type:
237 if weight_type == 'alpha':
238 new_key += '.alpha'
239 elif weight_type == 'lora_down.weight' or weight_type == 'lora_down':

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected