
    bi              	           d Z ddlZ G 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( Z
d*d)Zy)+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                       e Zd ZdZdZdZy)StateDictTypez6
    The mode to use when converting state dicts.
    diffusers_oldpeftN)__name__
__module____qualname____doc__DIFFUSERS_OLDPEFT     N/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/trl/models/sd_utils.pyr   r      s     $MDr   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                    i }| j                         D ]u  \  }}t        j                         D ]"  }||v st        |   }|j                  ||      }$ |j                         D ]  }||v s||   }|j                  ||      } n |||<   w |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_patterns          r   convert_state_dictr   H   s        " $1-224 	4G!|4W=IIg{3	4
 ||~ 	G!|%g.IIg{3		
 #$Q$  r   c                    |j                  dd      dz   nd|t        d | j                         D              rt        j                  }n`t        fd| j                         D              rt        j
                  }n-t        d | j                         D              r| S t        d      |t        j                         vrt        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              3   $   K   | ]  }d |v  
 yw)to_out_loraNr   .0r   s     r   	<genexpr>z2convert_state_dict_to_diffusers.<locals>.<genexpr>   s     =a}!=   c              3   .   K   | ]  }d  d|v   yw)z.lora_Az.weightNr   )r$   r   peft_adapter_names     r   r%   z2convert_state_dict_to_diffusers.<locals>.<genexpr>   s!     Vq7,-W5:Vs   c              3   $   K   | ]  }d |v  
 yw)lora_linear_layerNr   r#   s     r   r%   z2convert_state_dict_to_diffusers.<locals>.<genexpr>   s     Ea$)Er&   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(   s       @r   convert_state_dict_to_diffusersr1   i   s    , 

>48$"33=:??+<==)77MVJOODUVV)..ME:??3DEELMM9>>@@>-8IJKK+M:Gj'22r   )N)r
   enumEnumr   PEFT_TO_DIFFUSERSDIFFUSERS_OLD_TO_DIFFUSERSr   r   r.   r   r   r1   r   r   r   <module>r6      sN  
 DII 46 4 6	
 4 6 8 : # > # > # > +  )! ( 4737377;	  !;)!  3 
 B,3r   