
    +hw                          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JrJr  SS	KJr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g)    )AnyListTupleN   )FluxTransformer2DModel)FlowMatchEulerDiscreteScheduler)logging   )
BlockStateLoopSequentialPipelineBlocksModularPipelineBlocksPipelineState)ComponentSpec
InputParamOutputParam   )FluxModularPipelinec                       \ 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\R                  " 5       S\S\S	\S
\R&                  S\4
S j5       rSrg)FluxLoopDenoiser#   fluxreturnc                 $    [        S[        5      /$ )Ntransformer)r   r   selfs    b/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/modular_pipelines/flux/denoise.pyexpected_components$FluxLoopDenoiser.expected_components&   s    m-CDEE    c                      g)NzStep within the denoising loop that denoise the latents. This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` object (e.g. `FluxDenoiseLoopWrapper`) r   s    r   descriptionFluxLoopDenoiser.description*       5	
r    c                 F   [        S5      [        SS[        R                  SS9[        SS[        R                  SS9[        SS[        R                  S	S9[        S
S[        R                  SS9[        SS[        R                  SS9[        SS[        R                  SS9/$ )Njoint_attention_kwargslatentsTz^The initial latents to use for the denoising process. Can be generated in prepare_latent step.required	type_hintr#   guidancezGuidance scale as a tensorprompt_embedszPrompt embeddingspooled_prompt_embedszPooled prompt embeddingstext_idsz/IDs computed from text sequence needed for RoPElatent_image_idsz0IDs computed from image sequence needed for RoPE)r   torchTensorr   s    r   inputsFluxLoopDenoiser.inputs2   s     /0,,|	 ,,8	 ,,/	 &,,6	 ,,M	 ",,N	A'
 '	
r    
componentsblock_stateitc                     UR                  UR                  UR                  5       S-  UR                  UR                  UR
                  UR                  UR                  UR                  SS9	S   nXRl	        X4$ )Ni  F)	hidden_statestimestepr,   encoder_hidden_statespooled_projectionsr'   txt_idsimg_idsreturn_dictr   )
r   r(   flattenr,   r-   r.   r'   r/   r0   
noise_pred)r   r5   r6   r7   r8   rB   s         r   __call__FluxLoopDenoiser.__call__]   s      ++%--YY[4' ))"-";";*??#.#E#E((00 , 

 

 ",&&r    r"   N)__name__
__module____qualname____firstlineno__
model_namepropertyr   r   r   strr#   r   r   r3   r1   no_gradr   r   intr2   r   rC   __static_attributes__r"   r    r   r   r   #   s    JFT-%8 F F 
S 
 
 (
U38_- (
 (
T ]]_'-'<F'KN'SXS_S_'	' '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)FluxLoopAfterDenoiserq   r   r   c                 $    [        S[        5      /$ )N	scheduler)r   r   r   s    r   r   )FluxLoopAfterDenoiser.expected_componentst   s    k+JKLLr    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. `FluxDenoiseLoopWrapper`)r"   r   s    r   r#   !FluxLoopAfterDenoiser.descriptionx   r%   r    c                     / $ )Nr"   r   s    r   r3   FluxLoopAfterDenoiser.inputs   s    	r    c                     [        S5      /$ )N	generator)r   r   s    r   intermediate_inputs)FluxLoopAfterDenoiser.intermediate_inputs   s    ;'((r    c                 6    [        S[        R                  SS9/$ )Nr(   zThe denoised latents)r+   r#   )r   r1   r2   r   s    r   intermediate_outputs*FluxLoopAfterDenoiser.intermediate_outputs   s    IKabccr    r5   r6   r7   r8   c                    UR                   R                  nUR                  R                  UR                  UUR                   SS9S   Ul         UR                   R                  U:w  a   UR                   R                  U5      Ul         X4$ )NF)r@   r   )r(   dtyperS   steprB   to)r   r5   r6   r7   r8   latents_dtypes         r   rC   FluxLoopAfterDenoiser.__call__   s     $++11(2277""	 8 

  $$5"-"5"5"8"8"GK&&r    r"   N)rE   rF   rG   rH   rI   rJ   r   r   r   rK   r#   r   r   r3   r[   r   r^   r1   rL   r   r   rM   r2   rC   rN   r"   r    r   rP   rP   q   s    JMT-%8 M M 
S 
 
 U38_-   )T#Y ) ) dd;&7 d d ]]_'#6 'Z 'TW '\a\h\h ' 'r    rP   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)FluxDenoiseLoopWrapper   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#   "FluxDenoiseLoopWrapper.description   s    d	
r    c                 B    [        S[        5      [        S[        5      /$ )NrS   r   )r   r   r   r   s    r   loop_expected_components/FluxDenoiseLoopWrapper.loop_expected_components   s$     +'FG-)?@
 	
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.r)   num_inference_stepszgThe number of inference steps to use for the denoising process. Can be generated in set_timesteps step.)r   r1   r2   rM   r   s    r   loop_inputs"FluxDenoiseLoopWrapper.loop_inputs   s?     ,,u	 % F	
 	
r    r5   statec           	         U R                  U5      n[        [        UR                  5      UR                  UR
                  R                  -  -
  S5      Ul        UR
                  R                  S5        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)r7   r8   r   )get_block_statemaxlenro   rp   rS   ordernum_warmup_stepsset_begin_indexprogress_bar	enumerate	loop_stepupdateset_block_state)r   r5   rs   r6   r|   r7   r8   s          r   rC   FluxDenoiseLoopWrapper.__call__   s$   **51'*%%&)H)H:K_K_KeKe)eegh(
$
 	,,Q/[%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   AD>,D>D>>
Er"   N)rE   rF   rG   rH   rI   rJ   rK   r#   r   r   rl   r   rq   r1   rL   r   r   rC   rN   r"   r    r   rg   rg      s    J
S 
 
 
$}*= 
 
 
T*- 
 
  ]]_!#6 !} !Q^ ! !r    rg   c                   <    \ rS rSr\\/rSS/r\S\	4S j5       r
Srg)FluxDenoiseStep   denoiserafter_denoiserr   c                      g)Na*  Denoise step that iteratively denoise the latents. 
Its loop logic is defined in `FluxDenoiseLoopWrapper.__call__` method 
At each iteration, it runs blocks defined in `sub_blocks` sequencially:
 - `FluxLoopDenoiser`
 - `FluxLoopAfterDenoiser`
This block supports both text2image and img2img tasks.r"   r   s    r   r#   FluxDenoiseStep.description   s    E	
r    r"   N)rE   rF   rG   rH   r   rP   block_classesblock_namesrJ   rK   r#   rN   r"   r    r   r   r      s1    %'<=M/0K
S 
 
r    r   )typingr   r   r   r1   modelsr   
schedulersr   utilsr	   modular_pipeliner   r   r   r   modular_pipeline_utilsr   r   r   r   
get_loggerrE   loggerr   rP   rg   r   r"   r    r   <module>r      sz    $ #  , 9   L K 1 
		H	%K', K'\)'1 )'X6!9 6!r
, 
r    