o
    Niz                     @   sH   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 )    N)	BaseTunerBaseTunerLayer)2TRANSFORMERS_MODELS_TO_MISS_TARGET_MODULES_MAPPING   )	MissLayer
MissLinearc                   @   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 )		MissModela  
    Creates Householder reflection adaptation (MiSS) model from a pretrained model. The method is described in
    https://huggingface.co/papers/2409.15371

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

    Example:
        ```py
        >>> from diffusers import StableDiffusionPipeline
        >>> from peft import MissModel, MissConfig

        >>> config_te = MissConfig(
        ...     r=8,
        ...     target_modules=["k_proj", "q_proj", "v_proj", "out_proj", "fc1", "fc2"],
        ...     init_weights=True,
        ... )
        >>> config_unet = MissConfig(
        ...     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 = MissModel(model.text_encoder, config_te, "default")
        >>> model.unet = MissModel(model.unet, config_unet, "default")
        ```

    **Attributes**:
        - **model** ([`~torch.nn.Module`]) -- The model to be adapted.
        - **peft_config** ([`MissConfig`]): The configuration of the MiSS model.
    miss_prefixc                 K   s   |d u rt dt|do|jd u}|j|j|j|jd}	||	d< t|tsE| j	|||fi |	}
|| j
vr;|
d | |||
| d S |j||j|j|j|jd d S )NzCurrent Key shouldn't be `None`bias)rmini_rmiss_dropoutinit_weightsF)r   r   r   r   )
ValueErrorhasattrr   r   r   r   r   
isinstancer   _create_new_moduleactive_adaptersrequires_grad__replace_moduleupdate_layer)selfmiss_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/miss/model.py_create_and_replaceN   s*   




zMissModel._create_and_replacec                 K   sL   t |tr
| }n|}t |tjjrt||fi |}|S td| d)NzTarget module zB is not supported. Currently, only `torch.nn.Linear` is supported.)r   r   get_base_layertorchnnLinearr   r   )r   r   r   r    target_base_layerr!   r"   r"   r#   r   t   s   


zMissModel._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#   <module>   s
   