
    3j	                         S r SSKrSSKJr  SSKrSSKrSSKrSSKJ	r	J
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)aM  
ComfyUI nodes for autoregressive video generation (Causal Forcing, Self-Forcing, etc.).
  - EmptyARVideoLatent: create 5D [B, C, T, H, W] video latent tensors
  - SamplerARVideo: SAMPLER for the block-by-block autoregressive denoising loop
  - ARVideoI2V: image-to-video conditioning for AR models (seeds KV cache with start image)
    N)override)ComfyExtensionioc                   P    \ rS rSr\S 5       r\S\R                  4S j5       rSr	g)EmptyARVideoLatent   c                 f   [         R                  " SS[         R                  R                  SSSS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S9/[         R                  R                  SS9/S9$ )Nr   zmodel/latent/autoregressivewidth@         defaultminmaxstepheight  lengthQ            
batch_size@   r   r   r   LATENTdisplay_namenode_idcategoryinputsoutputs)r   SchemaIntInputLatentOutputclss    ;/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_ar_video.pydefine_schema EmptyARVideoLatent.define_schema   s    yy(2Wcrt"MXs2NXrqdK\1!D	 		  h 7
 	
    returnc                     US-
  S-  S-   n[         R                  " USXRS-  US-  /[        R                  R	                  5       S9n[
        R                  " SU05      $ )Nr   r   r      devicesamples)torchzeroscomfymodel_managementintermediate_devicer   
NodeOutput)r+   r
   r   r   r   lat_tlatents          r,   executeEmptyARVideoLatent.execute"   s_    1*"a'UaK!<))==?
 }}i011r/    N)
__name__
__module____qualname____firstlineno__classmethodr-   r   r;   r>   __static_attributes__r@   r/   r,   r   r      s3    
 
 22== 2 2r/   r   c                   T    \ rS rSrSr\S 5       r\S\R                  4S j5       r	Sr
g)SamplerARVideo,   zSampler for autoregressive video models (Causal Forcing, Self-Forcing).

All AR-loop parameters are owned by this node so they live in the workflow.
Add new widgets here as the AR sampler grows new options.
c                     [         R                  " SSS[         R                  R                  SSSSSS9/[         R                  R                  5       /S	9$ )
NrH   zSampler AR Videozmodel/sampling/samplersnum_frame_per_blockr   r   ziFrames per autoregressive block. 1 = framewise, 3 = chunkwise. Must match the checkpoint's training mode.)r   r   r   tooltip)r!   r   r"   r#   r$   )r   r%   r&   r'   Samplerr)   r*   s    r,   r-   SamplerARVideo.define_schema3   s[    yy$+.)1"X   ZZ&&()
 	
r/   r0   c                 r    SU0n[         R                  " [        R                  R	                  SU5      5      $ )NrK   ar_video)r   r;   r8   samplersksampler)r+   rK   extra_optionss      r,   r>   SamplerARVideo.executeD   s3     "#6
 }}U^^44ZOPPr/   r@   NrA   rB   rC   rD   __doc__rE   r-   r   r;   r>   rF   r@   r/   r,   rH   rH   ,   s=     
 
  QR]] Q Qr/   rH   c                   T    \ rS rSrSr\S 5       r\S\R                  4S j5       r	Sr
g)
ARVideoI2VL   a"  Image-to-video setup for AR video models (Causal Forcing, Self-Forcing).

VAE-encodes the start image and stores it in the model's transformer_options
so that sample_ar_video can seed the KV cache before denoising.
Uses the same T2V model checkpoint -- no separate I2V architecture needed.
c                 R   [         R                  " SS[         R                  R                  S5      [         R                  R                  S5      [         R
                  R                  S5      [         R                  R                  SSSS	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S9/[         R                  R                  SS9[         R                  R                  SS9/S9$ )NrX   z!model/conditioning/autoregressivemodelvaestart_imager
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   MODELr   r   r    )	r   r%   Modelr'   VaeImager&   r)   r(   r*   s    r,   r-   ARVideoI2V.define_schemaT   s    yy 8w'U#}-Wcrt"MXs2NXrqdK\1!D W5		  h 7
 	
r/   r0   c                    [         R                  R                  US S 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UR                  5       n	U	R                  R                  S0 5      n
U
R                  S0 5      nXS'   US-
  S	-  S-   n[        R                  " US
XS-  US-  /[         R                  R                  5       S9n[        R                  " U	SU05      $ )Nr   bilinearcenter   transformer_options	ar_configinitial_latentr   r   r2   r3   r5   )r8   utilscommon_upscalemovedimencodeclonemodel_options
setdefaultr6   r7   r9   r:   r   r;   )r+   r[   r\   r]   r
   r   r   r   rj   mtoar_cfgr<   r=   s                 r,   r>   ARVideoI2V.executeh   s    kk00O##B*E:x

'!R. 	 K1a!$<=KKM__''(=rB{B/#1 1*"a'UaK!<))==?
 }}QF 344r/   r@   NrU   r@   r/   r,   rX   rX   L   s=     
 
& 5TVTaTa 5 5r/   rX   c                   L    \ rS rSr\S\\\R                        4S j5       r	Sr
g)ARVideoExtension}   r0   c                 ,   #    [         [        [        /$ 7fN)r   rH   rX   )selfs    r,   get_node_listARVideoExtension.get_node_list~   s      
 	
s   r@   N)rA   rB   rC   rD   r   listtyper   	ComfyNoder|   rF   r@   r/   r,   rw   rw   }   s)    
T$r||*<%= 
 
r/   rw   r0   c                     #    [        5       $ 7frz   )rw   r@   r/   r,   comfy_entrypointr      s     s   )rV   r6   typing_extensionsr   comfy.model_managementr8   comfy.samplerscomfy.utilscomfy_api.latestr   r   r   r   rH   rX   rw   r   r@   r/   r,   <module>r      sk     &    /2 26QR\\ Q@.5 .5b
~ 
 0 r/   