o
    GÆÏiD0  ã                
   @   s¢  d dl mZ ddlmZmZ ddlmZmZ ddlm	Z	m
Z
mZmZmZ ddlmZmZ ddlmZ dd	lmZmZ dd
lmZmZ e e¡ZG dd„ deƒZG dd„ deƒZedeƒ fdeƒ fdeƒ fde	ƒ fdeƒ fdeƒ fdeƒ fgƒZG dd„ deƒZedeƒ fde
ƒ fdeƒ fdeƒ fde	ƒ fdeƒ fdeƒ fdeƒ fgƒZ G dd„ deƒZ!G dd„ deƒZ"edeƒ fdeƒ fde"ƒ fdeƒ fgƒZ#G d d!„ d!eƒZ$d"S )#é   )Úloggingé   )ÚAutoPipelineBlocksÚSequentialPipelineBlocks)ÚInsertableDictÚOutputParamé   )ÚFlux2PrepareGuidanceStepÚFlux2PrepareImageLatentsStepÚFlux2PrepareLatentsStepÚFlux2RoPEInputsStepÚFlux2SetTimestepsStep)ÚFlux2DecodeStepÚFlux2UnpackLatentsStep)ÚFlux2DenoiseStep)ÚFlux2TextEncoderStepÚFlux2VaeEncoderStep)ÚFlux2ProcessImagesInputStepÚFlux2TextInputStepc                   @   s:   e Zd ZdZdZeƒ eƒ gZddgZe	de
fdd„ƒZdS )	ÚFlux2VaeEncoderSequentialStepaË  
    VAE encoder step that preprocesses, encodes, and prepares image latents for Flux2 conditioning.

      Components:
          image_processor (`Flux2ImageProcessor`) vae (`AutoencoderKLFlux2`)

      Inputs:
          image (`None`, *optional*):
              TODO: Add description.
          height (`None`, *optional*):
              TODO: Add description.
          width (`None`, *optional*):
              TODO: Add description.
          generator (`None`, *optional*):
              TODO: Add description.

      Outputs:
          condition_images (`list`):
              TODO: Add description.
          image_latents (`list`):
              List of latent representations for each reference image
    Úflux2Ú
preprocessÚencodeÚreturnc                 C   ó   dS )Nz_VAE encoder step that preprocesses, encodes, and prepares image latents for Flux2 conditioning.© ©Úselfr   r   új/home/ubuntu/.local/lib/python3.10/site-packages/diffusers/modular_pipelines/flux2/modular_blocks_flux2.pyÚdescriptionG   ó   z)Flux2VaeEncoderSequentialStep.descriptionN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
model_namer   r   Úblock_classesÚblock_namesÚpropertyÚstrr   r   r   r   r   r   *   s    r   c                   @   s.   e Zd ZdZegZdgZdgZedd„ ƒZ	dS )ÚFlux2AutoVaeEncoderStepa‘  
    VAE encoder step that encodes the image inputs into their latent representations.
      This is an auto pipeline block that works for image conditioning tasks.
       - `Flux2VaeEncoderSequentialStep` is used when `image` is provided.
       - If `image` is not provided, step will be skipped.

      Components:
          image_processor (`Flux2ImageProcessor`) vae (`AutoencoderKLFlux2`)

      Inputs:
          image (`None`, *optional*):
              TODO: Add description.
          height (`None`, *optional*):
              TODO: Add description.
          width (`None`, *optional*):
              TODO: Add description.
          generator (`None`, *optional*):
              TODO: Add description.

      Outputs:
          condition_images (`list`):
              TODO: Add description.
          image_latents (`list`):
              List of latent representations for each reference image
    Úimg_conditioningÚimagec                 C   ó   	 dS )Na  VAE encoder step that encodes the image inputs into their latent representations.
This is an auto pipeline block that works for image conditioning tasks.
 - `Flux2VaeEncoderSequentialStep` is used when `image` is provided.
 - If `image` is not provided, step will be skipped.r   r   r   r   r   r   l   ó   ÿz#Flux2AutoVaeEncoderStep.descriptionN)
r!   r"   r#   r$   r   r&   r'   Úblock_trigger_inputsr(   r   r   r   r   r   r*   M   s    r*   ÚinputÚprepare_latentsÚset_timestepsÚprepare_guidanceÚprepare_rope_inputsÚdenoiseÚafter_denoisec                   @   ó<   e Zd ZdZdZe ¡ Ze ¡ Z	e
dd„ ƒZe
dd„ ƒZdS )ÚFlux2CoreDenoiseStepa  
    Core denoise step that performs the denoising process for Flux2-dev.

      Components:
          scheduler (`FlowMatchEulerDiscreteScheduler`) transformer (`Flux2Transformer2DModel`)

      Inputs:
          num_images_per_prompt (`None`, *optional*, defaults to 1):
              TODO: Add description.
          prompt_embeds (`Tensor`):
              Pre-generated text embeddings. Can be generated from text_encoder step.
          height (`int`, *optional*):
              TODO: Add description.
          width (`int`, *optional*):
              TODO: Add description.
          latents (`Tensor | NoneType`, *optional*):
              TODO: Add description.
          generator (`None`, *optional*):
              TODO: Add description.
          num_inference_steps (`None`, *optional*, defaults to 50):
              TODO: Add description.
          timesteps (`None`, *optional*):
              TODO: Add description.
          sigmas (`None`, *optional*):
              TODO: Add description.
          guidance_scale (`None`, *optional*, defaults to 4.0):
              TODO: Add description.
          joint_attention_kwargs (`None`, *optional*):
              TODO: Add description.
          image_latents (`Tensor`, *optional*):
              Packed image latents for conditioning. Shape: (B, img_seq_len, C)
          image_latent_ids (`Tensor`, *optional*):
              Position IDs for image latents. Shape: (B, img_seq_len, 4)

      Outputs:
          latents (`Tensor`):
              Denoised latents.
    r   c                 C   r   )NzDCore denoise step that performs the denoising process for Flux2-dev.r   r   r   r   r   r   ±   r    z Flux2CoreDenoiseStep.descriptionc                 C   ó   t  d¡gS ©NÚlatents©r   Útemplater   r   r   r   Úoutputsµ   ó   ÿzFlux2CoreDenoiseStep.outputsN)r!   r"   r#   r$   r%   ÚFlux2CoreDenoiseBlocksÚvaluesr&   Úkeysr'   r(   r   r>   r   r   r   r   r8   „   s    '
r8   Úprepare_image_latentsc                   @   r7   )Ú$Flux2ImageConditionedCoreDenoiseStepap  
    Core denoise step that performs the denoising process for Flux2-dev with image conditioning.

      Components:
          scheduler (`FlowMatchEulerDiscreteScheduler`) transformer (`Flux2Transformer2DModel`)

      Inputs:
          num_images_per_prompt (`None`, *optional*, defaults to 1):
              TODO: Add description.
          prompt_embeds (`Tensor`):
              Pre-generated text embeddings. Can be generated from text_encoder step.
          image_latents (`list`, *optional*):
              TODO: Add description.
          height (`int`, *optional*):
              TODO: Add description.
          width (`int`, *optional*):
              TODO: Add description.
          latents (`Tensor | NoneType`, *optional*):
              TODO: Add description.
          generator (`None`, *optional*):
              TODO: Add description.
          num_inference_steps (`None`, *optional*, defaults to 50):
              TODO: Add description.
          timesteps (`None`, *optional*):
              TODO: Add description.
          sigmas (`None`, *optional*):
              TODO: Add description.
          guidance_scale (`None`, *optional*, defaults to 4.0):
              TODO: Add description.
          joint_attention_kwargs (`None`, *optional*):
              TODO: Add description.

      Outputs:
          latents (`Tensor`):
              Denoised latents.
    r   c                 C   r   )Nz\Core denoise step that performs the denoising process for Flux2-dev with image conditioning.r   r   r   r   r   r   ö   r    z0Flux2ImageConditionedCoreDenoiseStep.descriptionc                 C   r9   r:   r<   r   r   r   r   r>   ú   r?   z,Flux2ImageConditionedCoreDenoiseStep.outputsN)r!   r"   r#   r$   r%   Ú&Flux2ImageConditionedCoreDenoiseBlocksrA   r&   rB   r'   r(   r   r>   r   r   r   r   rD   Ë   s    %
rD   c                   @   s4   e Zd ZdZeegZddgZddgZe	dd„ ƒZ
dS )ÚFlux2AutoCoreDenoiseStepr   Úimage_conditionedÚ
text2imageÚimage_latentsNc                 C   r-   )Na:  Auto core denoise step that performs the denoising process for Flux2-dev.This is an auto pipeline block that works for text-to-image and image-conditioned generation. - `Flux2CoreDenoiseStep` is used for text-to-image generation.
 - `Flux2ImageConditionedCoreDenoiseStep` is used for image-conditioned generation.
r   r   r   r   r   r     r.   z$Flux2AutoCoreDenoiseStep.description)r!   r"   r#   r%   rD   r8   r&   r'   r/   r(   r   r   r   r   r   rF     s    rF   Útext_encoderÚvae_encoderÚdecodec                   @   sP   e Zd ZdZdZe ¡ Ze ¡ Z	ddidddœdœZ
edd„ ƒZed	d
„ ƒZdS )ÚFlux2AutoBlocksa5  
    Auto Modular pipeline for text-to-image and image-conditioned generation using Flux2.

      Supported workflows:
        - `text2image`: requires `prompt`
        - `image_conditioned`: requires `image`, `prompt`

      Components:
          text_encoder (`Mistral3ForConditionalGeneration`) tokenizer (`AutoProcessor`) image_processor
          (`Flux2ImageProcessor`) vae (`AutoencoderKLFlux2`) scheduler (`FlowMatchEulerDiscreteScheduler`) transformer
          (`Flux2Transformer2DModel`)

      Inputs:
          prompt (`None`, *optional*):
              TODO: Add description.
          max_sequence_length (`int`, *optional*, defaults to 512):
              TODO: Add description.
          text_encoder_out_layers (`tuple`, *optional*, defaults to (10, 20, 30)):
              TODO: Add description.
          image (`None`, *optional*):
              TODO: Add description.
          height (`None`, *optional*):
              TODO: Add description.
          width (`None`, *optional*):
              TODO: Add description.
          generator (`None`, *optional*):
              TODO: Add description.
          num_images_per_prompt (`None`, *optional*, defaults to 1):
              TODO: Add description.
          image_latents (`list`, *optional*):
              TODO: Add description.
          latents (`Tensor | NoneType`):
              TODO: Add description.
          num_inference_steps (`None`):
              TODO: Add description.
          timesteps (`None`):
              TODO: Add description.
          sigmas (`None`, *optional*):
              TODO: Add description.
          guidance_scale (`None`, *optional*, defaults to 4.0):
              TODO: Add description.
          joint_attention_kwargs (`None`, *optional*):
              TODO: Add description.
          image_latent_ids (`Tensor`, *optional*):
              Position IDs for image latents. Shape: (B, img_seq_len, 4)
          output_type (`None`, *optional*, defaults to pil):
              TODO: Add description.

      Outputs:
          images (`list`):
              Generated images.
    r   ÚpromptT)r,   rN   )rH   rG   c                 C   r   )NzUAuto Modular pipeline for text-to-image and image-conditioned generation using Flux2.r   r   r   r   r   r   \  r    zFlux2AutoBlocks.descriptionc                 C   r9   )NÚimagesr<   r   r   r   r   r>   `  r?   zFlux2AutoBlocks.outputsN)r!   r"   r#   r$   r%   ÚAUTO_BLOCKSrA   r&   rB   r'   Ú_workflow_mapr(   r   r>   r   r   r   r   rM     s    5þ
rM   N)%Úutilsr   Úmodular_pipeliner   r   Úmodular_pipeline_utilsr   r   Úbefore_denoiser	   r
   r   r   r   Údecodersr   r   r5   r   Úencodersr   r   Úinputsr   r   Ú
get_loggerr!   Úloggerr   r*   r@   r8   rE   rD   rF   rP   rM   r   r   r   r   Ú<module>   sV   
#)ùÿ8øÿ6üÿ