
    
3j                       S SK r S SKJr  S SKrS SKJr  SSKJrJrJ	r	J
r
JrJrJrJrJrJrJr  SSKJrJrJrJrJrJr  SSKJrJrJrJrJrJrJ r J!r!J"r"J#r#J$r$J%r%J&r&J'r'J(r(J)r)J*r*  S	r+\" S
S5      (       a6  \" 5       (       a*  \" S
S5      (       a  \" 5       (       a  \" SS5      (       a  Sr+\RX                  " \-5      r.Sr/Sr0Sr1Sr2SS0r3S r4 " S S\5      r5 " S S\5      r6 " S S\5      r7 " S S\5      r8 " S S \5      r9 " S! S"\55      r: " S# S$\5      r; " S% S&\5      r< " S' S(\5      r= " S) S*\5      r> " S+ S,\5      r? " S- S.\5      r@ " S/ S0\5      rA " S1 S2\5      rB " S3 S4\5      rC " S5 S6\5      rD " S7 S8\5      rE " S9 S:\5      rF " S; S<\5      rG " S= S>\5      rH " S? S@\5      rI " SA SB\5      rJ " SC SD\5      rK " SE SF\55      rLg)G    N)Callable)validate_hf_hub_args   )USE_PEFT_BACKEND	deprecateget_submodule_by_nameis_bitsandbytes_availableis_gguf_availableis_peft_availableis_peft_versionis_torch_versionis_transformers_availableis_transformers_versionlogging   )LORA_WEIGHT_NAMELORA_WEIGHT_NAME_SAFELoraBaseMixin_fetch_state_dict_load_lora_into_text_encoder_pack_dict_with_prefix)+_convert_bfl_flux_control_lora_to_diffusers&_convert_fal_kontext_lora_to_diffusers(_convert_hunyuan_video_lora_to_diffusers&_convert_kohya_flux2_lora_to_diffusers%_convert_kohya_flux_lora_to_diffusers%_convert_musubi_wan_lora_to_diffusers._convert_non_diffusers_flux2_lora_to_diffusers0_convert_non_diffusers_hidream_lora_to_diffusers(_convert_non_diffusers_lora_to_diffusers-_convert_non_diffusers_ltx2_lora_to_diffusers-_convert_non_diffusers_ltxv_lora_to_diffusers0_convert_non_diffusers_lumina2_lora_to_diffusers-_convert_non_diffusers_qwen_lora_to_diffusers,_convert_non_diffusers_wan_lora_to_diffusers0_convert_non_diffusers_z_image_lora_to_diffusers%_convert_xlabs_flux_lora_to_diffusers"_maybe_map_sgm_blocks_to_diffusersF>=z1.9.00.13.1>z4.45.2Ttext_encoderunettransformer
connectors
x_embedderin_channelsc                    [        5       (       a  SSKJn  [        5       (       a  SSKJn  UR                  R                  R                  S:H  nUR                  R                  R                  S:H  nUR                  R                  R                  S:H  nU(       a  [        5       (       d  [        S5      eU(       a  [        5       (       d  [        S5      eU(       a  [        5       (       d  [        S	5      eS
nUR                  R                  R                  S:X  a  Sn[        [        S5      (       a(  [        R                  R                  5       R                  OSnU(       d  U(       ap  W" U(       a  UR                  R!                  U5      OUR                  U(       a  UR                  R"                  OUR$                  U R&                  S9R(                  n	OmU(       aP  W" U(       a  UR                  R!                  U5      OUR                  5      n	U	R!                  U R&                  5      n	OUR                  R(                  n	U(       a  U	R+                  5       n	U	$ )Nr   )dequantize_bnb_weight)dequantize_gguf_tensor
Params4bit
Int8ParamsGGUFParameterz~The checkpoint seems to have been quantized with `bitsandbytes` (4bits). Install `bitsandbytes` to load quantized checkpoints.z~The checkpoint seems to have been quantized with `bitsandbytes` (8bits). Install `bitsandbytes` to load quantized checkpoints.zfThe checkpoint seems to have been quantized with `gguf`. Install `gguf` to load quantized checkpoints.FcpuTacceleratorcuda)statedtype)r	   quantizers.bitsandbytesr3   r
   quantizers.gguf.utilsr4   weight	__class____name__
ValueErrordevicetypehasattrtorchr9   current_acceleratortoquant_stater;   r<   datar8   )
modelmoduler3   r4   is_bnb_4bit_quantizedis_bnb_8bit_quantizedis_gguf_quantizedweight_on_cpurC   module_weights
             Y/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/loaders/lora_pipeline.py*_maybe_dequantize_weight_for_expanded_lorarS   T   s    ""CB"MM33<<L"MM33<<L//88OK%>%@%@ M
 	
 %>%@%@ M
 	
 !2!4!4t
 	
 M}}  E)=DUM=Z=ZU22499`fF 5-(5FMMV$6==/D&--++&,,++
 $	 	
 
.(5FMMV$6==
 &((5**%))+    c                   P  ^  \ rS rSrSrSS/r\r\r	  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\\S\
\\
\R                  4   -  4S j5       5       r\     SS	\4S jj5       r\       S S	\4S jj5       r\        S!S\
\R,                  -  S\\
\R.                  R0                  \R                  -  4   S\\
\R.                  R0                  4   S\S\
S\S\4S jj5       rSS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrSS/4S\\
   4U 4S jjjrSrU =r $ )"StableDiffusionLoraLoaderMixin   z
Load LoRA layers into Stable Diffusion [`UNet2DConditionModel`] and
[`CLIPTextModel`](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel).
r-   r,   NF%pretrained_model_name_or_path_or_dictadapter_namehotswapc                    [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       d  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pgn[        S UR                  5        5       5      n	U	(       d  [        S	5      eU R                  UU[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  U R!                  UU[        U S5      (       d  [        X R"                  5      OU R$                  U R&                  UU UUUS9	  g)aw
  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.StableDiffusionLoraLoaderMixin.lora_state_dict`] for more details on how the state dict is
loaded.

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

See [`~loaders.StableDiffusionLoraLoaderMixin.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.StableDiffusionLoraLoaderMixin.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.
    low_cpu_mem_usage (`bool`, *optional*):
        Speed up model loading by only loading the pretrained LoRA weights and not initializing the random
        weights.
    hotswap (`bool`, *optional*):
        Defaults to `False`. Whether to substitute an existing (LoRA) adapter with the newly loaded adapter
        in-place. This means that, instead of loading an additional adapter, this will take the existing
        adapter weights and replace them with the weights of the new adapter. This can be faster and more
        memory efficient. However, the main advantage of hotswapping is that when the model is compiled with
        torch.compile, loading the new adapter does not require recompilation of the model. When using
        hotswapping, the passed `adapter_name` should be the name of an already loaded adapter.

        If the new adapter and the old adapter have different ranks and/or LoRA alphas (i.e. scaling), you need
        to call an additional method before loading the adapter:

        ```py
        pipeline = ...  # load diffusers pipeline
        max_rank = ...  # the highest rank among all LoRAs that you want to load
        # call *before* compiling and loading the LoRA adapter
        pipeline.enable_lora_hotswap(target_rank=max_rank)
        pipeline.load_lora_weights(file_name)
        # optionally compile the model now
        ```

        Note that hotswapping adapters of the text encoder is not yet supported. There are some further
        limitations to this technique, which are documented here:
        https://huggingface.co/docs/peft/main/en/package_reference/hotswap
    kwargs (`dict`, *optional*):
        See [`~loaders.StableDiffusionLoraLoaderMixin.lora_state_dict`].
)PEFT backend is required for this method.low_cpu_mem_usager)   r*   q`low_cpu_mem_usage=True` is not compatible with this `peft` version. Please update it with `pip install -U peft`.Treturn_lora_metadatac              3   ,   #    U  H
  nS U;   v   M     g7floraN .0keys     rR   	<genexpr>CStableDiffusionLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>        K9J##9J   SInvalid LoRA checkpoint. Make sure all LoRA param names contain `'lora'` substring.r-   network_alphasr-   rY   metadata	_pipeliner]   rZ   r,   )rm   r,   
lora_scalerY   ro   rn   r]   rZ   N)r   rB   pop_LOW_CPU_MEM_USAGE_DEFAULT_LORAr   
isinstancedictcopylora_state_dictallkeysload_lora_into_unetrE   getattr	unet_namer-   load_lora_into_text_encodertext_encoder_namer,   rp   
selfrX   rY   rZ   kwargsr]   
state_dictrm   rn   is_correct_formats
             rR   load_lora_weights0StableDiffusionLoraLoaderMixin.load_lora_weights   s[   p  HII"JJ':<[\_T8%D%D D 
 ;TBB4Y4^4^4`1 *.%&/3/C/CDi/tms/t,
HK9JKK rss  )6=dF6K6K~~.QUQZQZ%/ 	! 		
 	(()400 !'='=>""%/ 	) 	
rT   c                 P   UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnnSn[        S UR                  5        5       5      (       a  Ub  [        UU5      n[        U5      u  nnU(       a  UUU4nU$ UU4nU$ s  snnf )	  
Return state dict for lora weights and the network alphas.

> [!WARNING] > We support loading A1111 formatted LoRA checkpoints in a limited capacity. > > This function is
experimental and might change in the future.

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 (`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.

    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.
    weight_name (`str`, *optional*, defaults to None):
        Name of the serialized state dict file.
    return_lora_metadata (`bool`, *optional*, defaults to False):
        When enabled, additionally return the LoRA adapter metadata, typically found in the state dict.
	cache_dirNforce_downloadFproxieslocal_files_onlytokenrevision	subfolderweight_nameunet_configuse_safetensorsr_   Tattn_procs_weightspytorch	file_type	frameworkrX   r   r   r   r   r   r   r   r   r   
user_agentallow_picklec              3   ,   #    U  H
  nS U;   v   M     g7f
dora_scaleNrc   re   ks     rR   rg   AStableDiffusionLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>I       #Jz!LA$5zrj   !  It seems like you are using a DoRA checkpoint that is not compatible in Diffusers at the moment. So, we are going to filter out the keys associated to 'dora_scale` from the state dict. If you think this is a mistake please open an issue https://github.com/huggingface/diffusers/issues/new.r   c              3      #    U  H^  nUR                  S 5      =(       dA    UR                  S5      =(       d)    UR                  S5      =(       d    UR                  S5      v   M`     g7flora_te_
lora_unet_	lora_te1_	lora_te2_N
startswithr   s     rR   rg   r   Q  ^      
 ' Z( -<<--<<,- <<,-
 '   A&A(
rq   r   anyloggerwarningitemsrw   rx   r(   r    clsrX   r   r   r   r   r   r   r   r   r   r   r   r_   r   r   r   rn   is_dora_scale_presentwarn_msgr   vrm   outs                           rR   rv   .StableDiffusionLoraLoaderMixin.lora_state_dict   s   h JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H !$#Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JW 
  __&
 
 
 &?
KX
)QR\)]&J8Lz>84
 T^_mRn
) X   /F" F"c	                     [         (       d  [        S5      eU(       a  [        SS5      (       d  [        S5      e[        R	                  SU R
                   S35        UR                  UU R
                  UUUUUUS9  g	a  
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]`):
        The value of the network alpha used for stable learning and preventing underflow. This value has the
        same meaning as the `--network_alpha` option in the kohya-ss trainer script. Refer to [this
        link](https://github.com/darkstorm2150/sd-scripts/blob/main/docs/train_network_README-en.md#execute-learning).
    unet (`UNet2DConditionModel`):
        The UNet model to load the LoRA layers into.
    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.
    low_cpu_mem_usage (`bool`, *optional*):
        Speed up model loading only loading the pretrained LoRA weights and not initializing the random
        weights.
    hotswap (`bool`, *optional*):
        See [`~loaders.StableDiffusionLoraLoaderMixin.load_lora_weights`].
    metadata (`dict`):
        Optional LoRA adapter metadata. When supplied, the `LoraConfig` arguments of `peft` won't be derived
        from the state dict.
r\   r)   r*   r^   Loading .)prefixrm   rY   rn   ro   r]   rZ   Nr   rB   r   r   infor{   load_lora_adapter	r   r   rm   r-   rY   ro   r]   rZ   rn   s	            rR   ry   2StableDiffusionLoraLoaderMixin.load_lora_into_unetc  s    L  HII_T8%D%D D  	hs}}oQ/0==)%/ 	 		
rT         ?c                 >    [        UUUUUU R                  UU
UUU	S9  ga}  
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]`):
        The value of the network alpha used for stable learning and preventing underflow. This value has the
        same meaning as the `--network_alpha` option in the kohya-ss trainer script. Refer to [this
        link](https://github.com/darkstorm2150/sd-scripts/blob/main/docs/train_network_README-en.md#execute-learning).
    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.
    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.
    low_cpu_mem_usage (`bool`, *optional*):
        Speed up model loading by only loading the pretrained LoRA weights and not initializing the random
        weights.
    hotswap (`bool`, *optional*):
        See [`~loaders.StableDiffusionLoraLoaderMixin.load_lora_weights`].
    metadata (`dict`):
        Optional LoRA adapter metadata. When supplied, the `LoraConfig` arguments of `peft` won't be derived
        from the state dict.
)r   rm   rp   r,   r   r}   rY   rn   ro   r]   rZ   Nr   r}   r   r   rm   r,   r   rp   rY   ro   r]   rZ   rn   s              rR   r|   :StableDiffusionLoraLoaderMixin.load_lora_into_text_encoder  s4    X 	%!)!%!33%/	
rT   save_directoryunet_lora_layerstext_encoder_lora_layersis_main_processr   save_functionsafe_serializationc
           
          0 n
0 nU(       a  X*U R                   '   XU R                   '   U(       a  X:U R                  '   XU R                  '   U
(       d  [        S5      eU R                  UU
UUUUUS9  g)ux  
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`.
    unet_lora_adapter_metadata:
        LoRA adapter metadata associated with the unet to be serialized with the state dict.
    text_encoder_lora_adapter_metadata:
        LoRA adapter metadata associated with the text encoder to be serialized with the state dict.
zOYou must pass at least one of `unet_lora_layers` or `text_encoder_lora_layers`.r   lora_layerslora_metadatar   r   r   r   N)r{   r}   rB   _save_lora_weights)r   r   r   r   r   r   r   r   unet_lora_adapter_metadata"text_encoder_lora_adapter_metadatar   r   s               rR   save_lora_weights0StableDiffusionLoraLoaderMixin.save_lora_weights  s    N )9&+E#--(#1I--.3U#//0noo)#'+#'1 	 	
rT   
componentsrp   safe_fusingadapter_namesc                 0   > [         TU ]  " SUUUUS.UD6  g)a  
Fuses the LoRA parameters into the original parameters of the corresponding blocks.

> [!WARNING] > This is an experimental API.

Args:
    components: (`list[str]`): list of LoRA-injectable components to fuse the LoRAs into.
    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   rp   r   r   Nrc   super	fuse_lorar   r   rp   r   r   r   r@   s         rR   r   (StableDiffusionLoraLoaderMixin.fuse_lora  s.    F 	 	
!!#'		

 	
rT   c                 *   > [         TU ]  " SSU0UD6  g)aS  
Reverses the effect of
[`pipe.fuse_lora()`](https://huggingface.co/docs/diffusers/main/en/api/loaders#diffusers.loaders.LoraBaseMixin.fuse_lora).

> [!WARNING] > This is an experimental API.

Args:
    components (`list[str]`): list of LoRA-injectable components to unfuse LoRA from.
    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   Nrc   r   unfuse_lorar   r   r   r@   s      rR   r   *StableDiffusionLoraLoaderMixin.unfuse_loraD  s     	<z<V<rT   rc   NFNNFFNNr   NNFFNNNTNNTNN!rA   
__module____qualname____firstlineno____doc___lora_loadable_modules	UNET_NAMEr{   TEXT_ENCODER_NAMEr}   strrt   rF   Tensorboolr   classmethodr   rv   ry   r|   osPathLikennModuler   r   listfloatr   r   __static_attributes____classcell__r@   s   @rR   rV   rV      s!   
 %n5I)
 $(	c
/2T#u||:K5L/Lc
 Djc
 	c
J k/2T#u||:K5L/Lk  kZ  :
 :
 :
x  7
 7
 7
r  GK?C $"&#'#'+/<
bkk)<
 sEHHOOell$BBC<
 #'sEHHOO';"<	<

 <
 <
  <
 !<
 <
@ "( 8!*.)
I)
 )
 	)

 Cy4')
 )
V 4:>2J =d3i = =rT   rV   c                     ^  \ rS rSrSr/ SQr\r\r	  SS\
\\
\R                  4   -  S\
S-  S\4S	 jjr\\S\
\\
\R                  4   -  4S
 j5       5       r\     SS\4S jj5       r\       S S\4S jj5       r\          S!S\
\R,                  -  S\\
\R.                  R0                  \R                  -  4   S\\
\R.                  R0                  \R                  -  4   S\\
\R.                  R0                  \R                  -  4   S\S\
S\S\4S jj5       r/ SQSSS4S\\
   S\S\S\\
   S-  4U 4S jjjr/ SQ4S\\
   4U 4S jjjrSrU =r $ )" StableDiffusionXLLoraLoaderMixiniU  a3  
Load LoRA layers into Stable Diffusion XL [`UNet2DConditionModel`],
[`CLIPTextModel`](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel), and
[`CLIPTextModelWithProjection`](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModelWithProjection).
)r-   r,   text_encoder_2NFrX   rY   rZ   c                    [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       d  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U4SU R                  R                  0UD6u  pgn[        S	 UR                  5        5       5      n	U	(       d  [        S
5      eU R                  UUU R                  UUU UUS9  U R                  UUU R                  U R                   U R"                  UUU UUS9
  U R                  UUU R$                  U R                    S3U R"                  UUU UUS9
  g)U
See [`~loaders.StableDiffusionLoraLoaderMixin.load_lora_weights`] for more details.
r\   r]   r)   r*   r^   Tr_   r   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   EStableDiffusionXLLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>  ri   rj   rk   rl   	rm   r,   r   rp   rY   rn   ro   r]   rZ   _2N)r   rB   rq   rr   r   rs   rt   ru   rv   r-   configrw   rx   ry   r|   r,   r}   rp   r  r~   s
             rR   r   2StableDiffusionXLLoraLoaderMixin.load_lora_weights`  s     HII"JJ':<[\_T8%D%D D  ;TBB4Y4^4^4`1 *.%&/3/C/C10
		((0
 0
,
H  K9JKK rss  )%/ 	! 		
 	(()**))%/ 	) 	
 	((),,,,-R0%/ 	) 	
rT   c                 P   UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnnSn[        S UR                  5        5       5      (       a  Ub  [        UU5      n[        U5      u  nnU(       a  UUU4nU$ UU4nU$ s  snnf )r   r   Nr   Fr   r   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   CStableDiffusionXLLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>   r   rj   r   r   c              3      #    U  H^  nUR                  S 5      =(       dA    UR                  S5      =(       d)    UR                  S5      =(       d    UR                  S5      v   M`     g7fr   r   r   s     rR   rg   r    r   r   r   r   s                           rR   rv   0StableDiffusionXLLoraLoaderMixin.lora_state_dict  s   j JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H !$#Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JW 
  __&
 
 
 &?
KX
)QR\)]&J8Lz>84
 T^_mRn
) Xr   c	                     [         (       d  [        S5      eU(       a  [        SS5      (       d  [        S5      e[        R	                  SU R
                   S35        UR                  UU R
                  UUUUUUS9  gr   r   r   s	            rR   ry   4StableDiffusionXLLoraLoaderMixin.load_lora_into_unet  s    N  HII_T8%D%D D  	hs}}oQ/0==)%/ 	 		
rT   r   c                 >    [        UUUUUU R                  UU
UUU	S9  gr   r   r   s              rR   r|   <StableDiffusionXLLoraLoaderMixin.load_lora_into_text_encoderX  4    Z 	%!)!%!33%/	
rT   r   r   r   text_encoder_2_lora_layersr   r   r   r   c           
          0 n0 nU(       a  X,U R                   '   XU R                   '   U(       a  X<S'   XS'   U(       a  XLS'   XS'   U(       d  [        S5      eU R                  UUUUUUUS9  g)Y
See [`~loaders.StableDiffusionLoraLoaderMixin.save_lora_weights`] for more information.
r,   r  znYou must pass at least one of `unet_lora_layers`, `text_encoder_lora_layers`, or `text_encoder_2_lora_layers`.r   N)r{   rB   r   )r   r   r   r   r  r   r   r   r   r   r   $text_encoder_2_lora_adapter_metadatar   r   s                 rR   r   2StableDiffusionXLLoraLoaderMixin.save_lora_weights  s    $ )9&+E#--(#*B',N.)%,F().R*+ A  	)#'+#'1 	 	
rT   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gzM
See [`~loaders.StableDiffusionLoraLoaderMixin.fuse_lora`] for more details.
r   Nrc   r   r   s         rR   r   *StableDiffusionXLLoraLoaderMixin.fuse_lora  -     	 	
!!#'		

 	
rT   c                 *   > [         TU ]  " SSU0UD6  gzO
See [`~loaders.StableDiffusionLoraLoaderMixin.unfuse_lora`] for more details.
r   Nrc   r   r   s      rR   r   ,StableDiffusionXLLoraLoaderMixin.unfuse_lora       	<z<V<rT   rc   r   r   r   
NNNTNNTNNNr   r   s   @rR   r   r   U  sH    HI)
 $(	H
/2T#u||:K5L/LH
 DjH
 	H
T k/2T#u||:K5L/Lk  kZ  :
 :
 :
x  7
 7
 7
r  GKNRPT $"&#'#'+/-1-
bkk)-
 sEHHOOell$BBC-
 #'sEHHOOell,J'J"K	-

 %)ehhoo.L)L$M-
 -
 -
  -
 !-
 -
b !K!*.
I
 
 	

 Cy4'
 
& 3] =d3i = =rT   r   c                     ^  \ rS rSrSr/ SQr\r\r	\
\S\\\\R                  4   -  4S j5       5       r  SS\\\\R                  4   -  S\4S	 jjr\
     SS\4S
 jj5       r\
       SS\4S jj5       r\
          S S\\R,                  -  S\\\R.                  R0                  \R                  -  4   S\\\R.                  R0                  \R                  -  4   S\\\R.                  R0                  \R                  -  4   S\S\S\S\4S jj5       r/ SQSSS4S\\   S\S\S\\   S-  4U 4S jjjr/ SQ4S\\   4U 4S jjjrSrU =r $ )!SD3LoraLoaderMixini  aK  
Load LoRA layers into [`SD3Transformer2DModel`],
[`CLIPTextModel`](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel), and
[`CLIPTextModelWithProjection`](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModelWithProjection).

Specific to [`StableDiffusion3Pipeline`].
)r.   r,   r  rX   c                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnnU(       a  UU4nU$ UnU$ s  snnf )S
See [`~loaders.StableDiffusionLoraLoaderMixin.lora_state_dict`] for more details.
r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   5SD3LoraLoaderMixin.lora_state_dict.<locals>.<genexpr>  r   rj   r   r   rq   r   r   r   r   r   r   rX   r   r   r   r   r   r   r   r   r   r   r_   r   r   r   rn   r   r   r   r   r   s                         rR   rv   "SD3LoraLoaderMixin.lora_state_dict  s|    JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H !$#Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JW(<z8$
 CM
 X   E	.E	NFrZ   c                    [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  U R!                  USU R"                  U R$                  U R&                  UUU UUS9
  U R!                  USU R(                  U R$                   S3U R&                  UUU UUS9
  g)r  r\   r]   <0.13.0r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   7SD3LoraLoaderMixin.load_lora_weights.<locals>.<genexpr>;  ri   rj   rk   r.   r.   rY   rn   ro   r]   rZ   Nr  r  )r   rB   rq   rr   r   rs   rt   ru   rv   rw   rx   load_lora_into_transformerrE   rz   transformer_namer.   r|   r,   r}   rp   r  	r   rX   rY   rZ   r   r]   r   rn   r   s	            rR   r   $SD3LoraLoaderMixin.load_lora_weights   s     HII"JJ':<[\h!?!? D 
 ;TBB4Y4^4^4`1 *.%&#334Yd]cd
K9JKK rss''DKDR_D`D`&;&;<fjfvfv%/ 	( 	
 	((**))%/ 	) 	
 	((,,,,-R0%/ 	) 	
rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gW
See [`~loaders.StableDiffusionLoraLoaderMixin.load_lora_into_unet`] for more details.
r-  r.  r^   r   r   Nrm   rY   rn   ro   r]   rZ   r   rB   r   r   r3  r   r   r   r.   rY   ro   r]   rZ   rn   s           rR   r2  -SD3LoraLoaderMixin.load_lora_into_transformera  sl     h!?!? D 
 	hs334A67%%%/ 	& 	
rT   r   c                 >    [        UUUUUU R                  UU
UUU	S9  gr   r   r   s              rR   r|   .SD3LoraLoaderMixin.load_lora_into_text_encoder  r  rT   r   transformer_lora_layersr   r  r   r   r   r   c           
          0 n0 nU(       a  X,U R                   '   XU R                   '   U(       a  X<S'   XS'   U(       a  XLS'   XS'   U(       d  [        S5      eU R                  UUUUUUUS9  g)r  r,   r  zuYou must pass at least one of `transformer_lora_layers`, `text_encoder_lora_layers`, or `text_encoder_2_lora_layers`.r   Nr3  rB   r   )r   r   r?  r   r  r   r   r   r   !transformer_lora_adapter_metadatar   r  r   r   s                 rR   r   $SD3LoraLoaderMixin.save_lora_weights  s    & "0G,,-2S#../#*B',N.)%,F().R*+ H  	)#'+#'1 	 	
rT   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   SD3LoraLoaderMixin.fuse_lora  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   SD3LoraLoaderMixin.unfuse_lora  r   rT   rc   r   r   r   r!  )!rA   r   r   r   r   r   TRANSFORMER_NAMEr3  r   r}   r   r   r   rt   rF   r   rv   r   r   r2  r|   r   r   r   r   r   r   r   r   r   r   r   r   r   s   @rR   r#  r#    s1    O')2/2T#u||:K5L/L2  2n 	?
/2T#u||:K5L/L?
 	?
B 
 
 
 
<  7
 7
 7
r 
 NRNRPT $"&#'*.+/-1-
bkk)-
 "&c588??U\\+I&I!J-
 #'sEHHOOell,J'J"K	-

 %)ehhoo.L)L$M-
 -
 -
  -
 !-
 -
d !R!*.
I
 
 	

 Cy4'
 
( 3d =d3i = =rT   r#  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrSS/4S\\
   4U 4S jjjrSrU =r$ ) AuraFlowLoraLoaderMixini  zX
Load LoRA layers into [`AuraFlowTransformer2DModel`] Specific to [`AuraFlowPipeline`].
r.   rX   c                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnnU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   :AuraFlowLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>>  r   rj   r   r   r(  r)  s                         rR   rv   'AuraFlowLoraLoaderMixin.lora_state_dict  |    JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H !$#Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JW(<z8$
 CM
 Xr+  NFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   <AuraFlowLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>c  ri   rj   rk   r.   r1  Nr   rB   rq   rr   r   rs   rt   ru   rv   rw   rx   r2  rE   rz   r3  r.   r4  s	            rR   r   )AuraFlowLoraLoaderMixin.load_lora_weightsH       HII"JJ':<[\h!?!? D 
 ;TBB4Y4^4^4`1 *.%&#334Yd]cd
K9JKK rss''DKDR_D`D`&;&;<fjfvfv%/ 	( 	
rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  2AuraFlowLoraLoaderMixin.load_lora_into_transformerq  l     h!?!? D 
 	hs334A67%%%/ 	& 	
rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  gr  VYou must pass at least one of `transformer_lora_layers` or `text_encoder_lora_layers`.r   NrA  
r   r   r?  r   r   r   r   rB  r   r   s
             rR   r   )AuraFlowLoraLoaderMixin.save_lora_weights  d     "0G,,-2S#../uvv)#'+#'1 	 	
rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   !AuraFlowLoraLoaderMixin.fuse_lora  r  rT   r,   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   #AuraFlowLoraLoaderMixin.unfuse_lora  r   rT   rc   r   r   NTNNTNrA   r   r   r   r   r   rH  r3  r   r   r   rt   rF   r   rv   r   r   r2  r   r   r   r   r   r   r   r   r   r   r   r   r   s   @rR   rJ  rJ    s    ,_'2/2T#u||:K5L/L2  2p $(	'
/2T#u||:K5L/L'
 Dj'
 	'
R  
 
 
< 
 NR $"&#'9=
bkk)
 "&c588??U\\+I&I!J
 	

 
  
 !
 ,0$;
 
H "/!*.
I
 
 	

 Cy4'
 
( 4A.2Q =d3i = =rT   rJ  c                   L  ^  \ rS rSrSrSS/r\r\r	/ SQr
\\ S-S\\\\R                   4   -  S\4S	 jj5       5       r  S.S\\\\R                   4   -  S\S
-  S\4S jjr\     S/S\4S jj5       r\  S.S\\\R                   4   4S jj5       r\       S0S\4S jj5       r\        S1S\\R0                  -  S\\\R2                  R4                  \R                   -  4   S\\\R2                  R4                  4   S\S\S\S\4S jj5       rS/SSS
4S\\   S\S\S\\   S
-  4U 4S jjjrSS/4S\\   4U 4S  jjjr S-U 4S! jjr!\   S2S\R2                  R4                  S\4S" jj5       r"\S# 5       r#\$  S3S$S%S&S'S(\SS)4S* jj5       r%\$S4S+ j5       r&S,r'U =r($ )5FluxLoraLoaderMixini  z
Load LoRA layers into [`FluxTransformer2DModel`],
[`CLIPTextModel`](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel).

Specific to [`FluxPipeline`].
r.   r,   )norm_qnorm_knorm_added_qnorm_added_kFrX   return_alphasc                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      n	UR                  S	S5      n
UR                  S
S5      nUR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UUUUUUUUU	U
UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnn[        S U 5       5      nU(       a  [        U5      nU R                  UUSUUS9$ [        S U 5       5      nU(       a  [        U5      nU R                  UUSUUS9$ [        S U 5       5      nU(       a  [        U5      nU R                  UUSUUS9$ [        S U 5       5      nU(       a  [        U5      nU R                  UUSUUS9$ [        UR                  5       5      n0 nU H  nSU;   d  M  UR                  U5      n[        R                  " U5      (       a  [        R                   " U5      (       d  [#        U[$        5      (       a  UR                  U5      UU'   M}  ['        SU S35      e   U(       d  U(       a  U R                  UUUUUS9$ U$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   6FluxLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>
  r   rj   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fz.lora_down.weightNrc   r   s     rR   rg   rn         DA*a/rj   )rn   alphasrk  return_metadatac              3   ,   #    U  H
  nS U;   v   M     g7f)	processorNrc   r   s     rR   rg   rn    s     <A{a'rj   c              3   ,   #    U  H
  nS U;   v   M     g7f)zquery_norm.scaleNrc   r   s     rR   rg   rn  )  s     Ij/14jrj   c              3   ,   #    U  H
  nS U;   v   M     g7f)
base_modelNrc   r   s     rR   rg   rn  4  s     C
1\Q.
rj   alphazThe alpha key (zU) seems to be incorrect. If you think this error is unexpected, please open as issue.)rq   r   r   r   r   r   r   _prepare_outputsr'   r   r   r   rx   getrF   	is_tensoris_floating_pointrs   r   rB   )r   rX   rk  r   r   r   r   r   r   r   r   r   r   r_   r   r   r   rn   r   r   r   r   is_kohyais_xlabsis_bfl_controlis_fal_kontextrx   rm   alpha_values                                rR   rv   #FluxLoraLoaderMixin.lora_state_dict  sL    JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H !$#Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JW DDD>zJJ''!+ 4 (   <<<>zJJ''!+ 4 (   IjIIDZPJ''!+ 4 (   C
CC?
KJ''!+ 4 (   JOO%&A!|(nnQ/OOK00U5L5L[5Y5Y^h_ _ )3q(9N1%$)!  -B  C   0''!%+ 4 (   U Xs   K".K"NrY   rZ   c                 
  ^ ^ [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       d  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   T R                  " U4SS0UD6u  pgn[        S	 UR                  5        5       5      n	[        U 4S
 jUR                  5        5       5      n
U	(       d  U
(       d  [        S5      e[        UR                  5       5       Vs0 s HA  nUR                  T R                   S35      (       d  M(  SU;   d  M0  XR                  U5      _MC     nn[        UR                  5       5       V^s0 s H`  mTR                  T R                   S35      (       d  M(  [        U4S jT R                   5       5      (       d  MN  TUR                  T5      _Mb     nn[!        T S5      (       d  [#        T T R                  5      OT R$                  nSn['        U5      S:  a  T R)                  XU5      nU(       a  [*        R-                  S5        ['        U5      S:  a.  T R/                  XS9nU H  mUR1                  TUT   05        M     T R3                  UUUUUT UUS9  ['        U5      S:  a  T R5                  UUSS9Ul        T R9                  UUT R:                  T R<                  T R>                  UUT UUS9
  gs  snf s  snf )r  r\   r]   r)   r*   r^   Tr_   rk  c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   8FluxLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>w  s     G5FcFcM5Frj   c              3   T   >#    U  H  nTR                     H	  o"U;   v   M     M     g 7fN!_control_lora_supported_norm_keys)re   rf   norm_keyr   s      rR   rg   r  z  s&      
'8IoIoXOIoO'8   %(rk   r   rb   c              3   ,   >#    U  H	  oT;   v   M     g 7fr  rc   )re   r  r   s     rR   rg   r    s     Y2XhM2Xs   r.   Fr   a  The LoRA weights contain parameters that have different shapes that expected by the transformer. As a result, the state_dict of the transformer has been expanded to match the LoRA parameter shapes. To get a comprehensive list of parameter names that were modified, enable debug logging.)r.   rv   )rm   r.   rY   rn   ro   r]   rZ   )r.   discard_original_layersr  N) r   rB   rq   rr   r   rs   rt   ru   rv   r   rx   r   r   r3  r{  r  rE   rz   r.   len/_maybe_expand_transformer_param_shape_or_error_r   r   _maybe_expand_lora_state_dictupdater2  _load_norm_into_transformer_transformer_norm_layersr|   r,   r}   rp   )r   rX   rY   rZ   r   r]   r   rm   rn   has_lora_keyshas_norm_keysr   transformer_lora_state_dicttransformer_norm_state_dictr.   has_param_with_expanded_shapes   `          `    rR   r   %FluxLoraLoaderMixin.load_lora_weightsZ  s+     HII"JJ':<[\_T8%D%D D 
 ;TBB4Y4^4^4`1 *.%&/3/C/C10
AE0
IO0
,
H GZ__5FGG  
'1'8
 
 rss *//+,'
,||t445Q78 !=Cq[ !A~~a  , 	$ '
 *//+,'
,||t445Q78 ! Y$2X2XYY !Az~~a  , 	$ '
 CJ$P]B^B^gdD$9$9:dhdtdt(-%*+a/,0,`,`:U-) )KKk
 *+a/*.*L*L' +M +' 1!!1&A!&D"EF 1 	'')#%/ 	( 		
 *+a/373S3S+'(- 4T 4K0 	(()**))%/ 	) 	
e'

'
s$   'K;6K;>K;1'L "L L c	           
          U(       a  [        SS5      (       d  [        S5      e[        R                  SU R                   S35        UR                  UUUUUUUS9  gr8  r)   r*   r^   r   r   r9  Nr:  	r   r   rm   r.   rY   rn   ro   r]   rZ   s	            rR   r2  .FluxLoraLoaderMixin.load_lora_into_transformer  sl     _T8%D%D D 
 	hs334A67%%)%/ 	& 	
rT   returnc                 .  ^  U=(       d    T R                   n[        UR                  5       5       HB  nUR                  S5      S   U:X  d  M  UR	                  U5      XR                  U S35      '   MD     UR                  5       n[        UR                  5       5      n[        UR                  5       5      n[        X-
  5      n	U	(       a  [        R                  SU	 S35        U	 H  nUR	                  U5        M     0 n
U(       d+  UR                  5        H  nXe   R                  5       X'   M     [        R                  S5        UR                  USS9n[        USS 5      nU(       a)  [        U 4S jU 5       5      (       a  [        S	U S
35      eU
$ )Nr   r   zUnsupported keys found in state dict when trying to load normalization layers into the transformer. The following keys will be ignored:
aW  The provided state dict contains normalization layers in addition to LoRA layers. The normalization layers will directly update the state_dict of the transformer as opposed to the LoRA layers that will co-exist separately until the "fuse_lora()" method is called. That is to say, the normalization layers will always be directly fused into the transformer and can only be unfused if `discard_original_layers=True` is passed. This might also have implications when dealing with multiple LoRAs. If you notice something unexpected, please open an issue: https://github.com/huggingface/diffusers/issues.Fstrictunexpected_keysc              3   T   >#    U  H  nTR                     H	  o"U;   v   M     M     g 7fr  r  )re   r   r  r   s      rR   rg   BFluxLoraLoaderMixin._load_norm_into_transformer.<locals>.<genexpr>  s"     p/Q#JoJohq=Jo=/r  zFound zJ as unexpected keys while trying to load norm layers into the transformer.)r3  r   rx   splitrq   removeprefixr   setr   r   cloner   load_state_dictrz   r   rB   )r   r   r.   r   r  rf   transformer_state_dicttransformer_keysstate_dict_keys
extra_keysoverwritten_layers_state_dictincompatible_keysr  s   `            rR   r  /FluxLoraLoaderMixin._load_norm_into_transformer  s    /3//
)*Cyy~a F*=G^^C=P
++vhaL9: +
 "-!7!7!95::<=joo/0/<=
NN \  ]g  \h  hi  j CNN3  )+%&!(5K5P5V5V5X-2 ) 	y	
 (77
57Q!"35FM p/ppp _--wx  -,rT   r   c                 >    [        UUUUUU R                  UU
UUU	S9  gr   r   r   s              rR   r|   /FluxLoraLoaderMixin.load_lora_into_text_encoder  r  rT   r   r?  r   r   r   r   r   c
           
          0 n
0 nU(       a  X*U R                   '   XU R                   '   U(       a  X:U R                  '   XU R                  '   U
(       d  [        S5      eU R                  UU
UUUUUS9  g)u  
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.
    transformer_lora_layers (`dict[str, torch.nn.Module]` or `dict[str, torch.Tensor]`):
        State dict of the LoRA layers corresponding to the `transformer`.
    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`.
    transformer_lora_adapter_metadata:
        LoRA adapter metadata associated with the transformer to be serialized with the state dict.
    text_encoder_lora_adapter_metadata:
        LoRA adapter metadata associated with the text encoder to be serialized with the state dict.
r[  r   N)r3  r}   rB   r   )r   r   r?  r   r   r   r   r   rB  r   r   r   s               rR   r   %FluxLoraLoaderMixin.save_lora_weightsQ  s    P "0G,,-2S#../#1I--.3U#//0uvv)#'+#'1 	 	
rT   r   rp   r   r   c                 l  > [        U S5      (       d  [        X R                  5      OU R                  n[        US5      (       a[  [	        UR
                  [        5      (       a<  [        UR
                  R                  5       5      S:  a  [        R                  S5        [        TU ]0  " SUUUUS.UD6  g)r%  r.   r  r   a  The provided state dict contains normalization layers in addition to LoRA layers. The normalization layers will be directly updated the state_dict of the transformer as opposed to the LoRA layers that will co-exist separately until the 'fuse_lora()' method is called. That is to say, the normalization layers will always be directly fused into the transformer and can only be unfused if `discard_original_layers=True` is passed.r   Nrc   )rE   rz   r3  r.   rs   r  rt   r  rx   r   r   r   r   )r   r   rp   r   r   r   r.   r@   s          rR   r   FluxLoraLoaderMixin.fuse_lora  s     CJ$P]B^B^gd$9$9:dhdtdtK!;<<;??FFK88==?@1DKKr 	 	
!!#'		

 	
rT   c                   > [        U S5      (       d  [        X R                  5      OU R                  n[        US5      (       a+  UR                  (       a  UR                  UR                  SS9  [        TU ]  " SSU0UD6  g)a  
Reverses the effect of
[`pipe.fuse_lora()`](https://huggingface.co/docs/diffusers/main/en/api/loaders#diffusers.loaders.LoraBaseMixin.fuse_lora).

> [!WARNING] > This is an experimental API.

Args:
    components (`list[str]`): list of LoRA-injectable components to unfuse LoRA from.
r.   r  Fr  r   Nrc   )rE   rz   r3  r.   r  r  r   r   )r   r   r   r.   r@   s       rR   r   FluxLoraLoaderMixin.unfuse_lora  sr     CJ$P]B^B^gd$9$9:dhdtdt; :;;@d@d''(L(LUZ'[<z<V<rT   c           
        > [         TU ]  5         [        U S5      (       d  [        X R                  5      OU R
                  n[        US5      (       a2  UR                  (       a!  UR                  UR                  SS9  SUl        U(       Ga?  [        USS5      Gb/  UR                  n[        5       nU H<  nUR                  S5      (       d  M  UR                  UR                  SS5      5        M>     UR                  5        GH  u  pg[        U[        R                   R"                  5      (       d  M1  Xd;   d  M8  UR$                  R&                  nUR(                  b  UR(                  R&                  OSn	U	SLn
UR+                  S	5      u  pnUR-                  U5      nX6 S3   nUR.                  S
   UR.                  S   nn[        R0                  " S5         [        R                   R#                  UUU
UR2                  S9nSSS5        SU0nU	b  UR5                  SX6 S3   05        WR                  USSS9  [7        XU5        AU[8        ;   d  GMT  [8        U   n[;        UR.                  S
   5      n[        UR<                  U5      n[7        UR<                  UU5        [>        RA                  SU SU SU S	35        GM     ggg! , (       d  f       N= f)a  
Unloads the LoRA parameters.

Args:
    reset_to_overwritten_params (`bool`, defaults to `False`): Whether to reset the LoRA-loaded modules
        to their original params. Refer to the [Flux
        documentation](https://huggingface.co/docs/diffusers/main/en/api/pipelines/flux) to learn more.

Examples:

```python
>>> # Assuming `pipeline` is already loaded with the LoRA parameters.
>>> pipeline.unload_lora_weights()
>>> ...
```
r.   r  Fr  N_overwritten_params.weight r   r   r   metabiasr<   r?   r  .biasT)assignr  Set the  attribute of the model to  from )!r   unload_lora_weightsrE   rz   r3  r.   r  r  r  r  endswithaddreplacenamed_modulesrs   rF   r   Linearr?   rJ   r  
rpartitionget_submoduleshaperC   r<   r  setattr"_MODULE_NAME_TO_ATTRIBUTE_MAP_FLUXintr  r   r   )r   reset_to_overwritten_paramsr.   overwritten_paramsmodule_names
param_namenamerL   rQ   module_biasr  parent_module_name_current_module_nameparent_modulecurrent_param_weightin_featuresout_featuresoriginal_moduletmp_state_dictattribute_name	new_value	old_valuer@   s                          rR   r  'FluxLoraLoaderMixin.unload_lora_weights  s   " 	#%BI$P]B^B^gd$9$9:dhdtdt; :;;@d@d''(L(LUZ'[37K0&7;@UW[+\+h!,!@!@5L0
&&y11 $$Z%7%7	2%FG 1 !, 9 9 ;fehhoo664;O$*MM$6$6M6<kk6M&++"2"2SWK&d2DAEQTAU>&+>$/$=$=>P$QM+=g>N+O(0D0J0J10MOcOiOijkOlKf-*/((//'(!%"/"5"5	 +: + . '/0D%EN".&--v7IFRW.7Y.Z[#33N4X\3]MP&*.PP)KL_)`$'(<(B(B1(E$F	$+K,>,>$O	 2 2NIN&~&66QR[Q\\bclbmmnoA !< ,i&& .-s   +K
K	c           
         0 nUb  UR                  U5        Ub  UR                  U5        U=(       d    U R                  n[        UR                  5       5       HB  nUR	                  S5      S   U:X  d  M  UR                  U5      XVR                  U S35      '   MD     Sn0 n[        USS5      SLn	[        US5      n
UR                  5        GH  u  p[        U[        R                  R                  5      (       d  M1  UR                  R                  nUR                   b  UR                   R                  OSnUSLnU	(       a  UR#                  SS5      OUnU S	3nU S
3nUU;  a  M  UU   R$                  S   nUU   R$                  S   nU R'                  XS9n[)        U5      UU4:X  a  M  Uu  nnSnUU:  a  USU SU SU 3-  nUU:  a  USU SU S3-  nOUS-  nU(       a  [*        R-                  U5        UU:  d	  UU:  d  GMA  SnUR/                  S5      u  nnnUR1                  U5      nU
(       a  [3        X5      n[        R4                  " S5         [        R                  R                  UUXR6                  S9nSSS5        [        R8                  " WR                  R                  UR4                  UR6                  S9n[)        S U 5       5      nUUU'   SU0n Ub  UU S'   UR;                  U SSS9  [=        UUU5        A U[>        ;   a  [>        U   n![A        UR                  R                  R$                  S   5      n"[        URB                  U!5      n#[=        URB                  U!U"5        [*        RE                  SU! SU" SU# S35        XU S3'   Uc  GM  XU S3'   GM     [G        U5      S:  a  Xl$        U$ ! , (       d  f       GNJ= f)z
Control LoRA expands the shape of the input layer from (3072, 64) to (3072, 128). This method handles that and
generalizes things a bit so that any parameter that needs expansion receives appropriate treatment.
Nr   r   Fpeft_confighf_quantizerz.base_layerr  .lora_A.weight.lora_B.weightr   )rK   base_modulez:Expanding the nn.Linear input/output features for module="z" because the provided LoRA checkpoint contains higher number of features than expected. The number of input_features will be expanded from z to z:, and the number of output features will be expanded from Tr  r  )rC   r<   c              3   :   #    U  H  n[        S U5      v   M     g7f)r   N)slice)re   dims     rR   rg   VFluxLoraLoaderMixin._maybe_expand_transformer_param_shape_or_error_.<locals>.<genexpr>b  s     "P<OS5C==<Os   r?   r  )r  r  r  r  r  r  r  )%r  r3  r   rx   r  rq   r  rz   rE   r  rs   rF   r   r  r?   rJ   r  r  r  _calculate_module_shapetupler   debugr  r  rS   rC   r<   
zeros_liker  r  r  r  r  r   r  r  )$r   r.   rv   norm_state_dictr   r   rf   has_param_with_shape_updater  is_peft_loadedis_quantizedr  rL   rQ   r  r  lora_base_namelora_A_weight_namelora_B_weight_namer  r  module_weight_shapemodule_out_featuresmodule_in_featuresdebug_messager  r  r  r  expanded_module
new_weightslicesr  r  r  r  s$                                       rR   r  CFluxLoraLoaderMixin._maybe_expand_transformer_param_shape_or_error_  s+    
&o.&o. /3//
)*Cyy~a F*=G^^C=P
++vhaL9: +
 ',# mTB$N{N;'557LD&%((//22 & 2 228++2Ifkk..t"$.DRmR!@X\(6'7~%F"(6'7~%F"%Z7();<BB1E)*<=CCAF
 '*&A&A&A&h# ,-,1LL:M7#%7 "!33!TUYTZ [));(<DOM
  "55!))<(=T,qRM
 "S(M LL/"55GY9Y26/AEQTAU>&+>$/$=$=>P$QM#(RS^(g f-*/((//'DH[H[ +: + . "'!1!1'..33M<P<PXeXkXk"J #"P<O"PPF)6Jv&&.
%;N".1<v.#33N4X\3]M+>P&*.PP)KL_)`$'(>(>(C(C(I(I!(L$M	$+K,>,>$O	 2 2NIN&~&66QR[Q\\bclbmmno KX*=)>g'FG".LW.A-B%+HIo 8r !"Q&.@+**S .-s   *O
O	c                    [        5       nUR                  5       nU R                   S3nU Vs/ s H)  ofR                  S5      (       d  M  US [	        S5      *  PM+     nnU Vs/ s H(  oR                  U5      (       d  M  U[	        U5      S  PM*     nn[        [        U5      5      n[        UR                  5        VV	s1 s H  u  pUiM	     sn	n5      n
[        U5      [        U
5      -
  nU(       a  [        R                  SU S35        U GH3  nX;   a  M  UR                  US5       S3U;   a  UR                  US5       S3OUR                  US5       S3nXM   nX% U S3   nU R                  XS9nUS	   UR                  S	   :  a  UR                  S
   UR                  S	   4n[        R                  " UUR                  S9nUS S 2S UR                  S	   24   R!                  U5        UX% U S3'   UR#                  U5        GM  US	   UR                  S	   :  d  GM  [%        SU SUR                   S35      e   U(       a&  [        R'                  SU R                   SU S35        U$ s  snf s  snf s  sn	nf )Nr   r  zFound unexpected modules: z. These will be ignored.r  z.base_layer.weightr  )rK   base_weight_param_namer   r   rC   zThis LoRA param (z*.lora_A.weight) has an incompatible shape zk. Please open an issue to file for a feature request - https://github.com/huggingface/diffusers/issues/new.zGThe following LoRA modules were zero padded to match the state dict of z: zn. Please open an issue if you think this was unexpected - https://github.com/huggingface/diffusers/issues/new.)r  r   r3  r  r  r   sortedr  r   r  r  r  r  rF   zerosrC   copy_r  NotImplementedErrorr   )r   r.   rv   expanded_module_namesr  r   rf   lora_module_namesr  r  transformer_module_namesunexpected_modulesr   base_param_namebase_weight_paramlora_A_parambase_module_shaper  expanded_state_dict_weights                      rR   r  1FluxLoraLoaderMixin._maybe_expand_lora_state_dict  s    #!,!7!7!9(()+ 6E
5DcUeHf)C(3'(()_ 	 
 >Oj=NTRaRabhRi0T#f+-0=Nj"3'8#9:#){?X?X?Z*[?ZGD4?Z*[#\  !23c:R6SSLL56H5IIabc"A& ii+,,>?CYY 99VR());<		&"-.g6 
 !7 G*XaS+GHL !$ ; ;+ ; v #l&8&8&;;%++A.0A0G0G0JK-2[[GXG_G_-`**1.E0B0B10E.E+EFLL\Z@Z(1#^ <=%))!,"1%(:(:1(==)'s*TUaUgUgTh  iT  U - #4 !KKYZ]ZnZnYooq  sH  rI  Iw  x S
 k*[s   I7I7$I<I<J
rK   ztorch.nn.Moduler  ztorch.nn.Linearr  z
torch.Sizec                 "   S[         R                  4S jnUb  U" UR                  5      $ UbX  UR                  S5      (       d  [	        SU< S35      eUR                  SS5      S   n[        X5      nU" UR                  5      $ [	        S5      e)	Nr?   c                     U R                   R                  S:X  a  U R                  R                  $ U R                   R                  S:X  a  U R                  $ U R                  $ )Nr5   r7   )r@   rA   rI   r  quant_shape)r?   s    rR   _get_weight_shapeFFluxLoraLoaderMixin._calculate_module_shape.<locals>._get_weight_shape  sR    ((L8))///!!**o=)))||#rT   r  zaInvalid `base_weight_param_name` passed as it does not end with '.weight' base_weight_param_name=r   r   r   zBEither `base_module` or `base_weight_param_name` must be provided.)rF   r   r?   r  rB   rsplitr   )rK   r  r  r  module_path	submodules         rR   r  +FluxLoraLoaderMixin._calculate_module_shape  s    	$ell 	$ "$[%7%788#/)229== xaw`yyz{  177	1EaHK-eAI$Y%5%566]^^rT   c                     U /nU(       a  UR                  U5        U(       a  UR                  U5        U(       d  U(       a  [        U5      $ U $ r  )appendr  )r   rn   rr  rk  rs  outputss         rR   rz  $FluxLoraLoaderMixin._prepare_outputs  s:    ,NN6"NN8$"/?uW~SSrT   rc   )Fr   NNNFFr   r   )NNN)NN)NFF))rA   r   r   r   r   r   rH  r3  r   r}   r  r   r   r   rt   rF   r   r   rv   r   r2  r  r|   r   r   r   r   r   r   r   r   r   r   r  r  r  staticmethodr  rz  r   r   r   s   @rR   rf  rf    s    ,^<')(\% $y/2T#u||:K5L/Ly y  y| $(	d
/2T#u||:K5L/Ld
 Djd
 	d
L  
 
 
> 
  %3- 
c5<<	 3- 3-j  7
 7
 7
r 
 NR?C $"&#'*.+/<
bkk)<
 "&c588??U\\+I&I!J<
 #'sEHHOO';"<	<

 <
 <
  <
 !<
 <
@ "/!*.
I
 
 	

 Cy4'
 
@ 4A.2Q =d3i = ="BH  y+XX__y+ 
y+ y+v . .`  *.&*_ _&_ !$_ 
	_ _4 T TrT   rf  c                   2   \ rS rSrSS/r\r\r\	     SS\
4S jj5       r\	       SS\
4S jj5       r\	      SS\\R                  -  S	\\\R$                  R&                  4   S
\\\R$                  R&                  4   S\
S\S\S\
4S jj5       rSrg)AmusedLoraLoaderMixini  r.   r,   NrZ   c	           
          U(       a  [        SS5      (       d  [        S5      e[        R                  SU R                   S35        UR                  UUUUUUUS9  gr  r:  r  s	            rR   r2  0AmusedLoraLoaderMixin.load_lora_into_transformer  sl      _T8%D%D D 
 	hs334A67%%)%/ 	& 	
rT   c                 >    [        UUUUUU R                  UU
UUU	S9  gr   r   r   s              rR   r|   1AmusedLoraLoaderMixin.load_lora_into_text_encoder 	  r  rT   r   r   r?  r   r   r   r   c           	      &   0 nU(       d  U(       d  [        S5      eU(       a*  UR                  U R                  X0R                  5      5        U(       a*  UR                  U R                  X R                  5      5        U R                  UUUUUUS9  g)un  
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`.
r[  )r   r   r   r   r   r   N)rB   r  pack_weightsr3  r}   write_lora_layers)	r   r   r   r?  r   r   r   r   r   s	            rR   r   'AmusedLoraLoaderMixin.save_lora_weights;	  s    B 
'+Cuvv"c../FH\H\]^#c../GI^I^_` 	!)+#'1 	 	
rT   rc   r   r   )NNTNNT)rA   r   r   r   r   rH  r3  r   r}   r   r   r2  r|   r   r   r   rt   rF   r   r   r   r   r   rc   rT   rR   r#  r#    s   +^<') 
 
 
>  7
 7
 7
r  @D>B $"&#'3
bkk)3
 #'sEHHOO';"<3
 "&c588??&:!;	3

 3
 3
  3
 !3
 3
rT   r#  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )CogVideoXLoraLoaderMixinir	  z[
Load LoRA layers into [`CogVideoXTransformer3DModel`]. Specific to [`CogVideoXPipeline`].
r.   rX   c                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnnU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   ;CogVideoXLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>	  r   rj   r   r   r(  r)  s                         rR   rv   (CogVideoXLoraLoaderMixin.lora_state_dictz	  rO  r+  NFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   =CogVideoXLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>	  ri   rj   rk   r.   r1  NrS  r4  s	            rR   r   *CogVideoXLoraLoaderMixin.load_lora_weights	  rU  rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  3CogVideoXLoraLoaderMixin.load_lora_into_transformer	  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   *CogVideoXLoraLoaderMixin.save_lora_weights	  sd     "0G,,-2S#../uvv)#'+#'1 	 	
rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   "CogVideoXLoraLoaderMixin.fuse_lora
  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   $CogVideoXLoraLoaderMixin.unfuse_lora/
  r   rT   rc   r   r   rc  rd  r   s   @rR   r-  r-  r	  s    ,_'2/2T#u||:K5L/L2  2n $(	'
/2T#u||:K5L/L'
 Dj'
 	'
R  
 
 
<  NR $"&#'9=
bkk)
 "&c588??U\\+I&I!J
 	

 
  
 !
 ,0$;
 
F "/!*.
I
 
 	

 Cy4'
 
& 4A/ =d3i = =rT   r-  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )Mochi1LoraLoaderMixini6
  zS
Load LoRA layers into [`MochiTransformer3DModel`]. Specific to [`MochiPipeline`].
r.   rX   c                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnnU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   8Mochi1LoraLoaderMixin.lora_state_dict.<locals>.<genexpr>l
  r   rj   r   r   r(  r)  s                         rR   rv   %Mochi1LoraLoaderMixin.lora_state_dict>
  rO  r+  NFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   :Mochi1LoraLoaderMixin.load_lora_weights.<locals>.<genexpr>
  ri   rj   rk   r.   r1  NrS  r4  s	            rR   r   'Mochi1LoraLoaderMixin.load_lora_weightsv
  rU  rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  0Mochi1LoraLoaderMixin.load_lora_into_transformer
  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   'Mochi1LoraLoaderMixin.save_lora_weights
  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   Mochi1LoraLoaderMixin.fuse_lora
  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   !Mochi1LoraLoaderMixin.unfuse_lora
  r   rT   rc   r   r   rc  rd  r   s   @rR   r?  r?  6
      ,_'2/2T#u||:K5L/L2  2p $(	'
/2T#u||:K5L/L'
 Dj'
 	'
R  
 
 
< 
 NR $"&#'9=
bkk)
 "&c588??U\\+I&I!J
 	

 
  
 !
 ,0$;
 
H "/!*.
I
 
 	

 Cy4'
 
( 4A/ =d3i = =rT   r?  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )LTXVideoLoraLoaderMixini
  zT
Load LoRA layers into [`LTXVideoTransformer3DModel`]. Specific to [`LTXPipeline`].
r.   rX   c                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnn[        S U 5       5      nU(       a  [        U5      nU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   :LTXVideoLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>3  r   rj   r   r   c              3   B   #    U  H  oR                  S 5      v   M     g7fdiffusion_model.Nr   r   s     rR   rg   rU  9       %[PZ1ll3E&F&FPZ   )rq   r   r   r   r   r   r"   r   rX   r   r   r   r   r   r   r   r   r   r   r_   r   r   r   rn   r   r   r   r   is_non_diffusers_formatr   s                          rR   rv   'LTXVideoLoraLoaderMixin.lora_state_dict  s    JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H !$#Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JW"%%[PZ%["["FzRJ(<z8$
 CM
 X   E-.E-NFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   <LTXVideoLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>\  ri   rj   rk   r.   r1  NrS  r4  s	            rR   r   )LTXVideoLoraLoaderMixin.load_lora_weightsA  rU  rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  2LTXVideoLoraLoaderMixin.load_lora_into_transformerj  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   )LTXVideoLoraLoaderMixin.save_lora_weights  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   !LTXVideoLoraLoaderMixin.fuse_lora  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   #LTXVideoLoraLoaderMixin.unfuse_lora  r   rT   rc   r   r   rc  rd  r   s   @rR   rR  rR  
      ,_'6/2T#u||:K5L/L6  6x $(	'
/2T#u||:K5L/L'
 Dj'
 	'
R  
 
 
< 
 NR $"&#'9=
bkk)
 "&c588??U\\+I&I!J
 	

 
  
 !
 ,0$;
 
H "/!*.
I
 
 	

 Cy4'
 
( 4A/ =d3i = =rT   rR  c                     ^  \ rS rSrSrSS/r\r\r	\
\S\\\\R                  4   -  4S j5       5       r  SS\\\\R                  4   -  S	\S-  S
\4S jjr\
      SS
\S\4S jj5       r\
      S S\\R*                  -  S\\\R,                  R.                  \R                  -  4   S\S\S\S\S\S-  4S jj5       rS/SSS4S\\   S\S\S\\   S-  4U 4S jjjrS/4S\\   4U 4S jjjrSrU =r$ )!LTX2LoraLoaderMixini  zV
Load LoRA layers into [`LTX2VideoTransformer3DModel`]. Specific to [`LTX2Pipeline`].
r.   r/   rX   c                 V   UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnnUn[        S U 5       5      n[        S U 5       5      nU(       a  [        U5      nU(       a  [        US5      nUR                  U5        U(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   6LTX2LoraLoaderMixin.lora_state_dict.<locals>.<genexpr>  r   rj   r   r   c              3   B   #    U  H  oR                  S 5      v   M     g7frW  r   r   s     rR   rg   rp    rY  rZ  c              3   B   #    U  H  oR                  S 5      v   M     g7f)ztext_embedding_projection.Nr   r   s     rR   rg   rp    s     [PZ1LL)EFFPZrZ  text_embedding_projection)rq   r   r   r   r   r   r!   r  )r   rX   r   r   r   r   r   r   r   r   r   r   r_   r   r   r   rn   r   r   r   r   final_state_dictr\  has_connectorconnectors_state_dictr   s                             rR   rv   #LTX2LoraLoaderMixin.lora_state_dict  s    JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H !$#Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JW%"%%[PZ%["[[PZ[["LZX$Q7%! ##$9:.B*
 IY
 Xs   F%.F%NFrY   rZ   c                    [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eUR                  5        V	V
s0 s H,  u  pU	R                  U R                   S
35      (       d  M*  X_M.     nn	n
UR                  5        V	V
s0 s H,  u  pU	R                  U R                   S
35      (       d  M*  X_M.     nn	n
U R                  U[!        U S5      (       d  [#        X R                  5      OU R$                  UUU UUS9  U(       aQ  U R                  U[!        U S5      (       d  [#        X R                  5      OU R&                  UUU UUU R                  S9  ggs  sn
n	f s  sn
n	f )r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   8LTX2LoraLoaderMixin.load_lora_weights.<locals>.<genexpr>-  ri   rj   rk   r   r.   r1  r/   )r.   rY   rn   ro   r]   rZ   r   N)r   rB   rq   rr   r   rs   rt   ru   rv   rw   rx   r   r   r3  connectors_namer2  rE   rz   r.   r/   )r   rX   rY   rZ   r   r]   r   rn   r   r   r   transformer_peft_state_dictconnectors_peft_state_dicts                rR   r   %LTX2LoraLoaderMixin.load_lora_weights  s     HII"JJ':<[\h!?!? D 
 ;TBB4Y4^4^4`1 *.%&#334Yd]cd
K9JKK rss (--/'
/TQ1<<4CXCXBYYZ@[3\DAD/ 	$ '
 8B7G7G7I%v7ItqQ\\]a]q]q\rrsZtMudad7I"%v'''DKDR_D`D`&;&;<fjfvfv%/ 	( 	
 &++*t\22 $D*>*>?__)!"3++ ,  &'
 &ws   )G1G1)G7
G7r   c	                     U(       a  [        SS5      (       a  [        S5      e[        R                  SU S35        UR	                  USUUUUUUS9  g)r8  r-  r.  r^   r   r   N)rm   rY   rn   ro   r]   rZ   r   )r   rB   r   r   r   )	r   r   r.   rY   ro   r]   rZ   rn   r   s	            rR   r2  .LTX2LoraLoaderMixin.load_lora_into_transformerL  sh     h!?!? D 
 	hvha()%%%/ 	& 		
rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   %LTX2LoraLoaderMixin.save_lora_weightsm  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   LTX2LoraLoaderMixin.fuse_lora  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   LTX2LoraLoaderMixin.unfuse_lora  r   rT   rc   r   )NNFFNr.   rc  ) rA   r   r   r   r   r   rH  r3  LTX2_CONNECTOR_NAMEr{  r   r   r   rt   rF   r   rv   r   r   r2  r   r   r   r   r   r   r   r   r   r   r   r   r   s   @rR   rm  rm    s    ,\:')O</2T#u||:K5L/L<  <B $(	8/2T#u||:K5L/L8 Dj8 	8t 
 #
 
 
 
@ 
 NR $"&#'9=
bkk)
 "&c588??U\\+I&I!J
 	

 
  
 !
 ,0$;
 
H "/!*.
I
 
 	

 Cy4'
 
( 4A/ =d3i = =rT   rm  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )SanaLoraLoaderMixini  zQ
Load LoRA layers into [`SanaTransformer2DModel`]. Specific to [`SanaPipeline`].
r.   rX   c                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnnU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   6SanaLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>  r   rj   r   r   r(  r)  s                         rR   rv   #SanaLoraLoaderMixin.lora_state_dict  rO  r+  NFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   8SanaLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>  ri   rj   rk   r.   r1  NrS  r4  s	            rR   r   %SanaLoraLoaderMixin.load_lora_weights  rU  rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  .SanaLoraLoaderMixin.load_lora_into_transformer  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   %SanaLoraLoaderMixin.save_lora_weights5  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   SanaLoraLoaderMixin.fuse_loraY  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   SanaLoraLoaderMixin.unfuse_loram  r   rT   rc   r   r   rc  rd  r   s   @rR   r  r    rP  rT   r  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )HeliosLoraLoaderMixinit  zs
Load LoRA layers into [`HeliosTransformer3DModel`]. Specific to [`HeliosPipeline`] and [`HeliosPyramidPipeline`].
r.   rX   c                 (   UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      (       a  [        U5      nO"[        S U 5       5      (       a  [	        U5      n[        S U 5       5      nU(       aE  Sn[
        R                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnnU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   B   #    U  H  oR                  S 5      v   M     g7frW  r   r   s     rR   rg   8HeliosLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>       DA||.//rZ  c              3   B   #    U  H  oR                  S 5      v   M     g7fr   Nr   r   s     rR   rg   r         @Zl++ZrZ  c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   r    r   rj   r   r   rq   r   r   r%   r   r   r   r   r)  s                         rR   rv   %HeliosLoraLoaderMixin.lora_state_dict|      JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H DDDDEjQJ@Z@@@>zJJ ##Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JW(<z8$
 CM
 X   "F3FNFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   :HeliosLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>  ri   rj   rk   r.   r1  NrS  r4  s	            rR   r   'HeliosLoraLoaderMixin.load_lora_weights  s     HII"JJ':<[\h!?!? D 
 ;TBB4Y4^4^4`1 *.%&#334Yd]cd
K9JKK rss''DKDR_D`D`&;&;<fjfvfv%/ 	( 	
rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  0HeliosLoraLoaderMixin.load_lora_into_transformer  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   'HeliosLoraLoaderMixin.save_lora_weights  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   HeliosLoraLoaderMixin.fuse_lora"  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   !HeliosLoraLoaderMixin.unfuse_lora6  r   rT   rc   r   r   rc  rd  r   s   @rR   r  r  t  s    ,_'6/2T#u||:K5L/L6  6v $(	&
/2T#u||:K5L/L&
 Dj&
 	&
P  
 
 
< 
 NR $"&#'9=
bkk)
 "&c588??U\\+I&I!J
 	

 
  
 !
 ,0$;
 
H "/!*.
I
 
 	

 Cy4'
 
( 4A/ =d3i = =rT   r  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )HunyuanVideoLoraLoaderMixini=  za
Load LoRA layers into [`HunyuanVideoTransformer3DModel`]. Specific to [`HunyuanVideoPipeline`].
r.   rX   c                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnn[        S U 5       5      nU(       a  [        U5      nU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   >HunyuanVideoLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>r  r   rj   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7f)img_attn_qkvNrc   r   s     rR   rg   r  x  s     'PZ!(;Zrj   )rq   r   r   r   r   r   r   )r   rX   r   r   r   r   r   r   r   r   r   r   r_   r   r   r   rn   r   r   r   r   is_original_hunyuan_videor   s                          rR   rv   +HunyuanVideoLoraLoaderMixin.lora_state_dictE  s    JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H !$#Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JW$''PZ'P$P!$A*MJ(<z8$
 CM
 Xr^  NFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   @HunyuanVideoLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>  ri   rj   rk   r.   r1  NrS  r4  s	            rR   r   -HunyuanVideoLoraLoaderMixin.load_lora_weights  rU  rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  6HunyuanVideoLoraLoaderMixin.load_lora_into_transformer  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   -HunyuanVideoLoraLoaderMixin.save_lora_weights  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   %HunyuanVideoLoraLoaderMixin.fuse_lora  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   'HunyuanVideoLoraLoaderMixin.unfuse_lora  r   rT   rc   r   r   rc  rd  r   s   @rR   r  r  =  rk  rT   r  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )Lumina2LoraLoaderMixini  z_
Load LoRA layers into [`Lumina2Transformer2DModel`]. Specific to [`Lumina2Text2ImgPipeline`].
r.   rX   c                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnn[        S U 5       5      nU(       a  [        U5      nU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   9Lumina2LoraLoaderMixin.lora_state_dict.<locals>.<genexpr>=  r   rj   r   r   c              3   B   #    U  H  oR                  S 5      v   M     g7frW  r   r   s     rR   rg   r  D       QjLL);<<jrZ  )rq   r   r   r   r   r   r#   )r   rX   r   r   r   r   r   r   r   r   r   r   r_   r   r   r   rn   r   r   r   r   non_diffusersr   s                          rR   rv   &Lumina2LoraLoaderMixin.lora_state_dict  s    JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H !$#Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JW QjQQI*UJ(<z8$
 CM
 Xr^  NFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   ;Lumina2LoraLoaderMixin.load_lora_weights.<locals>.<genexpr>g  ri   rj   rk   r.   r1  NrS  r4  s	            rR   r   (Lumina2LoraLoaderMixin.load_lora_weightsL  rU  rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  1Lumina2LoraLoaderMixin.load_lora_into_transformeru  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   (Lumina2LoraLoaderMixin.save_lora_weights  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r    Lumina2LoraLoaderMixin.fuse_lora  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   "Lumina2LoraLoaderMixin.unfuse_lora  r   rT   rc   r   r   rc  rd  r   s   @rR   r  r    s    ,_'7/2T#u||:K5L/L7  7z $(	'
/2T#u||:K5L/L'
 Dj'
 	'
R  
 
 
< 
 NR $"&#'9=
bkk)
 "&c588??U\\+I&I!J
 	

 
  
 !
 ,0$;
 
H "/!*.
I
 
 	

 Cy4'
 
( 4A/ =d3i = =rT   r  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )KandinskyLoraLoaderMixini  z9
Load LoRA layers into [`Kandinsky5Transformer3DModel`],
r.   rX   c                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnnU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   ;KandinskyLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>
  r   rj   r   r   r(  r)  s                         rR   rv   (KandinskyLoraLoaderMixin.lora_state_dict  rO  r+  NFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   =KandinskyLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>/  ri   rj   rk   r.   r1  NrS  r4  s	            rR   r   *KandinskyLoraLoaderMixin.load_lora_weights  rU  rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  3KandinskyLoraLoaderMixin.load_lora_into_transformer=  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   *KandinskyLoraLoaderMixin.save_lora_weights]  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   "KandinskyLoraLoaderMixin.fuse_lora  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   $KandinskyLoraLoaderMixin.unfuse_lora  r   rT   rc   r   r   rc  rd  r   s   @rR   r  r    rP  rT   r  c                   .  ^  \ rS rSrSrSS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r\S\R                  R                   4S j5       r  SS\
\\
\R                  4   -  S
\
S-  S\4S jjr\     SS\4S jj5       r\      S S\
\R,                  -  S\\
\R                  R                   \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SS	S4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )!WanLoraLoaderMixini  zo
Load LoRA layers into [`WanTransformer3DModel`]. Specific to [`WanPipeline`] and `[WanImageToVideoPipeline`].
r.   transformer_2rX   c                 (   UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      (       a  [        U5      nO"[        S U 5       5      (       a  [	        U5      n[        S U 5       5      nU(       aE  Sn[
        R                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnnU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   B   #    U  H  oR                  S 5      v   M     g7frW  r   r   s     rR   rg   5WanLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>  r  rZ  c              3   B   #    U  H  oR                  S 5      v   M     g7fr  r   r   s     rR   rg   r    r  rZ  c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   r    r   rj   r   r   r  r)  s                         rR   rv   "WanLoraLoaderMixin.lora_state_dict  r  r  c           	      6   UR                   R                  c  U$ UR                  n[        S U 5       5      (       GaV  [	        U Vs1 s H1  nSU;   d  M  UR                  S5      S   R                  S5      S   iM3     sn5      n[        S U 5       5      =(       a    [        S U 5       5      n[        S U 5       5      nU(       a  U$ [        U5       H  n[        S	S
/SS/5       H  u  pSU S3nSU S3nX;  d  X;  a  M  [        R                  " USU S3   US9USU SU
 S3'   [        R                  " USU S3   US9USU SU
 S3'   SU S3nU(       d  Mx  X;   d  M  X-   n[        R                  " UUS9USU SU
 S3'   M     M     U$ s  snf )Nc              3   B   #    U  H  oR                  S 5      v   M     g7ftransformer.blocks.Nr   r   s     rR   rg   DWanLoraLoaderMixin._maybe_expand_t2v_lora_for_i2v.<locals>.<genexpr>       GJq||122JrZ  blocks.r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7f
add_k_projNrc   r   s     rR   rg   r         DAla/rj   c              3   ,   #    U  H
  nS U;   v   M     g7f
add_v_projNrc   r   s     rR   rg   r         Lshrcd\]^M^hrrj   c              3   ,   #    U  H
  nS U;   v   M     g7f.lora_B.biasNrc   r   s     rR   rg   r         C
1>Q.
rj   k_imgv_imgr  r  r  .attn2.to_k.lora_A.weight.attn2.to_k.lora_B.weightr  .attn2.r  r  .attn2.to_k.lora_B.biasr
  
r  	image_dimrC   r   r  r  rangeziprF   r  r   r.   r   target_devicer   
num_blocksis_i2v_lorahas_biasiocref_key_lora_Aref_key_lora_Bref_key_lora_B_biasref_lora_B_bias_tensors                  rR   _maybe_expand_t2v_lora_for_i2v1WanLoraLoaderMixin._maybe_expand_t2v_lora_for_i2v  s    ''/#**GJGGG:h:aYbfgYgAaggi0399#>qA:hiJDDDsLshrLsIsKC
CCH!!:&' 2\<4PQDA':1#=V%WN':1#=V%WN%7>;[ TYTdTd"%8;T#UV_lUJ!4QCwqcPQ UZTdTd"%8;T#UV_lUJ!4QCwqcPQ -@sBY*Z'x$7$E1;1P.V[VfVf2#0W
%871#\#RS+ R '6 E i   
F*FNFrY   rZ   c           
      f   [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pgU R                  [        U S5      (       d  [        X R                  5      OU R                  US	9n[        S
 UR                  5        5       5      nU(       d  [        S5      eUR                  SS5      n	U	(       aS  [        U S5      (       d"  [!        S[#        U 5      R$                   S35      eU R'                  UU R(                  UUU UUS9  gU R'                  U[        U S5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   r.   r.   r   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   7WanLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>-  ri   rj   rk   load_into_transformer_2Fr  '' object has no attribute transformer_2Note that Wan2.1 models do not have a transformer_2 component.Ensure the model has a transformer_2 component before setting load_into_transformer_2=True.r1  Nr   rB   rq   rr   r   rs   rt   ru   rv   r"  rE   rz   r3  r.   rw   rx   AttributeErrorrD   rA   r2  r  
r   rX   rY   rZ   r   r]   r   rn   r   r)  s
             rR   r   $WanLoraLoaderMixin.load_lora_weights       HII"JJ':<[\h!?!? D 
 ;TBB4Y4^4^4`1 *.%&#334Yd]cd
88DKDR_D`D`&;&;<fjfvfv! 9 

  K9JKK rss"(**-F"N"411$T
++, -r r 
 ++ ..)!"3 ,  ++t]33 $D*?*?@%%)!"3 , 
rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  -WanLoraLoaderMixin.load_lora_into_transformerO  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   $WanLoraLoaderMixin.save_lora_weightso  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   WanLoraLoaderMixin.fuse_lora  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   WanLoraLoaderMixin.unfuse_lora  r   rT   rc   r   r   rc  rA   r   r   r   r   r   rH  r3  r   r   r   rt   rF   r   rv   r   r   r"  r   r   r2  r   r   r   r   r   r   r   r   r   r   r   s   @rR   r  r    s    ,_='6/2T#u||:K5L/L6  6p -XX__- -d $(	?/2T#u||:K5L/L? Dj? 	?B  
 
 
< 
 NR $"&#'9=
bkk)
 "&c588??U\\+I&I!J
 	

 
  
 !
 ,0$;
 
H "/!*.
I
 
 	

 Cy4'
 
( 4A/ =d3i = =rT   r  c                   ,  ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r\S\R                  R                   4S j5       r  SS\
\\
\R                  4   -  S	\
S-  S
\4S jjr\     SS
\4S jj5       r\      SS\
\R,                  -  S\\
\R                  R                   \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ ) SkyReelsV2LoraLoaderMixini  z9
Load LoRA layers into [`SkyReelsV2Transformer3DModel`].
r.   rX   c                 (   UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      (       a  [        U5      nO"[        S U 5       5      (       a  [	        U5      n[        S U 5       5      nU(       aE  Sn[
        R                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnnU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   B   #    U  H  oR                  S 5      v   M     g7frW  r   r   s     rR   rg   <SkyReelsV2LoraLoaderMixin.lora_state_dict.<locals>.<genexpr>  r  rZ  c              3   B   #    U  H  oR                  S 5      v   M     g7fr  r   r   s     rR   rg   r>    r  rZ  c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   r>    r   rj   r   r   r  r)  s                         rR   rv   )SkyReelsV2LoraLoaderMixin.lora_state_dict  s    JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H DDDDEjQJ@Z@@@>zJJ ##Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JW(<z8$
 CM
 Xr  c           	      6   UR                   R                  c  U$ UR                  n[        S U 5       5      (       GaV  [	        U Vs1 s H1  nSU;   d  M  UR                  S5      S   R                  S5      S   iM3     sn5      n[        S U 5       5      =(       a    [        S U 5       5      n[        S U 5       5      nU(       a  U$ [        U5       H  n[        S	S
/SS/5       H  u  pSU S3nSU S3nX;  d  X;  a  M  [        R                  " USU S3   US9USU SU
 S3'   [        R                  " USU S3   US9USU SU
 S3'   SU S3nU(       d  Mx  X;   d  M  X-   n[        R                  " UUS9USU SU
 S3'   M     M     U$ s  snf )Nc              3   B   #    U  H  oR                  S 5      v   M     g7fr  r   r   s     rR   rg   KSkyReelsV2LoraLoaderMixin._maybe_expand_t2v_lora_for_i2v.<locals>.<genexpr>  r  rZ  r  r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr  rc   r   s     rR   rg   rD    r  rj   c              3   ,   #    U  H
  nS U;   v   M     g7fr  rc   r   s     rR   rg   rD    r  rj   c              3   ,   #    U  H
  nS U;   v   M     g7fr	  rc   r   s     rR   rg   rD     r  rj   r  r  r  r  r  r  r  r  r  r  r  r  r
  r  r  s                  rR   r"  8SkyReelsV2LoraLoaderMixin._maybe_expand_t2v_lora_for_i2v  s    ''/#**GJGGG:h:aYbfgYgAaggi0399#>qA:hiJDDDsLshrLsIsKC
CCH!!:&' 2\<4PQDA':1#=V%WN':1#=V%WN%7>;[ TYTdTd"%8;T#UV_lUJ!4QCwqcPQ UZTdTd"%8;T#UV_lUJ!4QCwqcPQ -@sBY*Z'x$7$E1;1P.V[VfVf2#0W
%871#\#RS+ R '6 E ir$  NFrY   rZ   c           
      f   [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pgU R                  [        U S5      (       d  [        X R                  5      OU R                  US	9n[        S
 UR                  5        5       5      nU(       d  [        S5      eUR                  SS5      n	U	(       aS  [        U S5      (       d"  [!        S[#        U 5      R$                   S35      eU R'                  UU R(                  UUU UUS9  gU R'                  U[        U S5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   r.   r&  c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   >SkyReelsV2LoraLoaderMixin.load_lora_weights.<locals>.<genexpr>B  ri   rj   rk   r)  Fr  r*  r+  r1  Nr,  r.  s
             rR   r   +SkyReelsV2LoraLoaderMixin.load_lora_weights#  r0  rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  4SkyReelsV2LoraLoaderMixin.load_lora_into_transformerd  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   +SkyReelsV2LoraLoaderMixin.save_lora_weights  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   #SkyReelsV2LoraLoaderMixin.fuse_lora  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   %SkyReelsV2LoraLoaderMixin.unfuse_lora  r   rT   rc   r   r   rc  r9  r   s   @rR   r;  r;    s    ,_'6/2T#u||:K5L/L6  6p -XX__- -f $(	?/2T#u||:K5L/L? Dj? 	?B  
 
 
< 
 NR $"&#'9=
bkk)
 "&c588??U\\+I&I!J
 	

 
  
 !
 ,0$;
 
H "/!*.
I
 
 	

 Cy4'
 
( 4A/ =d3i = =rT   r;  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )CogView4LoraLoaderMixini  zT
Load LoRA layers into [`WanTransformer3DModel`]. Specific to [`CogView4Pipeline`].
r.   rX   c                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnnU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   :CogView4LoraLoaderMixin.lora_state_dict.<locals>.<genexpr>  r   rj   r   r   r(  r)  s                         rR   rv   'CogView4LoraLoaderMixin.lora_state_dict  rO  r+  NFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   <CogView4LoraLoaderMixin.load_lora_weights.<locals>.<genexpr>  ri   rj   rk   r.   r1  NrS  r4  s	            rR   r   )CogView4LoraLoaderMixin.load_lora_weights  rU  rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  2CogView4LoraLoaderMixin.load_lora_into_transformer,  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   )CogView4LoraLoaderMixin.save_lora_weightsL  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   !CogView4LoraLoaderMixin.fuse_lorap  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   #CogView4LoraLoaderMixin.unfuse_lora  r   rT   rc   r   r   rc  rd  r   s   @rR   rV  rV    rP  rT   rV  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )HiDreamImageLoraLoaderMixini  za
Load LoRA layers into [`HiDreamImageTransformer2DModel`]. Specific to [`HiDreamImagePipeline`].
r.   rX   c                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnn[        S U 5       5      nU(       a  [        U5      nU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   >HiDreamImageLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>  r   rj   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7f)diffusion_modelNrc   r   s     rR   rg   rk    s     %Qj&71&<jrj   )rq   r   r   r   r   r   r   r[  s                          rR   rv   +HiDreamImageLoraLoaderMixin.lora_state_dict  s    JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H !$#Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JW"%%Qj%Q"Q"I*UJ(<z8$
 CM
 Xr^  NFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   @HiDreamImageLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>  ri   rj   rk   r.   r1  NrS  r4  s	            rR   r   -HiDreamImageLoraLoaderMixin.load_lora_weights  rU  rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  6HiDreamImageLoraLoaderMixin.load_lora_into_transformer  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   -HiDreamImageLoraLoaderMixin.save_lora_weights  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   %HiDreamImageLoraLoaderMixin.fuse_lora;  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   'HiDreamImageLoraLoaderMixin.unfuse_loraO  r   rT   rc   r   r   rc  rd  r   s   @rR   rh  rh    rk  rT   rh  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )QwenImageLoraLoaderMixiniV  z[
Load LoRA layers into [`QwenImageTransformer2DModel`]. Specific to [`QwenImagePipeline`].
r.   rX   c                 |   UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnn[        S U 5       5      n[        S U 5       5      n[        S U 5       5      n[        S U 5       5      nU(       d  U(       d  U(       d  U(       a  [        U5      nU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   ;QwenImageLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>  r   rj   r   r   c              3   B   #    U  H  oR                  S 5      v   M     g7fz.alphaNr  r   s     rR   rg   r         HZzz(33ZrZ  c              3   B   #    U  H  oR                  S 5      v   M     g7fr  r   r   s     rR   rg   r         K
1LL66
rZ  c              3   B   #    U  H  oR                  S 5      v   M     g7frW  r   r   s     rR   rg   r         !WJq,,/A"B"BJrZ  c              3   ,   #    U  H
  nS U;   v   M     g7fzdefault.Nrc   r   s     rR   rg   r         >:a*/:rj   )rq   r   r   r   r   r   r$   r   rX   r   r   r   r   r   r   r   r   r   r   r_   r   r   r   rn   r   r   r   r   has_alphas_in_sdhas_lora_unethas_diffusion_modelhas_defaultr   s                             rR   rv   (QwenImageLoraLoaderMixin.lora_state_dict^  s    JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H !$#Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JWHZHHK
KK!!WJ!WW>:>>}0C{FzRJ(<z8$
 CM
 X   F8.F8NFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   =QwenImageLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>  ri   rj   rk   r.   r1  NrS  r4  s	            rR   r   *QwenImageLoraLoaderMixin.load_lora_weights  rU  rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  3QwenImageLoraLoaderMixin.load_lora_into_transformer  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   *QwenImageLoraLoaderMixin.save_lora_weights  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   "QwenImageLoraLoaderMixin.fuse_lora	  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   $QwenImageLoraLoaderMixin.unfuse_lora  r   rT   rc   r   r   rc  rd  r   s   @rR   r|  r|  V      ,_'9/2T#u||:K5L/L9  9~ $(	'
/2T#u||:K5L/L'
 Dj'
 	'
R  
 
 
< 
 NR $"&#'9=
bkk)
 "&c588??U\\+I&I!J
 	

 
  
 !
 ,0$;
 
H "/!*.
I
 
 	

 Cy4'
 
( 4A/ =d3i = =rT   r|  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )ZImageLoraLoaderMixini$  zU
Load LoRA layers into [`ZImageTransformer2DModel`]. Specific to [`ZImagePipeline`].
r.   rX   c                 |   UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnn[        S U 5       5      n[        S U 5       5      n[        S U 5       5      n[        S U 5       5      nU(       d  U(       d  U(       d  U(       a  [        U5      nU(       a  UU4nU$ UnU$ s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   8ZImageLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>Y  r   rj   r   r   c              3   B   #    U  H  oR                  S 5      v   M     g7fr  r  r   s     rR   rg   r  _  r  rZ  c              3   B   #    U  H  oR                  S 5      v   M     g7fr  r   r   s     rR   rg   r  `  r  rZ  c              3   B   #    U  H  oR                  S 5      v   M     g7frW  r   r   s     rR   rg   r  a  r  rZ  c              3   ,   #    U  H
  nS U;   v   M     g7fr  rc   r   s     rR   rg   r  b  r  rj   )rq   r   r   r   r   r   r&   r  s                             rR   rv   %ZImageLoraLoaderMixin.lora_state_dict,  s    JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H !$#Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JWHZHHK
KK!!WJ!WW>:>>}0C{I*UJ(<z8$
 CM
 Xr  NFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   :ZImageLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>  ri   rj   rk   r.   r1  NrS  r4  s	            rR   r   'ZImageLoraLoaderMixin.load_lora_weightsj  rU  rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  0ZImageLoraLoaderMixin.load_lora_into_transformer  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   'ZImageLoraLoaderMixin.save_lora_weights  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   ZImageLoraLoaderMixin.fuse_lora  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   !ZImageLoraLoaderMixin.unfuse_lora  r   rT   rc   r   r   rc  rd  r   s   @rR   r  r  $  r  rT   r  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )Flux2LoraLoaderMixini  zS
Load LoRA layers into [`Flux2Transformer2DModel`]. Specific to [`Flux2Pipeline`].
r.   rX   c                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnn[        S U 5       5      nU(       a  [        U5      nU(       a  UU4nU$ UnU$ [        S U 5       5      nU(       a6  UR                  5        VVs0 s H  u  nnUR                  SS5      U_M     nnn[        S U 5       5      nU(       a  [        U5      nU(       a  UU4nU$ UnU$ s  snnf s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   7Flux2LoraLoaderMixin.lora_state_dict.<locals>.<genexpr>'  r   rj   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7frp  rc   r   s     rR   rg   r  -  rq  rj   c              3   B   #    U  H  oR                  S 5      v   M     g7fbase_model.model.Nr   r   s     rR   rg   r  4       S
1\\*=>>
rZ  r  rX  c              3   B   #    U  H  oR                  S 5      v   M     g7frW  r   r   s     rR   rg   r  8  r  rZ  )	rq   r   r   r   r   r   r   r  r   )r   rX   r   r   r   r   r   r   r   r   r   r   r_   r   r   r   rn   r   r   r   r   r~  r   is_peft_formatis_ai_toolkits                            rR   rv   $Flux2LoraLoaderMixin.lora_state_dict  s&    JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H !$#Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JWDDD?
KJ,@:x(CJ GQCJS
SS\f\l\l\no\nTXTUWX!))$79KLaO\nJoQjQQG
SJ(<z8$
 CM
% X ps   G1.G1 G7NFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   9Flux2LoraLoaderMixin.load_lora_weights.<locals>.<genexpr>[  ri   rj   rk   r.   r1  NrS  r4  s	            rR   r   &Flux2LoraLoaderMixin.load_lora_weights@  rU  rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  /Flux2LoraLoaderMixin.load_lora_into_transformeri  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   &Flux2LoraLoaderMixin.save_lora_weights  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   Flux2LoraLoaderMixin.fuse_lora  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r    Flux2LoraLoaderMixin.unfuse_lora  r   rT   rc   r   r   rc  rd  r   s   @rR   r  r    s    ,_'A/2T#u||:K5L/LA  AN $(	'
/2T#u||:K5L/L'
 Dj'
 	'
R  
 
 
< 
 NR $"&#'9=
bkk)
 "&c588??U\\+I&I!J
 	

 
  
 !
 ,0$;
 
H "/!*.
I
 
 	

 Cy4'
 
( 4A/ =d3i = =rT   r  c                     ^  \ rS rSrSrS/r\r\\	S\
\\
\R                  4   -  4S j5       5       r  SS\
\\
\R                  4   -  S\
S-  S	\4S
 jjr\     SS	\4S jj5       r\      SS\
\R&                  -  S\\
\R(                  R*                  \R                  -  4   S\S\
S\S\S\S-  4S jj5       rS/SSS4S\\
   S\S\S\\
   S-  4U 4S jjjrS/4S\\
   4U 4S jjjrSrU =r$ )ErnieImageLoraLoaderMixini  z]
Load LoRA layers into [`ErnieImageTransformer2DModel`]. Specific to [`ErnieImagePipeline`].
r.   rX   c                    UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      n	UR                  S
S5      n
UR                  SS5      nUR                  SS5      nSnUc  SnSnSSS.n[        UU
UUUUUUUU	UUS9u  nn[        S U 5       5      nU(       aE  Sn[        R	                  U5        UR                  5        VVs0 s H  u  nnSU;  d  M  UU_M     nnn[        S U 5       5      nU(       a6  UR                  5        VVs0 s H  u  nnUR                  SS5      U_M     nnn[        S U 5       5      nU(       a6  UR                  5        VVs0 s H  u  nnUR                  SS5      U_M     nnnU(       a  UU4nU$ UnU$ s  snnf s  snnf s  snnf )r%  r   Nr   Fr   r   r   r   r   r   r   r_   Tr   r   r   r   c              3   ,   #    U  H
  nS U;   v   M     g7fr   rc   r   s     rR   rg   <ErnieImageLoraLoaderMixin.lora_state_dict.<locals>.<genexpr>  r   rj   r   r   c              3   B   #    U  H  oR                  S 5      v   M     g7fr  r   r   s     rR   rg   r    r  rZ  r  rX  c              3   B   #    U  H  oR                  S 5      v   M     g7frW  r   r   s     rR   rg   r    s     ']R\Q5G(H(HR\rZ  ztransformer.)rq   r   r   r   r   r   r  )r   rX   r   r   r   r   r   r   r   r   r   r   r_   r   r   r   rn   r   r   r   r   r  is_diffusion_model_prefixr   s                           rR   rv   )ErnieImageLoraLoaderMixin.lora_state_dict  s!    JJ{D1	$4e<**Y-!::&8$?

7D)::j$/JJ{D1	jj5 **%6=%zz*@%H""OL#7iP
02W#+-)!% 

H !$#Jz#J J  {HNN8$+5+;+;+=W+=41aUVAV$!Q$+=JW S
SS\f\l\l\no\nTXTUWX!))$79KLaO\nJo
 %(']R\']$]!$WaWgWgWijWitqRS!))$6GJWiJj(<z8$
 CM
 X
 p ks   G'.G'$ G-3 G3NFrY   rZ   c           
         [         (       d  [        S5      eUR                  S[        5      nU(       a  [	        SS5      (       a  [        S5      e[        U[        5      (       a  UR                  5       nSUS'   U R                  " U40 UD6u  pg[        S UR                  5        5       5      nU(       d  [        S	5      eU R                  U[        U S
5      (       d  [        X R                  5      OU R                  UUU UUS9  g)r  r\   r]   r-  r.  r^   Tr_   c              3   ,   #    U  H
  nS U;   v   M     g7fra   rc   rd   s     rR   rg   >ErnieImageLoraLoaderMixin.load_lora_weights.<locals>.<genexpr>.  ri   rj   rk   r.   r1  NrS  r4  s	            rR   r   +ErnieImageLoraLoaderMixin.load_lora_weights  rU  rT   c           
          U(       a  [        SS5      (       a  [        S5      e[        R                  SU R                   S35        UR                  USUUUUUS9  gr7  r:  r;  s           rR   r2  4ErnieImageLoraLoaderMixin.load_lora_into_transformer<  rX  rT   r   r?  r   r   r   r   rB  c           
          0 n0 n	U(       a  X(U R                   '   XyU R                   '   U(       d  [        S5      eU R                  UUU	UUUUS9  grZ  rA  r\  s
             rR   r   +ErnieImageLoraLoaderMixin.save_lora_weights\  r^  rT   r   r   rp   r   r   c                 0   > [         TU ]  " SUUUUS.UD6  gr  r   r   s         rR   r   #ErnieImageLoraLoaderMixin.fuse_lora  r  rT   c                 *   > [         TU ]  " SSU0UD6  gr  r   r   s      rR   r   %ErnieImageLoraLoaderMixin.unfuse_lora  r   rT   rc   r   r   rc  rd  r   s   @rR   r  r    s    ,_'>/2T#u||:K5L/L>  >H $(	'
/2T#u||:K5L/L'
 Dj'
 	'
R  
 
 
< 
 NR $"&#'9=
bkk)
 "&c588??U\\+I&I!J
 	

 
  
 !
 ,0$;
 
H "/!*.
I
 
 	

 Cy4'
 
( 4A/ =d3i = =rT   r  c                   (   ^  \ rS rSrU 4S jrSrU =r$ )LoraLoaderMixini  c                 D   > Sn[        SSU5        [        TU ]  " U0 UD6  g )NzLoraLoaderMixin is deprecated and this will be removed in a future version. Please use `StableDiffusionLoraLoaderMixin`, instead.r  z1.0.0)r   r   __init__)r   argsr   deprecation_messager@   s       rR   r  LoraLoaderMixin.__init__  s,     b#W.AB$)&)rT   rc   )rA   r   r   r   r  r   r   r   s   @rR   r  r    s    * *rT   r  )Mr   typingr   rF   huggingface_hub.utilsr   utilsr   r   r   r	   r
   r   r   r   r   r   r   	lora_baser   r   r   r   r   r   lora_conversion_utilsr   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   rr   
get_loggerrA   r   r   r   rH  r  r  rS   rV   r   r#  rJ  rf  r#  r-  r?  rR  rm  r  r  r  r  r  r  r;  rV  rh  r|  r  r  r  r  rc   rT   rR   <module>r     s4   
   6        * #( D'""D(++%''#C22*.' 
		H	%" 	  " &2M%B ".bM=] M=`E=} E=Ph= h=V	E=m E=PET- ETTU
: U
pA=} A=HE=M E=PH=m H=V`=- `=FE=- E=PF=M F=RH=- H=VI=] I=XE=} E=PO= O=dR= R=jE=m E=PH=- H=VK=} K=\K=M K=\S== S=lP= P=f*4 *rT   