
    
3j>                     :   S SK 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
  SS	KJrJrJrJr  SS
KJrJr  SSKJr  \
R(                  " \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g)    N   )
FrozenDict)ClassifierFreeGuidance) HunyuanVideo15Transformer3DModel)FlowMatchEulerDiscreteScheduler)logging   )
BlockStateLoopSequentialPipelineBlocksModularPipelineBlocksPipelineState)ComponentSpec
InputParam   )HunyuanVideo15ModularPipelinec            	           \ rS rSrSr\S\4S j5       r\S\\	   4S j5       r
\R                  " 5       S\S\S\S	\R                   4S
 j5       rSrg) HunyuanVideo15LoopBeforeDenoiser$   hunyuan-video-1.5returnc                     g)Nz=Step within the denoising loop that prepares the latent input selfs    n/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/modular_pipelines/hunyuan_video1_5/denoise.pydescription,HunyuanVideo15LoopBeforeDenoiser.description'   s    N    c                     [         R                  " SSS9[        SS[        R                  S9[        SS[        R                  S9/$ )NlatentsTrequiredcond_latents_concat)r"   	type_hintmask_concat)r   templatetorchTensorr   s    r   inputs'HunyuanVideo15LoopBeforeDenoiser.inputs+   s?     	D9,tu||T}tu||L
 	
r   
componentsblock_stateitc                 ~    [         R                  " UR                  UR                  UR                  /SS9Ul        X4$ )Nr   )dim)r'   catr    r#   r%   latent_model_input)r   r+   r,   r-   r.   s        r   __call__)HunyuanVideo15LoopBeforeDenoiser.__call__3   s=    ).  +"A"A;CZCZ[ab*
& &&r   r   N)__name__
__module____qualname____firstlineno__
model_namepropertystrr   listr   r)   r'   no_gradr   r
   intr(   r3   __static_attributes__r   r   r   r   r   $   s    $JOS O O 
Z( 
 
 ]]_'#@ 'z '^a 'fkfrfr ' 'r   r   c                      ^  \ rS rSrSrSU 4S jjr\S\\   4S j5       r	\S\
4S j5       r\S\\   4S j5       r\R                  " 5       S\S	\S
\S\R&                  S\4
S j5       rSrU =r$ )HunyuanVideo15LoopDenoiser;   r   c                    > Uc  SSSSS.n[        U[        5      (       d  [        S[        U5       35      eXl        [
        TU ]  5         g N)prompt_embedsnegative_prompt_embeds)prompt_embeds_masknegative_prompt_embeds_mask)prompt_embeds_2negative_prompt_embeds_2)prompt_embeds_mask_2negative_prompt_embeds_mask_2)encoder_hidden_statesencoder_attention_maskencoder_hidden_states_2encoder_attention_mask_2z0guider_input_fields must be a dictionary but is 
isinstancedict
ValueErrortype_guider_input_fieldssuper__init__r   guider_input_fields	__class__s     r   rX   #HunyuanVideo15LoopDenoiser.__init__>   Y    &)T*_+Z,e	# -t44OPTUhPiOjkll$7!r   r   c                 X    [        S[        [        SS05      SS9[        S[        5      /$ Nguiderguidance_scaleg      @from_config)configdefault_creation_methodtransformerr   r   r   r   r   s    r   expected_components.HunyuanVideo15LoopDenoiser.expected_componentsK   :     &!#3S"9:(5	 -)IJ
 	
r   c                     g)NzFStep within the denoising loop that denoises the latents with guidancer   r   s    r   r   &HunyuanVideo15LoopDenoiser.descriptionW   s    Wr   c                 2   [         R                  " S5      [         R                  " SSS S9[        S[        R                  SS9/nU R                  R                  5        H  n[        U[        5      (       ao  UR                  [        US   S[        R                  S	US   < S
3S95        USS   H0  nUR                  [        U[        R                  SU< S
3S95        M2     M  UR                  [        US[        R                  U< S
3S95        M     U$ )Nattention_kwargsnum_inference_stepsTr"   defaultimage_embedsPSiglip image embeddings used as extra conditioning for I2V. Zero-filled for T2V.r$   r   r   Positive branch of the  field fed into the guider.namer"   r$   r   r   Negative branch of the rw   r$   r   	r   r&   r'   r(   rV   valuesrR   tupleappendr   r)   valueneg_names       r   r)   !HunyuanVideo15LoopDenoiser.inputs[   s     23 5dS,,n
 ..557E%''"1X!%"',,&=eAh\Id$e	 !&ab	HMM"!)&+ll*A(Mh(i !* "!%"',,',i/J$K	' 86 r   r+   r,   r-   r.   c                 L  ^ UR                  TR                  R                  S   5      R                  TR                  R                  5      nU R
                  R                  5        VVs0 s H<  u  pgU[        U[        5      (       a  [        U4S jU 5       5      O[        TU5      _M>     nnnUR                  R                  UTR                  US9  UR                  R                  U5      n	U	 H  n
UR                  R                  UR                  5        UR!                  5        Vs0 s H  of[        X5      _M     nn[        XR                  R"                  5      nUR                  R%                  U5         UR                  " STR                  TR&                  UTR(                  SS.UD6S   U
l        S S S 5        UR                  R-                  UR                  5        M     UR                  U	5      S   Tl        UT4$ s  snnf s  snf ! , (       d  f       N_= f)Nr   c              3   <   >#    U  H  n[        TU5      v   M     g 7fNgetattr.0vr,   s     r   	<genexpr>6HunyuanVideo15LoopDenoiser.__call__.<locals>.<genexpr>        Eu!gk155u   steprn   timestepF)hidden_statesrq   r   rm   return_dictr   )expandr2   shapetodtyperV   itemsrR   r|   r   r`   	set_statern   prepare_inputsprepare_modelsre   keys_identifier_keycache_contextrq   rm   
noise_predcleanup_models)r   r+   r,   r-   r.   r   
input_namer   guider_inputsguider_stateguider_state_batchcond_kwargscontext_names     `          r   r3   #HunyuanVideo15LoopDenoiser.__call__   s    88K::@@CDGGHfHfHlHlm &*%>%>%D%D%F	
 &G!
 %'' EuEEe,- &G	 	 
 	##@_@_jk#l "((77F #/,,Z-C-CDanasasauvauS]w/A'NNauKv"#57H7H7X7XYL''55lC0:0F0F 1"-"@"@!,!9!9%%0%A%A %1 "1 1"- D ,,Z-C-CD! #/& ",!2!2<!@!C;&&G
" w DCs   *AH
,H=?H
H#	rV   r   r5   r6   r7   r8   r9   rX   r:   r<   r   rg   r;   r   r   r)   r'   r=   r   r
   r>   r(   r   r3   r?   __classcell__r[   s   @r   rA   rA   ;   s    $J 	
T-%8 	
 	
 XS X X %Z( % %N ]]_)'7)'FP)'UX)']b]i]i)'	)' )'r   rA   c            	           \ rS rSrSr\S\\   4S j5       r\S\	4S j5       r
\R                  " 5       S\S\S\S	\R                   4S
 j5       rSrg)HunyuanVideo15LoopAfterDenoiser   r   r   c                 $    [        S[        5      /$ )N	scheduler)r   r   r   s    r   rg   3HunyuanVideo15LoopAfterDenoiser.expected_components   s    k+JKLLr   c                     g)Nz7Step within the denoising loop that updates the latentsr   r   s    r   r   +HunyuanVideo15LoopAfterDenoiser.description   s    Hr   r+   r,   r-   r.   c                 p   UR                   R                  nUR                  R                  UR                  XBR                   SS9S   Ul         UR                   R                  U:w  aM  [
        R                  R                  R                  5       (       a   UR                   R                  U5      Ul         X4$ )NF)r   r   )
r    r   r   r   r   r'   backendsmpsis_availabler   )r   r+   r,   r-   r.   latents_dtypes         r   r3   (HunyuanVideo15LoopAfterDenoiser.__call__   s    #++11(2277""A':': 8 

 $$5~~!!..00&1&9&9&<&<]&K#&&r   r   N)r5   r6   r7   r8   r9   r:   r<   r   rg   r;   r   r'   r=   r   r
   r>   r(   r3   r?   r   r   r   r   r      s    $JMT-%8 M M IS I I ]]_
'#@ 
'z 
'^a 
'fkfrfr 
' 
'r   r   c                       \ 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) HunyuanVideo15DenoiseLoopWrapper   r   r   c                     g)NzCPipeline block that iteratively denoises the latents over timestepsr   r   s    r   r   ,HunyuanVideo15DenoiseLoopWrapper.description   s    Tr   c                 B    [        S[        5      [        S[        5      /$ )Nr   re   )r   r   r   r   s    r   loop_expected_components9HunyuanVideo15DenoiseLoopWrapper.loop_expected_components   s$     +'FG-)IJ
 	
r   c                 X    [         R                  " SSS9[         R                  " SSS S9/$ )N	timestepsTr!   rn   ro   )r   r&   r   s    r   loop_inputs,HunyuanVideo15DenoiseLoopWrapper.loop_inputs   s1     d; 5dS
 	
r   r+   statec           	      h   U R                  U5      n[        [        UR                  5      UR                  UR
                  R                  -  -
  S5      Ul        U R                  UR                  S9 n[        UR                  5       Hz  u  pVU R                  XXVS9u  pU[        UR                  5      S-
  :X  d7  US-   UR                  :  d  MH  US-   UR
                  R                  -  S:X  d  Mj  UR                  5         M|     S S S 5        U R                  X#5        X4$ ! , (       d  f       N"= f)Nr   )total)r-   r.   r   )get_block_statemaxlenr   rn   r   ordernum_warmup_stepsprogress_bar	enumerate	loop_stepupdateset_block_state)r   r+   r   r,   r   r-   r.   s          r   r3   )HunyuanVideo15DenoiseLoopWrapper.__call__   s   **51'*%%&)H)H:K_K_KeKe)eegh(
$ [%D%DE!+"7"78*...TU.*['
K112Q66Uk:::AI]I]IcIc?cgh?h '') 9 F 	U0   FEs   3AD#D#3D##
D1r   N)r5   r6   r7   r8   r9   r:   r;   r   r<   r   r   r   r   r'   r=   r   r   r3   r?   r   r   r   r   r      s    $JUS U U 
$}*= 
 
 
T*- 
 
 ]]_!#@ ! ![h ! !r   r   c                   H    \ rS rSr\\" 5       \/r/ SQr\	S\
4S j5       rSrg)HunyuanVideo15DenoiseStep   before_denoiserdenoiserafter_denoiserr   c                      g)NzDenoise step that iteratively denoises the latents.
At each iteration:
 - `HunyuanVideo15LoopBeforeDenoiser`
 - `HunyuanVideo15LoopDenoiser`
 - `HunyuanVideo15LoopAfterDenoiser`
This block supports text-to-video tasks.r   r   s    r   r   %HunyuanVideo15DenoiseStep.description   s    7	
r   r   N)r5   r6   r7   r8   r   rA   r   block_classesblock_namesr:   r;   r   r?   r   r   r   r   r      s6    ("$'M
 DK
S 
 
r   r   c                      ^  \ rS rSrSrSU 4S jjr\S\\   4S j5       r	\S\
4S j5       r\S\\   4S j5       r\R                  " 5       S\S	\S
\S\R&                  S\4
S j5       rSrU =r$ )%HunyuanVideo15Image2VideoLoopDenoiseri  r   c                    > Uc  SSSSS.n[        U[        5      (       d  [        S[        U5       35      eXl        [
        TU ]  5         g rD   rQ   rY   s     r   rX   .HunyuanVideo15Image2VideoLoopDenoiser.__init__	  r]   r   r   c                 X    [        S[        [        SS05      SS9[        S[        5      /$ r_   rf   r   s    r   rg   9HunyuanVideo15Image2VideoLoopDenoiser.expected_components  ri   r   c                     g)Nz-I2V denoiser with MeanFlow timestep_r supportr   r   s    r   r   1HunyuanVideo15Image2VideoLoopDenoiser.description"  s    >r   c                 Z   [         R                  " S5      [         R                  " SSS S9[        S[        R                  SS9[         R                  " SSS	9/nU R                  R                  5        H  n[        U[        5      (       ao  UR                  [        US
   S[        R                  SUS
   < S3S95        USS   H0  nUR                  [        U[        R                  SU< S3S95        M2     M  UR                  [        US[        R                  U< S3S95        M     U$ )Nrm   rn   Tro   rq   rr   rs   r   r!   r   rt   ru   rv   r   rx   ry   rz   r~   s       r   r)   ,HunyuanVideo15Image2VideoLoopDenoiser.inputs&  s$     23 5dS,,n
 d;	
 ..557E%''"1X!%"',,&=eAh\Id$e	 !&ab	HMM"!)&+ll*A(Mh(i !* "!%"',,',i/J$K	' 86 r   r+   r,   r-   r.   c                   ^ UR                  TR                  R                  S   5      R                  TR                  R                  5      nUR
                  R                  R                  (       a  U[        TR                  5      S-
  :X  a!  [        R                  " S/UR                  S9nOTR                  US-      nUR                  TR                  R                  S   5      R                  TR                  R                  5      nOS nU R                  R                  5        VVs0 s H<  u  pxU[!        U["        5      (       a  [#        U4S jU 5       5      O[%        TU5      _M>     n	nnUR&                  R)                  UTR*                  US9  UR&                  R-                  U	5      n
U
 H  nUR&                  R/                  UR
                  5        U	R1                  5        Vs0 s H  ow[%        X5      _M     nn[%        XR&                  R2                  5      nUR
                  R5                  U5         UR
                  " S	TR                  TR6                  UUTR8                  SS.UD6S   Ul        S S S 5        UR&                  R=                  UR
                  5        M     UR'                  U
5      S   Tl        UT4$ s  snnf s  snf ! , (       d  f       N_= f)
Nr   r   g        )devicec              3   <   >#    U  H  n[        TU5      v   M     g 7fr   r   r   s     r   r   AHunyuanVideo15Image2VideoLoopDenoiser.__call__.<locals>.<genexpr>`  r   r   r   F)r   rq   r   
timestep_rrm   r   r   )r   r2   r   r   r   re   rc   use_meanflowr   r   r'   tensorr   r    rV   r   rR   r|   r   r`   r   rn   r   r   r   r   r   rq   rm   r   r   )r   r+   r,   r-   r.   r   r   r   r   r   r   r   r   r   s     `           r   r3   .HunyuanVideo15Image2VideoLoopDenoiser.__call__O  s    88K::@@CDGGHfHfHlHlm !!((55C--.22"\\3%H
(221q59
#**;+>+>+D+DQ+GHKKKL_L_LeLefJJ &*%>%>%D%D%F	
 &G!
 %'' EuEEe,- &G	 	 
 	##@_@_jk#l!((77F".,,Z-C-CDanasasauvauS]w/A'NNauKv"#57H7H7X7XYL''55lC0:0F0F 1"-"@"@!,!9!9%)%0%A%A %1 "1 1"- D ,,Z-C-CD# #/& ",!2!2<!@!C;&&?
 w DCs   ,AK.K?A K
K&	r   r   r   r   s   @r   r   r     s    $J 	
T-%8 	
 	
 ?S ? ? &Z( & &P ]]_.'7.'FP.'UX.']b]i]i.'	.' .'r   r   c                   H    \ rS rSr\\" 5       \/r/ SQr\	S\
4S j5       rSrg)$HunyuanVideo15Image2VideoDenoiseStepi  r   r   c                      g)NzDenoise step for image-to-video with MeanFlow support.
At each iteration:
 - `HunyuanVideo15LoopBeforeDenoiser`
 - `HunyuanVideo15Image2VideoLoopDenoiser`
 - `HunyuanVideo15LoopAfterDenoiser`r   r   s    r   r   0HunyuanVideo15Image2VideoDenoiseStep.description  s    3	
r   r   N)r5   r6   r7   r8   r   r   r   r   r   r:   r;   r   r?   r   r   r   r   r     s6    (-/'M
 DK
S 
 
r   r   )r'   configuration_utilsr   guidersr   modelsr   
schedulersr   utilsr   modular_pipeliner
   r   r   r   modular_pipeline_utilsr   r   r   
get_loggerr5   loggerr   rA   r   r   r   r   r   r   r   r   <module>r      s      - - 6 9   ? ; 
		H	%''< '.r'!6 r'j'&; '2&!'C &!R
 @ 
(x',A x'v
+K 
r   