
    +j                         d Z ddlZddlmZ ddlZddlZddlZddlm	Z	m
Z
  G d de
j                  Z G d de
j                  Z G d	 d
e
j                  Z G d de	          ZdefdZdS )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                   J    e Zd Zed             Zedej        fd            ZdS )EmptyARVideoLatentc                    t          j        ddt           j                            ddddd          t           j                            dd	ddd          t           j                            d
dddd          t           j                            dddd          gt           j                            d          g          S )Nr   zmodel/latent/video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_schemaz EmptyARVideoLatent.define_schema   s    y()Wcrt"MMXs2NNXrqdKK\1!DD	 	  h 77
 
 
 	
    returnc                     |dz
  dz  dz   }t          j        |d||dz  |dz  gt          j                                                  }t          j        d|i          S )Nr   r   r      devicesamples)torchzeroscomfymodel_managementintermediate_devicer   
NodeOutput)r*   r	   r   r   r   lat_tlatents          r+   executezEmptyARVideoLatent.execute"   sj    1*"a'UFaK!<)==??
 
 
 }i0111r-   N)__name__
__module____qualname__classmethodr,   r   r9   r<    r-   r+   r   r      sV        
 
 [
 22= 2 2 2 [2 2 2r-   r   c                   N    e Zd ZdZed             Zedej        fd            ZdS )SamplerARVideozSampler 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                     t          j        dddt           j                            ddddd          gt           j                                        g	          S )
NrC   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,   zSamplerARVideo.define_schema3   sg    y$+.)1"X    Z&&(()
 
 
 	
r-   r.   c                 n    d|i}t          j        t          j                            d|                    S )NrE   ar_video)r   r9   r6   samplersksampler)r*   rE   extra_optionss      r+   r<   zSamplerARVideo.executeD   s5     "#6
 }U^44ZOOPPPr-   N	r=   r>   r?   __doc__r@   r,   r   r9   r<   rA   r-   r+   rC   rC   ,   si          
 
 [
  QR] Q Q Q [Q Q Qr-   rC   c                   N    e Zd ZdZed             Zedej        fd            ZdS )
ARVideoI2Va2  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                 x   t          j        ddt           j                            d          t           j                            d          t           j                            d          t           j                            dddd	d
          t           j                            dddd	d
          t           j                            ddddd
          t           j                            dddd          gt           j                            d          t           j                            d          g          S )NrP   zmodel/conditioning/video_models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,   zARVideoI2V.define_schemaT   s    y 6w''U##}--Wcrt"MMXs2NNXrqdKK\1!DD W55	  h 77
 
 
 	
r-   r.   c                 H   t           j                            |d d                             dd          ||dd                              dd          }|                    |d d d d d d d df                   }|                                }	|	j                            di           }
|
                    di           }||d<   |dz
  d	z  dz   }t          j	        |d
||dz  |dz  gt           j
                                                  }t          j        |	d|i          S )Nr   bilinearcenter   transformer_options	ar_configinitial_latentr   r   r0   r1   r3   )r6   utilscommon_upscalemovedimencodeclonemodel_options
setdefaultr4   r5   r7   r8   r   r9   )r*   rR   rS   rT   r	   r   r   r   r`   mtoar_cfgr:   r;   s                 r+   r<   zARVideoI2V.executeh   s)   k00O##B**E6:x
 

'!R.. 	 K111aaa!$<==KKMM_''(=rBB{B//#1 1*"a'UFaK!<)==??
 
 
 }QF 3444r-   NrM   rA   r-   r+   rP   rP   L   sd          
 
 [
& 5TVTa 5 5 5 [5 5 5r-   rP   c                   L    e Zd Zedeeej                          fd            ZdS )ARVideoExtensionr.   c                 .   K   t           t          t          gS N)r   rC   rP   )selfs    r+   get_node_listzARVideoExtension.get_node_list~   s       
 	
r-   N)	r=   r>   r?   r   listtyper   	ComfyNoderp   rA   r-   r+   rl   rl   }   sE        
T$r|*<%= 
 
 
 X
 
 
r-   rl   r.   c                  "   K   t                      S rn   )rl   rA   r-   r+   comfy_entrypointru      s      r-   )rN   r4   typing_extensionsr   comfy.model_managementr6   comfy.samplerscomfy.utilscomfy_api.latestr   r   rs   r   rC   rP   rl   ru   rA   r-   r+   <module>r{      sF     & & & & & &             / / / / / / / /2 2 2 2 2 2 2 26Q Q Q Q QR\ Q Q Q@.5 .5 .5 .5 .5 .5 .5 .5b
 
 
 
 
~ 
 
 
 0      r-   