o
    ۷i/                     @   s   d dl 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eZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )    )AnyN   )FluxTransformer2DModel)FlowMatchEulerDiscreteScheduler)logging   )
BlockStateLoopSequentialPipelineBlocksModularPipelineBlocksPipelineState)ComponentSpec
InputParamOutputParam   )FluxModularPipelinec                   @   ~   e Zd ZdZedee fddZedefddZ	edee
eef  fddZe d	ed
ededejdef
ddZdS )FluxLoopDenoiserfluxreturnc                 C      t dtgS Ntransformerr   r   self r   ^/home/ubuntu/vllm_env/lib/python3.10/site-packages/diffusers/modular_pipelines/flux/denoise.pyexpected_components&      z$FluxLoopDenoiser.expected_componentsc                 C      	 dS )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   r   r   r   r   description*      zFluxLoopDenoiser.descriptionc                 C   sj   t dt ddtjddt ddtjddt d	dtjd
dt ddtjddt ddtjddt ddtjd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   r   r   r   inputs2   sL   zFluxLoopDenoiser.inputs
componentsblock_stateitc                 C   sD   |j |j| d |j|j|j|j|j|jdd	d }||_	||fS )N  F	hidden_statestimestepr(   encoder_hidden_statespooled_projectionsr"   r.   r0   return_dictr   )
r   r#   flattenr(   r*   r,   r"   r.   r0   
noise_pred)r   r5   r6   r7   r8   rA   r   r   r   __call__\   s   

zFluxLoopDenoiser.__call__N__name__
__module____qualname__
model_namepropertylistr   r   strr    tupler   r4   r2   no_gradr   r   intr3   r   rB   r   r   r   r   r   #   s(    )r   c                   @   r   )FluxKontextLoopDenoiserflux-kontextr   c                 C   r   r   r   r   r   r   r   r   s   r   z+FluxKontextLoopDenoiser.expected_componentsc                 C   r   )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   r   r   r   r    w   r!   z#FluxKontextLoopDenoiser.descriptionc                 C   sx   t dt ddtjddt dtjddt d	d
tjddt ddtjddt ddtjddt ddtjddt ddtjd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*   r+   r,   r-   r.   r/   r0   z1IDs computed from latent sequence needed for RoPEr1   r   r   r   r   r4      sV   zFluxKontextLoopDenoiser.inputsr5   r6   r7   r8   c           
      C   s   |j }|}|j}|d urtj||gdd}||jd |j}|j||d |j	|j
|j|j|j|jdd	d }	|	d d d |df }	|	|_||fS )Nr   )dimr   r9   Fr:   )r#   rP   r2   catexpandshapetodtyper   r(   r*   r,   r"   r.   r0   sizerA   )
r   r5   r6   r7   r8   r#   latent_model_inputrP   r<   rA   r   r   r   rB      s,   
z FluxKontextLoopDenoiser.__call__NrC   r   r   r   r   rN   p   s(    .rN   c                	   @   s   e Zd ZdZedee fddZedefddZ	edee
eef  fddZedee fd	d
Zedee fddZe dedededejfddZdS )FluxLoopAfterDenoiserr   r   c                 C   r   )N	scheduler)r   r   r   r   r   r   r      r   z)FluxLoopAfterDenoiser.expected_componentsc                 C   r   )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   r   r   r   r       r!   z!FluxLoopAfterDenoiser.descriptionc                 C   s   g S )Nr   r   r   r   r   r4      s   zFluxLoopAfterDenoiser.inputsc                 C   s
   t dgS )N	generator)r   r   r   r   r   intermediate_inputs   s   
z)FluxLoopAfterDenoiser.intermediate_inputsc                 C   s   t dtjddgS )Nr#   zThe denoised latentsrQ   )r   r2   r3   r   r   r   r   intermediate_outputs   s   z*FluxLoopAfterDenoiser.intermediate_outputsr5   r6   r7   r8   c                 C   sH   |j j}|jj|j||j ddd |_ |j j|kr |j ||_ ||fS )NF)r?   r   )r#   rW   r[   steprA   rV   )r   r5   r6   r7   r8   latents_dtyper   r   r   rB      s   zFluxLoopAfterDenoiser.__call__N)rD   rE   rF   rG   rH   rI   r   r   rJ   r    rK   r   r4   r]   r   r^   r2   rL   r   r   rM   r3   rB   r   r   r   r   rZ      s    "rZ   c                   @   sl   e Zd ZdZedefddZedee fddZ	edee
 fddZe d	ed
edefddZdS )FluxDenoiseLoopWrapperr   r   c                 C   r   )NzPipeline block that iteratively denoise the latents over `timesteps`. The specific steps with each iteration can be customized with `sub_blocks` attributesr   r   r   r   r   r       r!   z"FluxDenoiseLoopWrapper.descriptionc                 C   s   t dtt dtgS )Nr[   r   )r   r   r   r   r   r   r   loop_expected_components   s   z/FluxDenoiseLoopWrapper.loop_expected_componentsc                 C   s"   t ddtjddt ddtd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   r2   r3   rM   r   r   r   r   loop_inputs  s   z"FluxDenoiseLoopWrapper.loop_inputsr5   statec                 C   s   |  |}tt|j|j|jj  d|_| j|jd;}t	|jD ]-\}}| j
||||d\}}|t|jd ksK|d |jkrO|d |jj dkrO|  q"W d    n1 sZw   Y  | || ||fS )Nr   )total)r7   r8   r   )get_block_statemaxlenrc   rd   r[   ordernum_warmup_stepsprogress_bar	enumerate	loop_stepupdateset_block_state)r   r5   rf   r6   rm   r7   r8   r   r   r   rB     s   
"zFluxDenoiseLoopWrapper.__call__N)rD   rE   rF   rG   rH   rJ   r    rI   r   rb   r   re   r2   rL   r   r   rB   r   r   r   r   ra      s    ra   c                   @   s.   e Zd ZeegZddgZedefddZ	dS )FluxDenoiseStepdenoiserafter_denoiserr   c                 C   r   )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   r   r   r   r    0  r!   zFluxDenoiseStep.descriptionN)
rD   rE   rF   r   rZ   block_classesblock_namesrH   rJ   r    r   r   r   r   rr   ,  s
    rr   c                   @   s2   e Zd ZdZeegZddgZede	fddZ
dS )FluxKontextDenoiseSteprO   rs   rt   r   c                 C   r   )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   r   r   r   r    A  r!   z"FluxKontextDenoiseStep.descriptionN)rD   rE   rF   rG   rN   rZ   ru   rv   rH   rJ   r    r   r   r   r   rw   <  s    rw   )typingr   r2   modelsr   
schedulersr   utilsr   modular_pipeliner   r	   r
   r   modular_pipeline_utilsr   r   r   r   
get_loggerrD   loggerr   rN   rZ   ra   rr   rw   r   r   r   r   <module>   s   
MZ,6