
    bi                     v    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  G d	 d
e      Zy)    )UnionN)nn)LycorisConfigLycorisTuner)2TRANSFORMERS_MODELS_TO_LOHA_TARGET_MODULES_MAPPING)get_pattern_key   )Conv2dLinear	LoHaLayerc                   *   e Zd ZU dZdZeed<   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Zed        Zy)	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                    t        |j                  j                         |      }t        |j                  j                         |      }|j	                         }	|j                  j                  ||j                        |	d<   |j                  j                  ||j                        |	d<   t        |t              r |j                  |fi |	 y | j                  |||fi |	}
| j                  |||
|       y)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_modules              Q/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/peft/tuners/loha/model.py_create_and_replacezLoHaModel._create_and_replace[   s       3 3 8 8 :KH#F$8$8$=$=$?M	!))--eVXX>s ..229fllKwfi(F77000vXQWXJ  j&I    c                 ~    | j                   0|d   t        vrt        d      t        t        |d            | _         | S )N
model_typez0Please specify `target_modules` in `peft_config`)target_modulesr   
ValueErrorset)peft_configmodel_configs     r*   _prepare_adapter_configz!LoHaModel._prepare_adapter_configs   sK    %%-L)1cc !STT),B<P\C]^*K& r,   )__name__
__module____qualname____doc__r   str__annotations__torchr   r
   r   r   dicttypeModuler   r   r   r+   staticmethodr4    r,   r*   r   r      s    7r FCDNDehhoo.Y?@ 
JJ J i*+	J
 J 		J J 
J0  r,   r   )typingr   r;   r   peft.tuners.lycoris_utilsr   r   
peft.utilsr   peft.utils.otherr   layerr
   r   r   r   r@   r,   r*   <module>rF      s-       A I , , ,` `r,   