
    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_LOKR_TARGET_MODULES_MAPPING)get_pattern_key   )Conv2dLinear	LoKrLayerc                   *   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)	LoKrModela  
    Creates Low-Rank Kronecker Product model from a pretrained model. The original method is partially described in
    https://huggingface.co/papers/2108.06098 and in https://huggingface.co/papers/2309.14859 Current implementation
    heavily borrows from
    https://github.com/KohakuBlueleaf/LyCORIS/blob/eb460098187f752a5d66406d3affade6f0a07ece/lycoris/modules/lokr.py

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

    Example:
        ```py
        >>> from diffusers import StableDiffusionPipeline
        >>> from peft import LoKrModel, LoKrConfig

        >>> config_te = LoKrConfig(
        ...     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 = LoKrConfig(
        ...     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 = LoKrModel(model.text_encoder, config_te, "default")
        >>> model.unet = LoKrModel(model.unet, config_unet, "default")
        ```

    **Attributes**:
        - **model** ([`~torch.nn.Module`]) -- The model to be adapted.
        - **peft_config** ([`LoKrConfig`]): The configuration of the LoKr model.
    lokr_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<   |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alpharank_dropout_scaleN)r   rank_patternkeysalpha_patternto_dictgetr   r   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/lokr/model.py_create_and_replacezLoKrModel._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!LoKrModel._prepare_adapter_configu   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,   staticmethodr5    r-   r+   r   r      s    8t FCDNDehhoo.Y?@ 
JJ J i*+	J
 J 		J J 
J2  r-   r   )typingr   r<   r   peft.tuners.lycoris_utilsr   r   
peft.utilsr   peft.utils.otherr   layerr
   r   r   r   rA   r-   r+   <module>rG      s-       A I , , ,b br-   