
    oi                        S SK Jr  S SKrS SKrS SKJr  S SKrS SKJr  S SK	J
r
Jr  S SKJr  SSKJrJr   " S	 S
\
5      rg)    )annotationsN)chain)Conv1D)	BaseTunerBaseTunerLayer)7TRANSFORMERS_MODELS_TO_FOURIERFT_TARGET_MODULES_MAPPING   )FourierFTLayerFourierFTLinearc                  F    \ rS rSr% SrSrS\S'   \r\	r
S r\S 5       rSrg	)
FourierFTModel   a  
Creates FourierFT model from a pretrained transformers model.

The method is described in detail in https://huggingface.co/papers/2405.03003.

Args:
    model ([`torch.nn.Module`]): The model to be adapted.
    config ([`FourierFTConfig`]): The configuration of the FourierFT model.
    adapter_name (`str`): The name of the adapter, defaults to `"default"`.
    low_cpu_mem_usage (`bool`, `optional`, defaults to `False`):
        Create empty adapter weights on meta device. Useful to speed up the loading process.

Returns:
    `torch.nn.Module`: The FourierFT model.

**Attributes**:
    - **model** ([`~transformers.PreTrainedModel`]) -- The model to be adapted.
    - **peft_config** ([`FourierFTConfig`]): The configuration of the Fourier model.

fourierft_strprefixc                  ^ Tc  [        S5      e[        [        UR                  R	                  5       5      5      n[        [        U4S jU5      T5      n	UR                  R                  XR                  5      n
UR                  nUR                  n[        US5      =(       a    UR                  S LnU
UUR                  UR                  UR                  S.nXS'   [        U[         5      (       a   UR#                  UU
UUR                  U5        g U R$                  " XU40 UD6nX R&                  :w  a  UR)                  S5        U R+                  XTX5        g )NzCurrent Key shouldn't be `None`c                :   > [         R                  " SU  S3T5      $ )Nz.*\.$)rematch)keycurrent_keys    U/home/james-whalen/.local/lib/python3.13/site-packages/peft/tuners/fourierft/model.py<lambda>4FourierFTModel._create_and_replace.<locals>.<lambda>F   s    "((d3%q>;2W    bias)n_frequencyscalingfan_in_fan_outinit_weightsrandom_loc_seedF)
ValueErrorlistr   n_frequency_patternkeysnextfiltergetr   r   r"   hasattrr   r    r!   
isinstancer
   update_layer_create_new_moduleactive_adapterrequires_grad__replace_module)selffourierft_configadapter_nametargettarget_nameparentr   optional_kwargspattern_keystarget_name_keyr   r   r"   r   kwargs
new_modules         `         r   _create_and_replace"FourierFTModel._create_and_replace8   s?    >??E"2"F"F"K"K"MNOv&WYefhst&::>>PlPlm"***::vv&B6;;d+B&.==,99/??
 vfn-- -- 001AQWb[abJ222))%0  jIr   c                   [        U[        5      (       a  UR                  5       nOUn[        U[        R                  R
                  5      (       a-  US   (       a"  [        R                  " S5        S=US'   U l        OV[        U[        5      (       a2  SUS'   US   (       d"  [        R                  " S5        S=US'   U l        O[        SU S35      e[        X!40 UD6nU$ )	Nr    zjfan_in_fan_out is set to True but the target module is `torch.nn.Linear`. Setting fan_in_fan_out to False.FTis_target_conv_1d_layerzafan_in_fan_out is set to False but the target module is `Conv1D`. Setting fan_in_fan_out to True.zTarget module zZ is not supported. Currently, only the following modules are supported: `torch.nn.Linear`.)r+   r   get_base_layertorchnnLinearwarningswarnr    r   r#   r   )r2   r3   r4   r:   target_base_layerr;   s         r   r-   !FourierFTModel._create_new_modulec   s    fn-- & 5 5 7 &'99&'7 NSR'(+;+J)62204F,-*+w NRQ'(+;+J  )% % 
 %VDVD
r    N)__name__
__module____qualname____firstlineno____doc__r   __annotations__r
   tuner_layer_clsr   target_module_mappingr<   staticmethodr-   __static_attributes__rH   r   r   r   r      s9    ( FC$OS)JV  r   r   )
__future__r   r   rD   	itertoolsr   rA   transformers.pytorch_utilsr   peft.tuners.tuners_utilsr   r   
peft.utilsr   layerr
   r   r   rH   r   r   <module>rY      s4    # 	    - > 3aY ar   