
    h              	           S r SSKr " S S\R                  5      r0 SS_SS_S	S
_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS _S!S"_S#S$_rSSS
SSSSSS%.r\R                  \\R                  \0rS&S'0r	S( r
S*S) jrg)+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                        \ rS rSrSrSrSrSrg)StateDictType   z.
The mode to use when converting state dicts.
diffusers_oldpeft N)__name__
__module____qualname____firstlineno____doc__DIFFUSERS_OLDPEFT__static_attributes__r       M/home/james-whalen/.local/lib/python3.13/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                 "   0 nU R                  5        Hx  u  p4[        R                  5        H$  nXS;   d  M
  [        U   nUR                  XV5      nM&     UR                  5        H  nXS;   d  M
  X   nUR                  XV5      n  O   XBU'   Mz     U$ )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        "-224G|4W=IIg3 5
 ||~G|%.IIg3	 &
 #$Q #  r   c                   ^ UR                  SS5      mTb  ST-   mOSmUc  [        S U R                  5        5       5      (       a  [        R                  nOk[        U4S jU R                  5        5       5      (       a  [        R
                  nO2[        S U R                  5        5       5      (       a  U $ [        S5      eU[        R                  5       ;  a  [        S	U S
35      e[        U   n[        X5      $ )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   ,   #    U  H
  nS U;   v   M     g7f)to_out_loraNr   .0r   s     r   	<genexpr>2convert_state_dict_to_diffusers.<locals>.<genexpr>   s     =+<a}!+<   c              3   6   >#    U  H  nS T S3U;   v   M     g7f)z.lora_Az.weightNr   )r'   r   peft_adapter_names     r   r(   r)      s#     VDUq7,-W5:DUs   c              3   ,   #    U  H
  nS U;   v   M     g7f)lora_linear_layerNr   r&   s     r   r(   r)      s     E3Da$)3Dr*   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_diffusersr5   i   s    , 

>48$"33=:??+<===)77MVJOODUVVV)..ME:??3DE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   r2   r   r    r5   r   r   r   <module>r:      sN  
 DII 46 4 6	
 4 6 8 : # > # > # > +  )! ( 4737377;	  !;)!  3 
 B,3r   