
    +j)                       d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlmZmZmZ d dlmZ d dlZd dlZej                            d ej                            ej                            ej                            e                    d                     d dlZd dlZd dlZd dl Zd dl!Zd dl"Zd dl#m$Z$m%Z%m&Z&m'Z' d dl(m)Z)m*Z* d dl+m,Z, d dl-m.Z.m/Z/m0Z0 d dl1Zd dl2Zd d	l3m4Z4 d dl5Z5d dl6Z6d dl7Z7d dl8Z8e4j9        rd dl:Z:d
 Z;ddZ<dZ= G d de%          Z> G d d          Z? G d d          Z@ G d d          ZA G d d          ZB G d d          ZC G d d          ZD G d d          ZE G d d          ZF G d  d!          ZG G d" d#          ZH G d$ d%          ZI G d& d'          ZJ G d( d)          ZK G d* d+          ZL G d, d-          ZM G d. d/          ZN G d0 d1          ZO G d2 d3          ZP G d4 d5          ZQ G d6 d7          ZR G d8 d9          ZS G d: d;          ZT G d< d=          ZU G d> d?eU          ZV G d@ dA          ZW G dB dC          ZX G dD dE          ZY G dF dG          ZZ G dH dI          Z[ G dJ dK          Z\ G dL dM          Z] G dN dO          Z^ G dP dQ          Z_ G dR dS          Z` G dT dU          Za G dV dW          Zb G dX dY          Zc G dZ d[          Zd G d\ d]          Ze G d^ d_          Zf G d` da          Zg G db dc          Zh G dd de          Zi G df dg          Zj G dh di          Zk G dj dk          Zl G dl dm          Zm G dn do          Zn G dp dq          Zo G dr ds          ZpddvZq G dw dx          Zr G dy dz          Zs G d{ d|          Zt G d} d~et          Zu G d d          Zv G d dev          Zw G d dev          Zx G d d          Zy G d d          Zz G d d          Z{ G d d          Z| G d d          Z} G d d          Z~i dxerd5eQde>d;eTd#eHd'eJd+eLdAeWd_efdeeidgejdaegdcehd|etd~eudevdewi dexdeydezde{de|de~de}de@de?deAdeBdeCdeDdeEdzesdsepdmemi doendiekdkeldqeod=eUdMe]dKe\dOe^dSe`dWebdYecdGeZdIe[dCeXdEeYdUeadQe_eIeKeSedeeeMePeReOeNeFeGeVdZi dxdxdzdd3dd5ddAdd=dd?ddMddCddEddUddQddKdd9dd[ddSddWdi ddd;ddddddddddddddGddIdd+ddsdd#dd'ddiddkddqdi d_ddeddgddmddoddaddcdddd|dd~dddddddddddddÓddēddddddddddddМZi Zi ZdedefdӄZ e            dfdedefdՄZdք Zdׄ Zd؄ Zdل ZddڄZdS )    N)ImageImageOpsImageSequence)PngInfocomfy)IOComfyNodeABCInputTypeDictFileLocator)register_versionsComfyAPIWithVersion)supported_versions)ioComfyExtension	InputImpl)argsc                  B    t           j                                         d S N)r   model_management)throw_exception_if_processing_interrupted     %/home/wildlama/comfy/ComfyUI/nodes.pybefore_node_executionr   2   s    	DDFFFFFr   Tc                 D    t           j                            |            d S r   )r   r   interrupt_current_processing)values    r   interrupt_processingr   5   s    	77>>>>>r   i @  c                   X    e Zd Zedefd            Zej        fZdZ	dZ
dZdZg dZd Zd	S )
CLIPTextEncodereturnc                 L    dt           j        ddddft           j        ddifdiS )NrequiredTzThe text to be encoded.)	multilinedynamicPromptstooltipr&   z*The CLIP model used for encoding the text.)textclip)r   STRINGCLIPss    r   INPUT_TYPESzCLIPTextEncode.INPUT_TYPES;   s@     $$[t$u$uv9.Z"[\ 
 	
r   )zNA conditioning containing the embedded text used to guide the diffusion model.encodemodel/conditioningzEncodes a text prompt using a CLIP model into an embedding that can be used to guide the diffusion model towards generating specific images.)r'   promptztext promptzpositive promptznegative promptzencode textztext encoderzencode promptc                 z    |t          d          |                    |          }|                    |          fS )NzERROR: clip input is invalid: None

If the clip is from a checkpoint loader node your checkpoint does not contain a valid clip or text encoder model.)RuntimeErrortokenizeencode_from_tokens_scheduled)selfr(   r'   tokenss       r   r.   zCLIPTextEncode.encodeK   sJ    <   y  z  z  zt$$11&99<<r   N)__name__
__module____qualname__classmethodr
   r-   r   CONDITIONINGRETURN_TYPESOUTPUT_TOOLTIPSFUNCTIONCATEGORYDESCRIPTIONSEARCH_ALIASESr.   r   r   r   r    r    :   s        
- 
 
 
 [
 O%LiOH#H aK M  M  MN= = = = =r   r    c                   B    e Zd ZdZed             ZdZdZdZg dZ	d Z
dS )	ConditioningCombineImage Generationc                     ddddiS )Nr#   r;   )conditioning_1conditioning_2r   r+   s    r   r-   zConditioningCombine.INPUT_TYPEST   s    /AUghhiir   rF   combiner/   )rI   zmerge conditioningzcombine promptszmerge promptsmix promptsz
add promptc                     ||z   fS r   r   )r5   rG   rH   s      r   rI   zConditioningCombine.combine]   s    /22r   N)r7   r8   r9   ESSENTIALS_CATEGORYr:   r-   r<   r>   r?   rA   rI   r   r   r   rC   rC   R   s_        ,j j [j$LH#HwwwN3 3 3 3 3r   rC   c                   >    e Zd Zg dZed             ZdZdZdZd Z	dS )ConditioningAverage)zblend promptszinterpolate conditioningrJ   zstyle fusionzweighted blendc           	           dddddddddfdiS )	Nr#   rF   FLOAT      ?        {Gz?defaultminmaxstep)conditioning_toconditioning_fromconditioning_to_strengthr   r+   s    r   r-   zConditioningAverage.INPUT_TYPESc   s7    0BYk;BPS\_hkuyDzDz:{    	 r   rF   addWeightedr/   c           
         g }t          |          dk    rt          j        d           |d         d         }|d         d                             dd           }t	          t          |                    D ]`}||         d         }||         d                             d|          }	|d d d |j        d         f         }
|
j        d         |j        d         k     rTt          j        |
gt          j        d|j        d         |
j        d         z
  |j        d         f          gz   d          }
t          j	        ||          t          j	        |
d|z
            z   }||         d         
                                }|4|	2t          j	        |	|          t          j	        |d|z
            z   |d<   n|||d<   ||g}|                    |           b|fS )N   zWarning: ConditioningAverage conditioning_from contains more than 1 cond, only the first one will actually be applied to conditioning_to.r   pooled_output   dimrQ   )lenloggingwarninggetrangeshapetorchcatzerosmulcopyappend)r5   rY   rZ   r[   out	cond_frompooled_output_fromit1pooled_output_tot0twt_tons                 r   r\   zConditioningAverage.addWeightedm   s    !!A%%O  h  i  i  i%a(+	.q1!488$OOs?++,, 	 	A #A&B.q1!488J\]]111\bhqk\>*Bx{RXa[((Ytu{Abhqk8QTVT\]^T_3`'a'a&bbhijjj278859R#PhJh;j;jjB"1%a(--//D!-2B2N(-	2BD\(](]`e`ij|  @C  F^  @^  a`  a`  )`_%%#/(:_%T
AJJqMMMMwr   N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r\   r   r   r   rN   rN   `   sX        sssN    [  %LH#H    r   rN   c                   6    e Zd Zed             ZdZdZdZd ZdS )ConditioningConcatc                     ddddiS )Nr#   rF   )rY   rZ   r   r+   s    r   r-   zConditioningConcat.INPUT_TYPES   s    0!2   	r   rF   concatr/   c                 l   g }t          |          dk    rt          j        d           |d         d         }t          t          |                    D ]^}||         d         }t	          j        ||fd          }|||         d                                         g}|                    |           _|fS )Nr^   zWarning: ConditioningConcat conditioning_from contains more than 1 cond, only the first one will actually be applied to conditioning_to.r   )rc   rd   re   rg   ri   rj   rm   rn   )	r5   rY   rZ   ro   rp   rr   rs   rv   rx   s	            r   r|   zConditioningConcat.concat   s     !!A%%O  g  h  h  h%a(+	s?++,, 	 	A #A&BB	?1--B_Q'*//112AJJqMMMMwr   N)	r7   r8   r9   r:   r-   r<   r>   r?   r|   r   r   r   rz   rz      sM          [
 %LH#H    r   rz   c                   >    e Zd Zg dZed             ZdZdZdZd Z	dS )ConditioningSetArea)zregional promptzarea promptzspatial conditioningzlocalized promptc                     dddddt           ddfdddt           ddfdddt           ddfdddt           ddfdd	d
dddfdiS )Nr#   rF   INT@      rT   r   rP   rQ   rR         $@rS   conditioningwidthheightxystrengthMAX_RESOLUTIONr+   s    r   r-   zConditioningSetArea.INPUT_TYPES   s    -?(-2bQ_ij/k/k'l).BrR`jk0l0l(m$)q>cd+e+e#f$)q>cd+e+e#f+2CX\fj4k4k*l    	 r   rF   rn   r/   c                 Z    t          j        ||dz  |dz  |dz  |dz  f|dd          }|fS )Nr   Farear   set_area_to_boundsnode_helpersconditioning_set_valuesr5   r   r   r   r   r   r   cs           r   rn   zConditioningSetArea.append   sZ    0STV[_`V`bcghbhjkopjpHqLTV[@] @] ^ ^ ur   N
r7   r8   r9   rA   r:   r-   r<   r>   r?   rn   r   r   r   r   r      sX        cccN    [  %LH#H    r   r   c                   6    e Zd Zed             ZdZdZdZd ZdS )ConditioningSetAreaPercentagec                 ^    ddddddddfddddddfddddddfddddddfdddd	ddfd
iS )Nr#   rF   rP   rQ   r   rS   rT   rR   r   r   r   r+   s    r   r-   z)ConditioningSetAreaPercentage.INPUT_TYPES   s    -?(/SSV`d1e1e'f)0c!TWae2f2f(g$+1SZ^-_-_#`$+1SZ^-_-_#`+2CX\fj4k4k*l    	 r   rF   rn   r/   c                 D    t          j        |d||||f|dd          }|fS )N
percentageFr   r   r   s           r   rn   z$ConditioningSetAreaPercentage.append   sF    0W]_dfgijHkLTV[@] @] ^ ^ ur   N	r7   r8   r9   r:   r-   r<   r>   r?   rn   r   r   r   r   r      sM            [  %LH#H    r   r   c                   6    e Zd Zed             ZdZdZdZd ZdS )ConditioningSetAreaStrengthc                     ddddddddfd	iS )
Nr#   rF   rP   rQ   rR   r   rS   rT   )r   r   r   r+   s    r   r-   z'ConditioningSetAreaStrength.INPUT_TYPES   s2    -?+2CX\fj4k4k*l    	 r   rF   rn   r/   c                 6    t          j        |d|i          }|fS )Nr   r   )r5   r   r   r   s       r   rn   z"ConditioningSetAreaStrength.append   s     0
H?UVVur   Nr   r   r   r   r   r      sM            [  %LH#H    r   r   c                   >    e Zd Zg dZed             ZdZdZdZd Z	dS )ConditioningSetMask)zmasked promptzregional inpaint conditioningzmask conditioningc           	      (    ddddddddd	fd
dgfdiS )Nr#   rF   MASKrP   rQ   rR   r   rS   rT   rU   zmask bounds)r   maskr   set_cond_arear   r+   s    r   r-   zConditioningSetMask.INPUT_TYPES   s@    -?&0+2CX\fj4k4k*l1:M0J/L    	 r   rF   rn   r/   c                     d}|dk    rd}t          |j                  dk     r|                    d          }t          j        ||||d          }|fS )NFrU   T   r   )r   r   mask_strength)rc   rh   	unsqueezer   r   )r5   r   r   r   r   r   r   s          r   rn   zConditioningSetMask.append   sr    "I%%!%tz??Q>>!$$D0VhQY@[ @[ \ \ ur   Nr   r   r   r   r   r      sX        \\\N    [  %LH#H
 
 
 
 
r   r   c                   >    e Zd ZddgZed             ZdZdZdZd Z	dS )	ConditioningZeroOutznull conditioningzclear conditioningc                     dddiiS )Nr#   r   rF   r   r+   s    r   r-   zConditioningZeroOut.INPUT_TYPES   s    ^-?@AAr   rF   zero_outadvanced/conditioningc                 h   g }|D ]}|d                                          }|                    dd           }|t          j        |          |d<   |                    dd           }|t          j        |          |d<   t          j        |d                   |g}|                    |           |fS )Nr^   r_   conditioning_lyricsr   )rm   rf   ri   
zeros_likern   )r5   r   r   tdr_   r   rx   s           r   r   zConditioningZeroOut.zero_out  s     		 		A!		AEE/488M(%*%5m%D%D/""#%%(=t"D"D".+0+;<O+P+P'(!!A$''+AHHQKKKKur   N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r   r   r   r   r   r      sY        )+?@NB B [B$LH&H    r   r   c                   6    e Zd Zed             ZdZdZdZd ZdS )ConditioningSetTimestepRangec           	      .    ddddddddfddddddfdiS )	Nr#   rF   rP   rR   rQ   MbP?rT   )r   startendr   r+   s    r   r-   z(ConditioningSetTimestepRange.INPUT_TYPES  sH    -?'.CTWaf0g0g&h%,#cRU_d.e.e$f    	 r   rF   	set_ranger   c                 8    t          j        |||d          }|fS )N)start_percentend_percentr   )r5   r   r   r   r   s        r   r   z&ConditioningSetTimestepRange.set_range   s3    0QVOR@T @T U Uur   N)	r7   r8   r9   r:   r-   r<   r>   r?   r   r   r   r   r   r     sM            [ 
 %LH&H    r   r   c                   F    e Zd Zed             ZdZdZdZdZdZ	g dZ
d Zd	S )
	VAEDecodec                      ddddifdddifdiS )Nr#   LATENTr&   zThe latent to be decoded.VAEz+The VAE model used for decoding the latent.)samplesvaer   r+   s    r   r-   zVAEDecode.INPUT_TYPES&  s7     $y2M&NO	+XYZ 
 	
r   IMAGE)zThe decoded image.decodemodel/latentz3Decodes latent images back into pixel space images.)r   zdecode latentzlatent to imagezrender latentc                 &   |d         }|j         r|                                d         }|                    |          }t          |j                  dk    r9|                    d|j        d         |j        d         |j        d                   }|fS )Nr   r      )	is_nestedunbindr   rc   rh   reshape)r5   r   r   latentimagess        r   r   zVAEDecode.decode6  s    # 	(]]__Q'FF##v|!!^^BR(8&,r:JFLY[L\]]Fzr   N)r7   r8   r9   r:   r-   r<   r=   r>   r?   r@   rA   r   r   r   r   r   r   %  sb        
 
 [
 L-OHHGKTTTN    r   r   c                   8    e Zd Zed             ZdZdZdZd	dZdS )
VAEDecodeTiledc                 \    ddddddddd	d
fdddddd	d
fddddddd	dfddddddd	dfdiS )Nr#   r   r   r      r          TrU   rV   rW   rX   advancedr   r      z?Only used for video VAEs: Amount of frames to decode at a time.rU   rV   rW   rX   r&   r   6Only used for video VAEs: Amount of frames to overlap.)r   r   	tile_sizeoverlaptemporal_sizetemporal_overlapr   r+   s    r   r-   zVAEDecodeTiled.INPUT_TYPESA  s    Y+0c"UYcesw2x2x*y).BqQU_aos0t0t(u/4"QW[ef  tu  CG  7H  7H  /I27QqY]gh  vn  |@  :A  :A  2B	   	r   r   r   experimentalr   r   c                    ||dz  k     r|dz  }||dz  k     r|dz  }|                                 }|8t          d||z            }t          dt          |dz  ||z                      }nd }d }|                                }|                    |d         ||z  ||z  ||z  ||          }	t          |	j                  dk    r9|	                    d|	j        d         |	j        d	         |	j        d                   }	|	fS )
Nr   r`   r^   r   tile_xtile_yr   tile_t	overlap_tr   r   r   r   )temporal_compression_decoderW   rV   spacial_compression_decodedecode_tiledrc   rh   r   )
r5   r   r   r   r   r   r   temporal_compressioncompressionr   s
             r   r   zVAEDecodeTiled.decodeN  sF   w{""1nG+a////14">>@@+=4H#HIIM"1c-1*<>NRf>f&g&ghh M#4466!!')"4Y+=U^gkv^v  AH  LW  AW  `m  yI!  J  Jv|!!^^BR(8&,r:JFLY[L\]]Fzr   N)r   r   r   )	r7   r8   r9   r:   r-   r<   r>   r?   r   r   r   r   r   r   @  sR          [ LHH     r   r   c                   >    e Zd Zed             ZdZdZdZg dZd Z	dS )	VAEEncodec                     ddddiS )Nr#   r   r   )pixelsr   r   r+   s    r   r-   zVAEEncode.INPUT_TYPESb  s    IFFGGr   r   r.   r   )r.   zencode imagezimage to latentc                 6    |                     |          }d|ifS )Nr   )r.   )r5   r   r   r   s       r   r.   zVAEEncode.encodek  s     JJv1  r   N)
r7   r8   r9   r:   r-   r<   r>   r?   rA   r.   r   r   r   r   r   a  sY        H H [HLHHBBBN! ! ! ! !r   r   c                   8    e Zd Zed             ZdZdZdZd	dZdS )
VAEEncodeTiledc                 \    dddddddddd	fddd
dddd	fddddddddfddddddddfdiS )Nr#   r   r   r   r   r   r   Tr   r   r   r   r   z?Only used for video VAEs: Amount of frames to encode at a time.r   r   )r   r   r   r   r   r   r   r+   s    r   r-   zVAEEncodeTiled.INPUT_TYPESp  s    {9+0c"UYcesw2x2x*y).BqQU_aos0t0t(u/4"QW[ef  tu  CG  7H  7H  /I27QqY]gh  vn  |@  :A  :A  2B	   	r   r   r.   r   r   r   c                 B    |                     ||||||          }d|ifS )Nr   r   )encode_tiled)r5   r   r   r   r   r   r   r   s           r   r.   zVAEEncodeTiled.encode}  s<    VIiQXan  {K  L  LA!!r   N)r   r   	r7   r8   r9   r:   r-   r<   r>   r?   r.   r   r   r   r   r   o  sR          [ LHH" " " " " "r   r   c                   8    e Zd Zed             ZdZdZdZddZdS )	VAEEncodeForInpaintc           
      "    ddddddddd	d
fdiS )Nr#   r   r   r   r      r   r   r^   rT   )r   r   r   grow_mask_byr   r+   s    r   r-   zVAEEncodeForInpaint.INPUT_TYPES  sW    Izlq  @A  JK  TV  `a  tb  tb  lc  e  e  f  	fr   r   r.   model/latent/inpaintr  c           	         |                                 }|j        d         |z  |z  }|j        d         |z  |z  }t          j        j                            |                    dd|j        d         |j        d         f          |j        d         |j        d         fd          }|                                }|j        d         |k    s|j        d         |k    rZ|j        d         |z  dz  }|j        d         |z  dz  }	|d d |||z   |	||	z   d d f         }|d d d d |||z   |	||	z   f         }|dk    r|}
nt          j        dd||f          }t          j
        |dz
  dz            }t          j        t          j        j                            |                                ||          dd          }
d	|                                z
                      d          }t          d
          D ]P}|d d d d d d |fxx         dz  cc<   |d d d d d d |fxx         |z  cc<   |d d d d d d |fxx         dz  cc<   Q|                    |          }||
d d d d d |d |f                                         dfS )Nr^   r`   r   r   bilinearsizemoder   )paddingrQ   r         ?)r   
noise_mask)spacial_compression_encoderh   ri   nn
functionalinterpolater   cloneonesmathceilclampconv2droundsqueezerg   r.   )r5   r   r   r   r  downscale_ratior   r   x_offsety_offsetmask_erosionkernel_tensorr	  mrr   r   s                   r   r.   zVAEEncodeForInpaint.encode  s   88::\!_/?B\!_/?Bx"..t||RDJrNTXT^_aTb<c/d/dlrlxyzl{  ~D  ~J  KL  ~M  lN  U_.  `  `<?a6<?a#7#7Q/9a?HQ/9a?HAAAhq8|3Xa(l5J111LMF!!!HQ\18AL3HHID 1LL!J1lL'IJJMi!1Q 677G ;ux':'A'A$**,,P]gn'A'o'oqrtuvvL4::<<((++q 	# 	#A111QQQqqq7OOOs"OOO111QQQqqq7OOOq OOO111QQQqqq7OOOs"OOOOJJv\!!!AAAbqb!)-D-J-J-L-LNNQQr   N)r  r   r   r   r   r   r     sY        f f [fLH%HR R R R R Rr   r   c                   <    e Zd Zed             ZdZdZdZdZd	dZ	dS )
InpaintModelConditioningc           
      "    dddddddddd	fd
iS )Nr#   rF   r   r   r   BOOLEANTzAdd a noise mask to the latent so sampling will only happen within the mask. Might improve results or completely break things depending on the model.rU   r&   )positivenegativer   r   r   r  r   r+   s    r   r-   z$InpaintModelConditioning.INPUT_TYPES  sF    ););$-'2%/,54  Ul  8m  8m  ,n    	 r   )r;   r;   r   )r#  r$  r   r.   zmodel/conditioning/inpaintTc           	      \   |j         d         dz  dz  }|j         d         dz  dz  }t          j        j                            |                    dd|j         d         |j         d         f          |j         d         |j         d         fd          }|}	|	                                }|j         d         |k    s|j         d         |k    rZ|j         d         dz  dz  }
|j         d         dz  dz  }|d d |
||
z   |||z   d d f         }|d d d d |
||
z   |||z   f         }d|                                z
                      d          }t          d	          D ]P}|d d d d d d |fxx         d
z  cc<   |d d d d d d |fxx         |z  cc<   |d d d d d d |fxx         d
z  cc<   Q|
                    |          }|
                    |	          }i }||d<   |r||d<   g }||fD ]/}t          j        |||d          }|                    |           0|d         |d         |fS )Nr^   r   r`   r   r   r  r  rQ   r   r
  r   r  )concat_latent_imageconcat_maskr   )rh   ri   r  r  r  r   r  r  r  rg   r.   r   r   rn   )r5   r#  r$  r   r   r   r  r   r   orig_pixelsr  r  r  rr   concat_latentorig_latent
out_latentro   r   r   s                       r   r.   zInpaintModelConditioning.encode  s   \!_!Q&\!_!Q&x"..t||RDJrNTXT^_aTb<c/d/dlrlxyzl{  ~D  ~J  KL  ~M  lN  U_.  `  `""$$<?a6<?a#7#7Q!+1HQ!+1HAAAhq8|3Xa(l5J111LMF!!!HQ\18AL3HHID4::<<((++q 	# 	#A111QQQqqq7OOOs"OOO111QQQqqq7OOOq OOO111QQQqqq7OOOs"OOOO

6**jj--
 +
9 	,'+J|$%x0 	 	L4\[hSWDY DY Z ZAJJqMMMMAA
++r   NT)
r7   r8   r9   r:   r-   r<   RETURN_NAMESr>   r?   r.   r   r   r   r  r    sW            [  <L5LH+H ,  ,  ,  ,  ,  ,r   r  c                   H    e Zd ZdgZd Zed             ZdZdZdZ	dZ
dd
Zd	S )
SaveLatentzexport latentc                 6    t          j                    | _        d S r   )folder_pathsget_output_directory
output_dirr5   s    r   __init__zSaveLatent.__init__  s    &;==r   c                      ddddifddddd	S )
Nr   r)   rU   zlatents/ComfyUI)r   filename_prefixPROMPTEXTRA_PNGINFOr0   extra_pnginfor#   hiddenr   r+   s    r   r-   zSaveLatent.INPUT_TYPES  s9    )52:YHY<Z1[] ]%-PP  	r   r   saveTr   ComfyUINc                 "   t          j        || j                  \  }}}}}d}	|t          j        |          }	d }
t
          j        s(d|	i}
|"|D ]}t          j        ||                   |
|<    | d|dd}g }|                    ||dd           t          j	        
                    ||          }i }|d                                         |d	<   t          j        g           |d
<   t          j                            |||
           dd|iiS )N r0   _05z_.latentoutputfilename	subfoldertyper   latent_tensorlatent_format_version_0)metadatauilatents)r1  get_save_image_pathr3  jsondumpsr   disable_metadatarn   ospathjoin
contiguousri   tensorr   utilssave_torch_file)r5   r   r7  r0   r;  full_output_folderrF  counterrG  prompt_inforK  r   fileresultsrD  s                  r   r>  zSaveLatent.save  s[   LXLlm|  C  N  MO  MOIHgy/ *V,,K$ 	? +.H(& ? ?A"&*]1-=">">HQKK11W1111%'"
 
 	 	 	 w||.55"))"4"?"?"A"A,1L,<,<()##FD8#DDDG-//r   r?  NN)r7   r8   r9   rA   r5  r:   r-   r<   r>   OUTPUT_NODEr?   r>  r   r   r   r/  r/    sp        %&N> > >   [
 LHKH0 0 0 0 0 0r   r/  c                   j    e Zd ZddgZed             ZdZdZdZd Z	ed             Z
ed	             Zd
S )
LoadLatentzimport latentzopen latentc                     t          j                    fdt          j                  D             }ddt	          |          giiS )Nc                     g | ]V}t           j                            t           j                            |                    ?|                    d           T|WS )z.latent)rR  rS  isfilerT  endswith.0f	input_dirs     r   
<listcomp>z*LoadLatent.INPUT_TYPES.<locals>.<listcomp>  sS    zzzqRW^^BGLLQZ\]D^D^5_5_zdedndnoxdydyzzzzr   r#   r   )r1  get_input_directoryrR  listdirsortedr,   filesri  s     @r   r-   zLoadLatent.INPUT_TYPES  sM     466	zzzzBJy11zzzXu'89<<r   r   r   loadc                     t          j        |          }t          j                            |d          }d}d|vrd}d|d                                         |z  i}|fS )NcpudevicerQ   rJ  g!ޅ@r   rI  )r1  get_annotated_filepathsafetensorsri   	load_filefloat)r5   r   latent_path
multiplierr   s        r   rp  zLoadLatent.load!  sj    "9&AA",,[,GG
$F22&Jf_5;;==
JK{r   c                 :   t          j        |          }t          j                    }t	          |d          5 }|                    |                                           d d d            n# 1 swxY w Y   |                                                                S Nrb	r1  ru  hashlibsha256openupdatereaddigesthex)r,   r   
image_pathr  rh  s        r   
IS_CHANGEDzLoadLatent.IS_CHANGED*  s    !8@@
N*d## 	qHHQVVXX	 	 	 	 	 	 	 	 	 	 	 	 	 	 	xxzz~~   (A,,A03A0c                 X    t          j        |          sd                    |          S dS )NzInvalid latent file: {}Tr1  exists_annotated_filepathformat)r,   r   s     r   VALIDATE_INPUTSzLoadLatent.VALIDATE_INPUTS2  s.    5f== 	<,33F;;;tr   N)r7   r8   r9   rA   r:   r-   r?   r<   r>   rp  r  r  r   r   r   ra  ra    s        %}5N= = [=
 HLH       [    [  r   ra  c                   B    e Zd ZddgZed             ZdZdZdZdZ	d Z
d	S )
CheckpointLoader
load modelmodel loaderc                 \    dt          j        d          ft          j        d          fdiS )Nr#   configscheckpoints)config_name	ckpt_namer1  get_filename_listr+   s    r   r-   zCheckpointLoader.INPUT_TYPES<  s>    l.LY.W.W-Z,8,J=,Y,Y+\^ ^ _ 	_r   MODELr*   r   load_checkpointadvanced/loadersTc           	          t          j        d|          }t          j        d|          }t          j                            ||ddt          j        d                    S )Nr  r  T
embeddings
output_vaeoutput_clipembedding_directory)r1  get_full_pathget_full_path_or_raiser   sdr  get_folder_paths)r5   r  r  config_path	ckpt_paths        r   r  z CheckpointLoader.load_checkpointF  sr    "0KHH 7yQQ	x''Y4]a  xD  xU  Vb  xc  xc'  d  d  	dr   N)r7   r8   r9   rA   r:   r-   r<   r>   r?   
DEPRECATEDr  r   r   r   r  r  9  sd        "N3N_ _ [_ ,L H!HJd d d d dr   r  c                   F    e Zd Zed             ZdZdZdZdZdZ	g dZ
d Zd	S )
CheckpointLoaderSimplec                 :    ddt          j        d          ddifiiS )Nr#   r  r  r&   z+The name of the checkpoint (model) to load.r  r+   s    r   r-   z"CheckpointLoaderSimple.INPUT_TYPESL  s<     l<]KKi  ZG  NH  I
 	
r   r  )z%The model used for denoising latents.z.The CLIP model used for encoding text prompts.zMThe VAE model used for encoding and decoding images to and from latent space.r  model/loaderszQLoads a diffusion model checkpoint, diffusion models are used to denoise latents.)r  
checkpointr  zload checkpointckptmodelc                     t          j        d|          }t          j                            |ddt          j        d                    }|d d         S )Nr  Tr  r  r   r1  r  r   r  load_checkpoint_guess_configr  )r5   r  r  ro   s       r   r  z&CheckpointLoaderSimple.load_checkpoint]  sg     7yQQ	h33I$\`  wC  wT  Ua  wb  wb3  c  c2A2wr   N)r7   r8   r9   r:   r-   r<   r=   r>   r?   r@   rA   r  r   r   r   r  r  K  sg        
 
 [
 ,LhO !HHeKeeeN    r   r  c                   >    e Zd ZdgZed             ZdZdZdZd	dZ	dS )
DiffusersLoaderzload diffusers modelc                 *   g }t          j        d          D ]v}t          j                            |          rUt          j        |d          D ]>\  }}}d|v r4|                    t          j                            ||                     ?wdd|fiiS )N	diffusersT)followlinkszmodel_index.json)r   r#   
model_path)r1  r  rR  rS  existswalkrn   relpath)clspathssearch_pathrootsubdirro  s         r   r-   zDiffusersLoader.INPUT_TYPESe  s    '8EE 	O 	OKw~~k** O+-7;D+Q+Q+Q O O'D&%)U22RW__T_%M%MNNN\E8677r   r  r  zadvanced/loaders/deprecatedTc                 ^   t          j        d          D ]d}t          j                            |          rCt          j                            ||          }t          j                            |          r|} net          j                            |||t          j        d                    S )Nr  r  r  )	r1  r  rR  rS  r  rT  r   diffusers_loadload_diffusers)r5   r  r  r  r  rS  s         r   r  zDiffusersLoader.load_checkpointt  s    '8EE 	 	Kw~~k** w||K<<7>>$'' !%JE#22:*bm  DP  Da  bn  Do  Do2  p  p  	pr   NTT)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r  r   r   r   r  r  b  s`        ,-N8 8 [8 ,L H,Hp p p p p pr   r  c                   8    e Zd Zed             ZdZdZdZddZdS )	unCLIPCheckpointLoaderc                 4    ddt          j        d          fiiS )Nr#   r  r  r  r+   s    r   r-   z"unCLIPCheckpointLoader.INPUT_TYPES  &    kL,J=,Y,Y+\    	 r   )r  r*   r   CLIP_VISIONr  r  Tc           	          t          j        d|          }t          j                            |dddt          j        d                    }|S )Nr  Tr  )r  r  output_clipvisionr  r  )r5   r  r  r  r  ro   s         r   r  z&unCLIPCheckpointLoader.load_checkpoint  sb     7yQQ	h33I$\`tx  O[  Ol  my  Oz  Oz3  {  {
r   Nr  )	r7   r8   r9   r:   r-   r<   r>   r?   r  r   r   r   r  r    sR            [  ;L HH     r   r  c                   6    e Zd Zed             ZdZdZdZd ZdS )CLIPSetLastLayerc           	           dddddddddfd	iS )
Nr#   r*   r   r   ir^   Tr   )r(   stop_at_clip_layerr   r+   s    r   r-   zCLIPSetLastLayer.INPUT_TYPES  s8    j5:SV_aklz~<<  5A    ! 	!r   r  set_last_layerr/   c                 Z    |                                 }|                    |           |fS r   )r  
clip_layer)r5   r(   r  s      r   r  zCLIPSetLastLayer.set_last_layer  s)    zz||*+++wr   N)	r7   r8   r9   r:   r-   r<   r>   r?   r  r   r   r   r  r    sM        ! ! [! LH#H    r   r  c                   P    e Zd ZdZd Zed             ZdZdZdZ	dZ
dZg d	Zd
 ZdS )
LoraLoaderrD   c                     d | _         d S r   )loaded_lorar4  s    r   r5  zLoraLoader.__init__  s    r   c                 r    ddddifdddift          j        d          ddifd	d
dddddfd	d
dddddfdiS )Nr#   r  r&   z0The diffusion model the LoRA will be applied to.r*   z+The CLIP model the LoRA will be applied to.loraszThe name of the LoRA.rP   rQ         Y      Y@rS   zGHow strongly to modify the diffusion model. This value can be negative.rU   rV   rW   rX   r&   zBHow strongly to modify the CLIP model. This value can be negative.)r  r(   	lora_namestrength_modelstrength_clipr  r+   s    r   r-   zLoraLoader.INPUT_TYPES  s     !I/a#bc)-Z![\*<WEE	SjGkl#*FSXbf  t}  -~  -~  #")s6RWae  sw  ,x  ,x  "y 
 	
r   )r  r*   )zThe modified diffusion model.zThe modified CLIP model.	load_lorar  zThis LoRA loader is used to modify both diffusion and CLIP models, altering the way in which latents are denoised such as applying styles. Multiple LoRA nodes can be linked together.)loraz	load loraz
apply lorazlora loaderz
lora modelc                    |dk    r
|dk    r||fS t          j        d|          }d }d }| j        M| j        d         |k    r5| j        d         }t          | j                  dk    r| j        d         nd }nd | _        |/t          j                            |dd          \  }}|||f| _        t          j                            ||||||          \  }	}
|	|
fS )Nr   r  r^   r`   T)	safe_loadreturn_metadata)lora_metadata)	r1  r  r  rc   r   rW  load_torch_filer  load_lora_for_models)r5   r  r(   r  r  r  	lora_pathr  r  
model_lora	clip_loras              r   r  zLoraLoader.load_lora  s   Q=A#5#54=  7KK	'"i//'*7:4;K7L7Lq7P7P 0 3 3VZ#' <"'+"="=iSWim"="n"nD- )4?D % = =eT4Q_an  L =  !M  !M
II&&r   N)r7   r8   r9   rL   r5  r:   r-   r<   r=   r>   r?   r@   rA   r  r   r   r   r  r    s|        ,      	
 	
 [	
 %LSOHH KKUUUN' ' ' ' 'r   r  c                   6    e Zd Zed             ZdZdZdZd ZdS )LoraLoaderModelOnlyc           	      F    ddt          j        d          fdddddd	fd
iS )Nr#   r  r  rP   rQ   r  r  rS   rT   )r  r  r  r  r+   s    r   r-   zLoraLoaderModelOnly.INPUT_TYPES  sC    z,8,J7,S,S+V18cRXafpt:u:u0v    ! 	!r   r  zThis LoRAs loader is used to modify the diffusion model, altering the way in which latents are denoised such as applying styles. Multiple LoRA nodes can be linked together.load_lora_model_onlyc                 B    |                      |d ||d          d         fS )Nr   )r  )r5   r  r  r  s       r   r  z(LoraLoaderModelOnly.load_lora_model_only  s$    udI~qII!LNNr   N)	r7   r8   r9   r:   r-   r<   r@   r>   r  r   r   r   r  r    sU        ! ! [!
 L AK%HO O O O Or   r  c                   r    e Zd Zg dZg dZed             Zed             Zed             Z	dZ
dZdZd	 Zd
S )	VAELoader)taehvlighttaew2_2lighttaew2_1lighttaehy1_5taeltx_2)taesdtaesdxltaesd3taef1taef2c                    t          j        d          }t          j        d          }t                      t                      c}|D ]}|                    dd          }t	          |          dk    s|d         | j        vr7| j        D ].}|                    |          r|                    |            n/q|d                             d          r	                    |d                    |d                             d          r|	                    |d                    |fd	|D             z  }|                    d
           |S )Nr   
vae_approxrB  r^   r`   r   zencoder.zdecoder.c                     g | ]}|v |	S r   r   )rg  khave_img_encoders     r   rj  z&VAELoader.vae_list.<locals>.<listcomp>  s$    FFFq5E0E0E0E0E0Er   pixel_space)
r1  r  setsplitrc   
image_taes
video_taes
startswithrn   add)r,   vaesapprox_vaeshave_img_decodervpartstaer  s          @r   vae_listzVAELoader.vae_list  sZ   -e44"4\BB-0UUCEE** 	/ 	/AGGCOOE5zzQ%(!,">"><  C||C(( A Qx"":.. / $$U1X....q$$Z00 / $$U1X...FFFF,FFFFM"""r   c                     i }t          j        d          }t          t           fd|                    }t          t           fd|                    }t          j                            t          j        d|                    }|D ] }||         |d                    |          <   !t          j                            t          j        d|                    }|D ] }||         |d                    |          <   ! dk    r/t          j
        d          |d<   t          j
        d	          |d
<   n dk    r/t          j
        d          |d<   t          j
        d	          |d
<   ni dk    r/t          j
        d          |d<   t          j
        d          |d
<   n4 dk    r.t          j
        d          |d<   t          j
        d          |d
<   |S )Nr  c                 T    |                      d                                        S )Nz{}_encoder.r  r  anames    r   <lambda>z&VAELoader.load_taesd.<locals>.<lambda>  !    ]5I5I$5O5O(P(P r   c                 T    |                      d                                        S )Nz{}_decoder.r  r  s    r   r  z&VAELoader.load_taesd.<locals>.<lambda>  r  r   ztaesd_encoder.{}ztaesd_decoder.{}r  g{P?	vae_scalerR   	vae_shiftr  gy&1?r  grh|?gH.?r  gxz,C?g=U?)r1  r  nextfilterr   rW  r  r  r  ri   rV  )r  r  r  encoderdecoderencr  decs   `       r   
load_taesdzVAELoader.load_taesd  s   "4\BBvPPPPR]^^__vPPPPR]^^__k)),*Ml\c*d*dee 	6 	6A/21vB!((++,,k)),*Ml\c*d*dee 	6 	6A/21vB!((++,,7??#l733B{O#l3//B{OOY#l733B{O#l3//B{OOX#l622B{O#l622B{OOW__#l622B{O#l622B{O	r   c                 6    dd|                      |           fiiS )Nr#   vae_name)r  r+   s    r   r-   zVAELoader.INPUT_TYPES  s    j1::a==*;<==r   r   load_vaer  c                 V   d }d }|dk    ri }t          j        d          |d<   n|| j        v r|                     |          }n{t          j                            |          d         | j        v rt          j	        d|          }nt          j	        d|          }t          j                            |d          \  }}|d	k    r|d
di}nd|d
<   t          j                            ||          }|                                 | t          j        j        ||d ff|j        _        |fS )Nr  rQ   pixel_space_vaer   r  r   T)r  r  tae_latent_channels   )r  rK  )ri   rV  r  r"  rR  rS  splitextr  r1  r  r   rW  r  r  r   throw_exception_if_invalidload_vae_patcherpatchercached_patcher_init)r5   r$  rK  vae_pathr  r   s         r   r%  zVAELoader.load_vae  s8   }$$B$)L$5$5B !!((**BBw))!,??'>|XVV'>uhOO ;66xQU6VVLBw13725./hllb8l44&&((( /4x/H8U]_cJd.eCK+vr   N)r7   r8   r9   r  r  staticmethodr  r"  r:   r-   r<   r>   r?   r%  r   r   r   r  r    s        WWWJAAAJ  \(   \: > > [>LHH    r   r  c                   >    e Zd Zed             ZdZdZdZg dZd Z	dS )ControlNetLoaderc                 4    ddt          j        d          fiiS )Nr#   control_net_name
controlnetr  r+   s    r   r-   zControlNetLoader.INPUT_TYPES;  s"    0<3QR^3_3_2bcddr   CONTROL_NETload_controlnetr  )r5  control netcnzload controlnetzcontrolnet loaderc                     t          j        d|          }t          j                            |          }|t          d          |fS )Nr5  zPERROR: controlnet file is invalid and does not contain a valid controlnet model.)r1  r  r   r5  r8  r2   )r5   r4  controlnet_pathr5  s       r   r8  z ControlNetLoader.load_controlnetE  sH    &=lL\]]%55oFF
qrrr}r   N)
r7   r8   r9   r:   r-   r<   r>   r?   rA   r8  r   r   r   r2  r2  :  s[        e e [e $L HH```N    r   r2  c                   6    e Zd Zed             ZdZdZdZd ZdS )DiffControlNetLoaderc                 6    ddt          j        d          fdiS )Nr#   r  r5  )r  r4  r  r+   s    r   r-   z DiffControlNetLoader.INPUT_TYPESM  s1    z3?3QR^3_3_2bd d e 	er   r6  r8  r  c                 r    t          j        d|          }t          j                            ||          }|fS )Nr5  )r1  r  r   r5  r8  )r5   r  r4  r<  r5  s        r   r8  z$DiffControlNetLoader.load_controlnetW  s5    &=lL\]]%55ouMM
}r   N)	r7   r8   r9   r:   r-   r<   r>   r?   r8  r   r   r   r>  r>  L  sP        e e [e $L HH    r   r>  c                   :    e Zd Zed             ZdZdZdZdZd Z	dS )ControlNetApplyc           
      "    ddddddddd	d
fdiS )Nr#   rF   r6  r   rP   rQ   rR   r   rS   rT   )r   control_netimager   r   r+   s    r   r-   zControlNetApply.INPUT_TYPES^  s8    -?,=&1*1s3W[ei3j3j)k    	 r   rF   apply_controlnetTmodel/conditioning/controlnetc                    |dk    r|fS g }|                     dd          }|D ]}|d         |d                                         g}|                                                    ||          }	d|d         v r!|	                    |d         d                    |	|d         d<   d|d         d<   |                    |           |fS )Nr   r   r^   controlTcontrol_apply_to_uncond)movedimrm   set_cond_hintset_previous_controlnetrn   )
r5   r   rD  rE  r   r   control_hintr   rx   c_nets
             r   rF  z ControlNetApply.apply_controlnetk  s    q== ##}}R** 	 	A1qtyy{{#A$$&&44\8LLEAaD  --ad9o>>>#AaDO.2AaD*+HHQKKKKur   N)
r7   r8   r9   r:   r-   r<   r>   r  r?   rF  r   r   r   rB  rB  ]  sR            [  %L!HJ.H    r   rB  c                   H    e Zd Zed             ZdZdZdZdZg dZ	dg fdZ
dS )	ControlNetApplyAdvancedc                 J    dddddddddd	fddddd
d	fddddd
d	fdddidS )NrF   r6  r   rP   rQ   rR   r   rS   rT   r   )r#  r$  rD  rE  r   r   r   r   r   r#   optionalr   r+   s    r   r-   z#ControlNetApplyAdvanced.INPUT_TYPES}  sv    ););,=&1*1s3W[ei3j3j)k/6CPS\_in8o8o.p-4#cZ]gl6m6m,n  #I 
 
 
	r   )r;   r;   )r#  r$  rF  rG  )r5  zapply controlnetzuse controlnetr9  Nc
           	         |dk    r||fS |                     dd          }
i }g }||fD ]}g }|D ]}|d                                         }|                    dd           }||v r	||         }nH|                                                    |
|||f||	          }|                    |           |||<   ||d<   d|d<   |d         |g}|                    |           |                    |           |d         |d         fS )Nr   r   r^   rI  )r   extra_concatFrJ  )rK  rm   rf   rL  rM  rn   )r5   r#  r$  rD  rE  r   r   r   r   rV  rN  cnetsro   r   r   r   r   	prev_cnetrO  rx   s                       r   rF  z(ControlNetApplyAdvanced.apply_controlnet  sG   q==h''}}R**%x0 	 	LA!  aDIIKKEE)T22	%%!),EE',,..<<\8VcepUqwz  JV<  W  WE11)<<<',E)$$)/4+,qT1IJJqMMMMAAr   )r7   r8   r9   r:   r-   r<   r-  r>   r?   rA   rF  r   r   r   rQ  rQ  |  sl          [ 3L+L!H.HXXXNqu  EG            r   rQ  c                   6    e Zd Zed             ZdZdZdZd ZdS )
UNETLoaderc                 B    dt          j        d          fg dddifdiS )Nr#   diffusion_models)rU   
fp8_e4m3fnfp8_e4m3fn_fastfp8_e5m2r   T)	unet_nameweight_dtyper  r+   s    r   r-   zUNETLoader.INPUT_TYPES  s?    L,JK],^,^+a/g/g/gjtvzi{.|    	 r   r  	load_unetr  c                    i }|dk    rt           j        |d<   n0|dk    rt           j        |d<   d|d<   n|dk    rt           j        |d<   t          j        d|          }t
          j                            ||          }|fS )	Nr]  dtyper^  Tfp8_optimizationsr_  r\  )model_options)ri   float8_e4m3fnfloat8_e5m2r1  r  r   r  load_diffusion_model)r5   r`  ra  rf  	unet_pathr  s         r   rb  zUNETLoader.load_unet  s    <''%*%8M'""...%*%8M'"15M-..Z''%*%6M'" 78JIVV	--i}-UUxr   N)	r7   r8   r9   r:   r-   r<   r>   r?   rb  r   r   r   rZ  rZ    sM            [  LH!H    r   rZ  c                   <    e Zd Zed             ZdZdZdZdZd
dZ	d	S )
CLIPLoaderc                 N    t          j        d          fg dfddddgddifid	S )
Ntext_encoders)stable_diffusionstable_cascadesd3stable_audiomochiltxvpixartcosmoslumina2wanhidreamchromaaceomnigen2
qwen_imagehunyuan_imageflux2ovislongcat_image	cogvideoxlenspixeldit	ideogram4)	clip_namerH  rt  rU   rr  r   TrS  r  r+   s    r   r-   zCLIPLoader.INPUT_TYPES  s_    ,8,J?,[,[+^ (s  (s  (s  'v    ')U);j$=O(P    	 r   r  	load_clipr  aA  [Recipes]

stable_diffusion: clip-l
stable_cascade: clip-g
sd3: t5 xxl/ clip-g / clip-l
stable_audio: t5 base
mochi: t5 xxl
cogvideox: t5 xxl (226-token padding)
cosmos: old t5 xxl
lumina2: gemma 2 2B
wan: umt5 xxl
 hidream: llama-3.1 (Recommend) or t5
omnigen2: qwen vl 2.5 3B
lens: gpt-oss-20b
 pixeldit: gemma 2 2B elmro  rU   c                 r   t          t          j        j        |                                t          j        j        j                  }i }|dk    rt          j        d          x|d<   |d<   t          j	        d|          }t          j        
                    |gt          j        d          ||          }|fS )Nrr  load_deviceoffload_devicern  r  
ckpt_pathsr  	clip_typerf  )getattrr   r  CLIPTypeupperSTABLE_DIFFUSIONri   rt  r1  r  r  r  )r5   r  rH  rt  r  rf  	clip_pathr(   s           r   r  zCLIPLoader.load_clip  s    EH-tzz||UX=N=_``	U??MR\Z_M`M``M-(=9I+J 7SS	x!!i[lNklxNyNy  FO  _l!  m  mwr   N)ro  rU   
r7   r8   r9   r:   r-   r<   r>   r?   r@   r  r   r   r   rl  rl    sZ            [  LH!H dK	 	 	 	 	 	r   rl  c                   <    e Zd Zed             ZdZdZdZdZd	dZ	dS )
DualCLIPLoaderc                 v    t          j        d          ft          j        d          fg dfddddgddifid	S )
Nrn  )sdxlrq  fluxhunyuan_videory  r~  hunyuan_video_15
kandinsky5kandinsky5_imagert  newbier{  )
clip_name1
clip_name2rH  rt  rU   rr  r   TrS  r  r+   s    r   r-   zDualCLIPLoader.INPUT_TYPES  sp    -9-KO-\-\,_-9-KO-\-\,_ ({  ({  ({  '~   
 ')U);j$=O(P	    	 r   r  r  r  z[Recipes]

sdxl: clip-l, clip-g
sd3: clip-l, clip-g / clip-l, t5 / clip-g, t5
flux: clip-l, t5
hidream: at least one of t5 or llama, recommended t5 and llama
hunyuan_image: qwen2.5vl 7b and byt5 small
newbie: gemma-3-4b-it, jina clip v2rU   c                    t          t          j        j        |                                t          j        j        j                  }t          j        d|          }t          j        d|          }i }|dk    rt          j	        d          x|d<   |d<   t          j        
                    ||gt          j        d          ||          }	|	fS )Nrn  rr  r  r  r  r  )r  r   r  r  r  r  r1  r  ri   rt  r  r  )
r5   r  r  rH  rt  r  
clip_path1
clip_path2rf  r(   s
             r   r  zDualCLIPLoader.load_clip  s    EH-tzz||UX=N=_``	!8*UU
!8*UU
U??MR\Z_M`M``M-(=9I+Jx!!j*-E[g[x  zF  \G  \G  S\  ly!  z  zwr   N)rU   r  r   r   r   r  r    sZ            [  LH!H HK     r   r  c                   6    e Zd Zed             ZdZdZdZd ZdS )CLIPVisionLoaderc                 4    ddt          j        d          fiiS )Nr#   r  clip_visionr  r+   s    r   r-   zCLIPVisionLoader.INPUT_TYPES  r  r   r  r  r  c                     t          j        d|          }t          j                            |          }|t          d          |fS )Nr  zMERROR: clip vision file is invalid and does not contain a valid vision model.)r1  r  r   r  rp  r2   )r5   r  r  r  s       r   r  zCLIPVisionLoader.load_clip  sG     7yQQ	',,Y77nooo~r   N)	r7   r8   r9   r:   r-   r<   r>   r?   r  r   r   r   r  r    sM            [  $LHH    r   r  c                   6    e Zd Zed             ZdZdZdZd ZdS )CLIPVisionEncodec                     dddddgfdiS )Nr#   r  r   centernone)r  rE  cropr   r+   s    r   r-   zCLIPVisionEncode.INPUT_TYPES  s*    -='1(0&'9&;    	 r   CLIP_VISION_OUTPUTr.   r/   c                 J    d}|dk    rd}|                     ||          }|fS )NTr  Fr  )encode_image)r5   r  rE  r  
crop_imagerD  s         r   r.   zCLIPVisionEncode.encode  s7    
8J))%j)AAyr   Nr   r   r   r   r  r    sM            [ 
 +LH#H    r   r  c                   6    e Zd Zed             ZdZdZdZd ZdS )StyleModelLoaderc                 4    ddt          j        d          fiiS )Nr#   style_model_namestyle_modelsr  r+   s    r   r-   zStyleModelLoader.INPUT_TYPES&  s"    0<3QR`3a3a2deffr   STYLE_MODELload_style_modelr  c                 p    t          j        d|          }t          j                            |          }|fS )Nr  )r1  r  r   r  r  )r5   r  style_model_pathstyle_models       r   r  z!StyleModelLoader.load_style_model/  s4    '>~O_``h//0@AA~r   N)	r7   r8   r9   r:   r-   r<   r>   r?   r  r   r   r   r  r  %  sP        g g [g $L!HH    r   r  c                   <    e Zd ZdgZed             ZdZdZdZd Z	dS )StyleModelApplyzstyle transferc           
      *    ddddddddd	d
fddgfdiS )Nr#   rF   r  r  rP   rQ   rR   r   r   rT   multiply	attn_bias)r   r  clip_vision_outputr   strength_typer   r+   s    r   r-   zStyleModelApply.INPUT_TYPES8  sC    -?,=3K*1s3W[ej3k3k)l0:K/H.K	    	 r   rF   apply_stylemodelzmodel/conditioning/style_modelc                 d   |                     |                              dd                              d          }|dk    r||z  }|j        d         }g }|D ]R}	|	\  }
}|                                }d|v s|dk    r|dk    rt          j        t          j        |dk    r|ndg                    }|                    d	d
          }|d         |d         z  }|
j        d         }|                    dd           }|4t          j	        |
j        d         ||z   ||z   ft
          j
                  }|j        t
          j        k    r2t          j        |                    t
          j
                            }t          j	        |
j        d         ||z   |z   ||z   |z   ft
          j
                  }|d d d |d |f         |d d d |d |f<   |d d d ||d f         |d d d |||z   d f<   |d d |d d |f         |d d ||z   d d |f<   |d d |d |d f         |d d ||z   d ||z   d f<   ||d d d ||||z   f<   ||d d ||z   d |||z   f<   |                    |
j                  |d<   ||d	<   |                    t          j        |
|fd          |g           T|fS )Nr   r^   )	start_dimend_dimra   r  attention_maskr  rQ   attention_mask_img_shape)r^   r^   rd  )get_condflattenr   rh   rm   ri   logTensorrf   rk   float16rd  booltort  rn   rj   )r5   r   r  r  r   r  condrx   c_outr   txtkeysr  mask_ref_sizen_refn_txtr   new_masks                     r   r  z StyleModelApply.apply_stylemodelE  s:   ##$677??!UV?WWaafgahhJ&&HDJqM $	@ $	@AKS$99;;D4''M[,H,HXY\__ "IelQ\@\@\HHbe3f&g&ghh	 $)CV L L%a(=+;;	!xx 0$77< ;	!eemUU]'S[`[hiiiD :++ !9TWW5=W%A%ABBD ;	!eai%6GQRUZIZ'[chcpqqq.2111fuffuf3D.EFUFFUF*+04QQQ5F0GFUFE!GHH,-04QQQ5F0GE!GHHfuf,-26qqq%&&%&&7H2IE!GHHeAghh./5>FUFE%'M127@E!GHHeE!Gm34)1SZ)@)@%&3@/0LL%)S$KQ777>????xr   N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r  r   r   r   r  r  5  sU        &'N    [  %L!H/H- - - - -r   r  c                   6    e Zd Zed             ZdZdZdZd ZdS )unCLIPConditioningc           
      0    ddddddddd	fdd
d
ddd	fdiS )Nr#   rF   r  rP   rQ   g      $r   rS   rT   rR   )r   r  r   noise_augmentationr   r+   s    r   r-   zunCLIPConditioning.INPUT_TYPESu  sL    -?3K*1s5Y]gk3l3l)m4;UXadnr=s=s3t    	 r   rF   	apply_admr/   c                 V    |dk    r|fS t          j        |d|||dgid          }|fS )Nr   unclip_conditioning)r  r   r  T)rn   r   )r5   r   r  r   r  r   s         r   r  zunCLIPConditioning.apply_adm  sr    q== ##0@U  pB  PX  pB  YC  YC  XD  @E  NR  S  S  Sur   N)	r7   r8   r9   r:   r-   r<   r>   r?   r  r   r   r   r  r  t  sM            [  %LH#H    r   r  c                   6    e Zd Zed             ZdZdZdZd ZdS )GLIGENLoaderc                 4    ddt          j        d          fiiS )Nr#   gligen_namegligenr  r+   s    r   r-   zGLIGENLoader.INPUT_TYPES  s     ml.LX.V.V-YZ[[r   GLIGENload_gligenr  c                 p    t          j        d|          }t          j                            |          }|fS )Nr  )r1  r  r   r  r  )r5   r  gligen_pathr  s       r   r  zGLIGENLoader.load_gligen  s1    "9(KPP%%k22yr   N)	r7   r8   r9   r:   r-   r<   r>   r?   r  r   r   r   r  r    sP        \ \ [\ LHH    r   r  c                   6    e Zd Zed             ZdZdZdZd ZdS )GLIGENTextBoxApplyc                     ddddddddfdd	d
t           d
dfdd	d
t           d
dfdddt           d
dfdddt           d
dfdiS )Nr#   rF   r  r  r)   T)r$   r%   r   r   r   rT   r   )rY   r(   gligen_textbox_modelr'   r   r   r   r   r   r+   s    r   r-   zGLIGENTextBoxApply.INPUT_TYPES  s    0B&06B'/tW[1\1\&](-2aP^hi/j/j'k).BqQ_ij0k0k(l$)q>cd+e+e#f$)q>cd+e+e#f    	 r   rF   rn   zmodel/conditioning/gligenc	                 h   g }	|                     |                    |          d          \  }
}|D ]~}|d         |d                                         g}||dz  |dz  |dz  |dz  fg}g }d|d         v r|d         d         d         }d|||z   f|d         d<   |	                    |           |	fS )	Nunprojected)return_pooledr   r^   r   r  r`   position)encode_from_tokensr3   rm   rn   )r5   rY   r(   r  r'   r   r   r   r   r   r  cond_pooledr   rx   position_paramsprevs                   r   rn   zGLIGENTextBoxApply.append  s     33DMM$4G4GWd3eek  	 	A1qtyy{{#A +Vq[%1*a1faSTfUVOD1Q4tH~a((*>@VWAaDNHHQKKKKur   Nr   r   r   r   r  r    sM        	  	  [	  %LH*H    r   r  c                   H    e Zd Zed             ZdZdZdZdZdZ	g dZ
dd	Zd
S )EmptyLatentImagec           	      T    ddddt           dddfdddt           dddfdd	d	d
ddfdiS )Nr#   r   r      r   z)The width of the latent images in pixels.r  z*The height of the latent images in pixels.r^   r   z)The number of latent images in the batch.rU   rV   rW   r&   )r   r   
batch_sizer   r+   s    r   r-   zEmptyLatentImage.INPUT_TYPES  s     SN\]  kV  "W  "W  X c"^]^  lX  #Y  #Y  Z$!Ad  XC  'D  'D  E 
 	
r   r   )zThe empty latent image batch.generater   zFCreate a new batch of empty latent images to be denoised via sampling.)emptyzempty latentz
new latentzcreate latentzblank latentblankr^   c                     t          j        |d|dz  |dz  gt          j                                        t          j                                                  }|ddfS )Nr   r   rt  rd  )r   downscale_ratio_spacial)ri   rk   r   r   intermediate_deviceintermediate_dtype)r5   r   r   r  r   s        r   r   zEmptyLatentImage.generate  sz    j!Vq[%1*EeNdNxNxNzNz  CH  CY  Cl  Cl  Cn  Cn  o  o  o"qAADDr   Nr^   )r7   r8   r9   r:   r-   r<   r=   r>   r?   r@   rA   r   r   r   r   r  r    sm        
 
 [
 L8OHHZKfffNE E E E E Er   r  c                   >    e Zd Zg dZed             ZdZdZdZd Z	dS )LatentFromBatch)zselect from batchzpick latentzbatch subsetc                 @    ddddt            t           dfdddddfdiS )	Nr#   r   r   r   rU   rV   rW   r^   r   )r   batch_indexlengthr   r+   s    r   r-   zLatentFromBatch.INPUT_TYPES  sC    .3N?cq5r5r-s).AaPR0S0S(T    ! 	!r   r   	frombatchmodel/latent/batchc                 L   |                                 }|d         }|dk     r||j        d         z  }t          dt          |j        d         dz
  |                    }t          |j        d         |z
  |          }||||z                                            |d<   d|v r|d         }|j        d         dk    r|                                |d<   n|j        d         |j        d         k     rV|                    t          j        |j        d         |j        d         z            ddd          d |j        d                  }||||z                                            |d<   d|vr!d t          |||z             D             |d<   n|d         |||z            |d<   |fS )Nr   r   r^   r  r  c                     g | ]}|S r   r   rg  r   s     r   rj  z-LatentFromBatch.frombatch.<locals>.<listcomp>  s    RRRaRRRr   )	rm   rh   rW   rV   r  repeatr  r  rg   )r5   r   r  r  r,   s_inmaskss          r   r  zLatentFromBatch.frombatch  s   LLNNy!??4:a=(K!SA!2K@@AATZ][0&99Kf(<<=CCEE)7""L)E{1~"""'++--,;q>DJqM11!LL4:a=5;q>3Q)R)RTUWXZ[\\]k^b^hij^k]klE"'K&4H(H"I"O"O"Q"Q,!!RR5k&>P+Q+QRRRAm&}5k+PVBV6VWAmtr   N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r  r   r   r   r
  r
    sX        IIIN! ! [!
 LH#H    r   r
  c                   >    e Zd ZddgZed             ZdZdZdZd Z	dS )	RepeatLatentBatchzduplicate latentzclone latentc                     dddddddfdiS )Nr#   r   r   r^   r   r  )r   amountr   r+   s    r   r-   zRepeatLatentBatch.INPUT_TYPES  s.    ).AaPR0S0S(T    ! 	!r   r   r  r  c                    |                                 |d         }|                    |fd|j        dz
  z  z             d<   d|v r|d         j        d         dk    r|d         }|j        d         |j        d         k     rb|                    t	          j        |j        d         |j        d         z            fd|j        dz
  z  z             d |j        d                  }|d                             |fd|d         j        dz
  z  z             d<   dv rWt          d                   t          d                   z
  dz   d         fdt          d|          D             z   d<   fS )Nr   r  r^   r  r   r  c                 6    g | ]}d          D ]
}||z  z   S )r  r   )rg  rr   r   offsetr,   s      r   rj  z,RepeatLatentBatch.repeat.<locals>.<listcomp>
  s7    2x2x2xghivgw2x2xbc1F
3C2x2x2x2xr   )	rm   r  ndimrh   r  r  rW   rV   rg   )r5   r   r  r  r  r  r,   s        @@r   r  zRepeatLatentBatch.repeat  s   LLNNy!{{F9	A0F#GHH)7""w|'<'B1'E'I'IL)E{1~
1--di
1A0N&O&O%QUY]b]gjk]kUl%mnno}ptpz{|p}o}~%l3::F9PWXdPePjmnPnHo;pqqAlOA=)**S=1A-B-BBQFF /2x2x2x2x2xUSTV\M]M]2x2x2xxAmtr   N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r  r   r   r   r  r    sW        (.9N! ! [! LH#H    r   r  c                   N    e Zd ZddgZg dZddgZed             ZdZdZ	d	Z
d
 ZdS )LatentUpscaleenlarge latentresize latentnearest-exactr  r   bicubicbislerpdisabledr  c           
      ^    dd| j         fdddt          ddfdddt          ddf| j        fdiS )	Nr#   r   r   r   r   r   rT   )r   upscale_methodr   r   r  upscale_methodsr   crop_methodsr+   s    r   r-   zLatentUpscale.INPUT_TYPES  sW    IZH\(-3qQ_ij/k/k'l).CR`jk0l0l(m'(~&79 9 : 	:r   r   upscaler   c                 d   |dk    r
|dk    r|}n|                                 }|dk    rVt          d|          }t          dt          |d         j        d         |z  |d         j        d         z                      }n||dk    rVt          d|          }t          dt          |d         j        d         |z  |d         j        d         z                      }n t          d|          }t          d|          }t          j                            |d         |dz  |dz  ||          |d<   |fS )Nr   r   r   r   r   r   )rm   rW   r  rh   r   rW  common_upscale)r5   r   r)  r   r   r  r,   s          r   r-  zLatentUpscale.upscale  s&   A::&A++AAAzzRBgi&8&>r&BV&KgV_N`NfgiNj&j k kll1BRwy'9'?'Ce'KgV_N`NfgiNj'j!k!kllBR ;55gi6H%ST*V\`aVacqswxxAiLtr   N)r7   r8   r9   rA   r+  r,  r:   r-   r<   r>   r?   r-  r   r   r   r   r     sl        &8NQQQO)L: : [:
 LHH    r   r   c                   F    e Zd Zg dZg dZed             ZdZdZdZ	d Z
dS )	LatentUpscaleBy)r!  r"  zscale latentr#  c           	      ,    dd| j         fddddddfdiS )	Nr#   r   rP   g      ?rS          @rT   )r   r)  scale_byr+  r+   s    r   r-   zLatentUpscaleBy.INPUT_TYPES6  s=    IZH\+2DY\fj4k4k*lo o p 	pr   r   r-  r   c                    |                                 }t          |d         j        d         |z            }t          |d         j        d         |z            }t          j                            |d         |||d          |d<   |fS )Nr   r   r   r'  )rm   r  rh   r   rW  r/  )r5   r   r)  r4  r,   r   r   s          r   r-  zLatentUpscaleBy.upscale?  s|    LLNNgi(.r2X=>>wy)/3h>??{11')2DeVUceopp)tr   N)r7   r8   r9   rA   r+  r:   r-   r<   r>   r?   r-  r   r   r   r1  r1  1  sf        HHHNQQQOp p [p LHH    r   r1  c                   6    e Zd Zed             ZdZdZdZd ZdS )LatentRotatec                     ddg dfdiS )Nr#   r   )r  z
90 degreesz180 degreesz270 degrees)r   rotationr   r+   s    r   r-   zLatentRotate.INPUT_TYPESG  s'    +_+_+_*a    ! 	!r   r   rotatemodel/latent/transformc                    |                                 }d}|                    d          rd}n/|                    d          rd}n|                    d          rd}t          j        |d         |ddg	          |d<   |fS )
Nr   90r^   180r`   270r   r   )r  dims)rm   r  ri   rot90)r5   r   r:  r,   	rotate_bys        r   r;  zLatentRotate.rotateQ  s    LLNN	t$$ 	II  '' 	II  '' 	I{79#5!QPPP)tr   N)	r7   r8   r9   r:   r-   r<   r>   r?   r;  r   r   r   r8  r8  F  sM        ! ! [! LH'H    r   r8  c                   <    e Zd ZdgZed             ZdZdZdZd Z	dS )
LatentFlipzmirror latentc                     ddddgfdiS )Nr#   r   zx-axis: verticallyzy-axis: horizontally)r   flip_methodr   r+   s    r   r-   zLatentFlip.INPUT_TYPESa  s'    /CE[.\-^    ! 	!r   r   flipr<  c                    |                                 }|                    d          r!t          j        |d         dg          |d<   n5|                    d          r t          j        |d         dg          |d<   |fS )Nr   r   r`   )rA  r   r   )rm   r  ri   rH  )r5   r   rG  r,   s       r   rH  zLatentFlip.flipk  s    LLNN!!#&& 	D :gi&8sCCCAiLL##C(( 	D :gi&8sCCCAiLtr   N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   rH  r   r   r   rE  rE  ^  sU        %&N! ! [! LH'H    r   rE  c                   @    e Zd Zg dZed             ZdZdZdZd
dZ	d	S )LatentComposite)zoverlay latentzlayer latentzpaste latentc                 ^    ddddddt           ddfdddt           ddfdddt           ddfdiS )Nr#   r   r   r   r   rT   )
samples_tosamples_fromr   r   featherr   r+   s    r   r-   zLatentComposite.INPUT_TYPESw  s^    K.9$)q>cd+e+e#f$)q>cd+e+e#f*/QqQ_ij1k1k)l	    ! 	!r   r   	compositer   normalr   c           	         |dz  }|dz  }|dz  }|                                 }|d                                         }|d         }|d         }|dk    r]|d d d d d |j        d         |z
  d |j        d         |z
  f         |d d d d |||j        d         z   |||j        d         z   f<   n#|d d d d d |j        d         |z
  d |j        d         |z
  f         }t          j        |          }	t          |          D ]%}
|dk    r(|	d d d d |
d|
z   d d fxx         d|z  |
dz   z  z  cc<   ||j        d         z   |j        d         k     rD|	d d d d |	j        d         dz
  |
z
  |	j        d         |
z
  d d fxx         d|z  |
dz   z  z  cc<   |dk    r(|	d d d d d d |
d|
z   fxx         d|z  |
dz   z  z  cc<   ||j        d         z   |j        d         k     rD|	d d d d d d |	j        d         dz
  |
z
  |	j        d         |
z
  fxx         d|z  |
dz   z  z  cc<   't          j        |	          |	z
  }|d d d d d |j        d         |z
  d |j        d         |z
  f         |	z  |d d d d |||j        d         z   |||j        d         z   f         |z  z   |d d d d |||j        d         z   |||j        d         z   f<   ||d<   |fS )Nr   r   r   r`   r   r^   rQ   )rm   r  rh   ri   	ones_likerg   )r5   rM  rN  r   r   composite_methodrO  samples_outr,   r   r   rev_masks               r   rP  zLatentComposite.composite  s   !VFQ, oo''y!''))	*
#I.a<<IUVWVWVWXYXYXYZr[e[klm[nqr[rZr  uMu  vF  GH  vI  LM  vM  uM  WM  JNAaaa!Al(+++Aa0B10E.E,EEFF'!!!,DZ-=a-@1-D,DF^zGWXYGZ]^G^F^(^_L?<00D7^^ 	d 	d66111QqsU111%%%3w;1q5*AB%%%|)!,,z/?/BBB111TZ]A-q0$*Q-/A!!!CDDD#g+Z[^_Z_I`aDDD66111QQQq1u%%%3w;1q5*AB%%%|)!,,z/?/BBB111QQQtz!}a/!3TZ]A5EEEFFFCPWK\]`a\aKbcFFFt,,t3HIUVWVWVWXYXYXYZr[e[klm[nqr[rZr  uMu  vF  GH  vI  LM  vM  uM  WM  JN  QU  JU  XY  Z[  Z[  Z[  \]  \]  \]  ^_  `a  bn  bt  uv  bw  `w  ^w  xy  z{  |H  |N  OP  |Q  zQ  xQ  ZQ  XR  U]  X]  J]Aaaa!Al(+++Aa0B10E.E,EEF!"I~r   N)rQ  r   )
r7   r8   r9   rA   r:   r-   r<   r>   r?   rP  r   r   r   rK  rK  t  s]        GGGN! ! [! LHH     r   rK  c                   P    e Zd ZddgZed             ZdZdZdZdde	d	e
fd
Zd ZdS )LatentBlendzmix latentszinterpolate latentsc           	           dddddddddfd	iS )
Nr#   r   rP   r
  r   r^   rS   rT   )samples1samples2blend_factorr   r+   s    r   r-   zLatentBlend.INPUT_TYPES  s=    ##$	' ' 	
 	
 	 		r   r   blendr   rQ  r\  
blend_modec                    |                                 }|d         }|d         }|j        |j        k    rj|                    dddd           t          j                            ||j        d         |j        d         dd          }|                    dddd           |                     |||          }||z  |d|z
  z  z   }||d<   |fS )	Nr   r   r   r^   r`   r%  r  r  )rm   rh   permuter   rW  r/  r^  )r5   rZ  r[  r\  r^  rU  samples_blendeds          r   r]  zLatentBlend.blend  s    mmooI&I&>X^++Q1a((({11(HN1<Mx~^_O`bkrz1{{HQ1a(((//(HjII"\1Oq<GW4XX!0I~r   c                 6    |dk    r|S t          d|           )NrQ  zUnsupported blend mode: )
ValueError)r5   img1img2r  s       r   r^  zLatentBlend.blend_mode  s)    8K>>>???r   N)rQ  )r7   r8   r9   rA   r:   r-   r<   r>   r?   rx  strr]  r^  r   r   r   rX  rX    s        #%:;N
 
 [
 LHH U      @ @ @ @ @r   rX  c                   >    e Zd ZddgZed             ZdZdZdZd Z	dS )	
LatentCropztrim latentz
cut latentc                 v    dddddt           ddfdddt           ddfdddt           ddfdddt           ddfd	iS )
Nr#   r   r   r   r   r   rT   r   )r   r   r   r   r   r   r+   s    r   r-   zLatentCrop.INPUT_TYPES  sq    (-3rR`jk/l/l'm).CSakl0m0m(n$)q>cd+e+e#f$)q>cd+e+e#f	    ! 	!r   r   r  r<  c                 :   |                                 }|d         }|dz  }|dz  }||j        d         dz
  k    r|j        d         dz
  }||j        d         dz
  k    r|j        d         dz
  }|dz  }|dz  }||z   }	||z   }
|d d d d ||
||	f         |d<   |fS )Nr   r   r   r`   )rm   rh   )r5   r   r   r   r   r   r,   
new_height	new_widthto_xto_ys              r   r  zLatentCrop.crop  s    LLNN)$!VF a 1$%%a 1$Aa 1$%%a 1$Aq[
QJ	1}A~qqq1T61T612)tr   N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   r  r   r   r   rh  rh    sW        #\2N! ! [! LH'H    r   rh  c                   6    e Zd Zed             ZdZdZdZd ZdS )SetLatentNoiseMaskc                     ddddiS )Nr#   r   r   )r   r   r   r+   s    r   r-   zSetLatentNoiseMask.INPUT_TYPES  s    &/    ! 	!r   r   set_maskr  c                     |                                 }|                    dd|j        d         |j        d         f          |d<   |fS )Nr   r^   r   r  )rm   r   rh   )r5   r   r   r,   s       r   rr  zSetLatentNoiseMask.set_mask  s@    LLNN,,Atz"~tz"~'NOO,tr   N)	r7   r8   r9   r:   r-   r<   r>   r?   rr  r   r   r   rp  rp    sM        ! ! [! LH%H    r   rp  rQ   Fc                    |d         }t           j                            | ||                    dd           |                    dd                     }|
r5t	          j        |                                |j        |j        d          }n/d|v r|d         nd }t           j        	                    |||          }d }d|v r|d         }t          j        | |          }t           j        j         }t           j                            | |||||||||	|
|||||||          }|                                }|                    dd            |                    dd            ||d<   |fS )	Nr   r  downscale_ratio_temporalrr  )rd  layoutrt  r  r  )	denoisedisable_noise
start_step	last_stepforce_full_denoiser  callbackdisable_pbarseed)r   samplefix_empty_latent_channelsrf   ri   rk   r  rd  rv  prepare_noiselatent_previewprepare_callbackrW  PROGRESS_BAR_ENABLEDrm   pop)r  r~  stepscfgsampler_name	schedulerr#  r$  r   rw  rx  ry  rz  r{  latent_imagenoise
batch_indsr  r|  r}  r   ro   s                         r   common_ksamplerr    s   )$L<99%vzzZsuyOzOz  }C  }G  }G  Hb  dh  }i  }i  j  jL KL--//|7IR^Rensttt.;v.E.EVM**4
**<zJJJvL)
.ue<<H{77Ll!!%sL)U]_giu*1[eqz5GT^iq  AM  TX " Y YG ++--CGG%t,,,GG&---C	N7Nr   c                   H    e Zd Zed             ZdZdZdZdZdZ	g dZ
dd	Zd
S )KSamplerc                     ddddifdddddd	d
fddddddfddddddddft           j        j        j        ddift           j        j        j        ddifdddifdddifdddifdddddd d!fd"
iS )#Nr#   r  r&   z.The model used for denoising the input latent.r   r       Tz,The random seed used for creating the noise.)rU   rV   rW   control_after_generater&      r^   '  z2The number of steps used in the denoising process.r  rP   r3  rR   r  皙?rS   zThe Classifier-Free Guidance scale balances creativity and adherence to the prompt. Higher values result in images more closely matching the prompt however too high values will negatively impact quality.)rU   rV   rW   rX   r  r&   zhThe algorithm used when sampling, this can affect the quality, speed, and style of the generated output.zHThe scheduler controls how noise is gradually removed to form the image.r;   zLThe conditioning describing the attributes you want to include in the image.zNThe conditioning describing the attributes you want to exclude from the image.r   zThe latent image to denoise.rQ   zThe amount of denoising applied, lower values will maintain the structure of the initial image allowing for image to image sampling.r  )
r  r~  r  r  r  r  r#  r$  r  rw  r   samplersr  SAMPLERS
SCHEDULERSr+   s    r   r-   zKSampler.INPUT_TYPES  s<    !I/_#`aAa@Rnr  @n  !o  !o  pR%  UI  "J  "J  KSUSVae  s@  "A  "A  B!&!8!AI  Pz  D{  !|#n5@9  OY  CZ  [+i  :H  .I  J+i  :J  .K  L!)I7U+V W#SVZ  hn  &o  &o  p 
 	
r   r   )zThe denoised latent.r  model/samplingzXUses the provided model, positive and negative conditioning to denoise the latent image.)samplerr  r   rw  diffusetxt2imgimg2imgrQ   c                 4    t          |||||||||	|

  
        S )N)rw  r  )r5   r  r~  r  r  r  r  r#  r$  r  rw  s              r   r  zKSampler.sample1  s9    udE3iQY[ceq  |C  D  D  D  	Dr   NrQ   )r7   r8   r9   r:   r-   r<   r=   r>   r?   r@   rA   r  r   r   r   r  r    sm        
 
 [
  L/OHHlKbbbND D D D D Dr   r  c                   8    e Zd Zed             ZdZdZdZddZdS )	KSamplerAdvancedc                     ddddgddifdddd	dd
fdddddfdddddddft           j        j        j        ft           j        j        j        fdddddddddfddddddfddgddifdiS )Nr#   r  enabledisabler   Tr   r   r  )rU   rV   rW   r  r  r^   r  r  rP   r3  rR   r  r  rS   )rU   rV   rW   rX   r  rF   r   )rU   rV   rW   r   )r  	add_noise
noise_seedr  r  r  r  r#  r$  r  start_at_stepend_at_stepreturn_with_leftover_noiser  r+   s    r   r-   zKSamplerAdvanced.INPUT_TYPES5  s    (#+Y"7*d9K!L#(aJ\x|*}*}"~#Ae%L%LM#SWZei%j%jk%*^%<%E$H"'."9"D!G 2 2$0&+1U`d-e-e%f$)uQubf+g+g#h4=x3H:W[J\2]  	r   r   r  r  rQ   c                 d    d}|dk    rd}d}|dk    rd}t          ||||||||	|
|||||          S )NTr  Fr  )rw  rx  ry  rz  r{  r  )r5   r  r  r  r  r  r  r  r#  r$  r  r  r  r  rw  r{  rx  s                    r   r  zKSamplerAdvanced.sampleM  s    !%11!&	!! Muj%lIW_aikw  BI  Yf  s@  LW  l~        	r   Nr  )	r7   r8   r9   r:   r-   r<   r>   r?   r  r   r   r   r  r  4  sX          [$ LHH     r   r  c                   R    e Zd Zd Zed             ZdZdZdZdZ	dZ
dZg d	ZddZdS )	SaveImagec                 `    t          j                    | _        d| _        d| _        d| _        d S )NrD  rA  r   )r1  r2  r3  rH  prefix_appendcompress_levelr4  s    r   r5  zSaveImage.__init__W  s/    &;==	r   c                 *    dddifddddfdd	d
ddS )Nr   r&   zThe images to save.r)   r?  zThe prefix for the file to save. This may include formatting information such as %date:yyyy-MM-dd% or %Empty Latent Image.width% to include values from nodes.r"  )r   r7  r8  r9  r:  r<  r   r+   s    r   r-   zSaveImage.INPUT_TYPES]  sZ     #Y0E$FG$,)  Qq  /r  /r  $s 
 #_ 
 
 	
r   r   save_imagesTrE  Basicsz8Saves the input images to your ComfyUI output directory.)r>  z
save imagezexport imageoutput imagezwrite imagedownloadr?  Nc           	         || j         z  }t          j        || j        |d         j        d         |d         j        d                   \  }}}}}t                      }	t          |          D ]y\  }
}d|                                                                z  }t          j
        t          j        |dd                              t          j                            }d }t          j        smt#                      }|(|                    dt'          j        |                     |3|D ]0}|                    |t'          j        ||                              1|                    dt-          |
                    }| d|dd	}|                    t0          j                            ||          || j        
           |	                    ||| j        d           |dz  }{dd|	iiS )Nr   r^        o@   r0   z%batch_num%rB  rC  z_.png)pnginfor  rE  rL  r   )r  r1  rN  r3  rh   list	enumeraterr  numpyr   	fromarraynpr(   astypeuint8r   rQ  r   add_textrO  rP  replacerf  r>  rR  rS  rT  r  rn   rH  )r5   r   r7  r0   r;  rY  rF  rZ  rG  r]  batch_numberrE  rr   imgrK  r   filename_with_batch_numr\  s                     r   r  zSaveImage.save_imagess  s1   4--LXLlm|  C  N  PV  WX  PY  P_  `a  Pb  dj  kl  dm  ds  tu  dv  Mw  MwIHgy/&&%.v%6%6 	 	!\5uyy{{((***A/"'!Q"4"4";";BH"E"EFFCH( K"99%%%h
60B0BCCC ,* K K ))!TZa8H-I-IJJJJ&.&6&6}c,FWFW&X&X#-AAAAAADHHRW\\"4d;;X^b^qHrrrNN &	    
 qLGG7,..r   r^  )r7   r8   r9   r5  r:   r-   r<   r>   r_  r?   rL   r@   rA   r  r   r   r   r  r  V  s~              	
 	
 [	
 LHKH"LKfffN/ / / / / /r   r  c                   2    e Zd Zd Zg dZed             ZdS )PreviewImagec                     t          j                    | _        d| _        dd                    d t          d          D                       z   | _        d| _        d S )Ntemp_temp_rA  c              3   >   K   | ]}t          j        d           V  dS )abcdefghijklmnopqrstupvxyzN)randomchoicer  s     r   	<genexpr>z(PreviewImage.__init__.<locals>.<genexpr>  s.      /n/n`a>Z0[0[/n/n/n/n/n/nr   r   r^   )r1  get_temp_directoryr3  rH  rT  rg   r  r  r4  s    r   r5  zPreviewImage.__init__  sY    &9;;	%/n/nejklemem/n/n/n(n(nnr   )previewzpreview imagez
show imagez
view imagezdisplay imagezimage viewerc                     ddiddddS )Nr   r   r8  r9  r:  r<  r   r+   s    r   r-   zPreviewImage.INPUT_TYPES  s&     {-%-PP  	r   N)r7   r8   r9   r5  rA   r:   r-   r   r   r   r  r    sJ              onnN  [  r   r  c                   n    e Zd Zed             ZdZdZg dZdZdZ	d Z
ed             Zed	             Zd
S )	LoadImagec                     t          j                    fdt          j                  D             }t          j        |dg          }ddt          |          ddifiiS )Nc                     g | ]A}t           j                            t           j                            |                    ?|BS r   )rR  rS  rd  rT  rf  s     r   rj  z)LoadImage.INPUT_TYPES.<locals>.<listcomp>  s>    ```qRW^^BGLLQZ\]D^D^5_5_````r   rE  r#   image_uploadT)r1  rk  rR  rl  filter_files_content_typesrm  rn  s     @r   r-   zLoadImage.INPUT_TYPES  sm     466	````BJy11```7yIIve}}~t.DEF 	r   rE  r  )z
load imagez
open imagezimport imagezimage inputzupload imagez
read imagezimage loaderr   r   
load_imagec                    t          j        |          }t          j                                        }t          j                                        }t          j        |                                          }|j	        j
        d         dk    rr|j	                            ||          |j        %d|j        d         z
                      ||          n)t          j        |j	        j
        d         ddf||          fS t          j        t"          j        |          }g }g }d\  }	}
t'          j        |          D ]}t          j        t*          j        |          }|                    d          }t1          |          dk    r|j        d         }	|j        d	         }
|j        d         |	k    s|j        d	         |
k    rt5          j        |                              t4          j                  d
z  }t          j        |          d         }d|                                v r_t5          j        |                     d                                        t4          j                  d
z  }dt          j        |          z
  }n!t          j        dt          j        d          }|!                    |                    |                     |!                    |"                    d                              |                     t          j#        |d          }t          j#        |d          }|                    ||          |                    ||          fS )Nr   r  rQ   ).r   r   rd  rt  )NNRGBr^   r  r   A)r   r   rr  r  ra   )$r1  ru  r   r   r  r  r   VideoFromFileget_componentsr   rh   r  alphari   rk   r   pillowr   r  r   Iteratorr   exif_transposeconvertrc   r  r  arrayr  float32
from_numpygetbands
getchannelrn   r   rj   )r5   rE  r  rd  rt  
componentsr  output_imagesoutput_maskswhrr   r   output_imageoutput_masks                  r   r  zLoadImage.load_image  sS   !8??
&99;;';;==,Z88GGII
"1%))%((e(DD  KU  K[  KgsZM]^eMfGfFjFjrx  AFFj  GG  GG  GG  mr  mx  zD  zK  zQ  RS  zT  VX  Z\  y]  ej  sy  mz  mz  mz  {  { !%*j991',, 	C 	CA#H$;Q??AIIe$$E=!!Q&&JqMJqMz!}!!UZ]a%7%7HUOO**2:66>E$U++E2Eajjll""xS 1 12299"*EEME,T222{85=OOO  !6!6777q 1 1 4 45 4 A ABBBByA666i!444vU;;[^^SYaf^=g=ghhr   c                 :   t          j        |          }t          j                    }t	          |d          5 }|                    |                                           d d d            n# 1 swxY w Y   |                                                                S r|  r~  )r,   rE  r  r  rh  s        r   r  zLoadImage.IS_CHANGED  s    !8??
N*d## 	qHHQVVXX	 	 	 	 	 	 	 	 	 	 	 	 	 	 	xxzz~~r  c                 X    t          j        |          sd                    |          S dS )NzInvalid image file: {}Tr  )r,   rE  s     r   r  zLoadImage.VALIDATE_INPUTS  s.    5e<< 	:+225999tr   N)r7   r8   r9   r:   r-   r?   rL   rA   r<   r>   r  r  r  r   r   r   r  r    s          [ H"~~~N$LH*i *i *iX     [    [  r   r  c                   r     e Zd ZdZg dZg dZe fd            ZdZdZ	dZ
 fdZe fd	            Z xZS )
LoadImageMaskImage Tools)zimport maskz
alpha maskzchannel mask)r  redgreenbluec                 p    t                                                      }di |d         d| j        fiiS )Nr#   channel)superr-   _color_channels)r,   types	__class__s     r   r-   zLoadImageMask.INPUT_TYPES  sL    ##%% 
#A-0 
 	
r   rE  r   load_image_maskc                    t                                          |          \  }}|d                                         }|dk    r|fS dddd                    |d          }||j        d         k     r|d|f                                         fS t          j        |j        d d         |j        |j	                  }|fS )	Nr   r  r^   r`   )RGBr   .r  )
r  r  r  rf   rh   r  ri   rk   rd  rt  )	r5   rE  r  image_tensormask_tensorr   channel_idx
empty_maskr   s	           r   r  zLoadImageMask.load_image_mask   s    $)GG$6$6u$=$=!kAJ88>!AA..221a88+B/// k!1288::<<"3B3'"(#*  J
 = r   c                 F    t                                          |          S r   )r  r  )r,   rE  r  r   s      r   r  zLoadImageMask.IS_CHANGED  s    ww!!%(((r   )r7   r8   r9   rL   rA   r  r:   r-   r?   r<   r>   r  r  __classcell__)r   s   @r   r  r    s        'BBBN777O
 
 
 
 [
 HL H! ! ! ! !& ) ) ) ) [) ) ) ) )r   r  c                   8    e Zd ZddgZed             ZdZdZdZdS )LoadImageOutputr  zprevious generationc           	      "    dddddddddd	fiiS )
Nr#   rE  COMBOTrD  z/internal/files/outputfirst)routerefresh_buttoncontrol_after_refresh)r  image_folderremoter   r+   s    r   r-   zLoadImageOutput.INPUT_TYPES  sC     '$($,!9*.18 $ $ 

 	
r   zLoad an image from the output folder. When the refresh button is clicked, the node will update the image list and automatically select the first image, allowing for easy iteration.Tr  N)	r7   r8   r9   rA   r:   r-   r@   EXPERIMENTALr>   r   r   r   r  r    sD        $&;<N
 
 [
 IKLHHHr   r  c                   R    e Zd Zg dZddgZed             ZdZdZdZ	dZ
g d	Zd
 ZdS )
ImageScaler$  r  r   r%  lanczosr'  r  c           
      ^    dd| j         fdddt          ddfdddt          ddf| j        fdiS )	Nr#   r   r   r   r   r^   rT   )rE  r)  r   r   r  r*  r+   s    r   r-   zImageScale.INPUT_TYPES4  sW    zaFWEY(-3qQ_ij/k/k'l).CR`jk0l0l(m'(~&79 9 : 	:r   r   r-  image/upscalingr  )resizezresize imagezscale imagezimage resizezoomzzoom inzchange sizec                    |dk    r	|dk    r|}n|                     dd          }|dk    r:t          dt          |j        d         |z  |j        d         z                      }n?|dk    r9t          dt          |j        d         |z  |j        d         z                      }t          j                            |||||          }|                     dd          }|fS )Nr   r   r^   r   r`   )rK  rW   r  rh   r   rW  r/  )r5   rE  r)  r   r   r  r,   r   s           r   r-  zImageScale.upscaleA  s    A::&A++AAmmBq))GzzAuW]1%5%>qAQ%QRRSS1QgmA&6&>qAQ&Q R RSS**7E6>SWXXA		!BAtr   N)r7   r8   r9   r+  r,  r:   r-   r<   r>   r?   rL   rA   r-  r   r   r   r  r  0  ss        QQQO)L: : [:
 LH H'pppN    r   r  c                   B    e Zd ZdZg dZed             ZdZdZdZ	d Z
dS )	ImageScaleByr  r  c           	      ,    dd| j         fddddddfdiS )	Nr#   r   rP   rQ   rS   r3  rT   )rE  r)  r4  r5  r+   s    r   r-   zImageScaleBy.INPUT_TYPEST  s=    zaFWEY+2DY\fj4k4k*lo o p 	pr   r   r-  r  c                    |                     dd          }t          |j        d         |z            }t          |j        d         |z            }t          j                            ||||d          }|                     dd          }|fS )Nr   r^   r   r`   r'  )rK  r  rh   r   rW  r/  )r5   rE  r)  r4  r   r   r   r,   s           r   r-  zImageScaleBy.upscale]  s|    --1%%gmA&122w}Q'(233K&&wv~zZZIIaOOtr   N)r7   r8   r9   rL   r+  r:   r-   r<   r>   r?   r-  r   r   r   r!  r!  P  sa        'QQQOp p [p LH H    r   r!  c                   @    e Zd ZdgZdZed             ZdZdZdZ	d Z
dS )	ImageInvertzreverse colorsr  c                     dddiiS )Nr#   rE  r   r   r+   s    r   r-   zImageInvert.INPUT_TYPESi  s    gz233r   r   invertzimage/colorc                     d|z
  }|fS )NrQ   r   )r5   rE  r,   s      r   r'  zImageInvert.invertr  s    %Ktr   N)r7   r8   r9   rA   rL   r:   r-   r<   r>   r?   r'  r   r   r   r%  r%  e  s[        &'N'4 4 [4 LHH    r   r%  c                   B    e Zd Zg dZed             ZdZdZdZdZ	d Z
dS )	
ImageBatch)zcombine imageszmerge imageszstack imagesc                     ddddiS )Nr#   r   )image1image2r   r+   s    r   r-   zImageBatch.INPUT_TYPESy  s    
jIIJJr   r   batchzimage/batchTc                 L   |j         d         |j         d         k    rm|j         d         |j         d         k    r)t          j        j                            |ddd          }n(t          j        j                            |ddd          }|j         dd          |j         dd          k    rat
          j                            |                    dd          |j         d         |j         d         dd	                              dd          }t          j	        ||fd
          }|fS )Nr   )r   r^   constantrQ   )r  r   r^   r`   r  r  r   ra   )
rh   ri   r  r  padr   rW  r/  rK  rj   )r5   r,  r-  r,   s       r   r.  zImageBatch.batch  s   <v|B///|B&,r"222,00ZWZ0[[,00ZWZ0[[<v|ABB///[//r!0D0DflSToW]WcdeWfhrt|}}  F  F  GH  IK  L  LFIvv&A...tr   N)r7   r8   r9   rA   r:   r-   r<   r>   r?   r  r.  r   r   r   r*  r*  v  s`        GGGNK K [K LHHJ	 	 	 	 	r   r*  c                   @    e Zd ZddZed             ZdZdZdZdd	Z	d
S )
EmptyImagerr  c                     || _         d S r   rs  )r5   rt  s     r   r5  zEmptyImage.__init__  s    r   c                 `    ddddt           ddfdddt           ddfdddddfdddd	dd
dfdiS )Nr#   r   r   r^   rT   r   r  r   i color)rU   rV   rW   rX   display)r   r   r  r6  r   r+   s    r   r-   zEmptyImage.INPUT_TYPES  so    3qQ_ij/k/k'l).CR`jk0l0l(m-2!TX4Y4Y,Z(-1Qxabov/w/w'x    ! 	!r   r   r   rE  r^   r   c                    t           j                                        }t           j                                        }t	          j        |||dg|dz	  dz  dz  ||          }t	          j        |||dg|dz	  dz  dz  ||          }t	          j        |||dg|dz  dz  ||          }	t	          j        |||	fd          fS )Nr^   r  r  r  r   r   ra   )r   r   r  r  ri   fullrj   )
r5   r   r   r  r6  rd  rt  rgbs
             r   r   zEmptyImage.generate  s    &99;;';;==J
FE15"8LPT7T]ckpqqqJ
FE15!t8Kt7S\bjopppJ
FE15$$7NW]ejkkk	1a),,,//r   N)rr  )r^   r   )
r7   r8   r9   r5  r:   r-   r<   r>   r?   r   r   r   r   r3  r3    sh            ! ! [! LHH0 0 0 0 0 0r   r3  c                   >    e Zd ZddgZed             ZdZdZdZd Z	dS )	ImagePadForOutpaintzextend canvaszexpand imagec                     dddddt           ddfdddt           ddfdddt           ddfdddt           ddfdddt           dd	d
fdiS )Nr#   r   r   r   r   rT   (   r^   Tr   )rE  lefttoprightbottom
featheringr   r+   s    r   r-   zImagePadForOutpaint.INPUT_TYPES  s     #AaXY Z Z[1Q~WXYYZQqYZ![![\ a.Z["\"\]$"Q~_`nr&s&st 	
 		
r   r  expand_imagezimage/transformc                    |                                 \  }}}	}
t          j        |||z   |z   |	|z   |z   |
ft          j                  dz  }||d d |||z   |||	z   d d f<   t          j        ||z   |z   |	|z   |z   ft          j                  }t          j        ||	ft          j                  }|dk    r|dz  |k     r|dz  |	k     r}t          |          D ]m}t          |	          D ][}|dk    r|n|}|dk    r||z
  n|}|dk    r|n|	}|dk    r|	|z
  n|	}t          ||||          }||k    rI||z
  |z  }||z  |||f<   \n|||||z   |||	z   f<   ||                    d          fS )Nr  r
  r   r`   )r  ri   r  r  rk   rg   rV   r   )r5   rE  rA  rB  rC  rD  rE  d1d2d3d4	new_imager   r   rr   jdtdbdldrr   r  s                         r   rF  z ImagePadForOutpaint.expand_image  s   BBJcF"BI$5r:-
 
 
 	
 9>	!!!Sr\4r	>11145z#XT	E 12-
 
 

 KH-
 
 

 >>j1nr11j1nr6I6I2YY $ $r $ $A!QhhBB#)Q;;aBB"aiiRB#(A::a2BBB++AJ #a:5A!eAadGG$  ./Sr\4r	>)*4>>!,,--r   N)
r7   r8   r9   rA   r:   r-   r<   r>   r?   rF  r   r   r   r>  r>    sW        %~6N

 

 [

 %LH H). ). ). ). ).r   r>  )r   r   r  r  r  r  r  r  ra  r/  r   r   r  zKSampler (Advanced)z(Load Checkpoint With Config (DEPRECATED)zLoad CheckpointzLoad VAEzLoad LoRA (Model and CLIP)z	Load LoRAz	Load CLIPzLoad ControlNet ModelzLoad ControlNet Model (diff)zLoad Style ModelzLoad CLIP VisionzLoad Diffusion ModelzLoad unCLIP CheckpointzLoad GLIGEN ModelzCLIP Vision EncodezApply Style ModelzCLIP Text Encode (Prompt)zCLIP Set Last LayerzConditioning (Combine)zConditioningAverage zConditioning (Average)zConditioning (Concat)zConditioning (Set Area)z'Conditioning (Set Area with Percentage)zConditioning (Set Mask)zApply ControlNet (DEPRECATED)zApply ControlNetzVAE Encode (for Inpainting)zSet Latent Noise Maskz
VAE Decodez
VAE EncodezRotate LatentzFlip LatentzCrop LatentzEmpty Latent ImagezUpscale LatentzUpscale Latent ByzLatent CompositezLatent BlendzLatent From BatchzRepeat Latent BatchzEmpty Imagez
Save ImagezPreview Imagez
Load ImagezLoad Image (as Mask)zLoad Image (from Outputs)zUpscale ImagezUpscale Image ByzInvert Image ColorszPad Image for OutpaintingzBatch Images (DEPRECATED)z
Crop ImagezStitch ImageszBlend Imagesz
Blur ImagezQuantize ImagezSharpen ImagezScale Image to Total PixelszGet Image SizezVAE Decode (Tiled)zVAE Encode (Tiled))r*  	ImageCropImageStitch
ImageBlend	ImageBlurImageQuantizeImageSharpenImageScaleToTotalPixelsGetImageSizer   r   module_pathr!   c                     t           j                            |           }t           j                            |           r%t           j                            |          d         }|S )a  
    Returns the module name based on the given module path.
    Examples:
        get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node.py") -> "my_custom_node"
        get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node") -> "my_custom_node"
        get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node/") -> "my_custom_node"
        get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node/__init__.py") -> "my_custom_node"
        get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node/__init__") -> "my_custom_node"
        get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node/__init__/") -> "my_custom_node"
        get_module_name("C:/Users/username/ComfyUI/custom_nodes/my_custom_node.disabled") -> "custom_nodes
    Args:
        module_path (str): The path of the module.
    Returns:
        str: The module name.
    r   )rR  rS  basenamerd  r*  )rZ  	base_paths     r   get_module_namer^  z  sO        --I	w~~k"" 3G$$Y//2	r   custom_nodesc           	      "  K   t          |           }t          j                            |           r*t          j                            |           }|d         }|}n5t          j                            |           r|                     dd          }	 t          j        d	                    |                      t          j                            |           rFt          j                            ||           }t          j                            |           d         }n@t          j                            |t          j                            | d                    }| }t          j                            |          }|t           j        |<   |j                            |           t          j                            |          t*          |<   	 ddlm}	 |	                    |           }
|
j        j        }|r}t          j                            | |          }t          j                            |          r>|
j        j        }|t:          |<   t          j        d	                    ||                     n.# t>          $ r!}t          j         d|            Y d }~nd }~ww xY wtC          |d	          rtE          |d	          tt          j                            t          j                            |tE          |d	                              }t          j                            |          r
|t:          |<   tC          |d
          rtE          |d
          |j#        $                                D ];\  }}||vr2|tF          |<   d	                    |t          |                     |_%        <tC          |d          r/tE          |d          tL          '                    |j&                   dS tC          |d          rtE          |d          }tQ          |          st          j         d|  d           dS 	 tS          j*        |          r |             d {V }n
 |            }tW          |tX                    st          j         d|  d           dS |-                                 d {V  |.                                 d {V }tW          |t^                    st          j         d|  d           dS |D ]q}|0                                }|j1        |vr7|tF          |j1        <   d	                    |t          |                     |_%        |j2        |j2        tL          |j1        <   rdS # t>          $ r%}t          j         d|  d|            Y d }~dS d }~ww xY wt          j         d|  d           dS # t>          $ rJ}t          j         tg          j4                               t          j         d|  d|            Y d }~dS d }~ww xY w)Nr   ._x_zTrying to load custom node {}z__init__.py)config_parserz+Automatically register web folder {} for {}zwUnable to parse pyproject.toml due to lack dependency pydantic-settings, please run 'pip install -r requirements.txt': WEB_DIRECTORYNODE_CLASS_MAPPINGSz{}.{}NODE_DISPLAY_NAME_MAPPINGSTcomfy_entrypointzcomfy_entrypoint in z is not callable, skipping.Fz+ did not return a ComfyExtension, skipping.z* did not return a list of nodes, skipping.z(Error while calling comfy_entrypoint in z: zSkip z_ module for custom nodes due to the lack of NODE_CLASS_MAPPINGS or comfy_entrypoint (need one).zCannot import z module for custom nodes: )5r^  rR  rS  rd  r*  isdirr  rd   debugr  	importlibutilspec_from_file_locationr  rT  module_from_specsysmodulesloaderexec_moduleabspathLOADED_MODULE_DIRScomfy_configrc  extract_node_configuration
tool_comfywebprojectr  EXTENSION_WEB_DIRSinfo	Exceptionre   hasattrr  re  itemsRELATIVE_PYTHON_MODULErf  r  callableinspectiscoroutinefunction
isinstancer   on_loadget_node_listr  
GET_SCHEMAnode_iddisplay_name	traceback
format_exc)rZ  ignoremodule_parentmodule_namespsys_module_namemodule_spec
module_dirmodulerc  project_configweb_dir_nameweb_dir_pathproject_nameeweb_dirr  node_cls
entrypoint	extension	node_listschemas                         r   load_custom_noder    s[     !+..K	w~~k"" :Wk**e%	{	#	# :%--c599U5<<[IIJJJ7>>+&& 	%#.@@R]^^K{33A6JJ#.@@RTRYR^R^_jlyRzRz{{K$J00=='-O$&&v...*,'//**E*E;'	[222222*EEkRRN)48L s!w||KFF7==.. s#1#9#>L7C&|4L!N!U!UVbdp!q!qrrr 	[ 	[ 	[O  Z  WX  Z  Z  [  [  [  [  [  [  [  [	[ 6?++ 	:0P0P0\goobgll:wv?_?_&`&`aaGw}}W%% :29";/ 6011 )	gfF[6\6\6h"("<"B"B"D"D r rhv%%08'-6=nn]TcdoTpTp6q6qH3v;<< UQmAnAnAz*11&2STTT4V/00  	 );<<JJ''  _{ _ _ _```u.z:: -&0jll 2 2 2 2 2 2II *
I!)^<< !O$s;$s$s$sttt 5'')))))))))"+"9"9";";;;;;;;	!)T22 !O$r;$r$r$rsss 5 ) Y YH%0022F~V33>F+FN;:A..XghsXtXt:u:u7*6EKEX26>Bt    ]; ] ]Z[ ] ]^^^uuuuu O  QK  Q  Q  Q  R  R  R5   	,..///SSSPQSSTTTuuuuus   D;V: B&I4 3V: 4
J>JV: JEV: ,AV: 7AU. A!U. 8A4U. .
V8VV: VV: :
X?X		Xc            	        K   t          t                                                    } t          j        d          }g }|D ]}t          j        t
          j                            |                    }d|v r|	                    d           |D ]D}t
          j        
                    ||          }t
          j                            |          r*t
          j                            |          d         dk    rl|                    d          rt          j        r'|t          j        vrt#          j        d| d           t          j        r,t)          j        |          rt#          j        d|            t-          j                    }t1          || d	           d
{V }|                    t-          j                    |z
  ||f           Ft5          |          dk    r~t#          j        d           t7          |          D ]D}	|	d         rd}
nd}
t#          j        d                    |	d         |
|	d                              Et#          j        d           d
S d
S )a  
    Initializes the external custom nodes.

    This function loads custom nodes from the specified folder paths and imports them into the application.
    It measures the import times for each custom node and logs the results.

    Returns:
        None
    r_  __pycache__r^   z.pyz	.disabledz	Skipping z; due to disable_all_custom_nodes and whitelist_custom_nodeszBlocked by policy: r  Nr   z
Import times for custom nodes:r`   rA  z (IMPORT FAILED)z{:6.1f} seconds{}: {})r  re  r  r1  r  rR  rl  rS  realpathremoverT  rd  r*  re  r   disable_all_custom_nodeswhitelist_custom_nodesrd   rz  enable_managercomfyui_managershould_be_disabledtimeperf_counterr  rn   rc   rm  r  )base_node_names
node_pathsnode_import_timescustom_node_pathpossible_modulespossible_modulerZ  time_beforesuccessrx   import_messages              r   init_external_custom_nodesr    s      -224455O.~>>J& ` `:bg&6&67G&H&HII,,,##M222/ 	` 	`O',,'7IIKw~~k** rw/?/?/L/LQ/OSX/X/X##K00 , Hc1c1cuuuuvvv" "5kBB L!D{!D!DEEE+--K,[/YghhhhhhhhhG$$d&7&9&9K&GV]%^____#	`& !!7888)** 	U 	UAt 4!#!3L077!naPQdSSTTTTR "!r   c                  r  K   t           j                            t           j                            t           j                            t
                              d          } g d}g }|D ]L}t          t           j                            | |          d           d{V s|                    |           M|S )a  
    Initializes the built-in extra nodes in ComfyUI.

    This function loads the extra node files located in the "comfy_extras" directory and imports them into ComfyUI.
    If any of the extra node files fail to import, a warning message is logged.

    Returns:
        None
    comfy_extras)uznodes_latent.pyznodes_hypernetwork.pyznodes_upscale_model.pyznodes_post_processing.pyznodes_mask.pyznodes_compositing.pyznodes_rebatch.pyznodes_model_merging.pyznodes_tomesd.pyznodes_clip_sdxl.pyznodes_canny.pyznodes_freelunch.pyznodes_custom_sampler.pyznodes_hypertile.pyznodes_model_advanced.pyznodes_model_downscale.pyznodes_images.pyznodes_video_model.pyznodes_ideogram4.pyznodes_train.pyznodes_dataset.pyznodes_sag.pyznodes_perpneg.pyznodes_stable3d.pyznodes_sdupscale.pyznodes_photomaker.pyznodes_pixart.pyznodes_cond.pyznodes_morphology.pyznodes_stable_cascade.pyznodes_differential_diffusion.pyznodes_ip2p.pyz%nodes_model_merging_model_specific.pyznodes_pag.pyznodes_align_your_steps.pyznodes_attention_multiply.pyznodes_advanced_samplers.pyznodes_webcam.pyznodes_audio.pyznodes_sd3.pyznodes_gits.pyznodes_controlnet.pyznodes_hunyuan.pyznodes_eps.pyznodes_flux.pyznodes_lora_extract.pyznodes_torch_compile.pyznodes_mochi.pyznodes_slg.pyznodes_mahiro.pyznodes_lt_upsampler.pyznodes_lt_audio.pyznodes_lt.pyznodes_hooks.pyznodes_multigpu.pyznodes_load_3d.pyznodes_cosmos.pyznodes_video.pyznodes_lumina2.pyznodes_wan.pyznodes_bernini.pyznodes_lotus.pyznodes_hunyuan3d.pyznodes_primitive.pyznodes_cfg.pyznodes_optimalsteps.pyznodes_hidream.pyznodes_fresca.pyznodes_apg.pyznodes_preview_any.pyznodes_ace.pyznodes_string.pyznodes_camera_trajectory.pyznodes_edit_model.pyznodes_tcfg.pyznodes_context_windows.pyznodes_qwen.pyznodes_chroma_radiance.pyznodes_pid.pyznodes_model_patch.pyznodes_easycache.pyznodes_audio_encoder.pyznodes_rope.pyznodes_logic.pyznodes_resolution.pyznodes_nop.pyznodes_kandinsky5.pyznodes_wanmove.pyznodes_ar_video.pyznodes_image_compare.pyznodes_zimage.pyznodes_glsl.pyznodes_lora_debug.pyznodes_textgen.pyznodes_color.pyznodes_toolkit.pyznodes_replacements.pyznodes_nag.pyznodes_sdpose.pyznodes_math.pyznodes_number_convert.pyznodes_painter.pyznodes_curve.pyznodes_bg_removal.pyznodes_rtdetr.pyznodes_frame_interpolation.pyznodes_sam3.pyznodes_scail.pyznodes_void.pyznodes_wandancer.pyznodes_hidream_o1.pyznodes_save_3d.pyznodes_moge.pyznodes_mediapipe.pyznodes_gaussian_splat.pyznodes_triposplat.pyznodes_depth_anything_3.pyr  N)rR  rS  rT  dirnamer  __file__r  rn   )
extras_dirextras_filesimport_failed	node_files       r   init_builtin_extra_nodesr  	  s       bgoobg.>.>x.H.HII>ZZJv v vLp M! , ,	%bgll:y&I&IYghhhhhhhhh 	,  +++r   c                    K   t           j                            t           j                            t           j                            t
                              d          } t          t          j        t           j                            | d                              }g }|D ]K}t          |d           d {V s2|	                    t           j        
                    |                     L|S )Ncomfy_api_nodesz
nodes_*.pyr  )rR  rS  rT  r  r  r  rm  globr  rn   r\  )api_nodes_dirapi_nodes_filesr  r  s       r   init_builtin_api_nodesr  	  s      GLL1A1A(1K1K!L!LN_``MTYrw||M<'P'PQQRROM$ > >	%i?PQQQQQQQQQ 	>  !1!1)!<!<===r   c                  F   K   t          d t          D                        d S )Nc                 L    g | ]!}t          t          |d           |          "S )VERSION)version	api_class)r   r  )rg  r  s     r   rj  z$init_public_apis.<locals>.<listcomp>	  sI         	Ay))	
 	
 	
  r   )r   r   r   r   r   init_public_apisr  	  s@         &	       r   c                   K   t                       d {V  t                       d {V }g }|rt                       d {V }| rt                       d {V  nt	          j        d           t          |          dk    rt	          j        d           |D ])}t	          j        d                    |                     *t	          j        d           t          j
        rt	          j        d           nt	          j        d           t	          j        d           t          |          dk    rt	          j        d	           |D ])}t	          j        d                    |                     *t	          j        d           t          j
        rt	          j        d           nt	          j        d           t	          j        d           |S )
Nz Skipping loading of custom nodesr   zwWARNING: some comfy_api_nodes/ nodes did not import correctly. This may be because they are missing some dependencies.
zIMPORT FAILED: {}z`
This issue might be caused by new missing dependencies added the last time you updated ComfyUI.z7Please run the update script: update/update_comfyui.batz,Please do a: pip install -r requirements.txtrA  ztWARNING: some comfy_extras/ nodes did not import correctly. This may be because they are missing some dependencies.
)r  r  r  r  rd   rz  rc   re   r  r   windows_standalone_build)init_custom_nodesinit_api_nodesr  import_failed_apinodes        r   init_extra_nodesr  	  s     


244444444M ;"8":"::::::: 9(**********7888
!!  S  	T  	T  	T% 	> 	>DO/66t<<===={|||( 	LOUVVVVOJKKK
=A  P  	Q  	Q  	Q! 	> 	>DO/66t<<===={|||( 	LOUVVVVOJKKKr   r,  )rQ   FNNFr  )ri   rR  rn  rO  r  r  r  r  r  r  r  rd   PILr   r   r   PIL.PngImagePluginr   r  r  safetensors.torchrv  rS  insertrT  r  r  r  comfy.diffusers_loadr   comfy.samplerscomfy.samplecomfy.sdcomfy.utilscomfy.controlnetcomfy.comfy_typesr   r	   r
   r   comfy_api.internalr   r   comfy_api.version_listr   comfy_api.latestr   r   r   comfy.clip_visioncomfy.model_managementcomfy.cli_argsr   rj  r1  r  r   r  r  r   r   r   r    rC   rN   rz   r   r   r   r   r   r   r   r   r   r   r   r  r/  ra  r  r  r  r  r  r  r  r  r2  r>  rB  rQ  rZ  rl  r  r  r  r  r  r  r  r  r  r
  r  r   r1  r8  rE  rK  rX  rh  rp  r  r  r  r  r  r  r  r  r  r!  r%  r*  r3  r>  re  rf  ry  rs  rf  r^  r  r  r  r  r  r  r  r  r   r   r   <module>r     s    
			 



             . . . . . . . . . . & & & & & &         27<<0@0@0J0J K KWUU V V V                      J J J J J J J J J J J J E E E E E E E E 5 5 5 5 5 5 : : : : : : : : : :                               G G G? ? ? ? = = = = =\ = = =03 3 3 3 3 3 3 3& & & & & & & &P       8       .       *               6       2       "       6       B! ! ! ! ! ! ! !" " " " " " " "$&R &R &R &R &R &R &R &RR1, 1, 1, 1, 1, 1, 1, 1,h20 20 20 20 20 20 20 20j# # # # # # # #Ld d d d d d d d$       .p p p p p p p p:               -' -' -' -' -' -' -' -'^O O O O O* O O O] ] ] ] ] ] ] ]~       $       "       >0  0  0  0  0  0  0  0 f       2       6       <       "       &        = = = = = = = =~       (              >E E E E E E E E." " " " " " " "H       8" " " " " " " "H       *       0       ,* * * * * * * *X)@ )@ )@ )@ )@ )@ )@ )@V! ! ! ! ! ! ! !F           2D D D D D D D D:               D7/ 7/ 7/ 7/ 7/ 7/ 7/ 7/r    9    J J J J J J J JZ)) )) )) )) ))I )) )) ))X    i   0       @       *       "       00 0 0 0 0 0 0 00>. >. >. >. >. >. >. >.BDD4D nD (	D
 D D .D D (D ]D D D *D D LD  !D" ]#D D$ %D& *'D( L)D* ;+D, *-D. ./D0 *1D2 .3D4 .5D6 ,7D8 .9D: $%B;D< "#>=D> .?D@ (ADB ,CDD ED D DF ;GDH LIDJ *KDL *MDN *ODP *QDR *SDT nUDV (WDX YDZ ,[D\ ]D^ 6_D` (aDb 0cDd (eDf (gD Dh %$4 , 8(&.$@.GD D D LE
E -E
 BE /E E .E ;E +E /E :E *E *E (E  6!E" '#E& ,'E( *)E E* 1+E, --E. 3/E0 41E2 13E4 45E6 $%N7E8 49E: 6;E< 1=E@ 8AEB 1CED EEF GEH OIEJ -KEL -ME E EN ,OEP %QER *SET )UEV >WEX +YEZ .[E^ -_E` aEb OcEd eEf +gEh 2iEj /kEl &mEn (oEp 6qE Er ." %#<$**IE E E N         , 58CEE ^ ^ ^\` ^ ^ ^ ^@- - -^H H HV	 	 	  $ $ $ $ $ $r   