
    i/                     $   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 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    r/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/diffusers/modular_pipelines/flux/denoise.pyexpected_componentsz$FluxLoopDenoiser.expected_components&       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latentsT^The initial latents to use for the denoising process. Can be generated in prepare_latent step.required	type_hintr$   guidanceFGuidance scale as a tensorprompt_embedsPrompt embeddingspooled_prompt_embedsPooled prompt embeddingstxt_ids/IDs computed from text sequence needed for RoPEimg_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 )N  F	hidden_statestimestepr-   encoder_hidden_statespooled_projectionsr'   r3   r5   return_dictr   )
r   r(   flattenr-   r/   r1   r'   r3   r5   
noise_pred)r   r:   r;   r<   r=   rG   s         r   __call__zFluxLoopDenoiser.__call__\   s      ++%--YY[4' ))"-";";*??#.#E#E'''' , 

 

 ",;&&r!   N__name__
__module____qualname__
model_namepropertyr   r   r   strr$   r   r   r9   r7   no_gradr   r   intr8   r   rH   r#   r!   r   r   r   #   s    JFT-%8 F F 
S 
 
 '
U38_- '
 '
R 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j                         dededed	ej$                  def
d
       Zy)FluxKontextLoopDenoiserflux-kontextr   c                 $    t        dt              gS r   r   r   s    r   r   z+FluxKontextLoopDenoiser.expected_componentss   r    r!   c                      	 y)NzStep within the denoising loop that denoise the latents for Flux Kontext. This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` object (e.g. `FluxDenoiseLoopWrapper`)r#   r   s    r   r$   z#FluxKontextLoopDenoiser.descriptionw   r%   r!   c                    t        d      t        ddt        j                  d      t        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 )Nr'   r(   Tr)   r*   image_latentszXImage latents to use for the denoising process. Can be generated in prepare_latent step.r,   r$   r-   Fr.   r/   r0   r1   r2   r3   r4   r5   z1IDs computed from latent sequence needed for RoPEr6   r   s    r   r9   zFluxKontextLoopDenoiser.inputs   s     /0,,|	 ,,v
 ,,8	 ,,/	 &,,6	 ,,M	 ,,O	K+
 +	
r!   r:   r;   r<   r=   c                    |j                   }|}|j                  }|t        j                  ||gd      }|j	                  |j
                  d         j                  |j                        }|j                  ||dz  |j                  |j                  |j                  |j                  |j                  |j                  d	      d   }	|	d d d |j                  d      f   }	|	|_        ||fS )Nr   )dimr   r?   Fr@   )r(   rX   r7   catexpandshapetodtyper   r-   r/   r1   r'   r3   r5   sizerG   )
r   r:   r;   r<   r=   r(   latent_model_inputrX   rB   rG   s
             r   rH   z FluxKontextLoopDenoiser.__call__   s     %%$#11$!&,>+NTU!V88GMM!,-00?++,_ ))"-";";*??#.#E#E'''' , 

 

  #4W\\!_#4 45
!+;&&r!   NrI   r#   r!   r   rS   rS   p   s    JFT-%8 F F 
S 
 
 ,
U38_- ,
 ,
\ U]]_'-'<F'KN'SXS_S_'	' 'r!   rS   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_components   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.description   r%   r!   c                     g S )Nr#   r   s    r   r9   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 latentsrY   )r   r7   r8   r   s    r   intermediate_outputsz*FluxLoopAfterDenoiser.intermediate_outputs   s    IKabccr!   r:   r;   r<   r=   c                    |j                   j                  }|j                  j                  |j                  ||j                   d      d   |_         |j                   j                  |k7  r |j                   j                  |      |_         ||fS )NF)rE   r   )r(   r`   rf   steprG   r_   )r   r:   r;   r<   r=   latents_dtypes         r   rH   zFluxLoopAfterDenoiser.__call__   s     $++11(2277""	 8 

  $$5"-"5"5"8"8"GK;&&r!   N)rJ   rK   rL   rM   rN   r   r   r   rO   r$   r   r   r9   rk   r   rm   r7   rP   r   r   rQ   r8   rH   r#   r!   r   rd   rd      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!   rd   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 )Nrf   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   r7   r8   rQ   r   s    r   loop_inputsz"FluxDenoiseLoopWrapper.loop_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<   r=   r   )get_block_statemaxlenrw   rx   rf   ordernum_warmup_stepsprogress_bar	enumerate	loop_stepupdateset_block_state)r   r:   rz   r;   r   r<   r=   s          r   rH   zFluxDenoiseLoopWrapper.__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)rJ   rK   rL   rM   rN   rO   r$   r   r   ru   r   ry   r7   rP   r   r   rH   r#   r!   r   rr   rr      s    J
S 
 
 
$}*= 
 
 
T*- 
 
  U]]_!#6 !} !Q^ ! !r!   rr   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` sequentially:
 - `FluxLoopDenoiser`
 - `FluxLoopAfterDenoiser`
This block supports both text2image and img2img tasks.r#   r   s    r   r$   zFluxDenoiseStep.description0      E	
r!   N)
rJ   rK   rL   r   rd   block_classesblock_namesrN   rO   r$   r#   r!   r   r   r   ,  s1    %'<=M/0K
S 
 
r!   r   c                   6    e Zd ZdZeegZddgZede	fd       Z
y)FluxKontextDenoiseSteprT   r   r   r   c                      	 y)Na1  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` sequentially:
 - `FluxKontextLoopDenoiser`
 - `FluxLoopAfterDenoiser`
This block supports both text2image and img2img tasks.r#   r   s    r   r$   z"FluxKontextDenoiseStep.descriptionA  r   r!   N)rJ   rK   rL   rM   rS   rd   r   r   rN   rO   r$   r#   r!   r   r   r   <  s6    J,.CDM/0K
S 
 
r!   r   )typingr   r   r   r7   modelsr   
schedulersr   utilsr	   modular_pipeliner   r   r   r   modular_pipeline_utilsr   r   r   r   
get_loggerrJ   loggerr   rS   rd   rr   r   r   r#   r!   r   <module>r      s    $ #  , 9   L K 1 
		H	%J', J'ZW'3 W't)'1 )'X3!9 3!l
, 
 
3 
r!   