o
    	Ti                  	   @   s   d Z ddlZG dd dejZ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$Zddd
dddddd%ZejeejeiZd&d'iZ	d(d) Z
d,d*d+ZdS )-z
State dict utilities: utility methods for converting state dicts easily File copied from diffusers to avoid import
issues and make TRL compatible with most of diffusers versions.
    Nc                   @   s   e Zd ZdZdZdZdS )StateDictTypez6
    The mode to use when converting state dicts.
    diffusers_oldpeftN)__name__
__module____qualname____doc__DIFFUSERS_OLDPEFT r   r   G/home/ubuntu/.local/lib/python3.10/site-packages/trl/models/sd_utils.pyr      s    r   z.q_proj.lora_Bz.q_proj.lora_linear_layer.upz.q_proj.lora_Az.q_proj.lora_linear_layer.downz.k_proj.lora_Bz.k_proj.lora_linear_layer.upz.k_proj.lora_Az.k_proj.lora_linear_layer.downz.v_proj.lora_Bz.v_proj.lora_linear_layer.upz.v_proj.lora_Az.v_proj.lora_linear_layer.downz.out_proj.lora_Bz.out_proj.lora_linear_layer.upz.out_proj.lora_Az .out_proj.lora_linear_layer.downzto_k.lora_Azto_k.lora.downzto_k.lora_Bzto_k.lora.upzto_q.lora_Azto_q.lora.downzto_q.lora_Bzto_q.lora.upzto_v.lora_Azto_v.lora.downzto_v.lora_Bzto_v.lora.upzto_out.0.lora_Azto_out.0.lora.downzto_out.0.lora_Bzto_out.0.lora.up)z.to_q_lora.upz.to_q_lora.downz.to_k_lora.upz.to_k_lora.downz.to_v_lora.upz.to_v_lora.downz.to_out_lora.upz.to_out_lora.downz.processor..c                 C   sz   i }|   D ]4\}}t D ]}||v rt| }|||}q| D ]}||v r5|| }|||} nq#|||< q|S )a&  
    Simply iterates over the state dict and replaces the patterns in `mapping` with the corresponding values.

    Args:
        state_dict (`dict[str, torch.Tensor]`):
            The state dict to convert.
        mapping (`dict[str, str]`):
            The mapping to use for conversion, the mapping should be a dictionary with the following structure:
                - key: the pattern to replace
                - value: the pattern to replace with

    Returns:
        converted_state_dict (`dict`)
            The converted state dict.
    )itemsKEYS_TO_ALWAYS_REPLACEkeysreplace)
state_dictmappingconverted_state_dictkvpatternnew_patternr   r   r   convert_state_dictH   s   
r   c                    s   | dd  durd   nd |du rFtdd |  D r$tj}n"t fdd|  D r5tj}ntdd |  D rB| S td	|t vrTtd
| dt| }t| |S )a  
    Converts a state dict to new diffusers format. The state dict can be from previous diffusers format
    (`OLD_DIFFUSERS`), or PEFT format (`PEFT`) or new diffusers format (`DIFFUSERS`). In the last case the method will
    return the state dict as is.

    The method only supports the conversion from diffusers old, PEFT to diffusers new for now.

    Args:
        state_dict (`dict[str, torch.Tensor]`):
            The state dict to convert.
        original_type (`StateDictType`, *optional*):
            The original type of the state dict, if not provided, the method will try to infer it automatically.
        kwargs (`dict`, *args*):
            Additional arguments to pass to the method.

            - **adapter_name**: For example, in case of PEFT, some keys will be pre-pended
                with the adapter name, therefore needs a special handling. By default PEFT also takes care of that in
                `get_peft_model_state_dict` method:
                https://github.com/huggingface/peft/blob/ba0477f2985b1ba311b83459d29895c809404e99/src/peft/utils/save_and_load.py#L92
                but we add it here in case we don't want to rely on that method.
    adapter_nameNr    c                 s       | ]}d |v V  qdS )to_out_loraNr   .0r   r   r   r   	<genexpr>       z2convert_state_dict_to_diffusers.<locals>.<genexpr>c                 3   s     | ]}d   d|v V  qdS )z.lora_Az.weightNr   r   peft_adapter_namer   r   r       s    c                 s   r   )lora_linear_layerNr   r   r   r   r   r       r!   z-Could not automatically infer state dict typezOriginal type z is not supported)	popanyr   r   r	   r
   
ValueErrorDIFFUSERS_STATE_DICT_MAPPINGSr   )r   original_typekwargsr   r   r"   r   convert_state_dict_to_diffusersi   s    

r+   )N)r   enumEnumr   PEFT_TO_DIFFUSERSDIFFUSERS_OLD_TO_DIFFUSERSr	   r
   r(   r   r   r+   r   r   r   r   <module>   sh   		
!