o
    Gi#                     @   s   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	 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dS )    N   )
FrozenDict)Flux2ImageProcessor)logging   )ModularPipelineBlocksPipelineState)ComponentSpec
InputParamOutputParam   )Flux2ModularPipelinec                   @   l   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 )Flux2TextInputStepflux2returnc                 C      	 dS NzThis step:
  1. Determines `batch_size` and `dtype` based on `prompt_embeds`
  2. Ensures all text embeddings have consistent batch sizes (batch_size * num_images_per_prompt) selfr   r   \/home/ubuntu/.local/lib/python3.10/site-packages/diffusers/modular_pipelines/flux2/inputs.pydescription      zFlux2TextInputStep.descriptionc                 C   s    t dddt dddtjddgS )	Nnum_images_per_promptr   defaultprompt_embedsTdenoiser_input_fieldsGPre-generated text embeddings. Can be generated from text_encoder step.requiredkwargs_type	type_hintr   r
   torchTensorr   r   r   r   inputs'   s   
zFlux2TextInputStep.inputsc                 C   s.   t dtddt dtjddt dtjddd	gS )
N
batch_sizedNumber of prompts, the final batch size of model inputs should be batch_size * num_images_per_promptr#   r   dtype@Data type of model tensor inputs (determined by `prompt_embeds`)r   r   2Text embeddings used to guide the image generationr#   r"   r   r   intr%   r+   r&   r   r   r   r   intermediate_outputs4   s"   z'Flux2TextInputStep.intermediate_outputs
componentsstatec                 C   sr   |  |}|jjd |_|jj|_|jj\}}}|jd|jd|_|j|j|j |d|_| || ||fS Nr   r   )	get_block_stater   shaper(   r+   repeatr   viewset_block_stater   r2   r3   block_state_seq_lenr   r   r   __call__I   s   

zFlux2TextInputStep.__call__N__name__
__module____qualname__
model_namepropertystrr   listr
   r'   r1   r%   no_gradr   r   r?   r   r   r   r   r      s    r   c                   @   r   )Flux2KleinBaseTextInputStepzflux2-kleinr   c                 C   r   r   r   r   r   r   r   r   ]   r   z'Flux2KleinBaseTextInputStep.descriptionc              	   C   s2   t dddt dddtjddt d	d
dtjddgS )Nr   r   r   r   Tr   r   r    negative_prompt_embedsFzPPre-generated negative text embeddings. Can be generated from text_encoder step.r$   r   r   r   r   r'   e   s    
z"Flux2KleinBaseTextInputStep.inputsc              	   C   s>   t dtddt dtjddt dtjddd	t d
tjddd	gS )Nr(   r)   r*   r+   r,   r   r   r-   r.   rJ   z;Negative text embeddings used to guide the image generationr/   r   r   r   r   r1   y   s.   z0Flux2KleinBaseTextInputStep.intermediate_outputsr2   r3   c                 C   s   |  |}|jjd |_|jj|_|jj\}}}|jd|jd|_|j|j|j |d|_|jd urR|jj\}}}|jd|jd|_|j|j|j |d|_| 	|| ||fS r4   )
r6   r   r7   r(   r+   r8   r   r9   rJ   r:   r;   r   r   r   r?      s$   


z$Flux2KleinBaseTextInputStep.__call__Nr@   r   r   r   r   rI   Z   s    rI   c                   @   s~   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e fd	d
Ze dedefddZdS )Flux2ProcessImagesInputStepr   r   c                 C   s   dS )NzMImage preprocess step for Flux2. Validates and preprocesses reference images.r   r   r   r   r   r      s   z'Flux2ProcessImagesInputStep.descriptionc                 C   s   t dttdddddgS )Nimage_processor       )vae_scale_factorvae_latent_channelsfrom_config)configdefault_creation_method)r	   r   r   r   r   r   r   expected_components   s   z/Flux2ProcessImagesInputStep.expected_componentsc                 C   s   t dt dt dgS )Nimageheightwidth)r
   r   r   r   r   r'      s   z"Flux2ProcessImagesInputStep.inputsc                 C   s   t dttj dgS )Ncondition_images)namer#   )r   rG   r%   r&   r   r   r   r   r1      s   z0Flux2ProcessImagesInputStep.intermediate_outputsr2   r3   c                 C   s  |  |}|j}|d u rd |_| || ||fS t|ts!|g}g }|D ]O}|j| |j\}}|| dkrD|j	|d}|j\}}|j
d }	||	 |	 }||	 |	 }|jj|||dd}
||
 |jd u rl||_|jd u rt||_q%||_| || ||fS )Ni   r   crop)rV   rW   resize_mode)r6   rU   rX   r:   
isinstancerG   rL   check_image_inputsize_resize_to_target_arearO   
preprocessappendrV   rW   )r   r2   r3   r<   imagesrX   imgimage_widthimage_heightmultiple_ofcondition_imgr   r   r   r?      s<   







z$Flux2ProcessImagesInputStep.__call__N)rA   rB   rC   rD   rE   rF   r   rG   r	   rT   r
   r'   r   r1   r%   rH   r   r   r?   r   r   r   r   rK      s    
rK   )r%   configuration_utilsr   pipelines.flux2.image_processorr   utilsr   modular_pipeliner   r   modular_pipeline_utilsr	   r
   r   r   
get_loggerrA   loggerr   rI   rK   r   r   r   r   <module>   s   
>T