o
    Ni<                     @  s\   d dl mZ d dl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)	BaseTunerBaseTunerLayer)3TRANSFORMERS_MODELS_TO_SHIRA_TARGET_MODULES_MAPPING   )Linear
ShiraLayerc                   @  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 )

ShiraModela  
    Creates a Sparse High Rank Adapter (SHiRA) Model from a pretrained model.

    Args:
        model ([`~transformers.PreTrainedModel`]): The model to be adapted.
        config ([`ShiraConfig`]): The configuration of the SHiRA model.
        adapter_name (`str`): The name of the adapter, defaults to `"default"`.

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

    Example:

        ```py
        >>> from transformers import AutoModelForCausalLM
        >>> from peft import ShiraConfig, get_peft_model

        >>> base_model = AutoModelForCausalLM.from_pretrained("facebook/opt-125m")
        >>> config = ShiraConfig(r=32)
        >>> model = get_peft_model(base_model, config)
        ```

    **Attributes**:
        - **model** ([`~transformers.PreTrainedModel`]) -- The model to be adapted.
        - **peft_config** ([`ShiraConfig`]): The configuration of the SHiRA model.
    shira_strprefixc                 K  s   |d u rt dt|do|jd u}i }	||	d< |jdkr"|j|	d< | D ]\}
}||	|
< q&t|trT|jd urE|j|j	|j
fi |	nd }|j|||j
|jd d S | j|||fi |	}|| jvri|d | |||| d S )NzCurrent Key shouldn't be `None`biasrandomrandom_seed)init_weightsF)
ValueErrorhasattrr   	mask_typer   items
isinstancer   mask_fn
base_layerrupdate_layerr   _create_new_moduleactive_adapterrequires_grad__replace_module)selfshira_configadapter_nametargettarget_nameparentcurrent_keyoptional_kwargsr   kwargskvmask
new_module r+   K/home/ubuntu/.local/lib/python3.10/site-packages/peft/tuners/shira/model.py_create_and_replace=   s0   








zShiraModel._create_and_replacec           	      K  s   | j }|dd}t|tr| }n|}t|tjjr)|r(t	d d }| _ nt
d| d| jd urA| j|| jfi |nd }t|||| j|fd| ji|}|S )Nr   Fzjfan_in_fan_out is set to True but the target module is `torch.nn.Linear`. Setting fan_in_fan_out to False.zTarget module zZ is not supported. Currently, only the following modules are supported: `torch.nn.Linear`.r   )fan_in_fan_outpopr   r   get_base_layertorchnnr   warningswarnr   r   r   r   )	r   r    r!   r&   r.   _target_base_layerr)   r*   r+   r+   r,   r   f   s>   





zShiraModel._create_new_moduleN)__name__
__module____qualname____doc__r   __annotations__r   tuner_layer_clsr   target_module_mappingr-   staticmethodr   r+   r+   r+   r,   r	      s   
 )r	   )
__future__r   r3   r1   peft.tuners.tuners_utilsr   r   
peft.utilsr   layerr   r   r	   r+   r+   r+   r,   <module>   s   