
    
3jAB                         S SK r S SKrSSKJr  \" SS9 " S S5      5       rS\S\S	\S
\4S jrS\S\S\4S jr\" SS9 " S S\5      5       rg)    N   )
comfy_nodeLTXVTiledVAEDecode)namec                   B    \ 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           	      d    SSSSSSS.4SSSSS.4SSSSS.4SS	S
04S.SS/S	S04/ SQS	S04S.S.$ )N)VAE)LATENTINTr      )defaultminmaxr   BOOLEANr   F)vaelatentshorizontal_tilesvertical_tilesoverlaplast_frame_fixcpuautofloat16float32r   )working_deviceworking_dtype)requiredoptional ss    N/home/wildlama/comfy/ComfyUI/custom_nodes/ComfyUI-LTXVideo/tiled_vae_decode.pyINPUT_TYPESLTXVTiledVAEDecode.INPUT_TYPES   sx      &%*!A,N$O#(a!*L"M!q1#EF#,y%.@"A %*6?Y4G"H"@9fBU!V
 	
    IMAGEimagedecodelatentc	                    US   n	U(       a*  U	S S 2S S 2SS 2S S 2S S 24   n
[         R                  " X/SS9n	U	R                  u  ppnUR                  u  nnnSUS-
  U-  -   nUU-  nUU-  nXS-
  U-  -   U-  nXS-
  U-  -   U-  nS nS nUS:X  a  U	R                  OUnUS:X  a  U	R
                  nO-US:X  a  [         R                  nOUS:X  a  [         R                  n[         R                  " UUUUS	4UWS
9n[         R                  " UUUUS4UUS
9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#[        R                  " SU SU S35        [        R                  " SU SU! SU SU  S3	5        [        R                  " 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n%UR                  U%S   5      n&UU-  n'U!U-  n(UU-  n)U U-  n*U(U'-
  n+U*U)-
  n,[         R                  " UUU+U,S4U&R                  U&R
                  S
9n-UU-  n.UU-  n/US:  aP  [         R                  " SSU/U&R                  S9n0U-S S 2S S 2S S 2S U/2S S 24==   U0R!                  SSSSS5      -  ss'   UUS-
  :  aQ  [         R                  " SSU/U&R                  S9n0U-S S 2S S 2S S 2U/* S 2S S 24==   U0R!                  SSSSS5      -  ss'   US:  aP  [         R                  " SSU.U&R                  S9n1U-S S 2S S 2S U.2S S 2S S 24==   U1R!                  SSSSS5      -  ss'   UUS-
  :  aQ  [         R                  " SSU.U&R                  S9n1U-S S 2S S 2U.* S 2S S 2S S 24==   U1R!                  SSSSS5      -  ss'   US S 2S S 2U'U(2U)U*2S S 24==   U&U--  R#                  UU5      -  ss'   US S 2S S 2U'U(2U)U*2S S 24==   U-R#                  UU5      -  ss'   GM     GM     UUS-   -  nUR!                  UU-  UUUR                  S   5      nU(       a  US U* 2S S 2S S 24   nU4$ )Nsamples   )dimr   r   r   r      devicedtypez"Processing VAE decode tile at row z, col :z  Position: (z, )z  Size: xr   )r5   g:0yE>)torchcatshapedownscale_index_formular5   r6   r   r   zerosranger   logginginfor,   oneslinspaceviewto)2selfr   r   r   r   r   r   r   r   r/   
last_framebatchchannelsframesheightwidthtime_scale_factorwidth_scale_factorheight_scale_factorimage_framesoutput_heightoutput_widthbase_tile_heightbase_tile_widthoutputweightstarget_devicetarget_dtypevhh_startv_starth_endv_endtile_height
tile_widthtiletile_latentsdecoded_tileout_h_start	out_h_endout_w_start	out_w_endtile_out_heighttile_out_widthtile_weightsoverlap_out_hoverlap_out_wh_blendv_blends2                                                     r$   r,   LTXVTiledVAEDecode.decode%   s    )$ !1bc1aJ ii 51=G18.'' 	C-/B FQJ*;;; !4411 #q&8G%CCV q$8G#CCHXX *8F*BF""==Li' ==Li' ==L !

 ++L-qA 
 ~&A+,89/'9:
 +a// /159  >A-- "22F;  $go"W_
A!F1#QOP}WIQugRy%PQRSx}Aj\BC q!Qwu}DE !*40  #zz,y*AB &(;;!$77	%(::!$66	 #,k"9!*[!8$zzL/>1M'..&,,  !(*= = '*< < q5#nn1mL4G4GG !Aq.=.!!;<1aQA < '!++#nn1mL4G4GG !Aq=./1!<=1aQB =
 q5#nn1mL4G4GG !A~~q!!;<1b!QA < ~))#nn1mL4G4GG !A~1!<=1b!QB =
 q![2K	4I1LM </"]L12M
 q+i/Y1FI!__]LAB w - 'B 	'D.  L -v||B?O
 ////A56Fyr'   r!   N)r   r   )__name__
__module____qualname____firstlineno__classmethodr%   RETURN_TYPESRETURN_NAMESFUNCTIONCATEGORYr,   __static_attributes__r!   r'   r$   r   r      s:    
 
 
  LLHH nr'   chunk_starttemporal_tile_lengthtemporal_overlaptotal_latent_framesc                     U S:X  a  [        X-   U5      nU nXT4$ [        SX-
  S-
  5      nX-
  n[        X-   U-
  U5      nXT4$ )aR  Compute chunk boundaries for temporal tiling.

Args:
    chunk_start: Starting frame index for the current chunk
    temporal_tile_length: Length of each temporal tile
    temporal_overlap: Number of frames to overlap between chunks
    total_latent_frames: Total number of latent frames

Returns:
    Tuple of (overlap_start, chunk_end)
r   r   )r   r   )rz   r{   r|   r}   	chunk_endoverlap_startextra_framess          r$   compute_chunk_boundariesr      sm    " a:<OP	# ## A{=AB"2.=
	
 ##r'   r   rM   tile_out_framesc                     SX-  -   nX2-   nX44$ )a  Calculate temporal output boundaries for the decoded tile.

Args:
    overlap_start: Starting frame index including overlap
    time_scale_factor: Time scaling factor from VAE
    tile_out_frames: Number of frames in the decoded tile

Returns:
    Tuple of (out_t_start, out_t_end)
r   r!   )r   rM   r   out_t_start	out_t_ends        r$   $calculate_temporal_output_boundariesr      s$     m77K -I!!r'    LTXVSpatioTemporalTiledVAEDecodec                   L    \ 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S04SSS04SSS	S
SS.4SS	SS
SS.4SSSSSS.4SS	SS
SS.4SSSS.4SS/SSS.4/ SQSSS.4S.	0$ )Nr   r
   tooltipzThe VAE to use.r   zThe latent samples to decode.r      r   r   z<The number of spatial tiles to use, horizontal and vertical.)r   r   r   r   r   z9The overlap between the spatial tiles. (in latent frames)   r1   i  zlThe length of the temporal tile to use for the sampling, in latent frames, including the overlapping region.z9The overlap between the temporal tiles, in latent frames.r   FzJIf true, the last frame will be repeated and discarded after the decoding.)r   r   r   r   z>The device to use for the decoding. auto->same as the latents.r   zAThe data type to use for the decoding. auto->same as the latents.)	r   r   spatial_tilesspatial_overlapr{   r|   r   r   r   r!   r"   s    r$   r%   ,LTXVSpatioTemporalTiledVAEDecode.INPUT_TYPES  s     	+<=>$y2Q&RS#$  #a	" #$  #^	$ #% # $R	) #$  #^	% #(#o# FO#)#c# 3#)#f"k<>
 >	
r'   r(   r*   decode_spatial_temporalr-   c
                    XVS-   :  a  [        S5      eUS   n
U
R                  u  ppnUR                  u  nnnSUS-
  U-  -   nUU-  nUU-  nUS:X  a  U
R                  OUnU	S:X  a  U
R                  nO-U	S:X  a  [
        R                  nOU	S:X  a  [
        R                  n[
        R                  " UUUUS4UWS9nUnS	nUU:  Gao  [        UXVU5      u  nnUU-
  n[        R                  " S
U SU SU S35        U
S S 2S S 2UU24   nSU0nU R                  UUUUUUUU	S9S	   S    n US	:X  a  U US S 2S U R                  S   24'   OU R                  S   S:X  a  [        S5      eU S S 2SS 24   n [        UUU R                  S   5      u  n!n"UU-  n#[
        R                  " S	SU#S-   U R                  U R                  S9SS n$U$R                  SSSSS5      n%U!U#-   n&U S S 2S U#24   n'US S 2U!U&24==   SU%-
  -  ss'   US S 2U!U&24==   U%U'-  -  ss'   U S S 2U#S 24   US S 2U&U"24'   UnUU:  a  GMo  UR                  UU-  UUUR                  S   5      nU4$ )Nr   z>Temporal tile length must be greater than temporal overlap + 1r/   r   r   r   r3   r4   r   zProcessing temporal chunk: r7   z (z latent frames))r   r   r   r   r   r   r   r   z.Dropping first frame but tile has only 1 framer1   r0   )
ValueErrorr<   r=   r5   r6   r:   r   r   emptyr   r@   rA   r,   r   rC   rD   )(rF   r   r   r   r   r{   r|   r   r   r   r/   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rW   rX   rU   r}   rz   r   r   chunk_framesra   rb   rc   r   r   overlap_framesframe_weightsrj   after_overlap_frames_startoverlap_outputs(                                           r$   r   8LTXVSpatioTemporalTiledVAEDecode.decode_spatial_temporal]  sf     Q"66P 
 )$18.'' 	C-/B FQJ*;;; !4411*8F*BF""==Li' ==Li' ==L  !

 %//'?1EX($M9
 %}4LLL-m_Ai[<.Xgh
 1ay!889D &t,L  ;;$,!.'--+ ' 	 	 	L a5Aq1L..q1112  %%a(A-$%UVV+AqrE2 *N!#4l6H6H6K*&Y
 "24E!E %"Q&'..&,,! B!  -11!RAqA-8>-I*!-a..@!Aq+&@@@AQEUUAq+&@@@A >1A CO~&Cq4Y>>?
 $KE //J L -v||B?O
 yr'   r!   N)r   r   r   r   Fr   r   )rp   rq   rr   rs   rt   r%   ru   rv   rw   rx   r   ry   r!   r'   r$   r   r     sJ    
 ?
 ?
B LL(HH @r'   )	r@   r:   nodes_registryr   r   intr   r   r   r!   r'   r$   <module>r      s      & 	H HHV $ $ $  $ 	 $F""+."AD", 	+K'9 KKr'   