
    oi                         S SK JrJr  S SKJr  S SKJrJr  SSKJ	r	  SSK
Jr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJrJr   " S S\5      rg)    )is_bnb_4bit_availableis_bnb_available)	BaseTuner)1TRANSFORMERS_MODELS_TO_OFT_TARGET_MODULES_MAPPINGget_quantization_config   )dispatch_aqlm)dispatch_awq)dispatch_eetq)dispatch_gptq)dispatch_hqq)dispatch_inc)OFTLayerdispatch_defaultc                   Z   ^  \ rS rSr% SrSr\\S'   \r	\
rS r\S 5       rU 4S jrSrU =r$ )	OFTModel"   a  
Creates Orthogonal Finetuning model from a pretrained model. The method is described in
https://huggingface.co/papers/2306.07280

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

Example:
    ```py
    >>> from diffusers import StableDiffusionPipeline
    >>> from peft import OFTModel, OFTConfig

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

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

**Attributes**:
    - **model** ([`~torch.nn.Module`]) -- The model to be adapted.
    - **peft_config** ([`OFTConfig`]): The configuration of the OFT model.
oft_prefixc                    Uc  [        S5      eUR                  UR                  UR                  UR                  UR
                  UR                  UR                  UR                  UR                  UR                  [        U R                  SS5      [        U R                  SS5      S.n/ SQn	U	 H#  n
[        U R                  U
S9nUc  M  XU
 S3'   M%     [        U[        5      (       d|  [!        U R                  S	5      (       a  U R                  R"                  OS nU R$                  " XU4S
U0UD6nX R&                  ;  a  UR)                  S5        U R+                  XTX5        g UR-                  UUR                  UR                  UR                  UR                  UR
                  UR                  UR                  UR                  UR                  S9
  g )NzCurrent Key shouldn't be `None`is_loaded_in_8bitFis_loaded_in_4bit)roft_block_sizemodule_dropoutcoftepsblock_shareuse_cayley_neumannnum_cayley_neumann_termsfan_in_fan_outinit_weightsloaded_in_8bitloaded_in_4bit)gptqaqlmawq)method_quantization_confighf_device_map
device_map)	r   r   r   r   r   r   r   r    r"   )
ValueErrorr   r   r   r   r   r   r   r    r!   r"   getattrmodelr   
isinstancer   hasattrr*   _create_new_moduleactive_adaptersrequires_grad__replace_moduleupdate_layer)self
oft_configadapter_nametargettarget_nameparentcurrent_keyoptional_kwargskwargsquant_methodsquant_methodquantization_configr+   
new_modules                 O/home/james-whalen/.local/lib/python3.13/site-packages/peft/tuners/oft/model.py_create_and_replaceOFTModel._create_and_replaceZ   s    >?? (77(77OO>>%11","?"?(2(K(K(77&33%djj2EuM%djj2EuM
 0)L"9$**\"Z".@S,';<= * &(++5<TZZ5Y5Y11_cJ006s^hslrsJ#7#77))%0  jI,,)88)88__NN&22#-#@#@)3)L)L'44       c           	      d   / n[        5       (       a  SSKJn  UR                  U5        [	        5       (       a  SSKJn  UR                  U5        UR                  [        [        [        [        [        [        [        /5        S nU H  nU" X!4SU 0UD6nUc  M    O   Uc  [        SU S35      eU$ )Nr   )dispatch_bnb_8bit)dispatch_bnb_4bitr7   zTarget module zm is not supported. Currently, only the following modules are supported: `torch.nn.Linear`, `torch.nn.Conv2d`.)r   bnbrH   appendr   rI   extendr   r	   r
   r   r   r   r   r,   )	r7   r8   r9   r>   dispatchersrH   rI   rB   
dispatchers	            rC   r1   OFTModel._create_new_module   s      .01 "".01 
	
 
%J#FZZZSYZJ% &
   )8 8 
 rF   c                    > [         TU ]  5         [        U R                  SS5      S:X  a  [	        S5      eU R
                  R                  S5      (       a  [	        S5      eg)z{Verify that the configuration supports merging.

Currently gptq quantization and replicated layers do not support merging.
quantization_methodNr%   z8Cannot merge OFT layers when the model is gptq quantizedlayer_replicationz=Cannot merge OFT layers when base model layers are replicated)super_check_merge_allowedr-   r.   r,   peft_configget)r6   	__class__s    rC   rT   OFTModel._check_merge_allowed   s[    
 	$&4::4d;vEWXX 344\]] 5rF    )__name__
__module____qualname____firstlineno____doc__r   str__annotations__r   tuner_layer_clsr   target_module_mappingrD   staticmethodr1   rT   __static_attributes____classcell__)rW   s   @rC   r   r   "   sG    1f FCOM6p ) )V	^ 	^rF   r   N)peft.import_utilsr   r   peft.tuners.tuners_utilsr   
peft.utilsr   r   r&   r	   r'   r
   eetqr   r%   r   hqqr   incr   layerr   r   r   rY   rF   rC   <module>rm      s<     F
        -e^y e^rF   