
    oi                    r    S SK Jr  S SKrS SKJrJr  S SKJr  S SKJ	r	  SSK
Jr  SSKJrJr   " S	 S
\5      rg)    )annotationsN)	BaseTunerBaseTunerLayer)4TRANSFORMERS_MODELS_TO_DELORA_TARGET_MODULES_MAPPING)get_pattern_key   )DeloraConfig)DeloraLayerDeloraLinearc                  ^   ^  \ rS rSr% SrSrS\S'   \r\	r
S
U 4S jjrS r\S 5       rS	rU =r$ )DeloraModel   a"  
Creates DeLoRA model from a pretrained transformers model.

The method is described in detail in [TODO].

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

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

**Attributes**:
    - **model** ([`~transformers.PreTrainedModel`]) -- The model to be adapted.
    - **peft_config** ([`DeloraConfig`]): The configuration of the DeLoRA model.
delora_strprefixc                $   > [         TU ]  U5        g)z
A helper method to check the config when a new adapter is being added.

Raise a ValueError if there is something wrong with the config or if it conflicts with existing adapters.

N)super_check_new_adapter_config)selfconfig	__class__s     R/home/james-whalen/.local/lib/python3.13/site-packages/peft/tuners/delora/model.pyr   %DeloraModel._check_new_adapter_config3   s     	)&1    c                X   Uc  [        S5      e[        UR                  R                  5       U5      n[        UR                  R                  5       U5      n	UR                  R                  XR                  5      n
UR                  R                  XR                  5      nU
UUR                  UR                  S.n[        U[        5      (       a  UR                  " U40 UD6  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`)rdelora_lambdamodule_dropoutinit_weightsF)
ValueErrorr   rank_patternkeyslambda_patterngetr   r   r   r   
isinstancer   update_layer_create_new_moduleactive_adapterrequires_grad__replace_module)r   delora_configadapter_nametargettarget_nameparentcurrent_keyoptional_kwargsr_key
lambda_keyr   r   kwargs
new_modules                 r   _create_and_replaceDeloraModel._create_and_replace<   s	    >??   : : ? ? A;O$]%A%A%F%F%H+V
&&**5//B%4488E`E`a *+::)66	
 fl++7700f_X^_J222))%0  jIr   c                    [        U[        5      (       a  UR                  5       nOUn[        U[        R                  R
                  5      (       a  [        X!40 UD6nW$ )N)r%   r   get_base_layertorchnnLinearr   )r+   r,   r-   r4   target_base_layerr5   s         r   r'   DeloraModel._create_new_module_   sO    fn-- & 5 5 7 &'99%fEfEJr    )r   r	   returnNone)__name__
__module____qualname____firstlineno____doc__r   __annotations__r
   tuner_layer_clsr   target_module_mappingr   r6   staticmethodr'   __static_attributes____classcell__)r   s   @r   r   r      s?    $ FC!OP2!JF 	 	r   r   )
__future__r   r:   peft.tuners.tuners_utilsr   r   
peft.utilsr   peft.utils.otherr   r   r	   layerr
   r   r   r?   r   r   <module>rR      s.    #  > -   ,M) Mr   