
    +h<                     J   S SK r 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Jr  SS	KJrJrJr  S
SKJr  \	R(                  " \5      r    SS\\   S\\\\R2                  4      S\\\      S\\\      4S jjr " S S\5      r " S S\5      r " S S\5      rg)    N)ListOptionalUnion   )UniPCMultistepScheduler)logging)randn_tensor   )ModularPipelineBlocksPipelineState)ComponentSpec
InputParamOutputParam   )WanModularPipelinenum_inference_stepsdevice	timestepssigmasc                    Ub  Ub  [        S5      eUb  S[        [        R                  " U R                  5      R
                  R                  5       5      ;   nU(       d  [        SU R                   S35      eU R                  " S
X2S.UD6  U R                  n[        U5      nX14$ Ub  S[        [        R                  " U R                  5      R
                  R                  5       5      ;   nU(       d  [        SU R                   S35      eU R                  " S
XBS.UD6  U R                  n[        U5      nX14$ U R                  " U4S	U0UD6  U R                  nX14$ )a  
Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.

Args:
    scheduler (`SchedulerMixin`):
        The scheduler to get timesteps from.
    num_inference_steps (`int`):
        The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps`
        must be `None`.
    device (`str` or `torch.device`, *optional*):
        The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
    timesteps (`List[int]`, *optional*):
        Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed,
        `num_inference_steps` and `sigmas` must be `None`.
    sigmas (`List[float]`, *optional*):
        Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed,
        `num_inference_steps` and `timesteps` must be `None`.

Returns:
    `Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
    second element is the number of inference steps.
zYOnly one of `timesteps` or `sigmas` can be passed. Please choose one to set custom valuesr   zThe current scheduler class zx's `set_timesteps` does not support custom timestep schedules. Please check whether you are using the correct scheduler.)r   r   r   zv's `set_timesteps` does not support custom sigmas schedules. Please check whether you are using the correct scheduler.)r   r   r    )

ValueErrorsetinspect	signatureset_timesteps
parameterskeys	__class__r   len)	schedulerr   r   r   r   kwargsaccepts_timestepsaccept_sigmass           h/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/modular_pipelines/wan/before_denoise.pyretrieve_timestepsr&   &   s}   > !3tuu'3w/@/@AXAX/Y/d/d/i/i/k+ll .y/B/B.C Da b  	M)MfM''	!)n )) 
	 C(9(9):Q:Q(R(](](b(b(d$ee.y/B/B.C D_ `  	GvGG''	!)n )) 	 3MFMfM''	))    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\S\\   4S j5       rS r\R                  " 5       S	\S
\S\4S j5       rSrg)WanInputStepa   wanreturnc                      g)Na  Input processing step that:
  1. Determines `batch_size` and `dtype` based on `prompt_embeds`
  2. Adjusts input tensor shapes based on `batch_size` (number of prompts) and `num_videos_per_prompt`

All input tensors are expected to have either batch_size=1 or match the batch_size
of prompt_embeds. The tensors will be duplicated across the batch dimension to
have a final batch_size of batch_size * num_videos_per_prompt.r   selfs    r%   descriptionWanInputStep.descriptiond   s    M	
r'   c                     [        SSS9/$ )Nnum_videos_per_promptr   defaultr   r.   s    r%   inputsWanInputStep.inputso   s     .:
 	
r'   c                 h    [        SS[        R                  SS9[        S[        R                  SS9/$ )Nprompt_embedsTzGPre-generated text embeddings. Can be generated from text_encoder step.required	type_hintr0   negative_prompt_embedszPPre-generated negative text embeddings. Can be generated from text_encoder step.r=   r0   )r   torchTensorr.   s    r%   intermediate_inputs WanInputStep.intermediate_inputsu   s=     ,,e	 (,,n
 	
r'   c           
          [        S[        SS9[        S[        R                  SS9[        S[        R                  SSS	9[        S
[        R                  SSS	9/$ )N
batch_sizezdNumber of prompts, the final batch size of model inputs should be batch_size * num_videos_per_promptr?   dtypez@Data type of model tensor inputs (determined by `prompt_embeds`)r:   guider_input_fieldsz2text embeddings used to guide the image generation)r=   kwargs_typer0   r>   z;negative text embeddings used to guide the image generation)r   intr@   rF   rA   r.   s    r%   intermediate_outputs!WanInputStep.intermediate_outputs   sm      C
 ++^
 ,,1P	 (,,1Y	#
 	
r'   c                    UR                   bw  UR                  bi  UR                   R                  UR                  R                  :w  a:  [        SUR                   R                   SUR                  R                   S35      eg g g )Nzu`prompt_embeds` and `negative_prompt_embeds` must have the same shape when passed directly, but got: `prompt_embeds` z != `negative_prompt_embeds` .)r:   r>   shaper   )r/   
componentsblock_states      r%   check_inputsWanInputStep.check_inputs   s    $$0[5W5W5c((..+2T2T2Z2ZZ --8-F-F-L-L,M N#::@@AD  [ 6d0r'   rO   statec                    U R                  U5      nU R                  X5        UR                  R                  S   Ul        UR                  R
                  Ul        UR                  R                  u  pEnUR                  R                  SUR                  S5      Ul        UR                  R                  UR                  UR                  -  US5      Ul        UR                  b~  UR                  R                  u  pEnUR                  R                  SUR                  S5      Ul	        UR                  R                  UR                  UR                  -  US5      Ul	        U R                  X#5        X4$ )Nr   r   )get_block_staterQ   r:   rN   rE   rF   repeatr3   viewr>   set_block_state)r/   rO   rS   rP   _seq_lens         r%   __call__WanInputStep.__call__   sN   **51*2!,!:!:!@!@!C'55;;#1177A$/$=$=$D$DQHiHikl$m!$/$=$=$B$B""[%F%FFQS%
! --9'>>DDMA1<1S1S1Z1Z;44a2K. 2=1S1S1X1X&&)J)JJGUW2K. 	U0  r'   r   N)__name__
__module____qualname____firstlineno__
model_namepropertystrr0   r   r   r7   rB   rJ   rQ   r@   no_gradr   r   r\   __static_attributes__r   r'   r%   r)   r)   a   s    J
S 
 
 
Z( 
 

 
T#Y 
 
 
d3i 
 
4 ]]_!#5 !m !P] ! !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\S\\   4S j5       r\R                   " 5       S\S	\S\4S
 j5       rSrg)WanSetTimestepsStep   r+   r,   c                 $    [        S[        5      /$ )Nr!   )r   r   r.   s    r%   expected_components'WanSetTimestepsStep.expected_components   s     +'>?
 	
r'   c                     g)Nz6Step that sets the scheduler's timesteps for inferencer   r.   s    r%   r0   WanSetTimestepsStep.description   s    Gr'   c                 @    [        SSS9[        S5      [        S5      /$ )Nr   2   r4   r   r   r6   r.   s    r%   r7   WanSetTimestepsStep.inputs   s)     ,b9{#x 
 	
r'   c                 R    [        S[        R                  SS9[        S[        SS9/$ )Nr   z"The timesteps to use for inferencer?   r   z:The number of denoising steps to perform at inference time)r   r@   rA   rI   r.   s    r%   rJ   (WanSetTimestepsStep.intermediate_outputs   s0     u||Imn%X
 	
r'   rO   rS   c                 
   U R                  U5      nUR                  Ul        [        UR                  UR
                  UR                  UR                  UR                  5      u  Ul        Ul        U R                  X#5        X4$ N)	rV   _execution_devicer   r&   r!   r   r   r   rY   r/   rO   rS   rP   s       r%   r\   WanSetTimestepsStep.__call__   s|    **51'99AS  ++!!B
>{> 	U0  r'   r   N)r^   r_   r`   ra   rb   rc   r   r   rk   rd   r0   r   r7   r   rJ   r@   re   r   r   r\   rf   r   r'   r%   rh   rh      s    J
T-%8 
 

 HS H H 
Z( 
 
 
d;&7 
 
 ]]_!#5 !m !P] ! !r'   rh   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\S\\   4S j5       r\S\\   4S j5       r\S	 5       r\        SS\S\S\S\S\S\\R*                     S\\R,                     S\\\R0                  \\R0                     4      S\\R2                     S\R2                  4S jj5       r\R6                  " 5       S\S\S\4S j5       rSrg
)WanPrepareLatentsStep   r+   r,   c                     / $ ru   r   r.   s    r%   rk   )WanPrepareLatentsStep.expected_components   s    	r'   c                     g)NzWPrepare latents step that prepares the latents for the text-to-video generation processr   r.   s    r%   r0   !WanPrepareLatentsStep.description   s    hr'   c           
          [        S[        S9[        S[        S9[        S[        S9[        S[        [        R                     S9[        S[        SS9/$ )	Nheight)r=   width
num_frameslatentsr3   r   )r=   r5   )r   rI   r   r@   rA   r.   s    r%   r7   WanPrepareLatentsStep.inputs  sL     x3/w#.|s3yHU\\,BC.#qI
 	
r'   c                 h    [        S5      [        SS[        SS9[        S[        R                  SS9/$ )	N	generatorrE   TzNumber of prompts, the final batch size of model inputs should be `batch_size * num_videos_per_prompt`. Can be generated in input step.r;   rF   zThe dtype of the model inputsr?   )r   rI   r@   rF   r.   s    r%   rB   )WanPrepareLatentsStep.intermediate_inputs  s@     {# f	 w%++Cbc	
 		
r'   c                 6    [        S[        R                  SS9/$ )Nr   z4The initial latents to use for the denoising processr?   )r   r@   rA   r.   s    r%   rJ   *WanPrepareLatentsStep.intermediate_outputs  s"     U\\?u
 	
r'   c           	         UR                   b  UR                   U R                  -  S:w  d*  UR                  bP  UR                  U R                  -  S:w  a3  [        SU R                   SUR                    SUR                   S35      eUR                  bW  UR                  S:  d   UR                  S-
  U R
                  -  S:w  a&  [        SU R
                   SUR                   S35      eg g )	Nr   z-`height` and `width` have to be divisible by z	 but are z and rM   r   zQ`num_frames` has to be greater than 0, and (num_frames - 1) must be divisible by z
, but got )r   vae_scale_factor_spatialr   r   r   vae_scale_factor_temporal)rO   rP   s     r%   rQ   "WanPrepareLatentsStep.check_inputs   sO   *{/A/AJDgDg/gkl/l)k.?.?*BeBe.eij.j?
@c@c?ddmny  oA  oA  nB  BG  HS  HY  HY  GZ  Z[  \  !!-""Q&;+A+AA+EImIm*mqr*rcdn  eI  eI  dJ  JT  U`  Uk  Uk  Tl  lm  n  +s .r'   NrE   num_channels_latentsr   r   r   rF   r   r   r   c
                 L   U	b  U	R                  XvS9$ US-
  U R                  -  S-   n
UUU
[        U5      U R                  -  [        U5      U R                  -  4n[	        U[
        5      (       a*  [        U5      U:w  a  [        S[        U5       SU S35      e[        XXvS9n	U	$ )N)r   rF   r   z/You have passed a list of generators of length z+, but requested an effective batch size of z@. Make sure the batch size matches the length of the generators.)r   r   rF   )	tor   rI   r   
isinstancelistr    r   r	   )comprE   r   r   r   r   rF   r   r   r   num_latent_framesrN   s               r%   prepare_latents%WanPrepareLatentsStep.prepare_latents/  s     ::V:99'!^0N0NNQRR K4888J$777
 i&&3y>Z+GA#i.AQ R&<'gi 
 u&Vr'   rO   rS   c                    U R                  U5      nUR                  =(       d    UR                  Ul        UR                  =(       d    UR                  Ul        UR
                  =(       d    UR                  Ul        UR                  Ul        [        R                  Ul        UR                  Ul        U R                  X5        U R                  UUR                  UR                   -  UR                  UR                  UR                  UR
                  UR                  UR                  UR"                  UR$                  5
      Ul        U R'                  X#5        X4$ ru   )rV   r   default_heightr   default_widthr   default_num_framesrv   r   r@   float32rF   r   rQ   r   rE   r3   r   r   rY   rw   s       r%   r\   WanPrepareLatentsStep.__call__Q  s   **51(//L:3L3L'--I1I1I!,!7!7!X:;X;X'99!MM+5+J+J(*2"22""[%F%FF,,""!!
 	U0  r'   r   )   i  i@  Q   NNNN) r^   r_   r`   ra   rb   rc   r   r   rk   rd   r0   r   r7   rB   r   rJ   staticmethodrQ   rI   r   r@   rF   r   r   	GeneratorrA   r   re   r   r   r\   rf   r   r'   r%   rz   rz      s   JT-%8   iS i i 
Z( 
 
 

T*%5 

 

 
d;&7 
 
   
 %''+)-MQ*. " 	
   $ & E%//43H"HIJ %,,' 
 @ ]]_!#5 !m !P] ! !r'   rz   )NNNN)r   typingr   r   r   r@   
schedulersr   utilsr   utils.torch_utilsr	   modular_pipeliner   r   modular_pipeline_utilsr   r   r   r   
get_loggerr^   loggerrI   rd   r   floatr&   r)   rh   rz   r   r'   r%   <module>r      s     ( (  1  - C K K 0 
		H	% *.15%)$(8*!#8* U3,-.8* S	"	8*
 T%[!8*va!( a!H.!/ .!bw!1 w!r'   