o
    pi7                     @   sR   d dl Z ddlmZmZ eeZdddZdd
dZdd Z	dd Z
dd ZdS )    N   )is_peft_versionlogging_   c                    s  t  }g d}d}|D ]tfdd|D rd} nq|s$S i }g d}t t t }	}
}|D ]F}d|v rE|||< q7t| d | d }|d	 |v r^|	| q7|d
 |v rj|
| q7|d |v rv|| q7td| d fdd|	D } fdd|
D } fdd|D }|	D ]_}|d
 |j	d
  }|d
 |j	d
  }|| D ]Ft | }dvr|| nd}dvrt
|nd}  d |d
  t
|||g  |d
 d   }||< qq|
D ]Y}d }|d	kr|d	 dg}n |d
kr|d
 dg}n|dkr(|d	 dg}ntd| d|| D ]#  d |d
  |  |d   }||< q4q |D ]X}||j	d
  }||j	d
  }|| D ]Bt | }|| }|dk rt
|nd}  d |d
  t
|||g  |d
 d   }||< qpq\td	krtd|S )N)input_blocksmiddle_blockoutput_blocksFc                 3       | ]}| v V  qd S N ).0p)keyr   e/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/diffusers/loaders/lora_conversion_utils.py	<genexpr>       z5_maybe_map_sgm_blocks_to_diffusers.<locals>.<genexpr>T)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><      " A_maybe_map_sgm_blocks_to_diffusers.<locals>.<dictcomp>.<listcomp>r   r   r   
state_dictr   r   
<dictcomp>;       z6_maybe_map_sgm_blocks_to_diffusers.<locals>.<dictcomp>c                    r   )c                    r   )r   r   r   r   r   r   r   @   r    r!   r   r"   r#   r%   r   r&   ?   r'   c                    r   )c                    r   )r	   r   r   r   r   r   r   D   r    r!   r   r"   r#   r%   r   r&   C   r'   opdownsamplers01zInvalid middle block id .z:At this point all state dict entries have to be converted.)listkeysanysetpopintsplitadd
ValueErrorlayers_per_blockstrjoinlen)r$   unet_configr   block_slice_posall_keys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   r$   r   "_maybe_map_sgm_blocks_to_diffusers   s   


*rN   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 )rR   lora_te_	lora_te1_Nr   rT   r   r   r   r      s   & c                 s   rQ   )rR   	lora_te2_Nr   rT   r   r   r   r      rV   <z0.9.0zeYou need `peft` 0.9.0 at least to use DoRA-enabled LoRAs. Please upgrade your installation of `peft`.zlora_down.weightr,   r   z.lora_up.weight.alpharS   z.down.z.up.z_lora.down.z.lora.down.rR   z.lora_magnitude_vector.)rW   rX   rY   rW   rX   .lora_linear_layer.rY   z7The following keys have not been correctly renamed: 

 z, z"Non-diffusers checkpoint detected.c                        i | ]\}}  d | |qS r,   r   r   module_nameparams)	unet_namer   r   r&           z<_convert_non_diffusers_lora_to_diffusers.<locals>.<dictcomp>c                    r^   r_   r   r`   )text_encoder_namer   r   r&      rd   c                 S   s   i | ]
\}}d | |qS )text_encoder_2.r   r`   r   r   r   r&      s    N)r/   r   r5   r-   r.   endswithr3   
startswith_convert_unet_lora_keyr1   replace_convert_text_encoder_lora_keyitemupdate_get_alpha_namer9   r8   loggerinfoitems)r$   rc   re   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alphar?   r   )re   rc   r   (_convert_non_diffusers_lora_to_diffusers~   s   














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.
    rS    r   r,   zinput.blocksdown_blockszdown.blockszmiddle.block	mid_blockz	mid.blockzoutput.blocks	up_blocksz	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conv2r)   r   r(   convskipzskip.connectionconv_shortcutzconv.shortcutattn1attn2zattn1.processorzattn2.processorffc                 3   r
   r   r   r   r}   r   r   r     r   z)_convert_unet_lora_key.<locals>.<genexpr>)r   r   )rj   resubr/   )r   patternr   r   r   ri      sV   ri   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.
    r\   rW   rX   rY   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.r]   )rh   rj   )r   rz   key_to_replacer}   r   r   r   rk   "  s"   
rk   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.
    rS   zunet.r\   ztext_encoder.rf   r   r   r[   )rh   r3   )r|   r}   r   prefixnew_namer   r   r   rn   =  s   

rn   )r   r   )rO   rP   )r   utilsr   r   
get_logger__name__ro   rN   r   ri   rk   rn   r   r   r   r   <module>   s   


gl8