o
    NiM                     @   st   d dl mZ d dlZd dlmZ d dlmZmZ d dlmZ d dl	m
Z
 ddlmZmZmZmZ G d	d
 d
eZdS )    )UnionN)nn)LycorisConfigLycorisTuner)2TRANSFORMERS_MODELS_TO_LOHA_TARGET_MODULES_MAPPING)get_pattern_key   )Conv1dConv2dLinear	LoHaLayerc                   @   s   e Zd ZU dZdZeed< eZe	Z
ejjeejjeejjeiZeeejj ee f ed< dededeeejf ded	ejd
eddfddZdS )	LoHaModela  
    Creates Low-Rank Hadamard Product model from a pretrained model. The method is partially described in
    https://huggingface.co/papers/2108.06098 Current implementation heavily borrows from
    https://github.com/KohakuBlueleaf/LyCORIS/blob/eb460098187f752a5d66406d3affade6f0a07ece/lycoris/modules/loha.py

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

    Example:
        ```py
        >>> from diffusers import StableDiffusionPipeline
        >>> from peft import LoHaModel, LoHaConfig

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

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

    **Attributes**:
        - **model** ([`~torch.nn.Module`]) -- The model to be adapted.
        - **peft_config** ([`LoHaConfig`]): The configuration of the LoHa model.
    hada_prefixlayers_mappingconfigadapter_nametargettarget_nameparentcurrent_keyreturnNc                 C   s   t |j |}t |j |}| }	|j||j|	d< |j||j|	d< t|t	r8|j
|fi |	 dS | j|||fi |	}
| |||
| dS )zc
        A private method to create and replace the target module with the adapter module.
        ralphaN)r   rank_patternkeysalpha_patternto_dictgetr   r   
isinstancer   update_layer_create_new_module_replace_module)selfr   r   r   r   r   r   r_key	alpha_keykwargs
new_module r(   J/home/ubuntu/.local/lib/python3.10/site-packages/peft/tuners/loha/model.py_create_and_replace^   s   
zLoHaModel._create_and_replace)__name__
__module____qualname____doc__r   str__annotations__r   tuner_layer_clsr   target_module_mappingtorchr   r
   r	   r   r   dicttypeModuler   r   r*   r(   r(   r(   r)   r      s0   
 9 r   )typingr   r3   r   peft.tuners.lycoris_utilsr   r   
peft.utilsr   peft.utils.otherr   layerr	   r
   r   r   r   r(   r(   r(   r)   <module>   s   