
    ik                        d dl mZ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 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j2                  e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z  G d de      Z! G d de      Z" G d de      Z# G d de#      Z$ G d  d!e#      Z% G d" d#e#      Z& G d$ d%e#      Z' G d& d'e#      Z( G d( d)e#      Z)y)*    )ListTupleN   )
FrozenDict)ClassifierFreeGuidance)QwenImageControlNetModelQwenImageTransformer2DModel)FlowMatchEulerDiscreteScheduler)logging   )
BlockStateLoopSequentialPipelineBlocksModularPipelineBlocksPipelineState)ComponentSpec
InputParamOutputParam   )QwenImageModularPipelinec            	           e Zd ZdZedefd       Zedee   fd       Z	 e
j                         dededede
j                  fd	       Zy
)QwenImageLoopBeforeDenoiser	qwenimagereturnc                      	 yNzstep within the denoising loop that prepares the latent input for the denoiser. This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` object (e.g. `QwenImageDenoiseLoopWrapper`) selfs    w/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/diffusers/modular_pipelines/qwenimage/denoise.pydescriptionz'QwenImageLoopBeforeDenoiser.description#       :	
    c                 >    t        ddt        j                  d      gS )NlatentsT^The initial latents to use for the denoising process. Can be generated in prepare_latent step.required	type_hintr    r   torchTensorr   s    r   inputsz"QwenImageLoopBeforeDenoiser.inputs+   s&     ,,|	
 	
r"   
componentsblock_stateitc                     |j                  |j                  j                  d         j                  |j                  j                        |_        |j                  |_        ||fS )Nr   )expandr$   shapetodtypetimesteplatent_model_inputr   r-   r.   r/   r0   s        r   __call__z$QwenImageLoopBeforeDenoiser.__call__6   sV      !xx(;(;(A(A!(DEHHI\I\IbIbc)4)<)<&;&&r"   N__name__
__module____qualname__
model_namepropertystrr    r   r   r,   r*   no_gradr   r   intr+   r9   r   r"   r   r   r       s    J
S 
 
 
Z( 
 
 U]]_'#; '* 'Y\ 'afamam ' 'r"   r   c            	           e Zd ZdZedefd       Zedee   fd       Z	 e
j                         dededede
j                  fd	       Zy
)QwenImageEditLoopBeforeDenoiserr   r   c                      	 yr   r   r   s    r   r    z+QwenImageEditLoopBeforeDenoiser.descriptionA   r!   r"   c                 v    t        ddt        j                  d      t        ddt        j                  d      gS )Nr$   Tr%   r&   image_latentszThe initial image latents to use for the denoising process. Can be encoded in vae_encoder step and packed in prepare_image_latents step.r)   r   s    r   r,   z&QwenImageEditLoopBeforeDenoiser.inputsI   sC     ,,|	 ,, g	
 	
r"   r-   r.   r/   r0   c                    t        j                  |j                  |j                  gd      |_        |j                  |j                  j                  d         j                  |j                  j                        |_	        ||fS )Nr   )dimr   )
r*   catr$   rG   r7   r2   r3   r4   r5   r6   r8   s        r   r9   z(QwenImageEditLoopBeforeDenoiser.__call__Z   sm     */K4G4GIbIb3cij)k& xx(;(;(A(A!(DEHHI\I\IbIbc;&&r"   Nr:   r   r"   r   rD   rD   >   s    J
S 
 
 
Z( 
 
  U]]_'#; '* 'Y\ 'afamam ' 'r"   rD   c            	           e Zd ZdZedee   fd       Zedefd       Z	edee
   fd       Z ej                         dededed	efd
       Zy)%QwenImageLoopBeforeDenoiserControlNetr   r   c                 ^    t        dt        t        ddi      d      t        dt              gS )Nguiderguidance_scale      @from_configconfigdefault_creation_method
controlnet)r   r   r   r   r   s    r   expected_componentsz9QwenImageLoopBeforeDenoiserControlNet.expected_componentsf   s:     &!#3S"9:(5	 ,(@A
 	
r"   c                      	 y)Nzstep within the denoising loop that runs the controlnet before the denoiser. This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` object (e.g. `QwenImageDenoiseLoopWrapper`)r   r   s    r   r    z1QwenImageLoopBeforeDenoiserControlNet.descriptionr   r!   r"   c           	          t        ddt        j                  d      t        dt        d      t        ddt        t           d	      t        d
dt
        d      t        dd      gS )Ncontrol_image_latentsTzgThe control image to use for the denoising process. Can be generated in prepare_controlnet_inputs step.r&   controlnet_conditioning_scalez}The controlnet conditioning scale value to use for the denoising process. Can be generated in prepare_controlnet_inputs step.r(   r    controlnet_keepzpThe controlnet keep values to use for the denoising process. Can be generated in prepare_controlnet_inputs step.num_inference_stepsgThe number of inference steps to use for the denoising process. Can be generated in set_timesteps step.denoiser_input_fieldszAll conditional model inputs for the denoiser. It should contain prompt_embeds/negative_prompt_embeds, txt_seq_lens/negative_txt_seq_lens.kwargs_typer    )r   r*   r+   floatr   rB   r   s    r   r,   z,QwenImageLoopBeforeDenoiserControlNet.inputsz   s     ',, F	 / \
 !u+ O	 % F	 3r1
 	
r"   r-   r.   r/   r0   c                 6   t        |j                  |   t              r?t        |j                  |j                  |         D cg c]
  \  }}||z   c}}|_        n8|j                  }t        |t              r|d   }||j                  |   z  |_        |j                  |j                  |j                  |j
                  |j                  dz  |j                  |j                  |j                  |j                  d	      }||j                  d<   ||fS c c}}w )Nr     F)	hidden_statescontrolnet_condconditioning_scaler6   
img_shapesencoder_hidden_statesencoder_hidden_states_masktxt_seq_lensreturn_dictcontrolnet_block_samples)
isinstancer\   listziprZ   
cond_scalerU   r7   rY   r6   rh   prompt_embedsprompt_embeds_maskrk   additional_cond_kwargs)	r   r-   r.   r/   r0   cscontrolnet_cond_scalerm   s	            r   r9   z.QwenImageLoopBeforeDenoiserControlNet.__call__   s    k11!4d;"%k&O&OQ\QlQlmnQo"p&!QA&K" %0$M$M!/6(=a(@%%:[=X=XYZ=[%[K" $.#8#8%88'==*55 ))D0"--"-";";'2'E'E$11 $9 
$
  Jb**+EF;&&1&s   DN)r;   r<   r=   r>   r?   r   r   rV   r@   r    r   r,   r*   rA   r   r   rB   r9   r   r"   r   rL   rL   c   s    J	
T-%8 	
 	
 
S 
 
  
Z(  
  
D U]]_'#; '* 'Y\ 'ad ' 'r"   rL   c            	           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 ej                         dededed	ej"                  fd
       Zy)QwenImageLoopDenoiserr   r   c                      	 yNzstep within the denoising loop that denoise the latent input for the denoiser. This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` object (e.g. `QwenImageDenoiseLoopWrapper`)r   r   s    r   r    z!QwenImageLoopDenoiser.description   r!   r"   c                 ^    t        dt        t        ddi      d      t        dt              gS NrN   rO   rP   rQ   rR   transformerr   r   r   r	   r   s    r   rV   z)QwenImageLoopDenoiser.expected_components   :     &!#3S"9:(5	 -)DE
 	
r"   c                     t        d      t        ddt        j                  d      t        ddt        d      t        dd	
      t        ddt        t
        t        t        f      d      gS Nattention_kwargsr$   TzWThe latents to use for the denoising process. Can be generated in prepare_latents step.r&   r]   r^   r_   z[conditional model inputs for the denoiser: e.g. prompt_embeds, negative_prompt_embeds, etc.r`   rh   zhThe shape of the image latents for RoPE calculation. Can be generated in prepare_additional_inputs step.r   r*   r+   rB   r   r   r   s    r   r,   zQwenImageLoopDenoiser.inputs   ~     )*,,u	 % F	 3y uS#X/ G	%
 	
r"   r-   r.   r/   r0   c           
         t        |dd       t        |dd       ft        |dd       t        |dd       ft        |dd       t        |dd       fd}|j                  j                  ||j                  |       |j                  j	                  |      }|D ]  }|j                  j                  |j                         |j                         D ci c]  }|t        ||       }	} |j                  d|j                  |j                  d	z  |j                  |j                  d
d|	|j                  d   |_        |j                  j                  |j                          |j                  |      }
t        j                   |
j"                  dd      }t        j                   |
j$                  dd      }|
j$                  ||z  z  |_        ||fS c c}w )Nrr   negative_prompt_embedsrs   negative_prompt_embeds_maskrk   negative_txt_seq_lensri   rj   rk   stepr]   r6   rd   Fre   r6   rh   r   rl   r   TrI   keepdimr   )getattrrN   	set_stater]   prepare_inputsprepare_modelsr~   keysr7   r6   rh   r   rt   
noise_predcleanup_modelsr*   norm	pred_condpred)r   r-   r.   r/   r0   guider_inputsguider_stateguider_state_batch
input_namecond_kwargsguider_outputpred_cond_norm	pred_norms                r   r9   zQwenImageLoopDenoiser.__call__   s    _d;%=tD&
 %94@%BDI+
 ^T:%<dC
 	##@_@_jk#l!((77F". 	E,,Z-C-CDanasasauvS]:w/A:'NNvKv -CJ,B,B -)<<$--4&11!,!=!=!- - 44- -) ,,Z-C-CD	E" #)),7 M$;$;TRJJ}11r4H	!.!3!3~	7Q!R;&&- ws   GNr;   r<   r=   r>   r?   r@   r    r   r   rV   r   r,   r*   rA   r   r   rB   r+   r9   r   r"   r   ry   ry      s    J
S 
 
 	
T-%8 	
 	
 
Z( 
 
6 U]]_+'#; +'* +'Y\ +'afamam +' +'r"   ry   c            	           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 ej                         dededed	ej"                  fd
       Zy)QwenImageEditLoopDenoiserr   r   c                      	 yr{   r   r   s    r   r    z%QwenImageEditLoopDenoiser.description!  r!   r"   c                 ^    t        dt        t        ddi      d      t        dt              gS r}   r   r   s    r   rV   z-QwenImageEditLoopDenoiser.expected_components)  r   r"   c                     t        d      t        ddt        j                  d      t        ddt        d      t        dd	
      t        ddt        t
        t        t        f      d      gS r   r   r   s    r   r,   z QwenImageEditLoopDenoiser.inputs5  r   r"   r-   r.   r/   r0   c           
      *   t        |dd       t        |dd       ft        |dd       t        |dd       ft        |dd       t        |dd       fd}|j                  j                  ||j                  |       |j                  j	                  |      }|D ]  }|j                  j                  |j                         |j                         D ci c]  }|t        ||       }	} |j                  d|j                  |j                  d	z  |j                  |j                  d
d|	|j                  d   |_        |j                  j                  |j                          |j                  |      }
|
j                  d d d |j                   j#                  d      f   }|
j$                  d d d |j                   j#                  d      f   }t'        j(                  |dd      }t'        j(                  |dd      }|||z  z  |_        ||fS c c}w )Nrr   r   rs   r   rk   r   r   r   rd   Fr   r   r   r   Tr   r   )r   rN   r   r]   r   r   r~   r   r7   r6   rh   r   rt   r   r   r   r$   sizer   r*   r   )r   r-   r.   r/   r0   r   r   r   r   r   r   r   r   r   r   s                  r   r9   z"QwenImageEditLoopDenoiser.__call__Q  s7    _d;%=tD&
 %94@%BDI+
 ^T:%<dC
 	##@_@_jk#l!((77F". 	E,,Z-C-CDanasasauvS]:w/A:'NNvKv -CJ,B,B -)<<$--4&11!,!=!=!- - 44- -) ,,Z-C-CD	E" #)),7!!!%B{':':'?'?'B%B"BC!++A/L1D1D1I1I!1L/L,LM	 I2tDJJtT:	!%))C!D;&&3 ws   HNr   r   r"   r   r   r     s    J
S 
 
 	
T-%8 	
 	
 
Z( 
 
6 U]]_.'#; .'* .'Y\ .'afamam .' .'r"   r   c            	           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 ej                         dededed	ej"                  fd
       Zy)QwenImageLoopAfterDenoiserr   r   c                      	 y)Nzstep within the denoising loop that updates the latents. This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` object (e.g. `QwenImageDenoiseLoopWrapper`)r   r   s    r   r    z&QwenImageLoopAfterDenoiser.description  r!   r"   c                 $    t        dt              gS N	schedulerr   r
   r   s    r   rV   z.QwenImageLoopAfterDenoiser.expected_components       +'FG
 	
r"   c                 <    t        dt        j                  d      gS )Nr$   zThe denoised latents.r[   )r   r*   r+   r   s    r   intermediate_outputsz/QwenImageLoopAfterDenoiser.intermediate_outputs  s     	U\\G^_
 	
r"   r-   r.   r/   r0   c                 n   |j                   j                  }|j                  j                  |j                  ||j                   d      d   |_         |j                   j                  |k7  rHt
        j                  j                  j                         r |j                   j                  |      |_         ||fS )NF)rl   r   )
r$   r5   r   r   r   r*   backendsmpsis_availabler4   )r   r-   r.   r/   r0   latents_dtypes         r   r9   z#QwenImageLoopAfterDenoiser.__call__  s    #++11(2277""	 8 

  $$5~~!!..0&1&9&9&<&<]&K#;&&r"   N)r;   r<   r=   r>   r?   r@   r    r   r   rV   r   r   r*   rA   r   r   rB   r+   r9   r   r"   r   r   r     s    J
S 
 
 
T-%8 
 

 
d;&7 
 

 U]]_'#; '* 'Y\ 'afamam ' 'r"   r   c            	           e Zd ZdZedefd       Zedee   fd       Z	 e
j                         dededede
j                  fd	       Zy
)!QwenImageLoopAfterDenoiserInpaintr   r   c                      	 y)Nzstep within the denoising loop that updates the latents using mask and image_latents for inpainting. This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` object (e.g. `QwenImageDenoiseLoopWrapper`)r   r   s    r   r    z-QwenImageLoopAfterDenoiserInpaint.description  r!   r"   c           	          t        ddt        j                  d      t        ddt        j                  d      t        ddt        j                  d      t        d	dt        j                  d
      gS )NmaskTz]The mask to use for the inpainting process. Can be generated in inpaint prepare latents step.r&   rG   zfThe image latents to use for the inpainting process. Can be generated in inpaint prepare latents step.initial_noisezfThe initial noise to use for the inpainting process. Can be generated in inpaint prepare latents step.	timestepsWThe timesteps to use for the denoising process. Can be generated in set_timesteps step.r)   r   s    r   r,   z(QwenImageLoopAfterDenoiserInpaint.inputs  sz     ,,{	 ,, E	 ,, E	 ,,u	'
 	
r"   r-   r.   r/   r0   c                    |j                   |_        |t        |j                        dz
  k  rk|j                  |dz      |_        |j
                  j                  |j                  t        j                  |j                  g      |j                        |_        d|j                  z
  |j                  z  |j                  |j                  z  z   |_        ||fS )Nr   )rG   init_latents_properlenr   noise_timestepr   scale_noiser*   tensorr   r   r$   r8   s        r   r9   z*QwenImageLoopAfterDenoiserInpaint.__call__  s    *5*C*C's;(()A--)4)>)>q1u)EK&.8.B.B.N.N//{?Y?Y>Z1[]h]v]v/K+
    ++,.9.>.>ATAT.TU ;&&r"   Nr:   r   r"   r   r   r     s    J
S 
 
 
Z( 
 
8 U]]_'#; '* 'Y\ 'afamam ' 'r"   r   c                       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 ej                         dededefd       Zy	)
QwenImageDenoiseLoopWrapperr   r   c                      	 y)NzPipeline block that iteratively denoise the latents over `timesteps`. The specific steps with each iteration can be customized with `sub_blocks` attributesr   r   s    r   r    z'QwenImageDenoiseLoopWrapper.description  s    d	
r"   c                 $    t        dt              gS r   r   r   s    r   loop_expected_componentsz4QwenImageDenoiseLoopWrapper.loop_expected_components  r   r"   c                 b    t        ddt        j                  d      t        ddt        d      gS )Nr   Tr   r&   r]   r^   )r   r*   r+   rB   r   s    r   loop_inputsz'QwenImageDenoiseLoopWrapper.loop_inputs  s?     ,,u	 % F	
 	
r"   r-   statec                 x   | j                  |      }t        t        |j                        |j                  |j
                  j                  z  z
  d      |_        i |_        | j                  |j                        5 }t        |j                        D ]{  \  }}| j                  ||||      \  }}|t        |j                        dz
  k(  s3|dz   |j                  kD  sL|dz   |j
                  j                  z  dk(  sl|j                          } 	 d d d        | j                  ||       ||fS # 1 sw Y   xY w)Nr   )total)r/   r0   r   )get_block_statemaxr   r   r]   r   ordernum_warmup_stepsrt   progress_bar	enumerate	loop_stepupdateset_block_state)r   r-   r   r.   r   r/   r0   s          r   r9   z$QwenImageDenoiseLoopWrapper.__call__  s/   **51'*%%&)H)H:K_K_KeKe)eegh(
$ .0*[%D%DE 	*!+"7"78 *1*...[TUYZ.*['
KK112Q66Uk:::AI]I]IcIc?cgh?h '')*	* 	UK05  	* 	*s   =A!D0D0?D00D9N)r;   r<   r=   r>   r?   r@   r    r   r   r   r   r   r*   rA   r   r   r9   r   r"   r   r   r     s    J
S 
 
 
$}*= 
 

 
T*- 
 
  U]]_!#; !M !Vc ! !r"   r   c                   4    e Zd ZeeegZg dZede	fd       Z
y)QwenImageDenoiseStepbefore_denoiserdenoiserafter_denoiserr   c                      	 y)Nag  Denoise step that iteratively denoise the latents. 
Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method 
At each iteration, it runs blocks defined in `sub_blocks` sequencially:
 - `QwenImageLoopBeforeDenoiser`
 - `QwenImageLoopDenoiser`
 - `QwenImageLoopAfterDenoiser`
This block supports text2image and image2image tasks for QwenImage.r   r   s    r   r    z QwenImageDenoiseStep.description%  s    R	
r"   N)r;   r<   r=   r   ry   r   block_classesblock_namesr?   r@   r    r   r"   r   r   r     s3    #"M
 DK	
S 	
 	
r"   r   c                   6    e Zd ZeeeegZg dZe	de
fd       Zy)QwenImageInpaintDenoiseStepr   r   r   after_denoiser_inpaintr   c                      	 y)Na~  Denoise step that iteratively denoise the latents. 
Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method 
At each iteration, it runs blocks defined in `sub_blocks` sequencially:
 - `QwenImageLoopBeforeDenoiser`
 - `QwenImageLoopDenoiser`
 - `QwenImageLoopAfterDenoiser`
 - `QwenImageLoopAfterDenoiserInpaint`
This block supports inpainting tasks for QwenImage.r   r   s    r   r    z'QwenImageInpaintDenoiseStep.description<  s    B		
r"   N)r;   r<   r=   r   ry   r   r   r   r   r?   r@   r    r   r"   r   r   r   3  s6    #")	M ^K

S 

 

r"   r   c                   6    e Zd ZeeeegZg dZe	de
fd       Zy)QwenImageControlNetDenoiseStep)r   before_denoiser_controlnetr   r   r   c                      	 y)Na  Denoise step that iteratively denoise the latents. 
Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method 
At each iteration, it runs blocks defined in `sub_blocks` sequencially:
 - `QwenImageLoopBeforeDenoiser`
 - `QwenImageLoopBeforeDenoiserControlNet`
 - `QwenImageLoopDenoiser`
 - `QwenImageLoopAfterDenoiser`
This block supports text2img/img2img tasks with controlnet for QwenImage.r   r   s    r   r    z*QwenImageControlNetDenoiseStep.descriptionT  s    X		
r"   N)r;   r<   r=   r   rL   ry   r   r   r   r?   r@   r    r   r"   r   r   r   K  s6    #-"	M bK

S 

 

r"   r   c                   8    e Zd ZeeeeegZg dZ	e
defd       Zy)%QwenImageInpaintControlNetDenoiseStep)r   r   r   r   r   r   c                      	 y)Na  Denoise step that iteratively denoise the latents. 
Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method 
At each iteration, it runs blocks defined in `sub_blocks` sequencially:
 - `QwenImageLoopBeforeDenoiser`
 - `QwenImageLoopBeforeDenoiserControlNet`
 - `QwenImageLoopDenoiser`
 - `QwenImageLoopAfterDenoiser`
 - `QwenImageLoopAfterDenoiserInpaint`
This block supports inpainting tasks with controlnet for QwenImage.r   r   s    r   r    z1QwenImageInpaintControlNetDenoiseStep.descriptions  s    R
	
r"   N)r;   r<   r=   r   rL   ry   r   r   r   r   r?   r@   r    r   r"   r   r   r   c  s:    #-")MK 
S 
 
r"   r   c                   4    e Zd ZeeegZg dZede	fd       Z
y)QwenImageEditDenoiseStepr   r   c                      	 y)NaO  Denoise step that iteratively denoise the latents. 
Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method 
At each iteration, it runs blocks defined in `sub_blocks` sequencially:
 - `QwenImageEditLoopBeforeDenoiser`
 - `QwenImageEditLoopDenoiser`
 - `QwenImageLoopAfterDenoiser`
This block supports QwenImage Edit.r   r   s    r   r    z$QwenImageEditDenoiseStep.description  s    2	
r"   N)r;   r<   r=   rD   r   r   r   r   r?   r@   r    r   r"   r   r   r     s3    '!"M
 DK	
S 	
 	
r"   r   c                   6    e Zd ZeeeegZg dZe	de
fd       Zy)QwenImageEditInpaintDenoiseStepr   r   c                      	 y)Na  Denoise step that iteratively denoise the latents. 
Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method 
At each iteration, it runs blocks defined in `sub_blocks` sequencially:
 - `QwenImageEditLoopBeforeDenoiser`
 - `QwenImageEditLoopDenoiser`
 - `QwenImageLoopAfterDenoiser`
 - `QwenImageLoopAfterDenoiserInpaint`
This block supports inpainting tasks for QwenImage Edit.r   r   s    r   r    z+QwenImageEditInpaintDenoiseStep.description  s    G		
r"   N)r;   r<   r=   rD   r   r   r   r   r   r?   r@   r    r   r"   r   r   r     s6    '!")	M ^K

S 

 

r"   r   )*typingr   r   r*   configuration_utilsr   guidersr   modelsr   r	   
schedulersr
   utilsr   modular_pipeliner   r   r   r   modular_pipeline_utilsr   r   r   r   
get_loggerr;   loggerr   rD   rL   ry   r   r   r   r   r   r   r   r   r   r   r   r"   r   <module>r      s     - - K 9  m m K K 6 
		H	%'"7 '<"'&; "'JV',A V'r_'1 _'Db' 5 b'J&'!6 &'R5'(= 5'p5!"> 5!r
6 
,
"= 
0
%@ 
0
,G 
@
: 
*
&A 
r"   