
    
3j                     F   S SK r S SKJrJr  S SKrS SKrS SKrS SKJ	r	J
r
  SSKJr  SSKJrJr  SSKJr  SSKJrJrJr  SS	KJr  S
SKJr  SSKJr  SSKJr  SSKJrJ r J!r!  \" 5       (       a  S SK"J#s  J$r%  Sr&OSr&\RN                  " \(5      r)Sr*Sr+\S4S\,\-   S\-S\,\R\                  R\                  \,\,\R\                  R\                        4   S-  4S jjr/S\,\,\R\                  R\                        \,\R\                  R\                     -  S\S\0S\,\,\R\                  R\                        4S jr1S\0S\0S\24S jr3    S*S\0S-  S \-\Rh                  -  S-  S!\,\0   S-  S"\,\2   S-  4S# jjr5 S+S$\Rl                  S%\Rn                  S-  S&\-4S' jjr8 " S( S)\\5      r9g),    N)AnyCallable)AutoProcessor Mistral3ForConditionalGeneration   )Flux2LoraLoaderMixin)AutoencoderKLFlux2Flux2Transformer2DModel)FlowMatchEulerDiscreteScheduler)is_torch_xla_availableloggingreplace_example_docstring)randn_tensor   )DiffusionPipeline   )Flux2ImageProcessor)Flux2PipelineOutput)SYSTEM_MESSAGESYSTEM_MESSAGE_UPSAMPLING_I2ISYSTEM_MESSAGE_UPSAMPLING_T2ITFaU  
    Examples:
        ```py
        >>> import torch
        >>> from diffusers import Flux2Pipeline

        >>> pipe = Flux2Pipeline.from_pretrained("black-forest-labs/FLUX.2-dev", torch_dtype=torch.bfloat16)
        >>> pipe.to("cuda")
        >>> prompt = "A cat holding a sign that says hello world"
        >>> # Depending on the variant being used, the pipeline call will slightly vary.
        >>> # Refer to the pipeline documentation for more details.
        >>> image = pipe(prompt, num_inference_steps=50, guidance_scale=2.5).images[0]
        >>> image.save("flux.png")
        ```
i  	 promptssystem_messageimagesc           
         U  Vs/ s H  o3R                  SS5      PM     nnUb  [        U5      S:X  a#  U Vs/ s H  nSSUS./S.SSUS./S./PM     sn$ [        U5      [        U 5      :X  d   S	5       eU Vs/ s H  nSSUS./S./PM     nn[        [        Xb5      5       HM  u  nu  pUb(  UR	                  SU V	s/ s H  n	S
U	S.PM
     sn	S.5        UR	                  SSXG   S./S.5        MO     U$ s  snf s  snf s  snf s  sn	f )a  
Format a batch of text prompts into the conversation format expected by apply_chat_template. Optionally, add images
to the input.

Args:
    prompts: List of text prompts
    system_message: System message to use (default: CREATIVE_SYSTEM_MESSAGE)
    images (optional): List of images to add to the input.

Returns:
    List of conversations, where each conversation is a list of message dicts
z[IMG] r   systemtext)typer   )rolecontentuserz-Number of images must match number of promptsimage)r   r#   )replacelen	enumeratezipappend)
r   r   r   promptcleaned_txt_messagesiel	image_objs
             b/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/pipelines/flux2/pipeline_flux2.pyformat_inputr1   A   si   ( >EEW6>>'2.WKE~V) &	
 & %)/ HI  ff-M,NO &	
 		
 6{c'l*[,[[* !
 !	 %)/ HI ! 	 
  )X)>?OA|!		 &[a#b[aiWy$I[a#b II")/ HI  @" U F	

  $cs   C7C<:D?Dimage_processorupsampling_max_image_sizereturnc           
         U (       d  / $ [        U S   [        R                  R                  5      (       a  U  Vs/ s H  o3/PM     n nU  Vs/ s H&  n[        U5      S:  a  UR	                  U5      /OUPM(     n nU  Vs/ s H$  nU Vs/ s H  oAR                  XB5      PM     snPM&     n nU $ s  snf s  snf s  snf s  snf )Nr   r   )
isinstancePILImager%   concatenate_images_resize_if_exceeds_area)r   r2   r3   imimg_is        r0   _validate_and_process_imagesr=      s     	 &)SYY__--!'(2$( ekkdj[`SZ!^11%89QVVdjFk
 E aff`eW\	0	0	R`ef   M ) l 	gs#   B.-B3?
B=	B8
#B=8B=image_seq_len	num_stepsc                     Su  p#Su  pEU S:  a  X@-  U-   n[        U5      $ X@-  U-   nX -  U-   nXx-
  S-  n	USU	-  -
  n
X-  U
-   n[        U5      $ )N)gT	?gŒ_?)g w:/&?gDw:?i  g     g@g      i@)float)r>   r?   a1b1a2b2mum_200m_10abs              r0   compute_empirical_murK      sy    'FB#FBt"$Ry#E"D	A	A	
	B9    num_inference_stepsdevice	timestepssigmasc                    Ub  Ub  [        S5      eUb  S[        [        R                  " U R                  5      R
                  R                  5       5      ;   nU(       d  [        SU R                   S35      eU R                  " S
X2S.UD6  U R                  n[        U5      nX14$ Ub  S[        [        R                  " U R                  5      R
                  R                  5       5      ;   nU(       d  [        SU R                   S35      eU R                  " S
XBS.UD6  U R                  n[        U5      nX14$ U R                  " U4S	U0UD6  U R                  nX14$ )a  
Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.

Args:
    scheduler (`SchedulerMixin`):
        The scheduler to get timesteps from.
    num_inference_steps (`int`):
        The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps`
        must be `None`.
    device (`str` or `torch.device`, *optional*):
        The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
    timesteps (`list[int]`, *optional*):
        Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed,
        `num_inference_steps` and `sigmas` must be `None`.
    sigmas (`list[float]`, *optional*):
        Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed,
        `num_inference_steps` and `timesteps` must be `None`.

Returns:
    `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
    second element is the number of inference steps.
zYOnly one of `timesteps` or `sigmas` can be passed. Please choose one to set custom valuesrO   zThe current scheduler class zx's `set_timesteps` does not support custom timestep schedules. Please check whether you are using the correct scheduler.)rO   rN   rP   zv's `set_timesteps` does not support custom sigmas schedules. Please check whether you are using the correct scheduler.)rP   rN   rN    )

ValueErrorsetinspect	signatureset_timesteps
parameterskeys	__class__rO   r%   )	schedulerrM   rN   rO   rP   kwargsaccepts_timestepsaccept_sigmass           r0   retrieve_timestepsr_      s}   > !3tuu'3w/@/@AXAX/Y/d/d/i/i/k+ll .y/B/B.C Da b  	M)MfM''	!)n )) 
	 C(9(9):Q:Q(R(](](b(b(d$ee.y/B/B.C D_ `  	GvGG''	!)n )) 	 3MFMfM''	))rL   encoder_output	generatorsample_modec                    [        U S5      (       a!  US:X  a  U R                  R                  U5      $ [        U S5      (       a   US:X  a  U R                  R                  5       $ [        U S5      (       a  U R                  $ [        S5      e)Nlatent_distsampleargmaxlatentsz3Could not access latents of provided encoder_output)hasattrrd   re   moderg   AttributeError)r`   ra   rb   s      r0   retrieve_latentsrk      s}     ~}--+2I))00;;		/	/K84K))..00		+	+%%%RSSrL   c            )       Z  ^  \ rS rSrSrSrSS/rS\S\S\	S	\
S
\4
U 4S jjr\SSS\S4S\	S	\
S\\\   -  S\R$                  S-  S\R&                  S-  S\S\S\\   4S jj5       r\ SFS\R,                  S\R,                  S-  4S jj5       r\S\R,                  4S j5       r\ SGS\\R,                     S\4S jj5       r\S 5       r\S 5       r\S 5       r\S\R,                  S \R,                  S!\\R,                     4S" j5       r   SHS\\\   -  S#\\R>                  R>                  \\\R>                  R>                        4   S$\ S\R&                  S!\\   4
S% jjr!     SIS\\\   -  S\R&                  S-  S'\S\R,                  S-  S\S(\"\   4S) jjr#S*\R,                  S+\RH                  4S, jr% SFS+\RH                  S\R,                  S-  4S- jjr&S#\\R,                     S+\RH                  4S. jr'  SJS/ jr(\)S0 5       r*\)S1 5       r+\)S2 5       r,\)S3 5       r-\)S4 5       r.\R^                  " 5       \0" \15      SSSSS5SS6S&SSSS7S8SSS/SSS4S*\R>                  R>                  \\R>                  R>                     -  S-  S\\\   -  S9\S-  S:\S-  S;\S<\\    S-  S=\ S-  S'\S+\RH                  \\RH                     -  S-  S\R,                  S-  S\R,                  S-  S>\S-  S?\2S@\3\\44   S-  SA\5\\/S4   S-  SB\\   S\S(\"\   SC\ 4&SD jj5       5       r6SEr7U =r8$ )KFlux2Pipeline   a  
The Flux2 pipeline for text-to-image generation.

Reference: [https://bfl.ai/blog/flux-2](https://bfl.ai/blog/flux-2)

Args:
    transformer ([`Flux2Transformer2DModel`]):
        Conditional Transformer (MMDiT) architecture to denoise the encoded image latents.
    scheduler ([`FlowMatchEulerDiscreteScheduler`]):
        A scheduler to be used in combination with `transformer` to denoise the encoded image latents.
    vae ([`AutoencoderKLFlux2`]):
        Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.
    text_encoder ([`Mistral3ForConditionalGeneration`]):
        [Mistral3ForConditionalGeneration](https://huggingface.co/docs/transformers/en/model_doc/mistral3#transformers.Mistral3ForConditionalGeneration)
    tokenizer (`AutoProcessor`):
        Tokenizer of class
        [PixtralProcessor](https://huggingface.co/docs/transformers/en/model_doc/pixtral#transformers.PixtralProcessor).
ztext_encoder->transformer->vaerg   prompt_embedsr[   vaetext_encoder	tokenizertransformerc                   > [         TU ]  5         U R                  UUUUUS9  [        U SS 5      (       a/  S[	        U R
                  R                  R                  5      S-
  -  OSU l        [        U R                  S-  S9U l
        SU l        SU l        [        U l        [        U l        ["        U l        [&        U l        g )	N)rp   rq   rr   r[   rs   rp   r   r      )vae_scale_factor      )super__init__register_modulesgetattrr%   rp   configblock_out_channelsrv   r   r2   tokenizer_max_lengthdefault_sample_sizer   r   r   system_message_upsampling_t2ir   system_message_upsampling_i2iUPSAMPLING_MAX_IMAGE_SIZEr3   )selfr[   rp   rq   rr   rs   rZ   s         r0   rz   Flux2Pipeline.__init__  s     	%# 	 	
 W^^bdikoVpVpc$((//*L*L&MPQ&Q Rvw  3DDYDY\]D]^$'!#& ,-J*-J*)B&rL   Nrw   
         r)   dtyperN   max_sequence_lengthr   hidden_states_layersc                    Uc  U R                   OUnUc  U R                  OUn[        U[        5      (       a  U/OUn[	        X&S9nUR                  USSSSSSUS9n	U	S   R                  U5      n
U	S   R                  U5      nU " U
USSS	9n[        R                  " U Vs/ s H  oR                  U   PM     snS
S9nUR                  X4S9nUR                  u  nnnnUR                  SSS
S5      R                  UUUU-  5      nU$ s  snf )N)r   r   FTpt
max_lengthadd_generation_prompttokenizereturn_dictreturn_tensorspadding
truncationr   	input_idsattention_mask)r   r   output_hidden_states	use_cacher   dim)r   rN   r   r   r   )r   rN   r6   strr1   apply_chat_templatetotorchstackhidden_statesshapepermutereshape)rq   rr   r)   r   rN   r   r   r   messages_batchinputsr   r   outputkout
batch_sizenum_channelsseq_len
hidden_dimro   s                       r0   "_get_mistral_3_small_prompt_embeds0Flux2Pipeline._get_mistral_3_small_prompt_embeds/  s@    ',m""(.$$F'44&& &fT .."' * / 	
 ;'**62	 0144V< )!%	
 kk<PQ<Pq//2<PQWXYff5f08;		5
L':Aq!Q/77
G\\fMfg Rs   !D	xt_coordc                    U R                   u  p#n/ n[        U5       H  nUc  [        R                  " S5      OX   n[        R                  " S5      n[        R                  " S5      n	[        R                  " U5      n
[        R                  " XxX5      nUR                  U5        M     [        R                  " U5      $ )Nr   )r   ranger   arangecartesian_prodr(   r   )r   r   BLr+   out_idsr-   thwlcoordss               r0   _prepare_text_idsFlux2Pipeline._prepare_text_idsc  s    
 ''aqA#*?Q
AQAQAQA))!5FNN6"  {{7##rL   c                 F   U R                   u  pp4[        R                  " S5      n[        R                  " U5      n[        R                  " U5      n[        R                  " S5      n[        R                  " XVXx5      n	U	R	                  S5      R                  USS5      n	U	$ )a:  
Generates 4D position coordinates (T, H, W, L) for latent tensors.

Args:
    latents (torch.Tensor):
        Latent tensor of shape (B, C, H, W)

Returns:
    torch.Tensor:
        Position IDs tensor of shape (B, H*W, 4) All batches share the same coordinate structure: T=0,
        H=[0..H-1], W=[0..W-1], L=0
r   r   )r   r   r   r   	unsqueezeexpand)
rg   r   r+   heightwidthr   r   r   r   
latent_idss
             r0   _prepare_latent_ids!Flux2Pipeline._prepare_latent_idsv  s    " (/}}$
vLLOLL LLLLO ))!5
  ))!,33JBG
rL   image_latentsscalec           	         [        U [        5      (       d  [        S[        U 5       S35      e[        R
                  " S[        U 5      5       Vs/ s H	  o!X-  -   PM     nnU Vs/ s H  o"R                  S5      PM     nn/ n[        X5       H  u  pRUR                  S5      nUR                  u  pgn[        R                  " U[        R
                  " U5      [        R
                  " U5      [        R
                  " S5      5      n	UR                  U	5        M     [        R                  " USS9nUR                  S5      nU$ s  snf s  snf )a  
Generates 4D time-space coordinates (T, H, W, L) for a sequence of image latents.

This function creates a unique coordinate for every pixel/patch across all input latent with different
dimensions.

Args:
    image_latents (list[torch.Tensor]):
        A list of image latent feature tensors, typically of shape (C, H, W).
    scale (int, optional):
        A factor used to define the time separation (T-coordinate) between latents. T-coordinate for the i-th
        latent is: 'scale + scale * i'. Defaults to 10.

Returns:
    torch.Tensor:
        The combined coordinate tensor. Shape: (1, N_total, 4) Where N_total is the sum of (H * W) for all
        input latents.

Coordinate Components (Dimension 4):
    - T (Time): The unique index indicating which latent image the coordinate belongs to.
    - H (Height): The row index within that latent image.
    - W (Width): The column index within that latent image.
    - L (Seq. Length): A sequence length dimension, which is always fixed at 0 (size 1)
z+Expected `image_latents` to be a list, got .r   r   r   r   )r6   listrS   r   r   r   r%   viewr'   squeezer   r   r(   catr   )
r   r   r   t_coordsimage_latent_idsr   r+   r   r   x_idss
             r0   _prepare_image_ids Flux2Pipeline._prepare_image_ids  s   < -..J4P]K^J__`abb 05||As=?Q/RS/R!EI%/RS(011FF2J10DA		!A wwAu((ELL,@%,,uBUW\WcWcdeWfgE##E* 1 !99%51=+55a8 T1s   E&Ec                     U R                   u  pp4U R                  XUS-  SUS-  S5      n U R                  SSSSSS5      n U R                  XS-  US-  US-  5      n U $ )Nr   r   r   r         )r   r   r   r   rg   r   num_channels_latentsr   r   s        r0   _patchify_latentsFlux2Pipeline._patchify_latents  sr    :A--7
&,,z1aQVZ[Q[]^_//!Q1a3//*Q.FRSUZ^_U_`rL   c                     U R                   u  pp4U R                  XS-  SSX45      n U R                  SSSSSS5      n U R                  XS-  US-  US-  5      n U $ )Nr   r   r   r   r   r   r   r   r   r   s        r0   _unpatchify_latents!Flux2Pipeline._unpatchify_latents  sk    :A--7
&//*u.MqRSU[c//!Q1a3//*u.MvXYz[`cd[derL   c                 n    U R                   u  pp4U R                  XX4-  5      R                  SSS5      n U $ )zg
pack latents: (batch_size, num_channels, height, width) -> (batch_size, height * width, num_channels)
r   r   r   r   )rg   r   r   r   r   s        r0   _pack_latentsFlux2Pipeline._pack_latents  s;     3:--/
&//*FNKSSTUWXZ[\rL   r   r4   c                    / n[        X5       GH-  u  p4UR                  u  pVUSS2S4   R                  [        R                  5      nUSS2S4   R                  [        R                  5      n[        R
                  " U5      S-   n	[        R
                  " U5      S-   n
Xz-  U-   n[        R                  " X-  U4UR                  UR                  S9nUR                  SUR                  S5      R                  SU5      U5        UR                  XU5      R                  SSS5      nUR                  U5        GM0     [        R                  " USS9$ )z1
using position ids to scatter tokens into place
Nr   r   rN   r   r   r   r   )r'   r   r   r   int64maxzerosrN   r   scatter_r   r   r   r   r(   r   )r   r   x_listdataposr+   chh_idsw_idsr   r   flat_idsr   s                r0   _unpack_latents_with_ids&Flux2Pipeline._unpack_latents_with_ids  s   
 QIDJJEA1ILL-E1ILL-E		% 1$A		% 1$Ay5(H++qubk$++TZZPCLLH..q188R@$G ((1$,,Q15CMM#! '$ {{6q))rL   r   temperaturec                    [        U[        5      (       a  U/OUnUc  U R                  R                  OUnUb  [	        U5      S:X  d  US   c  [
        nO[        nU(       a   [        X R                  U R                  5      n[        XUS9nU R                  R                  USSSSSSSS9nUS   R                  U5      US'   US	   R                  U5      US	'   S
U;   a+  US
   R                  X@R                  R                  5      US
'   U R                  R                  " S0 UDSSUSS.D6nUS   R                   S   n	US S 2U	S 24   n
U R                  R                  R#                  U
SSS9nU$ )Nr   )r   r   r   Tr   r   i   r   r   r   pixel_valuesrw   )max_new_tokens	do_sampler   r   r   )skip_special_tokensclean_up_tokenization_spacesrR   )r6   r   rq   rN   r%   r   r   r=   r2   r3   r1   rr   r   r   r   generater   batch_decode)r   r)   r   r   rN   r   r   r   generated_idsinput_lengthgenerated_tokensupsampled_prompts               r0   upsample_promptFlux2Pipeline.upsample_prompt  s    (44&&-3^"")) >S[A-1B:N:N 1&:N:NPTPnPnoF &f\bc 33"&  4 	
 %[144V<{#)*:#;#>#>v#F V#%+N%;%>%>vGXGXG^G^%_F>" ))22 

#
 k*003(LM)9:>>33@@$UY A 
  rL   r   num_images_per_prompttext_encoder_out_layersc           
         U=(       d    U R                   nUc  Sn[        U[        5      (       a  U/OUnUc3  U R                  U R                  U R
                  UUUU R                  US9nUR                  u  pxn	UR                  SUS5      nUR                  Xs-  US5      nU R                  U5      n
U
R                  U5      n
XJ4$ )Nr   )rq   rr   r)   rN   r   r   r   r   r   )_execution_devicer6   r   r   rq   rr   r   r   repeatr   r   r   )r   r)   rN   r  ro   r   r  r   r   r+   text_idss              r0   encode_promptFlux2Pipeline.encode_prompt;  s     1411>F'44&&  CC!....$7#22%< D M "/!4!4
Q%,,Q0EqI%**:+MwXZ[))-8;;v&&&rL   r#   ra   c                    UR                   S:w  a  [        SUR                    S35      e[        U R                  R	                  U5      USS9nU R                  U5      nU R                  R                  R                  R                  SSSS5      R                  UR                  UR                  5      n[        R                  " U R                  R                  R                  R                  SSSS5      U R                  R                  R                   -   5      R                  UR                  UR                  5      nX4-
  U-  nU$ )Nr   zExpected image dims 4, got r   rf   )ra   rb   r   r   )ndimrS   rk   rp   encoder   bnrunning_meanr   r   rN   r   r   sqrtrunning_varr}   batch_norm_eps)r   r#   ra   r   latents_bn_meanlatents_bn_stds         r0   _encode_vae_imageFlux2Pipeline._encode_vae_image^  s   ::?:5::,aHII()?9bjk..}=((++22772q!DGGH\H\^k^q^qrDHHKK$;$;$@$@B1$MPTPXPXP_P_PnPn$norr  -"5"5
 '8NJrL   c	                    S[        U5      U R                  S-  -  -  nS[        U5      U R                  S-  -  -  nXS-  US-  US-  4n	[        U[        5      (       a*  [	        U5      U:w  a  [        S[	        U5       SU S35      eUc  [        XXeS9nOUR                  XeS9nU R                  U5      n
U
R                  U5      n
U R                  U5      nX4$ )Nr   r   z/You have passed a list of generators of length z+, but requested an effective batch size of z@. Make sure the batch size matches the length of the generators.)ra   rN   r   r   )
intrv   r6   r   r%   rS   r   r   r   r   )r   r   num_latents_channelsr   r   r   rN   ra   rg   r   r   s              r0   prepare_latentsFlux2Pipeline.prepare_latentsm  s     c&kd&;&;a&?@ASZD$9$9A$=>?A5v{EQJOi&&3y>Z+GA#i.AQ R&<'gi  ?"5fZGjjj<G--g6
]]6*
$$W-""rL   c                    / nU H2  nUR                  XES9nU R                  XsS9nUR                  U5        M4     U R                  U5      n	/ n
U H6  nU R	                  U5      nUR                  S5      nU
R                  U5        M8     [        R                  " U
SS9nUR                  S5      nUR                  USS5      nU	R                  USS5      n	U	R                  U5      n	Xi4$ )Nr   )r#   ra   r   r   r   )
r   r  r(   r   r   r   r   r   r   r  )r   r   r   ra   rN   r   r   r#   imagge_latentr   packed_latentslatentpackeds                r0   prepare_image_latents#Flux2Pipeline.prepare_image_latents  s     EHHFH8E 222TM  / 
  22=A #F''/F^^A&F!!&)	 $ 		.a8%//2%,,ZA>+22:q!D+..v6..rL   c           
      ^  ^  Ub  UT R                   S-  -  S:w  d  UbB  UT R                   S-  -  S:w  a,  [        R                  ST R                   S-   SU SU S35        UbW  [        U 4S jU 5       5      (       d=  [	        ST R
                   S	U Vs/ s H  ofT R
                  ;  d  M  UPM     sn 35      eUb  Ub  [	        S
U SU S35      eUc  Uc  [	        S5      eUbC  [        U[        5      (       d-  [        U[        5      (       d  [	        S[        U5       35      eg g g s  snf )Nr   r   z-`height` and `width` have to be divisible by z	 but are z and z(. Dimensions will be resized accordinglyc              3   @   >#    U  H  oTR                   ;   v   M     g 7fN)_callback_tensor_inputs).0r   r   s     r0   	<genexpr>-Flux2Pipeline.check_inputs.<locals>.<genexpr>  s      F
7Y!---7Ys   z2`callback_on_step_end_tensor_inputs` has to be in z, but found zCannot forward both `prompt`: z and `prompt_embeds`: z2. Please make sure to only forward one of the two.zeProvide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined.z2`prompt` has to be of type `str` or `list` but is )
rv   loggerwarningallrS   r&  r6   r   r   r   )r   r)   r   r   ro   "callback_on_step_end_tensor_inputsr   s   `      r0   check_inputsFlux2Pipeline.check_inputs  s    $//!349 ..23q8NN?@U@UXY@Y?ZZcdjckkpqvpw  x`  a .9# F
7YF
 C
 C
 DTEaEaDbbn  |^  pH  |^vw  ko  kG  kG  bGpq  |^  pH  oI  J  -";08N}o ^0 0  ^ 5w  FC)@)@TZ\`IaIaQRVW]R^Q_`aa Jb)@ pHs   D*+D*c                     U R                   $ r%  )_guidance_scaler   s    r0   guidance_scaleFlux2Pipeline.guidance_scale  s    ###rL   c                     U R                   $ r%  )_attention_kwargsr2  s    r0   attention_kwargsFlux2Pipeline.attention_kwargs      %%%rL   c                     U R                   $ r%  )_num_timestepsr2  s    r0   num_timestepsFlux2Pipeline.num_timesteps  s    """rL   c                     U R                   $ r%  )_current_timestepr2  s    r0   current_timestepFlux2Pipeline.current_timestep  r9  rL   c                     U R                   $ r%  )
_interruptr2  s    r0   	interruptFlux2Pipeline.interrupt  s    rL   2   g      @pilTr   r   rM   rP   r3  output_typer   r7  callback_on_step_endr-  caption_upsample_temperaturec                 >   U R                  UUUUUS9  Xpl        Xl        SU l        SU l        Ub  [        U[        5      (       a  SnO3Ub!  [        U[        5      (       a  [        U5      nOUR                  S   nU R                  nU(       a  U R                  X!UUS9nU R                  UUUUUUS9u  nnUb  [        U[        5      (       d  U/nSnUb  U H  nU R                  R                  U5        M      / nU H  nUR                  u  nnUU-  S:  a+  U R                  R!                  US5      nUR                  u  nnU R"                  S	-  nUU-  U-  nUU-  U-  nU R                  R%                  UUUS
S9nUR'                  U5        U=(       d    UnU=(       d    UnM     U=(       d    U R(                  U R"                  -  nU=(       d    U R(                  U R"                  -  nU R*                  R,                  R.                  S-  nU R1                  UU-  UUUUR2                  UU	U
S9u  n
nSnSnUb-  U R5                  UUU-  U	UU R6                  R2                  S9u  nnUc  [8        R:                  " SSU-  U5      OUn[=        U R>                  R,                  S5      (       a'  U R>                  R,                  R@                  (       a  SnU
R                  S   n [C        U US9n![E        U R>                  UUUU!S9u  n"n[G        [        U"5      XPR>                  RH                  -  -
  S5      n#[        U"5      U l%        [L        RN                  " S/UU[L        RP                  S9n$U$RS                  U
R                  S   5      n$U R>                  RU                  S5        U RW                  US9 n%[Y        U"5       GHG  u  n&n'U RZ                  (       a  M  U'U l        U'RS                  U
R                  S   5      R]                  U
R2                  5      n(U
R]                  U R*                  R2                  5      n)Un*UbQ  [L        R^                  " U
U/SS9R]                  U R*                  R2                  5      n)[L        R^                  " UU/SS9n*U R+                  U)U(S-  U$UUU*U R`                  SS9S   n+U+SS2SU
R                  S5      24   n+U
R2                  n,U R>                  Rc                  U+U'U
SS9S   n
U
R2                  U,:w  a>  [L        Rd                  Rf                  Ri                  5       (       a  U
R]                  U,5      n
UbJ  0 n-U H  n.[k        5       U.   U-U.'   M     U" U U&U'U-5      n/U/Rm                  SU
5      n
U/Rm                  SU5      nU&[        U"5      S-
  :X  d)  U&S-   U#:  a0  U&S-   U R>                  RH                  -  S:X  a  U%Ro                  5         [p        (       d  GM2  [r        Rt                  " 5         GMJ     SSS5        SU l        US:X  a  U
nGOBU Rw                  U
U5      n
U R6                  Rx                  Rz                  R}                  SSSS5      R]                  U
R~                  U
R2                  5      n0[L        R                  " U R6                  Rx                  R                  R}                  SSSS5      U R6                  R,                  R                  -   5      R]                  U
R~                  U
R2                  5      n1U
U1-  U0-   n
U R                  U
5      n
U R6                  R                  U
SS9S   nU R                  R                  XS9nU R                  5         U(       d  U4$ [        US9$ ! , (       d  f       GN= f)a  
Function invoked when calling the pipeline for generation.

Args:
    image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `list[torch.Tensor]`, `list[PIL.Image.Image]`, or `list[np.ndarray]`):
        `Image`, numpy array or tensor representing an image batch to be used as the starting point. For both
        numpy array and pytorch tensor, the expected value range is between `[0, 1]` If it's a tensor or a list
        or tensors, the expected shape should be `(B, C, H, W)` or `(C, H, W)`. If it is a numpy array or a
        list of arrays, the expected shape should be `(B, H, W, C)` or `(H, W, C)` It can also accept image
        latents as `image`, but if passing latents directly it is not encoded again.
    prompt (`str` or `list[str]`, *optional*):
        The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`.
        instead.
    guidance_scale (`float`, *optional*, defaults to 1.0):
        Embedded guiddance scale is enabled by setting `guidance_scale` > 1. Higher `guidance_scale` encourages
        a model to generate images more aligned with `prompt` at the expense of lower image quality.

        Guidance-distilled models approximates true classifer-free guidance for `guidance_scale` > 1. Refer to
        the [paper](https://huggingface.co/papers/2210.03142) to learn more.
    height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
        The height in pixels of the generated image. This is set to 1024 by default for the best results.
    width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
        The width in pixels of the generated image. This is set to 1024 by default for the best results.
    num_inference_steps (`int`, *optional*, defaults to 50):
        The number of denoising steps. More denoising steps usually lead to a higher quality image at the
        expense of slower inference.
    sigmas (`list[float]`, *optional*):
        Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in
        their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed
        will be used.
    num_images_per_prompt (`int`, *optional*, defaults to 1):
        The number of images to generate per prompt.
    generator (`torch.Generator` or `list[torch.Generator]`, *optional*):
        One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html)
        to make generation deterministic.
    latents (`torch.Tensor`, *optional*):
        Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
        generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
        tensor will be generated by sampling using the supplied random `generator`.
    prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
        provided, text embeddings will be generated from `prompt` input argument.
    output_type (`str`, *optional*, defaults to `"pil"`):
        The output format of the generate image. Choose between
        [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a [`~pipelines.qwenimage.QwenImagePipelineOutput`] instead of a plain tuple.
    attention_kwargs (`dict`, *optional*):
        A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
        `self.processor` in
        [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
    callback_on_step_end (`Callable`, *optional*):
        A function that calls at the end of each denoising steps during the inference. The function is called
        with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
        callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
        `callback_on_step_end_tensor_inputs`.
    callback_on_step_end_tensor_inputs (`List`, *optional*):
        The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
        will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
        `._callback_tensor_inputs` attribute of your pipeline class.
    max_sequence_length (`int` defaults to 512): Maximum sequence length to use with the `prompt`.
    text_encoder_out_layers (`tuple[int]`):
        Layer indices to use in the `text_encoder` to derive the final prompt embeddings.
    caption_upsample_temperature (`float`):
        When specified, we will try to perform caption upsampling for potentially improved outputs. We
        recommend setting it to 0.15 if caption upsampling is to be performed.

Examples:

Returns:
    [`~pipelines.flux2.Flux2PipelineOutput`] or `tuple`: [`~pipelines.flux2.Flux2PipelineOutput`] if
    `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is a list with the
    generated images.
)r)   r   r   ro   r-  NFr   r   )r   r   rN   )r)   ro   rN   r  r   r  i   r   crop)r   r   resize_moder   )r   r  r   r   r   rN   ra   rg   )r   r   ra   rN   r   g      ?use_flow_sigmas)r>   r?   )rP   rF   r   )totalr   i  )r   timestepguidanceencoder_hidden_statestxt_idsimg_idsjoint_attention_kwargsr   )r   rg   ro   r  r   )rH  )r   )Hr.  r1  r6  r?  rC  r6   r   r   r%   r   r  r  r	  r2   check_image_inputsize_resize_to_target_arearv   
preprocessr(   r   rs   r}   in_channelsr  r   r!  rp   nplinspacerh   r[   rN  rK   r_   r   orderr;  r   fullfloat32r   set_begin_indexprogress_barr&   rD  r   r   r7  stepbackendsmpsis_availablelocalspopupdateXLA_AVAILABLExm	mark_stepr   r  r  r   rN   r  r  r  r   decodepostprocessmaybe_free_model_hooksr   )2r   r#   r)   r   r   rM   rP   r3  r  ra   rg   ro   rH  r   r7  rI  r-  r   r  rJ  r   rN   r  condition_imagesimgimage_widthimage_heightmultiple_ofr   r   r   r   r>   rF   rO   num_warmup_stepsrQ  ra  r-   r   rP  latent_model_inputlatent_image_ids
noise_predlatents_dtypecallback_kwargsr   callback_outputsr  r  s2                                                     r0   __call__Flux2Pipeline.__call__  sg   H 	'/Q 	 	
  .!1!% *VS"9"9JJvt$<$<VJ&,,Q/J'' ())2NW] * F #'"4"4'"7 3$; #5 #
x Zt%<%<GE$$66s;   ",/HH)\-;..EEc;WC03-K"33a7*k9[H , ;{J**55c,Vaou5v '',/<,  K433d6K6KKI11D4I4II  $//66BBaG"22!$99!5%% 3 	
 '.2.H.H'%(==#hhnn /I /+M+ TZSaS!&9"9;NOgm4>>((*;<<AVAVAfAfFa(!I\]);NN*
&	& s9~0CnnFZFZ0ZZ\]^!)n ::qc>&V??7==#34
 	&&q)%89\!),1>>)*&88GMM!$4588G%,ZZ0@0@0F0F%G"#-  ,).G]3KQR)S)V)VW[WgWgWmWm)n&',yy*>N1OUV'W$!--"4%_%*7$,+/+@+@ % . 	 	
 (+>W\\!_+>(>?
 !(..--j!WRW-XYZ[==M1~~))6688")**]";'3&(O?-3Xa[* @';D!Q'X$.229gFG$4$8$8-$XM I**A9I/IqSTuX\XfXfXlXlNlpqNq '') =LLNg - :l "&("E33GZHG"hhkk66;;Ar1aHKKGNN\c\i\ijO"ZZ(?(?(D(DQAq(QTXT\T\TcTcTrTr(rsvvN .@G..w7GHHOOGO?BE((44U4TE 	##%8O"%00[ :9s   7H9^5^
^)r6  r?  r1  rC  r;  r   r2   r   r   r   r   r3   rv   r%  )r   )Ng333333?N)Nr   Nrw   r   )NN)9__name__
__module____qualname____firstlineno____doc__model_cpu_offload_seqr&  r   r	   r   r   r
   rz   staticmethodr   r   r   r   r   rN   r  r   Tensorr   r   r   r   r   r   r   r7   r8   rA   r  tupler	  	Generatorr  r  r!  r.  propertyr3  r7  r<  r@  rD  no_gradr   EXAMPLE_DOC_STRINGbooldictr   r   r{  __static_attributes____classcell__)rZ   s   @r0   rm   rm      ss   & =(/:C2C  C 7	C
 !C -C: 
 %)&*#&,*6161 1 d3i1 {{T!	1
 t#1 !1 1 #3i1 1f  (,$<<$$$ $$  >  / ELL)/ /  / b       *ELL * *$u||J\ * *8 FJ!#< d3i<  SYY__d4		+@&AAB<  	< 
 <  
c< B '+%&-1#&.:!'d3i!' t#!'  #	!'
 ||d*!' !!' "'s!'Fu||  0 (,# ??# $#B /U\\" / ??	 /N +/#bJ $ $ & & # # & &   ]]_12 AE"&! #%%)'*%&DH'+-1"' 26BF9B#&.:.2)_1yyciioo!66=_1 d3i_1 d
	_1
 Tz_1 !_1 Ud"_1 _1  #_1 ??T%//%::TA_1 $_1 ||d*_1 4Z_1 _1 sCx.4/_1  'Sz4'784?!_1" -1I#_1$ !%_1& "'s'_1( ',)_1 3 _1rL   rm   )NNNN)Nre   ):rU   typingr   r   numpyr[  r7   r   transformersr   r   loadersr   modelsr	   r
   
schedulersr   utilsr   r   r   utils.torch_utilsr   pipeline_utilsr   r2   r   pipeline_outputr   system_messagesr   r   r   torch_xla.core.xla_modelcore	xla_modelrj  ri  
get_loggerr}  r*  r  r   r   r   r8   r1   r  r=   rA   rK   rN   r_   r  r  rk   rm   rR   rL   r0   <module>r     s       
  H + A 9 O O - . 0 0 i i ))MM 
		H	%   #  )HL>#Y>> $tCIIOO'<"==>E>Fciioo&'$syy*??(  # 
$syy
 	6   * '+(,"&!%8*t8* %,,%8* Cy4	8*
 K$8*z `h
TLL
T-2__t-C
TY\
TO1%'; O1rL   