
    
3j"                         S SK r S SKrS SKJr   S SKJr  / SQr/ SQrS r	SS jr
SS jrSS jrS	 r\\S
.r\\
S.r " S S\R                   5      rg! \ a    Sr NGf = f)    N)io)standard_staticstandard_static_balancedstandard_uniformlooped_uniformbatchedbatched_shifted)pyramidrelativeflatzoverlap-linearhanngaussianc                 B    [        [        U 5      S S S S2   S5      S-  $ )N064b   l            )int)vals    ]/home/wildlama/comfy/ComfyUI/custom_nodes/ComfyUI-KJNodes/nodes/context_windows_visualizer.py_ordered_halvingr      s%    #c(4$B$'+w77    c                    [        U 5      n[        UR                  5      nX4::  a  [        [        U5      5      /$ [	        U[        UR                  5      -  5      U-  n/ nSnUS:  a&  UR                  [        [        SU5      5      5        UnXs:  a:  [        Xt-   U5      nUR                  [        [        Xx5      5      5        UnXs:  a  M:  U$ )Nr   )	r   context_lengthlistrangeroundr   _stepappendmin)	
num_frameshandlermodel_optionsFLoffsetwindowsstartends	            r   create_windows_batched_shiftedr)      s    JAG""#AvU1X1'667!;FGEztE!V,-.
)%)QtE%-./ ) Nr   c           
         [        U 5      n[        UR                  5      n[        UR                  5      nX4::  a  [        [	        U5      5      /$ [        SXE-
  5      nX4-
  * U-  * S-   n/ n[	        U5       HM  n	[        R                  " XU-
  -  US-
  -  S-   5      n
UR                  [        [	        XU-   5      5      5        MO     U$ )N         ?)	r   r   context_overlapr   r   maxmathfloorr   )r    r!   r"   r#   r$   overlapstride_targetNr&   ir'   s              r   create_windows_static_balancedr5   '   s    JAG""#A'))*GvU1X1;'ME(m
#$q(AG1X

1A;!a%0367tE%345  Nr   c                 @   S/U -  n[        [        [        [        USS5      =(       d    S5      S5      U 5      nUS:  a  U(       a  [        US-
  S5      n[        U5      S:  aN  [	        U5       H?  n[        SS[
        R                  " [
        R                  U-  U-  5      -
  -  S5      XX'   MA     Ubh  [        U5      US-
  :  aV  [	        U5       HG  n[        SS[
        R                  " [
        R                  Xx-
  -  U-  5      -
  -  S5      XPU-
  U-   '   MI     U$ )N      ?r-   r   r+   r,   gBA8)r   r.   r   getattrr   r/   cospi)	lengthfull_lengthidxsr!   kwargsweightsr1   denomjs	            r   create_weights_hannrB   7   s    efnG#c''+<a@EAFJFSG!|GaK#t9q=7^ DHHTWWq[55H,I(I!JER
 $"s4y;?'B7^03C1txxSXS\H]`eHe?f;f4gin0o(1,- $Nr   c                     U S::  a  S/U -  $ U S-
  S-  n[        U S-  S5      n[        U 5       Vs/ s H$  n[        R                  " SXB-
  U-  S-  -  5      PM&     sn$ s  snf )Nr+   r7   g       @g      @gư>g      r   )r.   r   r/   exp)r;   r>   cstdr4   s        r   create_weights_gaussianrG   F   sh    {uv~	!sA
fslD
!C;@=I=aDHHTaes]q001=IIIs   +A)r   r   )r   r	   c                   V    \ rS rSr\S 5       r\ SS\R                  4S jj5       rSr	g)ContextWindowsVisualizerKJV   c                    [         R                  " SSSSS[         R                  R                  SSSS9[         R                  R                  S	S
S/S
SS9[         R
                  R                  S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 S9[         R
                  R                  S!S"SSS#S9[         R                  R                  S$[        S%S&9[         R
                  R                  S'SSS(S)S9[         R                  R                  S*S+S,S-9[         R                  R                  S.[        S/S0S9[         R                  R                  S1S+S2SS39[         R                  R                  S4SS5SS39[         R                  R                  S6S7S8SS39/[         R                  R                  SS9S:9/S;9$ )<NrI   zContext Windows Visualizer (KJ)zKJNodes/misczInteractive visualization of ComfyUI context windows: window placement, overlap, fuse weights and per-frame blending, updated live. Optionally applies the settings to a model like the core Context Windows (Manual) node.Tmodelz?Optional model to apply the context windows to during sampling.)optionaltooltipframe_unitspixellatentzeUnits for the frame counts below. 'pixel' converts to latent via ((n - 1) // temporal_downscale) + 1.)optionsdefaultrN   dimr   r      zTLatent temporal dimension: 2 for WAN-like, 0 for AnimateDiff-style. Model path only.)rS   r   r.   rN   advancedtemporal_downscale   r+      zUVAE temporal compression for pixel->latent. WAN/Hunyuan/CogVideoX=4, Mochi=6, LTXV=8.)rS   r   r.   rN   r       i zVTotal frames to visualize. Ignored on the model path (the real latent length is used).r   Q   zWindow length.r-      zOverlap between windows.context_schedulezWindow placement. standard_static_balanced: static with windows spread evenly (uniform seams, any frame count). batched_shifted: zero-overlap windows whose seams roll each step (~1.0x compute, pairs with freenoise). Uniform schedules also shift per step.)rR   rN   context_stride    zJMax stride power for uniform schedules (adds dilated windows: 1, 2, 4...).closed_loopFz<Wrap looped-schedule windows around the end back to frame 0.)rS   rN   fuse_methodr
   z$How overlapping windows are blended.causal_window_fixzHPrepend an anchor frame to non-0 windows, stripped after the model call.)rS   rN   rV   	freenoisezAFreeNoise noise shuffling for smoother blending. Model path only.cond_retain_index_list zComma-separated window-relative indices that keep their original conditioning, e.g. '0' reuses the start frame's cond per window. No viz effect; model path only.zIModel with context windows applied; only valid when a model is connected.)display_namerN   )node_idrf   categorydescriptionis_experimentalinputsoutputs)r   SchemaModelInputComboIntSCHEDULE_OPTIONSBooleanFUSE_OPTIONSStringOutput)clss    r   define_schema(ContextWindowsVisualizerKJ.define_schemaW   s4   yy0:#R !w  @A  B}w6ISZ (O  PUA1!  F\  gk  l11!%|  ~\3A6  Tl  m-rqfVfg.vWqr1;K  VV  W-qaR  R^  _

    HF   G}lI  `F  G

  !4e  NX  cg   h

  d  EH  SW   X		 8"  Or  }A  B!& W  ?J  K5
 	
r   Nreturnc                     Uc  [         R                  " S 5      $ [        c  [        S5      eUS:X  a?  [	        [        U5      S5      n[	        US-
  U-  S-   S5      n[	        US-
  U-  S-   S5      nU[        ;   a  [        R                  " U[        U   5      nO[        R                  " U5      nU	[        ;   a  [        R                  " U	[        U	   5      nO[        R                  " U	5      nUR                  5       n[        UUUUUUU
UUUS9
n[        R                  " [        R                   R"                  5      nUR%                  5        VVs0 s H  u  nnUUR&                  ;   d  M  UU_M     nnn[        R                   " S0 UD6nUUR(                  S'   [        R*                  " U5        U(       a+  [-        [        S5      (       a  [        R.                  " U5        [         R                  " U5      $ s  snnf )	Nz<comfy.context_windows not available in this ComfyUI version.rP   r+   r   )
r]   ra   r   r-   r^   r`   rT   rc   rb   rd   context_handlercreate_sampler_sample_wrapper )r   
NodeOutputcontext_windows	Exceptionr.   r   LOCAL_SCHEDULESContextScheduleget_matching_context_schedule
LOCAL_FUSEContextFuseMethodget_matching_fuse_methodclonedictinspect	signatureIndexListContextHandler__init__items
parametersr"   create_prepare_sampling_wrapperhasattrr}   )rw   rO   rW   r    r   r-   r]   r^   r`   ra   rT   rb   rc   rd   rL   fschedulefusehandler_kwargssigkvr!   s                          r   execute"ContextWindowsVisualizerKJ.executex   s    ===&&"Z[['!C*+Q/A >A#5!";q!@!DN!Oa$7A#="BAFO.&667GYiIjkH&DDEUVH*$"44[*[BYZD";;KHD%)+)#/#9
  G G P PQ+9+?+?+AY+A41aQ#..EX$!Q$+AY!99KNK18-.77>2QRR99%@}}U## Zs   G:-G:r~   )re   N)
__name__
__module____qualname____firstlineno__classmethodrx   r   r   r   __static_attributes__r~   r   r   rI   rI   V   sB    
 
@ nr)$wy  xE  xE)$ )$r   rI   )N)NNN)r   r/   comfy_api.latestr   comfy.context_windowsr   ImportErrorrr   rt   r   r)   r5   rB   rG   r   r   	ComfyNoderI   r~   r   r   <module>r      s      3 G T8
& J *7NO
 >5L$ L$]  Os   A AA