






from typing import List, Optional, Union

import torch

from ...models import UNetaDConditionModel, VQModel
from ...pipelines import DiffusionPipeline
from ...pipelines.pipeline_utils import ImagePipelineOutput
from ...schedulers import DDPMScheduler
from ...utils import (
    is_accelerate_available,
    is_accelerate_version,
    logging,
    randn_tensor,
    replace_example_docstring,
)


snake_case__      :	Optional[int]       	=      logging.get_logger(__name__)  # pylint: disable=invalid-name

snake_case__      :	Union[str, Any]       	=      """
    Examples:
        ```py
        >>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline
        >>> import torch

        >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\"kandinsky-community/kandinsky-2-2-prior\")
        >>> pipe_prior.to(\"cuda\")
        >>> prompt = \"red cat, 4k photo\"
        >>> out = pipe_prior(prompt)
        >>> image_emb = out.image_embeds
        >>> zero_image_emb = out.negative_image_embeds
        >>> pipe = KandinskyV22Pipeline.from_pretrained(\"kandinsky-community/kandinsky-2-2-decoder\")
        >>> pipe.to(\"cuda\")
        >>> image = pipe(
        ...     image_embeds=image_emb,
        ...     negative_image_embeds=zero_image_emb,
        ...     height=768,
        ...     width=768,
        ...     num_inference_steps=50,
        ... ).images
        >>> image[0].save(\"cat.png\")
        ```
"""






def     _snake_case    (__lowercase    ,      __lowercase    ,      __lowercase=8):
   UpperCamelCase_									=     height // scale_factor**2
   if height % scale_factor**2 != 0:
      new_height += 1
   UpperCamelCase_									=     width // scale_factor**2
   if width % scale_factor**2 != 0:
      new_width += 1
   return new_height * scale_factor, new_width * scale_factor


class 						_a       (  UpperCAmelCase__    ):






  """simple docstring"""





  def __init__(     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		)			-> Optional[Any]:
     super().__init__()

     self.register_modules(
         unet=_UpperCAmelCase		,		scheduler=_UpperCAmelCase		,		movq=_UpperCAmelCase		,		)
     UpperCamelCase_									=     2 ** (len(self.movq.config.block_out_channels ) - 1)




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> Any:
     if latents is None:
        UpperCamelCase_									=     randn_tensor(_UpperCAmelCase		,		generator=_UpperCAmelCase		,		device=_UpperCAmelCase		,		dtype=_UpperCAmelCase )
     else:
        if latents.shape != shape:
           raise ValueError(f"""Unexpected latents shape, got {latents.shape}, expected {shape}""" )
        UpperCamelCase_									=     latents.to(_UpperCAmelCase )

     UpperCamelCase_									=     latents * scheduler.init_noise_sigma
     return latents




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase=0 )			-> int:
     if is_accelerate_available():
        from accelerate import cpu_offload
     else:
        raise ImportError('Please install accelerate via `pip install accelerate`' )

     UpperCamelCase_									=     torch.device(f"""cuda:{gpu_id}""" )

     UpperCamelCase_									=     [
         self.unet,
         self.movq,
     ]
     for cpu_offloaded_model in models:
        if cpu_offloaded_model is not None:
           cpu_offload(_UpperCAmelCase		,		_UpperCAmelCase )




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase=0 )			-> str:
     if is_accelerate_available() and is_accelerate_version('>='		,		'0.17.0.dev0' ):
        from accelerate import cpu_offload_with_hook
     else:
        raise ImportError('`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.' )

     UpperCamelCase_									=     torch.device(f"""cuda:{gpu_id}""" )

     if self.device.type != "cpu":
        self.to('cpu'		,		silence_dtype_warnings=_UpperCAmelCase )
        torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

     UpperCamelCase_									=     None
     for cpu_offloaded_model in [self.unet, self.movq]:
        UpperCamelCase_		,      UpperCamelCase_									=     cpu_offload_with_hook(_UpperCAmelCase		,		_UpperCAmelCase		,		prev_module_hook=_UpperCAmelCase )

     # We'll offload the last model manually.
     UpperCamelCase_									=     hook




  @property
  # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
  def    _UpperCAmelCase     (     self )			-> Any:
     if not hasattr(self.unet		,		'_hf_hook' ):
        return self.device
     for module in self.unet.modules():
        if (
            hasattr(_UpperCAmelCase		,		'_hf_hook' )
            and hasattr(module._hf_hook		,		'execution_device' )
            and module._hf_hook.execution_device is not None
        ):
           return torch.device(module._hf_hook.execution_device )
     return self.device




  @torch.no_grad()
  @replace_example_docstring(_UpperCAmelCase )
  def __call__(     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase = 512		,		_UpperCAmelCase = 512		,		_UpperCAmelCase = 100		,		_UpperCAmelCase = 4.0		,		_UpperCAmelCase = 1		,		_UpperCAmelCase = None		,		_UpperCAmelCase = None		,		_UpperCAmelCase = "pil"		,		_UpperCAmelCase = True		,		)			-> Tuple:
     UpperCamelCase_									=     self._execution_device

     UpperCamelCase_									=     guidance_scale > 1.0

     if isinstance(_UpperCAmelCase		,		_UpperCAmelCase ):
        UpperCamelCase_									=     torch.cat(_UpperCAmelCase		,		dim=0 )
     UpperCamelCase_									=     image_embeds.shape[0] * num_images_per_prompt
     if isinstance(_UpperCAmelCase		,		_UpperCAmelCase ):
        UpperCamelCase_									=     torch.cat(_UpperCAmelCase		,		dim=0 )

     if do_classifier_free_guidance:
        UpperCamelCase_									=     image_embeds.repeat_interleave(_UpperCAmelCase		,		dim=0 )
        UpperCamelCase_									=     negative_image_embeds.repeat_interleave(_UpperCAmelCase		,		dim=0 )

     UpperCamelCase_									=     torch.cat([negative_image_embeds, image_embeds]		,		dim=0 ).to(dtype=self.unet.dtype		,		device=_UpperCAmelCase )

     self.scheduler.set_timesteps(_UpperCAmelCase		,		device=_UpperCAmelCase )
     UpperCamelCase_									=     self.scheduler.timesteps

     UpperCamelCase_									=     self.unet.config.in_channels

     UpperCamelCase_		,      UpperCamelCase_									=     downscale_height_and_width(_UpperCAmelCase		,		_UpperCAmelCase		,		self.movq_scale_factor )

     # create initial latent
     UpperCamelCase_									=     self.prepare_latents(
         (batch_size, num_channels_latents, height, width)		,		image_embeds.dtype		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		self.scheduler		,		)

     for i, t in enumerate(self.progress_bar(_UpperCAmelCase ) ):
        # expand the latents if we are doing classifier free guidance
        UpperCamelCase_									=     torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents

        UpperCamelCase_									=     {'image_embeds': image_embeds}
        UpperCamelCase_									=     self.unet(
            sample=_UpperCAmelCase		,		timestep=_UpperCAmelCase		,		encoder_hidden_states=_UpperCAmelCase		,		added_cond_kwargs=_UpperCAmelCase		,		return_dict=_UpperCAmelCase		,		)[0]

        if do_classifier_free_guidance:
           UpperCamelCase_		,      UpperCamelCase_									=     noise_pred.split(latents.shape[1]		,		dim=1 )
           UpperCamelCase_		,      UpperCamelCase_									=     noise_pred.chunk(2 )
           UpperCamelCase_		,      UpperCamelCase_									=     variance_pred.chunk(2 )
           UpperCamelCase_									=     noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
           UpperCamelCase_									=     torch.cat([noise_pred, variance_pred_text]		,		dim=1 )

        if not (
            hasattr(self.scheduler.config		,		'variance_type' )
            and self.scheduler.config.variance_type in ["learned", "learned_range"]
        ):
           UpperCamelCase_		,      UpperCamelCase_									=     noise_pred.split(latents.shape[1]		,		dim=1 )

        # compute the previous noisy sample x_t -> x_t-1
        UpperCamelCase_									=     self.scheduler.step(
            _UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		generator=_UpperCAmelCase		,		)[0]
     # post-processing
     UpperCamelCase_									=     self.movq.decode(_UpperCAmelCase		,		force_not_quantize=_UpperCAmelCase )['sample']

     if output_type not in ["pt", "np", "pil"]:
        raise ValueError(f"""Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}""" )

     if output_type in ["np", "pil"]:
        UpperCamelCase_									=     image * 0.5 + 0.5
        UpperCamelCase_									=     image.clamp(0		,		1 )
        UpperCamelCase_									=     image.cpu().permute(0		,		2		,		3		,		1 ).float().numpy()

     if output_type == "pil":
        UpperCamelCase_									=     self.numpy_to_pil(_UpperCAmelCase )

     if not return_dict:
        return (image,)

     return ImagePipelineOutput(images=_UpperCAmelCase )













import json
import os
import shutil
import tempfile
import unittest

import numpy as np

from transformers import BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer
from transformers.testing_utils import require_tokenizers, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
 from PIL import Image

 from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor


@require_tokenizers
@require_vision
class 						_a       (  unittest.TestCase    ):






  """simple docstring"""





  def    _UpperCAmelCase     (     self )			-> Dict:
     UpperCamelCase_									=     tempfile.mkdtemp()

     # fmt: off
     UpperCamelCase_									=     ['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest']
     # fmt: on
     UpperCamelCase_									=     os.path.join(self.tmpdirname		,		VOCAB_FILES_NAMES['vocab_file'] )
     with open(self.vocab_file		,		'w'		,		encoding='utf-8' ) as vocab_writer:
        vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) )

     UpperCamelCase_									=     {
         'do_resize': True,
         'size': {'height': 18, 'width': 18},
         'do_normalize': True,
         'image_mean': [0.5, 0.5, 0.5],
         'image_std': [0.5, 0.5, 0.5],
     }
     UpperCamelCase_									=     os.path.join(self.tmpdirname		,		_UpperCAmelCase )
     with open(self.image_processor_file		,		'w'		,		encoding='utf-8' ) as fp:
        json.dump(_UpperCAmelCase		,		_UpperCAmelCase )




  def    _UpperCAmelCase     (     self		,		**_UpperCAmelCase )			-> Optional[Any]:
     return BertTokenizer.from_pretrained(self.tmpdirname		,		**_UpperCAmelCase )




  def    _UpperCAmelCase     (     self		,		**_UpperCAmelCase )			-> Optional[int]:
     return ViTImageProcessor.from_pretrained(self.tmpdirname		,		**_UpperCAmelCase )




  def    _UpperCAmelCase     (     self )			-> Any:
     shutil.rmtree(self.tmpdirname )




  def    _UpperCAmelCase     (     self )			-> Optional[int]:
     UpperCamelCase_									=     [np.random.randint(255		,		size=(3, 30, 400)		,		dtype=np.uinta )]

     UpperCamelCase_									=     [Image.fromarray(np.moveaxis(_UpperCAmelCase		,		0		,		-1 ) ) for x in image_inputs]

     return image_inputs




  def    _UpperCAmelCase     (     self )			-> Tuple:
     UpperCamelCase_									=     self.get_tokenizer()
     UpperCamelCase_									=     self.get_image_processor()

     UpperCamelCase_									=     VisionTextDualEncoderProcessor(tokenizer=_UpperCAmelCase		,		image_processor=_UpperCAmelCase )

     processor.save_pretrained(self.tmpdirname )
     UpperCamelCase_									=     VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname )

     self.assertEqual(processor.tokenizer.get_vocab()		,		tokenizer.get_vocab() )
     self.assertIsInstance(processor.tokenizer		,		(BertTokenizer, BertTokenizerFast) )

     self.assertEqual(processor.image_processor.to_json_string()		,		image_processor.to_json_string() )
     self.assertIsInstance(processor.image_processor		,		_UpperCAmelCase )




  def    _UpperCAmelCase     (     self )			-> Optional[Any]:
     UpperCamelCase_									=     VisionTextDualEncoderProcessor(
         tokenizer=self.get_tokenizer()		,		image_processor=self.get_image_processor() )
     processor.save_pretrained(self.tmpdirname )

     UpperCamelCase_									=     self.get_tokenizer(bos_token='(BOS)'		,		eos_token='(EOS)' )
     UpperCamelCase_									=     self.get_image_processor(do_normalize=_UpperCAmelCase		,		padding_value=1.0 )

     UpperCamelCase_									=     VisionTextDualEncoderProcessor.from_pretrained(
         self.tmpdirname		,		bos_token='(BOS)'		,		eos_token='(EOS)'		,		do_normalize=_UpperCAmelCase		,		padding_value=1.0 )

     self.assertEqual(processor.tokenizer.get_vocab()		,		tokenizer_add_kwargs.get_vocab() )
     self.assertIsInstance(processor.tokenizer		,		(BertTokenizer, BertTokenizerFast) )

     self.assertEqual(processor.image_processor.to_json_string()		,		image_processor_add_kwargs.to_json_string() )
     self.assertIsInstance(processor.image_processor		,		_UpperCAmelCase )




  def    _UpperCAmelCase     (     self )			-> str:
     UpperCamelCase_									=     self.get_image_processor()
     UpperCamelCase_									=     self.get_tokenizer()

     UpperCamelCase_									=     VisionTextDualEncoderProcessor(tokenizer=_UpperCAmelCase		,		image_processor=_UpperCAmelCase )

     UpperCamelCase_									=     self.prepare_image_inputs()

     UpperCamelCase_									=     image_processor(_UpperCAmelCase		,		return_tensors='np' )
     UpperCamelCase_									=     processor(images=_UpperCAmelCase		,		return_tensors='np' )

     for key in input_feat_extract.keys():
        self.assertAlmostEqual(input_feat_extract[key].sum()		,		input_processor[key].sum()		,		delta=1e-2 )




  def    _UpperCAmelCase     (     self )			-> Optional[int]:
     UpperCamelCase_									=     self.get_image_processor()
     UpperCamelCase_									=     self.get_tokenizer()

     UpperCamelCase_									=     VisionTextDualEncoderProcessor(tokenizer=_UpperCAmelCase		,		image_processor=_UpperCAmelCase )

     UpperCamelCase_									=     'lower newer'

     UpperCamelCase_									=     processor(text=_UpperCAmelCase )

     UpperCamelCase_									=     tokenizer(_UpperCAmelCase )

     for key in encoded_tok.keys():
        self.assertListEqual(encoded_tok[key]		,		encoded_processor[key] )




  def    _UpperCAmelCase     (     self )			-> Any:
     UpperCamelCase_									=     self.get_image_processor()
     UpperCamelCase_									=     self.get_tokenizer()

     UpperCamelCase_									=     VisionTextDualEncoderProcessor(tokenizer=_UpperCAmelCase		,		image_processor=_UpperCAmelCase )

     UpperCamelCase_									=     'lower newer'
     UpperCamelCase_									=     self.prepare_image_inputs()

     UpperCamelCase_									=     processor(text=_UpperCAmelCase		,		images=_UpperCAmelCase )

     self.assertListEqual(list(inputs.keys() )		,		['input_ids', 'token_type_ids', 'attention_mask', 'pixel_values'] )

     # test if it raises when no input is passed
     with self.assertRaises(_UpperCAmelCase ):
        processor()




  def    _UpperCAmelCase     (     self )			-> Union[str, Any]:
     UpperCamelCase_									=     self.get_image_processor()
     UpperCamelCase_									=     self.get_tokenizer()

     UpperCamelCase_									=     VisionTextDualEncoderProcessor(tokenizer=_UpperCAmelCase		,		image_processor=_UpperCAmelCase )

     UpperCamelCase_									=     [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

     UpperCamelCase_									=     processor.batch_decode(_UpperCAmelCase )
     UpperCamelCase_									=     tokenizer.batch_decode(_UpperCAmelCase )

     self.assertListEqual(_UpperCAmelCase		,		_UpperCAmelCase )




  def    _UpperCAmelCase     (     self )			-> List[Any]:
     UpperCamelCase_									=     self.get_image_processor()
     UpperCamelCase_									=     self.get_tokenizer()

     UpperCamelCase_									=     VisionTextDualEncoderProcessor(tokenizer=_UpperCAmelCase		,		image_processor=_UpperCAmelCase )

     UpperCamelCase_									=     'lower newer'
     UpperCamelCase_									=     self.prepare_image_inputs()

     UpperCamelCase_									=     processor(text=_UpperCAmelCase		,		images=_UpperCAmelCase )

     self.assertListEqual(list(inputs.keys() )		,		processor.model_input_names )





