
    i`                        d dl mZ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j4                  e      Z G d de      Z G d de      Z G d de      Z G d de      Z  G d de      Z! G d de      Z" G d de      Z# G d de#      Z$ G d de#      Z% G d  d!e#      Z& G d" d#e#      Z' G d$ d%e#      Z(y)&    )AnyDictListTupleN   )
FrozenDict)ClassifierFreeGuidance)WanTransformer3DModel)UniPCMultistepScheduler)logging   )
BlockStateLoopSequentialPipelineBlocksModularPipelineBlocksPipelineState)ComponentSpec
ConfigSpec
InputParam   )WanModularPipelinec            	           e Zd ZdZed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
)WanLoopBeforeDenoiserwanreturnc                      	 yNzstep within the denoising loop that prepares the latent input for the denoiser. This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` object (e.g. `WanDenoiseLoopWrapper`) selfs    q/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/diffusers/modular_pipelines/wan/denoise.pydescriptionz!WanLoopBeforeDenoiser.description(       4	
    c                 v    t        ddt        j                  d      t        ddt        j                  d      gS )NlatentsT^The initial latents to use for the denoising process. Can be generated in prepare_latent step.required	type_hintr!   dtype>The dtype of the model inputs. Can be generated in input step.r   torchTensorr*   r   s    r    inputszWanLoopBeforeDenoiser.inputs0   s@     ,,|	 ++\	
 	
r#   
componentsblock_stateitc                 ^    |j                   j                  |j                        |_        ||fS )N)r%   tor*   latent_model_inputr   r0   r1   r2   r3   s        r    __call__zWanLoopBeforeDenoiser.__call__A   s,    )4)<)<)?)?@Q@Q)R&;&&r#   N__name__
__module____qualname__
model_namepropertystrr!   r   r   r/   r-   no_gradr   r   intr.   r8   r   r#   r    r   r   %   s    J
S 
 
 
Z( 
 
  U]]_'#5 'J 'SV '[`[g[g ' 'r#   r   c            	           e Zd ZdZed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
) WanImage2VideoLoopBeforeDenoiserr   r   c                      	 yr   r   r   s    r    r!   z,WanImage2VideoLoopBeforeDenoiser.descriptionJ   r"   r#   c                     t        ddt        j                  d      t        ddt        j                  d      t        ddt        j                  d      gS )	Nr%   Tr&   r'   first_frame_latentszoThe first frame latents to use for the denoising process. Can be generated in prepare_first_frame_latents step.r*   r+   r,   r   s    r    r/   z'WanImage2VideoLoopBeforeDenoiser.inputsR   s]     ,,|	 %,, N	 ++\	
 	
r#   r0   r1   r2   r3   c                     t        j                  |j                  |j                  gd      j	                  |j
                        |_        ||fS Nr   )dim)r-   catr%   rF   r5   r*   r6   r7   s        r    r8   z)WanImage2VideoLoopBeforeDenoiser.__call__i   sH    ).K4G4GIhIh3iop)q)t)t*
& ;&&r#   Nr9   r   r#   r    rC   rC   G       J
S 
 
 
Z( 
 
, U]]_'#5 'J 'SV '[`[g[g ' 'r#   rC   c            	           e Zd ZdZed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
)WanFLF2VLoopBeforeDenoiserr   r   c                      	 yr   r   r   s    r    r!   z&WanFLF2VLoopBeforeDenoiser.descriptiont   r"   r#   c                     t        ddt        j                  d      t        ddt        j                  d      t        ddt        j                  d      gS )	Nr%   Tr&   r'   first_last_frame_latentsz}The first and last frame latents to use for the denoising process. Can be generated in prepare_first_last_frame_latents step.r*   r+   r,   r   s    r    r/   z!WanFLF2VLoopBeforeDenoiser.inputs|   s]     ,,|	 *,, \	 ++\	
 	
r#   r0   r1   r2   r3   c                     t        j                  |j                  |j                  gd      j	                  |j
                        |_        ||fS rH   )r-   rJ   r%   rP   r5   r*   r6   r7   s        r    r8   z#WanFLF2VLoopBeforeDenoiser.__call__   sI    ).  +"F"FGQ*

"[
 	& ;&&r#   Nr9   r   r#   r    rM   rM   q   rK   r#   rM   c                        e Zd ZdZddifdeeef   f f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j                          d
edededej(                  def
d       Z xZS )WanLoopDenoiserr   encoder_hidden_statesprompt_embedsnegative_prompt_embedsguider_input_fieldsc                 ~    t        |t              st        dt        |             || _        t
        |           y)au  Initialize a denoiser block that calls the denoiser model. This block is used in Wan2.1.

        Args:
            guider_input_fields: A dictionary that maps each argument expected by the denoiser model
                (for example, "encoder_hidden_states") to data stored on 'block_state'. The value can be either:

                - A tuple of strings. For instance, {"encoder_hidden_states": ("prompt_embeds",
                  "negative_prompt_embeds")} tells the guider to read `block_state.prompt_embeds` and
                  `block_state.negative_prompt_embeds` and pass them as the conditional and unconditional batches of
                  'encoder_hidden_states'.
                - A string. For example, {"encoder_hidden_image": "image_embeds"} makes the guider forward
                  `block_state.image_embeds` for both conditional and unconditional batches.
        0guider_input_fields must be a dictionary but is N
isinstancedict
ValueErrortype_guider_input_fieldssuper__init__r   rX   	__class__s     r    rb   zWanLoopDenoiser.__init__   >    " -t4OPTUhPiOjkll$7!r#   r   c                 ^    t        dt        t        ddi      d      t        dt              gS )Nguiderguidance_scaleg      @from_configconfigdefault_creation_methodtransformerr   r	   r   r
   r   s    r    expected_componentsz#WanLoopDenoiser.expected_components   s:     &!#3S"9:(5	 -)>?
 	
r#   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   r   s    r    r!   zWanLoopDenoiser.description   r"   r#   c                 N   t        d      t        ddt        d      g}g }| j                  j                         D ]5  }t	        |t
              r|j                  |       %|j                  |       7 |D ]-  }|j                  t        |dt        j                               / |S Nattention_kwargsnum_inference_stepsTgThe number of inference steps to use for the denoising process. Can be generated in set_timesteps step.r'   )namer(   r)   
r   rA   r`   valuesr\   tupleextendappendr-   r.   r   r/   guider_input_namesvaluerw   s        r    r/   zWanLoopDenoiser.inputs        )*% F	
  ..557 	1E%'"))%0"))%0		1 ' 	XDMM*$VW	Xr#   r0   r1   r2   r3   c           
         |j                   j                  ||j                  |       |j                   j                  || j                        }|D ]\  }|j                   j                  |j                         |j                         }|j                         D 	ci c]Y  \  }}	|| j                  j                         v r8|t        |	t        j                        r|	j                  |j                        n|	[ }}}	 |j                  d|j                  j                  |j                        |j!                  |j                  j"                  d         j                  |j                        |j$                  dd|d   |_        |j                   j)                  |j                         _ |j                  |      d   |_        ||fS c c}	}w N)stepru   timestepr   F)hidden_statesr   rt   return_dictr   )rg   	set_stateru   prepare_inputs_from_block_stater`   prepare_modelsrm   as_dictitemskeysr\   r-   r.   r5   r*   r6   expandshapert   
noise_predcleanup_models)
r   r0   r1   r2   r3   guider_stateguider_state_batchcond_kwargskvs
             r    r8   zWanLoopDenoiser.__call__   s    	##@_@_jk#l "((HHVZVoVop #/ 	E,,Z-C-CD,446K (--/Aq116688 jELL.I144))*qPK  -CJ,B,B -)<<??@Q@QR+"@"@"F"Fq"IJMMkN_N_`!,!=!=!	-
 - -) ,,Z-C-CD%	E* ",!2!2<!@!C;&&)s   AG)r:   r;   r<   r=   r   r?   r   rb   r>   r   r   ro   r!   r   r/   r-   r@   r   r   rA   r.   r   r8   __classcell__rd   s   @r    rS   rS      s    J 0GHs.t!#s(^, 	
T-%8 	
 	
 
S 
 
 U38_-  * U]]_'','';E''JM''RWR^R^''	'' ''r#   rS   c                       e Zd ZdZddifdeeef   f fdZede	e
   fd       Zedefd       Zede	e   fd	       Zede	eeef      fd
       Z ej$                         dedededej,                  def
d       Z xZS )Wan22LoopDenoiserr   rT   rU   rX   c                 ~    t        |t              st        dt        |             || _        t
        |           y)ay  Initialize a denoiser block that calls the denoiser model. This block is used in Wan2.2.

        Args:
            guider_input_fields: A dictionary that maps each argument expected by the denoiser model
                (for example, "encoder_hidden_states") to data stored on `block_state`. The value can be either:

                - A tuple of strings. For instance, `{"encoder_hidden_states": ("prompt_embeds",
                  "negative_prompt_embeds")}` tells the guider to read `block_state.prompt_embeds` and
                  `block_state.negative_prompt_embeds` and pass them as the conditional and unconditional batches of
                  `encoder_hidden_states`.
                - A string. For example, `{"encoder_hidden_image": "image_embeds"}` makes the guider forward
                  `block_state.image_embeds` for both conditional and unconditional batches.
        rZ   Nr[   rc   s     r    rb   zWan22LoopDenoiser.__init__  re   r#   r   c           	          t        dt        t        ddi      d      t        dt        t        ddi      d      t        dt              t        d	t              gS )
Nrg   rh   g      @ri   rj   guider_2g      @rm   transformer_2rn   r   s    r    ro   z%Wan22LoopDenoiser.expected_components"  sf     &!#3S"9:(5	 &!#3S"9:(5	 -)>?/+@A
 	
r#   c                      	 yrq   r   r   s    r    r!   zWan22LoopDenoiser.description5  r"   r#   c                      t        ddd      gS )Nboundary_ratiog      ?zUThe boundary ratio to divide the denoising loop into high noise and low noise stages.)rw   defaultr!   )r   r   s    r    expected_configsz"Wan22LoopDenoiser.expected_configs=  s     %s
 	
r#   c                 N   t        d      t        ddt        d      g}g }| j                  j                         D ]5  }t	        |t
              r|j                  |       %|j                  |       7 |D ]-  }|j                  t        |dt        j                               / |S rs   rx   r}   s        r    r/   zWan22LoopDenoiser.inputsG  r   r#   r0   r1   r2   r3   c           
         |j                   j                  |j                  z  }||k\  r#|j                  |_        |j
                  |_        n"|j                  |_        |j                  |_        |j
                  j                  ||j                  |       |j
                  j                  || j                        }|D ]\  }|j
                  j                  |j                         |j                         }|j                         D 	
ci c]Y  \  }	}
|	| j                  j                         v r8|	t!        |
t"        j$                        r|
j'                  |j(                        n|
[ }}	}
 |j                  d|j*                  j'                  |j(                        |j-                  |j*                  j.                  d         j'                  |j(                        |j0                  dd|d   |_        |j
                  j5                  |j                         _ |j                  |      d   |_        ||fS c c}
}	w r   )rk   r   num_train_timestepsrm   current_modelrg   r   r   r   ru   r   r`   r   r   r   r   r\   r-   r.   r5   r*   r6   r   r   rt   r   r   )r   r0   r1   r2   r3   boundary_timestepr   r   r   r   r   s              r    r8   zWan22LoopDenoiser.__call__]  s    '--<<z?]?]]!!(2(>(>K%!+!2!2K(2(@(@K%!+!4!4K$$!A`A`kl$m #))II+W[WpWpq #/ 	I--k.G.GH,446K (--/Aq116688 jELL.I144))*qPK  -FK,E,E -)<<??@Q@QR+"@"@"F"Fq"IJMMkN_N_`!,!=!=!	-
 - -) --k.G.GH%	I* "-!3!3L!A!!D;&&)s   
AH;)r:   r;   r<   r=   r   r?   r   rb   r>   r   r   ro   r!   r   r   r   r/   r-   r@   r   r   rA   r.   r   r8   r   r   s   @r    r   r   	  s    J 0GHs.t!#s(^, 
T-%8 
 
$ 
S 
 
 
$z"2 
 
 U38_-  * 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
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    ro   z(WanLoopAfterDenoiser.expected_components       +'>?
 	
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#   r0   r1   r2   r3   c                 V   |j                   j                  }|j                  j                  |j                  j                         ||j                   j                         d      d   |_         |j                   j                  |k7  r |j                   j                  |      |_         ||fS )NF)r   r   )r%   r*   r   r   r   floatr5   )r   r0   r1   r2   r3   latents_dtypes         r    r8   zWanLoopAfterDenoiser.__call__  s     $++11(2277""((*%%'	 8 

  $$5"-"5"5"8"8"GK;&&r#   N)r:   r;   r<   r=   r>   r   r   ro   r?   r!   r-   r@   r   r   rA   r.   r8   r   r#   r    r   r     s    J
T-%8 
 

 
S 
 
 U]]_'#5 'J 'SV '[`[g[g ' 'r#   r   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              gS r   r   r   s    r    loop_expected_componentsz.WanDenoiseLoopWrapper.loop_expected_components  r   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.r'   ru   rv   )r   r-   r.   rA   r   s    r    loop_inputsz!WanDenoiseLoopWrapper.loop_inputs  s?     ,,u	 % F	
 	
r#   r0   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)r2   r3   r   )get_block_statemaxlenr   ru   r   ordernum_warmup_stepsprogress_bar	enumerate	loop_stepupdateset_block_state)r   r0   r   r1   r   r2   r3   s          r    r8   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=   r>   r?   r!   r   r   r   r   r   r-   r@   r   r   r8   r   r#   r    r   r     s    J
S 
 
 
$}*= 
 

 
T*- 
 
  U]]_!#5 !m !P] ! !r#   r   c                   F    e Zd Ze eddi      egZg dZede	fd       Z
y)WanDenoiseSteprT   rU   rX   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` sequentially:
 - `WanLoopBeforeDenoiser`
 - `WanLoopDenoiser`
 - `WanLoopAfterDenoiser`
This block supports text-to-video tasks for wan2.1.r   r   s    r    r!   zWanDenoiseStep.description      B	
r#   N)r:   r;   r<   r   rS   r   block_classesblock_namesr>   r?   r!   r   r#   r    r   r     sE    ')T!	

 	M DK	
S 	
 	
r#   r   c                   F    e Zd Ze eddi      egZg dZede	fd       Z
y)Wan22DenoiseSteprT   rU   r   r   r   c                      	 y)NaA  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` sequentially:
 - `WanLoopBeforeDenoiser`
 - `Wan22LoopDenoiser`
 - `WanLoopAfterDenoiser`
This block supports text-to-video tasks for Wan2.2.r   r   s    r    r!   zWan22DenoiseStep.description  r   r#   N)r:   r;   r<   r   r   r   r   r   r>   r?   r!   r   r#   r    r   r     sE    ')T!	

 	M DK	
S 	
 	
r#   r   c                   H    e Zd Ze eddd      egZg dZede	fd       Z
y)	WanImage2VideoDenoiseSteprU   image_embedsrT   encoder_hidden_states_imager   r   r   c                      	 y)NaK  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` sequentially:
 - `WanImage2VideoLoopBeforeDenoiser`
 - `WanLoopDenoiser`
 - `WanLoopAfterDenoiser`
This block supports image-to-video tasks for wan2.1.r   r   s    r    r!   z%WanImage2VideoDenoiseStep.description'      C	
r#   N)r:   r;   r<   rC   rS   r   r   r   r>   r?   r!   r   r#   r    r   r     sE    ()T/=!	
 		M DK	
S 	
 	
r#   r   c                   F    e Zd Ze eddi      egZg dZede	fd       Z
y)Wan22Image2VideoDenoiseSteprT   rU   r   r   r   c                      	 y)NaK  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` sequentially:
 - `WanImage2VideoLoopBeforeDenoiser`
 - `WanLoopDenoiser`
 - `WanLoopAfterDenoiser`
This block supports image-to-video tasks for Wan2.2.r   r   s    r    r!   z'Wan22Image2VideoDenoiseStep.description@  r   r#   N)r:   r;   r<   rC   r   r   r   r   r>   r?   r!   r   r#   r    r   r   4  sE    (')T!	

 	M DK	
S 	
 	
r#   r   c                   H    e Zd Ze eddd      egZg dZede	fd       Z
y)	WanFLF2VDenoiseSteprU   r   r   r   r   r   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` sequentially:
 - `WanFLF2VLoopBeforeDenoiser`
 - `WanLoopDenoiser`
 - `WanLoopAfterDenoiser`
This block supports FLF2V tasks for wan2.1.r   r   s    r    r!   zWanFLF2VDenoiseStep.descriptionZ  s    :	
r#   N)r:   r;   r<   rM   rS   r   r   r   r>   r?   r!   r   r#   r    r   r   M  sE    ")T/=!	
 		M DK	
S 	
 	
r#   r   ))typingr   r   r   r   r-   configuration_utilsr   guidersr	   modelsr
   
schedulersr   utilsr   modular_pipeliner   r   r   r   modular_pipeline_utilsr   r   r   r   
get_loggerr:   loggerr   rC   rM   rS   r   r   r   r   r   r   r   r   r   r#   r    <module>r      s    * )  - - + 1   K J 0 
		H	%'1 'D'''< ''T''!6 ''Tk'+ k'\D'- D'N'0 'D3!8 3!l
* 
2
, 
2
 5 
4
"7 
2
/ 
r#   