o
    Ni                     @   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_BONE_TARGET_MODULES_MAPPING   )	BoneLayer
BoneLinearc                   @   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 )		BoneModela  
    Creates Householder reflection adaptation (Bone) 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 ([`BoneConfig`]): The configuration of the Bone 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 Bone model.

    Example:
        ```py
        >>> from diffusers import StableDiffusionPipeline
        >>> from peft import BoneModel, BoneConfig

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

    **Attributes**:
        - **model** ([`~torch.nn.Module`]) -- The model to be adapted.
        - **peft_config** ([`BoneConfig`]): The configuration of the Bone model.
    bone_prefixc                 K   s   |d u rt dt|do|jd u}|j|jd}	||	d< t|tsA| j|||fi |	}
|| jvr7|
	d | 
|||
| d S |j||j|jd d S )NzCurrent Key shouldn't be `None`bias)rinit_weightsF)
ValueErrorhasattrr   r   r   
isinstancer   _create_new_moduleactive_adaptersrequires_grad__replace_moduleupdate_layer)selfbone_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/bone/model.py_create_and_replaceN   s"   




zBoneModel._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   p   s   


zBoneModel._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
   