
    
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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%\RL                  " \'5      r(Sr)S\*S\*S\+4S jr,    S"S\*S-  S\-\R\                  -  S-  S\/\*   S-  S\/\+   S-  4S jjr0 S#S\Rb                  S\Rd                  S-  S\-4S jjr3 " S  S!\\5      r4g)$    N)AnyCallable)Qwen2TokenizerFastQwen3ForCausalLM   )Flux2LoraLoaderMixin)AutoencoderKLFlux2Flux2Transformer2DModel)Flux2KVAttnProcessor Flux2KVParallelSelfAttnProcessor)FlowMatchEulerDiscreteScheduler)is_torch_xla_availableloggingreplace_example_docstring)randn_tensor   )DiffusionPipeline   )Flux2ImageProcessor)Flux2PipelineOutputTFa  
    Examples:
        ```py
        >>> import torch
        >>> from PIL import Image
        >>> from diffusers import Flux2KleinKVPipeline

        >>> pipe = Flux2KleinKVPipeline.from_pretrained(
        ...     "black-forest-labs/FLUX.2-klein-9b-kv", torch_dtype=torch.bfloat16
        ... )
        >>> pipe.to("cuda")
        >>> ref_image = Image.open("reference.png")
        >>> image = pipe("A cat dressed like a wizard", image=ref_image, num_inference_steps=4).images[0]
        >>> image.save("flux2_kv_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              k/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/pipelines/flux2/pipeline_flux2_klein_kv.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_timestepsr;   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@   rA   moderC   AttributeError)r<   r=   r>   s      r%   retrieve_latentsrG      s}     ~}--+2I))00;;		/	/K84K))..00		+	+%%%RSSr'   c            &         ^  \ rS rSrSrSrSS/r SAS\S\S	\	S
\
S\S\4U 4S jjjr\    SBS	\	S
\
S\\\   -  S\R$                  S-  S\R&                  S-  S\S\\   4S jj5       r\ SCS\R,                  S\R,                  S-  4S jj5       r\S\R,                  4S j5       r\ SDS\\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S$ r     SES\\\   -  S\R&                  S-  S&\S\R,                  S-  S\S'\\   4S( jjr S)\R,                  S*\RB                  4S+ jr" SCS*\RB                  S\R,                  S-  4S, jjr#S-\\R,                     S*\RB                  4S. jr$  SFS/ jr%\&S0 5       r'\&S1 5       r(\&S2 5       r)\&S3 5       r*\RV                  " 5       \," \-5      SSSSS4SS%SSSS5SSSS/SS4S)\\.R^                  R^                     \.R^                  R^                  -  S-  S\\\   -  S6\S-  S7\S-  S8\S9\\0   S-  S&\S*\RB                  \\RB                     -  S-  S\R,                  S-  S\R,                  S-  S:\S;\S<\1\\24   S-  S=\3\\\1/S4   S-  S>\\   S\S'\\   4"S? jj5       5       r4S@r5U =r6$ )GFlux2KleinKVPipeline   a  
The Flux2 Klein KV pipeline for text-to-image generation with KV-cached reference image conditioning.

On the first denoising step, reference image tokens are included in the forward pass and their attention K/V
projections are cached. On subsequent steps, the cached K/V are reused without recomputing, providing faster
inference when using reference images.

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->vaerC   prompt_embedsTr7   vaetext_encoder	tokenizertransformeris_distilledc                 J  > [         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 R                  5         g )	N)rL   rM   rN   r7   rO   rL   r   r      )vae_scale_factor      )super__init__register_modulesgetattrr6   rL   configblock_out_channelsrS   r   image_processortokenizer_max_lengthdefault_sample_size_set_kv_attn_processors)selfr7   rL   rM   rN   rO   rP   r5   s          r%   rW   Flux2KleinKVPipeline.__init__   s     	%# 	 	
 W^^bdikoVpVpc$((//*L*L&MPQ&Q Rvw  3DDYDY\]D]^$'!#&  	$$&r'   NrT   	         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
truncationrr   	input_idsattention_maskr   dim)rv   rw   output_hidden_states	use_cacher   )rg   r)   r   r   )rg   r)   
isinstancestrapply_chat_templateappendtorchcattostackhidden_statesshapepermutereshape)rM   rN   rf   rg   r)   rh   ri   all_input_idsall_attention_maskssingle_promptmessagestextinputsrv   rw   outputkout
batch_sizenum_channelsseq_len
hidden_dimrK   s                          r%   _get_qwen3_prompt_embeds-Flux2KleinKVPipeline._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&Flux2KleinKVPipeline._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)
rC   r   r   heightwidthr   r   r   r   
latent_idss
             r%   _prepare_latent_ids(Flux2KleinKVPipeline._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   rx   )r|   listr.   typer   r   r6   viewzipsqueezer   r   r   r   r   )
r   r   r   t_coordsimage_latent_idsr   r   r   r   x_idss
             r%   _prepare_image_ids'Flux2KleinKVPipeline._prepare_image_idsB  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   rC   r   num_channels_latentsr   r   s        r%   _patchify_latents&Flux2KleinKVPipeline._patchify_latentsu  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(Flux2KleinKVPipeline._unpatchify_latents~  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   )rC   r   r   r   r   s        r%   _pack_latents"Flux2KleinKVPipeline._pack_latents  s;     3:--/
&//*FNKSSTUWXZ[\r'   r   r   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   r)   rg   r   r   rx   )r   r   r   r   int64maxzerosr)   rg   scatter_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-Flux2KleinKVPipeline._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))r'   c                    U R                   R                   H&  nUR                  R                  [	        5       5        M(     U R                   R
                   H&  nUR                  R                  [        5       5        M(     g)zBReplace default attention processors with KV-cache-aware variants.N)rO   transformer_blocksattnset_processorr   single_transformer_blocksr   )r`   blocks     r%   r_   ,Flux2KleinKVPipeline._set_kv_attn_processors  s[    %%88EJJ$$%9%;< 9%%??EJJ$$%E%GH @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 )rM   rN   rf   r)   rh   ri   r   r   )_execution_devicer|   r}   r   rM   rN   r   repeatr   r   r   )r`   rf   r)   r   rK   rh   r   r   r   r   text_idss              r%   encode_prompt"Flux2KleinKVPipeline.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   rB   )r=   r>   r   r   )ndimr.   rG   rL   encoder   bnrunning_meanr   r   r)   rg   r   sqrtrunning_varrZ   batch_norm_eps)r`   r   r=   r   latents_bn_meanlatents_bn_stds         r%   _encode_vae_image&Flux2KleinKVPipeline._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)   rg   r   )
intrS   r|   r   r6   r.   r   r   r   r   )r`   r   num_latents_channelsr   r   rg   r)   r=   rC   r   r   s              r%   prepare_latents$Flux2KleinKVPipeline.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   rx   r   )
r   r   r   r   r   r   r   r   r   r   )r`   r   r   r=   r)   rg   r   r   imagge_latentr   packed_latentslatentpackeds                r%   prepare_image_latents*Flux2KleinKVPipeline.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  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     r%   	<genexpr>4Flux2KleinKVPipeline.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 )
rS   loggerwarningallr.   r
  r|   r}   r   r   )r`   rf   r   r   rK   "callback_on_step_end_tensor_inputsr   s   `      r%   check_inputs!Flux2KleinKVPipeline.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	  )_attention_kwargsr`   s    r%   attention_kwargs%Flux2KleinKVPipeline.attention_kwargsQ      %%%r'   c                     U R                   $ r	  )_num_timestepsr  s    r%   num_timesteps"Flux2KleinKVPipeline.num_timestepsU  s    """r'   c                     U R                   $ r	  )_current_timestepr  s    r%   current_timestep%Flux2KleinKVPipeline.current_timestepY  r  r'   c                     U R                   $ r	  )
_interruptr  s    r%   	interruptFlux2KleinKVPipeline.interrupt]  s    r'   r   pilr   r   r(   r+   output_typereturn_dictr  callback_on_step_endr  c                 |   U R                  UUUU
US9  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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 R-                  UU-  UUUU
R.                  UUU	S9u  n	nSnSnUb-  U R1                  UUU-  UUU R2                  R.                  S9u  nnUc  [4        R6                  " SSU-  U5      OUn[9        U R:                  R(                  S5      (       a'  U R:                  R(                  R<                  (       a  SnU	R                  S   n[?        UUS9n[A        U R:                  UUUUS9u  n n[C        [        U 5      XPR:                  RD                  -  -
  S5      n![        U 5      U l#        U R:                  RI                  S5        Sn"U RK                  US9 n#[M        U 5       GH  u  n$n%U RN                  (       a  M  U%U l        U%RQ                  U	R                  S   5      RS                  U	R.                  5      n&U$S:X  a  Ub  [T        RV                  " UU	/SS9RS                  U R&                  R.                  5      n'[T        RV                  " UU/SS9n(U R'                  U'U&S-  SU
UU(U RX                  SSUR                  S   S9
u  n)n"OU"bL  U R'                  U	RS                  U R&                  R.                  5      U&S-  SU
UUU RX                  SU"SS9
S   n)OIU R'                  U	RS                  U R&                  R.                  5      U&S-  SU
UUU RX                  SS9S   n)U	R.                  n*U R:                  R[                  U)U%U	SS9S   n	U	R.                  U*:w  a>  [T        R\                  R^                  Ra                  5       (       a  U	RS                  U*5      n	UbJ  0 n+U H  n,[c        5       U,   U+U,'   M     U" U U$U%U+5      n-U-Re                  SU	5      n	U-Re                  SU
5      n
U$[        U 5      S-
  :X  d)  U$S-   U!:  a0  U$S-   U R:                  RD                  -  S:X  a  U#Rg                  5         [h        (       d  GM  [j        Rl                  " 5         GM     SSS5        U"b  U"Ro                  5         SU l        U Rq                  U	U5      n	U R2                  Rr                  Rt                  Rw                  SSSS5      RS                  U	Rx                  U	R.                  5      n.[T        Rz                  " U R2                  Rr                  R|                  Rw                  SSSS5      U R2                  R(                  R~                  -   5      RS                  U	Rx                  U	R.                  5      n/U	U/-  U.-   n	U R                  U	5      n	US:X  a  U	nO6U R2                  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 (`PIL.Image.Image` or `List[PIL.Image.Image]`, *optional*):
        Reference image(s) for conditioning. On the first denoising step, reference tokens are included in the
        forward pass and their attention K/V are cached. On subsequent steps, the cached K/V are reused without
        recomputing.
    prompt (`str` or `List[str]`, *optional*):
        The prompt or prompts to guide the image generation.
    height (`int`, *optional*):
        The height in pixels of the generated image.
    width (`int`, *optional*):
        The width in pixels of the generated image.
    num_inference_steps (`int`, *optional*, defaults to 4):
        The number of denoising steps.
    sigmas (`List[float]`, *optional*):
        Custom sigmas for the denoising schedule.
    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*):
        Generator(s) for deterministic generation.
    latents (`torch.Tensor`, *optional*):
        Pre-generated noisy latents.
    prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated text embeddings.
    output_type (`str`, *optional*, defaults to `"pil"`):
        Output format: `"pil"` or `"np"`.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether to return a `Flux2PipelineOutput` or a plain tuple.
    attention_kwargs (`dict`, *optional*):
        Extra kwargs passed to attention processors.
    callback_on_step_end (`Callable`, *optional*):
        Callback function called at the end of each denoising step.
    callback_on_step_end_tensor_inputs (`List`, *optional*):
        Tensor inputs for the callback function.
    max_sequence_length (`int`, defaults to 512):
        Maximum sequence length for the prompt.
    text_encoder_out_layers (`tuple[int]`):
        Layer indices for text encoder hidden state extraction.

Examples:

Returns:
    [`~pipelines.flux2.Flux2PipelineOutput`] or `tuple`.
)rf   r   r   rK   r  NFr   r   )rf   rK   r)   r   rh   r   i   r   crop)r   r   resize_moder   )r   r   r   r   rg   r)   r=   rC   )r   r   r=   r)   rg   g      ?use_flow_sigmas)r   r   )r+   r    )totalrx   i  extract)
r   timestepguidanceencoder_hidden_statestxt_idsimg_idsjoint_attention_kwargsr(  kv_cache_modenum_ref_tokenscached)
r   r0  r1  r2  r3  r4  r5  r(  kv_cacher6  )r   r0  r1  r2  r3  r4  r5  r(  )r(  rC   rK   r   r  )r'  )r   )Er  r  r  r#  r|   r}   r   r6   r   r   r   r\   check_image_inputsize_resize_to_target_arearS   
preprocessr   r^   rO   rZ   in_channelsr   rg   r  rL   nplinspacerD   r7   r-  r&   r;   r   orderr  set_begin_indexprogress_bar	enumerater$  r   r   r   r   r  stepbackendsmpsis_availablelocalspopupdateXLA_AVAILABLExm	mark_stepclearr   r   r   r   r)   r   r   r   r   decodepostprocessmaybe_free_model_hooksr   )0r`   r   rf   r   r   r(   r+   r   r=   rC   rK   r'  r(  r  r)  r  rh   r   r   r)   r   condition_imagesimgimage_widthimage_heightmultiple_ofr   r   r   r   r   r    r*   num_warmup_stepsr9  rC  r   r   r0  latent_model_inputlatent_image_ids
noise_predlatents_dtypecallback_kwargsr   callback_outputsr   r   s0                                                   r%   __call__Flux2KleinKVPipeline.__call__a  s   L 	'/Q 	 	
 "2!% *VS"9"9JJvt$<$<VJ&,,Q/J'' #'"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 	&&q)%89\!),1>>)*&88GMM!$4588G6m7).M73KQR)S)V)VW[WgWgWmWm)n&',yy2BJ1OUV'W$+/+;+;&8!)D!%.; ( 0/3/D/D$)&/'4':':1'= ,< ,(J )!%!1!1&-jj1A1A1G1G&H!)D!%.; ( */3/D/D$)!)&. "2 " "J "&!1!1&-jj1A1A1G1G&H!)D!%.; ( */3/D/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 '') =LLNU - :\ NN!%//D((++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0O :9s   J&^,^,,
^;)r  r  r#  r  r^   r\   r]   rS   )T)NNrT   rb   r	  )
   )Nr   NrT   rb   )NN)7__name__
__module____qualname____firstlineno____doc__model_cpu_offload_seqr
  r   r	   r   r   r
   boolrW   staticmethodr}   r   r   rg   r)   r   r   Tensorr   r   r   r   r   r   r   r_   tupler   	Generatorr   r   r  r  propertyr  r  r   r$  no_gradr   EXAMPLE_DOC_STRINGPILImager   dictr   r   r_  __static_attributes____classcell__)r5   s   @r%   rI   rI      s   0 =(/: "'2'  ' '	'
 &' -' ' ': 
 %)&*#&*56&6%6 d3i6 {{T!	6
 t#6 !6 #3i6 6p  (,$<<$$$ $$  >  / ELL)/ /  / b       *ELL * *$u||J\ * *2I '+%&-1#&.9 'd3i ' t# '  #	 '
 ||d* ' ! ' "'s 'Fu||  2 (,# ??# $#D /U\\" / ??	 /N +/#bJ & & # # & &   ]]_12 AE"&! #$%)%&DH'+-1  26HL9B#&.9%U1CIIOO$syy6=U1 d3iU1 d
	U1
 TzU1 !U1 Ud"U1  #U1 ??T%//%::TAU1 $U1 ||d*U1 U1 U1 sCx.4/U1 'S$'7'=>EU1  -1I!U1" !#U1$ "'s%U1 3 U1r'   rI   )NNNN)NrA   )5r0   typingr   r   numpyr?  rp  r   transformersr   r   loadersr   modelsr	   r
   %models.transformers.transformer_flux2r   r   
schedulersr   utilsr   r   r   utils.torch_utilsr   pipeline_utilsr   r\   r   pipeline_outputr   torch_xla.core.xla_modelcore	xla_modelrM  rL  
get_loggerrb  r  ro  r   r   r&   r}   r)   r   r;   rj  rl  rG   rI   r-   r'   r%   <module>r     s+       
  = + A k 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]1,.B ]1r'   