o
    ۷i3I                     @   sD  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
 ddlmZmZ d	d
lmZmZmZmZ d	dlmZmZmZmZ ddlmZmZ e rYd dlm  mZ dZnd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'G dd de&Z(G dd de&Z)dS )     )AnyN   )
FrozenDict)ClassifierFreeGuidance)Flux2Transformer2DModel)FlowMatchEulerDiscreteScheduler)is_torch_xla_availablelogging   )
BlockStateLoopSequentialPipelineBlocksModularPipelineBlocksPipelineState)ComponentSpec
ConfigSpec
InputParamOutputParam   )Flux2KleinModularPipelineFlux2ModularPipelineTFc                   @   ~   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 )Flux2LoopDenoiserflux2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/flux2/denoise.pyexpected_components0      z%Flux2LoopDenoiser.expected_componentsc                 C      	 dS NzStep within the denoising loop that denoises the latents for Flux2. This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` object (e.g. `Flux2DenoiseLoopWrapper`)r    r   r    r    r!   description4      zFlux2LoopDenoiser.descriptionc                 C   sv   t dt ddtjddt 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gS )Njoint_attention_kwargslatentsT.The latents to denoise. Shape: (B, seq_len, C)required	type_hintr&   image_latentsAPacked image latents for conditioning. Shape: (B, img_seq_len, C)r-   r&   image_latent_ids:Position IDs for image latents. Shape: (B, img_seq_len, 4)guidancezGuidance scale as a tensorprompt_embedszText embeddings from Mistral3txt_ids,4D position IDs for text tokens (T, H, W, L)
latent_ids.4D position IDs for latent tokens (T, H, W, L)r   torchTensorr   r    r    r!   inputs<   sT   zFlux2LoopDenoiser.inputs
componentsblock_stateitc              
   C   s   |j }||jj}|j}t|dd }|d ur1tj||gdd|jj}|j}	tj||	gdd}|	|j
d |j}
|j||
d |j|j|j||jddd }|d d d |df }||_||fS Nr.   r   dimr     F)hidden_statestimestepr3   encoder_hidden_statesr5   img_idsr(   return_dict)r)   tor   dtyper7   getattrr:   catr1   expandshaper3   r4   r5   r(   size
noise_predr   r=   r>   r?   r@   r)   latent_model_inputrH   r.   r1   rF   rQ   r    r    r!   __call__j   s0   	zFlux2LoopDenoiser.__call__N)__name__
__module____qualname__
model_namepropertylistr   r"   strr&   tupler   r<   r:   no_gradr   r   intr;   r   rT   r    r    r    r!   r   -   s(    -r   c                   @   r   )Flux2KleinLoopDenoiserflux2-kleinr   c                 C   r   r   r   r   r    r    r!   r"      r#   z*Flux2KleinLoopDenoiser.expected_componentsc                 C   r$   r%   r    r   r    r    r!   r&      r'   z"Flux2KleinLoopDenoiser.descriptionc                 C   sf   t dt ddtjddt 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gS )Nr(   r)   Tr*   r+   r.   r/   r0   r1   r2   r4   Text embeddings from Qwen3r5   r6   r7   r8   r9   r   r    r    r!   r<      sH   zFlux2KleinLoopDenoiser.inputsr=   r>   r?   r@   c              
   C   s   |j }||jj}|j}t|dd }|d ur1tj||gdd|jj}|j}	tj||	gdd}|	|j
d |j}
|j||
d d |j|j||jddd }|d d d |df }||_||fS rA   )r)   rJ   r   rK   r7   rL   r:   rM   r1   rN   rO   r4   r5   r(   rP   rQ   rR   r    r    r!   rT      s0   	zFlux2KleinLoopDenoiser.__call__N)rU   rV   rW   rX   rY   rZ   r   r"   r[   r&   r\   r   r<   r:   r]   r   r   r^   r;   r   rT   r    r    r    r!   r_      s(    'r_   c                   @   s   e Zd ZdZedee fd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 )Flux2KleinBaseLoopDenoiserr`   r   c                 C   s"   t dtt dttddiddgS )Nr   guiderguidance_scaleg      @from_config)configdefault_creation_method)r   r   r   r   r   r    r    r!   r"      s   
z.Flux2KleinBaseLoopDenoiser.expected_componentsc                 C   s   t dddgS )Nis_distilledF)namedefault)r   r   r    r    r!   expected_configs   s   
z+Flux2KleinBaseLoopDenoiser.expected_configsc                 C   r$   r%   r    r   r    r    r!   r&      r'   z&Flux2KleinBaseLoopDenoiser.descriptionc                 C   s   t dt ddtjddt 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+   r.   r/   r0   r1   r2   r4   ra   negative_prompt_embedsFz#Negative text embeddings from Qwen3r5   r6   negative_txt_idsz54D position IDs for negative text tokens (T, H, W, L)r7   r8   r9   r   r    r    r!   r<     s`   z!Flux2KleinBaseLoopDenoiser.inputsr=   r>   r?   r@   c              
      sd  |j }||jj}|j}t|dd }|d ur1tj||gdd|jj}|j}	tj||	gdd}|	|j
d |j}
t|dd t|dd ft|dd t|dd fd	}|jj||j|d
 |j|}|D ]= |j|j  fdd| D }|jd||
d d ||jdd|d }|d d d |df  _|j|j qh||d |_||fS )Nr.   r   rB   r   r4   rl   r5   rm   )rG   r5   )stepnum_inference_stepsrF   c                    s   i | ]}|t  |qS r    )rL   ).0
input_nameguider_state_batchr    r!   
<dictcomp>V  s    z7Flux2KleinBaseLoopDenoiser.__call__.<locals>.<dictcomp>rD   F)rE   rF   r3   rH   r(   rI   r    )r)   rJ   r   rK   r7   rL   r:   rM   r1   rN   rO   rc   	set_statero   prepare_inputsprepare_modelskeysr(   rP   rQ   cleanup_models)r   r=   r>   r?   r@   r)   rS   rH   r.   r1   rF   guider_inputsguider_statecond_kwargsrQ   r    rr   r!   rT   6  sJ   



	z#Flux2KleinBaseLoopDenoiser.__call__N)rU   rV   rW   rX   rY   rZ   r   r"   r   rk   r[   r&   r\   r   r<   r:   r]   r   r   r^   r;   r   rT   r    r    r    r!   rb      s,    3rb   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 )Flux2LoopAfterDenoiserr   r   c                 C   r   )N	scheduler)r   r   r   r    r    r!   r"   m  r#   z*Flux2LoopAfterDenoiser.expected_componentsc                 C   r$   )NzStep within the denoising loop that updates the latents after denoising. This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` object (e.g. `Flux2DenoiseLoopWrapper`)r    r   r    r    r!   r&   q  r'   z"Flux2LoopAfterDenoiser.descriptionc                 C   s   g S )Nr    r   r    r    r!   r<   y  s   zFlux2LoopAfterDenoiser.inputsc                 C   s
   t dgS )N	generator)r   r   r    r    r!   intermediate_inputs}  s   
z*Flux2LoopAfterDenoiser.intermediate_inputsc                 C   s   t dtjddgS )Nr)   zThe denoised latentsr0   )r   r:   r;   r   r    r    r!   intermediate_outputs  s   z+Flux2LoopAfterDenoiser.intermediate_outputsr=   r>   r?   r@   c                 C   sT   |j j}|jj|j||j ddd |_ |j j|kr&tjj r&|j 	||_ ||fS )NF)rI   r   )
r)   rK   r~   rn   rQ   r:   backendsmpsis_availablerJ   )r   r=   r>   r?   r@   latents_dtyper    r    r!   rT     s   zFlux2LoopAfterDenoiser.__call__N)rU   rV   rW   rX   rY   rZ   r   r"   r[   r&   r\   r   r<   r   r   r   r:   r]   r   r   r^   r;   rT   r    r    r    r!   r}   j  s    "r}   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 )Flux2DenoiseLoopWrapperr   r   c                 C   r$   )NzPipeline block that iteratively denoises the latents over `timesteps`. The specific steps within each iteration can be customized with `sub_blocks` attributer    r   r    r    r!   r&     r'   z#Flux2DenoiseLoopWrapper.descriptionc                 C   s   t dtt dtgS )Nr~   r   )r   r   r   r   r    r    r!   loop_expected_components  s   z0Flux2DenoiseLoopWrapper.loop_expected_componentsc                 C   s"   t ddtjddt ddtddgS )N	timestepsTz/The timesteps to use for the denoising process.r+   ro   z?The number of inference steps to use for the denoising process.)r   r:   r;   r^   r   r    r    r!   loop_inputs  s   z#Flux2DenoiseLoopWrapper.loop_inputsr=   statec                 C   s   |  |}tt|j|j|jj  d|_| j|jdA}t	|jD ]3\}}| j
||||d\}}|t|jd ksK|d |jkrO|d |jj dkrO|  trUt  q"W d    n1 s`w   Y  | || ||fS )Nr   )total)r?   r@   r   )get_block_statemaxlenr   ro   r~   ordernum_warmup_stepsprogress_bar	enumerate	loop_stepupdateXLA_AVAILABLExm	mark_stepset_block_state)r   r=   r   r>   r   r?   r@   r    r    r!   rT     s"   
"z Flux2DenoiseLoopWrapper.__call__N)rU   rV   rW   rX   rY   r[   r&   rZ   r   r   r   r   r:   r]   r   r   rT   r    r    r    r!   r     s    r   c                   @   .   e Zd ZeegZddgZedefddZ	dS )Flux2DenoiseStepdenoiserafter_denoiserr   c                 C   r$   )NaJ  Denoise step that iteratively denoises the latents for Flux2. 
Its loop logic is defined in `Flux2DenoiseLoopWrapper.__call__` method 
At each iteration, it runs blocks defined in `sub_blocks` sequentially:
 - `Flux2LoopDenoiser`
 - `Flux2LoopAfterDenoiser`
This block supports both text-to-image and image-conditioned generation.r    r   r    r    r!   r&     r'   zFlux2DenoiseStep.descriptionN)
rU   rV   rW   r   r}   block_classesblock_namesrY   r[   r&   r    r    r    r!   r     
    r   c                   @   r   )Flux2KleinDenoiseStepr   r   r   c                 C   r$   )NaO  Denoise step that iteratively denoises the latents for Flux2. 
Its loop logic is defined in `Flux2DenoiseLoopWrapper.__call__` method 
At each iteration, it runs blocks defined in `sub_blocks` sequentially:
 - `Flux2KleinLoopDenoiser`
 - `Flux2LoopAfterDenoiser`
This block supports both text-to-image and image-conditioned generation.r    r   r    r    r!   r&     r'   z!Flux2KleinDenoiseStep.descriptionN)
rU   rV   rW   r_   r}   r   r   rY   r[   r&   r    r    r    r!   r     r   r   c                   @   r   )Flux2KleinBaseDenoiseStepr   r   r   c                 C   r$   )NaS  Denoise step that iteratively denoises the latents for Flux2. 
Its loop logic is defined in `Flux2DenoiseLoopWrapper.__call__` method 
At each iteration, it runs blocks defined in `sub_blocks` sequentially:
 - `Flux2KleinBaseLoopDenoiser`
 - `Flux2LoopAfterDenoiser`
This block supports both text-to-image and image-conditioned generation.r    r   r    r    r!   r&     r'   z%Flux2KleinBaseDenoiseStep.descriptionN)
rU   rV   rW   rb   r}   r   r   rY   r[   r&   r    r    r    r!   r     r   r   )*typingr   r:   configuration_utilsr   guidersr   modelsr   
schedulersr   utilsr   r	   modular_pipeliner   r   r   r   modular_pipeline_utilsr   r   r   r   r   r   torch_xla.core.xla_modelcore	xla_modelr   r   
get_loggerrU   loggerr   r_   rb   r}   r   r   r   r   r    r    r    r!   <module>   s0   
_Y ,: