o
    Gi                    @   s   d dl Z d dlZddlmZmZmZ eeZdd Z	d8dd	Z
d9ddZdd Zdd Zdd Zdd Zdd Zdedee defddZdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd:d*d+Zd:d,d-Zd:d.d/Zd0d1 Zd2d3 Zd4d5 Zd6d7 Z dS );    N   )is_peft_versionloggingstate_dict_all_zeroc                 C   s(   | j ddd\}}tj||gdd}|S )Nr   r   dim)chunktorchcat)weightshiftscale
new_weight r   [/home/ubuntu/.local/lib/python3.10/site-packages/diffusers/loaders/lora_conversion_utils.pyswap_scale_shift   s   r   _   c                    s  t  }g d}g d}d}d}|D ]tfdd|D r$d}qtfdd|D r1d}q|rM|rM|D ]tfdd|D rJ q8S d}	|D ]tfd	d|D rbd}	 nqQ|	sgS i }
g d
}t t t }}}|D ]F}d|v r||
|< qzt| d | d }|d |v r|| qz|d |v r|| qz|d |v r|| qztd| d fdd|D } fdd|D } fdd|D }|D ]a}|d |j	d  }|d |j	d  }|| D ]Ht | }dvr|| nd}dvrt
|nd}  d |d  t
|||g  |d d   }|
|< qq|D ]Y}d }|dkrU|d dg}n |dkra|d dg}n|dkrm|d dg}ntd| d|| D ]#  d |d  |  |d   }|
|< qyqE|D ]X}||j	d  }||j	d  }|| D ]Bt | }|| }|dk rt
|nd}  d |d  t
|||g  |d d   }|
|< qqrtd|
S )N)input_blocksmiddle_blockoutput_blocks)down_blocks	mid_block	up_blocksFc                 3       | ]}| v V  qd S Nr   .0pkeyr   r   	<genexpr>)       z5_maybe_map_sgm_blocks_to_diffusers.<locals>.<genexpr>Tc                 3   r   r   r   r   r   r   r   r!   +   r"   c                 3   r   r   r   r   r   r   r   r!   2   r"   c                 3   r   r   r   r   r   r   r   r!   9   r"   )resnets
attentions
upsamplerstextr      r   z'Checkpoint not supported because layer z not supported.c                    "   i | ]   fd dD qS )c                    "   g | ]}d    |v r|qS )r   r   r   r    	delimiterlayer_idr   r   
<listcomp>V      " A_maybe_map_sgm_blocks_to_diffusers.<locals>.<dictcomp>.<listcomp>r   r   r-   
state_dictr.   r   
<dictcomp>U       z6_maybe_map_sgm_blocks_to_diffusers.<locals>.<dictcomp>c                    r)   )c                    r*   )r   r   r+   r,   r   r   r/   Z   r0   r1   r   r2   r3   r5   r   r6   Y   r7   c                    r)   )c                    r*   )r   r   r+   r,   r   r   r/   ^   r0   r1   r   r2   r3   r5   r   r6   ]   r7   opdownsamplers01zInvalid middle block id .z:At this point all state dict entries have to be converted.)listkeysanypopsetintsplitadd
ValueErrorlayers_per_blockstrjoin)r4   unet_configr-   block_slice_posall_keyssgm_patternsnot_sgm_patternscontains_sgm_patternscontains_not_sgm_patternsis_in_sgm_formatnew_state_dictinner_block_mapinput_block_idsmiddle_block_idsoutput_block_idslayerr.   r   middle_blocksr   iblock_idlayer_in_block_idinner_block_idinner_block_keyinner_layers_in_blocknew_keykey_partr   )r-   r    r4   r   "_maybe_map_sgm_blocks_to_diffusers   s   



*r`   unettext_encoderc                    s  i }i }i }i }t dd | D }t dd | D }t dd | D }	|s)|s)|	r2tddr2tdt|  }
|
D ]}|dsBq:|d	d
 }|d }|d }|drt|}| 	|||< | 	|||
dd< |rd|v rtdnd}| 	|
dd||
|d< ni|drt||}|dr| 	|||< | 	|||
dd< n| 	|||< | 	|||
dd< |s|	rd|v rdnd}|dr| 	|
dd||
|d< n|dr| 	|
dd||
|d< || v r| 	| }|t||| q:t| d
krtdd|   td fdd| D } fdd| D }t|d
krCdd | D nd}|durO|| i ||}||fS ) a  
    Converts a non-Diffusers LoRA state dict to a Diffusers compatible state dict.

    Args:
        state_dict (`dict`): The state dict to convert.
        unet_name (`str`, optional): The name of the U-Net module in the Diffusers model. Defaults to "unet".
        text_encoder_name (`str`, optional): The name of the text encoder module in the Diffusers model. Defaults to
            "text_encoder".

    Returns:
        `tuple`: A tuple containing the converted state dict and a dictionary of alphas.
    c                 s        | ]}d |v od|v V  qdS )
dora_scale
lora_unet_Nr   r   kr   r   r   r!          z;_convert_non_diffusers_lora_to_diffusers.<locals>.<genexpr>c                 s   s(    | ]}d |v od|v pd|v V  qdS )rd   lora_te_	lora_te1_Nr   rf   r   r   r   r!      s   & c                 s   rc   )rd   	lora_te2_Nr   rf   r   r   r   r!      rh   <z0.9.0zeYou need `peft` 0.9.0 at least to use DoRA-enabled LoRAs. Please upgrade your installation of `peft`.lora_down.weightr<   r   .lora_up.weight.alphare   .down..up.z_lora.down.z.lora.down.rd   z.lora_magnitude_vector.)ri   rj   rk   ri   rj   .lora_linear_layer.rk   z7The following keys have not been correctly renamed: 

 , z"Non-diffusers checkpoint detected.c                        i | ]\}}  d | |qS r<   r   r   module_nameparams)	unet_namer   r   r6           z<_convert_non_diffusers_lora_to_diffusers.<locals>.<dictcomp>c                    ru   rv   r   rw   )text_encoder_namer   r   r6      r{   c                 S      i | ]
\}}d | |qS )text_encoder_2.r   rw   r   r   r   r6          N)r?   r   rE   r=   r>   endswithrC   
startswith_convert_unet_lora_keyr@   replace_convert_text_encoder_lora_keyitemupdate_get_alpha_namelenrH   loggerinfoitems)r4   rz   r|   unet_state_dictte_state_dictte2_state_dictnetwork_alphasdora_present_in_unetdora_present_in_tedora_present_in_te2all_lora_keysr    	lora_namelora_name_uplora_name_alphadiffusers_namedora_scale_key_to_replacedora_scale_key_to_replace_tealpharQ   r   )r|   rz   r   (_convert_non_diffusers_lora_to_diffusers   sz   











r   c                    s  |  dd dd   dd   dd   dd	   d
d	   dd   dd   dd   dd   dd   dd   dd   dd   dd   dd d v rqd vrqd }tj|d d!d" d# v r{  d$d% d& v r  d'd( d) v sd* v r  d+d, d- v r  d.d/ d v r  dd d0 v r  d0d/ d v rd1 v sd2 v rˈ  d1d3   d2d4  S d5 v r	  S t fd6d7d8D r	  S 	  S )9zB
    Converts a U-Net LoRA key to a Diffusers compatible key.
    re    r   r<   zinput.blocksr   zdown.blockszmiddle.blockr   z	mid.blockzoutput.blocksr   z	up.blocksztransformer.blockstransformer_blocksz	to.q.lora	to_q_loraz	to.k.lora	to_k_loraz	to.v.lora	to_v_lorazto.out.0.lorato_out_lorazproj.inproj_inzproj.outproj_outz
emb.layerstime_emb_projembztime.emb.projz\.\d+(?=\D*$)r(   )countz.in.zin.layers.2conv1z.out.zout.layers.3conv2r9   r%   r8   convskipzskip.connectionconv_shortcutzconv.shortcutattn1attn2zattn1.processorzattn2.processorffc                 3   r   r   r   r+   r   r   r   r!   4  r"   z)_convert_unet_lora_key.<locals>.<genexpr>)r   r   )r   resubr?   )r    patternr   r   r   r     sV   r   c                 C   s   | dr| drdnd}nd}| |ddd}|dd	}|d
d}|dd}|dd}|dd}|dd}|dd}d|v sMd|v rP	 |S d|v rZ|dd}|S )zI
    Converts a text encoder LoRA key to a Diffusers compatible key.
    rr   ri   rj   rk   r   r   r<   z
text.model
text_modelz	self.attn	self_attnzq.proj.lorar   zk.proj.lorar   zv.proj.lorar   zout.proj.lorar   ztext.projectiontext_projectionmlp.lora.rs   r   r   )r    r   key_to_replacer   r   r   r   r   <  s"   
r   c                 C   sB   |  drd}n
|  drd}nd}||dd  d }||iS )	z>
    Gets the correct alpha name for the Diffusers model.
    re   zunet.rr   text_encoder.r~   r   r   ro   )r   rC   )r   r   r   prefixnew_namer   r   r   r   X  s   

r   c                    s@  dd  d,dd fdd}dd }t d	d
 | D }|r*dd |  D } | S t dd
 | D }|sdd |  D } dd |  D } t dd
 | D }|rnt| d}|r`td ntd dd |  D } t dd
 | D }|rt| d}|rtd ntd dd |  D } t dd
 | D }	|	rt| d}
|
rtd ntd dd |  D } t dd
 | D }|rt| d }|rtd! ntd" d#d |  D } d$d%gt d&d
 | D rd' fd(d|  D } t d)d
 | D rtd* d+d |  D } |r|| S || S )-Nc                 S      |d | vrd S |  |d }|jd }tj||j|jdd}|  |d | }|| }|}	d}
|	d |
k rE|	d9 }	|
d }
|	d |
k s7||	 ||d < |  |d	 |
 ||d
 < d S N.lora_down.weightr   Fdtypedevicerequires_gradro         ?r   .lora_A.weightrn   .lora_B.weightr@   shaper	   tensorr   r   r   sds_sdait_sdsds_keyait_keydown_weightrankdefault_alphar   r   
scale_downscale_upr   r   r   _convert_to_ai_toolkiti  s   
zE_convert_kohya_flux_lora_to_diffusers.<locals>._convert_to_ai_toolkitc              
   S   b  |d | vrd S |  |d }|  |d }|jd }tj||j|jdd}|  |d |}	|	| }
|
}d}|d |k rJ|d9 }|d }|d |k s<|| }|| }t|}|d u re|jd | g| }nt||jd kspJ d}|| dkr|| }d	}d}tt|D ]1}tt|D ]"}||krq|ot	|||||  || |d
 | f dk}q||| 7 }q|rt
d|  dd |D }dd |D }|s|t|| |dd t|tj||ddD  d S |dd t|tj||ddD  d}tt|D ]#}|||||  || |d
 | f  ||| < ||| 7 }qd S )Nr   rn   r   Fr   ro   r   r   Tr(   weight is sparse: c                 S      g | ]}|d  qS r   r   rf   r   r   r   r/         z]_convert_kohya_flux_lora_to_diffusers.<locals>._convert_to_ai_toolkit_cat.<locals>.<listcomp>c                 S   r   r   r   rf   r   r   r   r/     r   c                 S      i | ]\}}||qS r   r   r   rg   vr   r   r   r6         z]_convert_kohya_flux_lora_to_diffusers.<locals>._convert_to_ai_toolkit_cat.<locals>.<dictcomp>r   c                 S   r   r   r   r   r   r   r   r6     r   r@   r   r	   r   r   r   r   sumrangeallr   r   r   dictfromkeysziprC   r   
contiguousr   r   r   ait_keysdimsr   	up_weightsd_lora_rankr   r   r   r   r   
num_splits	is_sparseait_rankrX   jrg   ait_down_keysait_up_keysr   r   r   _convert_to_ai_toolkit_cat~  s`   
**&4zI_convert_kohya_flux_lora_to_diffusers.<locals>._convert_to_ai_toolkit_catc                    s  i  t dD ]}|  d| dd| d |  d| dd| dd| dd| d	g |  d| d
d| d |  d| dd| d |  d| dd| d |  d| dd| d |  d| dd| dd| dd| dg |  d| dd| d |  d| dd| d |  d| dd| d qt dD ]C}|  d| dd| dd| dd| d	d| d gg d!d" |  d| d#d| d$ |  d| d%d| d& q fd'd(}td)d* | D r|  d+d, |  d-d. td/d* | D r,|  d0d1 td2d* | D r=|  d3d4 td5d* | D rU|  d6d7 |  d8d9 td:d* | D rm|  d;d< |  d=d> td?d* | D r|d@dAtfdBg|  t|  }i }|r!tdCd* |D stdDdE| |D ]~}|dFsq|	dGdH }| dI}| dJ}t
||}	|dKr| |}
|
jdH }|
||	< | |||	dLdM< || v r| | }|| }|}dN}|dO |k r|dO9 }|dO }|dO |k s||	  |9  < ||	dLdM  |9  < qt| dHkr2tdP|    |r>dQdR | D }i  |}|S )SN   lora_unet_double_blocks__img_attn_projtransformer.transformer_blocks..attn.to_out.0_img_attn_qkv
.attn.to_q
.attn.to_k
.attn.to_v
_img_mlp_0z.ff.net.0.proj
_img_mlp_2z	.ff.net.2_img_mod_linz.norm1.linear_txt_attn_proj.attn.to_add_out_txt_attn_qkv.attn.add_q_proj.attn.add_k_proj.attn.add_v_proj
_txt_mlp_0z.ff_context.net.0.proj
_txt_mlp_2z.ff_context.net.2_txt_mod_linz.norm1_context.linear&   lora_unet_single_blocks__linear1&transformer.single_transformer_blocks.z	.proj_mlp)   r  r  i 0  )r   _linear2	.proj_out_modulation_linz.norm.linearc           
         sd   dD ]-}|dkr
dnd}| D ] \}}}|j |d}|j |d}||}	|r*||	}	|	 |< qqd S )Nlora_Alora_Br  	lora_downlora_up)lora_key)orig_lora_key)formatr@   )
assignmentssourcer  r  
target_fmt
source_fmt	transform
target_key
source_keyvaluer   r   r   assign_remaining_weights$  s   

zr_convert_kohya_flux_lora_to_diffusers.<locals>._convert_sd_scripts_to_ai_toolkit.<locals>.assign_remaining_weightsc                 s       | ]}d |v V  qdS )guidance_inNr   rf   r   r   r   r!   /  r"   zc_convert_kohya_flux_lora_to_diffusers.<locals>._convert_sd_scripts_to_ai_toolkit.<locals>.<genexpr>lora_unet_guidance_in_in_layer*time_text_embed.guidance_embedder.linear_1lora_unet_guidance_in_out_layer*time_text_embed.guidance_embedder.linear_2c                 s   r"  )img_inNr   rf   r   r   r   r!   >  r"   lora_unet_img_in
x_embedderc                 s   r"  )txt_inNr   rf   r   r   r   r!   F  r"   lora_unet_txt_incontext_embedderc                 s   r"  )time_inNr   rf   r   r   r   r!   N  r"   lora_unet_time_in_in_layer*time_text_embed.timestep_embedder.linear_1lora_unet_time_in_out_layer*time_text_embed.timestep_embedder.linear_2c                 s   r"  )	vector_inNr   rf   r   r   r   r!   \  r"   lora_unet_vector_in_in_layer&time_text_embed.text_embedder.linear_1lora_unet_vector_in_out_layer&time_text_embed.text_embedder.linear_2c                 s   r"  )final_layerNr   rf   r   r   r   r!   j  r"   z!norm_out.linear.{lora_key}.weightz?lora_unet_final_layer_adaLN_modulation_1.{orig_lora_key}.weight)zproj_out.{lora_key}.weightz3lora_unet_final_layer_linear.{orig_lora_key}.weightNc                 s       | ]}| d V  qdS ))lora_telora_te1Nr   rf   r   r   r   r!   {      zIncompatible keys detected: 

 rt   rm   r<   r   rn   ro   rr   rp   rq   r   r   z!Unsupported keys for ai-toolkit: c                 S   r}   )r   r   rw   r   r   r   r6     r   zd_convert_kohya_flux_lora_to_diffusers.<locals>._convert_sd_scripts_to_ai_toolkit.<locals>.<dictcomp>)r   r?   r   r=   r>   r   rE   rH   r   rC   r   r   r@   r   r   r   r   r   warningr   )r   rX   r!  remaining_keysr   r    r   r   r   r   r   r   r   r   r   r   rQ   )r   r   r   r   !_convert_sd_scripts_to_ai_toolkit  s~  








































zP_convert_kohya_flux_lora_to_diffusers.<locals>._convert_sd_scripts_to_ai_toolkitc                    s  i }dd }dd | D }t dd |D sJ d|d}|D ]  d	r<t d	d
 dd }d| }n drUt dd
 dd }d| }nl dr]d}q! dred}n\ drmd}nT drud}nL dr}d}nD drt dd
 }d| }n0 drt dd
 }d| }n drt dd
 }d| }ntd   d!d" v rd# v r|d$7 }n=d% v r|d&7 }n4t fd'dd(D r d"d
 }|d)| 7 }nt fd*dd+D r d"d
 }|d)| 7 }| || | q!|rtd,}d-d.d/d0d1}	d2d3d4}
|D ]]  ds4q*| }|s>q*t|	d5}d6| }d7 v re|	
 D ]\}}| v rb||7 } nqSnd8 v r|

 D ]\}}| v r}||7 } nqn| || | q*d}| rt d9d | D }|rt|  }|D ] |   qt| dkrtd:t|   d;d<d= |
 D }d>d= |
 D }i ||S )?Nc                 S   s   |  d}| |}|jd }|  d}| |}|  d}	| |	}
|
| }|}d}|d |k r?|d9 }|d }|d |k s1|| }|| }| d}|||< |||dd	< d S )
Nr   r   rn   ro   r   r   r   z.lora_A.z.lora_B.)r@   r   r   )original_keydiffusers_keyr4   rQ   down_keyr   	lora_rankup_weight_keyr   	alpha_keyr   r   r   r   diffusers_down_keyr   r   r   _convert  s&   







zi_convert_kohya_flux_lora_to_diffusers.<locals>._convert_mixture_state_dict_to_diffusers.<locals>._convertc                 S   s2   h | ]}| d s|ddddddqS )re   r   r   rn   ro   r   rf   r   r   r   	<setcomp>  s    zj_convert_kohya_flux_lora_to_diffusers.<locals>._convert_mixture_state_dict_to_diffusers.<locals>.<setcomp>c                 s   r9  ))lora_transformer_rj   Nr<  rf   r   r   r   r!     r=  zj_convert_kohya_flux_lora_to_diffusers.<locals>._convert_mixture_state_dict_to_diffusers.<locals>.<genexpr>zall_unique_keys=F+lora_transformer_single_transformer_blocks_r'   r   r   single_transformer_blocks.$lora_transformer_transformer_blocks_transformer_blocks.rj   T!lora_transformer_context_embedderr-   lora_transformer_norm_out_linearnorm_out.linearlora_transformer_proj_outr   lora_transformer_x_embedderr*  :lora_transformer_time_text_embed_guidance_embedder_linear_z)time_text_embed.guidance_embedder.linear_6lora_transformer_time_text_embed_text_embedder_linear_z%time_text_embed.text_embedder.linear_:lora_transformer_time_text_embed_timestep_embedder_linear_z)time_text_embed.timestep_embedder.linear_zHandling for key (z) is not implemented.attn_	_to_out_0r   _to_add_outr   c                 3   r   r   r   )r   qkvrg   r   r   r!     r"   to_qto_kto_v.attn.c                 3   r   r   r   )r   add_qkvr[  r   r   r!     r"   
add_q_proj
add_k_proj
add_v_projz(lora_te1_text_model_encoder_layers_(\d+)z.self_attn.q_projz.self_attn.k_projz.self_attn.v_projz.self_attn.out_proj)q_projk_projv_projout_projz.mlp.fc1z.mlp.fc2)fc1fc2r(   ztext_model.encoder.layers.attnr   c                 s   r9  re   Nr<  rf   r   r   r   r!     r=  z\Expected an empty state dict at this point but its has these keys which couldn't be parsed: r<   c                 S   s&   i | ]\}}| d sd| |qS )text_model.transformer.r<  r   r   r   r   r6      s
    
zk_convert_kohya_flux_lora_to_diffusers.<locals>._convert_mixture_state_dict_to_diffusers.<locals>.<dictcomp>c                 S   s&   i | ]\}}| d rd| |qS )rn  r   r<  r   r   r   r   r6   #     & )r   r   rB   rC   NotImplementedErrorr?   r   compilesearchgroupr   r=   r>   r@   r   rE   )r4   rQ   rH  all_unique_keyshas_te_keysrX   rB  	remaininglayer_patternattn_mappingmlp_mappingmatchkey_fragmentsuffixremaining_all_unetr>   transformer_state_dictr   r   r[  r   (_convert_mixture_state_dict_to_diffusers  s    




















zW_convert_kohya_flux_lora_to_diffusers.<locals>._convert_mixture_state_dict_to_diffusersc                 s   r9  )ro  Nr<  rf   r   r   r   r!   )  r=  z8_convert_kohya_flux_lora_to_diffusers.<locals>.<genexpr>c                 S   s0   i | ]\}}| d r|dddd|qS )ro  rm   zlora_A.weightzlora_up.weightzlora_B.weightr   r   r   r   r   r6   +  s    z9_convert_kohya_flux_lora_to_diffusers.<locals>.<dictcomp>c                 s   s2    | ]}| d od|v pd|v pd|v V  qdS )rJ  r  r  r   Nr<  rf   r   r   r   r!   3  s    "
c                 S      i | ]\}}| d d|qS )diffusion_model.re   r   r   r   r   r   r6   9      c                 S   r  )z!text_encoders.clip_l.transformer.ri   r  r   r   r   r   r6   :  r  c                 s   r"  )position_embeddingNr   rf   r   r   r   r!   <  r"   r  zThe `position_embedding` LoRA params are all zeros which make them ineffective. So, we will purge them out of the current state dict to make loading possible.zThe state_dict has position_embedding LoRA params and we currently do not support them. Open an issue if you need this supported - https://github.com/huggingface/diffusers/issues/new.c                 S      i | ]\}}d |vr||qS )r  r   r   r   r   r   r6   J  r  c                 s   r9  ) text_encoders.t5xxl.transformer.Nr<  rf   r   r   r   r!   L  r=  ztext_encoders.t5xxlzThe `t5xxl` LoRA params are all zeros which make them ineffective. So, we will purge them out of the current state dict to make loading possible.zT5-xxl keys found in the state dict, which are currently unsupported. We will filter them out.Open an issue if this is a problem - https://github.com/huggingface/diffusers/issues/new.c                 S   s    i | ]\}}| d s||qS )r  r<  r   r   r   r   r6   Y  r{   c                 s   "    | ]}d |v o| dV  qdS )diff_bre   ri   rj   Nr<  rf   r   r   r   r!   [       .diff_bzThe `diff_b` LoRA params are all zeros which make them ineffective. So, we will purge them out of the current state dict to make loading possible.z`diff_b` keys found in the state dict which are currently unsupported. So, we will filter out those keys. Open an issue if this is a problem - https://github.com/huggingface/diffusers/issues/new.c                 S   r  )r  r   r   r   r   r   r6   i  r  c                 s   rc   ).norm.diffNr   rf   r   r   r   r!   k  rh   r  zThe `diff` LoRA params are all zeros which make them ineffective. So, we will purge them out of the current state dict to make loading possible.zNormalization diff keys found in the state dict which are currently unsupported. So, we will filter out those keys. Open an issue if this is a problem - https://github.com/huggingface/diffusers/issues/new.c                 S   s&   i | ]\}}d |vrd|vr||qS )r  r  r   r   r   r   r   r6   y  rp  r  r  c                 s   r"  )r   Nr   rf   r   r   r   r!   |  r"   r   c                    s&   i | ]\}}| d rt| |qS )r  )r   _custom_replacer   )limit_substringsr   r   r6     s    
c                 s   r"  )r   Nr   rf   r   r   r   r!     r"   z`text_projection` keys found in the `state_dict` which are unexpected. So, we will filter out those keys. Open an issue if this is a problem - https://github.com/huggingface/diffusers/issues/new.c                 S   r  )r   r   r   r   r   r   r6     r  r   )r?   r   r   r   r   append)r4   r@  r  has_peft_state_dicthas_mixturehas_position_embeddingzero_status_pe	has_t5xxlzero_status_t5	has_diffbzero_status_diff_bhas_norm_diffzero_status_diffr   )r   r   r  r   %_convert_kohya_flux_lora_to_diffusersh  s   
C b 





r  c              
   C   s  i }t |  }d!dd}|D ]}|drtd|d}d| }d|v r-|d7 }nFd	|v r6|d
7 }n=d|v rUd|vrU|| ||d| dd| dd| dg nd|v rsd|vrs|| ||d| dd| dd| dg d|v r||d7 }nWd|v r|d7 }nN|drtd|d}d| }d|v r|d7 }nd|v rd|vr|| ||d| dd| dd| dg d|v r|d7 }nd|v r|d7 }n|}d|vr| |||< qt| dkrtdt |   d |S )"Nc           
   
   S   s   |  |}|  |dd}t|}|d u r!|jd | g| }nt||jd ks,J dd |D }dd |D }	|t|| |dd t|	t	j
||dd	D  d S )
Nz.down.weightz
.up.weightr   c                 S   r   r   r   rf   r   r   r   r/     r   zM_convert_xlabs_flux_lora_to_diffusers.<locals>.handle_qkv.<locals>.<listcomp>c                 S   r   r   r   rf   r   r   r   r/     r   c                 S   r   r   r   r   r   r   r   r6     r   zM_convert_xlabs_flux_lora_to_diffusers.<locals>.handle_qkv.<locals>.<dictcomp>r   )r@   r   r   r   r   r   r   r   r   r	   rC   )
r   r   r   r   r   r   r   r   r   r   r   r   r   
handle_qkv  s   
*z9_convert_xlabs_flux_lora_to_diffusers.<locals>.handle_qkv)zdiffusion_model.double_blocksdouble_blockszdouble_blocks\.(\d+)r(   r   zprocessor.proj_lora1r   zprocessor.proj_lora2r   zprocessor.qkv_lora2upr  r  r  zprocessor.qkv_lora1r   r   r   downr   r   )zdiffusion_model.single_blockssingle_blockszsingle_blocks\.(\d+)r  	proj_lorar  qkv_lorarZ  r   z2`old_state_dict` should be at this point but has: r<   r   )	r=   r>   r   r   rs  rt  r@   r   rE   )old_state_dictrQ   	orig_keysr  old_key	block_numr^   r   r   r   %_convert_xlabs_flux_lora_to_diffusers  s~   


















r  r    
substringsreturnc                 C   s   dd dd |D  d }t|| }|rA| }|dkr+| |d  dkr+|d }n|}| d | dd	}| |d  }|| S | dd	S )
N(|c                 s   s    | ]}t |V  qd S r   )r   escape)r   r   r   r   r   r!     r=  z"_custom_replace.<locals>.<genexpr>)r   r(   r<   r   )rH   r   rs  startr   )r    r  r   r{  	start_subboundaryleftrightr   r   r   r    s   
r  c           &   
   C   s`  i }t |  }d}d}d}d}dD ]A}| d| d|d| d< d| d	|v r;| d| d	|d| d	< | d
| d|d| d< d
| d	|v ra| d
| d	|d| d	< | d| d|d| d< d| d	|v r| d| d	|d| d	< | d| d|d| d< d| d	|v r| d| d	|d| d	< tdd | D }|r| d| d|d| d< d| d	|v r| d| d	|d| d	< | d| d|d| d< d| d	|v r| d| d	|d| d	< | d| d|d| d< d| d	|v r-| d| d	|d| d	< | d| d|d| d< d| d	|v rT| d| d	|d| d	< qt|D ]n}	d|	 d}
dD ])}| d|	 d| d||
 d| d< d|	 d| d	|v r| d|	 d| d	||
 d| d	< | d|	 d| d||
 d | d< d|	 d| d	|v r| d|	 d| d	||
 d | d	< |d!krA| d|	 d"| d}t|g||
 d#| d< t|g||
 d$| d< t|g||
 d%| d< | d|	 d&| d}t|g||
 d'| d< t|g||
 d(| d< t|g||
 d)| d< n~tj| d|	 d"| dd*d+d,\}}}t|g||
 d$| d< t|g||
 d%| d< t|g||
 d#| d< tj| d|	 d&| dd*d+d,\}}}t|g||
 d'| d< t|g||
 d(| d< t|g||
 d)| d< d|	 d"| d	|v r
tj| d|	 d"| d	d*d+d,\}}}t|g||
 d$| d	< t|g||
 d%| d	< t|g||
 d#| d	< d|	 d&| d	|v rUtj| d|	 d&| d	d*d+d,\}}}t|g||
 d'| d	< t|g||
 d(| d	< t|g||
 d)| d	< | d|	 d-| d||
 d.| d< d|	 d-| d	|v r| d|	 d-| d	||
 d.| d	< | d|	 d/| d||
 d0| d< d|	 d/| d	|v r| d|	 d/| d	||
 d0| d	< | d|	 d1| d||
 d2| d< d|	 d1| d	|v r| d|	 d1| d	||
 d2| d	< | d|	 d3| d||
 d4| d< d|	 d3| d	|v r%| d|	 d3| d	||
 d4| d	< | d|	 d5| d||
 d6| d< d|	 d5| d	|v rY| d|	 d5| d	||
 d6| d	< | d|	 d7| d||
 d8| d< d|	 d7| d	|v r| d|	 d7| d	||
 d8| d	< qd| d|	 d9||
 d:< | d|	 d;||
 d<< | d|	 d=||
 d>< | d|	 d?||
 d@< qYt|D ]}	dA|	 d}
dD ]}| dB|	 dC| d||
 dD| d< dB|	 dC| d	|v r| dB|	 dC| d	||
 dD| d	< t|| }||||f}|d!kr| dB|	 dE| d}t|g||
 d$| d< t|g||
 d%| d< t|g||
 d#| d< t|g||
 dF| d< dB|	 dE| d	|v r| dB|	 dE| d	}t|g||
 d$| d	< t|g||
 d%| d	< t|g||
 d#| d	< t|g||
 dF| d	< ntj	| dB|	 dE| d|d+d,\}}}} t|g||
 d$| d< t|g||
 d%| d< t|g||
 d#| d< t| g||
 dF| d< dB|	 dE| d	|v r]tj	| dB|	 dE| d	|d+d,\}!}"}#}$t|!g||
 d$| d	< t|"g||
 d%| d	< t|#g||
 d#| d	< t|$g||
 dF| d	< | dB|	 dG| d||
 dH| d< dB|	 dG| d	|v r| dB|	 dG| d	||
 dH| d	< q| dB|	 dI||
 d:< | dB|	 dJ||
 d<< qdD ]U}| dK| d|dH| d< dK| d	|v r| dK| d	|dH| d	< t
| dL| d|dM| d< dL| d	|v rt
| dL| d	|dM| d	< qt| d+krtdN|  dt | D ]}%||%|dO|% < q |S )PNr   r  r        @r  ztime_in.in_layer..weightz+time_text_embed.timestep_embedder.linear_1..biasztime_in.out_layer.z+time_text_embed.timestep_embedder.linear_2.zvector_in.in_layer.z'time_text_embed.text_embedder.linear_1.zvector_in.out_layer.z'time_text_embed.text_embedder.linear_2.c                 s   r"  )guidanceNr   rf   r   r   r   r!   ?  r"   z>_convert_bfl_flux_control_lora_to_diffusers.<locals>.<genexpr>zguidance_in.in_layer.z+time_text_embed.guidance_embedder.linear_1.zguidance_in.out_layer.z+time_text_embed.guidance_embedder.linear_2.ztxt_in.zcontext_embedder.zimg_in.zx_embedder.rN  r<   double_blocks..img_mod.lin.norm1.linear..txt_mod.lin.norm1_context.linear.r  .img_attn.qkv.
attn.to_v.
attn.to_q.
attn.to_k..txt_attn.qkv.attn.add_q_proj.attn.add_k_proj.attn.add_v_proj.   r   r   .img_mlp.0.ff.net.0.proj..img_mlp.2.	ff.net.2..txt_mlp.0.ff_context.net.0.proj..txt_mlp.2.ff_context.net.2..img_attn.proj.attn.to_out.0..txt_attn.proj.attn.to_add_out.z.img_attn.norm.query_norm.scalezattn.norm_q.weightz.img_attn.norm.key_norm.scalezattn.norm_k.weightz.txt_attn.norm.query_norm.scalezattn.norm_added_q.weightz.txt_attn.norm.key_norm.scalezattn.norm_added_k.weightrL  single_blocks..modulation.lin.norm.linear.	.linear1.	proj_mlp.	.linear2.	proj_out.z.norm.query_norm.scalez.norm.key_norm.scalefinal_layer.linear.zfinal_layer.adaLN_modulation.1.znorm_out.linear.W`original_state_dict` should be empty at this point but has original_state_dict.keys()=ro  )r=   r>   r@   r?   r   r	   r
   r   rB   rC   r   r   rE   )&original_state_dictconverted_state_dictoriginal_state_dict_keys
num_layersnum_single_layers	inner_dim	mlp_ratior  has_guidancerX   block_prefixsample_lora_weightcontext_lora_weightsample_qsample_ksample_v	context_q	context_k	context_vsample_q_biassample_k_biassample_v_biascontext_q_biascontext_k_biascontext_v_biasmlp_hidden_dim
split_sizelora_weight	lora_biasqrg   r   r   q_biask_biasv_biasmlp_biasr    r   r   r   +_convert_bfl_flux_control_lora_to_diffusers  s  


















r  c           &      C   s8  i }t |  }d}d}d}d}t|D ]J}d| d}d}	dD ]<}
| |	 d	| d
|
 d|| d|
 d< d	| d
|
 d|v r[| |	 d	| d
|
 d|| d|
 d< | |	 d	| d|
 d|| d|
 d< |
dkr| |	 d	| d|
 d}t|g|| d|
 d< t|g|| d|
 d< t|g|| d|
 d< | |	 d	| d|
 d}t|g|| d|
 d< t|g|| d|
 d< t|g|| d|
 d< ntj| |	 d	| d|
 dddd\}}}t|g|| d|
 d< t|g|| d|
 d< t|g|| d|
 d< tj| |	 d	| d|
 dddd\}}}t|g|| d|
 d< t|g|| d|
 d< t|g|| d|
 d< d	| d|
 d|v rtj| |	 d	| d|
 dddd\}}}t|g|| d|
 d< t|g|| d|
 d< t|g|| d|
 d< d	| d|
 d|v rtj| |	 d	| d|
 dddd\}}}t|g|| d|
 d< t|g|| d|
 d< t|g|| d|
 d< | |	 d	| d|
 d|| d|
 d< |	 d	| d|
 d|v r<| |	 d	| d|
 d|| d|
 d< | |	 d	| d|
 d|| d|
 d< |	 d	| d|
 d|v rv| |	 d	| d|
 d|| d|
 d< | |	 d	| d |
 d|| d!|
 d< |	 d	| d |
 d|v r| |	 d	| d |
 d|| d!|
 d< | |	 d	| d"|
 d|| d#|
 d< |	 d	| d"|
 d|v r| |	 d	| d"|
 d|| d#|
 d< | |	 d	| d$|
 d|| d%|
 d< |	 d	| d$|
 d|v r$| |	 d	| d$|
 d|| d%|
 d< | |	 d	| d&|
 d|| d'|
 d< |	 d	| d&|
 d|v r^| |	 d	| d&|
 d|| d'|
 d< q!qt|D ]}d(| d}dD ]}
| |	 d)| d*|
 d|| d+|
 d< |	 d)| d*|
 d|v r| |	 d)| d*|
 d|| d+|
 d< t|| }||||f}|
dkrV| |	 d)| d,|
 d}t|g|| d|
 d< t|g|| d|
 d< t|g|| d|
 d< t|g|| d-|
 d< |	 d)| d,|
 d|v rU| d)| d,|
 d}t|g|| d|
 d< t|g|| d|
 d< t|g|| d|
 d< t|g|| d-|
 d< ntj| |	 d)| d,|
 d|dd\}}}} t|g|| d|
 d< t|g|| d|
 d< t|g|| d|
 d< t| g|| d-|
 d< |	 d)| d,|
 d|v rtj| |	 d)| d,|
 d|dd\}!}"}#}$t|!g|| d|
 d< t|"g|| d|
 d< t|#g|| d|
 d< t|$g|| d-|
 d< | |	 d)| d.|
 d|| d/|
 d< |	 d)| d.|
 d|v r>| |	 d)| d.|
 d|| d/|
 d< qoqddD ]0}
| |	 d0|
 d|d/|
 d< |	 d0|
 d|v rs| |	 d0|
 d|d/|
 d< qDt	| dkrt
d1|  dt | D ]}%||%|d2|% < q|S )3Nr   r  r  r  rN  r<   zbase_model.model.r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r  r  r  r  r  r  r  r  r  r  r  r  rL  r  r  r  r  r  r  r  r  r  ro  )r=   r>   r   r@   r	   r
   r   rB   rC   r   rE   )&r  r  r  r  r  r  r  rX   r  original_block_prefixr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rg   r   r   r  r  r  r  r    r   r   r   &_convert_fal_kontext_lora_to_diffusers@  s~  
 

<r  c                    s   fddt   D }dd }dd }dd }d	d
 }dd }i dddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d,d1d2d"d3d4d5d6d7d8d9d:}|||||d;}t | D ]&}	|	d<r||	||	td<d  < |	d=r||	||	td=d  < qtt | D ]}	|	d d  }
| D ]
\}}|
||}
q||	||
< qt | D ]}	| D ]\}}||	vrq||	| qqt | D ]}	||	|d<|	 < q|S )>Nc                    s   i | ]}|  |qS r   )r@   rf   r  r   r   r6     s    z<_convert_hunyuan_video_lora_to_diffusers.<locals>.<dictcomp>c                 S   sB   | | }|jddd\}}tj||gdd}||| dd< d S )Nr   r   r   final_layer.adaLN_modulation.1rQ  )r@   r   r	   r
   r   )r    r4   r   r   r   r   r   r   r   remap_norm_scale_shift_!  s   
zI_convert_hunyuan_video_lora_to_diffusers.<locals>.remap_norm_scale_shift_c                 S   s   dd }d| v r7| | }|jddd\}}}|||| dd< |||| dd< |||| dd	< d S | | ||| < d S )
Nc                 S   sX   |  dd}| dd}| dd}| dd}| d	d
}| dd}| dd}|S )Nzindividual_token_refiner.blocksztoken_refiner.refiner_blockszadaLN_modulation.1rQ  r+  r-  zt_embedder.mlp.0r0  zt_embedder.mlp.2r2  
c_embedderztime_text_embed.text_embedderr   r   r  )r    r^   r   r   r   
rename_key(  s   zS_convert_hunyuan_video_lora_to_diffusers.<locals>.remap_txt_in_.<locals>.rename_keyself_attn_qkvr  r   r   	attn.to_q	attn.to_k	attn.to_v)r@   r   r   )r    r4   r  r   r]  r^  r_  r   r   r   remap_txt_in_'  s   

z?_convert_hunyuan_video_lora_to_diffusers.<locals>.remap_txt_in_c                 S      | | }d| v r#||| dd< ||| dd< ||| dd< d S |jddd\}}}||| dd< ||| dd< ||| dd< d S )	Nr  img_attn_qkvr  r  r  r  r   r   r@   r   r   r    r4   r   r]  r^  r_  r   r   r   remap_img_attn_qkv_;     
zE_convert_hunyuan_video_lora_to_diffusers.<locals>.remap_img_attn_qkv_c                 S   r  )	Nr  txt_attn_qkvzattn.add_q_projzattn.add_k_projzattn.add_v_projr  r   r   r
  r  r   r   r   remap_txt_attn_qkv_G  r  zE_convert_hunyuan_video_lora_to_diffusers.<locals>.remap_txt_attn_qkv_c                 S   s&  d}d| v s
d| v rz| | }d| v r:| ddd}||| d< ||| d	< ||| d
< ||| d< d S ||||dd|  f}tj||dd\}}}}	| ddd}||| d< ||| d< ||| d< |	|| d< d S d| v sd| v r| | }
d| v r| ddd}|
|| d< |
|| d< |
|| d< |
|| d< d S ||||
dd|  f}tj|
|dd\}}}}| ddd}||| d< ||| d< ||| d< ||| d< d S | dd}|d d!}|d"d#}|d$d%}| | ||< d S )&Nr  zlinear1.lora_A.weightzlinear1.lora_B.weightr  r  single_transformer_blocksz.linear1.lora_A.weightz.attn.to_q.lora_A.weightz.attn.to_k.lora_A.weightz.attn.to_v.lora_A.weightz.proj_mlp.lora_A.weightr   r  r   z.linear1.lora_B.weightz.attn.to_q.lora_B.weightz.attn.to_k.lora_B.weightz.attn.to_v.lora_B.weightz.proj_mlp.lora_B.weightzlinear1.lora_A.biaszlinear1.lora_B.biasz.linear1.lora_A.biasz.attn.to_q.lora_A.biasz.attn.to_k.lora_A.biasz.attn.to_v.lora_A.biasz.proj_mlp.lora_A.biasz.linear1.lora_B.biasz.attn.to_q.lora_B.biasz.attn.to_k.lora_B.biasz.attn.to_v.lora_B.biasz.proj_mlp.lora_B.biaslinear2r   q_normattn.norm_qk_normattn.norm_k)r@   r   removesuffixsizer	   rC   )r    r4   hidden_sizelinear1_weightr^   r  r  rg   r   r   linear1_biasr  r  r  r  r   r   r    remap_single_transformer_blocks_S  sX   

zR_convert_hunyuan_video_lora_to_diffusers.<locals>.remap_single_transformer_blocks_r(  r*  ztime_in.mlp.0r0  ztime_in.mlp.2r2  zguidance_in.mlp.0r%  zguidance_in.mlp.2r'  zvector_in.in_layerr5  zvector_in.out_layerr7  r  r   img_attn_q_normr  img_attn_k_normr  img_attn_projattn.to_out.0txt_attn_q_normzattn.norm_added_qtxt_attn_k_normzattn.norm_added_ktxt_attn_projattn.to_add_outzimg_mod.linearznorm1.linear	img_norm1z
norm1.norm	img_norm2norm2r   znorm1_context.linearnorm2_context
ff_contextznorm.linearz	norm.normznorm_out.normr   
net.0.projnet.2r   )img_mlpztxt_mod.linear	txt_norm1	txt_norm2txt_mlpself_attn_projzmodulation.linearpre_normzfinal_layer.norm_finalfinal_layer.linearrj  rk  input_embedder)r+  r	  r  r  r  ro  r  )r=   r>   r   r@   r   r   r   )r  r  r   r  r  r  r  TRANSFORMER_KEYS_RENAME_DICTTRANSFORMER_SPECIAL_KEYS_REMAPr    r^   replace_keyr  special_keyhandler_fn_inplacer   r  r   (_convert_hunyuan_video_lora_to_diffusers  s   4	
"

r8  c                    s   dd   D i  dd } fdd}d}| |}t|D ]	}|d|d	d
 q#d}| |}t|D ]	}|d|dd
 q:d}| |}	t|	D ]	}|d|d	d
 qQtdkrjtd t  D ]}
 |
 d|
 < qp S )Nc                 S   "   i | ]\}}|t d d |qS r  Nr   r   r   r   r   r6     r0   zD_convert_non_diffusers_lumina2_lora_to_diffusers.<locals>.<dictcomp>c                 S   s<   t  }| D ]}t||}|r|t|d qt|S )Nr(   )rA   r   rs  rD   rB   rt  r   )r>   r   layersr    r{  r   r   r   get_num_layers  s   zH_convert_non_diffusers_lumina2_lora_to_diffusers.<locals>.get_num_layersc              	      s   |  d| d} |  d| d}dD ]}| |  d| d| d< qtg dtj|g ddd	D ]\}}| |  d| d| d
< q7 |  d| d |  d| d<  |  d| d |  d| d< tddD ]4} |  d| d| d |  d| d| d<  |  d| d| d
 |  d| d| d
< qt|rӈ |  d| d |  d| d<  |  d| d |  d| d< d S d S )Nr<   z.attention.qkv.lora_A.weightz.attention.qkv.lora_B.weightr\  r`  r   )i 	     r>  r   r   r   z.attention.out.lora_A.weightz.attn.to_out.0.lora_A.weightz.attention.out.lora_B.weightz.attn.to_out.0.lora_B.weightr(      z.feed_forward.wz.feed_forward.linear_z!.adaLN_modulation.1.lora_A.weightz.norm1.linear.lora_A.weightz!.adaLN_modulation.1.lora_B.weightz.norm1.linear.lora_B.weight)r@   r   r	   rC   r   )r   indexconvert_normr  r  attn_keyr   rV   r  r4   r   r   process_block  s6   &zG_convert_non_diffusers_lumina2_lora_to_diffusers.<locals>.process_blockznoise_refiner\.(\d+)\.noise_refinerT)rA  zcontext_refiner\.(\d+)\.context_refinerFzlayers\.(\d+)\.r<  r   E`state_dict` should be empty at this point but has state_dict.keys()=ro  )r   r>   r   r   rE   r=   r@   )r4   r=  rD  noise_refiner_patternnum_noise_refiner_layersrX   context_refiner_patternnum_context_refiner_layerscore_transformer_patternnum_core_transformer_layersr    r   rC  r   0_convert_non_diffusers_lumina2_lora_to_diffusers  s*   "rN  c           "   
      s	  i }dd |   D dd D }t|}t|}tdd D o+tdd D }tdd D r7d	nd
tdd D rDdndtdd D }fdd}t D ]$}|drtd|v rt| t	d| d d|v r|s| q[t
||d D ]m}	tg dg dD ]\}
}d|	 d|
 d}|v }d|	 d|
 d d}d|	 d| d}d|	 d|
 d d}d|	 d| d }|r|}|}|||\}}|| ||< || ||< n|v r|||< |v r|||< d|	 d|
 d!}d|	 d| d"}|v r#|||< qtg dg dD ]\}
}d|	 d#|
 d}|v }d|	 d#|
 d d}d|	 d$| d}d|	 d#|
 d d}d|	 d$| d }|v rv|}|||< |v r|}|||< |r|||\}}||  |9  < ||  |9  < d|	 d#|
 d!}d|	 d$| d"}|v r|||< q-|r[td%d&gd'd(gD ]\}
}d|	 d#|
 d}|v }d|	 d#|
 d d}d|	 d$| d}d|	 d#|
 d d}d|	 d$| d }|v r|}|||< |v r!|}|||< |r;|||\}}||  |9  < ||  |9  < d|	 d#|
 d!}d|	 d$| d"}|v rY|||< qtd)d*gd+d,gD ]\}
}d|	 d|
 d}|v }d|	 d|
 d d}d|	 d-| d}d|	 d|
 d d}d|	 d-| d }|v r|}|||< |v r|}|||< |r|||\}}||  |9  < ||  |9  < d|	 d|
 d!}d|	 d-| d"}|v r|||< qdqrtd.d D r7d/ d}d0}|v r|||< d/ d}d1}|v r+|||< d2v r7d2|d3< td4d D rtfd5d| D rXd6 d|d7< tfd8d| D rod6 d|d9< d:v r{d:|d;< td<d | D rd6 d| v rtd= d> d?|d7< |d7 }|jd@ |d; jd@ f}tj||j|jdAj|d9< dBD ]~tfdCdD r;dDD ]l  d@krdndE}dFkrdGndH}t fdIdD r d  d d|| dJ| d<  d  d d|| dJ| d <  d  d!v r9 d  d!|| dJ| d"< qΐqdKD ]W\}}| d d}dL| d}|v r]|||< | d d}dL| d }|v r|||< |d dd! }|v r|ddM }|||< q?td@krtdNd D } | rdOd D }!tdPd |!D sttdQ n	tdR t| D ]}|||dS| < q|S )TNc                 S   r9  r:  r;  r   r   r   r   r6     r0   z@_convert_non_diffusers_wan_lora_to_diffusers.<locals>.<dictcomp>c                 S   s(   h | ]}| d rt|dd qS )blocks.r<   r(   )r   rB   rC   rf   r   r   r   rI    s   ( z?_convert_non_diffusers_wan_lora_to_diffusers.<locals>.<setcomp>c                 s   r"  k_imgNr   rf   r   r   r   r!     r"   z?_convert_non_diffusers_wan_lora_to_diffusers.<locals>.<genexpr>c                 s   r"  v_imgNr   rf   r   r   r   r!     r"   c                 s   r"  )r  Nr   rf   r   r   r   r!     r"   r  r  c                 s   r"  )r  Nr   rf   r   r   r   r!     r"   r  r  c                 s   s$    | ]}| d o|dV  qdS )time_projectionr  N)r   r   rf   r   r   r   r!     s    
c                    X   | j d } | }|| }|}d}|d |k r(|d9 }|d }|d |k s||fS Nr   r   r   r   r@   r   r   rF  r   r   r   r   r   r  r   r   get_alpha_scales#     
zF_convert_non_diffusers_wan_lora_to_diffusers.<locals>.get_alpha_scales)r  r  normz	Removing zG key from the state dict as it is a norm diff key. This is unsupported.rT  r(   r  rg   r   or]  r^  r_  zto_out.0rO  z.self_attn.ro   r<   r  .attn1.r   r   r  z.lora_B.biasz.cross_attn..attn2.rQ  rS  rd  re  zffn.0zffn.2r)  r*  .ffn.c                 s   r"  )rT  Nr   rf   r   r   r   r!     r"   ztime_projection.1.z*condition_embedder.time_proj.lora_A.weightz*condition_embedder.time_proj.lora_B.weightztime_projection.1.diff_bz(condition_embedder.time_proj.lora_B.biasc                 s   r"  )	head.headNr   rf   r   r   r   r!     r"   c                 3        | ]}d   d|v V  qdS 
head.head.r  Nr   rf   )lora_down_keyr   r   r!     rh   re  zproj_out.lora_A.weightc                 3   rc  rd  r   rf   )lora_up_keyr   r   r!     rh   zproj_out.lora_B.weightzhead.head.diff_bzproj_out.lora_B.biasc                 s   r  )rb  r  Nr   rf   r   r   r   r!     r  zEThe state dict seems to be have both `head.head.diff` and `head.head.z#.weight` keys, which is unexpected.zhead.head.diffr   )r   r   )text_embeddingtime_embeddingc                 3       | ]} |v V  qd S r   r   rf   )	text_timer   r   r!     r"   )r   r   r   ri  z condition_embedder.text_embedderz condition_embedder.time_embedderc                 3   s"    | ]} d   |v V  qdS r<   Nr   rf   )b_nrl  r   r   r!     r  z.linear_))zff.net.0.projzimg_emb.proj.1)zff.net.2zimg_emb.proj.3z"condition_embedder.image_embedder.r  c                 s   r"  )r  Nr   rf   r   r   r   r!     r"   c                 S   s   h | ]	}| d r|qS )r  rh  rf   r   r   r   rI        c                 s   s    | ]}d |vV  qdS )loraNr   rf   r   r   r   r!     r"   zThe remaining `state_dict` contains `diff` keys which we do not handle yet. If you see performance issues, please file an issue: https://github.com/huggingface/diffusers//issues/newN`state_dict` should be empty at this point but has original_state_dict.keys()=ro  )r   minmaxr?   r=   r>   r   r@   r   debugr   r   r   r   r	   eyer   r   Tr  r   r   rE   )"r4   r  block_numbers	min_block	max_blockis_i2v_lorahas_time_projection_weightrY  r    rX   r]  crF  	has_alphaoriginal_key_Aconverted_key_Aoriginal_key_Bconverted_key_Br   r   r   r   rA  converted_keydown_matrix_headup_matrix_shapediffusers_b_nr   img_ours
img_theirsbias_key_theirsbias_keydiff	diff_keysr   )rn  rf  rg  r  rl  r   ,_convert_non_diffusers_wan_lora_to_diffusers  s  $
































r  c              	      s$  i }dd |   D  tdd  D }tdd  D o%tdd  D } fdd	}t|D ]:}tg d
g dD ]C\}} d| d| d} d| d| d}	||d| d| \}
}||
 |d| d| d< |	| |d| d| d< q<tg d
g dD ]C\}} d| d| d} d| d| d}	||d| d| \}
}||
 |d| d| d< |	| |d| d| d< q|rtddgddgD ]C\}} d| d| d} d| d| d}	||d| d| \}
}||
 |d| d| d< |	| |d| d| d< qtddgddgD ]D\}} d| d| d} d| d| d}	||d| d| \}
}||
 |d| d| d< |	| |d| d| d< q&q0t d kr|td!  t| D ]}|||d"| < q|S )#Nc                 S   r9  rm  r;  r   r   r   r   r6     r0   z9_convert_musubi_wan_lora_to_diffusers.<locals>.<dictcomp>c                 S   s$   h | ]}| d d  dd qS )blocks_r(   r   r   )rC   rf   r   r   r   rI    s   $ z8_convert_musubi_wan_lora_to_diffusers.<locals>.<setcomp>c                 s   r"  rP  r   rf   r   r   r   r!     r"   z8_convert_musubi_wan_lora_to_diffusers.<locals>.<genexpr>c                 s   r"  rR  r   rf   r   r   r   r!     r"   c                    s\   | j d } |d  }|| }|}d}|d |k r*|d9 }|d }|d |k s||fS )Nr   ro   r   r   rW  )r   r    r   r   r   r   r   r  r   r   rY    s   
z?_convert_musubi_wan_lora_to_diffusers.<locals>.get_alpha_scalesr\  r^  r  _self_attn_r   rn   rO  r_  r   r   _cross_attn_r`  rQ  rS  rd  re  ffn_0ffn_2r)  r*  r   ra  r   rq  ro  )	r   r   r?   r   r   r@   rE   r>   r=   )r4   r  
num_blocksrz  rY  rX   r]  r|  r   r   r   r   r    r   r  r   %_convert_musubi_wan_lora_to_diffusers  sJ   $r  diffusion_modelc                    J   t  fdd| D std fdd|  D }dd | D }|S )Nc                 3   s    | ]}|  V  qd S r   r<  rf   non_diffusers_prefixr   r   r!   O  r=  zC_convert_non_diffusers_hidream_lora_to_diffusers.<locals>.<genexpr>z$Invalid LoRA state dict for HiDream.c                    "   i | ]\}}|   d |qS rv   removeprefixr   r  r   r   r6   Q  r0   zD_convert_non_diffusers_hidream_lora_to_diffusers.<locals>.<dictcomp>c                 S   r}   ro  r   r   r   r   r   r6   R  r   r   rE   r   r4   r  r  r   r  r   0_convert_non_diffusers_hidream_lora_to_diffusersN  
   r  c                    r  )Nc                 3   s     | ]}|   d V  qdS rm  r<  rf   r  r   r   r!   W  rh   z@_convert_non_diffusers_ltxv_lora_to_diffusers.<locals>.<genexpr>z&Invalid LoRA state dict for LTX-Video.c                    r  rv   r  r   r  r   r   r6   Y  r0   zA_convert_non_diffusers_ltxv_lora_to_diffusers.<locals>.<dictcomp>c                 S   r}   r  r   r   r   r   r   r6   Z  r   r  r  r   r  r   -_convert_non_diffusers_ltxv_lora_to_diffusersV  r  r  c                    s(   fdd|   D }  fdd|   D } dkr(ddddd	d
ddddd
}nddi}i }|  D ]\}}|d d  }|  D ]
\}}	|||	}q@|||< q2i }
|  D ](\}}|drj|dd}||
|< qV|drz|dd}||
|< qV||
|< qV dkrdndfdd|
  D }
|
S )Nc                    s&   i | ]\}}|   d r||qS rv   r<  r   r  r   r   r6   `  rp  zA_convert_non_diffusers_ltx2_lora_to_diffusers.<locals>.<dictcomp>c                    r  rv   r  r   r  r   r   r6   a  r0   r  r   audio_proj_inav_cross_attn_video_scale_shiftav_cross_attn_video_a2v_gateav_cross_attn_audio_scale_shiftav_cross_attn_audio_v2a_gate&video_a2v_cross_attn_scale_shift_table&audio_a2v_cross_attn_scale_shift_tablenorm_qnorm_k)
patchify_projaudio_patchify_proj$av_ca_video_scale_shift_adaln_singleav_ca_a2v_gate_adaln_single$av_ca_audio_scale_shift_adaln_singleav_ca_v2a_gate_adaln_singlescale_shift_table_a2v_ca_videoscale_shift_table_a2v_ca_audior  r  aggregate_embedtext_proj_inzadaln_single.ztime_embed.zaudio_adaln_single.zaudio_time_embed.transformer
connectorsc                    ru   rv   r   r   r   r   r   r6     r{   )r   r   r   )r4   r  r  rename_dictrenamed_state_dictr    r  r^   old_patternnew_patternfinal_state_dictr   )r  r   r   -_convert_non_diffusers_ltx2_lora_to_diffusers^  sB   





r  c                    s  t dd D }|rdd  D t dd D }|r<dd  D dtdtfd	d
fdd D t dd D }|rPdd  D i }t }ddd dt fdd|D }t  fdd|D }|rfdd}|D ]>}	|	r|	d}
|	d}|	d}|	}|	}|||\}}|| ||
< || ||< qn |r|D ]}	 |	v sЈ|	v r؈|	||	< qd|	v r|	 qtdkrt	d dd | D }|S )Nc                 s   r9  r:  r<  rf   r   r   r   r!     r=  z@_convert_non_diffusers_qwen_lora_to_diffusers.<locals>.<genexpr>c                 S      i | ]
\}}| d |qS r  r  r   r   r   r   r6     r   zA_convert_non_diffusers_qwen_lora_to_diffusers.<locals>.<dictcomp>c                 s   r9  rm  r<  rf   r   r   r   r!     r=  c                 S   r  re   r  r   r   r   r   r6     r   r    r  c              	   S   s  d}d| v r|  dd\}}n| d}}| d}|t|d  }d|v r2|dd\}}d| }n|d}}h d}i }	|D ]}
|	t|
t |
 q?|d}g }d}t|	 dd	}|t|k rd
}|D ].}|| t|krt||||  |	| v r|	d
||||   ||7 }d} nqi|s|	||  |d7 }|t|k sed
|}| d| | }||rd|  S d S )Nr   r<   r(   r   r   >   rD   rg   rD   r  rD   r   imgr   r  modtxtr   r  r  torD   outrD   rg   projrD   r  r  rD   r   r  r  rg   r  r  r  r   r  r  r   TreverseF)rsplitr   rC   
setdefaultrA   rD   sortedr>   tupler  rH   )r    r   baser}  r  restheadtail	protectedprot_by_lenngpartsmergedrX   lengths_descmatchedLhead_convertedconverted_baser   r   r   convert_key  sD   




,
zB_convert_non_diffusers_qwen_lora_to_diffusers.<locals>.convert_keyc                       i | ]	\}} ||qS r   r   r   r  r   r   r6     ro  c                 s   r"  default.Nr   rf   r   r   r   r!     r"   c                 S   r  r  r   r  r   r   r   r   r6     r  r   rn   r   r   c                 3        | ]} |v p|v V  qd S r   r   rf   rC  up_keyr   r   r!     rh   c                 3   r  r   r   rf   a_keyb_keyr   r   r!     rh   c                    rU  rV  rW  rX  r4   r   r   rY    rZ  zG_convert_non_diffusers_qwen_lora_to_diffusers.<locals>.get_alpha_scalesro   r   rG  c                 S   r}   r  r   r   r   r   r   r6   		  r   )
r?   r   rG   r=   r>   r   r   r@   r   rE   )r4   has_diffusion_modelhas_lora_unethas_defaultr  rK   has_non_diffusers_lora_idhas_diffusers_lora_idrY  rg   rG  diffusers_up_keyrF  r   r   r   r   r   )r  r  r  rC  r4   r  r   -_convert_non_diffusers_qwen_lora_to_diffusers  sX   >


r  c           (         sx  i }d  fdd|   D }tdd | D }|r7i }|  D ]\}}|dddd	}|||< q"|}d
}d
}	| D ])}
|
drUt|	t|
dd d }	q?|
drht|t|
dd d }q?d}d}t|	D ]D}d| }d| d}|D ]4}| d| d}||v r|	||| d| d< | d| d}||v r|	||| d| d< qqqt|D ]#}d| }|D ]}|D ]}| d}d| d| d| d}||vrq|	|}|dkr|dkrg dng d}|D ]}t
|g|| d| d| d< qqt
j|dd
d\}}}|dkrHt
|g|| d| d< t
|g|| d | d< t
|g|| d!| d< qt
|g|| d"| d< t
|g|| d#| d< t
|g|| d$| d< qqd%d&g}|D ].\}}|D ]&}d| d| d| d} | |v r| d| d| d}!|	| ||!< qqzg d'}"|"D ].\}#}$|D ]&}d| d|# d| d} | |v r| d|$ d| d}!|	| ||!< qqqd(d)d*d+d,d-d.d/d0d1	}%|%  D ]%\}&}'|D ]}|& d| d} | |v r|	| ||' d| d< qqt|d
kr&td2| dt| D ]}
|	|
|d3|
 < q,|S )4Nr  c                    s"   i | ]\}}|t  d  |qS r   r;  r   r  r   r   r6   	  r0   zB_convert_non_diffusers_flux2_lora_to_diffusers.<locals>.<dictcomp>c                 s   s     | ]}d |v pd|v V  qdS )r  r  Nr   rf   r   r   r   r!   	  rh   zA_convert_non_diffusers_flux2_lora_to_diffusers.<locals>.<genexpr>r  r  r  r  r   r  r<   r(   r  r  )img_attntxt_attnrL  z.attnr  r  z.to_qkv_mlp_proj.r  z.to_out.rN  z.qkv.r  r\  rb  r  r   z.to_q.z.to_k.z.to_v.z.add_q_proj.z.add_k_proj.z.add_v_proj.)zimg_attn.projr  )ztxt_attn.projr#  ))z	img_mlp.0zff.linear_in)z	img_mlp.2zff.linear_out)z	txt_mlp.0zff_context.linear_in)z	txt_mlp.2zff_context.linear_outr*  r-  z.time_guidance_embed.timestep_embedder.linear_1z.time_guidance_embed.timestep_embedder.linear_2r   rQ  zsingle_stream_modulation.linearz#double_stream_modulation_img.linearz#double_stream_modulation_txt.linear)	r(  r+  ztime_in.in_layerztime_in.out_layerr1  r  zsingle_stream_modulation.linz double_stream_modulation_img.linz double_stream_modulation_txt.linr  ro  )r   r?   r>   r   r   rs  rB   rC   r   r@   r	   r
   r   r   rE   r=   )(r4   r  r  has_lora_down_uptemp_state_dictrg   r   r^   num_double_layersr  r    	lora_keys
attn_typesslsingle_block_prefixattn_prefixr  linear1_keylinear2_keydltransformer_block_prefix	attn_typeqkv_keyfused_qkv_weightdiff_attn_proj_keysproj_keyr  r  r  proj_mappingsorg_proj	diff_projrA  rB  mlp_mappingsorg_mlpdiff_mlpextra_mappingsorg_keydiff_keyr   r  r   ._convert_non_diffusers_flux2_lora_to_diffusers	  s   








 


r  c              
   C   sN  dd }d,dd}d}d}|   D ]-}|dr)t|dd }t||d	 }q|d
r>t|dd }t||d	 }qi }t|D ]}|| |d| dd| d || |d| dd| dd| dd| dg || |d| dd| d || |d| dd| dd| dd| dg || |d| dd| d || |d| dd| d || |d| dd| d || |d| dd| d qEt|D ] }|| |d
| d d!| d" || |d
| d#d!| d$ qd%d&d'd(d)d*}	|	 D ]\}
}|| ||
| qt|   }|r%t	d+|  |S )-Nc                 S   r   r   r   r   r   r   r   r   	  s   
zF_convert_kohya_flux2_lora_to_diffusers.<locals>._convert_to_ai_toolkitc              
   S   r   )Nr   rn   r   Fr   ro   r   r   Tr(   r   c                 S   r   r   r   rf   r   r   r   r/   	  r   z^_convert_kohya_flux2_lora_to_diffusers.<locals>._convert_to_ai_toolkit_cat.<locals>.<listcomp>c                 S   r   r   r   rf   r   r   r   r/   	  r   c                 S   r   r   r   r   r   r   r   r6   	  r   z^_convert_kohya_flux2_lora_to_diffusers.<locals>._convert_to_ai_toolkit_cat.<locals>.<dictcomp>r   c                 S   r   r   r   r   r   r   r   r6   	  r   r   r   r   r   r   r   	  s`   
**&4zJ_convert_kohya_flux2_lora_to_diffusers.<locals>._convert_to_ai_toolkit_catr   r   r   r?  r(   r	  r   r   r   r   r   r   r   r   r   r  r  r  r  r   z.ff.linear_inr   z.ff.linear_outr  z.ff_context.linear_inr  z.ff_context.linear_outr
  r  z.attn.to_qkv_mlp_projr  z.attn.to_outztransformer.x_embedderztransformer.context_embedderz:transformer.time_guidance_embed.timestep_embedder.linear_1z:transformer.time_guidance_embed.timestep_embedder.linear_2ztransformer.proj_out)r)  r,  r/  r1  lora_unet_final_layer_linearz2Unsupported keys for Kohya Flux2 LoRA conversion: r   )
r>   r   rB   rC   rs  r   r   r=   r   r>  )r4   r   r   r  r  r    	block_idxr   rX   r  r   r   r?  r   r   r   &_convert_kohya_flux2_lora_to_diffusers	  s   
:


























	r  c                    s  t dd D }|rdd  D t dd D }|r<dd  D dtdtfd	d
fdd D dtdtfddfdd D t dd D }|rddd  D dd  D i }t }ddd dt fdd|D }t  fdd|D }fdd}|r|D ]>}	|	r|	d}
|	d}|	d}|	}|	}|||\}}|| ||
< || ||< qn7|r|D ]1}	|	 r|	 }|	 d}|	}|}|||\}}|| ||	< || ||< qdd}t fddD }|rt }|D ]}	|	vr9q0|	vr@q0|	dt  }|d rh|	 |	|d |d d q0t	
d!|rd"|vr|	 |	|d q0t	d#t	 d$ d% |	}|dt  }|d }|d}|d}|	}|	|}|||\}}|| ||< || ||< q0td&krtd' d(d | D }|S ))uG  
    Convert non-diffusers ZImage LoRA state dict to diffusers format.

    Handles:
    - `diffusion_model.` prefix removal
    - `lora_unet_` prefix conversion with key mapping
    - `default.` prefix removal
    - `.lora_down.weight`/`.lora_up.weight` → `.lora_A.weight`/`.lora_B.weight` conversion with alpha scaling
    c                 s   r9  r:  r<  rf   r   r   r   r!   N
  r=  zC_convert_non_diffusers_z_image_lora_to_diffusers.<locals>.<genexpr>c                 S   r  r  r  r   r   r   r   r6   P
  r   zD_convert_non_diffusers_z_image_lora_to_diffusers.<locals>.<dictcomp>c                 s   r9  rm  r<  rf   r   r   r   r!   R
  r=  c                 S   r  r  r  r   r   r   r   r6   T
  r   r    r  c              	   S   s,  d| v r|  dd\}}n| d}}h d}i }|D ]}|t|t | q|d}g }d}t| dd}	|t|k rd	}
|	D ].}|| t|krrt||||  || v rr|	d
||||   ||7 }d}
 nqD|
s|	||  |d7 }|t|k s@d
|}||rd|  S d S )
Nr<   r(   r   >   feedforwardr  r  r  r  r   r   Tr  F)r  r  r   rA   rD   rC   r  r>   r  r  rH   )r    r  r}  r  r  r  r  r  rX   r  r  r  r  r   r   r   r  V
  s4   


,
zE_convert_non_diffusers_z_image_lora_to_diffusers.<locals>.convert_keyc                    r  r   r   r   r  r   r   r6   
  ro  rg   c                 S   s   d| v r| S t dd| S )N.to_outz2\.out(?=\.(?:lora_down|lora_up)\.weight$|\.alpha$)z	.to_out.0)r   r   r[  r   r   r   normalize_out_key
  s   zK_convert_non_diffusers_z_image_lora_to_diffusers.<locals>.normalize_out_keyc                    r  r   r   r   )r!  r   r   r6   
  ro  c                 s   r"  r  r   rf   r   r   r   r!   
  r"   c                 S   r  r  r  r   r   r   r   r6   
  r  c                 S   s&   i | ]\}}| d d dd|qS )zcontext.refiner.zcontext_refiner.znoise.refiner.znoise_refiner.r  r   r   r   r   r6   
  s    r   rn   r   r   c                 3   r  r   r   rf   r  r   r   r!   
  rh   c                 3   r  r   r   rf   r  r   r   r!   
  rh   c                    rU  rV  rW  rX  r  r   r   rY  
  rZ  zJ_convert_non_diffusers_z_image_lora_to_diffusers.<locals>.get_alpha_scalesro   z.lora.down.weightz.lora.up.weightc                 3   rk  r   r   rf   )lora_dot_down_keyr   r   r!   
  r"   Nz.qkvz\.out$r   z\.to\.([qkv])$z.to_\1r   rG  c                 S   r}   r  r   r   r   r   r   r6   
  r   )r?   r   rG   r=   r>   r   r   r@   r   r   rs  r   r  rE   )r4   r  r  r  r  rK   r  r  rY  rg   rG  r  rF  r   r   r   r   lora_dot_up_keyhas_lora_dot_formatdot_keysr  r  	norm_basediffusers_downdiffusers_upr   )r  r  r  rC  r"  r!  r4   r  r   0_convert_non_diffusers_z_image_lora_to_diffusersD
  s   
,	








r*  )r   r   )ra   rb   )r  )!r   r	   utilsr   r   r   
get_logger__name__r   r   r`   r   r   r   r   r  r  rG   r=   r  r  r  r8  rN  r  r  r  r  r  r  r  r  r*  r   r   r   r   <module>   sH   


yl8    2i  / _ 0G ~
=

0~ :