
    oi                     Z    S SK r S SKrS SKJrJr  S SKJr  SSKJrJ	r	J
r
   " S S\5      rg)    N)	BaseTunerBaseTunerLayer)2TRANSFORMERS_MODELS_TO_BOFT_TARGET_MODULES_MAPPING   )	BOFTLayerConv2dLinearc                   F    \ rS rSr% SrSr\\S'   \r	\
rS r\S 5       rSrg)		BOFTModel   aJ  
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           	      8   Uc  [        S5      e[        US5      =(       a    UR                  S LnUR                  UR                  UR
                  UR                  UR                  UR                  S.n	XS'   [        U[        5      (       dG  U R                  " XU40 U	D6n
X R                  ;  a  U
R                  S5        U R                  XTX5        g UR                  UUR                  UR                  UR
                  UR                  UR                  S9  g )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_modules              P/home/james-whalen/.local/lib/python3.13/site-packages/peft/tuners/boft/model.py_create_and_replaceBOFTModel._create_and_replaceC   s
    >??vv&B6;;d+B*::)88'2'J'J'44)88'44
 v &),,00F]V\]J#7#77))%0  jI + ; ;*99(3(K(K(55(55       c                    [        U[        5      (       a  UR                  5       nOUn[        U[        R                  R
                  5      (       a:  US   (       a"  [        R                  " S5        S=US'   U l        [        X!40 UD6nU$ [        U[        R                  R                  5      (       a  [        X!40 UD6nU$ [        SU S35      e)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(   s         r)   r   BOFTModel._create_new_modulel   s    fn-- & 5 5 7 &'99&'7 INM'(;+E??J  )588??;;??J    )Y Y r,    N)__name__
__module____qualname____firstlineno____doc__r   str__annotations__r   tuner_layer_clsr   target_module_mappingr*   staticmethodr   __static_attributes__r5   r,   r)   r   r      s8    > FCON'R  r,   r   )r1   r/   peft.tuners.tuners_utilsr   r   
peft.utilsr   layerr   r   r	   r   r5   r,   r)   <module>rD      s,   $   J , ,d	 dr,   