
    
3j                        S SK r S SKJr  S SKrS SKrS SKrS SKJ	r
  S SKJrJrJr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  SS
KJr  SSKJr  SSKJrJ r J!r!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-\!R\                  " \/5      r0\" 5       (       a  S SK1r1\!R\                  " \/5      r0Sr2S r3S r4S r5 " S S\&\5      r6g)    N)Callable)
functional)CLIPTextModelCLIPTokenizer"Qwen2_5_VLForConditionalGenerationQwen2VLProcessor   )MultiPipelineCallbacksPipelineCallback)PipelineImageInputVaeImageProcessor)KandinskyLoraLoaderMixin)AutoencoderKL)Kandinsky5Transformer3DModel)FlowMatchEulerDiscreteScheduler)is_ftfy_availableis_torch_xla_availableloggingreplace_example_docstring)randn_tensor   )DiffusionPipeline   )KandinskyImagePipelineOutputTFa<  
    Examples:

        ```python
        >>> import torch
        >>> from diffusers import Kandinsky5I2IPipeline

        >>> # Available models:
        >>> # kandinskylab/Kandinsky-5.0-I2I-Lite-sft-Diffusers
        >>> # kandinskylab/Kandinsky-5.0-I2I-Lite-pretrain-Diffusers

        >>> model_id = "kandinskylab/Kandinsky-5.0-I2I-Lite-sft-Diffusers"
        >>> pipe = Kandinsky5I2IPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
        >>> pipe = pipe.to("cuda")

        >>> prompt = "A cat and a dog baking a cake together in a kitchen."

        >>> output = pipe(
        ...     prompt=prompt,
        ...     negative_prompt="",
        ...     height=1024,
        ...     width=1024,
        ...     num_inference_steps=50,
        ...     guidance_scale=3.5,
        ... ).frames[0]
        ```
c                     [        5       (       a  [        R                  " U 5      n [        R                  " [        R                  " U 5      5      n U R                  5       $ )z
Copied from https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/wan/pipeline_wan.py

Clean text using ftfy if available and unescape HTML entities.
)r   ftfyfix_texthtmlunescapestriptexts    o/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/pipelines/kandinsky5/pipeline_kandinsky_i2i.pybasic_cleanr$   W   s>     }}T"==t,-D::<    c                 V    [         R                  " SSU 5      n U R                  5       n U $ )z
Copied from https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/wan/pipeline_wan.py

Normalize whitespace in text by replacing multiple spaces with single space.
z\s+ )resubr    r!   s    r#   whitespace_cleanr*   c   s&     66&#t$D::<DKr%   c                 .    [        [        U 5      5      n U $ )z
Copied from https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/wan/pipeline_wan.py

Apply both basic cleaning and whitespace normalization to prompts.
)r*   r$   r!   s    r#   prompt_cleanr,   n   s     K-.DKr%   c            -         ^  \ rS rSrSrSr/ SQrS\S\S\	S\
S	\S
\S\4U 4S jjr    S8S\\   S\S-  S\R&                  S-  S\S\R*                  S-  4
S jjr  S9S\\\   -  S\R&                  S-  S\R*                  S-  4S jjr    S:S\\\   -  S\R0                  S\S\S\R&                  S-  S\R*                  S-  4S jjr        S;S jr       S<S\S\S\S\S\S\R*                  S-  S\R&                  S-  S\R6                  \\R6                     -  S-  S\R0                  S-  S \R0                  4S! jjr\S" 5       r\S# 5       r\S$ 5       r \RB                  " 5       \"" \#5      SSSSS%S&SSSSSSSSSS'S(SS/S4S\S\\\   -  S)\\\   -  S-  S\S-  S\S-  S*\S+\$S\S-  S\R6                  \\R6                     -  S-  S\R0                  S-  S,\R0                  S-  S-\R0                  S-  S.\R0                  S-  S/\R0                  S-  S0\R0                  S-  S1\R0                  S-  S2\S-  S3\%S4\&\\S/\'\(-  4   S-  S5\\   S\4*S6 jj5       5       r)S7r*U =r+$ )=Kandinsky5I2IPipelinex   a2  
Pipeline for image-to-image generation using Kandinsky 5.0.

This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods
implemented for all pipelines (downloading, saving, running on a particular device, etc.).

Args:
    transformer ([`Kandinsky5Transformer3DModel`]):
        Conditional Transformer to denoise the encoded image latents.
    vae ([`AutoencoderKL`]):
        Variational Auto-Encoder Model [black-forest-labs/FLUX.1-dev
        (vae)](https://huggingface.co/black-forest-labs/FLUX.1-dev) to encode and decode videos to and from latent
        representations.
    text_encoder ([`Qwen2_5_VLForConditionalGeneration`]):
        Frozen text-encoder [Qwen2.5-VL](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct).
    tokenizer ([`AutoProcessor`]):
        Tokenizer for Qwen2.5-VL.
    text_encoder_2 ([`CLIPTextModel`]):
        Frozen [CLIP](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel),
        specifically the [clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14) variant.
    tokenizer_2 ([`CLIPTokenizer`]):
        Tokenizer for CLIP.
    scheduler ([`FlowMatchEulerDiscreteScheduler`]):
        A scheduler to be used in combination with `transformer` to denoise the encoded image latents.
z.text_encoder->text_encoder_2->transformer->vae)latentsprompt_embeds_qwenprompt_embeds_clipnegative_prompt_embeds_qwennegative_prompt_embeds_cliptransformervaetext_encoder	tokenizertext_encoder_2tokenizer_2	schedulerc           
         > [         TU ]  5         U R                  UUUUUUUS9  SU l        SU l        SU l        [        U R
                  S9U l        / SQU l        g )N)r5   r6   r7   r8   r9   r:   r;   a=  <|im_start|>system
You are a promt engineer. Based on the provided source image (first image) and target image (second image), create an interesting text prompt that can be used together with the source image to create the target image:<|im_end|><|im_start|>user{}<|vision_start|><|image_pad|><|vision_end|><|im_end|>7      )vae_scale_factor))   r@   )    )rB   rA   )      )rD   rC   )    )rF   rE   )	super__init__register_modulesprompt_template prompt_template_encode_start_idxvae_scale_factor_spatialr   image_processorresolutions)	selfr5   r6   r7   r8   r9   r:   r;   	__class__s	           r#   rH   Kandinsky5I2IPipeline.__init__   sn     	#%)# 	 	
  `02-()%0$B_B_`wr%   Nr@   promptimagedevicemax_sequence_lengthdtypec           
      *   U=(       d    U R                   nU=(       d    U R                  R                  n[        U[        5      (       d  U/nU Vs/ s H5  ofR                  UR                  S   S-  UR                  S   S-  45      PM7     nnU Vs/ s H  opR                  R                  U5      PM     nnU R                  U-   n	U R                  UUSSSS9S   n
U
R                  S	   U	:  a  [        U5       H  u  pkX   nXR                  R                  :H  R                  5       nXU R                  R                  :g     U R                  S
 nU R                  R                  XU-
  S-
  S 5      n[!        U5      S:  d  M  US[!        U5      *  X'   ["        R%                  SU SU 35        M     U R                  UUSU	SSSS9R'                  U5      nU R                  " S0 UDSSS.D6S   S	   SS2U R                  S24   nUS   SS2U R                  S24   n[(        R*                  " UR                  S5      SS9n[,        R.                  " USSS9R'                  [(        R0                  S9nUR'                  U5      U4$ s  snf s  snf )aE  
Encode prompt using Qwen2.5-VL text encoder.

This method processes the input prompt through the Qwen2.5-VL model to generate text embeddings suitable for
image generation.

Args:
    prompt list[str]: Input list of prompts
    image (PipelineImageInput): Input list of images to condition the generation on
    device (torch.device): Device to run encoding on
    max_sequence_length (int): Maximum sequence length for tokenization
    dtype (torch.dtype): Data type for embeddings

Returns:
    tuple[torch.Tensor, torch.Tensor]: Text embeddings and cumulative sequence lengths
r   r   r   Nptlongest)r"   imagesvideosreturn_tensorspadding	input_idsr	   zXThe following part of your input was truncated because `max_sequence_length` is set to  z	 tokens: T)r"   rZ   r[   
max_length
truncationr\   r]   )return_dictoutput_hidden_stateshidden_statesattention_mask)dim)r   r   )valuerV    )_execution_devicer7   rV   
isinstancelistresizesizerJ   formatrK   r8   shape	enumerateimage_token_idsumdecodelenloggerwarningtotorchcumsumFpadint32)rO   rR   rS   rT   rU   rV   ip
full_textsmax_allowed_lenuntruncated_idsr"   tokensnum_image_tokensremoved_textinputsembedsrf   
cu_seqlenss                      r#   _encode_prompt_qwen)Kandinsky5I2IPipeline._encode_prompt_qwen   s   0 14110**00%&&GEEJKU166!9>166!9>:;UK>DEf**11!4f
E??BUU.. ) 
    $6$Z0(+$*nn.K.K$K#P#P#R $..*G*G GHInInqst#~~44VRb<bef<f<h5ij|$q($()=C,=+=$>JMNN/0	,I 1 &   
 "V* 	 "" 

!%
 	 	  !$"G"G"II	K   01!T5Z5Z5\2\]\\."4"4Q"7Q?
UU:vQ7:::M
yy++[ LEs   <J$Jc           	          U=(       d    U R                   nU=(       d    U R                  R                  nU R                  USSSSSS9R	                  U5      nU R                  " S0 UD6S   nUR	                  U5      $ )a  
Encode prompt using CLIP text encoder.

This method processes the input prompt through the CLIP model to generate pooled embeddings that capture
semantic information.

Args:
    prompt (str | list[str]): Input prompt or list of prompts
    device (torch.device): Device to run encoding on
    dtype (torch.dtype): Data type for embeddings

Returns:
    torch.Tensor: Pooled text embeddings from CLIP
M   Tra   rX   )ra   rb   add_special_tokensr]   r\   pooler_outputrj   )rk   r9   rV   r:   ry   )rO   rR   rT   rV   r   pooled_embeds         r#   _encode_prompt_clip)Kandinsky5I2IPipeline._encode_prompt_clip  s    ( 14112,,22!!#  " 
 "V* 	 **4V4_Eu%%r%   r   num_images_per_promptc                    U=(       d    U R                   nU=(       d    U R                  R                  n[        U[        5      (       d  U/n[        U5      nU Vs/ s H  n[        U5      PM     nnU R                  UUUUUS9u  pU R                  UUUS9nU	R                  SUS5      n	U	R                  Xs-  SU	R                  S   5      n	UR                  SUS5      nUR                  Xs-  S5      nU
R                  5       nUR                  U5      n[        R                  " [        R                   " S/U[        R"                  S9UR%                  S5      /5      nXU4$ s  snf )a1  
Encodes a single prompt (positive or negative) into text encoder hidden states.

This method combines embeddings from both Qwen2.5-VL and CLIP text encoders to create comprehensive text
representations for image generation.

Args:
    prompt (`str` or `list[str]`):
        Prompt to be encoded.
    num_images_per_prompt (`int`, *optional*, defaults to 1):
        Number of images to generate per prompt.
    max_sequence_length (`int`, *optional*, defaults to 1024):
        Maximum sequence length for text encoding. Must be less than 1024
    device (`torch.device`, *optional*):
        Torch device.
    dtype (`torch.dtype`, *optional*):
        Torch dtype.

Returns:
    tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
        - Qwen text embeddings of shape (batch_size * num_images_per_prompt, sequence_length, embedding_dim)
        - CLIP pooled embeddings of shape (batch_size * num_images_per_prompt, clip_embedding_dim)
        - Cumulative sequence lengths (`cu_seqlens`) for Qwen embeddings of shape (batch_size *
          num_images_per_prompt + 1,)
)rR   rS   rT   rU   rV   )rR   rT   rV   r   r_   r   rT   rV   )rk   r7   rV   rl   rm   rv   r,   r   r   repeatviewrq   diffrepeat_interleaverz   cattensorr~   r{   )rO   rR   rS   r   rU   rT   rV   
batch_sizer   r1   prompt_cu_seqlensr2   original_lengthsrepeated_lengthsrepeated_cu_seqlenss                  r#   encode_prompt#Kandinsky5I2IPipeline.encode_prompt'  s   D 14110**00&$''XF[
+126a,q/62 150H0H 3 1I 1
- "55 6 
 066$a
 044.4F4L4LR4P

 066$a
 044Z5WY[\ -113+==!
 $ii\\1#fEKK@BRBYBYZ[B\]
 "7JJJc 3s   E$c                   ^  Ub  US:  a  [        S5      eUc  [        S5      eXT4T R                  ;  aV  SR                  T R                   VVs/ s H  u  pSU SU S3PM     snn5      n[        R	                  SU S	U S
U S35        UbX  [        U 4S jU 5       5      (       d>  [        ST R                   SU Vs/ s H  nUT R                  ;  d  M  UPM     sn 35      eUc  Uc  U
b  Ub  Ub  U
c  [        S5      eUc  U	c  Ub  Ub  U	b  Uc  [        S5      eUc  Uc  [        S5      eUbA  [        U[        5      (       d,  [        U[        5      (       d  [        S[        U5       35      eUbC  [        U[        5      (       d-  [        U[        5      (       d  [        S[        U5       35      egggs  snnf s  snf )a  
Validate input parameters for the pipeline.

Args:
    prompt: Input prompt
    negative_prompt: Negative prompt for guidance
    image: Input image for conditioning
    height: Image height
    width: Image width
    prompt_embeds_qwen: Pre-computed Qwen prompt embeddings
    prompt_embeds_clip: Pre-computed CLIP prompt embeddings
    negative_prompt_embeds_qwen: Pre-computed Qwen negative prompt embeddings
    negative_prompt_embeds_clip: Pre-computed CLIP negative prompt embeddings
    prompt_cu_seqlens: Pre-computed cumulative sequence lengths for Qwen positive prompt
    negative_prompt_cu_seqlens: Pre-computed cumulative sequence lengths for Qwen negative prompt
    callback_on_step_end_tensor_inputs: Callback tensor inputs

Raises:
    ValueError: If inputs are invalid
Nr@   z*max_sequence_length must be less than 1024z6`image` must be provided for image-to-image generation,()z'`height` and `width` have to be one of 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).0krO   s     r#   	<genexpr>5Kandinsky5I2IPipeline.check_inputs.<locals>.<genexpr>  s      F
7Y!---7Ys   z2`callback_on_step_end_tensor_inputs` has to be in z, but found zuIf any of `prompt_embeds_qwen`, `prompt_embeds_clip`, or `prompt_cu_seqlens` is provided, all three must be provided.zIf any of `negative_prompt_embeds_qwen`, `negative_prompt_embeds_clip`, or `negative_prompt_cu_seqlens` is provided, all three must be provided.zProvide either `prompt` or `prompt_embeds_qwen` (and corresponding `prompt_embeds_clip` and `prompt_cu_seqlens`). Cannot leave all undefined.z2`prompt` has to be of type `str` or `list` but is z;`negative_prompt` has to be of type `str` or `list` but is )
ValueErrorrN   joinrw   rx   allr   rl   strrm   type)rO   rR   negative_promptrS   heightwidthr1   r2   r3   r4   r   negative_prompt_cu_seqlens"callback_on_step_end_tensor_inputsrU   whresolutions_strr   s   `                 r#   check_inputs"Kandinsky5I2IPipeline.check_inputs  sm   J */BT/IIJJ=UVV?$"2"22!hhAQAQ'RAQ!A3as!AQ'RSONN9/9J*U[T\\abgah  iQ  R .9# F
7YF
 C
 C
 DTEaEaDbbn  |^  pH  |^vw  bc  ko  kG  kG  bGpq  |^  pH  oI  J 
 )-?-KO`Ol!)-?-GK\Kd 2  (3*6)5 ,3.6-5 2  >08 ` 
 z&#'>'>zRXZ^G_G_QRVW]R^Q_`aa&?C00OUY9Z9ZZ[_`o[pZqrss :[0 '[ (S pHs   F<
?GGr   num_channels_latentsr   r   	generatorr0   returnc
                 v   U	b  U	R                  XvS9$ US[        U5      U R                  -  [        U5      U R                  -  U4n
[        U[        5      (       a*  [        U5      U:w  a  [        S[        U5       SU S35      e[        XXvS9n	U R                  R                  XUS9R                  XvS	9n[        R                  " 5          U R                  R                  U5      R                  R                  US
9nUR!                  S5      n[#        U R                  R$                  S5      (       a"  XR                  R$                  R&                  -  nUR)                  SSSSS5      n[        R*                  " X[        R,                  " U	SSS24   5      /S5      n	SSS5        U	$ ! , (       d  f       U	$ = f)a  
Prepare initial latent variables for image-to-image generation.

This method creates random noise latents with encoded image,

Args:
    image (PipelineImageInput): Input image to condition the generation on
    batch_size (int): Number of images to generate
    num_channels_latents (int): Number of channels in latent space
    height (int): Height of generated image
    width (int): Width of generated image
    dtype (torch.dtype): Data type for latents
    device (torch.device): Device to create latents on
    generator (torch.Generator): Random number generator
    latents (torch.Tensor): Pre-existing latents to use

Returns:
    torch.Tensor: Prepared latent tensor with encoded image
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   rT   rV   )r   r   ri   )r   r   scaling_factorr   r	      .r_   )ry   intrL   rl   rm   rv   r   r   rM   
preprocessrz   no_gradr6   encodelatent_distsample	unsqueezehasattrconfigr   permuter   	ones_like)rO   rS   r   r   r   r   rV   rT   r   r0   rq   image_tensorimage_latentss                r#   prepare_latents%Kandinsky5I2IPipeline.prepare_latents  s   > ::V:99 K4888J$777 
 i&&3y>Z+GA#i.AQ R&<'gi  u&V ++66uSX6Y\\]c\q]]_ HHOOL9EELLW`LaM)33A6M txx(899 -0N0N N *11!Q1a@MiiQTVXWXVXQXIY9Z []_`G   _ s   CF))
F8c                     U R                   $ )z%Get the current guidance scale value.)_guidance_scalerO   s    r#   guidance_scale$Kandinsky5I2IPipeline.guidance_scale'  s     ###r%   c                     U R                   $ )z&Get the number of denoising timesteps.)_num_timestepsr   s    r#   num_timesteps#Kandinsky5I2IPipeline.num_timesteps,  s     """r%   c                     U R                   $ )z)Check if generation has been interrupted.)
_interruptr   s    r#   	interruptKandinsky5I2IPipeline.interrupt1  s     r%   2   g      @pilTr   num_inference_stepsr   r1   r2   r3   r4   r   r   output_typerc   callback_on_step_endr   c                    [        U[        [        45      (       a  UR                  nUc5  Uc2  [        U[        5      (       a  US   R
                  OUR
                  u  pTU R                  UUUUUUUUUUUUUS9  XT4U R                  ;  aW  U R                  [        R                  " U R                   Vs/ s H  n[        US   US   -  XT-  -
  5      PM     sn5         u  pTXpl        SU l        U R                  nU R                  R                  nUb  [        U[         5      (       a  SnU/nO3Ub!  [        U[        5      (       a  [#        U5      nOUR$                  S   nUc  U R'                  UUUUUUS9u  pnU R(                  S:  a  Uc  Sn[        U[         5      (       a  Ub  U/[#        U5      -  OU/nO<[#        U5      [#        U5      :w  a$  [+        S	[#        U5       S
[#        U5       S35      eUc  U R'                  UUUUUUS9u  pnU R,                  R/                  UUS9  U R,                  R0                  nU R                  R2                  R4                  nU R7                  UUU-  UUUUUU	U
S9	n
[8        R:                  " SUS9[8        R:                  " X@R<                  -  S-  US9[8        R:                  " XPR<                  -  S-  US9/n[8        R:                  " UR?                  5       RA                  5       RC                  5       US9nUb?  [8        R:                  " UR?                  5       RA                  5       RC                  5       US9OSn/ SQnSn [#        U5      X`R,                  RD                  -  -
  n![#        U5      U l#        U RI                  US9 n"[K        U5       GHR  u  nn#U RL                  (       a  M  U#RO                  S5      RQ                  UU-  5      n$U R                  U
RS                  U5      URS                  U5      URS                  U5      U$RS                  U5      UUUU SS9	RT                  n%U R(                  S:  ak  Ubh  U R                  U
RS                  U5      URS                  U5      URS                  U5      U$RS                  U5      UUUU SS9	RT                  n&U&UU%U&-
  -  -   n%U R,                  RW                  U%SS2SS24   U#U
SS2SS2SS2SS2SU24   SS9S   U
SS2SS2SS2SS2SU24'   Ub  0 n'U H  n([Y        5       U(   U'U('   M     U" U UU#U'5      n)U)R[                  SU
5      n
U)R[                  SU5      nU)R[                  SU5      nU)R[                  SU5      nU)R[                  SU5      nU[#        U5      S-
  :X  d)  US-   U!:  a0  US-   U R,                  RD                  -  S:X  a  U"R]                  5         [^        (       d  GM=  [`        Rb                  " 5         GMU     SSS5        U
SS2SS2SS2SS2SU24   n
US:w  a  U
RS                  U Rd                  R                  5      n
U
Rg                  UUSX@R<                  -  XPR<                  -  U5      n
U
Ri                  SSSSSS5      n
U
Rg                  UU-  UX@R<                  -  XPR<                  -  5      n
XRd                  R2                  Rj                  -  n
U Rd                  Rm                  U
5      RT                  nU Rn                  Rq                  UUS9nOU
nU Rs                  5         U(       d  U4$ [u        US9$ s  snf ! , (       d  f       GNM= f)ab  
The call function to the pipeline for image-to-image generation.

Args:
    image (`PipelineImageInput`):
        The input image to condition the generation on. Must be an image, a list of images or a `torch.Tensor`.
    prompt (`str` or `list[str]`, *optional*):
        The prompt or prompts to guide the image generation. If not defined, pass `prompt_embeds` instead.
    negative_prompt (`str` or `list[str]`, *optional*):
        The prompt or prompts to avoid during image generation. If not defined, pass `negative_prompt_embeds`
        instead. Ignored when not using guidance (`guidance_scale` < `1`).
    height (`int`):
        The height in pixels of the generated image.
    width (`int`):
        The width in pixels of the generated image.
    num_inference_steps (`int`, defaults to `50`):
        The number of denoising steps.
    guidance_scale (`float`, defaults to `5.0`):
        Guidance scale as defined in classifier-free guidance.
    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*):
        A torch generator to make generation deterministic.
    latents (`torch.Tensor`, *optional*):
        Pre-generated noisy latents.
    prompt_embeds_qwen (`torch.Tensor`, *optional*):
        Pre-generated Qwen text embeddings.
    prompt_embeds_clip (`torch.Tensor`, *optional*):
        Pre-generated CLIP text embeddings.
    negative_prompt_embeds_qwen (`torch.Tensor`, *optional*):
        Pre-generated Qwen negative text embeddings.
    negative_prompt_embeds_clip (`torch.Tensor`, *optional*):
        Pre-generated CLIP negative text embeddings.
    prompt_cu_seqlens (`torch.Tensor`, *optional*):
        Pre-generated cumulative sequence lengths for Qwen positive prompt.
    negative_prompt_cu_seqlens (`torch.Tensor`, *optional*):
        Pre-generated cumulative sequence lengths for Qwen negative prompt.
    output_type (`str`, *optional*, defaults to `"pil"`):
        The output format of the generated image.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a [`KandinskyImagePipelineOutput`].
    callback_on_step_end (`Callable`, `PipelineCallback`, `MultiPipelineCallbacks`, *optional*):
        A function that is called at the end of each denoising step.
    callback_on_step_end_tensor_inputs (`List`, *optional*):
        The list of tensor inputs for the `callback_on_step_end` function.
    max_sequence_length (`int`, defaults to `1024`):
        The maximum sequence length for text and image qwen encoding. Must be less than 1024

Examples:

Returns:
    [`~KandinskyImagePipelineOutput`] or `tuple`:
        If `return_dict` is `True`, [`KandinskyImagePipelineOutput`] is returned, otherwise a `tuple` is
        returned where the first element is a list with the generated images.
Nr   )rR   r   rS   r   r   r1   r2   r3   r4   r   r   r   rU   r   F)rR   rS   r   rU   rT   rV         ? z9`negative_prompt` must have same length as `prompt`. Got z vs .)rT   )	rS   r   r   r   r   rV   rT   r   r0   r   )r   r   r   )totalT)	re   encoder_hidden_statespooled_projectionstimestepvisual_rope_postext_rope_posscale_factorsparse_paramsrc   )rc   r0   r1   r2   r3   r4   latent   r	   r   )r   )rS   );rl   r   r
   tensor_inputsrm   ro   r   rN   npargminabsr   r   rk   r5   rV   r   rv   rq   r   r   r   r;   set_timesteps	timestepsr   in_visual_dimr   rz   arangerL   r   maxitemorderr   progress_barrr   r   r   r   ry   r   steplocalspopupdateXLA_AVAILABLExm	mark_stepr6   reshaper   r   ru   rM   postprocessmaybe_free_model_hooksr   )*rO   rS   rR   r   r   r   r   r   r   r   r0   r1   r2   r3   r4   r   r   r   rc   r   r   rU   r   rT   rV   r   r   r   r   r   negative_text_rope_posr   r   num_warmup_stepsr   tr   pred_velocityuncond_pred_velocitycallback_kwargsr   callback_outputss*                                             r#   __call__Kandinsky5I2IPipeline.__call__6  s   b *-=?U,VWW1E1S1S.>em-7t-D-DE!HMM%**ME+11(C(C/'A/Q 3 	 	
 ?$"2"22 ,,		$JZJZ[JZQ3!qt?@JZ[\ME  .''  && *VS"9"9JXFJvt$<$<VJ+11!4J %HLHZHZ&;$7 I[ IE4E $&"$/3//EKEW?"3c&k"A^m]n_%V4 OPSTcPdOeeijmntjuivvwx  +2&&.#.C,?%# '  e+Jd 	$$%8$HNN,,	  $//66DD&&!$99!5 ' 

 LL6*LL#@#@@AEfULL"?"??1DVT
 %6%;%;%=%A%A%C%H%H%JSYZ *5 LL388:>>@EEGPVW 	 '  y>,?..BVBV,VV!)n%89\!),1>>;;q>00>S1ST !% 0 0")**U"3*<*?*?*F'9'<'<U'C%[[/$3"/!-"/ $ !1 
! &  &&,1L1X+/+;+;&-jj&7.I.L.LU.S+F+I+I%+P!)U!3(7&<%1&3$( ,< 
, f ) %9>]]qMq;r$rM=A^^=P=P!!Q$'GAq!Q@UAU@U4U,Vdi >Q >>1a$9%9$99: (3&(O?-3Xa[* @';D!Q'X$.229gFG)9)=)=>RTf)g&)9)=)=>RTf)g&2B2F2F57R3/ 3C2F2F57R3/ I**A9I/IqSTuX\XfXfXlXlNlpqNq '') =LLNu - :| !Q1&;';&;;< ("jj0Goo%777666$G ooaAq!Q7Goo22$777666	G  > >>GHHOOG,33E((44U4TEE 	##%8O+%88A \~ :9s   5#]	
%I].]
])r   r   r   rM   rJ   rK   rN   rL   )NNr@   N)NN)r   r@   NN)NNNNNNNN)   r@   r@   NNNN),__name__
__module____qualname____firstlineno____doc__model_cpu_offload_seqr   r   r   r   r   r   r   r   rH   rm   r   r   rz   rT   r   rV   r   r   Tensorr   r   	Generatorr   propertyr   r   r   r   r   EXAMPLE_DOC_STRINGfloatboolr   r   r
   r  __static_attributes____classcell__)rP   s   @r#   r.   r.   x   s   4 Mx1x x 9	x
 $x &x #x 3x> ,0&*#'$(I,S	I, "D(I, t#	I,
 !I, {{T!I,\ '+$(	"&d3i"& t#"& {{T!	"&P &'#'&*$([Kd3i[K ||[K  #	[K
 ![K t#[K {{T![KH  $($(#'+/ \tD %'$(&*DH'+C!C C "	C
 C C {{T!C t#C ??T%//%::TAC $C 
CJ $ $ # #   ]]_12 #'26! #% #,-DH'+2626;?;?15:>"' mq9B#'-e9!e9 d3ie9 tCy4/	e9
 d
e9 Tze9 !e9 e9  #Tze9 ??T%//%::TAe9 $e9 "LL4/e9 "LL4/e9 &+\\D%8e9 &+\\D%8e9  !<<$.!e9" %*LL4$7#e9$ 4Z%e9& 'e9( 'S$'79ILb9b'bcfjj)e9* -1I+e9, !-e9 3 e9r%   r.   )7r   typingr   numpyr   regexr(   rz   torch.nnr   r|   transformersr   r   r   r   	callbacksr
   r   rM   r   r   loadersr   modelsr   models.transformersr   
schedulersr   utilsr   r   r   r   utils.torch_utilsr   pipeline_utilsr   pipeline_outputr   torch_xla.core.xla_modelcore	xla_modelr  r  
get_loggerr  rw   r   r  r$   r*   r,   r.   rj   r%   r#   <module>r1     s         $ k k A D / # ? 9  . . 9 ))MM			H	% 
		H	% :	e9-/G e9r%   