
    ^(j\                         d Z ddlZddlZddlZddlZddlmZ ddlZddl	Z	ddddddd	Z
dd
Zi fdZi fdZde	j                  dee   de	j                  fdZddZe	j&                  dfdZd Zy)a  
    This file is part of ComfyUI.
    Copyright (C) 2024 Comfy

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
    Nmlp_fc1mlp_fc2self_attn_k_projself_attn_q_projself_attn_v_projself_attn_out_proj)zmlp.fc1zmlp.fc2zself_attn.k_projzself_attn.q_projzself_attn.v_projzself_attn.out_projc           
      V   i }t               }|D ]V  }dj                  |      }d }|| j                         v r$| |   j                         }|j	                  |       dj                  |      }d }	|| j                         v r| |   }	|j	                  |       t
        j                  D ]=  }
|
j                  || ||	|      }|||||   <   |j                  |j                         ? dj                  |      }dj                  |      }| j                  |d       }| j                  |d       }|V|j	                  |       d|ff|||   <   |8|j	                  |       d|ff|dj                  ||   d t        d              <   dj                  |      }| j                  |d       }|d|ff|||   <   |j	                  |       d	j                  |      }| j                  |d       }|8d|ff|dj                  ||   d t        d              <   |j	                  |       d
j                  |      }| j                  |d       }|;d|ff|||   <   |j	                  |       Y |r>| j                         D ]+  }||vst        j                  dj                  |             - |S )Nz{}.alphaz{}.dora_scalez	{}.w_normz	{}.b_normdiffz{}.bias.weightz{}.diffz	{}.diff_bz{}.set_weightsetzlora key not loaded: {})r   formatkeysitemaddweight_adapteradaptersloadupdateloaded_keysgetlenloggingwarning)lorato_loadlog_missing
patch_dictr   x
alpha_namealphadora_scale_name
dora_scaleadapter_clsadapterw_norm_nameb_norm_namew_normb_norm	diff_namediff_weightdiff_bias_name	diff_biasset_weight_name
set_weights                         4/Users/danicosta/Desktop/Flux2/ComfyUI/comfy/lora.py	load_lorar0   %   s   J%K&&q)
$$))+EOOJ')003
diik)o.JOOO,)22K!&&q$z;OG")0
71:&""7#6#67 3 "((+!((++t,+t,OOK(&,vi%8Jwqz"!,NTW]V_M`
9++GAJ7GY,HIJ$$Q'	hhy$/"&,{n%=Jwqz"OOI&$++A.HH^T2	 JPS\R^I_Jy''
3CS^O(DEFOON+)003XXot4
!&+j]%;Jwqz"OOO,a d A# 9 @ @ CD      c           
      6   | j                         j                         }t               }|D ]  }|j                  d      s||dj	                  |d t        d              <   |j                  d      }|dkD  r5|j                  d      s$||dj	                  ||dz   t        d              <   |j                  |j                  d      d           d}d	}d	}t        d
      D ]  }	t        D ]|  }
dj	                  |	|
      }||v rS|j	                  |	t        |
         }|||<   dj	                  |	t        |
         }|||<   dj	                  |	|
      }|||<   dj	                  |	|
      }||v rU|j	                  |	t        |
         }|||<   dj	                  |	t        |
         }|||<   d}dj	                  |	|
      }|||<   dj	                  |	|
      }||v sd}|r7dj	                  |	t        |
         }|||<   dj	                  |	|
      }|||<   *dj	                  |	t        |
         }|||<   dj	                  |	|
      }|||<   dj	                  |	t        |
         }|||<     |D ]  }|j                  d      s|j                  d      r}|t        d      t        d        }d}|r|dz  }|r4|dz  }|dk(  r*||dj	                  ||j                  dd            <   |dz  }||dj	                  ||j                  dd            <   |j                  d      s|t        d      t        d        }dj	                  |j                  dd            }|||<    t        |      dk(  rdj	                  t        t        |                  }|D ]b  }|j                  d      s|j                  |      s'|t        |      t        d        }||dj	                  |j                  dd            <   d d}||v r
||d<   ||d<   d}||v r||d <   |S )!Nr   ztext_encoders.{}z.transformer.r   clip_   .z'lora_te_text_model_encoder_layers_{}_{}F    z9clip_h.transformer.text_model.encoder.layers.{}.{}.weightz(lora_te1_text_model_encoder_layers_{}_{}z,text_encoder.text_model.encoder.layers.{}.{}z9clip_l.transformer.text_model.encoder.layers.{}.{}.weightTz9clip_g.transformer.text_model.encoder.layers.{}.{}.weightz(lora_te2_text_model_encoder_layers_{}_{}z.text_encoder_2.text_model.encoder.layers.{}.{}z-lora_prior_te_text_model_encoder_layers_{}_{}zt5xxl.transformer.   zlora_te{}_{}_zhydit_clip.transformer.bert.zlora_te1_{}z{}.transformer.model.z
lora_te_{}z)clip_g.transformer.text_projection.weightlora_prior_te_text_projectionlora_te2_text_projectionz)clip_l.transformer.text_projection.weightlora_te1_text_projection)
state_dictr   r   endswithr   r   find
startswithr   splitrangeLORA_CLIP_MAPreplacenextiter)modelkey_mapsdk
prefix_setktptext_model_lora_keyclip_l_presentclip_g_presentbclora_keyl_keyt5_indexfull_prefixs                  r/   model_lora_keys_cliprU   a   sl   




!
!
#CJ::i FGG&--a0@#i..ABC(BAvall73PQ*11!BFC	N?2KLMNN1773<?+  DNN2YAKRRSTVWXACx.55aq9IJ$%!ELLQP]^_P`a$%!IPPQRTUV$%!KRRSTVWXACx.55aq9IJ$%!ELLQP]^_P`a$%!!%IPPQRTUV$%!KRRSTVWXACx!%!IPPQRTabcTdeH()GH%OVVWXZ[\H()GH%HOOPQS`abScdH()GH%MTTUVXYZH()GH%NUUVWYfghYijH()GH%E  J ::i ||01#23S^OD!MH!MH1}\] 5 5hcSV@W XY ATU--hc38OPQ<=#<=s9~oN(//c30GH$%!# & :!-44T$z:J5KLAzz)$<<,c+.I?ELMGL//c30GHI	  	4ACx34/0./*+3ACx./*+Nr1   c           
      *   | j                         }|j                         }|D ]  }|j                  d      s|j                  d      r^|t	        d      t	        d        j                  dd      }||dj                  |      <   ||dj                  |d t	        d              <   ||dj                  |      <    t        j                  j                  | j                  j                        }|D ]  }|j                  d      sdj                  ||         }|d t	        d        j                  dd      }||dj                  |      <   ||dj                  |      <   d	d
g}|D ]L  }	dj                  |	|d t	        d        j                  dd            }
|
j                  d      r|
d d }
|||
<   N  t        | t        j                  j                        rg|D ]b  }|j                  d      s|j                  d      s'|t	        d      t	        d        j                  dd      }||dj                  |      <   d t        | t        j                  j                        rt        j                  j!                  | j                  j                  d      }|D ]  }|j                  d      s||   }dj                  |d t	        d              }|||<   dj                  |d t	        d              }|||<   dj                  |d t	        d        j                  dd            }|||<   dj                  |d t	        d        j                  dd            }|||<    t        | t        j                  j"                        rvt        j                  j%                  | j                  j                  d      }|D ]<  }|j                  d      s||   }dj                  |d t	        d              }|||<   > t        | t        j                  j&                        rt        j                  j)                  | j                  j                  d      }|D ]  }|j                  d      s||   }dj                  |d t	        d              }|||<   dj                  |d t	        d              }|||<   dj                  |d t	        d              }|||<    t        | t        j                  j*                        rW|D ]R  }|j                  d      s|j                  d      s'|t	        d      t	        d        }||dj                  |      <   T t        | t        j                  j,                        rXt        j                  j/                  | j                  j                  d      }|D ]  }|j                  d      s||   }||dj                  |d t	        d              <   ||dj                  |d t	        d        j                  dd            <   ||dj                  |d t	        d        j                  dd            <   |||d t	        d        <    |D ]k  }| j                  j                  j1                  dd      }|j                  d      s;d|v s@|dd|dz  ff|dj                  |j                  dd            <   m t        | t        j                  j2                        rW|D ]R  }|j                  d      s|j                  d      s'|t	        d      t	        d        }||dj                  |      <   T t        | t        j                  j4                        r|D ]  }|j                  d      s|j                  d      s'|}|j                  dd      j                  dd      j                  d d!      }|j                  d"d#      j                  d$d%      }|j                  d&d'      }|t	        d      t	        d        }||dj                  |      <   ||dj                  |      <    t        | t        j                  j6                        r{|D ]v  }|j                  d      s|j                  d      s'|t	        d      t	        d        }||dj                  |j                  dd            <   ||dj                  |      <   x t        | t        j                  j8                        rW|D ]R  }|j                  d      s|j                  d      s'|t	        d      t	        d        }||dj                  |      <   T t        | t        j                  j:                        rW|D ]R  }|j                  d      s|j                  d      s'|t	        d      t	        d        }||dj                  |      <   T t        | t        j                  j<                        r|D ]  }|j                  d      s|j                  d      s'|t	        d      t	        d        }||dj                  |      <   ||dj                  |      <   ||dj                  |j                  dd            <    t        | t        j                  j>                        rt        j                  jA                  | j                  j                  d      }|D ]y  }|j                  d      s||   }|d t	        d        }||dj                  |      <   ||dj                  |      <   ||dj                  |j                  dd            <   |||<   { t        | t        j                  jB                        rk|D ]f  }|j                  d      s|j                  d      s'|t	        d      t	        d        }||dj                  |      <   ||dj                  |      <   h t        | t        j                  jD                        r{|D ]v  }|j                  d(      s|j                  d      s'|t	        d(      t	        d        }||dj                  |      <   ||dj                  |j                  dd            <   x t        | t        j                  jF                        rW|D ]R  }|j                  d      s|j                  d      s'|t	        d      t	        d        }||dj                  |      <   T |S ))Nzdiffusion_model.r   r5   r8   zlora_unet_{}z{}zdiffusion_model.{}z
lycoris_{} zunet.z{}{}z.to_z.processor.to_z	.to_out.0zlora_prior_unet_{})output_prefixztransformer.{}zbase_model.model.{}zlora_transformer_{}zunet.base_model.model.{}hidden_sizer   z	.linear1.   z.linear1.weightz.linear1_qkvz	_mod.lin.z_mod.linear.z
_attn.qkv.z
_attn_qkv.z_attn.proj.z_attn_proj.zmlp.0.zmlp.fc1.zmlp.2.zmlp.fc2.z.modulation.lin.z.modulation.linear.zdiffusion_model.decoder.)$r<   r   r?   r=   r   rC   r   comfyutilsunet_to_diffusersmodel_configunet_config
isinstance
model_baseStableCascade_CSD3mmdit_to_diffusersAuraFlowauraflow_to_diffusersPixArtpixart_to_diffusers
HunyuanDiTFluxflux_to_diffusersr   
GenmoMochiHunyuanVideoHiDreamACEStepOmnigen2	QwenImageLumina2z_image_to_diffusers
Kandinsky5	ACEStep15
ErnieImage)rF   rG   sdrH   rJ   key_loradiffusers_keysunet_keydiffusers_lora_prefixpdiffusers_lora_keytorZ   s                r/   model_lora_keys_unetr      s   				B
'')C<<*+zz)$S!34c)n_EMMcSVW;<--h78<=A&6I$789*+A'  [[2253E3E3Q3QRN::i +22>!3DEH)3y>/*223<H7?GN))(345=GL''12%'M!*%+]]1a8H#i.6I6Q6QRXZj6k%l"%..{;);CR)@&.6*+	 +  %))99:A||./::i( %7!8#i.IQQRUWZ[HEFG077AB	  %))--.778J8J8V8Vfx7yAzz)$#A&+2215Es9~o3FG$&!077:JC	N?8KL$&!077:JC	N?8K8S8STWY\8]^$&!'..q1A3y>//B/J/J3PS/TU$&!   %))223::5;M;M;Y;Yi{:|Azz)$#A&+2215Es9~o3FG$&!	   %))001889K9K9W9Wgy8zAzz)$#A&+2215Es9~o3FG$&!077:JC	N?8KL$&!5<<Q?OY=PQ$&!   %))445A||./AJJy4IS!34c)n_EBC-44X>? 
 %))../66u7I7I7U7Uew6xAzz)$#A&HJ(//2BC	N?0CDEVX++A.>I,?,G,GS,QRS_a-44Q7GY5H5P5PQTVY5Z[\/1*C	N?+,   A,,88<<]ANKzz)$)9VWZ[]^`kno`oYpUqAII.?$PQR 
 %))445A||./AJJy4IS!34c)n_E12H-. 
 %))667A||./AJJy4I#++KHPPQ]_kltt  vC  ER  S#++HjAII(T^_#++,>@UV#C(:$;S^OL=>(//9:AB,33H=>  %))112A||./::i( %7!8#i.IHOPGL//0@0@c0JKLABG,33H=>  %))112A||./AJJy4IS!34c)n_E12H-. 
 %))223A||./AJJy4IS!34c)n_E12H-. 
 %))334A||./AJJy4IS!34c)n_E12H-.=>(//9:KL++H,<,<S#,FGH  %))11299%:L:L:X:Xhz9{Azz)$#A&-s9~o.AC,33H=>=?(//9:KM++H,<,<S#,FGH$&!   %))445A||./AJJy4IS!34c)n_E12H-.=>(//9:	  %))334A||67AJJy<QS!;<c)n_MBC-44X>?KL++H,<,<S#,FGH	  %))445A||./AJJy4IS!34c)n_E=>(//9: 
 Nr1   tensor	new_shapereturnc                    t        t        t        |            D cg c]  }||   | j                  |   k   c}      rt	        d      t        |      t        | j                        k7  rt	        d      t        j                  || j                  | j                        }t        d | j                  D              }t        d | j                  D              }| |   ||<   |S c c}w )a  
    Pad a tensor to a new shape with zeros.

    Args:
        tensor (torch.Tensor): The original tensor to be padded.
        new_shape (List[int]): The desired shape of the padded tensor.

    Returns:
        torch.Tensor: A new tensor padded with zeros to the specified shape.

    Note:
        If the new shape is smaller than the original tensor in any dimension,
        the original tensor will be truncated in that dimension.
    zGThe new shape must be larger than the original tensor in all dimensionszLThe new shape must have the same number of dimensions as the original tensor)dtypedevicec              3   4   K   | ]  }t        d |        ywr   Nslice.0dims     r/   	<genexpr>z&pad_tensor_to_shape.<locals>.<genexpr>  s     >#a   c              3   4   K   | ]  }t        d |        ywr   r   r   s     r/   r   z&pad_tensor_to_shape.<locals>.<genexpr>  s     =uQ}r   )
anyrA   r   shape
ValueErrortorchzerosr   r   tuple)r   r   ipadded_tensororig_slices
new_slicess         r/   pad_tensor_to_shaper   k  s     E#i.4IJ4IqIaL6<<?*4IJKbcc
9~V\\**ghh KK	fmmTM >>>K===J !'{ 3M*! Ks   C$c                 R   |j                   }| D ]  }|d   }|d   }|t        |t              r t        |t        j                        r|j                  |      }||}Pt        |      dk(  s_|d   |d   }
}	|	dk(  sot        |
      dkD  s~|
d   d   s|
d   j                   } |S )Nr4   r[   r7   r   r
   
pad_weight)r   ra   listr   WeightAdapterBasecalculate_shaper   )patchesweightkeyoriginal_weightscurrent_shaper}   voffsetadapter_shape
patch_type
patch_datas              r/   r   r     s    LLMaD1 At!4a99:--c2M( - q6Q;%&qT1Q4
JV#J!(;
1l@[ *1 3 3% ( r1   c                    | D ]`  }|d   }|d   }|d   }|d   }	|d   }
|
d }
d }|	|}|j                  |	d   |	d   |	d         }|dk7  r||z  }t        |t              rRt        |dd   |d   d   t        j
                  j                  |d   d   |j                  |d	      d
      ||      f}t        |t        j                        rR|j                  |||||	|
||      }|0t        j                  dj                  |j                  |             n|}||}t        |      dk(  rd}nt        |      dk(  r
|d   }|d   }dk(  r%|d   }t        |      dkD  xr |d   d   }|ri|j                  |j                  k7  rPt        j                   dj                  ||j                  |j                               t#        ||j                        }|dk7  r|j                  |j                  k7  r<t        j                  dj                  ||j                  |j                               n-| |
|t        j
                  j                  ||j                  |j$                        z        z  }n|dk(  r|j'                  |d          n|dk(  r|d   }t        j
                  j                  ||j                  |      t        j
                  j                  ||   d   d   |j                  |      z
  }| |
|t        j
                  j                  ||j                  |j$                        z        z  }n%t        j                  dj                  ||             |_|}c |S )Nr   r4   r7   r[      c                     | S N )as    r/   <lambda>z"calculate_weight.<locals>.<lambda>  s    r1   g      ?T)copy)inplace)intermediate_dtypezCalculate Weight Failed: {} {}r
   r   z"Pad weight {} from {} to shape: {}g        z4WARNING SHAPE MISMATCH {} WEIGHT NOT MERGED {} != {}r   model_as_lorazpatch type not recognized {} {})narrowra   r   calculate_weightr\   model_managementcast_to_devicer   r   r   r   r   r   namer   r   infor   r   copy_)r   r   r   r   r   r}   strengthr   strength_modelr   function
old_weightoutputr   r
   do_pad_weighttarget_weightr*   s                     r/   r   r     s   Q4aD11Q4"H
J]]6!9fQiCFS n$Fa!!AB%1a1G1G1V1VWXYZW[\]W^`f`m`m  pB  IM1V  2N  X\  *]  _b  wI  J  MAa99:''X~vW_as  vF  GF~ @ G GPS TU)'Fq6Q;JVq[1J!A!"1DFQJ=1Q4+=Mv||!;AHHfll\`\f\fgh,VTZZ@3::-OO$Z$a$abegkgqgqsyss  %A  Bhx%2H2H2W2WX\^d^k^kmsmymy2z'z{{F5 LL1?**+A$M00??v}}^pq00??@PQT@UVW@XYZ@[]c]j]jl~@Khx%*@*@*O*OP[]c]j]jlrlxlx*yyzzFOO=DDZQTUV!Fy | Mr1   c           
         t        | t        j                        rt        j                  j                  |       }d   }dxx   |z  cc<   | S |||z    }r$t        j                  j                  | g|d       t        j                  j                  | g|      d   S t        | t        j                        r3 t        |       | j                  t        | j                              S t        | t              rt        fd| D              S t        | t               r| D cg c]  }t        |       c}S | S c c}w )Nr   T)non_blockingstreamc              3   <   K   | ]  }t        |        y wr   )prefetch_prepared_value)r   r   r   counterdestinationr   s     r/   r   z*prefetch_prepared_value.<locals>.<genexpr>  s$     ich[_,T7KQUVchs   )ra   r   Tensorr\   memory_managementvram_aligned_sizer   cast_to_gatheredinterpret_gathered_liker   r   typer   r   weightsr   r   )	valuer   r   r   r   sizer   destr   s	    ````    r/   r   r     s3   %&&&88?
d
L6&4-0""33UGTPT]c3d&&>>wMaPP	E>;;	<tE{5,,.EemmU\^ikqsw.xyy	E5	!ichiii	E4	 ^cd^cVZ'g{FDQ^cddL es   .E	)Tr   )__doc__comfy.memory_managementr\   comfy.utilscomfy.model_managementcomfy.model_basecomfy.weight_adapterr   r   r   rB   r0   rU   r   r   r   intr   r   float32r   r   r   r1   r/   <module>r      s   $     -   ***.:x )+ Xt )+ m` c u|| B2 ?Dmm^b ?Br1   