o
    Ni                     @  s|   d dl mZ d dlZd dlZd dlmZ d dlZd dlmZ d dl	m
Z
mZ d dlmZ ddlmZmZ G d	d
 d
e
ZdS )    )annotationsN)chain)Conv1D)	BaseTunerBaseTunerLayer)7TRANSFORMERS_MODELS_TO_FOURIERFT_TARGET_MODULES_MAPPING   )FourierFTLayerFourierFTLinearc                   @  s:   e Zd ZU dZdZded< eZeZ	dd Z
edd Zd	S )
FourierFTModela*  
    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                   s    d u rt dtt|j }tt fdd| }	|j|	|j}
|j	}|j
}t|do4|jd u}|
||j|j|j
d}||d< t|trU|||
||j| d S | j|||fi |}|| jkrj|d | |||| d S )NzCurrent Key shouldn't be `None`c                   s   t d|  d S )Nz.*\.$)rematch)keycurrent_key O/home/ubuntu/.local/lib/python3.10/site-packages/peft/tuners/fourierft/model.py<lambda>F   s    z4FourierFTModel._create_and_replace.<locals>.<lambda>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_moduler   r   r   _create_and_replace8   s6   



z"FourierFTModel._create_and_replacec                 K  s   t |tr
| }n|}t |tjjr$|d r#td d |d< | _n"t |t	r>d|d< |d s=td d |d< | _nt
d| dt||fi |}|S )	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
   )r-   r.   r/   r5   target_base_layerr6   r   r   r   r(   c   s.   



z!FourierFTModel._create_new_moduleN)__name__
__module____qualname____doc__r   __annotations__r	   tuner_layer_clsr   target_module_mappingr7   staticmethodr(   r   r   r   r   r      s   
 +r   )
__future__r   r   r=   	itertoolsr   r:   transformers.pytorch_utilsr   peft.tuners.tuners_utilsr   r   
peft.utilsr   layerr	   r
   r   r   r   r   r   <module>   s   