o
    Ni                     @   sT   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	m
Z
 G dd deZdS )    N)	BaseTunerBaseTunerLayer)2TRANSFORMERS_MODELS_TO_BOFT_TARGET_MODULES_MAPPING   )	BOFTLayerConv2dLinearc                   @   s:   e Zd ZU dZdZeed< eZe	Z
dd Zedd ZdS )		BOFTModela  
    Creates BOFT and OFT model from a pretrained transformers model. Paper: https://huggingface.co/papers/2311.06243
    https://huggingface.co/papers/2306.07280

    Args:
        model ([`transformers.PreTrainedModel`]): The model to be adapted.
        config ([`BOFTConfig`]): The configuration of the BOFT 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 BOFT model.

    Example::

        >>> import transformers >>> from transformers import AutoModelForSeq2SeqLM, BOFTConfig >>> from peft import
        BOFTConfig, get_peft_model

        >>> config = BOFTConfig( ... boft_block_size=8, ... boft_n_butterfly_factor=1, ... target_modules=["query",
        "value", "key", "output.dense", "mlp.fc1", "mlp.fc2"], ... boft_dropout=0.1, ... bias="boft_only", ...
        modules_to_save=["classifier"], ... )

        >>> model = transformers.Dinov2ForImageClassification.from_pretrained( ... "facebook/dinov2-large", ...
        num_labels=100, ... ) >>> boft_model = get_peft_model(model, config)

    **Attributes**:
        - **model** ([`transformers.PreTrainedModel`]) -- The model to be adapted.
        - **peft_config** ([`BOFTConfig`]): The configuration of the BOFT model.
    boft_prefixc                 K   s   |d u rt dt|do|jd u}|j|j|j|j|j|jd}	||	d< t	|t
sI| j|||fi |	}
|| jvr?|
d | |||
| d S |j||j|j|j|j|jd d S )NzCurrent Key shouldn't be `None`bias)boft_block_sizeboft_block_numboft_n_butterfly_factorboft_dropoutfan_in_fan_outinit_weightsF)r   r   r   r   r   )
ValueErrorhasattrr   r   r   r   r   r   r   
isinstancer   _create_new_moduleactive_adaptersrequires_grad__replace_moduleupdate_layer)selfboft_configadapter_nametargettarget_nameparentcurrent_keyoptional_kwargsr   kwargs
new_module r%   J/home/ubuntu/.local/lib/python3.10/site-packages/peft/tuners/boft/model.py_create_and_replaceC   s0   




zBOFTModel._create_and_replacec                 K   s   t |tr
| }n|}t |tjjr.|d r#td d |d< | _t||fi |}|S t |tjj	r@t	||fi |}|S t
d| d)Nr   zjfan_in_fan_out is set to True but the target module is `torch.nn.Linear`. Setting fan_in_fan_out to False.FzTarget module zY is not supported. Currently, only `torch.nn.Linear` and `torch.nn.Conv2d` are supported.)r   r   get_base_layertorchnnr   warningswarnr   r   r   )r   r   r   r#   target_base_layerr$   r%   r%   r&   r   l   s"   

	
zBOFTModel._create_new_moduleN)__name__
__module____qualname____doc__r   str__annotations__r   tuner_layer_clsr   target_module_mappingr'   staticmethodr   r%   r%   r%   r&   r	      s   
 )r	   )r+   r)   peft.tuners.tuners_utilsr   r   
peft.utilsr   layerr   r   r   r	   r%   r%   r%   r&   <module>   s   