
    i8;                         d dl mZ d dl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 dd	l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y)    )ListN   )FluxPipeline)logging   )ModularPipelineBlocksPipelineState)
InputParamOutputParam) calculate_dimension_from_latentsrepeat_tensor_to_batch_size   )FluxModularPipelinec                       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
d Z ej                         dededefd	       Zy
)FluxTextInputStepfluxreturnc                      	 y)NzText input processing step that standardizes text embeddings for the pipeline.
This 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) selfs    q/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/diffusers/modular_pipelines/flux/inputs.pydescriptionzFluxTextInputStep.description#   s    p	
    c                     t        dd      t        dddt        j                  d      t        d	dt        j                  d
      gS )Nnum_images_per_promptr   )defaultprompt_embedsTdenoiser_input_fieldszGPre-generated text embeddings. Can be generated from text_encoder step.)requiredkwargs_type	type_hintr   pooled_prompt_embedszNPre-generated pooled text embeddings. Can be generated from text_encoder step.)r!   r"   r   )r
   torchTensorr   s    r   inputszFluxTextInputStep.inputs,   sN     .:3,,e &3,,l	
 	
r   c           	          t        dt        d      t        dt        j                  d      t        dt        j                  dd	      t        d
t        j                  dd	      gS )N
batch_sizezdNumber of prompts, the final batch size of model inputs should be batch_size * num_images_per_prompt)r"   r   dtypez@Data type of model tensor inputs (determined by `prompt_embeds`)r   r   z2text embeddings used to guide the image generation)r"   r!   r   r#   z9pooled text embeddings used to guide the image generation)r   intr$   r)   r%   r   s    r   intermediate_outputsz&FluxTextInputStep.intermediate_outputs@   sm      C
 ++^
 ,,3P	 &,,3W	#
 	
r   c                    |j                   {|j                  n|j                   j                  d   |j                  j                  d   k7  r:t        d|j                   j                   d|j                  j                   d      y y y )Nr   zx`prompt_embeds` and `pooled_prompt_embeds` must have the same batch size when passed directly, but got: `prompt_embeds` z != `pooled_prompt_embeds` .)r   r#   shape
ValueError)r   
componentsblock_states      r   check_inputszFluxTextInputStep.check_inputs\   s    $$0[5U5U5a((..q1[5U5U5[5[\]5^^ --8-F-F-L-L,M N#88>>?qB  _ 6b0r   r0   statec                    | j                  |      }| j                  ||       |j                  j                  d   |_        |j                  j
                  |_        |j                  j                  \  }}}|j                  j                  d|j                  d      |_        |j                  j                  |j                  |j                  z  |d      |_        |j                  j                  d|j                        }|j                  |j                  |j                  z  d      |_	        | j                  ||       ||fS )Nr   r   )get_block_stater2   r   r.   r(   r)   repeatr   viewr#   set_block_state)r   r0   r3   r1   _seq_lenr#   s          r   __call__zFluxTextInputStep.__call__e   s%    **51*k2!,!:!:!@!@!C'55;;#11777A$/$=$=$D$DQHiHikl$m!$/$=$=$B$B""[%F%FFQS%
!  +??FFq+JkJkl+?+D+D""[%F%FF,
( 	UK05  r   N)__name__
__module____qualname__
model_namepropertystrr   r   r
   r&   r+   r2   r$   no_gradr   r	   r<   r   r   r   r   r       s    J
S 
 
 
Z( 
 
& 
d3i 
 
6 U]]_!#6 !} !Q^ ! !r   r   c                        e Zd ZdZdgg fdee   dee   f fdZedefd       Zedee	   fd       Z
edee   fd	       Zd
ededefdZ xZS )FluxInputsDynamicStepr   image_latentsimage_latent_inputsadditional_batch_inputsc                     t        |t              s|g}t        |t              s|g}|| _        || _        t        |           y )N)
isinstancelist_image_latent_inputs_additional_batch_inputssuper__init__)r   rG   rH   	__class__s      r   rO   zFluxInputsDynamicStep.__init__   sH    
 -t4#6"7148'>&?#$7!(?%r   r   c                     d}d}| j                   s| j                  r>d}| j                   r|d| j                    z  }| j                  r|d| j                   z  }d}||z   |z   S )NzInput processing step that:
  1. For image latent inputs: Updates height/width if None, patchifies latents, and expands batch size
  2. For additional batch inputs: Expands batch dimensions to match final batch size z

Configured inputs:z
  - Image latent inputs: z
  - Additional batch inputs: zN

This block should be placed after the encoder steps and the text input step.)rL   rM   )r   summary_sectioninputs_infoplacement_sections       r   r   z!FluxInputsDynamicStep.description   s    c 	 $$(E(E2K((!<T=V=V<WXX,,!@A^A^@_`` o,/@@@r   c                    t        dd      t        dd      t        d      t        d	      g}| j                  D ]  }|j                  t        |              | j                  D ]  }|j                  t        |              |S )
Nr   r   )namer   r(   T)rW   r    heightrW   width)r
   rL   appendrM   )r   r&   image_latent_input_name
input_names       r   r&   zFluxInputsDynamicStep.inputs   s     3Q?L48H%G$	
 (,'@'@ 	D#MM**ABC	D 77 	7JMM**56	7 r   c                 J    t        dt        d      t        dt        d      gS )Nimage_heightzThe height of the image latentsrW   r"   r   image_widthzThe width of the image latentsr   r*   r   s    r   r+   z*FluxInputsDynamicStep.intermediate_outputs   s(     ^sHij]cGgh
 	
r   r0   r3   c                    | j                  |      }| j                  D ]  }t        ||      }|t        ||j                        \  }}|j
                  xs ||_        |j                  xs ||_        t        |d      s||_        t        |d      s||_	        |j                  dd  \  }}	t        j                  ||j                  |j                  d   ||	      }t        |||j                  |j                        }t!        |||        | j"                  D ]A  }
t        ||
      }|t        |
||j                  |j                        }t!        ||
|       C | j%                  ||       ||fS Nr_   ra   r   r   )r]   input_tensorr   r(   )r6   rL   getattrr   vae_scale_factorrX   rZ   hasattrr_   ra   r.   r   _pack_latentsr(   r   r   setattrrM   r9   r   r0   r3   r1   r\   image_latent_tensorrX   rZ   latent_heightlatent_widthr]   re   s               r   r<   zFluxInputsDynamicStep.__call__   s   **51 (,'@'@ 	O#")+7N"O"* ==PR\RmRmnMFE!,!3!3!=vK + 1 1 :UK;7+1(;6*/' +>*C*CAB*G'M<"."<"<#[%;%;=P=V=VWX=Y[hjv#
 #>20&1&G&G&11	# K!8:MN=	OB 77 	;J";
;L# 7%)&1&G&G&11	L K\:	; 	UK05  r   )r=   r>   r?   r@   r   rB   rO   rA   r   r
   r&   r   r+   r   r	   r<   __classcell__)rP   s   @r   rE   rE   }   s    J +:):-/!#Y "&c AS A A, Z(  $ 
d;&7 
 
5!#6 5!} 5!Q^ 5!r   rE   c                   $    e Zd ZdZdededefdZy)FluxKontextInputsDynamicStepflux-kontextr0   r3   r   c                    | j                  |      }| j                  D ]  }t        ||      }|t        ||j                        \  }}t        |d      s||_        t        |d      s||_        |j                  dd  \  }}	t        j                  ||j                  |j                  d   ||	      }t        |||j                  |j                        }t        |||        | j                  D ]A  }
t        ||
      }|t        |
||j                  |j                        }t        ||
|       C | j!                  ||       ||fS rd   )r6   rL   rf   r   rg   rh   r_   ra   r.   r   ri   r(   r   r   rj   rM   r9   rk   s               r   r<   z%FluxKontextInputsDynamicStep.__call__   s{   **51 (,'@'@ 	O#")+7N"O"* ==PR\RmRmnMFE;7+1(;6*/' +>*C*CAB*G'M<"."<"<#[%;%;=P=V=VWX=Y[hjv#
 #>20&1&G&G&11	# K!8:MN9	O> 77 	;J";
;L# 7%)&1&G&G&11	L K\:	; 	UK05  r   N)r=   r>   r?   r@   r   r	   r<   r   r   r   rq   rq      s#    J3!#6 3!} 3!Q^ 3!r   rq   c                   r    e Zd ZdZd Zedee   fd       Zedee	   fd       Z
ed        Zdededefd	Zy
)FluxKontextSetResolutionSteprr   c                      	 y)NzDetermines the height and width to be used during the subsequent computations.
It should always be placed _before_ the latent preparation step.r   r   s    r   r   z(FluxKontextSetResolutionStep.description3  s    O	
r   r   c                 X    t        d      t        d      t        dt        d      g}|S )NrX   rY   rZ   max_areai   )rW   r"   r   )r
   r*   )r   r&   s     r   r&   z#FluxKontextSetResolutionStep.inputs9  s.     H%G$J#wG

 r   c                 J    t        dt        d      t        dt        d      gS )NrX   z'The height of the initial noisy latentsr`   rZ   z&The width of the initial noisy latentsrb   r   s    r   r+   z1FluxKontextSetResolutionStep.intermediate_outputsB  s(     XBklWAij
 	
r   c                     | | |dz  z  dk7  rt        d|dz   d|        | ||dz  z  dk7  rt        d|dz   d|       y y )Nr   r   zHeight must be divisible by z but is zWidth must be divisible by )r/   )rX   rZ   rg   s      r   r2   z)FluxKontextSetResolutionStep.check_inputsI  s    &,<q,@"AQ"F;<Lq<P;QQYZ`Yabcc*:Q*>!?1!D:;Ka;O:PPXY^X_`aa "Er   r0   r3   c                    | j                  |      }|j                  xs |j                  }|j                  xs |j                  }| j                  |||j                         ||}}|j                  }||z  }	t        ||	z  dz        }t        ||	z  dz        }|j                  dz  }
||
z  |
z  }||
z  |
z  }||k7  s||k7  rt        j                  d| d| d       ||_        ||_        | j                  ||       ||fS )Ng      ?r   z6Generation `height` and `width` have been adjusted to z and z to fit the model requirements.)r6   rX   default_heightrZ   default_widthr2   rg   rx   roundloggerwarningr9   )r   r0   r3   r1   rX   rZ   original_heightoriginal_widthrx   aspect_ratiomultiple_ofs              r   r<   z%FluxKontextSetResolutionStep.__call__Q  s'   **51##@z'@'@!!=Z%=%=&%)D)DE*0%''v~x,.367</C78 11A5${2;&4_$(?NNHPUV[U\\{| $!UK05  r   N)r=   r>   r?   r@   r   rA   r   r
   r&   r   r+   staticmethodr2   r   r	   r<   r   r   r   ru   ru   0  s~    J
 Z(   
d;&7 
 
 b b!#6 !} !Q^ !r   ru   )typingr   r$   	pipelinesr   utilsr   modular_pipeliner   r	   modular_pipeline_utilsr
   r   qwenimage.inputsr   r   r   
get_loggerr=   r   r   rE   rq   ru   r   r   r   <module>r      sq      %  C < ] 1 
		H	%Y!- Y!zw!1 w!t6!#8 6!r;!#8 ;!r   