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,
)


lowerCAmelCase			:							int     =logging.get_logger(__name__)  # pylint: disable=invalid-name

lowerCAmelCase			:							Union[str, Any]     ='\n    Examples:\n        ```py\n        >>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline\n        >>> import torch\n\n        >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior")\n        >>> pipe_prior.to("cuda")\n        >>> prompt = "red cat, 4k photo"\n        >>> out = pipe_prior(prompt)\n        >>> image_emb = out.image_embeds\n        >>> zero_image_emb = out.negative_image_embeds\n        >>> pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder")\n        >>> pipe.to("cuda")\n        >>> image = pipe(\n        ...     image_embeds=image_emb,\n        ...     negative_image_embeds=zero_image_emb,\n        ...     height=768,\n        ...     width=768,\n        ...     num_inference_steps=50,\n        ... ).images\n        >>> image[0].save("cat.png")\n        ```\n'







def 						A__  (					__A   , __A   , __A=8	):







				'''simple docstring'''
				_lowerCamelCase     :		List[Any] 						=						height // scale_factor**2
				if height % scale_factor**2 != 0:
								new_height += 1
				_lowerCamelCase     :		Dict 						=						width // scale_factor**2
				if width % scale_factor**2 != 0:
								new_width += 1
				return new_height * scale_factor, new_width * scale_factor




class 	__snake_case	( _A		):





						'''simple docstring'''






						def __init__(					self     :				Tuple    ,  _UpperCamelCase     :				UNetaDConditionModel    ,  _UpperCamelCase     :				DDPMScheduler    ,  _UpperCamelCase     :				VQModel    ,  )				->Union[str, Any]:


										"""simple docstring"""


										super().__init__()

										self.register_modules(
										    unet=UpperCamelCase__    ,  scheduler=UpperCamelCase__    ,  movq=UpperCamelCase__    ,  )
										_lowerCamelCase     :		Dict 						=						2 ** (len(self.movq.config.block_out_channels) - 1)



						def 	_SCREAMING_SNAKE_CASE    (					self     :				Tuple    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				Optional[int]    ,  _UpperCamelCase     :				Optional[int]    ,  _UpperCamelCase     :				Tuple    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				Optional[Any])				->Tuple:


										"""simple docstring"""


										if latents is None:
														_lowerCamelCase     :		Union[str, Any] 						=						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}""")
														_lowerCamelCase     :		Union[str, Any] 						=						latents.to(UpperCamelCase__)

										_lowerCamelCase     :		str 						=						latents * scheduler.init_noise_sigma
										return latents



						def 	_SCREAMING_SNAKE_CASE    (					self     :				int    ,  _UpperCamelCase     :				List[Any]=0)				->Union[str, Any]:


										"""simple docstring"""


										if is_accelerate_available():
														from accelerate import cpu_offload
										else:
														raise ImportError("""Please install accelerate via `pip install accelerate`""")

										_lowerCamelCase     :		List[Any] 						=						torch.device(F"""cuda:{gpu_id}""")

										_lowerCamelCase     :		List[Any] 						=						[
										    self.unet,
										    self.movq,
										]
										for cpu_offloaded_model in models:
														if cpu_offloaded_model is not None:
																		cpu_offload(UpperCamelCase__    ,  UpperCamelCase__)



						def 	_SCREAMING_SNAKE_CASE    (					self     :				List[Any]    ,  _UpperCamelCase     :				Dict=0)				->int:


										"""simple docstring"""


										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.""")

										_lowerCamelCase     :		List[str] 						=						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)

										_lowerCamelCase     :		str 						=						None
										for cpu_offloaded_model in [self.unet, self.movq]:
														_lowerCamelCase						,							_lowerCamelCase     :		int 						=						cpu_offload_with_hook(UpperCamelCase__    ,  UpperCamelCase__    ,  prev_module_hook=UpperCamelCase__)

										# We'll offload the last model manually.
										_lowerCamelCase     :		Tuple 						=						hook



						@property
						# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
						def 	_SCREAMING_SNAKE_CASE    (					self     :				int)				->Tuple:


										"""simple docstring"""


										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     :				Dict    ,  _UpperCamelCase     :				Union[torch.FloatTensor, List[torch.FloatTensor]]    ,  _UpperCamelCase     :				Union[torch.FloatTensor, List[torch.FloatTensor]]    ,  _UpperCamelCase     :				int = 512    ,  _UpperCamelCase     :				int = 512    ,  _UpperCamelCase     :				int = 100    ,  _UpperCamelCase     :				float = 4.0    ,  _UpperCamelCase     :				int = 1    ,  _UpperCamelCase     :				Optional[Union[torch.Generator, List[torch.Generator]]] = None    ,  _UpperCamelCase     :				Optional[torch.FloatTensor] = None    ,  _UpperCamelCase     :				Optional[str] = "pil"    ,  _UpperCamelCase     :				bool = True    ,  )				->int:


										"""simple docstring"""


										_lowerCamelCase     :		int 						=						self._execution_device

										_lowerCamelCase     :		Union[str, Any] 						=						guidance_scale > 1.0

										if isinstance(UpperCamelCase__    ,  UpperCamelCase__):
														_lowerCamelCase     :		List[Any] 						=						torch.cat(UpperCamelCase__    ,  dim=0)
										_lowerCamelCase     :		Dict 						=						image_embeds.shape[0] * num_images_per_prompt
										if isinstance(UpperCamelCase__    ,  UpperCamelCase__):
														_lowerCamelCase     :		Tuple 						=						torch.cat(UpperCamelCase__    ,  dim=0)

										if do_classifier_free_guidance:
														_lowerCamelCase     :		str 						=						image_embeds.repeat_interleave(UpperCamelCase__    ,  dim=0)
														_lowerCamelCase     :		int 						=						negative_image_embeds.repeat_interleave(UpperCamelCase__    ,  dim=0)

										_lowerCamelCase     :		Union[str, Any] 						=						torch.cat([negative_image_embeds, image_embeds]    ,  dim=0).to(dtype=self.unet.dtype    ,  device=UpperCamelCase__)

										self.scheduler.set_timesteps(UpperCamelCase__    ,  device=UpperCamelCase__)
										_lowerCamelCase     :		List[Any] 						=						self.scheduler.timesteps

										_lowerCamelCase     :		List[str] 						=						self.unet.config.in_channels

										_lowerCamelCase						,							_lowerCamelCase     :		List[Any] 						=						downscale_height_and_width(UpperCamelCase__    ,  UpperCamelCase__    ,  self.movq_scale_factor)

										# create initial latent
										_lowerCamelCase     :		int 						=						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
														_lowerCamelCase     :		Optional[Any] 						=						torch.cat([latents] * 2) if do_classifier_free_guidance else latents

														_lowerCamelCase     :		Optional[Any] 						=						{"""image_embeds""": image_embeds}
														_lowerCamelCase     :		List[Any] 						=						self.unet(
														    sample=UpperCamelCase__    ,  timestep=UpperCamelCase__    ,  encoder_hidden_states=UpperCamelCase__    ,  added_cond_kwargs=UpperCamelCase__    ,  return_dict=UpperCamelCase__    ,  )[0]

														if do_classifier_free_guidance:
																		_lowerCamelCase						,							_lowerCamelCase     :		Any 						=						noise_pred.split(latents.shape[1]    ,  dim=1)
																		_lowerCamelCase						,							_lowerCamelCase     :		Union[str, Any] 						=						noise_pred.chunk(2)
																		_lowerCamelCase						,							_lowerCamelCase     :		Optional[Any] 						=						variance_pred.chunk(2)
																		_lowerCamelCase     :		Tuple 						=						noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
																		_lowerCamelCase     :		Dict 						=						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"]
														):
																		_lowerCamelCase						,							_lowerCamelCase     :		Any 						=						noise_pred.split(latents.shape[1]    ,  dim=1)

														# compute the previous noisy sample x_t -> x_t-1
														_lowerCamelCase     :		Tuple 						=						self.scheduler.step(
														    UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    ,  generator=UpperCamelCase__    ,  )[0]
										# post-processing
										_lowerCamelCase     :		Tuple 						=						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"]:
														_lowerCamelCase     :		Tuple 						=						image * 0.5 + 0.5
														_lowerCamelCase     :		Union[str, Any] 						=						image.clamp(0    ,  1)
														_lowerCamelCase     :		str 						=						image.cpu().permute(0    ,  2    ,  3    ,  1).float().numpy()

										if output_type == "pil":
														_lowerCamelCase     :		Optional[int] 						=						self.numpy_to_pil(UpperCamelCase__)

										if not return_dict:
														return (image,)

										return ImagePipelineOutput(images=UpperCamelCase__)





from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
    import PIL


lowerCAmelCase			:							Tuple     =logging.get_logger(__name__)




class 	__snake_case	( __lowerCAmelCase		):





      '''simple docstring'''






      _snake_case    		=       ['pixel_values']



      def __init__(					self     :				Optional[Any]    ,  _UpperCamelCase     :				bool = True    ,  _UpperCamelCase     :				Dict[str, int] = None    ,  _UpperCamelCase     :				PILImageResampling = PIL.Image.BICUBIC    ,  _UpperCamelCase     :				bool = True    ,  _UpperCamelCase     :				Dict[str, int] = None    ,  _UpperCamelCase     :				Union[int, float] = 1 / 255    ,  _UpperCamelCase     :				bool = True    ,  _UpperCamelCase     :				bool = True    ,  _UpperCamelCase     :				Optional[Union[float, List[float]]] = None    ,  _UpperCamelCase     :				Optional[Union[float, List[float]]] = None    ,  **_UpperCamelCase     :				str    ,  )				->None:


          """simple docstring"""


          super().__init__(**_UpperCamelCase)
          _lowerCamelCase     :		Tuple 						=						size if size is not None else {"""height""": 256, """width""": 256}
          _lowerCamelCase     :		Optional[Any] 						=						get_size_dict(_UpperCamelCase)
          _lowerCamelCase     :		Any 						=						crop_size if crop_size is not None else {"""height""": 224, """width""": 224}
          _lowerCamelCase     :		Any 						=						get_size_dict(_UpperCamelCase    ,  param_name="""crop_size""")

          _lowerCamelCase     :		int 						=						do_resize
          _lowerCamelCase     :		int 						=						size
          _lowerCamelCase     :		Optional[int] 						=						resample
          _lowerCamelCase     :		int 						=						do_center_crop
          _lowerCamelCase     :		Optional[Any] 						=						crop_size
          _lowerCamelCase     :		Union[str, Any] 						=						do_rescale
          _lowerCamelCase     :		List[str] 						=						rescale_factor
          _lowerCamelCase     :		List[Any] 						=						do_normalize
          _lowerCamelCase     :		Dict 						=						image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
          _lowerCamelCase     :		int 						=						image_std if image_std is not None else IMAGENET_STANDARD_STD



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any    ,  _UpperCamelCase     :				np.ndarray    ,  _UpperCamelCase     :				Dict[str, int]    ,  _UpperCamelCase     :				PILImageResampling = PIL.Image.BICUBIC    ,  _UpperCamelCase     :				Optional[Union[str, ChannelDimension]] = None    ,  **_UpperCamelCase     :				Union[str, Any]    ,  )				->np.ndarray:


          """simple docstring"""


          _lowerCamelCase     :		Dict 						=						get_size_dict(_UpperCamelCase)
          if "height" not in size or "width" not in size:
              raise ValueError(F"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}""")
          return resize(
              _UpperCamelCase    ,  size=(size["""height"""], size["""width"""])    ,  resample=_UpperCamelCase    ,  data_format=_UpperCamelCase    ,  **_UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[str]    ,  _UpperCamelCase     :				np.ndarray    ,  _UpperCamelCase     :				Dict[str, int]    ,  _UpperCamelCase     :				Optional[Union[str, ChannelDimension]] = None    ,  **_UpperCamelCase     :				List[str]    ,  )				->np.ndarray:


          """simple docstring"""


          _lowerCamelCase     :		Union[str, Any] 						=						get_size_dict(_UpperCamelCase)
          if "height" not in size or "width" not in size:
              raise ValueError(F"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}""")
          return center_crop(_UpperCamelCase    ,  size=(size["""height"""], size["""width"""])    ,  data_format=_UpperCamelCase    ,  **_UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Union[str, Any]    ,  _UpperCamelCase     :				np.ndarray    ,  _UpperCamelCase     :				Union[int, float]    ,  _UpperCamelCase     :				Optional[Union[str, ChannelDimension]] = None    ,  **_UpperCamelCase     :				Union[str, Any]    ,  )				->str:


          """simple docstring"""


          return rescale(_UpperCamelCase    ,  scale=_UpperCamelCase    ,  data_format=_UpperCamelCase    ,  **_UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[Any]    ,  _UpperCamelCase     :				np.ndarray    ,  _UpperCamelCase     :				Union[float, List[float]]    ,  _UpperCamelCase     :				Union[float, List[float]]    ,  _UpperCamelCase     :				Optional[Union[str, ChannelDimension]] = None    ,  **_UpperCamelCase     :				Union[str, Any]    ,  )				->np.ndarray:


          """simple docstring"""


          return normalize(_UpperCamelCase    ,  mean=_UpperCamelCase    ,  std=_UpperCamelCase    ,  data_format=_UpperCamelCase    ,  **_UpperCamelCase)




      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[Any]    ,  _UpperCamelCase     :				ImageInput    ,  _UpperCamelCase     :				bool = None    ,  _UpperCamelCase     :				Dict[str, int] = None    ,  _UpperCamelCase     :				Tuple=None    ,  _UpperCamelCase     :				bool = None    ,  _UpperCamelCase     :				Dict[str, int] = None    ,  _UpperCamelCase     :				bool = None    ,  _UpperCamelCase     :				float = None    ,  _UpperCamelCase     :				bool = None    ,  _UpperCamelCase     :				Optional[Union[float, List[float]]] = None    ,  _UpperCamelCase     :				Optional[Union[float, List[float]]] = None    ,  _UpperCamelCase     :				Optional[Union[str, TensorType]] = None    ,  _UpperCamelCase     :				ChannelDimension = ChannelDimension.FIRST    ,  **_UpperCamelCase     :				List[Any]    ,  )				->PIL.Image.Image:


          """simple docstring"""


          _lowerCamelCase     :		Any 						=						do_resize if do_resize is not None else self.do_resize
          _lowerCamelCase     :		List[str] 						=						resample if resample is not None else self.resample
          _lowerCamelCase     :		Dict 						=						do_center_crop if do_center_crop is not None else self.do_center_crop
          _lowerCamelCase     :		Optional[Any] 						=						do_rescale if do_rescale is not None else self.do_rescale
          _lowerCamelCase     :		Dict 						=						rescale_factor if rescale_factor is not None else self.rescale_factor
          _lowerCamelCase     :		Dict 						=						do_normalize if do_normalize is not None else self.do_normalize
          _lowerCamelCase     :		int 						=						image_mean if image_mean is not None else self.image_mean
          _lowerCamelCase     :		Dict 						=						image_std if image_std is not None else self.image_std

          _lowerCamelCase     :		Optional[Any] 						=						size if size is not None else self.size
          _lowerCamelCase     :		Optional[int] 						=						get_size_dict(_UpperCamelCase)
          _lowerCamelCase     :		List[str] 						=						crop_size if crop_size is not None else self.crop_size
          _lowerCamelCase     :		Dict 						=						get_size_dict(_UpperCamelCase    ,  param_name="""crop_size""")

          _lowerCamelCase     :		int 						=						make_list_of_images(_UpperCamelCase)

          if not valid_images(_UpperCamelCase):
              raise ValueError(
                  """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
                  """torch.Tensor, tf.Tensor or jax.ndarray.""")

          if do_resize and size is None or resample is None:
              raise ValueError("""Size and resample must be specified if do_resize is True.""")

          if do_center_crop and crop_size is None:
              raise ValueError("""Crop size must be specified if do_center_crop is True.""")

          if do_rescale and rescale_factor is None:
              raise ValueError("""Rescale factor must be specified if do_rescale is True.""")

          if do_normalize and (image_mean is None or image_std is None):
              raise ValueError("""Image mean and std must be specified if do_normalize is True.""")

          # All transformations expect numpy arrays.
          _lowerCamelCase     :		Union[str, Any] 						=						[to_numpy_array(_UpperCamelCase) for image in images]

          if do_resize:
              _lowerCamelCase     :		Any 						=						[self.resize(image=_UpperCamelCase    ,  size=_UpperCamelCase    ,  resample=_UpperCamelCase) for image in images]

          if do_center_crop:
              _lowerCamelCase     :		str 						=						[self.center_crop(image=_UpperCamelCase    ,  size=_UpperCamelCase) for image in images]

          if do_rescale:
              _lowerCamelCase     :		Optional[int] 						=						[self.rescale(image=_UpperCamelCase    ,  scale=_UpperCamelCase) for image in images]

          if do_normalize:
              _lowerCamelCase     :		List[str] 						=						[self.normalize(image=_UpperCamelCase    ,  mean=_UpperCamelCase    ,  std=_UpperCamelCase) for image in images]

          _lowerCamelCase     :		List[str] 						=						[to_channel_dimension_format(_UpperCamelCase    ,  _UpperCamelCase) for image in images]

          _lowerCamelCase     :		int 						=						{"""pixel_values""": images}
          return BatchFeature(data=_UpperCamelCase    ,  tensor_type=_UpperCamelCase)




