
    
3j2                     z    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	J
r
  SSKJr  \" SS9 " S	 S5      5       rg)
    N)SamplerCustomAdvanced)LTXVAddGuideLTXVCropGuides   )LTXVAddLatentGuideLTXVSelectLatents)
comfy_nodeLTXVTiledSampler)namec                   F    \ 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SSSSSS	S	S
S.4SS	S	S
S.4SS	S	SS.4SSSSSS.4SSS04SS/SS04S.SSSS04SSS04S.S.$ )N)MODEL)VAE)NOISE)SAMPLER)SIGMAS)GUIDER)LATENTINTr      )defaultminmax   FLOATg333333?g        g      ?g{Gz?)r   r   r   stepBOOLEANr   Fcenterdisabled)modelvaenoisesamplersigmasguiderlatentshorizontal_tilesvertical_tilesoverlaplatents_cond_strengthboost_latent_similaritycrop)IMAGESTRING00.9)optional_cond_imagesoptional_cond_indicesimages_cond_strengths)requiredoptional )ss    K/home/wildlama/comfy/ComfyUI/custom_nodes/ComfyUI-LTXVideo/tiled_sampler.pyINPUT_TYPESLTXVTiledSampler.INPUT_TYPES   s     $#'%%&%*!A,N$O#(a!*L"M!q1#EF $SdK*
 &, #J/)Z1HI', )3*2Y4D)E*2Y4F)G-
 	
    )r   r   )outputdenoised_outputsamplesamplingNc                 *   US   nUR                   u  nnnnnUR                  u  nnnUb]  UU-  nUU-  n[        R                  R	                  UR                  SS5      UUSUS9R                  SS5      nUR                   u  nnnnO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R                  S5       Vs/ s H  n[        U5      PM     nnUb9  [        U5      [        U5      :  a!  UUS   /[        U5      [        U5      -
  -  -   nUU	S-
  U
-  -   U	-  n UUS-
  U
-  -   U-  n![        R                  " U5      n"[        R                  " U5      n#[        R                  " U5      n$ UR                  u  n%n&[        U	5       GH  n'[        U5       GH  n(U(U!U
-
  -  n)U'U U
-
  -  n*U(US-
  :  a  [!        U)U!-   U5      OUn+U'U	S-
  :  a  [!        U*U -   U5      OUn,U,U*-
  n-U+U)-
  n.[#        S	U' S
U( S35        [#        SU* SU, SU) SU+ S3	5        [#        SU- SU. 35        US S 2S S 2S S 2U*U,2U)U+24   n/SU/0n0U0R%                  5       n1UGb  U*U-  n2U,U-  n3U)U-  n4U+U-  n5U%R%                  5       n6U&R%                  5       n7['        [)        UUU5      5       H  u  n8u  n9n:n;U9U2U32U4U52S S 24   R+                  S5      n<[#        SU8 SU' S
U( SU; SU: S35        [#        SU2 SU3 SU4 SU5 S3	5        [#        SU<R                    35        [-        5       R/                  U6U7UU0U<U:U;S9u  n6n7n0M     U(       a  US-
  S-  n=[1        5       R3                  U1U=U=S9S   n>[1        5       R3                  U1SSS9S   n?[#        SU= SU 35        [5        5       R7                  UU6U7U0U>U=US9u  n6n7n0[#        SUS-
   SU 35        [5        5       R7                  UU6U7U0U?US-
  US9u  n6n7n0[$        R$                  " U5      nUR9                  U6U75        [;        5       R=                  UUUUU0S9S   n@Ub  [?        5       R/                  W6W7W@S9u  n6n7n@[        R@                  " U/5      nAU(S:  aP  [        RB                  " SSU
U/RD                  S 9nBWAS S 2S S 2S S 2S S 2S U
24==   UBRG                  SSSSS5      -  ss'   U(US-
  :  aQ  [        RB                  " SSU
U/RD                  S 9nBWAS S 2S S 2S S 2S S 2U
* S 24==   UBRG                  SSSSS5      -  ss'   U'S:  aP  [        RB                  " SSU
U/RD                  S 9nCWAS S 2S S 2S S 2S U
2S S 24==   UCRG                  SSSSS5      -  ss'   U'U	S-
  :  aQ  [        RB                  " SSU
U/RD                  S 9nCWAS S 2S S 2S S 2U
* S 2S S 24==   UCRG                  SSSSS5      -  ss'   U"S S 2S S 2S S 2U*U,2U)U+24==   W@S   WA-  -  ss'   U#S S 2S S 2S S 2U*U,2U)U+24==   U@S   UA-  -  ss'   U$S S 2S S 2S S 2U*U,2U)U+24==   UA-  ss'   GM     GM     U"U$S!-   -  n"U#U$S!-   -  n#SU"0SU#04$ s  snf s  snf ! [         a    [        S5      ef = f)"Nsamplesr   bicubic)r-   ,zINumber of optional cond images must match number of optional cond indiceszXGuider does not have raw conds, cannot use it as a guider. Please use STGGuiderAdvanced.zProcessing tile at row z, col :z  Position: (z, )z  Size: xr   z*Applying image conditioning on cond image z for tile at row z with strength z at position z  Image tile position: (z  Image tile size: )positivenegativer"   latentimage	frame_idxstrength   )rB   start_index	end_indexz;using LTXVAddLatentGuide on tiled latent with latent index z and strength )r"   rI   rJ   rK   guiding_latent
latent_idxrN   )r#   r&   r$   r%   latent_image)rI   rJ   rK   )deviceg:0yE>)$shapedownscale_index_formulacomfyutilscommon_upscalemovedimsplitintlenfloattorch
zeros_like	raw_condsAttributeError
ValueErrorranger   printcopy	enumeratezip	unsqueezer   executer   select_latentsr   generate	set_condsr   r?   r   	ones_likelinspacerU   view)Dselfr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r2   r3   r4   rB   batchchannelsframesheightwidthtime_scale_factorwidth_scale_factorheight_scale_factor
img_height	img_widthcond_images	img_batchimg_channelsibase_tile_heightbase_tile_widthr=   r>   weightsrI   rJ   vhh_startv_starth_endv_endtile_height
tile_widthtiletile_latentsunconditioned_tile_latentsimg_h_start	img_h_endimg_w_start	img_w_endtile_positivetile_negativei_cond_image
cond_imagecond_image_idxcond_image_strengthimg_tilemiddle_latent_idxmiddle_index_latentlast_index_latentdenoised_tiletile_weightsh_blendv_blendsD                                                                       r9   r?   LTXVTiledSampler.sample=   s   * )$18.x'' 	C-/B  +"55J 22I++44$,,R3 5  gan  >I=N=N:Iz9lK ,1E1Q$9$?$?$D!5J$K5JSV5J!$K,-$2  [Z[  4I3N3Ns3S T3Saq3S T+4I0JS N
 1
 %:%b)=)*S1F-GG=I %I!
 #nq&8G%CCV $4q$8G#CCHXX !!'***73""7+	!'!1!1Hh ~&A+,89/'9:
 +a// /159  >A-- "22F;  $go"W_
/s&1=>gYawb	5'KLQzl;< q!Qwu}DE !*40-9->->-@* *"),?"?K %(; ;I"),>">K %(: :I %-MMOM$,MMOM #K)>@UV	 '"&+
 $.'	1;y3H!K$#)A, ! HVghigjjpqrps  tC  DW  CX  Xe  ft  eu  uv  w 6{m1YKrR]Q^^_`i_jjkl  3HNN3CDE )N22%2%2 ##/"*&4%8 3 	))()< /-3aZA,=).?.A.P.P$>(9&7 /Q / 	/+
 ->,?,N,N$>(*&( -O - 	-)
 YZkYllz  |Q  {R  S /099 #%2%2#/+>'8%: : 	))( YZ`abZbYccq  sH  rI  J /099 #%2%2#/+<'-z%: : 	))( "YYv.F$$]MB !6 7 > >!#!!- !? ! ! *
 '(00!.!., 1 	%%%  %t4 q5#nnQ74;;OG Aq!XgX!56',,q!QPQSU:VV6'!++#nnQ74;;OG Aq!gXY!677<<1aQRTV;WW7 q5#nnQ74;;OG Aq(7(A!56',,q!QPRTU:VV6~))#nnQ74;;OG Aq7()Q!677<<1aQSUV;WW7 q!Qwu}<=!),|;=  1a EF!),|;F
 1a=>,N>_ - 'f 7T>*)Wt^<6"Y$@@@o %L
 !U*  	0 	s   %Y2.Y7 Y< <Zr7   )r    Nr0   r1   )__name__
__module____qualname____firstlineno__classmethodr:   RETURN_TYPESRETURN_NAMESFUNCTIONCATEGORYr?   __static_attributes__r7   r<   r9   r
   r
      sE    
 
 
<LL
 HH  !!##CAr<   )rg   rX   r`   !comfy_extras.nodes_custom_samplerr   comfy_extras.nodes_ltr   r   r'   r   r   nodes_registryr	   r
   r7   r<   r9   <module>r      sA       C > : & 	qA qAqAr<   