o
    1Ai                     @   sp  d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	 d dl
Z
d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z( d dl)m*Z*m+Z+ e# rd dl,m-Z- d dl.m/Z/m0Z0 e" rd dl1m2Z2m3Z3m4Z4 e$5e6Z7dZ8dZ9dZ:dZ;dZ<dZ=G dd dZ>G dd de>Z?dS )    N)Path)CallableDictListOptionalUnion)
model_info)HF_HUB_OFFLINE)validate_hf_hub_args)version)nn)__version__)_LOW_CPU_MEM_USAGE_DEFAULT)USE_PEFT_BACKEND_get_model_fileconvert_state_dict_to_diffusersconvert_state_dict_to_peftconvert_unet_state_dict_to_peftdelete_adapter_layersget_adapter_nameget_peft_kwargsis_accelerate_availableis_transformers_availableloggingrecurse_remove_peft_layersscale_lora_layersset_adapter_layers!set_weights_and_activate_adapters) _convert_kohya_lora_to_diffusers"_maybe_map_sgm_blocks_to_diffusers)PreTrainedModel)text_encoder_attn_modulestext_encoder_mlp_modules)AlignDevicesHook
CpuOffloadremove_hook_from_moduletext_encoderunettransformerzpytorch_lora_weights.binz pytorch_lora_weights.safetensorszYou are using an old version of LoRA backend. This will be deprecated in the next releases in favor of PEFT make sure to install the latest PEFT and transformers packages in the future.c                   @   s  e Zd ZdZeZeZeZ	dZ
	dOdeeeeejf f fddZeedeeeeejf f fddZe	
dPddZedd Ze	dQddZe					dRddZe	dQddZedefddZdd Ze							dSdeeejf deeeejj ejf f deeejj f deeejj f d e!d!ed"e"d#e!fd$d%Z#e$d&eeejf ded e!d!ed"e"d#e!fd'd(Z%d)d* Z&				
	dTd+e!d,e!d-ed.e!d/e'e(e  f
d0d1Z)dUd2e!d3e!fd4d5Z*		dVd/ee(e ef d6e'd7 d8e(e fd9d:Z+dOd6e'd7 fd;d<Z,dOd6e'd7 fd=d>Z-	dOd/ee(e ef d?e'e(e  fd@dAZ.dBdC Z/dDdE Z0d/ee(e ef fdFdGZ1de(e fdHdIZ2deee(e f fdJdKZ3d/e(e dLeej4ee5f ddfdMdNZ6dS )WLoraLoaderMixinz
    Load LoRA layers into [`UNet2DConditionModel`] and
    [`CLIPTextModel`](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel).
    r   N%pretrained_model_name_or_path_or_dictc              	   K   s   t stdt|tr| }| j|fi |\}}tdd | D }|s+td|dt	}| j
||t| ds@t| | jn| j||| d | j||t| dsWt| | jn| j| j||| d	 d
S )a_  
        Load LoRA weights specified in `pretrained_model_name_or_path_or_dict` into `self.unet` and
        `self.text_encoder`.

        All kwargs are forwarded to `self.lora_state_dict`.

        See [`~loaders.LoraLoaderMixin.lora_state_dict`] for more details on how the state dict is loaded.

        See [`~loaders.LoraLoaderMixin.load_lora_into_unet`] for more details on how the state dict is loaded into
        `self.unet`.

        See [`~loaders.LoraLoaderMixin.load_lora_into_text_encoder`] for more details on how the state dict is loaded
        into `self.text_encoder`.

        Parameters:
            pretrained_model_name_or_path_or_dict (`str` or `os.PathLike` or `dict`):
                See [`~loaders.LoraLoaderMixin.lora_state_dict`].
            kwargs (`dict`, *optional*):
                See [`~loaders.LoraLoaderMixin.lora_state_dict`].
            adapter_name (`str`, *optional*):
                Adapter name to be used for referencing the loaded adapter model. If not specified, it will use
                `default_{i}` where i is the total number of adapters being loaded.
        )PEFT backend is required for this method.c                 s       | ]}d |v V  qdS loraN .0keyr/   r/   =/data/cameron/vidgen/svd_motion_lora/Motion-LoRA/LoRA/lora.py	<genexpr>t       z4LoraLoaderMixin.load_lora_weights.<locals>.<genexpr>Invalid LoRA checkpoint.low_cpu_mem_usager'   )network_alphasr'   r7   adapter_name	_pipeliner&   )r8   r&   
lora_scaler7   r9   r:   N)r   
ValueError
isinstancedictcopylora_state_dictallkeyspopr   load_lora_into_unethasattrgetattr	unet_namer'   load_lora_into_text_encodertext_encoder_namer&   r;   )selfr*   r9   kwargs
state_dictr8   is_correct_formatr7   r/   r/   r3   load_lora_weightsP   s8   

z!LoraLoaderMixin.load_lora_weightsc                 K   s  | dd}| dd}| dd}| dd}| dd}| dd}| d	d}	| d
d}
| dd}| dd}| dd}d}|du rLd}d}ddd}d}t|ts|r^|du sg|dur|drz'|du rt| j|d|d}t||pyt|||||||	|
|d}tjj	|dd}W n t
tjfy } z|s|d}W Y d}~nd}~ww |du r|du r| j|d|d}t||pt|||||||	|
|d}tj|dd}n|}d}tdd | D r|durt||}t|\}}||fS )a  
        Return state dict for lora weights and the network alphas.

        <Tip warning={true}>

        We support loading A1111 formatted LoRA checkpoints in a limited capacity.

        This function is experimental and might change in the future.

        </Tip>

        Parameters:
            pretrained_model_name_or_path_or_dict (`str` or `os.PathLike` or `dict`):
                Can be either:

                    - A string, the *model id* (for example `google/ddpm-celebahq-256`) of a pretrained model hosted on
                      the Hub.
                    - A path to a *directory* (for example `./my_model_directory`) containing the model weights saved
                      with [`ModelMixin.save_pretrained`].
                    - A [torch state
                      dict](https://pytorch.org/tutorials/beginner/saving_loading_models.html#what-is-a-state-dict).

            cache_dir (`Union[str, os.PathLike]`, *optional*):
                Path to a directory where a downloaded pretrained model configuration is cached if the standard cache
                is not used.
            force_download (`bool`, *optional*, defaults to `False`):
                Whether or not to force the (re-)download of the model weights and configuration files, overriding the
                cached versions if they exist.
            resume_download (`bool`, *optional*, defaults to `False`):
                Whether or not to resume downloading the model weights and configuration files. If set to `False`, any
                incompletely downloaded files are deleted.
            proxies (`Dict[str, str]`, *optional*):
                A dictionary of proxy servers to use by protocol or endpoint, for example, `{'http': 'foo.bar:3128',
                'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request.
            local_files_only (`bool`, *optional*, defaults to `False`):
                Whether to only load local model weights and configuration files or not. If set to `True`, the model
                won't be downloaded from the Hub.
            token (`str` or *bool*, *optional*):
                The token to use as HTTP bearer authorization for remote files. If `True`, the token generated from
                `diffusers-cli login` (stored in `~/.huggingface`) is used.
            revision (`str`, *optional*, defaults to `"main"`):
                The specific model version to use. It can be a branch name, a tag name, a commit id, or any identifier
                allowed by Git.
            subfolder (`str`, *optional*, defaults to `""`):
                The subfolder location of a model file within a larger model repository on the Hub or locally.
            low_cpu_mem_usage (`bool`, *optional*, defaults to `True` if torch version >= 1.9.0 else `False`):
                Speed up model loading only loading the pretrained weights and not initializing the weights. This also
                tries to not use more than 1x model size in CPU memory (including peak memory) while loading the model.
                Only supported for PyTorch >= 1.9.0. If you are using an older version of PyTorch, setting this
                argument to `True` will raise an error.
            mirror (`str`, *optional*):
                Mirror source to resolve accessibility issues if you're downloading a model in China. We do not
                guarantee the timeliness or safety of the source, and you should refer to the mirror site for more
                information.

        	cache_dirNforce_downloadFresume_downloadproxieslocal_files_onlytokenrevision	subfolderweight_nameunet_configuse_safetensorsTattn_procs_weightspytorch)	file_type	framework.safetensors)file_extensionrS   )
weights_namerO   rP   rQ   rR   rS   rT   rU   rV   
user_agentcpu)devicez.bin)map_locationc                 s   s8    | ]}| d p| dp| dp| dV  qdS )Zlora_te_Z
lora_unet_Z	lora_te1_Z	lora_te2_N)
startswithr1   kr/   r/   r3   r4   "  s    

z2LoraLoaderMixin.lora_state_dict.<locals>.<genexpr>)rC   r=   r>   endswith_best_guess_weight_namer   LORA_WEIGHT_NAME_SAFEsafetensorstorch	load_fileIOErrorSafetensorErrorLORA_WEIGHT_NAMEloadrA   rB   r   r   )clsr*   rK   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   allow_picklera   
model_filerL   er8   r/   r/   r3   r@      s   A


zLoraLoaderMixin.lora_state_dictr^   Fc                    s  |st rtdg }tj|rd S tj|r% fddt|D }nt|j} fdd|D }t	|dkr;d S h dt
tfdd|}td	d
 |D r]t
tdd |}ntdd
 |D rot
tdd |}t	|dkrtd  d| d|d }|S )Nz>When using the offline mode, you must specify a `weight_name`.c                    s   g | ]	}|  r|qS r/   )rh   r1   fr_   r/   r3   
<listcomp>?  s
    
z;LoraLoaderMixin._best_guess_weight_name.<locals>.<listcomp>c                    s   g | ]}|j  r|j qS r/   )	rfilenamerh   rv   rx   r/   r3   ry   D      r   >   	optimizer
checkpoint	schedulerc                    s   t  fddD S )Nc                 3   s    | ]}| vV  qd S Nr/   )r1   	substringxr/   r3   r4   M  r5   zLLoraLoaderMixin._best_guess_weight_name.<locals>.<lambda>.<locals>.<genexpr>)rA   r   )unallowed_substringsr   r3   <lambda>M  s    z9LoraLoaderMixin._best_guess_weight_name.<locals>.<lambda>c                 s       | ]}| tV  qd S r   rh   rp   rv   r/   r/   r3   r4   P      z:LoraLoaderMixin._best_guess_weight_name.<locals>.<genexpr>c                 S   
   |  tS r   r   r   r/   r/   r3   r   Q     
 c                 s   r   r   rh   rj   rv   r/   r/   r3   r4   R  r   c                 S   r   r   r   r   r/   r/   r3   r   S  r      z9Provided path contains more than one weights file in the z~ format. Either specify `weight_name` in `load_lora_weights` or make sure there's only one  `.safetensors` or `.bin` file in  .)r	   r<   ospathisfileisdirlistdirr   siblingslenlistfilterany)rr   r*   r_   rS   Ztargeted_filesZfiles_in_reporW   r/   )r_   r   r3   ri   3  s6   

z'LoraLoaderMixin._best_guess_weight_namec                 C   sx   d}d}|dur8|j  D ]*\}}t|tjr7t|dr7|s$t|jt}|s,t|jt}t	
d t||d q||fS )ar  
        Optionally removes offloading in case the pipeline has been already sequentially offloaded to CPU.

        Args:
            _pipeline (`DiffusionPipeline`):
                The pipeline to disable offloading for.

        Returns:
            tuple:
                A tuple indicating if `is_model_cpu_offload` or `is_sequential_cpu_offload` is True.
        FN_hf_hookzAccelerate hooks detected. Since you have called `load_lora_weights()`, the previous hooks will be first removed. Then the LoRA parameters will be loaded and the hooks will be applied again.)recurse)
componentsitemsr=   r   ModulerE   r   r$   r#   loggerinfor%   )rr   r:   is_model_cpu_offloadis_sequential_cpu_offload_	componentr/   r/   r3   _optionally_disable_offloading\  s   z.LoraLoaderMixin._optionally_disable_offloadingc                    s  t stdddlm}m}m}	 |dur|nt}t| }
t	fdd|
D rdt
dj d fd	d
|
D fdd| D }|durcfdd
| D   fdd| D }n	t smd}t
| t| dkr|t|di v rtd| dt|}|durt|}i }| D ]\}}d|v r|jd ||< qt|||dd}|di |}|du rt|}|\}}||||d |	|||}|durt|dd}|rt
d| d |r|  n|r|  |j||||d dS )aa  
        This will load the LoRA layers specified in `state_dict` into `unet`.

        Parameters:
            state_dict (`dict`):
                A standard state dict containing the lora layer parameters. The keys can either be indexed directly
                into the unet or prefixed with an additional `unet` which can be used to distinguish between text
                encoder lora layers.
            network_alphas (`Dict[str, float]`):
                See `LoRALinearLayer` for more details.
            unet (`UNet2DConditionModel`):
                The UNet model to load the LoRA layers into.
            low_cpu_mem_usage (`bool`, *optional*, defaults to `True` if torch version >= 1.9.0 else `False`):
                Speed up model loading only loading the pretrained weights and not initializing the weights. This also
                tries to not use more than 1x model size in CPU memory (including peak memory) while loading the model.
                Only supported for PyTorch >= 1.9.0. If you are using an older version of PyTorch, setting this
                argument to `True` will raise an error.
            adapter_name (`str`, *optional*):
                Adapter name to be used for referencing the loaded adapter model. If not specified, it will use
                `default_{i}` where i is the total number of adapters being loaded.
        r+   r   
LoraConfiginject_adapter_in_modelset_peft_model_state_dictNc                 3   s(    | ]}|  jp|  jV  qd S r   )re   rG   rI   r0   rr   r/   r3   r4     s   & z6LoraLoaderMixin.load_lora_into_unet.<locals>.<genexpr>Loading r   c                       g | ]
}|  jr|qS r/   re   rG   rf   r   r/   r3   ry         z7LoraLoaderMixin.load_lora_into_unet.<locals>.<listcomp>c                    .   i | ]\}}|v r|  j d d|qS r    replacerG   r1   rg   v)rr   	unet_keysr/   r3   
<dictcomp>  s   . z7LoraLoaderMixin.load_lora_into_unet.<locals>.<dictcomp>c                    r   r/   r   rf   r   r/   r3   ry     r   c                    .   i | ]\}}| v r| j d d|qS r   r   r   
alpha_keysrr   r/   r3   r         (a+  You have saved the LoRA weights using the old format. To convert the old LoRA weights to the new format, you can first load them in a dictionary and then create a new dictionary like the following: `new_state_dict = {f'unet.{module_name}': params for module_name, params in old_state_dict.items()}`.peft_configAdapter name z? already in use in the Unet - please select a new adapter name.lora_Br   Tis_unetr9   unexpected_keysXLoading adapter weights from state_dict led to unexpected keys not found in the model:  . )r8   r7   r:   r/   )r   r<   peftr   r   r   r   r   rB   rA   r   r   rG   r   warningr   rF   r   shaper   r   r   enable_model_cpu_offloadenable_sequential_cpu_offloadload_attn_procs)rr   rL   r8   r'   r7   r9   r:   r   r   r   rB   warn_messagerankr2   vallora_config_kwargslora_configr   r   incompatible_keysr   r/   )r   rr   r   r3   rD   {  sj   



z#LoraLoaderMixin.load_lora_into_unet      ?c	                    s  t stdddlm}	 |dur|nt}t| }
du r!jntfdd|
D rfdd|
D fd	d
|	 D }t
|dkr
td d i }t|}t|}t|D ]\}}| d}|| jd ||< qbtdd | D }|rt|D ] \}}| d}| d}|| jd ||< || jd ||< q|durfdd| D   fdd
|	 D }t|||dd}|	di |}|du rt|}|\}}|j|||d t||d |j|j|jd |r|  dS |r|  dS dS dS dS )aE  
        This will load the LoRA layers specified in `state_dict` into `text_encoder`

        Parameters:
            state_dict (`dict`):
                A standard state dict containing the lora layer parameters. The key should be prefixed with an
                additional `text_encoder` to distinguish between unet lora layers.
            network_alphas (`Dict[str, float]`):
                See `LoRALinearLayer` for more details.
            text_encoder (`CLIPTextModel`):
                The text encoder model to load the LoRA layers into.
            prefix (`str`):
                Expected prefix of the `text_encoder` in the `state_dict`.
            lora_scale (`float`):
                How much to scale the output of the lora linear layer before it is added with the output of the regular
                lora layer.
            low_cpu_mem_usage (`bool`, *optional*, defaults to `True` if torch version >= 1.9.0 else `False`):
                Speed up model loading only loading the pretrained weights and not initializing the weights. This also
                tries to not use more than 1x model size in CPU memory (including peak memory) while loading the model.
                Only supported for PyTorch >= 1.9.0. If you are using an older version of PyTorch, setting this
                argument to `True` will raise an error.
            adapter_name (`str`, *optional*):
                Adapter name to be used for referencing the loaded adapter model. If not specified, it will use
                `default_{i}` where i is the total number of adapters being loaded.
        r+   r   )r   Nc                 3   s    | ]} j |v V  qd S r   )rI   r0   r   r/   r3   r4     r   z>LoraLoaderMixin.load_lora_into_text_encoder.<locals>.<genexpr>c                    s,   g | ]}|  r|d d  kr|qS r   r   re   splitrf   prefixr/   r3   ry     s   , z?LoraLoaderMixin.load_lora_into_text_encoder.<locals>.<listcomp>c                    s,   i | ]\}}|v r|   d d|qS r   r   r   )r   text_encoder_keysr/   r3   r         &z?LoraLoaderMixin.load_lora_into_text_encoder.<locals>.<dictcomp>r   r   z.out_proj.lora_B.weightr   c                 s   r,   )z.mlp.Nr/   r0   r/   r/   r3   r4   -  r5   z.fc1.lora_B.weightz.fc2.lora_B.weightc                    s,   g | ]}|  r|d d  kr|qS r   r   rf   r   r/   r3   ry   7  s    
c                    s,   i | ]\}}| v r|  d d|qS r   r   r   )r   r   r/   r3   r   :  r   Fr   )r9   adapter_state_dictr   )weight)rc   dtyper/   )r   r<   r   r   r   r   rB   rI   r   r   r   r   r   r   r   r!   r   r"   r   r   r   load_adapterr   torc   r   r   r   )rr   rL   r8   r&   r   r;   r7   r9   r:   r   rB   Ztext_encoder_lora_state_dictr   namer   Zrank_keyZ	patch_mlpZrank_key_fc1Zrank_key_fc2r   r   r   r   r/   )r   rr   r   r   r3   rH     sh   %



6z+LoraLoaderMixin.load_lora_into_text_encoderc                    s  ddl m}m}m}	 |dur|nt}t| }
fdd|
D fdd| D }|durHfdd| D   fd	d| D }t| dkr|t	|d
i v r`t
d| di }| D ]\}}d|v ru|jd ||< qft|||}|di |}|du rt|}|\}}||||d |	|||}|durt	|dd}|rtd| d |r|  dS |r|  dS dS dS )ah  
        This will load the LoRA layers specified in `state_dict` into `transformer`.

        Parameters:
            state_dict (`dict`):
                A standard state dict containing the lora layer parameters. The keys can either be indexed directly
                into the unet or prefixed with an additional `unet` which can be used to distinguish between text
                encoder lora layers.
            network_alphas (`Dict[str, float]`):
                See `LoRALinearLayer` for more details.
            unet (`UNet2DConditionModel`):
                The UNet model to load the LoRA layers into.
            low_cpu_mem_usage (`bool`, *optional*, defaults to `True` if torch version >= 1.9.0 else `False`):
                Speed up model loading only loading the pretrained weights and not initializing the weights. This also
                tries to not use more than 1x model size in CPU memory (including peak memory) while loading the model.
                Only supported for PyTorch >= 1.9.0. If you are using an older version of PyTorch, setting this
                argument to `True` will raise an error.
            adapter_name (`str`, *optional*):
                Adapter name to be used for referencing the loaded adapter model. If not specified, it will use
                `default_{i}` where i is the total number of adapters being loaded.
        r   r   Nc                    r   r/   re   transformer_namerf   r   r/   r3   ry   z  r   z>LoraLoaderMixin.load_lora_into_transformer.<locals>.<listcomp>c                    r   r   r   r   r   )rr   transformer_keysr/   r3   r   {  r   z>LoraLoaderMixin.load_lora_into_transformer.<locals>.<dictcomp>c                    r   r/   r   rf   r   r/   r3   ry     r   c                    r   r   r   r   r   r/   r3   r     r   r   r   zF already in use in the transformer - please select a new adapter name.r   r   r   r   r   r   r/   )r   r   r   r   r   r   rB   r   r   rF   r<   r   r   r   r   r   r   r   r   )rr   rL   r8   r(   r7   r9   r:   r   r   r   rB   r   r2   r   r   r   r   r   r   r   r/   )r   rr   r   r3   load_lora_into_transformer[  sV   
%z*LoraLoaderMixin.load_lora_into_transformerreturnc                 C   s   t | dr| jS dS )N_lora_scaler   )rE   r   rJ   r/   r/   r3   r;     s   zLoraLoaderMixin.lora_scalec                 C   sx   t }t| dr|| j t| jdd d ur| j`d | j_t| dr8|| j t| jdd d ur:| j`d | j_d S d S d S )Nr&   r   text_encoder_2)r   rE   r&   rF   r   _hf_peft_config_loadedr   )rJ   Zremove_methodr/   r/   r3   !_remove_text_encoder_monkey_patch  s   



z1LoraLoaderMixin._remove_text_encoder_monkey_patchTsave_directoryunet_lora_layerstext_encoder_lora_layerstransformer_lora_layersis_main_processrW   save_functionsafe_serializationc	                 C   sz   i }	dd }
|s|s|st d|r|	|
|| j |r&|	|
|| j |r0|	|
|d | j|	|||||d dS )  
        Save the LoRA parameters corresponding to the UNet and text encoder.

        Arguments:
            save_directory (`str` or `os.PathLike`):
                Directory to save LoRA parameters to. Will be created if it doesn't exist.
            unet_lora_layers (`Dict[str, torch.nn.Module]` or `Dict[str, torch.Tensor]`):
                State dict of the LoRA layers corresponding to the `unet`.
            text_encoder_lora_layers (`Dict[str, torch.nn.Module]` or `Dict[str, torch.Tensor]`):
                State dict of the LoRA layers corresponding to the `text_encoder`. Must explicitly pass the text
                encoder LoRA state dict because it comes from 🤗 Transformers.
            is_main_process (`bool`, *optional*, defaults to `True`):
                Whether the process calling this is the main process or not. Useful during distributed training and you
                need to call this function on all processes. In this case, set `is_main_process=True` only on the main
                process to avoid race conditions.
            save_function (`Callable`):
                The function to use to save the state dictionary. Useful during distributed training when you need to
                replace `torch.save` with another method. Can be configured with the environment variable
                `DIFFUSERS_SAVE_MODE`.
            safe_serialization (`bool`, *optional*, defaults to `True`):
                Whether to save the model using `safetensors` or the traditional PyTorch way with `pickle`.
        c                    4   t | tjjr|  n| } fdd| D }|S )Nc                        i | ]\}}  d | |qS r   r/   r1   module_nameparamr   r/   r3   r          zKLoraLoaderMixin.save_lora_weights.<locals>.pack_weights.<locals>.<dictcomp>r=   rl   r   r   rL   r   layersr   Zlayers_weightsZlayers_state_dictr/   r   r3   pack_weights     z7LoraLoaderMixin.save_lora_weights.<locals>.pack_weightszkYou must pass at least one of `unet_lora_layers`, `text_encoder_lora_layers`, or `transformer_lora_layers`.r(   rL   r   r   rW   r   r   N)r<   updaterG   rI   write_lora_layers)rr   r   r   r   r   r   rW   r   r   rL   r   r/   r/   r3   save_lora_weights  s(   "
z!LoraLoaderMixin.save_lora_weightsrL   c                 C   s   t j|rtd| d d S |d u r|rdd }ntj}t j|dd |d u r1|r/t}nt	}t
|| }|| | td|  d S )NzProvided path (z#) should be a directory, not a filec                 S   s   t jj| |ddidS )Nformatpt)metadata)rk   rl   	save_file)weightsfilenamer/   r/   r3   r     s   z8LoraLoaderMixin.write_lora_layers.<locals>.save_functionT)exist_okzModel weights saved in )r   r   r   r   errorrl   savemakedirsrj   rp   r   as_posixr   )rL   r   r   rW   r   r   	save_pathr/   r/   r3   r     s   	

z!LoraLoaderMixin.write_lora_layersc                 C   s   t | dst| | jn| j}ts3tttdkrt	d |
 D ]\}}t |dr1|d q#nt| t |dr>|`|   dS )z
        Unloads the LoRA parameters.

        Examples:

        ```python
        >>> # Assuming `pipeline` is already loaded with the LoRA parameters.
        >>> pipeline.unload_lora_weights()
        >>> ...
        ```
        r'   z0.23zYou are using `unload_lora_weights` to disable and unload lora weights. If you want to iteratively enable and disable adapter weights,you can use `pipe.enable_lora()` or `pipe.disable_lora()`. After installing the latest version of PEFT.set_lora_layerNr   )rE   rF   rG   r'   r   r   parser   r   r   named_modulesr  r   r   r   )rJ   r'   r   moduler/   r/   r3   unload_lora_weights&  s   


z#LoraLoaderMixin.unload_lora_weights	fuse_unetfuse_text_encoderr;   safe_fusingadapter_namesc                    s   ddl m  |s
|r|  jd7  _| jdkrtd |r3t| ds(t| | jn| j}|j	|||d d fd
d	}|rZt| drJ|| j
|||d t| dr\|| j|||d d	S d	S d	S )a  
        Fuses the LoRA parameters into the original parameters of the corresponding blocks.

        <Tip warning={true}>

        This is an experimental API.

        </Tip>

        Args:
            fuse_unet (`bool`, defaults to `True`): Whether to fuse the UNet LoRA parameters.
            fuse_text_encoder (`bool`, defaults to `True`):
                Whether to fuse the text encoder LoRA parameters. If the text encoder wasn't monkey-patched with the
                LoRA parameters then it won't have any effect.
            lora_scale (`float`, defaults to 1.0):
                Controls how much to influence the outputs with the LoRA parameters.
            safe_fusing (`bool`, defaults to `False`):
                Whether to check fused weights for NaN values before fusing and if values are NaN not fusing them.
            adapter_names (`List[str]`, *optional*):
                Adapter names to be used for fusing. If nothing is passed, all active adapters will be fused.

        Example:

        ```py
        from diffusers import DiffusionPipeline
        import torch

        pipeline = DiffusionPipeline.from_pretrained(
            "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
        ).to("cuda")
        pipeline.load_lora_weights("nerijs/pixel-art-xl", weight_name="pixel-art-xl.safetensors", adapter_name="pixel")
        pipeline.fuse_lora(lora_scale=0.7)
        ```
        r   BaseTunerLayerr   zThe current API is supported for operating with a single LoRA file. You are trying to load and fuse more than one LoRA which is not well-supported.r'   )r  r  r   FNc                    s   d|i}|   D ]6}t| r>|dkr|| tt|jj}d|v r*||d< nd|vr6|d ur6td|jdi | qd S )N
safe_merger   r  zThe `adapter_names` argument is not supported with your PEFT version. Please upgrade to the latest version of PEFT. `pip install -U peft`r/   )	modulesr=   scale_layerr   inspect	signaturemerge
parametersr<   )r&   r;   r  r  merge_kwargsr  supported_merge_kwargsr  r/   r3   fuse_text_encoder_lora}  s   


z9LoraLoaderMixin.fuse_lora.<locals>.fuse_text_encoder_lorar&   )r  r   )r   FN)peft.tuners.tuners_utilsr  num_fused_lorasr   r   rE   rF   rG   r'   	fuse_lorar&   r   )rJ   r  r  r;   r  r  r'   r   r/   r  r3   r#  F  s$   *


zLoraLoaderMixin.fuse_loraunfuse_unetunfuse_text_encoderc                    s   ddl m  t| dst| | jn| j}|r&| D ]}t| r%|  q fdd}|rBt| dr8|| j	 t| drB|| j
 |  jd8  _d	S )
af  
        Reverses the effect of
        [`pipe.fuse_lora()`](https://huggingface.co/docs/diffusers/main/en/api/loaders#diffusers.loaders.LoraLoaderMixin.fuse_lora).

        <Tip warning={true}>

        This is an experimental API.

        </Tip>

        Args:
            unfuse_unet (`bool`, defaults to `True`): Whether to unfuse the UNet LoRA parameters.
            unfuse_text_encoder (`bool`, defaults to `True`):
                Whether to unfuse the text encoder LoRA parameters. If the text encoder wasn't monkey-patched with the
                LoRA parameters then it won't have any effect.
        r   r  r'   c                    s$   |   D ]}t| r|  qd S r   )r  r=   unmerge)r&   r  r  r/   r3   unfuse_text_encoder_lora  s
   
z=LoraLoaderMixin.unfuse_lora.<locals>.unfuse_text_encoder_lorar&   r   r   N)r!  r  rE   rF   rG   r'   r  r=   r&  r&   r   r"  )rJ   r$  r%  r'   r  r'  r/   r  r3   unfuse_lora  s   




zLoraLoaderMixin.unfuse_lorar&   r    text_encoder_weightsc                 C   sb   t stddd }t|tr|gn|}|||}|p t| dd}|du r)tdt||| dS )a4  
        Sets the adapter layers for the text encoder.

        Args:
            adapter_names (`List[str]` or `str`):
                The names of the adapters to use.
            text_encoder (`torch.nn.Module`, *optional*):
                The text encoder module to set the adapter layers for. If `None`, it will try to get the `text_encoder`
                attribute.
            text_encoder_weights (`List[float]`, *optional*):
                The weights to use for the text encoder. If `None`, the weights are set to `1.0` for all the adapters.
        r+   c                 S   sX   |d u rdgt |  }nt|tr|g}t | t |kr*tdt |  dt | |S )Nr   zLength of adapter names z+ is not equal to the length of the weights )r   r=   floatr<   )r  r  r/   r/   r3   process_weights  s   
zFLoraLoaderMixin.set_adapters_for_text_encoder.<locals>.process_weightsr&   NzrThe pipeline does not have a default `pipe.text_encoder` class. Please make sure to pass a `text_encoder` instead.)r   r<   r=   strrF   r   )rJ   r  r&   r)  r+  r/   r/   r3   set_adapters_for_text_encoder  s   
z-LoraLoaderMixin.set_adapters_for_text_encoderc                 C   s<   t std|pt| dd}|du rtdt|dd dS )a  
        Disables the LoRA layers for the text encoder.

        Args:
            text_encoder (`torch.nn.Module`, *optional*):
                The text encoder module to disable the LoRA layers for. If `None`, it will try to get the
                `text_encoder` attribute.
        r+   r&   NText Encoder not found.Fenabled)r   r<   rF   r   rJ   r&   r/   r/   r3   disable_lora_for_text_encoder  s   	z-LoraLoaderMixin.disable_lora_for_text_encoderc                 C   s>   t std|pt| dd}|du rtdt| jdd dS )a  
        Enables the LoRA layers for the text encoder.

        Args:
            text_encoder (`torch.nn.Module`, *optional*):
                The text encoder module to enable the LoRA layers for. If `None`, it will try to get the `text_encoder`
                attribute.
        r+   r&   Nr.  Tr/  )r   r<   rF   r   r&   r1  r/   r/   r3   enable_lora_for_text_encoder  s   	z,LoraLoaderMixin.enable_lora_for_text_encoderadapter_weightsc                 C   sd   t | dst| | jn| j}||| t | dr!| || j| t | dr0| || j| d S d S )Nr'   r&   r   )rE   rF   rG   r'   set_adaptersr-  r&   r   )rJ   r  r4  r'   r/   r/   r3   r5  	  s   

zLoraLoaderMixin.set_adaptersc                 C   d   t stdt| dst| | jn| j}|  t| dr#| | j t| dr0| | j	 d S d S Nr+   r'   r&   r   )
r   r<   rE   rF   rG   r'   disable_lorar2  r&   r   rJ   r'   r/   r/   r3   r8       

zLoraLoaderMixin.disable_lorac                 C   r6  r7  )
r   r<   rE   rF   rG   r'   enable_lorar3  r&   r   r9  r/   r/   r3   r;  &  r:  zLoraLoaderMixin.enable_lorac                 C   s|   t stdt|tr|g}t| dst| | jn| j}|| |D ]}t| dr0t	| j
| t| dr;t	| j| q#dS )a   
        Args:
        Deletes the LoRA layers of `adapter_name` for the unet and text-encoder(s).
            adapter_names (`Union[List[str], str]`):
                The names of the adapter to delete. Can be a single string or a list of strings
        r+   r'   r&   r   N)r   r<   r=   r,  rE   rF   rG   r'   delete_adaptersr   r&   r   )rJ   r  r'   r9   r/   r/   r3   r<  4  s   



zLoraLoaderMixin.delete_adaptersc                 C   s`   t stdddlm} g }t| dst| | jn| j}| D ]}t	||r-|j
} |S q |S )a  
        Gets the list of the current active adapters.

        Example:

        ```python
        from diffusers import DiffusionPipeline

        pipeline = DiffusionPipeline.from_pretrained(
            "stabilityai/stable-diffusion-xl-base-1.0",
        ).to("cuda")
        pipeline.load_lora_weights("CiroN2022/toy-face", weight_name="toy_face_sdxl.safetensors", adapter_name="toy")
        pipeline.get_active_adapters()
        ```
        iPEFT backend is required for this method. Please install the latest version of PEFT `pip install -U peft`r   r  r'   )r   r<   r!  r  rE   rF   rG   r'   r  r=   active_adapters)rJ   r  r>  r'   r  r/   r/   r3   get_active_adaptersL  s   
z#LoraLoaderMixin.get_active_adaptersc                 C   s   t stdi }t| drt| jdrt| jj |d< t| dr2t| jdr2t| jj |d< t| ds=t| | j	n| j
}t| | j	rVt|drVt| j
j || j	< |S )zR
        Gets the current list of all available adapters in the pipeline.
        r=  r&   r   r   r'   )r   r<   rE   r&   r   r   rB   r   rF   rG   r'   )rJ   r5  r'   r/   r/   r3   get_list_adaptersl  s   z!LoraLoaderMixin.get_list_adaptersrc   c           
      C   s   t stdddlm} t| dst| | jn| j}| D ]}t	||r:|D ]}|j
| | |j| | q'qg }t| drH|| j t| drS|| j |D ]#}| D ]}	t	|	|rw|D ]}|	j
| | |	j| | qdq[qUdS )a  
        Moves the LoRAs listed in `adapter_names` to a target device. Useful for offloading the LoRA to the CPU in case
        you want to load multiple adapters and free some GPU memory.

        Args:
            adapter_names (`List[str]`):
                List of adapters to send device to.
            device (`Union[torch.device, str, int]`):
                Device to send the adapters to. Can be either a torch device, a str or an integer.
        r+   r   r  r'   r&   r   N)r   r<   r!  r  rE   rF   rG   r'   r  r=   lora_Ar   r   appendr&   r   )
rJ   r  rc   r  r'   Zunet_moduler9   Zmodules_to_processr&   Ztext_encoder_moduler/   r/   r3   set_lora_device  s0   



zLoraLoaderMixin.set_lora_devicer   )r^   F)NNN)Nr   NNNNNNTNNT)TTr   FN)TT)NN)7__name__
__module____qualname____doc__TEXT_ENCODER_NAMErI   	UNET_NAMErG   TRANSFORMER_NAMEr   r"  r   r,  r   rl   TensorrN   classmethodr
   r@   ri   r   rD   rH   r   propertyr*  r;   r   r   PathLiker   r   boolr   r   staticmethodr   r  r   r   r#  r(  r-  r2  r3  r5  r8  r;  r<  r?  r@  rc   intrC  r/   r/   r/   r3   r)   E   s    
> $(
ksR	@!"

R)
*

 *r)   c                   @   s   e Zd ZdZ	ddeeeeejf f de	e fddZ
e							ddeeejf d	eeeejjejf f d
eeeejjejf f deeeejjejf f dedededefddZdd ZdS ) StableDiffusionXLLoraLoaderMixinz\This class overrides `LoraLoaderMixin` with LoRA loading/saving code that's specific to SDXLNr*   r9   c           	   	   K   s   t stdt|tr| }| j|fd| jji|\}}tdd |	 D }|s/td| j
||| j|| d dd | D }t|d	krW| j||| jd
| j|| d dd | D }t|d	krv| j||| jd| j|| d dS dS )a_  
        Load LoRA weights specified in `pretrained_model_name_or_path_or_dict` into `self.unet` and
        `self.text_encoder`.

        All kwargs are forwarded to `self.lora_state_dict`.

        See [`~loaders.LoraLoaderMixin.lora_state_dict`] for more details on how the state dict is loaded.

        See [`~loaders.LoraLoaderMixin.load_lora_into_unet`] for more details on how the state dict is loaded into
        `self.unet`.

        See [`~loaders.LoraLoaderMixin.load_lora_into_text_encoder`] for more details on how the state dict is loaded
        into `self.text_encoder`.

        Parameters:
            pretrained_model_name_or_path_or_dict (`str` or `os.PathLike` or `dict`):
                See [`~loaders.LoraLoaderMixin.lora_state_dict`].
            adapter_name (`str`, *optional*):
                Adapter name to be used for referencing the loaded adapter model. If not specified, it will use
                `default_{i}` where i is the total number of adapters being loaded.
            kwargs (`dict`, *optional*):
                See [`~loaders.LoraLoaderMixin.lora_state_dict`].
        r+   rX   c                 s   r,   r-   r/   r0   r/   r/   r3   r4     r5   zEStableDiffusionXLLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>r6   )r8   r'   r9   r:   c                 S      i | ]\}}d |v r||qS )ztext_encoder.r/   r   r/   r/   r3   r     r{   zFStableDiffusionXLLoraLoaderMixin.load_lora_weights.<locals>.<dictcomp>r   r&   )r8   r&   r   r;   r9   r:   c                 S   rT  )ztext_encoder_2.r/   r   r/   r/   r3   r     r{   r   N)r   r<   r=   r>   r?   r@   r'   configrA   rB   rD   r   r   rH   r&   r;   r   )	rJ   r*   r9   rK   rL   r8   rM   text_encoder_state_dictZtext_encoder_2_state_dictr/   r/   r3   rN     sP   



z2StableDiffusionXLLoraLoaderMixin.load_lora_weightsTr   r   r   text_encoder_2_lora_layersr   rW   r   r   c	                 C   sv   i }	dd }
|s|s|st d|r|	|
|d |r.|r.|	|
|d |	|
|d | j|	|||||d dS )	r   c                    r   )Nc                    r   r   r/   r   r   r/   r3   r   #  r   z\StableDiffusionXLLoraLoaderMixin.save_lora_weights.<locals>.pack_weights.<locals>.<dictcomp>r   r   r/   r   r3   r   !  r   zHStableDiffusionXLLoraLoaderMixin.save_lora_weights.<locals>.pack_weightszmYou must pass at least one of `unet_lora_layers`, `text_encoder_lora_layers` or `text_encoder_2_lora_layers`.r'   r&   r   r   N)r<   r   r   )rr   r   r   r   rW  r   rW   r   r   rL   r   r/   r/   r3   r     s&   "
z2StableDiffusionXLLoraLoaderMixin.save_lora_weightsc                 C   s\   t | j t| jdd d ur| j`d | j_t | j t| jdd d ur,| j`d | j_d S d S )Nr   )r   r&   rF   r   r   r   r   r/   r/   r3   r   ;  s   

zBStableDiffusionXLLoraLoaderMixin._remove_text_encoder_monkey_patchr   rD  )rE  rF  rG  rH  r   r,  r   rl   rL  r   rN   rM  r   rO  r   r   rP  r   r   r   r/   r/   r/   r3   rS    sD    
M	=rS  )@r  r   pathlibr   typingr   r   r   r   r   rk   rl   huggingface_hubr   Zhuggingface_hub.constantsr	   huggingface_hub.utilsr
   	packagingr   r   	diffusersr   Zdiffusers.models.modeling_utilsr   diffusers.utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Z'diffusers.loaders.lora_conversion_utilsr   r   transformersr    Zdiffusers.models.lorar!   r"   Zaccelerate.hooksr#   r$   r%   
get_loggerrE  r   rI  rJ  rK  rp   rj   ZLORA_DEPRECATION_MESSAGEr)   rS  r/   r/   r/   r3   <module>   sJ   D
        o