
    bi'                        d dl mZmZmZ d dlZddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ d	d
lmZmZmZmZ d	dlmZmZmZ ddlmZ  ej2                  e      Z G d de      Z G d de      Z G d de      Z G d de      Zy)    )AnyListTupleN   )
FrozenDict)ClassifierFreeGuidance)WanTransformer3DModel)UniPCMultistepScheduler)logging   )
BlockStateLoopSequentialPipelineBlocksModularPipelineBlocksPipelineState)ComponentSpec
InputParamOutputParam   )WanModularPipelinec                       e Zd ZdZedee   fd       Zedefd       Z	edee
eef      fd       Zedee   fd       Z ej                         deded	ed
ej&                  def
d       Zy)WanLoopDenoiserwanreturnc                 ^    t        dt        t        ddi      d      t        dt              gS )Nguiderguidance_scale      @from_configconfigdefault_creation_methodtransformer)r   r   r   r	   selfs    b/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/modular_pipelines/wan/denoise.pyexpected_componentsz#WanLoopDenoiser.expected_components(   s:     &!#3S"9:(5	 -)>?
 	
    c                      	 y)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%   descriptionzWanLoopDenoiser.description4       4	
r'   c                     t        d      gS )Nattention_kwargsr   r#   s    r%   inputszWanLoopDenoiser.inputs<   s     )*
 	
r'   c                 z    t        ddt        j                  d      t        ddt        d      t        dd	      gS )
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_inputsz#WanLoopDenoiser.intermediate_inputsB   sR     ,,|	 % F	 1a
 	
r'   
componentsblock_stateitc                    ddi}|j                   j                  }|j                  j                  ||j                  |       |j                  j                  ||      }|D ]  }|j                  j                  |j                          |j                         }	|	j                         D 
ci c]  \  }
}|
|v s|
| }	}
}|	j                  d      }|j                  |j                  j                  |      |j                         ||j                  d      d   |_        |j                  j                  |j                           |j                  |      \  |_        |_        ||fS c c}}
w )Nprompt_embeds)rC   negative_prompt_embeds)stepr5   timestepF)hidden_statesrF   encoder_hidden_statesr-   return_dictr   )r"   dtyper   	set_stater5   prepare_inputsprepare_modelsas_dictitemspopr1   toflattenr-   
noise_predcleanup_modelsscheduler_step_kwargs)r$   r>   r?   r@   rA   r7   transformer_dtypeguider_stateguider_state_batchcond_kwargskvrC   s                r%   __call__zWanLoopDenoiser.__call__[   sy    H
 '2288##@_@_jk#l "((77EXY #/ 	E,,Z-C-CD,446K,7,=,=,?\DAq1H[C[1a4\K\'OOO<M -7,B,B)11445FG&3!,!=!=! -C - -) ,,Z-C-CD	E$ EODUDUVbDcA A;&&# ]s   ,E*9E*N)__name__
__module____qualname__
model_namepropertyr   r   r&   strr*   r   r   r/   r=   r:   no_gradr   r   r<   r;   r   r\   r)   r'   r%   r   r   %   s    J	
T-%8 	
 	
 
S 
 
 
U38_- 
 

 
T#Y 
 
0 U]]_(',(';E('JM('RWR^R^('	(' ('r'   r   c            	           e Zd ZdZedee   fd       Zedefd       Z	edee
eef      fd       Zedee   fd       Zedee   fd       Z ej"                         ded	ed
edej*                  fd       Zy)WanLoopAfterDenoiserr   r   c                 $    t        dt              gS )N	scheduler)r   r
   r#   s    r%   r&   z(WanLoopAfterDenoiser.expected_components   s     +'>?
 	
r'   c                      	 y)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*   z WanLoopAfterDenoiser.description   r+   r'   c                     g S )Nr)   r#   s    r%   r/   zWanLoopAfterDenoiser.inputs   s    	r'   c                     t        d      gS )N	generatorr.   r#   s    r%   r=   z(WanLoopAfterDenoiser.intermediate_inputs   s     {#
 	
r'   c                 <    t        dt        j                  d      gS )Nr1   zThe denoised latents)r4   r*   )r   r:   r;   r#   s    r%   intermediate_outputsz)WanLoopAfterDenoiser.intermediate_outputs   s    IKabccr'   r>   r?   r@   rA   c                 r   |j                   j                  } |j                  j                  |j                  j                         ||j                   j                         fi |j                  ddid   |_         |j                   j                  |k7  r |j                   j                  |      |_         ||fS )NrI   Fr   )r1   rJ   rg   rE   rS   floatrU   rQ   )r$   r>   r?   r@   rA   latents_dtypes         r%   r\   zWanLoopAfterDenoiser.__call__   s     $++117j2277""((*%%'
 //	

 
  $$5"-"5"5"8"8"GK;&&r'   N)r]   r^   r_   r`   ra   r   r   r&   rb   r*   r   r   r/   r=   r   rm   r:   rc   r   r   r<   r;   r\   r)   r'   r%   re   re      s    J
T-%8 
 

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

 dd;&7 d d U]]_'#5 'J 'SV '[`[g[g ' 'r'   re   c                       e Zd ZdZedefd       Zedee   fd       Z	edee
   fd       Z ej                         dededefd       Zy	)
WanDenoiseLoopWrapperr   r   c                      	 y)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*   z!WanDenoiseLoopWrapper.description   s    d	
r'   c                 |    t        dt        t        ddi      d      t        dt              t        dt              gS )Nr   r   r   r   r   rg   r"   )r   r   r   r
   r	   r#   s    r%   loop_expected_componentsz.WanDenoiseLoopWrapper.loop_expected_components   sE     &!#3S"9:(5	 +'>?-)>?	
 		
r'   c                 b    t        ddt        j                  d      t        ddt        d      gS )N	timestepsTzWThe timesteps to use for the denoising process. Can be generated in set_timesteps step.r2   r5   r6   r9   r#   s    r%   loop_intermediate_inputsz.WanDenoiseLoopWrapper.loop_intermediate_inputs   s?     ,,u	 % F	
 	
r'   r>   statec                 j   | j                  |      }t        t        |j                        |j                  |j
                  j                  z  z
  d      |_        | j                  |j                        5 }t        |j                        D ]{  \  }}| j                  ||||      \  }}|t        |j                        dz
  k(  s3|dz   |j                  kD  sL|dz   |j
                  j                  z  dk(  sl|j                          } 	 d d d        | j                  ||       ||fS # 1 sw Y   xY w)Nr   )total)r@   rA   r   )get_block_statemaxlenrw   r5   rg   ordernum_warmup_stepsprogress_bar	enumerate	loop_stepupdateset_block_state)r$   r>   ry   r?   r   r@   rA   s          r%   r\   zWanDenoiseLoopWrapper.__call__   s'   **51'*%%&)H)H:K_K_KeKe)eegh(
$ [%D%DE 	*!+"7"78 *1*...[TUYZ.*['
KK112Q66Uk:::AI]I]IcIc?cgh?h '')*	* 	UK05  	* 	*s   6A!D)D)8D))D2N)r]   r^   r_   r`   ra   rb   r*   r   r   ru   r   rx   r:   rc   r   r   r\   r)   r'   r%   rr   rr      s    J
S 
 
 

$}*= 

 

 
$z*: 
 
  U]]_!#5 !m !P] ! !r'   rr   c                   2    e Zd ZeegZg dZedefd       Z	y)WanDenoiseStep)before_denoiserdenoiserafter_denoiserr   c                      	 y)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*   zWanDenoiseStep.description   s    7	
r'   N)
r]   r^   r_   r   re   block_classesblock_namesra   rb   r*   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_loggerr]   loggerr   re   rr   r   r)   r'   r%   <module>r      s    $ #  - - + 1   L K 0 
		H	%_'+ _'D.'0 .'b:!8 :!z
* 
r'   