
    oi                     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)1TRANSFORMERS_MODELS_TO_HRA_TARGET_MODULES_MAPPING   )	HRAConv2dHRALayer	HRALinearc                   F    \ rS rSr% SrSr\\S'   \r	\
rS r\S 5       rSrg)	HRAModel   ak  
Creates Householder reflection adaptation (HRA) model from a pretrained model. The method is described in
https://huggingface.co/papers/2405.17484

Args:
    model (`torch.nn.Module`): The model to which the adapter tuner layers will be attached.
    config ([`HRAConfig`]): The configuration of the HRA 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 HRA model.

Example:
    ```py
    >>> from diffusers import StableDiffusionPipeline
    >>> from peft import HRAModel, HRAConfig

    >>> config_te = HRAConfig(
    ...     r=8,
    ...     target_modules=["k_proj", "q_proj", "v_proj", "out_proj", "fc1", "fc2"],
    ...     init_weights=True,
    ... )
    >>> config_unet = HRAConfig(
    ...     r=8,
    ...     target_modules=[
    ...         "proj_in",
    ...         "proj_out",
    ...         "to_k",
    ...         "to_q",
    ...         "to_v",
    ...         "to_out.0",
    ...         "ff.net.0.proj",
    ...         "ff.net.2",
    ...     ],
    ...     init_weights=True,
    ... )

    >>> model = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
    >>> model.text_encoder = HRAModel(model.text_encoder, config_te, "default")
    >>> model.unet = HRAModel(model.unet, config_unet, "default")
    ```

**Attributes**:
    - **model** ([`~torch.nn.Module`]) -- The model to be adapted.
    - **peft_config** ([`HRAConfig`]): The configuration of the HRA model.
hra_prefixc                    Uc  [        S5      e[        US5      =(       a    UR                  S Ln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
                  S9  g )NzCurrent Key shouldn't be `None`bias)rapply_GSinit_weightsF)
ValueErrorhasattrr   r   r   r   
isinstancer   _create_new_moduleactive_adaptersrequires_grad__replace_moduleupdate_layer)self
hra_configadapter_nametargettarget_nameparentcurrent_keyoptional_kwargsr   kwargs
new_modules              O/home/james-whalen/.local/lib/python3.13/site-packages/peft/tuners/hra/model.py_create_and_replaceHRAModel._create_and_replaceN   s     >??vv&B6;;d+B"++&33

 v &(++006\U[\J#7#77))%0  jI,,#,,'44	       c                 L   [        U[        5      (       a  UR                  5       nOUn[        U[        R                  R
                  5      (       a  [        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)NzTarget module zY is not supported. Currently, only `torch.nn.Linear` and `torch.nn.Conv2d` are supported.)
r   r   get_base_layertorchnnLinearr	   Conv2dr   r   )r   r   r   r$   target_base_layerr%   s         r&   r   HRAModel._create_new_moduler   s    fn-- & 5 5 7 &'99"6B6BJ  )588??;;"6B6BJ    )Y Y r)    N)__name__
__module____qualname____firstlineno____doc__r   str__annotations__r   tuner_layer_clsr   target_module_mappingr'   staticmethodr   __static_attributes__r2   r)   r&   r   r      s9    /b FCOM"H  r)   r   )r,   peft.tuners.tuners_utilsr   r   
peft.utilsr   layerr   r   r	   r   r2   r)   r&   <module>rA      s$      > H 1 1ky kr)   