o
    NiG                     @  sh   d dl mZ d dlZd dlmZ d dl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)	BaseTunerBaseTunerLayer)1TRANSFORMERS_MODELS_TO_C3A_TARGET_MODULES_MAPPING   )C3ALayer	C3ALinearc                   @  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 )
C3AModelab  
    Creates C3A model from a pretrained transformers model.

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

    Args:
        model ([`torch.nn.Module`]): The model to be adapted.
        config ([`C3AConfig`]): The configuration of the C3A model.
        adapter_name (`str`): The name of the adapter, defaults to `"default"`.

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

    **Attributes**:
        - **model** ([`~transformers.PreTrainedModel`]) -- The model to be adapted.
        - **peft_config** ([`C3AConfig`]): The configuration of the C3A model.
    c3a_strprefixc                   s    d u rt dtt|j }tt fdd| }	|j|	|j}
|
|j	d}t
|tr:|||
|j	 d S | j|||fi |}|| jkrO|d | |||| d S )NzCurrent Key shouldn't be `None`c                   s   t d|  d S )Nz.*\.$)rematch)keycurrent_key I/home/ubuntu/.local/lib/python3.10/site-packages/peft/tuners/c3a/model.py<lambda>B   s    z.C3AModel._create_and_replace.<locals>.<lambda>)
block_sizeinit_weightsF)
ValueErrorlistr   block_size_patternkeysnextfiltergetr   r   
isinstancer	   update_layer_create_new_moduleactive_adapterrequires_grad__replace_module)self
c3a_configadapter_nametargettarget_nameparentr   optional_kwargspattern_keystarget_name_keyr   kwargs
new_moduler   r   r   _create_and_replace4   s$   



zC3AModel._create_and_replacec                 K  s<   t |tr
| }n|}t |tjjrt||fi |}|S )N)r    r   get_base_layertorchnnLinearr	   )r'   r(   r)   r/   target_base_layerr0   r   r   r   r"   W   s   

zC3AModel._create_new_moduleN)__name__
__module____qualname____doc__r   __annotations__r   tuner_layer_clsr   target_module_mappingr1   staticmethodr"   r   r   r   r   r
      s   
 #r
   )
__future__r   r   	itertoolsr   r3   peft.tuners.tuners_utilsr   r   
peft.utilsr   layerr   r	   r
   r   r   r   r   <module>   s   