o
    Ni                     @   sL   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)1TRANSFORMERS_MODELS_TO_HRA_TARGET_MODULES_MAPPING   )	HRAConv2dHRALayer	HRALinearc                   @   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 )	HRAModela  
    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                 K   s   |d u rt dt|do|jd u}|j|j|jd}	||	d< t|tsC| j|||fi |	}
|| j	vr9|

d | |||
| d S |j||j|j|jd d S )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_module r"   I/home/ubuntu/.local/lib/python3.10/site-packages/peft/tuners/hra/model.py_create_and_replaceN   s&   




zHRAModel._create_and_replacec                 K   sp   t |tr
| }n|}t |tjjrt||fi |}|S t |tjjr0t||fi |}|S t	d| d)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!   r"   r"   r#   r   r   s   

	
zHRAModel._create_new_moduleN)__name__
__module____qualname____doc__r   str__annotations__r   tuner_layer_clsr   target_module_mappingr$   staticmethodr   r"   r"   r"   r#   r	      s   
 1$r	   )r&   peft.tuners.tuners_utilsr   r   
peft.utilsr   layerr   r   r   r	   r"   r"   r"   r#   <module>   s
   