
    biw                          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mZmZmZ dd	lmZmZmZ d
dlmZ  e
j*                  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   )FluxTransformer2DModel)FlowMatchEulerDiscreteScheduler)logging   )
BlockStateLoopSequentialPipelineBlocksModularPipelineBlocksPipelineState)ComponentSpec
InputParamOutputParam   )FluxModularPipelinec                       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j                         dededed	ej$                  def
d
       Zy)FluxLoopDenoiserfluxreturnc                 $    t        dt              gS )Ntransformer)r   r   selfs    c/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/modular_pipelines/flux/denoise.pyexpected_componentsz$FluxLoopDenoiser.expected_components&   s    m-CDEE    c                      	 y)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   descriptionzFluxLoopDenoiser.description*       5	
r   c                 j   t        d      t        ddt        j                  d      t        ddt        j                  d      t        ddt        j                  d	      t        d
dt        j                  d      t        ddt        j                  d      t        ddt        j                  d      gS )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   inputszFluxLoopDenoiser.inputs2   s     /0,,|	 ,,8	 ,,/	 &,,6	 ,,M	 ",,N	A'
 '	
r   
componentsblock_stateitc                     |j                  |j                  |j                         dz  |j                  |j                  |j
                  |j                  |j                  |j                  d	      d   }||_	        ||fS )Ni  F)	hidden_statestimestepr)   encoder_hidden_statespooled_projectionsr$   txt_idsimg_idsreturn_dictr   )
r   r%   flattenr)   r*   r+   r$   r,   r-   
noise_pred)r   r1   r2   r3   r4   r>   s         r   __call__zFluxLoopDenoiser.__call__]   s      ++%--YY[4' ))"-";";*??#.#E#E((00 , 

 

 ",;&&r   N)__name__
__module____qualname__
model_namepropertyr   r   r   strr!   r   r   r0   r.   no_gradr   r   intr/   r   r?   r    r   r   r   r   #   s    JFT-%8 F F 
S 
 
 (
U38_- (
 (
T U]]_'-'<F'KN'SXS_S_'	' '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)FluxLoopAfterDenoiserr   r   c                 $    t        dt              gS )N	scheduler)r   r   r   s    r   r   z)FluxLoopAfterDenoiser.expected_componentst   s    k+JKLLr   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. `FluxDenoiseLoopWrapper`)r    r   s    r   r!   z!FluxLoopAfterDenoiser.descriptionx   r"   r   c                     g S )Nr    r   s    r   r0   zFluxLoopAfterDenoiser.inputs   s    	r   c                     t        d      gS )N	generator)r   r   s    r   intermediate_inputsz)FluxLoopAfterDenoiser.intermediate_inputs   s    ;'((r   c                 <    t        dt        j                  d      gS )Nr%   zThe denoised latents)r(   r!   )r   r.   r/   r   s    r   intermediate_outputsz*FluxLoopAfterDenoiser.intermediate_outputs   s    IKabccr   r1   r2   r3   r4   c                    |j                   j                  }|j                  j                  |j                  ||j                   d      d   |_         |j                   j                  |k7  r |j                   j                  |      |_         ||fS )NF)r<   r   )r%   dtyperK   stepr>   to)r   r1   r2   r3   r4   latents_dtypes         r   r?   zFluxLoopAfterDenoiser.__call__   s     $++11(2277""	 8 

  $$5"-"5"5"8"8"GK;&&r   N)r@   rA   rB   rC   rD   r   r   r   rE   r!   r   r   r0   rP   r   rR   r.   rF   r   r   rG   r/   r?   r    r   r   rI   rI   q   s    JMT-%8 M M 
S 
 
 U38_-   )T#Y ) ) dd;&7 d d U]]_'#6 'Z 'TW '\a\h\h ' 'r   rI   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	)
FluxDenoiseLoopWrapperr   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"FluxDenoiseLoopWrapper.description   s    d	
r   c                 B    t        dt              t        dt              gS )NrK   r   )r   r   r   r   s    r   loop_expected_componentsz/FluxDenoiseLoopWrapper.loop_expected_components   s$     +'FG-)?@
 	
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&   num_inference_stepszgThe number of inference steps to use for the denoising process. Can be generated in set_timesteps step.)r   r.   r/   rG   r   s    r   loop_inputsz"FluxDenoiseLoopWrapper.loop_inputs   s?     ,,u	 % F	
 	
r   r1   statec                    | j                  |      }t        t        |j                        |j                  |j
                  j                  z  z
  d      |_        |j
                  j                  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)r3   r4   r   )get_block_statemaxlenr^   r_   rK   ordernum_warmup_stepsset_begin_indexprogress_bar	enumerate	loop_stepupdateset_block_state)r   r1   ra   r2   rj   r3   r4   s          r   r?   zFluxDenoiseLoopWrapper.__call__   s;   **51'*%%&)H)H:K_K_KeKe)eegh(
$
 	,,Q/[%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   A!E3EEEN)r@   rA   rB   rC   rD   rE   r!   r   r   r\   r   r`   r.   rF   r   r   r?   r    r   r   rY   rY      s    J
S 
 
 
$}*= 
 
 
T*- 
 
  U]]_!#6 !} !Q^ ! !r   rY   c                   2    e Zd ZeegZddgZedefd       Z	y)FluxDenoiseStepdenoiserafter_denoiserr   c                      	 y)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!   zFluxDenoiseStep.description   s    E	
r   N)
r@   rA   rB   r   rI   block_classesblock_namesrD   rE   r!   r    r   r   rp   rp      s1    %'<=M/0K
S 
 
r   rp   )typingr   r   r   r.   modelsr   
schedulersr   utilsr	   modular_pipeliner   r   r   r   modular_pipeline_utilsr   r   r   r   
get_loggerr@   loggerr   rI   rY   rp   r    r   r   <module>r~      s|    $ #  , 9   L K 1 
		H	%K', K'\)'1 )'X6!9 6!r
, 
r   