
    +h'                        S SK JrJrJr  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Jr  SSKJr  \R2                  " \5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)    )AnyListTupleN   )
FrozenDict)ClassifierFreeGuidance)WanTransformer3DModel)UniPCMultistepScheduler)logging   )
BlockStateLoopSequentialPipelineBlocksModularPipelineBlocksPipelineState)ComponentSpec
InputParamOutputParam   )WanModularPipelinec                       \ rS rSrSr\S\\   4S j5       r\S\	4S j5       r
\S\\\	\4      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g)WanLoopDenoiser%   wanreturnc                 X    [        S[        [        SS05      SS9[        S[        5      /$ )Nguiderguidance_scale      @from_configconfigdefault_creation_methodtransformer)r   r   r   r	   selfs    a/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/modular_pipelines/wan/denoise.pyexpected_components#WanLoopDenoiser.expected_components(   s:     &!#3S"9:(5	 -)>?
 	
    c                      g)NzStep within the denoising loop that denoise the latents with guidance. This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` object (e.g. `WanDenoiseLoopWrapper`) r$   s    r&   descriptionWanLoopDenoiser.description4       4	
r)   c                     [        S5      /$ )Nattention_kwargsr   r$   s    r&   inputsWanLoopDenoiser.inputs<   s     )*
 	
r)   c                 h    [        SS[        R                  SS9[        SS[        SS9[        SSS	9/$ )
NlatentsTz^The initial latents to use for the denoising process. Can be generated in prepare_latent step.required	type_hintr,   num_inference_stepsgThe number of inference steps to use for the denoising process. Can be generated in set_timesteps step.guider_input_fieldsa  All conditional model inputs that need to be prepared with guider. It should contain prompt_embeds/negative_prompt_embeds. Please add `kwargs_type=guider_input_fields` to their parameter spec (`OutputParam`) when they are created and added to the pipeline state)kwargs_typer,   r   torchTensorintr$   s    r&   intermediate_inputs#WanLoopDenoiser.intermediate_inputsB   sR     ,,|	 % F	 1a
 	
r)   
componentsblock_stateitc           	         SS0nUR                   R                  nUR                  R                  X2R                  US9  UR                  R                  X%5      nU H  nUR                  R                  UR                   5        UR                  5       n	U	R                  5        V
Vs0 s H  u  pX;   d  M  X_M     n	n
nU	R                  S5      nUR                  UR                  R                  U5      UR                  5       UUR                  SS9S   Ul        UR                  R                  UR                   5        M     UR                  U5      u  Ul        Ul        X4$ s  snn
f )Nprompt_embeds)rH   negative_prompt_embeds)stepr9   timestepF)hidden_statesrK   encoder_hidden_statesr0   return_dictr   )r#   dtyper   	set_stater9   prepare_inputsprepare_modelsas_dictitemspopr5   toflattenr0   
noise_predcleanup_modelsscheduler_step_kwargs)r%   rC   rD   rE   rF   r;   transformer_dtypeguider_stateguider_state_batchcond_kwargskvrH   s                r&   __call__WanLoopDenoiser.__call__[   si    H
 '2288##@_@_jk#l "((77Y #/,,Z-C-CD,446K,7,=,=,?\,?DA1C[414,?K\'OOO<M -7,B,B)11445FG&3!,!=!=! -C - -) ,,Z-C-CD #/$ EODUDUVbDcA A&&# ]s   'E&6E&r+   N)__name__
__module____qualname____firstlineno__
model_namepropertyr   r   r'   strr,   r   r   r2   rA   r>   no_gradr   r   r@   r?   r   ra   __static_attributes__r+   r)   r&   r   r   %   s    J	
T-%8 	
 	
 
S 
 
 
U38_- 
 

 
T#Y 
 
0 ]]_(',(';E('JM('RWR^R^('	(' ('r)   r   c            	          \ rS rSrSr\S\\   4S j5       r\S\	4S j5       r
\S\\\	\4      4S j5       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)WanLoopAfterDenoiser   r   r   c                 $    [        S[        5      /$ )N	scheduler)r   r
   r$   s    r&   r'   (WanLoopAfterDenoiser.expected_components   s     +'>?
 	
r)   c                      g)Nzstep within the denoising loop that update the latents. This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` object (e.g. `WanDenoiseLoopWrapper`)r+   r$   s    r&   r,    WanLoopAfterDenoiser.description   r.   r)   c                     / $ )Nr+   r$   s    r&   r2   WanLoopAfterDenoiser.inputs   s    	r)   c                     [        S5      /$ )N	generatorr1   r$   s    r&   rA   (WanLoopAfterDenoiser.intermediate_inputs   s     {#
 	
r)   c                 6    [        S[        R                  SS9/$ )Nr5   zThe denoised latents)r8   r,   )r   r>   r?   r$   s    r&   intermediate_outputs)WanLoopAfterDenoiser.intermediate_outputs   s    IKabccr)   rC   rD   rE   rF   c                 r   UR                   R                  nUR                  R                  " UR                  R                  5       UUR                   R                  5       40 UR                  DSS0D6S   Ul         UR                   R                  U:w  a   UR                   R                  U5      Ul         X4$ )NrN   Fr   )r5   rO   rp   rJ   rX   floatrZ   rV   )r%   rC   rD   rE   rF   latents_dtypes         r&   ra   WanLoopAfterDenoiser.__call__   s     $++11(2277""((*%%'
 //	

 
  $$5"-"5"5"8"8"GK&&r)   r+   N)rc   rd   re   rf   rg   rh   r   r   r'   ri   r,   r   r   r2   rA   r   rz   r>   rj   r   r   r@   r?   ra   rk   r+   r)   r&   rm   rm      s    J
T-%8 
 

 
S 
 
 U38_-   
T#Y 
 

 dd;&7 d d ]]_'#5 'J 'SV '[`[g[g ' 'r)   rm   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)WanDenoiseLoopWrapper   r   r   c                      g)NzPipeline block that iteratively denoise the latents over `timesteps`. The specific steps with each iteration can be customized with `sub_blocks` attributesr+   r$   s    r&   r,   !WanDenoiseLoopWrapper.description   s    d	
r)   c                 v    [        S[        [        SS05      SS9[        S[        5      [        S[        5      /$ )Nr   r   r   r   r    rp   r#   )r   r   r   r
   r	   r$   s    r&   loop_expected_components.WanDenoiseLoopWrapper.loop_expected_components   sE     &!#3S"9:(5	 +'>?-)>?	
 		
r)   c                 V    [        SS[        R                  SS9[        SS[        SS9/$ )N	timestepsTzWThe timesteps to use for the denoising process. Can be generated in set_timesteps step.r6   r9   r:   r=   r$   s    r&   loop_intermediate_inputs.WanDenoiseLoopWrapper.loop_intermediate_inputs   s?     ,,u	 % F	
 	
r)   rC   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)rE   rF   r   )get_block_statemaxlenr   r9   rp   ordernum_warmup_stepsprogress_bar	enumerate	loop_stepupdateset_block_state)r%   rC   r   rD   r   rE   rF   s          r&   ra   WanDenoiseLoopWrapper.__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)rc   rd   re   rf   rg   rh   ri   r,   r   r   r   r   r   r>   rj   r   r   ra   rk   r+   r)   r&   r   r      s    J
S 
 
 

$}*= 

 

 
$z*: 
 
  ]]_!#5 !m !P] ! !r)   r   c                   <    \ rS rSr\\/r/ SQr\S\	4S j5       r
Srg)WanDenoiseStep   )before_denoiserdenoiserafter_denoiserr   c                      g)Na  Denoise step that iteratively denoise the latents. 
Its loop logic is defined in `WanDenoiseLoopWrapper.__call__` method 
At each iteration, it runs blocks defined in `sub_blocks` sequencially:
 - `WanLoopDenoiser`
 - `WanLoopAfterDenoiser`
This block supports both text2vid tasks.r+   r$   s    r&   r,   WanDenoiseStep.description   s    7	
r)   r+   N)rc   rd   re   rf   r   rm   block_classesblock_namesrh   ri   r,   rk   r+   r)   r&   r   r      s0    M DK
S 
 
r)   r   ) typingr   r   r   r>   configuration_utilsr   guidersr   modelsr	   
schedulersr
   utilsr   modular_pipeliner   r   r   r   modular_pipeline_utilsr   r   r   r   
get_loggerrc   loggerr   rm   r   r   r+   r)   r&   <module>r      s    $ #  - - + 1   L K 0 
		H	%_'+ _'D.'0 .'b:!8 :!z
* 
r)   