
    +j8                         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 d dlm	Z	m
Z
  G d de
j                  Zdd	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ioc                   L    e Zd Zed             Zeddej        fd            ZdS )Kandinsky5ImageToVideoc                 t   t          j        ddt           j                            d          t           j                            d          t           j                            d          t           j                            dddt          j        d	          t           j                            d
ddt          j        d	          t           j                            dddt          j        d	          t           j                            dddd          t           j                            dd          gt           j        	                    d          t           j        	                    d          t           j
        	                    dd          t           j
        	                    dd          g          S )Nr   model/conditioning/video_modelspositivenegativevaewidthi      )defaultminmaxstepheighti   lengthy         
batch_sizei   )r   r   r   start_imageT)optionaldisplay_namelatentzEmpty video latent)r   tooltipcond_latentzWClean encoded start images, used to replace the noisy start of the model output latents)node_idcategoryinputsoutputs)r   SchemaConditioningInputVaeIntnodesMAX_RESOLUTIONImageOutputLatentclss    =/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_kandinsky5.pydefine_schemaz$Kandinsky5ImageToVideo.define_schema   sW   y,6%%j11%%j11U##Wcru?SZ\]]Xs@T[]^^Xsu?SZ[\\\1!FF}t<<	 &&J&??&&J&??	  h@T UU	  m  F_   `  `	
 
 
 	
    Nreturnc	                    t          j        |d|dz
  dz  dz   |dz  |dz  gt          j                                                  }	i }
|t          j                            |d |                             dd          ||dd                              dd          }|                    |d d d d d d d d	f                   }||
d
<   t          j	        dd|	j
        d         |	j
        d         |	j
        d         f|j        |j                  }d|d d d d d |j
        d         dz
  dz  dz   f<   t          j        |||d          }t          j        |||d          }i }|	|d
<   t          j        ||||
          S )Nr   r   r      )devicebilinearcenter   samples   )r6   dtypeg        r   )time_dim_replaceconcat_mask)torchzeroscomfymodel_managementintermediate_deviceutilscommon_upscalemovedimencodeonesshaper6   r>   node_helpersconditioning_set_valuesr   
NodeOutput)r/   r
   r   r   r   r   r   r   r   r   cond_latent_outencodedmask
out_latents                 r0   executezKandinsky5ImageToVideo.execute#   s   j"
q/@A.EvQR{TY]^T^_hmh~  iS  iS  iU  iU  V  V  V"+44[&5I5Q5QRTVW5X5XZ_agisu}~~  G  G  HI  KM  N  NKjjQQQ111bqb[!9::G)0OI&:q!V\!_fl26FUWHXYbmbt  }H  }N  O  O  ODADDAAA=++A.2q8A===>#;H[bswFxFxyyH#;H[bswFxFxyyH
 &
9}Xx_MMMr2   N__name__
__module____qualname__classmethodr1   r   rN   rS    r2   r0   r   r      se        
 
 [
, N Nfhfs N N N [N N Nr2   r   333333?ffffff?      ?c                 @   |                      dd          }|                     dd          }t          j        |                                 ||z
  ||z             }t          j        |                                ||z
  ||z             }	| |z
  |dz   z  }
|
|	z  |z   }
|
S )N)r   r:   r   T)dimkeepdimg:0yE>)meanstdrA   clamp)source	referenceclump_mean_lowclump_mean_highclump_std_lowclump_std_highsource_mean
source_stdreference_meanreference_std
normalizeds              r0   adaptive_mean_std_normalizationro   7   s    ++)T+::K	488J[!1!1;3OQ\_nQnooNK	m1KZZhMhiiM ;&:+<=Jm+n<Jr2   c                   J    e Zd Zed             Zedej        fd            ZdS )NormalizeVideoLatentStartc                 f   t          j        dddt           j                            d          t           j                            dddt
          j        dd	          t           j                            d
ddt
          j        dd	          gt           j                            d          g          S )Nrq   r	   zNormalizes the initial frames of a video latent to match the mean and standard deviation of subsequent reference frames. Helps reduce differences between the starting frames and the rest of the video.r   start_frame_countr   r   z<Number of latent frames to normalize, counted from the start)r   r   r   r   r   reference_frame_count   zBNumber of latent frames after the start frames to use as referencer   )r    r!   descriptionr"   r#   )r   r$   r-   r&   r(   r)   r*   r,   r.   s    r0   r1   z'NormalizeVideoLatentStart.define_schemaF   s    y/6 c	))0!H\cd  om  n  n4aQEL`gh  sw  x  x 	  h 77
 
 
 	
r2   r3   c           
         |d         j         d         dk    rt          j        |          S |                                }|d                                         }|d d d d d |f         }|d d d d ||t          ||j         d         dz
            z   f         }t          ||          }||d d d d d |f<   ||d<   t          j        |          S )Nr;   r<   r   )rK   r   rN   copycloner   ro   )	r/   r   rs   rt   sr;   first_framesreference_frames_datanormalized_first_framess	            r0   rS   z!NormalizeVideoLatentStart.executeV   s   )"1%**=(((KKMM#))++qqq!!!%7&7%778 '111.?@QRUVkmtmz{|m}~m  SA  SA  AA  /A  )A  !B"A,Pe"f"f,C111(((())}Qr2   NrU   rZ   r2   r0   rq   rq   E   sV        
 
 [
  "-       [     r2   rq   c                   J    e Zd Zed             Zedej        fd            ZdS )CLIPTextEncodeKandinsky5c                 0   t          j        ddgdt           j                            d          t           j                            ddd          t           j                            ddd          gt           j                                        g	          S )
Nr   zkandinsky promptz advanced/conditioning/kandinsky5clipclip_lT)	multilinedynamic_prompts	qwen25_7b)r    search_aliasesr!   r"   r#   )r   r$   Clipr&   Stringr%   r,   r.   s    r0   r1   z&CLIPTextEncodeKandinsky5.define_schemag   s    y../7f%%	D$OO	tTRR &&((
 
 
 	
r2   r3   c                     |                     |          }|                     |          d         |d<   t          j        |                    |                    S )Nr   )tokenizer   rN   encode_from_tokens_scheduled)r/   r   r   r   tokenss        r0   rS   z CLIPTextEncodeKandinsky5.executew   sL    v&&"mmI66{C{}T>>vFFGGGr2   NrU   rZ   r2   r0   r   r   f   s]        
 
 [
 H H H H [H H Hr2   r   c                   L    e Zd Zedeeej                          fd            ZdS )Kandinsky5Extensionr3   c                 .   K   t           t          t          gS rT   )r   rq   r   )selfs    r0   get_node_listz!Kandinsky5Extension.get_node_list   s       #%$
 	
r2   N)	rV   rW   rX   r   listtyper   	ComfyNoder   rZ   r2   r0   r   r      sE        
T$r|*<%= 
 
 
 X
 
 
r2   r   r3   c                  "   K   t                      S rT   )r   rZ   r2   r0   comfy_entrypointr      s         r2   )r[   r\   r\   r]   )r)   rL   rA   comfy.model_managementrC   comfy.utilstyping_extensionsr   comfy_api.latestr   r   r   r   ro   rq   r   r   r   rZ   r2   r0   <module>r      s_                 & & & & & & / / / / / / / /)N )N )N )N )NR\ )N )N )NX                  BH H H H Hr| H H H2
 
 
 
 
. 
 
 
! 3 ! ! ! ! ! !r2   