
    3jN                     J   S SK r S SKrS SKrS SKrS SKJr  S SKJrJ	r	  S SK
Jr  S SKJr  S SKrS SKr " S S\	R                   5      r " S S	\	R                   5      r " S
 S\5      r " S S\	R                   5      r " S S\	R                   5      r " S S\	R                   5      r " S S\	R                   5      rSr " S S\	R                   5      r " S S\	R                   5      r " S S\	R                   5      r " S S\	R                   5      r " S S\5      rS\4S  jrg)!    N)override)ComfyExtensionio)HunyuanVideo15SRModel)LatentUpsamplerc                   T    \ rS rSr\S 5       r\S\R                  4S j5       r\r	Sr
g)CLIPTextEncodeHunyuanDiT   c                     [         R                  " SSS[         R                  R                  S5      [         R                  R                  SSSS9[         R                  R                  SSSS9/[         R
                  R                  5       /S	9$ )
Nr	   z CLIP Text Encode (Hunyuan Image)z model/conditioning/hunyuan imageclipbertT	multilinedynamic_promptsmt5xlnode_iddisplay_namecategoryinputsoutputs)r   SchemaClipInputStringConditioningOutputclss    :/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_hunyuan.pydefine_schema&CLIPTextEncodeHunyuanDiT.define_schema   st    yy.;7f%		$M		4N &&(
 	
    returnc                     UR                  U5      nUR                  U5      S   US'   [        R                  " UR                  U5      5      $ )Nr   )tokenizer   
NodeOutputencode_from_tokens_scheduled)r   r   r   r   tokenss        r    execute CLIPTextEncodeHunyuanDiT.execute   sA    t$--.w7w}}T>>vFGGr#    N__name__
__module____qualname____firstlineno__classmethodr!   r   r'   r*   encode__static_attributes__r,   r#   r    r	   r	      s=    
 
 H2== H H Fr#   r	   c                   X    \ rS rSr\S 5       r\SS\R                  4S jj5       r\r	Sr
g)EmptyHunyuanLatentVideo'   c                    [         R                  " SSS[         R                  R                  SSS[        R
                  SS9[         R                  R                  SS	S[        R
                  SS9[         R                  R                  S
SS[        R
                  SS9[         R                  R                  SSSSS9/[         R                  R                  5       /S9$ )Nr6   zEmpty HunyuanVideo 1.0 Latentmodel/latent/hunyuan videowidthP     defaultminmaxstepheight  length         
batch_size   r>   r?   r@   r   r   r   Intr   nodesMAX_RESOLUTIONLatentr   r   s    r    r!   %EmptyHunyuanLatentVideo.define_schema(   s    yy-81Wcru?S?SZ\]Xs@T@T[]^Xrqe>R>RYZ[\1!F	 		  "
 	
r#   r$   c                     [         R                  " USUS-
  S-  S-   US-  US-  /[        R                  R	                  5       S9n[
        R                  " USS.5      $ )Nr<   rF   rG      devicesamplesdownscale_ratio_spacialtorchzeroscomfymodel_managementintermediate_devicer   r'   r   r:   rB   rD   rH   latents         r    r*   EmptyHunyuanLatentVideo.execute9   sj    j"
q/@A.EvQR{TY]^T^_hmh~h~  iS  iS  iU  V}}ANOOr#   r,   NrF   r.   r/   r0   r1   r2   r!   r   r'   r*   generater4   r,   r#   r    r6   r6   '   s=    
 
  PR]] P P Hr#   r6   c                   b   ^  \ rS rSr\U 4S j5       r\SS\R                  4S jj5       rSr	U =r
$ )EmptyHunyuanVideo15LatentA   c                 N   > [         TU ]  5       nSUl        SUl        SUl        U$ )Nre   zEmpty HunyuanVideo 1.5 Latentr9   )superr!   r   r   r   )r   schema	__class__s     r    r!   'EmptyHunyuanVideo15Latent.define_schemaB   s+    &(4=6r#   r$   c                     [         R                  " USUS-
  S-  S-   US-  US-  /[        R                  R	                  5       S9n[
        R                  " USS.5      $ )N    rF   rG   r<   rS   rU   rX   r^   s         r    r*   !EmptyHunyuanVideo15Latent.executeJ   st     j"
q/@A.EvQS|UZ^`U`ajo  kA  kA  kU  kU  kW  X}}BOPPr#   r,   ra   )r.   r/   r0   r1   r2   r!   r   r'   r*   r4   __classcell__)rj   s   @r    re   re   A   s7      QR]] Q Qr#   re   c                   T    \ rS rSr\S 5       r\SS\R                  4S jj5       rSr	g)HunyuanVideo15ImageToVideoQ   c                 R   [         R                  " SS[         R                  R                  S5      [         R                  R                  S5      [         R                  R                  S5      [         R
                  R                  SSS[        R                  SS	9[         R
                  R                  S
SS[        R                  SS	9[         R
                  R                  SSS[        R                  SS	9[         R
                  R                  SSSSS9[         R                  R                  SSS9[         R                  R                  SSS9/	[         R                  R                  SS9[         R                  R                  SS9[         R                  R                  SS9/S9$ )Nrq    model/conditioning/hunyuan videopositivenegativevaer:   r;   r<   r=   rB   rC   rD   !   rF   rG   rH   rI   rJ   start_imageToptionalclip_vision_outputr   r_   r   r   r   r   )r   r   r   r   VaerL   rM   rN   ImageClipVisionOutputr   rO   r   s    r    r!   (HunyuanVideo15ImageToVideo.define_schemaR   sA   yy07%%j1%%j1U#Wcru?S?SZ\]Xs@T@T[]^Xrqe>R>RYZ[\1!F}t<##))*>)N
 &&J&?&&J&?		  h 7
 	
r#   Nr$   c
                 n   [         R                  " USUS-
  S-  S-   US-  US-  /[        R                  R	                  5       S9n
UGb  [        R
                  R                  US U R                  SS5      XESS5      R                  SS5      nUR                  US S 2S S 2S S 2S S	24   5      n[         R                  " U
R                  S
   SU
R                  S   U
R                  S	   U
R                  S   4[        R                  R	                  5       S9nXS S 2S S 2S UR                  S   2S S 2S S 24'   [         R                  " SSU
R                  S   UR                  S   UR                  S   4UR                  UR                  S9nSUS S 2S S 2S UR                  S
   S-
  S-  S-   24'   [        R                  " XUS.5      n[        R                  " X,US.5      nU	b2  [        R                  " USU	05      n[        R                  " USU	05      n0 nXS'   [        R                   " XU5      $ )Nrm   rF   rG   r<   rS   bilinearcenter   r      rT   dtype        concat_latent_imageconcat_maskr|   rV   )rY   rZ   r[   r\   r]   utilscommon_upscalemovedimr3   shapeonesrT   r   node_helpersconditioning_set_valuesr   r'   )r   ru   rv   rw   r:   rB   rD   rH   ry   r|   r_   encodedr   mask
out_latents                  r    r*   "HunyuanVideo15ImageToVideo.executei   s   j"
q/@A.EvQS|UZ^`U`ajo  kA  kA  kU  kU  kW  X"++44[&5I5Q5QRTVW5XZ_isu}~  G  G  HI  KM  NKjjQ1bqb[!9:G"'++v||AFLLQROU[UaUabcUdflfrfrstfu.v  @E  @V  @V  @j  @j  @l  #mAH1&7w}}Q'7&7A =>::q!V\\!_6I6O6OPR6SUhUnUnoqUrs  }H  }O  }O  Wb  Wh  Wh  iDADDA=+++A.2q8A===>#;;H  CG  GH  IH#;;H  CG  GH  IH)#;;HG[]oFpqH#;;HG[]oFpqH
 &9}}X<<r#   r,   )NN
r.   r/   r0   r1   r2   r!   r   r'   r*   r4   r,   r#   r    rq   rq   Q   sB    
 
, =  @B  @M  @M = =r#   rq   c                   T    \ rS rSr\S 5       r\SS\R                  4S jj5       rSr	g)HunyuanVideo15SuperResolution   c                 ~   [         R                  " SSS[         R                  R                  S5      [         R                  R                  S5      [         R                  R                  SSS9[         R
                  R                  S	SS9[         R                  R                  S
SS9[         R                  R                  S5      [         R                  R                  SSSSSSS9/[         R                  R                  SS9[         R                  R                  SS9[         R                  R                  SS9/S9$ )Nr   z"Hunyuan Video 1.5 Super Resolutionrt   ru   rv   rw   Trz   ry   r|   r_   noise_augmentationgffffff?r         ?{Gz?r>   r?   r@   rA   advancedr}   r   )
r   r   r   r   r   r   r   rO   Floatr   r   s    r    r!   +HunyuanVideo15SuperResolution.define_schema   s    yy3=7%%j1%%j1UT2}t<##))*>)N		)3TsPSZ^imn	 &&J&?&&J&?		  h 7
 	
r#   Nr$   c                    US   nUR                   S   n	[        R                  " UR                   S   U	S-  S-   UR                   S   UR                   S   UR                   S   /[        R                  R                  5       S9n
XS S 2U	S-   SU	-  S-   24'   SU
S S 2SU	-  S-   4'   Ub  [        R                  R                  UR                  SS5      UR                   S   S	-  UR                   S   S	-  S
S5      R                  SS5      nUR                  US S 2S S 2S S 2S S24   5      nXS S 2S U	2S UR                   S   2S S 2S S 24'   SU
S S 2U	S-   S4'   [        R                  " XUS.5      n[        R                  " X*US.5      nUb2  [        R                  " USU05      n[        R                  " USU05      n[        R                  " XU5      $ )NrV   rF   r   r   r   r   rS   r<   r   r   r   r   r   r|   )r   rY   rZ   r[   r\   r]   r   r   r   r3   r   r   r   r'   )r   ru   rv   r_   r   rw   ry   r|   	in_latentin_channelscond_latentr   s               r    r*   %HunyuanVideo15SuperResolution.execute   sO   9%	ooa(kk9??1#5{Q7JIOO\^L_ajapapqsatv  wF  wF  GI  wJ  #K  TY  Tj  Tj  T~  T~  T@  A@IA{Q[1)<<<=./Aq;**+"++44[5H5HQ5OQZQ`Q`acQdgiQiktkzkz{}k~  BD  lD  FP  RZ  [  c  c  de  gi  jKjjQ1bqb[!9:GDK<K<):'--*:):Aq@A12K;?A-.77  ~P  CQ  R77  ~P  CQ  R)#;;HG[]oFpqH#;;HG[]oFpqH}}X88r#   r,   )NNNr   r,   r#   r    r   r      s>    
 
, 9}  ~K  ~K 9 9r#   r   c                   P    \ rS rSr\S 5       r\S\R                  4S j5       rSr	g)LatentUpscaleModelLoader   c                     [         R                  " SSS[         R                  R                  S[        R
                  " S5      S9/[         R                  R                  5       /S9$ )Nr   zLoad Latent Upscale Modelzmodel/loaders
model_namelatent_upscale_modelsoptionsr   )r   r   Combor   folder_pathsget_filename_listLatentUpscaleModelr   r   s    r    r!   &LatentUpscaleModelLoader.define_schema   sZ    yy.4$|\5S5STk5lm %%,,.

 
	
r#   r$   c                 .  ^
 [         R                  " SU5      n[        R                  R	                  USSS9u  m
nST
;   a  T
S   R
                  S   T
S   R
                  S   T
S   R
                  S   [        T
R                  5        Vs/ s H4  oDR                  S	5      (       d  M  UR                  S
5      (       d  M2  UPM6     sn5      SS.nSn[        Xe5      nUR                  T
5        GOST
;   a  T
R                  5        VV	s0 s H  u  pUR                  SSS5      U	_M     sn	nm
T
S   R
                  S   T
S   R
                  S   [        U
4S j[        [        T
R                  5        Vs/ s H4  oDR                  S5      (       d  M  UR                  S5      (       d  M2  UPM6     sn5      5       5       5      S.nSn[        Xe5      nUR                  T
5        OST
;   a  [         R"                  " US   5      n[$        R&                  " U5      R)                  [        R*                  R-                  [.        R0                  [.        R2                  /S9S9nUR5                  T
5        [6        R8                  " W5      $ s  snf s  sn	nf s  snf )Nr   T)	safe_loadreturn_metadatazblocks.0.block.0.conv.weightzin_conv.conv.weightrF   zout_conv.conv.weightr   zblocks.z.block.0.conv.weightF)r   out_channelshidden_channels
num_blocksglobal_residual720pzup.0.block.0.conv1.conv.weightnin_shortcutznin_shortcut.convzconv_in.conv.weightzconv_out.conv.weightc              3   P   >#    U  H  nTS U S3   R                   S   v   M     g7f)up..block.0.conv1.conv.weightr   N)r   ).0isds     r    	<genexpr>3LatentUpscaleModelLoader.execute.<locals>.<genexpr>   s:       ,N  gMabBQC7Q/R,S,Y,YZ[,\  gMs   #&r   r   )
z_channelsr   block_out_channels1080pz%post_upsample_res_blocks.0.conv2.biasconfig)allowed_dtypes)r   )r   get_full_path_or_raiser[   r   load_torch_filer   lenkeys
startswithendswithr   load_sditemsreplacetuplerangejsonloadsr   from_configtor\   	vae_dtyperY   bfloat16float32load_state_dictr   r'   )r   r   
model_pathmetadatakr   
model_typemodelkeyvaluer   s             @r    r*    LatentUpscaleModelLoader.execute   s   !889PR\]
{{22:_c2dH)R/!"78>>qA "#9 : @ @ C#%&;#<#B#B1#E!bggi"zi<<	;R1WXWaWabxWy1i"z{#(F  J)*=EMM"-3_a_g_g_ij_iQ[QT#++n.A1EuL_ijB !67==a@ "#9 : @ @ C&+  ,Nfklo|~  }D  }D  }F  qK  }Fwx  JV  JV  W\  J]qr  bc  bl  bl  mI  bJqr  }F  qK  mL  gM  ,N  'NF
 !J)*=EMM"4:ZZ 23F#//7::AWAWAaAarw  sA  sA  CH  CP  CP  rQAa  BR:  SE!!"%}}U##+ #{ k qKs*   J(J J	 J5JJ*Jr,   Nr   r,   r#   r    r   r      s3    
 
 $BMM $ $r#   r   c                   P    \ rS rSr\S 5       r\S\R                  4S j5       rSr	g)$HunyuanVideo15LatentUpscaleWithModel   c                    [         R                  " SSS[         R                  R                  S5      [         R                  R                  S5      [         R
                  R                  S/ SQSS	9[         R                  R                  S
SSSSS9[         R                  R                  SSSSSS9[         R
                  R                  SSS/S9/[         R                  R                  5       /S9$ )Nr   z*Hunyuan Video 15 Latent Upscale With Modelzmodel/latent/hunyhuan videor   rV   upscale_method)znearest-exactr   areabicubicbislerpr   )r   r>   r:   i   r   i @  rR   r=   rB   i  cropdisabledr   r   r   )r   r   r   r   rO   r   rL   r   r   s    r    r!   2HunyuanVideo15LatentUpscaleWithModel.define_schema   s    yy:E2%%++G4			*/9t  I  JWdu1MXsu1Mv
H/EF 		  "
 	
r#   r$   c                    US:X  a  US:X  a  [         R                  " U5      $ US:X  aH  [        SU5      n[        S[        US   R                  S   U-  US   R                  S   -  5      5      nOfUS:X  aH  [        SU5      n[        S[        US   R                  S   U-  US   R                  S   -  5      5      nO[        SU5      n[        SU5      n[
        R                  R                  US   US-  US-  X65      nUR                  U5      n[         R                  " SUR                  5       R                  5       05      $ )Nr   @   rV   r   r   r<   )r   r'   r@   roundr   r[   r   r   resample_latentcpufloat)r   r   rV   r   r:   rB   r   ss           r    r*   ,HunyuanVideo15LatentUpscaleWithModel.execute   s+   A:&A+==))zRBgi&8&>&>r&BV&KgV_N`NfNfgiNj&j kl1BRwy'9'?'?'Ce'KgV_N`NfNfgiNj'j!klBR**79+=u{FVXLZhoA%%a(A==)QUUW]]_!=>>r#   r,   Nr   r,   r#   r    r   r      s3    
 
$ ?R]] ? ?r#   r   aZ  <|start_header_id|>system<|end_header_id|>

<image>
Describe the video by detailing the following aspects according to the reference image: 1. The main content and theme of the video.2. The color, shape, size, texture, quantity, text, and spatial relationships of the objects.3. Actions, events, behaviors temporal relationships, physical movement changes of the objects.4. background environment, light, style and atmosphere.5. camera angles, movements, and transitions used in the video:<|eot_id|>

<|start_header_id|>user<|end_header_id|>

{}<|eot_id|><|start_header_id|>assistant<|end_header_id|>

c                   T    \ rS rSr\S 5       r\S\R                  4S j5       r\r	Sr
g)#TextEncodeHunyuanVideo_ImageToVideoi  c                 `   [         R                  " SS[         R                  R                  S5      [         R                  R                  S5      [         R
                  R                  SSSS9[         R                  R                  SS	S
SSSS9/[         R                  R                  5       /S9$ )Nr   rt   r   r|   promptTr   image_interleaver   rF   i   zqHow much the image influences things vs the text prompt. Higher number means more influence from the text prompt.)r>   r?   r@   tooltipr   r~   )	r   r   r   r   r   r   rL   r   r   r   s    r    r!   1TextEncodeHunyuanVideo_ImageToVideo.define_schema  s    yy97f%##))*>?		D$O& P!  	 &&(!
 	
r#   r$   c                     UR                  U[        UR                  US9n[        R                  " UR                  U5      5      $ )N)llama_templateimage_embedsr  )r&    PROMPT_TEMPLATE_ENCODE_VIDEO_I2Vmm_projectedr   r'   r(   )r   r   r|   r  r  r)   s         r    r*   +TextEncodeHunyuanVideo_ImageToVideo.execute,  sJ    v6Vew  fE  fE  Xh  i}}T>>vFGGr#   r,   Nr-   r,   r#   r    r   r     s=    
 
, HBMM H H Fr#   r   c                   X    \ rS rSr\S 5       r\SS\R                  4S jj5       r\r	Sr
g)HunyuanImageToVideoi4  c                    [         R                  " SS[         R                  R                  S5      [         R                  R                  S5      [         R
                  R                  SSS[        R                  SS9[         R
                  R                  S	S
S[        R                  SS9[         R
                  R                  SSS[        R                  SS9[         R
                  R                  SSSSS9[         R                  R                  S/ SQSS9[         R                  R                  SSS9/[         R                  R                  SS9[         R                  R                  SS9/S9$ )Nr  rt   ru   rw   r:   r;   r<   r=   rB   rC   rD   5   rF   rG   rH   rI   rJ   guidance_type)v1 (concat)v2 (replace)customT)r   r   ry   rz   r}   r_   r~   )r   r   r   r   r   rL   rM   rN   r   r   r   rO   r   s    r    r!   !HunyuanImageToVideo.define_schema5  s   yy)7%%j1U#Wcru?S?SZ\]Xs@T@T[]^Xrqe>R>RYZ[\1!F8alpq}t<	 &&J&?		  h 7
 	
r#   Nr$   c	                 .   [         R                  " USUS-
  S-  S-   US-  US-  /[        R                  R	                  5       S9n	0 n
UGb1  [        R
                  R                  US U2S S 2S S 2S S24   R                  SS5      X4SS	5      R                  SS5      nUR                  U5      n[         R                  " SSU	R                  S
   UR                  S   UR                  S   4UR                  UR                  S9nSUS S 2S S 2S UR                  S   S-
  S-  S-   24'   US:X  a  XS.nO3US:X  a#  SS0nXS S 2S S 2S UR                  S
   24'   XS'   O
US:X  a  SU0n[        R                  " UW5      nXS'   [        R                   " X5      $ )Nr<   rF   rG   rR   rS   r   r   r   r   r   r   r   r   r   r  r   r  guiding_frame_index
noise_maskr  
ref_latentrV   )rY   rZ   r[   r\   r]   r   r   r   r3   r   r   rT   r   r   r   r   r'   )r   ru   rw   r:   rB   rD   rH   r  ry   r_   r   r   r   conds                 r    r*   HunyuanImageToVideo.executeJ  s   j"
q/@A.EvQR{TY]^T^_hmh~h~  iS  iS  iU  V
"++44[&!QPRQRPRAR5S5[5[\^`a5bdis}  @H  I  Q  Q  RS  UW  XK"%**["9::q!V\\!_6I6O6OPR6SUhUnUnoqUrs  }H  }O  }O  Wb  Wh  Wh  iDADDA=+++A.2q8A===>-/BX.0-q1>Qq!:177:::;+/<((*$&9:#;;HdKH &9}}X22r#   r,   Nr-   r,   r#   r    r  r  4  s=    
 
( 3kmkxkx 3 32 Fr#   r  c                   X    \ rS rSr\S 5       r\SS\R                  4S jj5       r\r	Sr
g)EmptyHunyuanImageLatentig  c                 `   [         R                  " SS[         R                  R                  SSS[        R
                  SS9[         R                  R                  SSS[        R
                  SS9[         R                  R                  S	S
S
SS9/[         R                  R                  5       /S9$ )Nr  zmodel/latent/hunyuan imager:   i   r   rm   r=   rB   rH   rF   rI   rJ   r~   rK   r   s    r    r!   %EmptyHunyuanImageLatent.define_schemah  s    yy-1Wd@T@T[]^XtAUAU\^_\1!F 		  "
 	
r#   r$   c                     [         R                  " USUS-  US-  /[        R                  R	                  5       S9n[
        R                  " SU05      $ )Nr   rm   rS   rV   rX   )r   r:   rB   rH   r_   s        r    r*   EmptyHunyuanImageLatent.executew  sG    j"flERKHQVQgQgQ{Q{Q}~}}i/00r#   r,   Nra   rb   r,   r#   r    r  r  g  s:    
 
 1R]] 1 1 Hr#   r  c                   P    \ rS rSr\S 5       r\S\R                  4S j5       rSr	g)HunyuanRefinerLatenti  c                    [         R                  " SSS[         R                  R                  S5      [         R                  R                  S5      [         R                  R                  S5      [         R
                  R                  SSS	S
SSS9/[         R                  R                  SS9[         R                  R                  SS9[         R                  R                  SS9/S9$ )Nr#  zHunyuan Latent Refinerrt   ru   rv   r_   r   g?r   r   r   Tr   r}   r   )r   r   r   r   rO   r   r   r   s    r    r!   "HunyuanRefinerLatent.define_schema  s    yy*17%%j1%%j1		)3TsPSZ^imn	 &&J&?&&J&?		  h 7
 	
r#   r$   c                 |   US   n[         R                  " XUS.5      n[         R                  " X#US.5      n0 n[        R                  " UR                  S   SUR                  S   UR                  S   UR                  S   /[
        R                  R                  5       S9US'   [        R                  " XU5      $ )	NrV   r   r   rm   r   r   r   rS   )
r   r   rY   rZ   r   r[   r\   r]   r   r'   )r   ru   rv   r_   r   r   s         r    r*   HunyuanRefinerLatent.execute  s    	"77  yK  CL  M77  yK  CL  M
 %V\\!_b&,,rBRTZT`T`acTdflfrfrsufv,w  AF  AW  AW  Ak  Ak  Am  !n
9}}X<<r#   r,   Nr   r,   r#   r    r#  r#    s3    
 
& = = =r#   r#  c                   L    \ rS rSr\S\\\R                        4S j5       r	Sr
g)HunyuanExtensioni  r$   c                 |   #    [         [        [        [        [        [
        [        [        [        [        [        /$ 7fr  )r	   r   r6   re   rq   r   r   r   r  r  r#  )selfs    r    get_node_listHunyuanExtension.get_node_list  s2      %/#%&)0$# 
 	
s   :<r,   N)r.   r/   r0   r1   r   listtyper   	ComfyNoder,  r4   r,   r#   r    r)  r)    s)    
T$r||*<%= 
 
r#   r)  r$   c                     #    [        5       $ 7fr  )r)  r,   r#   r    comfy_entrypointr2    s     s   )rM   r   rY   comfy.model_managementr[   typing_extensionsr   comfy_api.latestr   r   !comfy.ldm.hunyuan_video.upsamplerr   %comfy.ldm.lightricks.latent_upsamplerr   r   r   r0  r	   r6   re   rq   r   r   r   r	  r   r  r  r#  r)  r2  r,   r#   r    <module>r8     s        & / C A  r|| 6bll 4Q 7 Q /= /=d+9BLL +9\.$r|| .$b$?2<< $?P8 !",, @0",, 0fbll 0=2<< =>
~ 
$ 0 r#   