
    
3jʩ                     ^   S SK r S SKrS SKrS SKJs  Jr  S SKJr  SSK	J
r
  SSKJrJr  SSKJr  SSKJr  SSKJr  SS	KJr  S
SKJrJrJrJr  S
SKJrJrJr  SSKJ r   SSKJ!r!  \RD                  " \#5      r$   S1S jr% " S S\5      r& " S S\5      r' " S S\5      r( " S S\5      r) " S S\5      r* " S S\5      r+ " S S\5      r, " S S \5      r- " S! S"\5      r. " S# S$\.5      r/ " S% S&\.5      r0 " S' S(\5      r1 " S) S*\.5      r2 " S+ S,\.5      r3 " S- S.\.5      r4 " S/ S0\.5      r5g)2    N)tqdm   )
FrozenDict)ClassifierFreeGuidanceClassifierFreeZeroStarGuidance)HeliosTransformer3DModel)HeliosScheduler)logging)randn_tensor   )
BlockStateLoopSequentialPipelineBlocksModularPipelineBlocksPipelineState)ComponentSpec
InputParamOutputParam   )calculate_shift)HeliosModularPipelinec	           	         Uc  [         R                  " US9nO[        U[        5      (       a  US   nUu  pnX-  n[         R                  " XS9SU-   -  [         R
                  " XUS9U-  -
  nU[         R                  " XS9S-  -  nUR                  5       n[         R                  R                  U5      nX-  U-  X:-  -  XK-  -  n[         R                  " XUR                  US9R                  U5      nUUR                  -  nUR                  XX#U
-  XK-  X5      nUR                  SSSSSS	S
5      R                  XX#U5      nU$ )zGenerate spatially-correlated block noise for pyramid upsampling correction.

Uses a multivariate normal distribution with covariance based on `gamma` to produce noise with block structure,
matching the upsampling artifacts that need correction.
)devicer   r   g:0yE>r   	generatorr   r            )torch	Generator
isinstancelisteyeonesfloatlinalgcholeskyrandnr   toTviewpermutereshape)
batch_sizechannel
num_framesheightwidthgamma
patch_sizer   r   _phpw
block_sizecovLblock_numberznoises                     d/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/modular_pipelines/helios/denoise.pysample_block_noiser>   *   sA   & OO62		It	$	$aL	IA2J 			*,E	:UZZ
gm=nqv=vv  599Z/$66C
))+Cc"A'*4EULLY5E5EQZ[^^_efAGEJJzJ"ekSUZEMM!Q1aA.66zJ`efEL    c                       \ rS rSrSrSr\S\4S j5       r\S\	\
   4S j5       r\S\	\   4S j5       r\R                  " 5       S\S	\S
\4S j5       rSrg)HeliosChunkHistorySliceStepZ   zSlices history latents into short/mid/long for a T2V chunk.

At k==0 with no image_latents, creates a zero prefix. Otherwise uses image_latents (either provided or captured
from first chunk by HeliosChunkUpdateStep).
heliosreturnc                      g)NzT2V history slice: splits history into long/mid/short. At k==0 with no image_latents, creates a zero prefix; otherwise uses image_latents as prefix for short history. selfs    r=   description'HeliosChunkHistorySliceStep.descriptionc   s    _	
r?   c           	          [        SS[        SS9[        SS[        SS9[        SS[        R                  S	S9[        S
S[
        S9/$ )Nkeep_first_frameT7Whether to keep the first frame as a prefix in history.default	type_hintrI   history_sizes=Sizes of long/mid/short history buffers for temporal context.requiredrP   rI   history_latents1Accumulated history latents from previous chunks.latent_shaperT   rP   )r   boolr!   r   TensortuplerG   s    r=   inputs"HeliosChunkHistorySliceStep.inputsj   s`     "U	 [	 !,,O	 ~F'
 	
r?   c                     / $ NrF   rG   s    r=   intermediate_outputs0HeliosChunkHistorySliceStep.intermediate_outputs       	r?   
componentsblock_statekc           
          UR                   nUR                  nUR                  nUR                  nUR                  u  ppnU(       a  UR
                  S S 2S S 2[        U5      * S 24   R                  USS9u  pnUc/  US:X  a)  [        R                  " UU	SUUU[        R                  S9nOUn[        R                  " UU/SS9nO3UR
                  S S 2S S 2[        U5      * S 24   R                  USS9u  pnUUl        Xl        Xl        X4$ )Nr   dimr   r   )r   dtype)rL   rQ   image_latents_execution_devicerW   rU   sumsplitr   zerosfloat32catlatents_history_shortlatents_history_midlatents_history_long)rH   rc   rd   re   rL   rQ   rj   r   r-   num_channels_latentsr4   h_latentw_latentrs   rr   latents_history_1xlatents_prefixrq   s                     r=   __call__$HeliosChunkHistorySliceStep.__call__   s4   &77#11#11--BMBZBZ?
!xLWLgLg1s=))++MeMqe) J 7I $a!&(!--" "/$)II~?Q.RXY$Z!OZOjOj1s=))++PeMqe) M 7L -B)*='+?(&&r?   rF   N__name__
__module____qualname____firstlineno____doc__
model_namepropertystrrI   r!   r   r\   r   r`   r   no_gradr   r   intry   __static_attributes__rF   r?   r=   rA   rA   Z   s     J
S 
 
 
Z( 
 
. d;&7   ]]_"'#8 "'z "'VY "' "'r?   rA   c                       \ rS rSrSrSr\S\4S j5       r\S\	\
   4S j5       r\S\	\   4S j5       r\R                  " 5       S\S	\S
\4S j5       rSrg)HeliosI2VChunkHistorySliceStep   zSlices history latents into short/mid/long for an I2V chunk.

Always uses image_latents as prefix (assumes history pre-seeded with fake_image_latents).
rC   rD   c                      g)NzyI2V history slice: splits pre-seeded history into long/mid/short, always using image_latents as prefix for short history.rF   rG   s    r=   rI   *HeliosI2VChunkHistorySliceStep.description   s    F	
r?   c           
          [        SS[        SS9[        SS[        SS9[        SS[        R                  S	S9[        S
S[        R                  SS9/$ )NrL   TrM   rN   rQ   rR   rS   rU   rV   rj   z5First-frame latents used as prefix for short history.)r   rY   r!   r   rZ   rG   s    r=   r\   %HeliosI2VChunkHistorySliceStep.inputs   sl     "U	 [	 !,,O	 ,,S	'
 	
r?   c                     / $ r_   rF   rG   s    r=   r`   3HeliosI2VChunkHistorySliceStep.intermediate_outputs   rb   r?   rc   rd   re   c                 |   UR                   nUR                  nUR                  nU(       aJ  UR                  S S 2S S 2[	        U5      * S 24   R                  USS9u  pxn	[        R                  " Xi/SS9n
O3UR                  S S 2S S 2[	        U5      * S 24   R                  USS9u  pxn
Xl        Xl	        Xrl
        X4$ )Nr   rg   )rL   rQ   rj   rU   rl   rm   r   rp   rq   rr   rs   )rH   rc   rd   re   rL   rQ   rj   rs   rr   rw   rq   s              r=   ry   'HeliosI2VChunkHistorySliceStep.__call__   s    &77#11#11LWLgLg1s=))++MeMqe) J 7I %*II}.QWX$Y!OZOjOj1s=))++PeMqe) M 7L -B)*='+?(&&r?   rF   Nr{   rF   r?   r=   r   r      s    
 J
S 
 
 
Z( 
 
8 d;&7   ]]_'#8 'z 'VY ' 'r?   r   c                       \ rS rSrSrSr\S\4S j5       r\S\	\
   4S j5       r\R                  " 5       S\S\S	\4S
 j5       rSrg)HeliosChunkNoiseGenStep   z7Generates noise latents for a chunk using randn_tensor.rC   rD   c                     g)NzEGenerates random noise latents at full resolution for a single chunk.rF   rG   s    r=   rI   #HeliosChunkNoiseGenStep.description   s    Vr?   c                 L    [        SS[        S9[         R                  " S5      /$ )NrW   TrX   r   )r   r[   templaterG   s    r=   r\   HeliosChunkNoiseGenStep.inputs   s(     ~F,
 	
r?   rc   rd   re   c                     UR                   n[        UR                  UR                  U[        R
                  S9Ul        X4$ )Nr   r   ri   )rk   r   rW   r   r   ro   latentsrH   rc   rd   re   r   s        r=   ry    HeliosChunkNoiseGenStep.__call__  sA    --*$$0E0Ef\a\i\i
 &&r?   rF   Nr|   r}   r~   r   r   r   r   r   rI   r!   r   r\   r   r   r   r   r   ry   r   rF   r?   r=   r   r      sw    AJWS W W 
Z( 
 
 ]]_'#8 'z 'VY ' 'r?   r   c                       \ rS rSrSrSr\S\4S j5       r\S\	\
   4S j5       r\R                  " 5       S\S\S	\4S
 j5       rSrg)HeliosPyramidChunkNoiseGenStepi  zBGenerates noise latents and downsamples to smallest pyramid level.helios-pyramidrD   c                      g)NzuGenerates random noise at full resolution, then downsamples to the smallest pyramid level via bilinear interpolation.rF   rG   s    r=   rI   *HeliosPyramidChunkNoiseGenStep.description  s    8	
r?   c                 n    [        SS[        S9[        S/ SQ[        SS9[         R                  " S5      /$ )	NrW   TrX    pyramid_num_inference_steps_list
   r   r   ,Number of denoising steps per pyramid stage.rN   r   )r   r[   r!   r   rG   s    r=   r\   %HeliosPyramidChunkNoiseGenStep.inputs  s>     ~F2$J	 ,	
 		
r?   rc   rd   re   c                    UR                   nUR                  u  pVpxn	[        UR                  UR                  U[        R
                  S9n
XpU
R                  SSSSS5      R                  XW-  XkU5      n
[        [        UR                  5      S-
  5       H'  nUS-  nUS-  n[        R                  " XU4SS9S-  n
M)     U
R                  XWXkU5      R                  SSSSS5      Ul        X4$ )	Nr   r   r   r   r   r   bilinearsizemode)rk   rW   r   r   r   ro   r+   r,   rangelenr   Finterpolater   )rH   rc   rd   re   r   r-   rt   num_latent_framesru   rv   r   hwr4   s                 r=   ry   'HeliosPyramidChunkNoiseGenStep.__call__%  s    --R]RjRjO
*;x$$0E0Ef\a\i\i

 1//!Q1a0889WYmrsts;GGH1LMA!GA!GAmmGa&zJQNG N &oojMafghppq!Q
 &&r?   rF   Nr   rF   r?   r=   r   r     st    L!J
S 
 
 

Z( 

 

 ]]_'#8 'z 'VY ' 'r?   r   c                       \ rS rSrSrSr\S\4S j5       r\S\	\
   4S j5       r\S\	\   4S j5       r\R                  " 5       S\S	\S
\4S j5       rSrg)HeliosChunkSchedulerResetStepi<  z7Resets the scheduler with timesteps for a single chunk.rC   rD   c                     g)NzXResets the scheduler with the correct timesteps and shift parameter (mu) for this chunk.rF   rG   s    r=   rI   )HeliosChunkSchedulerResetStep.descriptionA  s    ir?   c                 $    [        S[        5      /$ )N	scheduler)r   r	   rG   s    r=   expected_components1HeliosChunkSchedulerResetStep.expected_componentsE  s     +7
 	
r?   c                 t    [        SS[        S9[         R                  " SSS9[         R                  " S5      /$ )NmuTrX   sigmas)rT   num_inference_steps)r   r$   r   rG   s    r=   r\   $HeliosChunkSchedulerResetStep.inputsK  s9     tde<48 56
 	
r?   rc   rd   re   c                     UR                   nUR                  R                  UR                  XBR                  UR
                  S9  UR                  R                  Ul        X4$ )N)r   r   r   )rk   r   set_timestepsr   r   r   	timestepsr   s        r=   ry   &HeliosChunkSchedulerResetStep.__call__S  s_    --**++FCUCUZeZhZh 	+ 	
 !+ 4 4 > >&&r?   rF   Nr|   r}   r~   r   r   r   r   r   rI   r!   r   r   r   r\   r   r   r   r   r   ry   r   rF   r?   r=   r   r   <  s    AJjS j j 
T-%8 
 

 
Z( 
 
 ]]_'#8 'z 'VY ' 'r?   r   c                       \ rS rSrSrSr\S\4S j5       r\S\	\
   4S j5       r\S\	\   4S j5       r\R                  " 5       S\S	\S
\4S j5       rSrg)HeliosChunkDenoiseInneric  zLInner timestep loop for denoising a single chunk, using guider for guidance.rC   rD   c                      g)NzInner denoising loop that iterates over timesteps for a single chunk. Uses the guider to manage conditional/unconditional forward passes with cache_context, applies guidance, and runs scheduler step.rF   rG   s    r=   rI   #HeliosChunkDenoiseInner.descriptionh  s    9	
r?   c           
      v    [        S[        5      [        S[        5      [        S[        [	        SS05      SS9/$ )Ntransformerr   guiderguidance_scale      @from_configconfigdefault_creation_methodr   r   r	   r   r   rG   s    r=   r   +HeliosChunkDenoiseInner.expected_componentsp  B     -)AB+7&!#3S"9:(5		
 		
r?   c           
      ^   [         R                  " S5      [         R                  " S5      [        S[        R                  S9[        S[        R                  S9[         R                  " S5      [         R                  " S5      [         R                  " S5      [         R                  " S	5      /$ )
Nr   r   prompt_embedsrP   negative_prompt_embedsdenoiser_input_fieldsr   attention_kwargsr   )r   r   r   rZ   rG   s    r=   r\   HeliosChunkDenoiseInner.inputs}  s     	*,%,,?/5<<H 78 56 23,	
 		
r?   rc   rd   re   c                 \   UR                   nUR                  nUR                  nUR                  R                  n[        U5      XaR                  R                  -  -
  nSUR                  UR                  40n	[        [        R                  " UR                  R                  5      R                  R                  5       5      n
0 nUR                   R#                  5        H  u  pX;   d  M  X;  d  M  XU'   M     UR$                  R'                  U5      US'   UR(                  R'                  U5      US'   UR*                  R'                  U5      US'   UR,                  US'   [/        US9 n[1        U5       GH  u  nnUR3                  UR4                  S   5      R'                  [6        R8                  5      nUR'                  U5      nUR:                  R=                  XUS9  UR:                  R?                  U	5      nU H  nUR:                  RA                  UR                  5        U	R                  5        Vs0 s H  o3[C        UU5      _M     nn[C        UUR:                  RD                  5      nUR                  RG                  U5         UR                  " SUUS	S
.UDUD6S   Ul$        S S S 5        UR:                  RK                  UR                  5        M     UR;                  U5      S   nUR                  RM                  UUUURN                  S	S9S   nU[        U5      S-
  :X  d/  US-   U:  d  GM  US-   UR                  R                  -  S:X  d  GM  URQ                  5         GM     S S S 5        XBl         X4$ s  snf ! , (       d  f       N= f! , (       d  f       N-= f)Nencoder_hidden_statesrq   rr   rs   r   totalr   stepr   timestepFhidden_statesr   return_dictr   r   r   rF   ))r   r   r   r   ri   r   r   orderr   r   setinspect	signatureforward
parameterskeysr   itemsrq   r(   rr   rs   r   r   	enumerateexpandshaper   int64r   	set_stateprepare_inputsprepare_modelsgetattr_identifier_keycache_context
noise_predcleanup_modelsr   r   update)rH   rc   rd   re   r   r   r   transformer_dtypenum_warmup_stepsguider_inputstransformer_argsshared_kwargs
field_namefield_valueprogress_baritr   latent_model_inputguider_stateguider_state_batchcond_kwargscontext_namer   s                           r=   ry    HeliosChunkDenoiseInner.__call__  su   %%))	)==&2288y>,?BVBVB\B\,\\ $k&?&?AcAc%d

 w001G1G1O1OP[[``bc'2'H'H'N'N'P#J-*2Q,7j) (Q
 2=1R1R1U1UVg1h-./:/N/N/Q/QRc/d+,0;0P0P0S0STe0f,-,7,H,H()+,!),188GMM!$4588E%,ZZ0A%B"!!++fg+h)00??N*6&%%44Z5K5KLN[N`N`Nb"cNbg.@!&D#DNbK"c#*+=z?P?P?`?`#aL#//==lK8B8N8N 9*<%-(-9 *	9
 ,9 9*5 L %%44Z5K5KL +7 (..|<Q?
 %..33)33 % 4   I**U..AEZ=Q=Q=W=W3W[\3\ '')I - -N &&&= #d LK -,sD   -CN=N
=N!N	1BN>N!NN
NN
N+rF   Nr   rF   r?   r=   r   r   c  s    VJ
S 
 
 

T-%8 

 

 

Z( 

 

 ]]_B'#8 B'z B'VY B' B'r?   r   c                       \ rS rSrSrSr\S\4S j5       r\S\	\
   4S j5       r\S\	\   4S j5       r\R                  " 5       S\S	\S
\4S j5       rSrg)HeliosPyramidChunkDenoiseInneri  a-  Nested pyramid stage loop with inner timestep denoising.

For each pyramid stage (small -> full resolution):
1. Upsample latents + block noise correction (stages > 0)
2. Compute mu from current resolution, set scheduler timesteps
3. Run timestep denoising loop (same logic as HeliosChunkDenoiseInner)
r   rD   c                      g)NzPyramid denoising inner block: loops over pyramid stages from smallest to full resolution. Each stage upsamples latents (with block noise correction), recomputes scheduler parameters, and runs the timestep denoising loop.rF   rG   s    r=   rI   *HeliosPyramidChunkDenoiseInner.description  s    4	
r?   c                 x    [        S[        5      [        S[        5      [        S[        [	        SSS.5      SS9/$ )	Nr   r   r   r   r   )r   zero_init_stepsr   r   )r   r   r	   r   r   rG   s    r=   r   2HeliosPyramidChunkDenoiseInner.expected_components  sA     -)AB+7.!SQ"OP(5		
 		
r?   c                 ,   [         R                  " S5      [        S[        R                  S9[        S[        R                  S9[         R                  " S5      [        S/ SQ[        SS	9[         R                  " S
5      [         R                  " S5      /$ )Nr   r   r   r   r   r   r   r   rN   r   r   )r   r   r   rZ   r!   rG   s    r=   r\   %HeliosPyramidChunkDenoiseInner.inputs  s{     	*%,,?/5<<H 782$J	  23,
 	
r?   rc   rd   re   c                 X   UR                   nUR                  R                  nUR                  n[	        UR
                  5      nSUR                  UR                  40n[        [        R                  " UR                  R                  5      R                  R                  5       5      n	0 n
UR                  R                  5        H  u  pX;   d  M  X;  d  M  XU'   M     UR                   R#                  U5      U
S'   UR$                  R#                  U5      U
S'   UR&                  R#                  U5      U
S'   UR(                  U
S'   [+        UR,                  SS 5      n[/        U5       GH  nUb  US:  a  SUR,                  l        UR                  R2                  R4                  nUR6                  S   UR6                  S	   -  UR6                  S
   -  US   US   -  US   -  -  n[9        UUR:                  R2                  R=                  SS5      UR:                  R2                  R=                  SS5      UR:                  R2                  R=                  SS5      UR:                  R2                  R=                  SS5      5      nUR
                  U   nUR:                  R?                  UUUUS9  UR:                  R@                  nUS:  GaP  UR6                  u  nnnnnUS-  nUS-  nURC                  SSSSS5      RE                  UU-  UUU5      n[F        RH                  " UUU4SS9nURE                  UUUUU5      RC                  SSSSS5      nSUR:                  RJ                  U   -
  nUR:                  R2                  RL                  nS[N        RP                  " SSU-  -   5      SU-
  -  U-   -  nUSU-
  -  [N        RP                  " U5      -  nUR6                  u  nnnnn [S        UUUUU UUUURT                  S9	n!U!R#                  US9n!UU-  UU!-  -   n[	        U5      UUR:                  RV                  -  -
  n"[Y        US9 n#[[        U5       GH  u  n$n%U%R]                  UR6                  S   5      R#                  [^        R`                  5      n&UR#                  U5      n'UR,                  Rc                  U$UU%S9  UR,                  Re                  U5      n(U( H  n)UR,                  Rg                  UR                  5        UR                  5        V*s0 s H  n*U*[+        U)U*5      _M     n+n*[+        U)UR,                  Rh                  5      n,UR                  Rk                  U,5         UR                  " S!U'U&SS.U+DU
D6S   U)l6        S S S 5        UR,                  Ro                  UR                  5        M     UR-                  U(5      S   n-UR:                  Rq                  U-U%UURT                  SS 9S   nU$[	        U5      S-
  :X  d/  U$S-   U":  d  GM  U$S-   UR:                  RV                  -  S:X  d  GM  U#Rs                  5         GM     S S S 5        GM     Ub  XR,                  l        Xbl        X4$ s  sn*f ! , (       d  f       N= f! , (       d  f       GM>  = f)"Nr   rq   rr   rs   r   r  r   r   r   base_image_seq_len   max_image_seq_len   
base_shift      ?	max_shiftffffff?)r   r   r   r   nearestr   r   ri   r   r   Fr   r   rF   ):rk   r   ri   r   r   r   r   r   r   r   r   r   r   r   r   r   rq   r(   rr   rs   r   r   r   r   r  r   r3   r   r   r   getr   r   r+   r,   r   r   ori_start_sigmasr2   mathsqrtr>   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ).rH   rc   rd   re   r   r  r   pyramid_num_stagesr  r  r  r  r  orig_zero_init_stepsi_sr3   image_seq_lenr   r   r   r-   rt   r/   	current_h	current_wnew_hnew_w	ori_sigmar2   alphabetar   r   r<   r  r  r	  r
  r   r  r  r  kkr  r  r   s.                                                 r=   ry   'HeliosPyramidChunkDenoiseInner.__call__  s   --&2288%% !M!MN $k&?&?AcAc%d

 w001G1G1O1OP[[``bc'2'H'H'N'N'P#J-*2Q,7j) (Q
 2=1R1R1U1UVg1h-./:/N/N/Q/QRc/d+,0;0P0P0S0STe0f,-,7,H,H()
  'z'8'8:KTR+,C $/C!G45
!!1 $//66AAJ$]]2.r1BBW]]SUEVV1
1-
1=M !$$++//0DcJ$$++//0CTJ$$++//cB$$++//TBB #."N"Ns"S  ..#	 /  #,,66I QwU\UbUbR
0*i!A!A!//!Q1a8@@+-A9i --uen9U!//*jBVX]_demmq!Q
 
 4 4 E Ec JJ	",,3399TYYqAI71y=IIUVI.51AAEL]]B
0*a*(!)33
 '89'/D5L8  #9~0CjFZFZF`F`0``/0L%i0DAq xxa(89<<U[[IH)04E)F&%%//QL_jk/l#-#4#4#C#CM#RL.:*"))889O9OPUbUgUgUi&jUirr73Er+J'JUi&j'./A:CTCTCdCd'e'33AA,O<F<R<R =.@)1,1= #.	=
 #0=  =!.9 P #))889O9OP /; ",!2!2<!@!CJ )2277""-"7"7$) 8  G C	NQ..Q"22AAUAUA[A[7[_`7`$++-I 1 10I -X  +0D-%&&E 'k PO 10sD   CZ Z7=Z4!ZBZ"ZZZ
ZZ
Z)	rF   Nr   rF   r?   r=   r  r    s     "J
S 
 
 

T-%8 

 

 
Z( 
 
  ]]_M'#8 M'z M'VY M' M'r?   r  c                       \ rS rSrSrSr\S\4S j5       r\S\	\
   4S j5       r\S\	\   4S j5       r\R                  " 5       S\S	\S
\4S j5       rSrg)HeliosChunkUpdateStepi  zDUpdates chunk collection and history after denoising a single chunk.rC   rD   c                      g)NzPost-denoising update step: appends the denoised latents to the chunk list, captures image_latents from the first chunk if needed, and extends history_latents.rF   rG   s    r=   rI   !HeliosChunkUpdateStep.description  s    b	
r?   c                     / $ r_   rF   rG   s    r=   r   )HeliosChunkUpdateStep.expected_components  rb   r?   c                 ~    [        S[        R                  S9[        S[        R                  S9[        SS[        S9/$ )Nr   r   rU   rL   T)rO   rP   )r   r   rZ   rY   rG   s    r=   r\   HeliosChunkUpdateStep.inputs  s8     yELL9(ELLA)44H
 	
r?   rc   rd   re   c                 @   UR                   R                  UR                  5        UR                  (       a6  US:X  a0  UR                  c#  UR                  S S 2S S 2SS2S S 2S S 24   Ul        [
        R                  " UR                  UR                  /SS9Ul        X4$ )Nr   r   r   rg   )latent_chunksappendr   rL   rj   r   rp   rU   )rH   rc   rd   re   s       r=   ry   HeliosChunkUpdateStep.__call__  s     	!!(()<)<= ''AF{7P7P7X(3(;(;Aq!A#q!O(LK%&+ii1L1LkNaNa0bhi&j#&&r?   rF   Nr   rF   r?   r=   r:  r:    s    NJ
S 
 
 T-%8   
Z( 
 
 ]]_
'#8 
'z 
'VY 
' 
'r?   r:  c                       \ rS rSrSrSr\S\4S j5       r\S\	\
   4S j5       r\S\	\   4S j5       r\R                  " 5       S\S	\S\4S
 j5       rSrg)HeliosChunkLoopWrapperi  a  Outer chunk loop that iterates over temporal chunks.

History indices, scheduler params, and history state are prepared by HeliosPrepareHistoryStep and
HeliosSetTimestepsStep before this block runs. Sub-blocks handle per-chunk preparation, denoising, and history
updates.
rC   rD   c                      g)NzPipeline block that iterates over temporal chunks for progressive video generation. At each chunk iteration, it runs sub-blocks for preparation, denoising, and history updates.rF   rG   s    r=   rI   "HeliosChunkLoopWrapper.description  s    k	
r?   c                 "    [        SS[        S9/$ )Nnum_latent_chunkTrX   )r   r   rG   s    r=   loop_inputs"HeliosChunkLoopWrapper.loop_inputs  s     )DCH
 	
r?   c                 "    [        S[        SS9/$ )NrB  z)List of per-chunk denoised latent tensors)rP   rI   )r   r!   rG   s    r=   loop_intermediate_outputs0HeliosChunkLoopWrapper.loop_intermediate_outputs  s     4Epq
 	
r?   rc   statec                     U R                  U5      n/ Ul        [        US5      (       d  S Ul        [	        UR
                  5       H  nU R                  XUS9u  pM     U R                  X#5        X4$ )Nrj   )re   )get_block_staterB  hasattrrj   r   rJ  	loop_stepset_block_state)rH   rc   rP  rd   re   s        r=   ry   HeliosChunkLoopWrapper.__call__  su    **51$&!{O44(,K%{334A&*nnZPQn&R#J 5 	U0  r?   rF   N)r|   r}   r~   r   r   r   r   r   rI   r!   r   rK  r   rN  r   r   r   r   ry   r   rF   r?   r=   rF  rF    s     J
S 
 
 
T*- 
 

 
4+< 
 

 ]]_!#8 ! !S` ! !r?   rF  c                   F    \ rS rSrSr\\\\\	/r
/ SQr\S\4S j5       rSrg)HeliosChunkDenoiseStepi  z^T2V chunk-based denoising: history slice -> noise gen -> scheduler reset -> denoise -> update.history_slice	noise_genscheduler_resetdenoise_innerupdate_chunkrD   c                      g)NzT2V chunk denoise step that iterates over temporal chunks.
At each chunk: history_slice -> noise_gen -> scheduler_reset -> denoise_inner -> update_chunk.rF   rG   s    r=   rI   "HeliosChunkDenoiseStep.description   s    m	
r?   rF   N)r|   r}   r~   r   r   rA   r   r   r   r:  block_classesblock_namesr   r   rI   r   rF   r?   r=   rX  rX    s>    h 	$%M eK
S 
 
r?   rX  c                   F    \ rS rSrSr\\\\\	/r
/ SQr\S\4S j5       rSrg)HeliosI2VChunkDenoiseStepi  zbI2V chunk-based denoising: I2V history slice -> noise gen -> scheduler reset -> denoise -> update.rY  rD   c                      g)NzI2V chunk denoise step that iterates over temporal chunks.
At each chunk: history_slice (I2V) -> noise_gen -> scheduler_reset -> denoise_inner -> update_chunk.rF   rG   s    r=   rI   %HeliosI2VChunkDenoiseStep.description  s    s	
r?   rF   N)r|   r}   r~   r   r   r   r   r   r   r:  ra  rb  r   r   rI   r   rF   r?   r=   rd  rd    s>    l 	'%M eK
S 
 
r?   rd  c                       \ rS rSrSrSr\S\4S j5       r\S\	\
   4S j5       r\S\	\   4S j5       r\R                  " 5       S\S	\S
\4S j5       rSrg)'HeliosPyramidDistilledChunkDenoiseInneri  a|  Nested pyramid stage loop with DMD denoising for distilled checkpoints.

Same progressive multi-resolution strategy as HeliosPyramidChunkDenoiseInner, but:
- Guidance is disabled (guidance_scale=1.0, no unconditional pass)
- Supports is_amplify_first_chunk (doubles first chunk's timesteps via scheduler)
- Tracks start_point_list and passes DMD-specific args to scheduler.step()
r   rD   c                      g)NzDistilled pyramid denoising inner block for DMD checkpoints. Loops over pyramid stages from smallest to full resolution with guidance disabled and DMD scheduler support.rF   rG   s    r=   rI   3HeliosPyramidDistilledChunkDenoiseInner.description'  s    a	
r?   c           
      v    [        S[        5      [        S[        5      [        S[        [	        SS05      SS9/$ )Nr   r   r   r   g      ?r   r   r   rG   s    r=   r   ;HeliosPyramidDistilledChunkDenoiseInner.expected_components.  r   r?   c                 J   [         R                  " S5      [        S[        R                  S9[        S[        R                  S9[         R                  " S5      [        S/ SQ[        SS	9[        S
S[
        SS	9[         R                  " S5      [         R                  " S5      /$ )Nr   r   r   r   r   r   )r   r   r   r   rN   is_amplify_first_chunkTzYWhether to double the first chunk's timesteps via the scheduler for amplified generation.r   r   )r   r   r   rZ   r!   rY   rG   s    r=   r\   .HeliosPyramidDistilledChunkDenoiseInner.inputs;  s     	*%,,?/5<<H 782!J	 (w	  23,%
 	
r?   rc   rd   re   c                    UR                   nUR                  R                  nUR                  n[	        UR
                  5      nUS:H  nU/n	SUR                  UR                  40n
[        [        R                  " UR                  R                  5      R                  R                  5       5      n0 nUR                  R                  5        H  u  pX;   d  M  X;  d  M  XU'   M     UR                   R#                  U5      US'   UR$                  R#                  U5      US'   UR&                  R#                  U5      US'   UR(                  US'   [+        U5       GH0  nUR                  R,                  R.                  nUR0                  S   UR0                  S   -  UR0                  S	   -  US   US
   -  US   -  -  n[3        UUR4                  R,                  R7                  SS5      UR4                  R,                  R7                  SS5      UR4                  R,                  R7                  SS5      UR4                  R,                  R7                  SS5      5      nUR
                  U   nUR4                  R9                  UUUUUR:                  =(       a    US9  UR4                  R<                  nUS:  Gaa  UR0                  u  nnnnnUS-  nUS-  nUR?                  SSS
SS5      RA                  UU-  UUU5      n[B        RD                  " UUU4SS9nURA                  UUUUU5      R?                  SSS
SS5      nS
UR4                  RF                  U   -
  nUR4                  R,                  RH                  nS
[J        RL                  " S
S
U-  -   5      S
U-
  -  U-   -  nUS
U-
  -  [J        RL                  " U5      -  nUR0                  u  nnnn n![O        UUUU U!UUUURP                  S9	n"U"R#                  US9n"UU-  UU"-  -   nU	RS                  U5        [	        U5      UUR4                  RT                  -  -
  n#[W        US9 n$[Y        U5       GH(  u  n%n&U&R[                  UR0                  S   5      R#                  [\        R^                  5      n'UR#                  U5      n(UR`                  Rc                  U%UU&S9  UR`                  Re                  U
5      n)U) H  n*UR`                  Rg                  UR                  5        U
R                  5        Vs0 s H  o3[i        U*U5      _M     n+n[i        U*UR`                  Rj                  5      n,UR                  Rm                  U,5         UR                  " S U(U'SS.U+DUD6S   U*l7        S S S 5        UR`                  Rq                  UR                  5        M     URa                  U)5      S   n-UR4                  Rs                  U-U&UURP                  SU%X   UR4                  Rt                  UR4                  R<                  US9
S   nU%[	        U5      S
-
  :X  d/  U%S
-   U#:  d  GM  U%S
-   UR4                  RT                  -  S:X  d  GM  U$Rw                  5         GM+     S S S 5        GM3     Xbl        X4$ s  snf ! , (       d  f       GN= f! , (       d  f       GMe  = f)!Nr   r   rq   rr   rs   r   r  r  r  r   r   r  r  r   r!  r"  r#  r$  r%  )r   r   rn  r   r   r&  r   r   r'  r   r   Fr   )r   r   cur_sampling_stepdmd_noisy_tensor
dmd_sigmasdmd_timestepsall_timestepsrF   )<rk   r   ri   r   r   r   r   r   r   r   r   r   r   r   r   r   rq   r(   rr   rs   r   r   r   r3   r   r   r   r(  r   rn  r   r+   r,   r   r   r)  r2   r*  r+  r>   r   rC  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ).rH   rc   rd   re   r   r  r   r,  is_first_chunkstart_point_listr  r  r  r  r  r.  r3   r/  r   r   r   r-   rt   r/   r0  r1  r2  r3  r4  r2   r5  r6  r   r   r<   r  r  r	  r
  r   r  r  r  r  r  r   s.                                                 r=   ry   0HeliosPyramidDistilledChunkDenoiseInner.__call__R  s   --&2288%% !M!MNa $9 $k&?&?AcAc%d

 w001G1G1O1OP[[``bc'2'H'H'N'N'P#J-*2Q,7j) (Q
 2=1R1R1U1UVg1h-./:/N/N/Q/QRc/d+,0;0P0P0S0STe0f,-,7,H,H()+,C#//66AAJ %]]2.r1BBW]]SUEVV1
1-
1=M !$$++//0DcJ$$++//0CTJ$$++//cB$$++//TBB #."N"Ns"S  ..#'2'I'I'\n /  #,,66I QwU\UbUbR
0*i!A!A!//!Q1a8@@+-A9i --uen9U!//*jBVX]_demmq!Q
 
 4 4 E Ec JJ	",,3399TYYqAI71y=IIUVI.51AAEL]]B
0*a*(!)33
 '89'/D5L8 ''0  #9~0CjFZFZF`F`0``/0L%i0DAq xxa(89<<U[[IH)04E)F&%%//QL_jk/l#-#4#4#C#CM#RL.:*"))889O9OPR_RdRdRf&gRfQ'2Da*H'HRf&g'./A:CTCTCdCd'e'33AA,O<F<R<R =.@)1,1= #.	=
 #0=  =!.9 P #))889O9OP /; ",!2!2<!@!CJ )2277""-"7"7$)*+)9)>#-#7#7#>#>&0&:&:&D&D&/ 8  G C	NQ..Q"22AAUAUA[A[7[_`7`$++-S 1 10G -^ &&&G 'h PO 10sD   CZ1Z2=Z1/!ZB7Z1Z1/Z1Z1
Z.)Z11
[	rF   Nr   rF   r?   r=   rh  rh    s     "J
S 
 
 

T-%8 

 

 
Z( 
 
, ]]_L'#8 L'z L'VY L' L'r?   rh  c                   D    \ rS rSrSr\\\\/r	/ SQr
\S\4S j5       rSrg)HeliosPyramidChunkDenoiseStepi  zcT2V pyramid chunk denoising: history slice -> pyramid noise gen -> pyramid denoise inner -> update.rZ  r[  r]  r^  rD   c                      g)NzT2V pyramid chunk denoise step that iterates over temporal chunks.
At each chunk: history_slice -> noise_gen (pyramid) -> denoise_inner (pyramid stages) -> update_chunk.
Denoising starts at the smallest resolution and progressively upsamples.rF   rG   s    r=   rI   )HeliosPyramidChunkDenoiseStep.description      W	
r?   rF   N)r|   r}   r~   r   r   rA   r   r  r:  ra  rb  r   r   rI   r   rF   r?   r=   rz  rz    s;    m 	$&&	M RK
S 
 
r?   rz  c                   D    \ rS rSrSr\\\\/r	/ SQr
\S\4S j5       rSrg) HeliosPyramidI2VChunkDenoiseStepi  zgI2V pyramid chunk denoising: I2V history slice -> pyramid noise gen -> pyramid denoise inner -> update.r{  rD   c                      g)NzI2V pyramid chunk denoise step that iterates over temporal chunks.
At each chunk: history_slice (I2V) -> noise_gen (pyramid) -> denoise_inner (pyramid stages) -> update_chunk.
Denoising starts at the smallest resolution and progressively upsamples.rF   rG   s    r=   rI   ,HeliosPyramidI2VChunkDenoiseStep.description  r~  r?   rF   N)r|   r}   r~   r   r   r   r   r  r:  ra  rb  r   r   rI   r   rF   r?   r=   r  r    s;    q 	'&&	M RK
S 
 
r?   r  c                   D    \ rS rSrSr\\\\/r	/ SQr
\S\4S j5       rSrg)&HeliosPyramidDistilledChunkDenoiseStepi
  zDT2V distilled pyramid chunk denoising with DMD scheduler and no CFG.r{  rD   c                      g)NzT2V distilled pyramid chunk denoise step with DMD scheduler.
At each chunk: history_slice -> noise_gen (pyramid) -> denoise_inner (distilled/DMD) -> update_chunk.rF   rG   s    r=   rI   2HeliosPyramidDistilledChunkDenoiseStep.description  s    t	
r?   rF   N)r|   r}   r~   r   r   rA   r   rh  r:  ra  rb  r   r   rI   r   rF   r?   r=   r  r  
  s;    N 	$&/	M RK
S 
 
r?   r  c                   D    \ rS rSrSr\\\\/r	/ SQr
\S\4S j5       rSrg))HeliosPyramidDistilledI2VChunkDenoiseStepi  zDI2V distilled pyramid chunk denoising with DMD scheduler and no CFG.r{  rD   c                      g)NzI2V distilled pyramid chunk denoise step with DMD scheduler.
At each chunk: history_slice (I2V) -> noise_gen (pyramid) -> denoise_inner (distilled/DMD) -> update_chunk.rF   rG   s    r=   rI   5HeliosPyramidDistilledI2VChunkDenoiseStep.description(  s    z	
r?   rF   N)r|   r}   r~   r   r   r   r   rh  r:  ra  rb  r   r   rI   r   rF   r?   r=   r  r    s;    N 	'&/	M RK
S 
 
r?   r  ))r   r   r   NN)6r   r*  r   torch.nn.functionalnn
functionalr   	tqdm.autor   configuration_utilsr   guidersr   r   modelsr   
schedulersr	   utilsr
   utils.torch_utilsr   modular_pipeliner   r   r   r   modular_pipeline_utilsr   r   r   before_denoiser   r   
get_loggerr|   loggerr>   rA   r   r   r   r   r   r  r:  rF  rX  rd  rh  rz  r  r  r  rF   r?   r=   <module>r     sJ         - M . )  -  L K + 3 
		H	% (`O'"7 O'dD'%: D'N'3 '2-'%: -'`'$9 'Nj'3 j'Z'%: 'N#'1 #'V*!9 *!d
3 
(
 6 
(C'.C C'L
$: 
(
'= 
(
-C 
&
0F 
r?   