
    +j6D                        d dl Zd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
 d dlZd dlZd&dZ G d de
j                  Z G d d	e
j                  Z G d
 de
j                  Z G d de
j                  Z G d de
j                  Z G d de
j                  Z G d de
j                  Z G d de
j                  Z G d de
j                  Z G d de
j                  Z G d de
j                  Z G d de
j                  Z G d de
j                  Z G d  d!e
j                  Z G d" d#e	          Zd$efd%ZdS )'    N)override)ComfyExtensionioTc                     |j         dd          | dd          k    r/t          j                            || d         | d         dd          }|r&t          j                            || d                   S |S )N   bilinearcenterr   )shapecomfyutilscommon_upscalerepeat_to_batch_size)target_shapelatentrepeat_batchs      9/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_latent.pyreshape_latent_tor   
   st    |ABB<+++++FL4DlSUFVXbdlmm {//QHHH    c                   J    e Zd Zed             Zedej        fd            ZdS )	LatentAddc           	          t          j        dddgdt           j                            d          t           j                            d          gt           j                                        g          S )Nr   combine latentszsum latentsmodel/latent/advancedsamples1samples2node_idsearch_aliasescategoryinputsoutputsr   SchemaLatentInputOutputclss    r   define_schemazLatentAdd.define_schema   sh    y-}=,	
++	
++
 	  ""
 
 
 	
r   returnc                     |                                 }|d         }|d         }t          |j        |          }||z   |d<   t          j        |          S Nsamplescopyr   r   r   
NodeOutputr*   r   r   samples_outs1s2s         r   executezLatentAdd.execute#   R    mmooi i rx,,!#bI}[)))r   N__name__
__module____qualname__classmethodr+   r   r2   r7    r   r   r   r      V        
 
 [
 *BM * * * [* * *r   r   c                   J    e Zd Zed             Zedej        fd            ZdS )LatentSubtractc           	          t          j        dddgdt           j                            d          t           j                            d          gt           j                                        g          S )NrA   zdifference latentzremove featuresr   r   r   r   r$   r)   s    r   r+   zLatentSubtract.define_schema/   si    y$/1BC,	
++	
++
 	  ""
 
 
 	
r   r,   c                     |                                 }|d         }|d         }t          |j        |          }||z
  |d<   t          j        |          S r.   r0   r3   s         r   r7   zLatentSubtract.execute>   r8   r   Nr9   r>   r   r   rA   rA   .   r?   r   rA   c                   J    e Zd Zed             Zedej        fd            ZdS )LatentMultiplyc                     t          j        dg ddt           j                            d          t           j                            ddddd	
          gt           j                                        g          S )NrE   )zscale latentzamplify latentzlatent gainr   r/   
multiplier      ?g      $      $@{Gz?defaultminmaxstepr   r   r%   r&   r'   Floatr(   r)   s    r   r+   zLatentMultiply.define_schemaJ   st    y$LLL,		**|SeTXYY
 	  ""
 
 
 	
r   r,   c                 r    |                                 }|d         }||z  |d<   t          j        |          S r.   r1   r   r2   )r*   r/   rG   r4   r5   s        r   r7   zLatentMultiply.executeY   s7    llnnY!#jI}[)))r   Nr9   r>   r   r   rE   rE   I   sV        
 
 [
 *R] * * * [* * *r   rE   c                   J    e Zd Zed             Zedej        fd            ZdS )LatentInterpolatec                 0   t          j        dg ddt           j                            d          t           j                            d          t           j                            ddddd	
          gt           j                                        g          S )NrU   )zblend latentz
mix latentzlerp latent
transitionr   r   r   ratiorH           rJ   rK   r   rP   r)   s    r   r+   zLatentInterpolate.define_schemab   s    y'VVV,	
++	
++w#DQQ 	  ""
 
 
 	
r   r,   c                 0   |                                 }|d         }|d         }t          |j        |          }t          j                            |d          }t          j                            |d          }t          j        ||z            }t          j        ||z            }||z  |d|z
  z  z   }	t          j                            |	d          }
t          j        |	|
z            }|||z  |d|z
  z  z   z  |d<   t          j        |          S )Nr/   r   dimrH   )	r1   r   r   torchlinalgvector_norm
nan_to_numr   r2   )r*   r   r   rX   r4   r5   r6   m1m2tmtsts               r   r7   zLatentInterpolate.executer   s   mmooi i rx,,\%%bq%22\%%bq%22b2g&&b2g&&%Z"e,,\%%aa%11a"f%%!#rEzB#+4F'F!GI}[)))r   Nr9   r>   r   r   rU   rU   a   sV        
 
 [
 *2= * * * [* * *r   rU   c                   J    e Zd Zed             Zedej        fd            ZdS )LatentConcatc                 .   t          j        dddgdt           j                            d          t           j                            d          t           j                            dg d	          gt           j                                        g
          S )Nrg   join latentszstitch latentsr   r   r   r\   )xz-xyz-yrc   z-toptionsr   r   r%   r&   r'   Combor(   r)   s    r   r+   zLatentConcat.define_schema   s    y"*,<=,	
++	
++u.O.O.OPP 	  ""
 
 
 	
r   r,   c                 <   |                                 }|d         }|d         }t          j                            ||j        d                   }d|v r||f}n||f}d|v rd}nd|v rd}nd|v rd	}t          j        ||
          |d<   t          j        |          S )Nr/   r   -rj   r   rk   r	   rc   r[   )	r1   r   r   r   r   r]   catr   r2   )r*   r   r   r\   r4   r5   r6   cs           r   r7   zLatentConcat.execute   s    mmooi i [--b"(1+>>#::RAARA#::CCCZZCCCZZC!&1#!6!6!6I}[)))r   Nr9   r>   r   r   rg   rg      sV        
 
 [
 * * * * [* * *r   rg   c                   J    e Zd Zed             Zedej        fd            ZdS )	LatentCutc                    t          j        dg ddt           j                            d          t           j                            dg d          t           j                            dd	t          j         t          j        d
          t           j                            dd
d
t          j        d
          gt           j                                        g          S )Nrv   )zcrop latentzslice latentzextract regionr   r/   r\   )rj   rk   rc   rl   indexr   r   rK   amountr   	r   r%   r&   r'   ro   IntnodesMAX_RESOLUTIONr(   r)   s    r   r+   zLatentCut.define_schema   s    yLLL,		**uooo>>Wae6J5JPUPdklmmXqaU=QXYZZ	 	  ""
 
 
 	
r   r,   c                    |                                 }|d         }d|v r|j        dz
  }nd|v r|j        dz
  }nd|v r
|j        dz
  }|dk    r=t          ||j        |         dz
            }t          |j        |         |z
  |          }n-t	          ||j        |                    }t          | |          }t          j        ||||          |d<   t          j        |          S )	Nr/   rj   r   rk      rc      r   )	r1   ndimrM   r   rN   r]   narrowr   r2   )r*   r/   r\   rx   ry   r4   r5   s          r   r7   zLatentCut.execute   s    llnnY#::'A+CCCZZ'A+CCCZZ'A+CA::rx}q011E#.77FF~..E%((F!&b#uf!E!EI}[)))r   Nr9   r>   r   r   rv   rv      sV        
 
 [
  *R] * * * [* * *r   rv   c                   J    e Zd Zed             Zedej        fd            ZdS )LatentCutToBatchc                 L   t          j        dg ddt           j                            d          t           j                            dg d          t           j                            dd	d	t          j        d	
          gt           j                                        g          S )Nr   )zslice to batchzsplit latentztile latentr   r/   r\   )rc   rj   rk   rl   
slice_sizer   rK   r   rz   r)   s    r   r+   zLatentCutToBatch.define_schema   s    y&LLL,		**uooo>>\1!AU\]^^ 	  ""
 
 
 	
r   r,   c                 |   |                                 }|d         }d|v r|j        dz
  }nd|v r|j        dz
  }nd|v r
|j        dz
  }|dk     rt          j        |          S |                    |d          }|j        d         |k     r|j        d         }nE|j        d         |z  dk    r1|d d d t          j        |j        d         |z            |z  f         }d	|gt          |j        dd                    z   }|	                    |                              d|          |d<   t          j        |          S )
Nr/   rj   r   rk   r   rc   r   r   r   )
r1   r   r   r2   movedimr   mathfloorlistreshape)r*   r/   r\   r   r4   r5   s	new_shapes           r   r7   zLatentCutToBatch.execute   sC   llnnY#::'A+CCCZZ'A+CCCZZ'A+C77=)))JJsA71:
""JJWQZ*$))!!!Fdjj!899JFFFGA$tAGABBK'8'88	!"9!5!5!=!=a!E!EI}[)))r   Nr9   r>   r   r   r   r      sV        
 
 [
 *"- * * * [* * *r   r   c                   J    e Zd Zed             Zedej        fd            ZdS )LatentBatchc           
          t          j        dg dddt           j                            d          t           j                            d          gt           j                                        g          S )Nr   )r   zmerge latentsri   model/latent/batchTr   r   )r   r    r!   is_deprecatedr"   r#   r$   r)   s    r   r+   zLatentBatch.define_schema  sl    y!OOO)	
++	
++
 	  ""
 
 
 	
r   r,   c           
         |                                 }|d         }|d         }t          |j        |d          }t          j        ||fd          }||d<   |                    dd t          d|j        d                   D                       |                    dd t          d|j        d                   D                       z   |d<   t          j        |          S )	Nr/   F)r   r   r[   batch_indexc                     g | ]}|S r>   r>   .0rj   s     r   
<listcomp>z'LatentBatch.execute.<locals>.<listcomp>  s    AcAcAc!AcAcAcr   c                     g | ]}|S r>   r>   r   s     r   r   z'LatentBatch.execute.<locals>.<listcomp>  s:      Df  Df  Df  KL  EF  Df  Df  Dfr   )	r1   r   r   r]   rs   getranger   r2   )r*   r   r   r4   r5   r6   r   s          r   r7   zLatentBatch.execute  s&   mmooi i rx%@@@Ir2hA&&&!"I%-\\-AcAcUSTVXV^_`VaMbMbAcAcAc%d%dgogsgs  uB  Df  Df  PU  VW  Y[  Ya  bc  Yd  Pe  Pe  Df  Df  Df  hg  hg  &gM"}[)))r   Nr9   r>   r   r   r   r     sV        
 
 [
 	*BM 	* 	* 	* [	* 	* 	*r   r   c                   J    e Zd Zed             Zedej        fd            ZdS )LatentBatchSeedBehaviorc           
          t          j        ddt           j                            d          t           j                            dddgd          gt           j                                        g          S )	Nr   r   r/   seed_behaviorrandomfixed)rm   rL   r   r!   r"   r#   rn   r)   s    r   r+   z%LatentBatchSeedBehavior.define_schema!  sl    y-,		**78KU\]]
 	  ""

 

 

 
	
r   r,   c                    |                                 }|d         }|dk    rd|v r|                    d           n7|dk    r1|                    ddg          d         }|g|j        d         z  |d<   t	          j        |          S )Nr/   r   r   r   r   )r1   popr   r   r   r2   )r*   r/   r   r4   r   batch_numbers         r   r7   zLatentBatchSeedBehavior.execute/  s    llnn#H$$++...g%%&??=1#>>qAL*6&,q/)IK&}[)))r   Nr9   r>   r   r   r   r      sV        
 
 [
 
* 
* 
* 
* [
* 
* 
*r   r   c                   J    e Zd Zed             Zedej        fd            ZdS )LatentApplyOperationc           
          t          j        ddgddt           j                            d          t           j                            d          gt           j                                        g          S )Nr   ztransform latent model/latent/advanced/operationsTr/   	operationr   r    r!   is_experimentalr"   r#   )r   r%   r&   r'   LatentOperationr(   r)   s    r   r+   z"LatentApplyOperation.define_schema=  sl    y*./7 		**"((55
 	  ""
 
 
 	
r   r,   c                     |                                 }|d         } ||          |d<   t          j        |          S )Nr/   r   rS   )r*   r/   r   r4   r5   s        r   r7   zLatentApplyOperation.executeM  s@    llnnY!*"!5!5!5I}[)))r   Nr9   r>   r   r   r   r   <  sV        
 
 [
 *BM * * * [* * *r   r   c                   J    e Zd Zed             Zedej        fd            ZdS )LatentApplyOperationCFGc           	          t          j        dddt           j                            d          t           j                            d          gt           j                                        g          S )Nr   r   Tmodelr   r   r!   r   r"   r#   )r   r%   Modelr'   r   r(   r)   s    r   r+   z%LatentApplyOperationCFG.define_schemaV  sd    y-7 w''"((55
 !!
 
 
 	
r   r,   c                     |                                 }fd}|                    |           t          j        |          S )Nc                     | d         }t          |          dk    r( |d         |d         z
            |d         z   |d<   n |d                   |d<   |S )N	conds_outr   r   r   r   )len)argsr   r   s     r   pre_cfg_functionz9LatentApplyOperationCFG.execute.<locals>.pre_cfg_functioni  so    [)I9~~""(y1	!1LNNNQZ[\Q]]	!(y	!===	!r   )clone"set_model_sampler_pre_cfg_functionr   r2   )r*   r   r   mr   s     `  r   r7   zLatentApplyOperationCFG.executee  sP    KKMM	 	 	 	 	 	
,,-=>>>}Qr   Nr9   r>   r   r   r   r   U  sV        
 
 [
  "-       [     r   r   c                   J    e Zd Zed             Zedej        fd            ZdS )LatentOperationTonemapReinhardc                     t          j        ddgddt           j                            ddddd	
          gt           j                                        g          S )Nr   z
hdr latentr   TrG   rH   rY   g      Y@rJ   rK   r   )r   r%   rQ   r'   r   r(   r)   s    r   r+   z,LatentOperationTonemapReinhard.define_schemau  sc    y4(>7 |ScuSWXX "))++
 
 
 	
r   r,   c                 6    fd}t          j        |          S )Nc                 T   t           j                            | d          dz   d d d f         }| |z  }t          t	          d|j                            }t          j        ||d          }t          j        ||d          }|dz  |z   	z  }|d|z  z  }||dz   z  }||z  }||z  S )Nr   r[   g|=T)r\   keepdim   rH   )r]   r^   r_   r   r   r   meanstd)
r   kwargslatent_vector_magnitudenormalized_latentdimsr   r   topnew_magnituderG   s
            r   tonemap_reinhardz@LatentOperationTonemapReinhard.execute.<locals>.tonemap_reinhard  s    ',|'?'?Q'?'P'PS_'_abababcgag&h# &)@ @a!8!=>>??D:54NNND)3tLLLC7T>Z/C $c	2#37NQT7TUMS M$}44r   r   r2   )r*   rG   r   s    ` r   r7   z&LatentOperationTonemapReinhard.execute  s/    	5 	5 	5 	5 	5  }-...r   Nr9   r>   r   r   r   r   t  sV        
 
 [
 /BM / / / [/ / /r   r   c                   J    e Zd Zed             Zedej        fd            ZdS )LatentOperationSharpenc                 F   t          j        dddt           j                            dddddd          t           j                            d	d
dddd          t           j                            dddddd          gt           j                                        g          S )Nr   r   Tsharpen_radius	   r      )rL   rM   rN   rO   advancedsigmarH   g?rI   alpharY   g      @rJ   r   )r   r%   r{   r'   rQ   r   r(   r)   s    r   r+   z$LatentOperationSharpen.define_schema  s    y,7 -qaRaZ^__w$S[_``w#D[_`` "))++
 
 
 	
r   r,   c                 >    fd}t          j        |          S )Nc                 v   t           j                            | d          dz   d d d f         }| |z  }| j        d         }dz  dz   }t          j                            ||j                  }|dz  }|
dz  z  }|||f         |                                z
  dz   |||f<   t           j	        j
                            |fd          }t           j	        j
                            ||                    |dd                              d          |dz  |	          d d d d   f         }	||	z  S )
Nr   r[   gư>r   )deviceirH   reflect)paddinggroups)r]   r^   r_   r   comfy_extrasnodes_post_processinggaussian_kernelr   sumnn
functionalpadconv2drepeat	unsqueeze)r   r   	luminancer   channelskernel_sizekernelr   padded_image	sharpenedr   r   r   s             r   sharpenz/LatentOperationSharpen.execute.<locals>.sharpen  s   11&q1BBTI111T6RI & 2|AH(1,q0K!7GGUZclcsGttF A%Feck!F%+FFN%;fjjll%JS%PF66>" 8.223D~Vdes  uC  GD  FO  P  PL+22<xYZ\]A^A^AhAhijAkAk  vA  EF  vF  OW2  X  X  YZ  YZ  YZ  [\  [\  [\  ]k  m{  l{  ]{  }K  M[  L[  }[  Y[  \Iy((r   r   )r*   r   r   r   r   s    ``` r   r7   zLatentOperationSharpen.execute  s:    	) 	) 	) 	) 	) 	) 	)  }W%%%r   Nr9   r>   r   r   r   r     sV        
 
 [
 &bm & & & [& & &r   r   c                   L    e Zd Zed             Zeddej        fd            ZdS )ReplaceVideoLatentFramesc                 `   t          j        ddt           j                            dd          t           j                            ddd	          t           j                            d
dt
          j         t
          j        dd          gt           j                                        g          S )Nr   r   destinationz5The destination latent where frames will be replaced.)tooltipsourceTzThe source latent providing frames to insert into the destination latent. If not provided, the destination latent is returned unchanged.)optionalr   rx   r   r   zThe starting latent frame index in the destination latent where the source latent frames will be placed. Negative values count from the end.)rL   rM   rN   rO   r   r   )r   r%   r&   r'   r{   r|   r}   r(   r)   s    r   r+   z&ReplaceVideoLatentFrames.define_schema  s    y.)	7noo	4  BL  M  MWae6J5JPUPdkl  wE  F  F 	  ""
 
 
 	
r   Nr,   c           	      F   |t          j        |          S |d         j        d         }|d         j        d         }|dk     r||z   }||k    r/t          j        d| d| d           t          j        |          S ||z   |k    r2t          j        d| d| d	| d           t          j        |          S |                                }|d         }|d                                         }||d d d d |||j        d         z   f<   ||d<   t          j        |          S )
Nr/   r   r   z ReplaceVideoLatentFrames: Index z0 is out of bounds for destination latent frames .z/ReplaceVideoLatentFrames: Source latent frames z- do not fit within destination latent frames z at the specified index )r   r2   r   loggingwarningr1   r   )	r*   r   rx   r   dest_framessource_framesr   s_sources_destinations	            r   r7   z ReplaceVideoLatentFrames.execute  s   >=---!),215y)/2199%'E;O  Eu  E  E  wB  E  E  E  F  F  F=---= ;..O  xm  x  x  KV  x  x  pu  x  x  x  y  y  y=---KKMM)$#I.4466?GaaaE%(.*;";;;<$)}Qr   Nr9   r>   r   r   r   r     s[        
 
 [
           [     r   r   c                   L    e Zd Zedeeej                          fd            ZdS )LatentExtensionr,   c                    K   t           t          t          t          t          t
          t          t          t          t          t          t          t          t          gS r   )r   rA   rE   rU   rg   rv   r   r   r   r   r   r   r   r   )selfs    r   get_node_listzLatentExtension.get_node_list  s<       # #*"$
 	
r   N)	r:   r;   r<   r   r   typer   	ComfyNoder  r>   r   r   r  r    sE        
T$r|*<%= 
 
 
 X
 
 
r   r  r,   c                  "   K   t                      S r   )r  r>   r   r   comfy_entrypointr	    s      r   )T)comfy.utilsr   "comfy_extras.nodes_post_processingr   r]   r|   typing_extensionsr   comfy_api.latestr   r   r   r   r   r  r   rA   rE   rU   rg   rv   r   r   r   r   r   r   r   r   r  r	  r>   r   r   <module>r     s=       ) ) ) )   & & & & & & / / / / / / / /     * * * * * * * *6* * * * *R\ * * *6* * * * *R\ * * *0%* %* %* %* %* %* %* %*N&* &* &* &* &*2< &* &* &*P'* '* '* '* '* '* '* '*R(* (* (* (* (*r| (* (* (*T* * * * *", * * *:* * * * *bl * * *8* * * * *2< * * *2         bl      >"/ "/ "/ "/ "/R\ "/ "/ "/H#& #& #& #& #&R\ #& #& #&J#  #  #  #  # r| #  #  # J
 
 
 
 
n 
 
 
*      r   