
    
3j              
          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  \" 5       (       a  S SKJs  J r!  Sr"OSr"\RF                  " \$5      r%Sr&S\'S\'S\(4S jr)    S!S\'S-  S\*\RV                  -  S-  S\,\'   S-  S\,\(   S-  4S jjr- S"S\R\                  S\R^                  S-  S\*4S jjr0 " S S \\5      r1g)#    N)AnyCallable)Qwen2TokenizerFastQwen3ForCausalLM   )Flux2LoraLoaderMixin)AutoencoderKLFlux2Flux2Transformer2DModel)FlowMatchEulerDiscreteScheduler)is_torch_xla_availableloggingreplace_example_docstring)randn_tensor   )DiffusionPipeline   )Flux2ImageProcessor)Flux2PipelineOutputTFa  
    Examples:
        ```py
        >>> import torch
        >>> from diffusers import Flux2KleinPipeline

        >>> pipe = Flux2KleinPipeline.from_pretrained(
        ...     "black-forest-labs/FLUX.2-klein-base-9B", 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=4.0).images[0]
        >>> image.save("flux2_output.png")
        ```
image_seq_len	num_stepsreturnc                     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              h/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/pipelines/flux2/pipeline_flux2_klein.pycompute_empirical_mur$   ?   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 valuesr(   zThe current scheduler class zx's `set_timesteps` does not support custom timestep schedules. Please check whether you are using the correct scheduler.)r(   r'   r)   zv's `set_timesteps` does not support custom sigmas schedules. Please check whether you are using the correct scheduler.)r)   r'   r'    )

ValueErrorsetinspect	signatureset_timesteps
parameterskeys	__class__r(   len)	schedulerr&   r'   r(   r)   kwargsaccepts_timestepsaccept_sigmass           r#   retrieve_timestepsr9   R   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''	))r%   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)hasattrr>   r?   moderA   AttributeError)r:   r;   r<   s      r#   retrieve_latentsrE      s}     ~}--+2I))00;;		/	/K84K))..00		+	+%%%RSSr%   c            *         ^  \ rS rSrSrSrSS/r SES\S\S\	S	\
S
\S\4U 4S jjjr\    SFS\	S	\
S\\\   -  S\R$                  S-  S\R&                  S-  S\S\\   4S jj5       r\ SGS\R,                  S\R,                  S-  4S jj5       r\S\R,                  4S j5       r\ SHS\\R,                     S\4S jj5       r\S 5       r\S 5       r\S 5       r\ SIS\R,                  S \R,                  S!\S-  S"\S-  S#\\R,                     4
S$ jj5       r     SJS\\\   -  S\R&                  S-  S&\S\R,                  S-  S\S'\\   4S( jjrS)\R,                  S*\R@                  4S+ jr! SGS*\R@                  S\R,                  S-  4S, jjr"S-\\R,                     S*\R@                  4S. jr#   SKS/ jr$\%S0 5       r&\%S1 5       r'\%S2 5       r(\%S3 5       r)\%S4 5       r*\%S5 5       r+\RX                  " 5       \-" \.5      SSSSS6SS7S%SSSSS8S9SSS/SS4S)\\/R`                  R`                     \/R`                  R`                  -  S-  S\\\   -  S!\S-  S"\S-  S:\S;\\1   S-  S<\1S&\S*\R@                  \\R@                     -  S-  S\R,                  S-  S\R,                  S-  S=\\\   -  S-  S>\S?\S@\2\\34   S-  SA\4\\\2/S4   S-  SB\\   S\S'\\   4&SC jj5       5       r5SDr6U =r7$ )LFlux2KleinPipeline   a  
The Flux2 Klein pipeline for text-to-image generation.

Reference:
[https://bfl.ai/blog/flux2-klein-towards-interactive-visual-intelligence](https://bfl.ai/blog/flux2-klein-towards-interactive-visual-intelligence)

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 ([`Qwen3ForCausalLM`]):
        [Qwen3ForCausalLM](https://huggingface.co/docs/transformers/en/model_doc/qwen3#transformers.Qwen3ForCausalLM)
    tokenizer (`Qwen2TokenizerFast`):
        Tokenizer of class
        [Qwen2TokenizerFast](https://huggingface.co/docs/transformers/en/model_doc/qwen2#transformers.Qwen2TokenizerFast).
ztext_encoder->transformer->vaerA   prompt_embedsr5   vaetext_encoder	tokenizertransformeris_distilledc                 H  > [         TU ]  5         U R                  UUUUUS9  U R                  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        g )
N)rJ   rK   rL   r5   rM   )rN   rJ   r   r      )vae_scale_factor      )super__init__register_modulesregister_to_configgetattrr4   rJ   configblock_out_channelsrQ   r   image_processortokenizer_max_lengthdefault_sample_size)selfr5   rJ   rK   rL   rM   rN   r3   s          r#   rU   Flux2KleinPipeline.__init__   s     	%# 	 	
 	\:V]^bdikoVpVpc$((//*L*L&MPQ&Q Rvw  3DDYDY\]D]^$'!#& r%   NrR   	         promptdtyper'   max_sequence_lengthhidden_states_layersc           	         Uc  U R                   OUnUc  U R                  OUn[        U[        5      (       a  U/OUn/ n/ nU HM  n	SU	S./n
UR	                  U
SSSS9nU" USSSUS9nUR                  US	   5        UR                  US
   5        MO     [        R                  " USS9R                  U5      n[        R                  " USS9R                  U5      nU " UUSSS9n[        R                  " U Vs/ s H  nUR                  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user)rolecontentFT)tokenizeadd_generation_promptenable_thinkingpt
max_length)return_tensorspadding
truncationrp   	input_idsattention_maskr   dim)rt   ru   output_hidden_states	use_cacher   )re   r'   r   r   )re   r'   
isinstancestrapply_chat_templateappendtorchcattostackhidden_statesshapepermutereshape)rK   rL   rd   re   r'   rf   rg   all_input_idsall_attention_maskssingle_promptmessagestextinputsrt   ru   outputkout
batch_sizenum_channelsseq_len
hidden_dimrI   s                          r#   _get_qwen3_prompt_embeds+Flux2KleinPipeline._get_qwen3_prompt_embeds   s    ',m""(.$$F'44&& #M!'MBCH00&* %	 1 D #$.F   !45&&v.>'?@# $& IIm366v>	#6A>AA&I )!%	
 kk<PQ<Pq6//2<PQWXYff5f08;		5
L':Aq!Q/77
G\\fMfg Rs   9E"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L_out_idsithwlcoordss               r#   _prepare_text_ids$Flux2KleinPipeline._prepare_text_ids  s     ''aqA#*?Q
AQAQAQA))!5FNN6"  {{7##r%   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)
rA   r   r   heightwidthr   r   r   r   
latent_idss
             r#   _prepare_latent_ids&Flux2KleinPipeline._prepare_latent_ids  s    $ (/}}$
vLLOLL LLLLO ))!5
  ))!,33JBG
r%   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   rv   )rz   listr,   typer~   r   r4   viewzipsqueezer   r   r}   r   r   )
r   r   r   t_coordsimage_latent_idsr   r   r   r   x_idss
             r#   _prepare_image_ids%Flux2KleinPipeline._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   rA   r   num_channels_latentsr   r   s        r#   _patchify_latents$Flux2KleinPipeline._patchify_latentsp  su     ;B--7
&,,z1aQVZ[Q[]^_//!Q1a3//*Q.FRSUZ^_U_`r%   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        r#   _unpatchify_latents&Flux2KleinPipeline._unpatchify_latentsy  sn     ;B--7
&//*u.MqRSU[c//!Q1a3//*u.MvXYz[`cd[der%   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   )rA   r   r   r   r   s        r#   _pack_latents Flux2KleinPipeline._pack_latents  s;     3:--/
&//*FNKSSTUWXZ[\r%   r   r   r   r   c                    / n[        X5       GH7  u  pVUR                  u  pxUSS2S4   R                  [        R                  5      n	USS2S4   R                  [        R                  5      n
Ub  UO[        R
                  " U	5      S-   nUb  UO[        R
                  " U
5      S-   nX-  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M:     [        R                  " USS9$ )z1
using position ids to scatter tokens into place
Nr   r   r'   re   r   r   rv   )r   r   r   r~   int64maxzerosr'   re   scatter_r   r   r   r   r}   r   )r   r   r   r   x_listdataposr   chh_idsw_idsr   r   flat_idsr   s                  r#   _unpack_latents_with_ids+Flux2KleinPipeline._unpack_latents_with_ids  s"    QIDJJEA1ILL-E1ILL-E !,%))E2BQ2FA*		%0@10DAy5(H++qubk$++TZZPCLLH..q188R@$G ((1$,,Q15CMM## '& {{6q))r%   r   num_images_per_prompttext_encoder_out_layersc           	      x   U=(       d    U R                   nUc  Sn[        U[        5      (       a  U/OUnUc(  U R                  U R                  U R
                  UUU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$ )N )rK   rL   rd   r'   rf   rg   r   r   )_execution_devicerz   r{   r   rK   rL   r   repeatr   r   r   )r^   rd   r'   r   rI   rf   r   r   r   r   text_idss              r#   encode_prompt Flux2KleinPipeline.encode_prompt  s     1411>F'44&&  99!....$7%< : M "/!4!4
Q%,,Q0EqI%**:+MwXZ[))-8;;v&&&r%   imager;   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   r@   )r;   r<   r   r   )ndimr,   rE   rJ   encoder   bnrunning_meanr   r   r'   re   r~   sqrtrunning_varrY   batch_norm_eps)r^   r   r;   r   latents_bn_meanlatents_bn_stds         r#   _encode_vae_image$Flux2KleinPipeline._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r%   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.)r;   r'   re   r   )
intrQ   rz   r   r4   r,   r   r   r   r   )r^   r   num_latents_channelsr   r   re   r'   r;   rA   r   r   s              r#   prepare_latents"Flux2KleinPipeline.prepare_latents  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-""r%   imagesc                    / 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   r;   r   rv   r   )
r   r   r}   r   r   r   r~   r   r   r   )r^   r   r   r;   r'   re   r   r   imagge_latentr   packed_latentslatentpackeds                r#   prepare_image_latents(Flux2KleinPipeline.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..r%   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  owT 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bA  [        U[        5      (       d,  [        U[        5      (       d  [	        S[        U5       35      eUS:  a6  T R                  R                  (       a  [        R                  SU S35        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     r#   	<genexpr>2Flux2KleinPipeline.check_inputs.<locals>.<genexpr>5  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       ?zGuidance scale z+ is ignored for step-wise distilled models.)rQ   loggerwarningallr,   r  rz   r{   r   r   rY   rN   )r^   rd   r   r   rI   "callback_on_step_end_tensor_inputsguidance_scaler   s   `       r#   check_inputsFlux2KleinPipeline.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CDKK$<$<NN_^,<<ghi %= pHs   E$+E$c                     U R                   $ r  )_guidance_scaler^   s    r#   r  !Flux2KleinPipeline.guidance_scaleK  s    ###r%   c                 d    U R                   S:  =(       a    U R                  R                  (       + $ r   )r  rY   rN   r  s    r#   do_classifier_free_guidance.Flux2KleinPipeline.do_classifier_free_guidanceO  s%    ##a'H0H0H,HHr%   c                     U R                   $ r  )_attention_kwargsr  s    r#   attention_kwargs#Flux2KleinPipeline.attention_kwargsS      %%%r%   c                     U R                   $ r  )_num_timestepsr  s    r#   num_timesteps Flux2KleinPipeline.num_timestepsW  s    """r%   c                     U R                   $ r  )_current_timestepr  s    r#   current_timestep#Flux2KleinPipeline.current_timestep[  r  r%   c                     U R                   $ r  )
_interruptr  s    r#   	interruptFlux2KleinPipeline.interrupt_  s    r%   2   g      @pilTr&   r)   r  negative_prompt_embedsoutput_typereturn_dictr  callback_on_step_endr
  c                 @   U R                  U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 R                  UUUUUUS9u  nnU R                  (       a@  SnUb$  [        U[        5      (       a  U/[        U5      -  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%        U R>                  RM                  S5        U RO                  US9 n&[Q        U$5       GH  u  n'n(U RR                  (       a  M  U(U l        U(RU                  U
R                  S   5      RW                  U
R2                  5      n)U
RW                  U R*                  R2                  5      n*Un+U bQ  [X        RZ                  " U
U /SS9RW                  U R*                  R2                  5      n*[X        RZ                  " UU!/SS9n+U R*                  R]                  S5         U R+                  U*U)S-  SUUU+U R^                  SS9S   n,SSS5        W,SS2SU
R                  S5      24   n,U R                  (       ao  U R*                  R]                  S5         U R+                  U*U)S-  SUWU+U R                  SS9S   n-SSS5        W-SS2SU
R                  S5      24   n-U-UU,U--
  -  -   n,U
R2                  n.U R>                  Ra                  U,U(U
SS9S   n
U
R2                  U.:w  a>  [X        Rb                  Rd                  Rg                  5       (       a  U
RW                  U.5      n
UbJ  0 n/U H  n0[i        5       U0   U/U0'   M     U" U U'U(U/5      n1U1Rk                  SU
5      n
U1Rk                  SU5      nU'[        U$5      S-
  :X  d)  U'S-   U%:  a0  U'S-   U R>                  RH                  -  S:X  a  U&Rm                  5         [n        (       d  GM  [p        Rr                  " 5         GM     SSS5        SU l        S	[u        U5      U R"                  S	-  -  -  n2S	[u        U5      U R"                  S	-  -  -  n3U Rw                  U
UU2S	-  U3S	-  5      n
U R6                  Rx                  Rz                  R}                  SSSS5      RW                  U
R~                  U
R2                  5      n4[X        R                  " U R6                  Rx                  R                  R}                  SSSS5      U R6                  R,                  R                  -   5      RW                  U
R~                  U
R2                  5      n5U
U5-  U4-   n
U R                  U
5      n
US:X  a  U
nO6U 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! , (       d  f       GN@= f! , (       d  f       GN= f) ak  
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 4.0):
        Guidance scale as defined in [Classifier-Free Diffusion
        Guidance](https://huggingface.co/papers/2207.12598). `guidance_scale` is defined as `w` of equation 2.
        of [Imagen Paper](https://huggingface.co/papers/2205.11487). Guidance scale is enabled by setting
        `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to
        the text `prompt`, usually at the expense of lower image quality. For step-wise distilled models,
        `guidance_scale` is ignored.
    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.
    negative_prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated negative text embeddings. Note that "" is used as the negative prompt in this pipeline.
        If not provided, will be generated from "".
    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.

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.
)rd   r   r   rI   r
  r  NFr   r   )rd   rI   r'   r   rf   r   r   i   r   crop)r   r   resize_moder   )r   r   r   r   re   r'   r;   rA   )r   r   r;   r'   re   r  use_flow_sigmas)r   r   )r)   r   )totalrv   condi  )r   timestepguidanceencoder_hidden_statestxt_idsimg_idsjoint_attention_kwargsr*  uncond)r*  rA   rI   r   r   )r)  )r   )Hr  r  r  r  r#  rz   r{   r   r4   r   r   r   r  r[   check_image_inputsize_resize_to_target_arearQ   
preprocessr}   r]   rM   rY   in_channelsr   re   r   rJ   nplinspacerB   r5   r/  r$   r9   r   orderr  set_begin_indexprogress_bar	enumerater$  r   r   r~   r   cache_contextr  stepbackendsmpsis_availablelocalspopupdateXLA_AVAILABLExm	mark_stepr   r   r   r   r   r'   r   r   r   r   decodepostprocessmaybe_free_model_hooksr   )6r^   r   rd   r   r   r&   r)   r  r   r;   rA   rI   r(  r)  r*  r  r+  r
  rf   r   r   r'   r   negative_promptnegative_text_idscondition_imagesimgimage_widthimage_heightmultiple_ofr   r   r   r   r   r   r(   num_warmup_stepsrB  r   r   r2  latent_model_inputlatent_image_ids
noise_predneg_noise_predlatents_dtypecallback_kwargsr   callback_outputslatent_heightlatent_widthr   r   s6                                                         r#   __call__Flux2KleinPipeline.__call__c  s   J 	'/Q) 	 	
  .!1!% *VS"9"9JJvt$<$<VJ&,,Q/J'' #'"4"4'"7 3$; #5 #
x ++ O!j&>&>#2"3c&k"A8<8J8J&4&;$7(? 9K 95"$5 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
 	&&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$%%33F;!%!1!1&8!)D!%.; ( 0/3/D/D$) "2 	" 	"J < (+>W\\!_+>(>?
33))77A)-)9)9*<%-_%)2H$5$4373I3I(- *: 	* 	* B &4A7Ja7J4J%KN!/.JQ_D_2`!`J !(..--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 "& S[T-B-BQ-FGHCJ4+@+@1+DEF//]VWEWYeijYjk((++22772q!DGGX_XeXefDHHKK$;$;$@$@B1$MPTPXPXP_P_PnPn$norrNNGMM
 N*_<**73("EHHOOGO?BE((44U4TE 	##%8O"%00_ <; BA? :9sE   -C:b''a*Ab'a<Ebb*
a94b<
bb
b)	r  r  r  r#  r  r]   r[   r\   rQ   )F)NNrR   r`   r  )
   )NN)Nr   NrR   r`   )NNN)8__name__
__module____qualname____firstlineno____doc__model_cpu_offload_seqr  r   r	   r   r   r
   boolrU   staticmethodr{   r   r~   re   r'   r   r   Tensorr   r   r   r   r   r   r   tupler   	Generatorr   r   r   r  propertyr  r  r  r  r   r$  no_gradr   EXAMPLE_DOC_STRINGPILImager   dictr   r   rc  __static_attributes____classcell__)r3   s   @r#   rG   rG      s>   ( =(/: #'2'  ' '	'
 &' -' ' '8 
 %)&*#&*56&6%6 d3i6 {{T!	6
 t#6 !6 #3i6 6p  (,$<<$$$ $$  >  / ELL)/ /  / b       ]a*<<* %*69Dj*PSVZPZ*	ell	* *> '+%&-1#&.9 'd3i ' t# '  #	 '
 ||d* ' ! ' "'s 'Fu||  2 (,# ??# $#D /U\\" / ??	 /N +/'jR $ $ I I & & # # & &   ]]_12 AE"&! #%%) #%&DH'+-19=  26HL9B#&.9)x1CIIOO$syy6=x1 d3ix1 d
	x1
 Tzx1 !x1 Ud"x1 x1  #x1 ??T%//%::TAx1 $x1 ||d*x1 !$d3i$ 6x1 x1 x1  sCx.4/!x1" 'S$'7'=>E#x1$ -1I%x1& !'x1( "'s)x1 3 x1r%   rG   )NNNN)Nr?   )2r.   typingr   r   numpyr>  rt  r~   transformersr   r   loadersr   modelsr	   r
   
schedulersr   utilsr   r   r   utils.torch_utilsr   pipeline_utilsr   r[   r   pipeline_outputr   torch_xla.core.xla_modelcore	xla_modelrM  rL  
get_loggerrf  r  rs  r   r   r$   r{   r'   r   r9   rn  rp  rE   rG   r+   r%   r#   <module>r     s(       
  = + A 9 O O - . 0 0 ))MM 
		H	% (   * '+(,"&!%8*t8* %,,%8* Cy4	8*
 K$8*z `h
TLL
T-2__t-C
TY\
TB1*,@ B1r%   