
    
3j3                        S SK r 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  S SK	J
r
  S SKJrJr  S SKJrJrJr  SSKJr  SS	KJr  SS
KJrJr  SSKJr  S r\" SS9 " S S5      5       r\" SS9 " S S5      5       r\" SS9 " S S5      5       r\" SS9 " S S5      5       r\" SS9 " S S\
R@                  5      5       r!g)    N)
LTXAVModel)NestedTensor)io)SamplerCustomAdvancedSplitSigmas)EmptyLTXVLatentVideoLTXVAddGuideLTXVCropGuides   )blur_internal)LTXVAdainLatent)LTXVAddLatentGuideLTXVSelectLatents)
comfy_nodec                 F   [        U S5      (       d  SU R                  ;  a  [        S5      eU R                  S   nUS   S   [        R                  " US   5      //nU R                  S   nUS   S   [        R                  " US   5      //nX$4U l        U R
                  $ )N	raw_condsnegativez?Guider does not have negative conds, cannot use it as a guider.positiver   
cross_attn)hasattroriginal_conds
ValueErrorcopydeepcopyr   )guiderraw_posr   raw_negr   s        K/home/wildlama/comfy/ComfyUI/custom_nodes/ComfyUI-LTXVideo/easy_samplers.py_get_raw_conds_from_guiderr      s    6;''V222Q  ''
3QZ-t}}WQZ/HIJ''
3QZ-t}}WQZ/HIJ$/    LTXVBaseSampler)namec                   V    \ rS rSr\S 5       rSrSrSrSr	            S
S jr
S	rg)r!   "   c                 $   SSS04SSS04SSS[         R                  S	S
.4SSS[         R                  S	S
.4SSS[         R                  SS
.4SSS04SSS04SSS04SSS04S.	SSS04SSS04SSSSSS .4S!S"/S"S#S$.4SS%SS&S'S .4SSSS(S)S .4S*.S+.$ ),NMODELtooltipThe diffusion model to use.VAEThe VAE to use.INTi   @       )defaultminmaxstepi   a   r      GUIDER/The guider to use, must be a STGGuiderAdvanced.SAMPLERThe sampler to use.SIGMASThe sigmas to use.NOISE"The noise to use for the sampling.)	modelvaewidthheight
num_framesr   samplersigmasnoiseIMAGE0The images to use for conditioning the sampling.STRINGz?The indices of the images to use for conditioning the sampling.FLOAT?r   /The strength of the conditioning on the images.r.   r/   r0   r'   centerdisabledz$The crop mode to use for the images.)r.   r'   #   d   z2The CRF value to use for preprocessing the images.
   z3The blur value to use for preprocessing the images.)optional_cond_imagesoptional_cond_indicesstrengthcropcrfblurrequiredoptionalnodesMAX_RESOLUTIONss    r   INPUT_TYPESLTXVBaseSampler.INPUT_TYPES'   sh    "I/L#MN	+<=>#&!$33 "	 #&!$33 "	  "1U5I5ISTU
  QR &	3H'IJ#i1E%FG!I/S#TU? F  RS)
 !#d* #&  #T	 z*#-#I #% "#W	 #$ !#X	I-EP
 P	
r    LATENTCONDITIONINGrb   )denoisedr   r   samplesamplingNc                    [         R                   " U5      n[         R                  " UR                  5      Ul        [        U5      u  nnU
b  [        R
                  R                  U
R                  SS5      UUSUS9R                  SS5      R                  SS5      n
[        R                  R                  R                  X5      S   n
[        U
R                  S   5       H$  n[        U
U   R!                  S5      U5      U
U'   M&     UbN  U
bK  UR#                  S5      nU Vs/ s H  n[%        U5      PM     nn['        U5      ['        U
5      :X  d   S5       eUc  [)        5       R                  X4US5      u  nOUnU
b  SU;   a  UR+                  S5      nU
UUS-   2S S 2S S 2S S24   nUR-                  U5      nUUS	   S S 2S S 2S UR                  S
   24'   SU;  aj  [.        R0                  " SSUS	   R                  S
   SS4[.        R2                  US	   R4                  S9nSU-
  US S 2S S 2S UR                  S
   24'   UUS'   O)SU-
  US   S S 2S S 2S UR                  S
   24'   US   nOS n[7        5       R9                  UU5      u  nn[7        5       R9                  UUU-
  5      u  n n!['        U5      S:  a*  [;        SU5        [=        5       R?                  U	UUUUS9u  n"n#U
bJ  [A        X5       H;  u  n$n%U%S:X  a  M  [B        R                  " UUUUU$R!                  S5      U%US9u  nnnM=     Ub!  [E        5       RG                  UUUUUUUS9u  nnnURI                  UU5        [;        SU 5        [=        5       R?                  U	UUU US9u  n&n'[J        R                  " UUU'S9u  nnn'UU'S'   ['        U!5      S:  a*  [;        SU!5        [=        5       R?                  U	UUU!U'S9u  n"n'U'UU4$ s  snf )Nr   bilinear)rS   r   ,INumber of optional cond images must match number of optional cond indices   samples   
noise_mask)dtypedevice      ?z*Denoising with no conditioning on sigmas: rC   r   rA   rB   latent_imager   r   r=   latentimage	frame_idxrR   r=   r   r   ru   guiding_latent
latent_idxrR   z'Denoising with conditioning on sigmas: r   r   ru   zLDenoising with no conditioning but with classical i2v noise mask on sigmas: )&r   r   r   r   comfyutilscommon_upscalemovedimclampcomfy_extrasnodes_ltLTXVPreprocessexecuterangeshaper   	unsqueezesplitintlenr   indexencodetorchonesfloat32rp   r   
get_sigmasprintr   rd   zipr	   r   generate	set_condsr
   )(selfr<   r=   r>   r?   r@   r   rA   rB   rC   rP   rQ   rR   rS   rT   rU   optional_negative_index_latentsoptional_negative_index optional_negative_index_strengthoptional_initialization_latentsguiding_start_stepguiding_end_stepr   r   ilatentsidx_0encode_pixelstconditioning_latent_frames_maskhigh_sigmasrest_sigmasmiddle_sigmas
low_sigmas_new_latents
cond_imagecond_idxoutput_latentsdenoised_output_latentss(                                           r   rd   LTXVBaseSampler.sample   s   0 6" $f.C.C D7?(+**(00Q7 +  Bq! ! $0#8#8#G#G#O#O$$$  /55a89*7(+55a8$+$Q' :
 !,1E1Q$9$?$?$D!5J$K5JSV5J!$K,-$2  [Z[  +2-/77zSTUJW5G+5J0J)//2E01BAq"1"1LMM

=)A56GIq!\qwwqz\127*27**79-33A61=--"9-443/
 GJHn/1l
l0BC(G%<?(N%aLaggajL&8929,2G/.2+#.=#;#;FDV#W [$/M$<$<),>>%
!z {a>L46=="$  >  Q  +(+$)$
H q=
 !((%%"$..q1&%	)( +6
 #$--!!>29 . 	 	8, 	7G4I4K4R4R   5S 5
10 7E6L6L*7
3(3 1P-z?Q^ ,A+B+I+I!4 ,J ,(Q' (8<<k %Ls   %O4 )NNrH   rL   rM   r   Nrg   rq   Nr     __name__
__module____qualname____firstlineno__classmethodr^   RETURN_TYPESRETURN_NAMESFUNCTIONCATEGORYrd   __static_attributes__r   r    r   r!   r!   "   s[    
 Q
 Q
f >L7LHH ""(, "),(,-g=r    LTXVExtendSamplerc                   \    \ rS rSr\S 5       rSrSrSrSr	               S
S jr
S	rg)r   i*  c                     SSS04SSS04SSS04SS	S
[         R                  SSS.4SSSSSSS.4SSS04SSS04SSS04SSS04SSSSSS.4S .
SSS!04S"SS#04S$SSSSS%S.4S&.S'.$ )(Nr&   r'   r(   r)   r*   ra   z#The latents of the video to extend.r+   P   rg   r   zbIf -1, the number of frames will be based on the number of frames in the optional_guiding_latents.r.   r/   r0   r1   r'         r3   z]The overlap region to use for conditioning the new frames on the end of the provided latents.r4   r5   r6   r7   r8   r9   r:   r;   rG         ?        rq   zaThe strength of the conditioning on the overlapping latents, when using optional_guiding_latents.rJ   )
r<   r=   r   num_new_framesframe_overlapr   rA   rB   rC   rR   z'Optional latents to guide the sampling.rD   rE   )rF   rI   )optional_guiding_latentsrP   rQ   cond_image_strengthrV   rY   r\   s    r   r^   LTXVExtendSampler.INPUT_TYPES.  s1    "I/L#MN	+<=> EF
 #%!$33 ! $H	# #%!" ! $C	"  QR &	3H'IJ#i1E%FG!I/S#TU#&"" $G	E+\  IJ-
  RS) *5#&""#T	([A
 A	
r    r`   denoised_videor   r   rd   re   Nc                 	   [         R                   " U5      n[         R                  " UR                  5      Ul        UbN  UbK  UR                  S5      nU Vs/ s H  n[	        U5      PM     nn[        U5      [        U5      :X  d   S5       e[        U5      u  nnUS   nUR                  u  nnnn n!UR                  u  n"n#n$UU"-  n%US:X  a  Ub  US   R                  S   U%-
  U"-  n[        5       R                  UU%* S5      u  n&Uc'  [        R                  " U!U#-  U U$-  U%U"-  U-   SS9S   n'OUn'U&S   R                  U'S   R                  5      U&S'   [        5       R!                  UUUU'U&SU
S	9u  nnn'[#        5       R%                  UU5      u  n(n)[#        5       R%                  U)UU-
  5      u  n*n+Ubh  ['        S
5        [)        X5       HN  u  n,n-Ub  U-S-  S:X  a  [+        SU- S35      e[,        R                  " UUUU'U,R/                  S5      U-US9u  nnn'MP     [        U(5      S:  a<  UR1                  UU5        ['        SU(5        [3        5       R5                  U	UUU(U'S9u  n.n'UbO  [        5       R                  UU%S5      S   n[        5       R!                  UUUU'UU&S   R                  S   US	9u  nnn'Ub!  [        5       R!                  UUUU'UUUS	9u  nnn'UR1                  UU5        ['        SU*5        [3        5       R5                  U	UUU*U'S9u  n/n0[6        R                  " UUU0S9u  nnn0[        U+5      S:  a  [        5       R!                  UUUU0U&SU
S	9u  nnn0Ubh  ['        S
5        [)        X5       HN  u  n,n-Ub  U-S-  S:X  a  [+        SU- S35      e[,        R                  " UUUU0U,R/                  S5      U-US9u  nnn0MP     UR1                  UU5        ['        SU+5        [3        5       R5                  U	UUU+U0S9u  n.n0[6        R                  " UUU0S9u  nnn0Ub  [9        5       R;                  U0UUUS9S   n0[        5       R                  U0SS5      S   n1[=        5       R?                  UU1U%S-
  SS9u  nUUU4$ s  snf )Nri   rj   rl   rg   rm   r   r>   r?   length
batch_sizer   rx    Adding conditioning on keyframesr3   Conditioning image index z (relative to the in the temporal chunk) is a multiple of 8 + 1,and guiding latents are used. Please provide other conditioning image indicesrt   z4Denoising with overlap conditioning only on sigmas: rr   ,Denoising with full conditioning on sigmas: r{   z@Denoising with overlap + keyframes conditioning only on sigmas: )r   	referencefactor	per_frame)axis) r   r   r   r   r   r   r   r   downscale_index_formular   select_latentsr   r   torp   r   r   r   r   r   r   r   r	   r   r   r   rd   r
   r   batch_normalizeLinearOverlapLatentTransitionprocess)2r   r<   r=   r   r   r   r   rA   rB   rC   rR   guiding_strengthr   r   rP   rQ   optional_reference_latentsr   adain_factorr   r   r   r   r   normalize_per_framer   r   r   rl   batchchannelsframesr?   r>   time_scale_factorwidth_scale_factorheight_scale_factoroverlaplast_overlap_latentsr   r   r   r   r   r   r   r   r   r   !truncated_denoised_output_latentss2                                                     r   rd   LTXVExtendSampler.samplex  s1   6 6" $f.C.C D ,1E1Q$9$?$?$D!5J$K5JSV5J!$K,-$2  [Z[  8?()$18.x'' 	C-/B  #44R$<$H(399!<wF!"N #4"5"D"DgXr#
	 +2.6600 33!22^C	
 K :K*>y*I*L*L	"))+
Y'  ))/ * 
		
 $/=#;#;FDV#W [$/M$<$<),>>%
!z  +45(+$)$
H ,7HqLA<M$3H: >h h  !((%%&$..q1&0	), {aXx0H+V46=="(  >  Q $/'8':'I'I('2(($ #$--!!"7/	:@@C) . 	 +6
 #$--!!">29 . 	 	8, 	<mL4I4K4R4R $ 5S 5
10 7E6L6L*7
3(3 z?Q
 #$--!!.3! . 	' $/89,/(-(J 0;1PQ@Q(7z Bl l  %,,!)!)6(2215"*!4	  /-, Xx0R ,A+B+I+I!4 ,J ,(Q' ;I:P:P!!.;7Hh 7 &1&5&7&G&G/4#-	 'H '
 '# ->,?,N,N#Q-

-)
 34<<6!! = 

 8,,q %Ls   R	r   )r   rq   rq   NNNNNr   Nrg   rq   r   r   Fr   r   r    r   r   r   *  sd     B
 B
H >L=LHH !%!"#'(,(, "),!3x-r    LTXVInContextSamplerc                   T    \ rS rSr\S 5       rSrSrSrSr	           S
S jr
S	rg)r   is  c           	      h    SSS04SSS04SSS04SSS	04S
SS04SSS04S.SSS04SSSSSSS.4S.S.$ )Nr)   r'   r*   r4   r5   r6   r7   r8   r9   r:   r;   ra   zGThe latents to use for guiding the sampling, typically with an IC-LoRA.)r=   r   rA   rB   rC   guiding_latentsrD   zThe image to use for conditioning the sampling, if not provided, the sampling will be unconditioned (t2v setup). The image will be resized to the size of the first frame.r+   rg   r   r   zYIf -1, the number of frames will be based on the number of frames in the guiding_latents.r   )rP   r@   rV   r   r\   s    r   r^    LTXVInContextSampler.INPUT_TYPESx  s     	+<=> QR &	3H'IJ#i1E%FG!I/S#TU!#l$$ !  $P) #%!# !#~	##
 #	
r    r`   r   rd   re   Nc                 \   [         R                   " U5      n[         R                  " UR                  5      Ul        Uc  S nUbN  UbK  UR                  S5      nU Vs/ s H  n[	        U5      PM     nn[        U5      [        U5      :X  d   S5       e[        U5      u  nnUR                  u  nnnUS   R                  u  nnnnnU	S:w  a  U	S-
  U-  S-   nU
b  U
nO)[        R                  " UU-  UU-  US-
  U-  S-   SS9S   n[        5       R                  UU5      u  nn[        5       R                  UUU-
  5      u  n n![        U5      S:  a*  [        SU5        [        5       R                  UUUUUS	9u  n"nUb'  SU;   a!  [!        5       R#                  USS5      S   nS
n#OSn#[        S5        [%        5       R'                  UUUUUU#(       a  SOSUS9u  nnnUbf  [        SU5        [)        Xx5       HK  u  n$n%U%S-  S:X  a  [+        SU% S35      e[,        R                  " UUUUU$R/                  S5      U%US9u  nnnMM     Ub!  [%        5       R'                  UUUUUUUS9u  nnnUR1                  UU5        [        SU 5        [        5       R                  UUUU US	9u  n"n&[2        R                  " UUU&S9u  nnn&[        U!5      S:  aV  UR1                  UU5        [        SU!5        [        5       R                  UUUU!U&S	9u  n"n&[2        R                  " UUU&S9u  nnn&U&UU4$ s  snf )Nri   rj   rl   rg   r   r   r   z8Denoising with keyframes only [if available] on sigmas: rr   TFz&Adding conditioning on guiding latentsrx   r   r3   r   z] is a multiple of 8 + 1 and guiding latents are used. Please provide other cond image indicesrt   r   r{   zEDenoising with keyframes only [if available] conditioning on sigmas: )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rd   r   r   r   r   r   r   r	   r   r   r
   )'r   r=   r   rA   rB   rC   r   rP   rQ   r@   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r?   r>   r   r   r   r   r   r   skip_one_guiding_latentr   r   r   s'                                          r   rd   LTXVInContextSampler.sample  s>   ( 6" $f.C.C D'$(! ,1E1Q$9$?$?$D!5J$K5JSV5J!$K,-$2  [Z[  8?( '' 	C-/B 2A1K1Q1Q.x 1n)::Q>F*69K.6600 33
&77!;	
 K $/=#;#;FDV#W [$/M$<$<),>>%
!z {aJ  56=="(  >  Q !,6K1K/1@@BO '+#&+#67
  ))*3q% * 
		
  +46KL(+$)$
H a<1$$3H:  >[  \  !((%%&$..q1&0	)( +6
 #$--!!">29 . 	 	8, 	<mL'<'>'E'E $ (F (
$# 7E6L6L*7
3(3 z?QXx0W ,A+B+I+I!4 ,J ,(Q' ;I:P:P!!.;7Hh 7 (8<<Y %Ls   L)r   )NNr   NNrg   rq   rq   rq   r   r   r   r   r    r   r   r   s  sV    
 $
 $
L >L=LHH ""(,(, "),%g=r    zLinear transition with overlap)descriptionc                   @    \ rS rSr\S 5       rSrSrSrS r	S
S jr
Srg	)r   iN  c                 ,    SSSSSSS.4S.SSSS	040S
.$ )Nra   r+   r      )r.   r/   r0   )samples1samples2r   r   r.   r   rV   r   r\   s    r   r^   )LinearOverlapLatentTransition.INPUT_TYPESP  s=     ('!q3#GH A/	
 		
r    r   r   zLightricks/latentc                 0    UR                  5       nUS   nU$ )Nrl   )r   )r   rl   r]   s      r   get_subbatch*LinearOverlapLatentTransition.get_subbatchb  s    LLNI,r    c                    U R                  U5      nU R                  U5      n[        R                  " SSUS-   5      SS R                  UR                  5      nS/UR                  5       -  nUR                  S5      Xd'   UR                  U5      n[        S 5      /UR                  5       -  nUR                  5       n[        U* S 5      X'   UR                  5       n	[        SU5      X'   UR                  5       n
[        S U* 5      X'   UR                  5       n[        US 5      X'   U[        U
5         XQ[        U5         -  SU-
  U[        U	5         -  -   U[        U5         /n[        R                  " XS9n[        R                  " SUR                  S   5      R                  [        R                  S9nUUS.4$ )Nr   r   rm   rg   )dim)ro   )rl   batch_index)r   r   linspacer   rp   r   sizereshapeslicer   tuplecataranger   r   )r   r   r   r   r   alphar   	slice_allslice_overlap1slice_overlap2slice_rest1slice_rest2partscombined_samplescombined_batch_indexs                  r   r   %LinearOverlapLatentTransition.processg  s   $$X.$$X. q!Wq[1!B7::8??K hlln$jjme$ 4[MHLLN2	")$gXt4")$Q0nn&!$1nn&!'40 U;'(U>2335yHU>%:;;<U;'(	
 !99U5$||A/?/E/Ea/HILL--  M  
 ,3
 	
r    r   N)r   )r   r   r   r   r   r^   r   r   r   r   r   r   r   r    r   r   r   N  s/    

 

 LH"H
)
r    r   zLTXV Normalizing Samplerc                   P    \ rS rSr\S 5       r\S\R                  4S j5       rSr	g)LTXVNormalizingSampleri  c                    [         R                  " SS[         R                  R                  S5      [         R                  R                  S5      [         R
                  R                  S5      [         R                  R                  S5      [         R                  R                  S5      [         R                  R                  SS	S
9[         R                  R                  SSS
9/[         R                  R                  SS9/S9$ )Nr  utilityrC   r   rA   rB   rs   video_normalization_factorsz1,1,1,1,1,1,1,1)r.   audio_normalization_factorsz1,1,0.25,1,1,0.25,1,1denoised_output)display_name)node_idcategoryinputsoutputs)
r   SchemaNoiseInputGuiderSamplerSigmasLatentStringOutput)clss    r   define_schema$LTXVNormalizingSampler.define_schema  s    yy,w'		)

  +		)		/		1;L    		1;R    		  .? @!
 	
r    returnc           	      h   UR                   R                  R                  R                  R                  S:w  a
  [        5       eUR                   R                  R                  nUR                  S5      nUR                  S5      nU V	s/ s H  n	[        U	5      PM     nn	U V	s/ s H  n	[        U	5      PM     nn	[        U5      S-
  n
[        U5      U
:  a3  [        U5      S:  a$  UR                  US   /U
[        U5      -
  -  5        [        U5      U
:  a3  [        U5      S:  a$  UR                  US   /U
[        U5      -
  -  5        [        [        Xg5      5       VVVs/ s H  u  nu  pUS:w  d  US:w  d  M  US-   PM     nnnn[        SU-  SS	9  S
 nU" XN5      n[        SU-  SS	9  SnU H  nU[        U5      S-
  -  n[        SU-  SS	9  [        5       R                  UUUUUS9u  nnUR                  US   R                   S 5      u  nnUS-
  [        U5      :  d  Mu  US-
  [        U5      :  d  M  UXkS-
     -  nUX{S-
     -  n[#        UR%                  UU5      5      US'   [        SUXkS-
     X{S-
     4-  SS	9  M     [&        R(                  " U5      $ s  sn	f s  sn	f s  snnnf )Nr   ri   r   r   rg   rq   zSampling split indices: %sT)flushc                     U(       d  U /$ [        [        U5      5      n/ nSnU H   nXE:  a  UR                  XUS-    5        UnM"     U[        U 5      :  a  UR                  XS 5        U$ )z
Splits arr into chunks according to indices (split points).
Indices are treated as starting a new chunk at each index in the list.
r   r   N)sortedsetappendr   )arrindicessplit_pointschunksprevidxs         r   split_by_indices8LTXVNormalizingSampler.execute.<locals>.split_by_indices  sq    
 u!#g,/LFD#:MM#S1W"56 $ c#hc%j)Mr    zSigmas chunks: %szSampling with sigmas %srr   rl   z<After %d steps, the latent image was normalized by %f and %f)model_patcherr<   diffusion_model	__class__r   r   r   floatr   extend	enumerater   r   r   r    separate_audio_and_video_latentstensorsr   !recombine_audio_and_video_latentsr   
NodeOutput)r%  rC   r   rA   rB   rs   r  r  ltxavr   
sigmas_lenr   vasampling_split_indicesr5  sigmas_chunkssigmas_chunkr   video_samplesaudio_sampless                        r   r   LTXVNormalizingSampler.execute  s,      &&66@@II ,"0066FF&A&G&G&L#&A&G&G&L#(C'
(CfE&M(C 	$ '
 )D'
(CfE&M(C 	$ '

 [1_
*+j8./!3+22045!C(C$DDF *+j8./!3+22045!C(C$DDF '/M"
	6A Cx18	 AE 	 "
 	*-CC4P	$ )H!M1>)L\"Q&&A+|<DI 5 7 ? ?#) !@ !Q ,1+Q+QY'//,(M= 1us677AEC+E = !.0KPQE0R R -0KPQE0R R*6;;%}+Y'
 R3E:3E: 1 *D }}\**s'
'
("
s   	J#$J(%J->	J-r   N)
r   r   r   r   r   r&  r   r@  r   r   r   r    r   r  r    s9    
 
, n+ 
n+ n+r    r  )"r   r|   r   rZ   r   comfy.ldm.lightricks.av_modelr   comfy.nested_tensorr   comfy_api.latestr   !comfy_extras.nodes_custom_samplerr   r   comfy_extras.nodes_ltr   r	   r
   guider   latent_normr   r   r   r   nodes_registryr   r   r!   r   r   r   	ComfyNoder  r   r    r   <module>rT     s         4 ,  P T T   ( : & 	B= B=B=J 	C- C-C-L
 	U= U=U=p 89A
 A
 :A
H 23G+R\\ G+ 4G+r    