




from typing import List, Optional, Union

import torch
from transformers import (
    XLMRobertaTokenizer,
)

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


SCREAMING_SNAKE_CASE__       :      Dict  =   logging.get_logger(__name__)  # pylint: disable=invalid-name

SCREAMING_SNAKE_CASE__       :      Dict  =   '\n    Examples:\n        ```py\n        >>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline\n        >>> import torch\n\n        >>> pipe_prior = KandinskyPriorPipeline.from_pretrained("kandinsky-community/Kandinsky-2-1-prior")\n        >>> pipe_prior.to("cuda")\n\n        >>> prompt = "red cat, 4k photo"\n        >>> out = pipe_prior(prompt)\n        >>> image_emb = out.image_embeds\n        >>> negative_image_emb = out.negative_image_embeds\n\n        >>> pipe = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1")\n        >>> pipe.to("cuda")\n\n        >>> image = pipe(\n        ...     prompt,\n        ...     image_embeds=image_emb,\n        ...     negative_image_embeds=negative_image_emb,\n        ...     height=768,\n        ...     width=768,\n        ...     num_inference_steps=100,\n        ... ).images\n\n        >>> image[0].save("cat.png")\n        ```\n'

def      A      (      _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE=8    )	-> Optional[int]:
       lowerCamelCase   :      Union[str, Any]			 =					h // scale_factor**2
       if h % scale_factor**2 != 0:
              new_h += 1
       lowerCamelCase   :      Any			 =					w // scale_factor**2
       if w % scale_factor**2 != 0:
              new_w += 1
       return new_h * scale_factor, new_w * scale_factor




class 		UpperCamelCase__	(lowerCAmelCase__     ):

    '''simple docstring'''



    def __init__(    self     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  )     ->      Any:
           super().__init__()

           self.register_modules(
               text_encoder=UpperCamelCase__     ,  tokenizer=UpperCamelCase__     ,  unet=UpperCamelCase__     ,  scheduler=UpperCamelCase__     ,  movq=UpperCamelCase__     ,  )
           lowerCamelCase   :      List[Any]			 =					2 ** (len(self.movq.config.block_out_channels     ) - 1)







    def     _lowercase     (    self     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     )     ->      Optional[Any]:
           if latents is None:
                  lowerCamelCase   :      Dict			 =					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   :      str			 =					latents.to(UpperCamelCase__     )

           lowerCamelCase   :      Dict			 =					latents * scheduler.init_noise_sigma
           return latents







    def     _lowercase     (    self     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__=None     ,  )     ->      Tuple:
           lowerCamelCase   :      int			 =					len(UpperCamelCase__     ) if isinstance(UpperCamelCase__     ,  UpperCamelCase__     ) else 1
           # get prompt text embeddings
           lowerCamelCase   :      Optional[int]			 =					self.tokenizer(
               UpperCamelCase__     ,  padding="max_length"     ,  truncation=UpperCamelCase__     ,  max_length=77     ,  return_attention_mask=UpperCamelCase__     ,  add_special_tokens=UpperCamelCase__     ,  return_tensors="pt"     ,  )

           lowerCamelCase   :      str			 =					text_inputs.input_ids
           lowerCamelCase   :      Dict			 =					self.tokenizer(UpperCamelCase__     ,  padding="longest"     ,  return_tensors="pt"     ).input_ids

           if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(UpperCamelCase__     ,  UpperCamelCase__     ):
                  lowerCamelCase   :      str			 =					self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]     )
                  logger.warning(
                      "The following part of your input was truncated because CLIP can only handle sequences up to"
                      F''' {self.tokenizer.model_max_length} tokens: {removed_text}'''     )

           lowerCamelCase   :      int			 =					text_input_ids.to(UpperCamelCase__     )
           lowerCamelCase   :      int			 =					text_inputs.attention_mask.to(UpperCamelCase__     )

           lowerCamelCase ,      lowerCamelCase   :      Union[str, Any]			 =					self.text_encoder(
               input_ids=UpperCamelCase__     ,  attention_mask=UpperCamelCase__     )

           lowerCamelCase   :      Any			 =					prompt_embeds.repeat_interleave(UpperCamelCase__     ,  dim=0     )
           lowerCamelCase   :      str			 =					text_encoder_hidden_states.repeat_interleave(UpperCamelCase__     ,  dim=0     )
           lowerCamelCase   :      List[str]			 =					text_mask.repeat_interleave(UpperCamelCase__     ,  dim=0     )

           if do_classifier_free_guidance:
                  lowerCamelCase   :      List[str]
                  if negative_prompt is None:
                         lowerCamelCase   :      Optional[int]			 =					[""] * batch_size
                  elif type(UpperCamelCase__     ) is not type(UpperCamelCase__     ):
                         raise TypeError(
                             F'''`negative_prompt` should be the same type to `prompt`, but got {type(UpperCamelCase__     )} !='''
                             F''' {type(UpperCamelCase__     )}.'''     )
                  elif isinstance(UpperCamelCase__     ,  UpperCamelCase__     ):
                         lowerCamelCase   :      Any			 =					[negative_prompt]
                  elif batch_size != len(UpperCamelCase__     ):
                         raise ValueError(
                             F'''`negative_prompt`: {negative_prompt} has batch size {len(UpperCamelCase__     )}, but `prompt`:'''
                             F''' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches'''
                             " the batch size of `prompt`."     )
                  else:
                         lowerCamelCase   :      int			 =					negative_prompt

                  lowerCamelCase   :      str			 =					self.tokenizer(
                      UpperCamelCase__     ,  padding="max_length"     ,  max_length=77     ,  truncation=UpperCamelCase__     ,  return_attention_mask=UpperCamelCase__     ,  add_special_tokens=UpperCamelCase__     ,  return_tensors="pt"     ,  )
                  lowerCamelCase   :      Optional[int]			 =					uncond_input.input_ids.to(UpperCamelCase__     )
                  lowerCamelCase   :      Any			 =					uncond_input.attention_mask.to(UpperCamelCase__     )

                  lowerCamelCase ,      lowerCamelCase   :      Any			 =					self.text_encoder(
                      input_ids=UpperCamelCase__     ,  attention_mask=UpperCamelCase__     )

                  # duplicate unconditional embeddings for each generation per prompt, using mps friendly method

                  lowerCamelCase   :      Tuple			 =					negative_prompt_embeds.shape[1]
                  lowerCamelCase   :      str			 =					negative_prompt_embeds.repeat(1     ,  UpperCamelCase__     )
                  lowerCamelCase   :      List[str]			 =					negative_prompt_embeds.view(batch_size * num_images_per_prompt     ,  UpperCamelCase__     )

                  lowerCamelCase   :      int			 =					uncond_text_encoder_hidden_states.shape[1]
                  lowerCamelCase   :      str			 =					uncond_text_encoder_hidden_states.repeat(1     ,  UpperCamelCase__     ,  1     )
                  lowerCamelCase   :      List[str]			 =					uncond_text_encoder_hidden_states.view(
                      batch_size * num_images_per_prompt     ,  UpperCamelCase__     ,  -1     )
                  lowerCamelCase   :      Any			 =					uncond_text_mask.repeat_interleave(UpperCamelCase__     ,  dim=0     )

                  # done duplicates

                  # For classifier free guidance, we need to do two forward passes.
                  # Here we concatenate the unconditional and text embeddings into a single batch
                  # to avoid doing two forward passes
                  lowerCamelCase   :      Optional[int]			 =					torch.cat([negative_prompt_embeds, prompt_embeds]     )
                  lowerCamelCase   :      Union[str, Any]			 =					torch.cat([uncond_text_encoder_hidden_states, text_encoder_hidden_states]     )

                  lowerCamelCase   :      List[Any]			 =					torch.cat([uncond_text_mask, text_mask]     )

           return prompt_embeds, text_encoder_hidden_states, text_mask







    def     _lowercase     (    self     ,  UpperCamelCase__=0     )     ->      Union[str, Any]:
           if is_accelerate_available():
                  from accelerate import cpu_offload
           else:
                  raise ImportError("Please install accelerate via `pip install accelerate`"     )

           lowerCamelCase   :      Union[str, Any]			 =					torch.device(F'''cuda:{gpu_id}'''     )

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







    def     _lowercase     (    self     ,  UpperCamelCase__=0     )     ->      Dict:
           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[Any]			 =					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.text_encoder, self.unet, self.movq]:
                  lowerCamelCase ,      lowerCamelCase   :      Union[str, Any]			 =					cpu_offload_with_hook(UpperCamelCase__     ,  UpperCamelCase__     ,  prev_module_hook=UpperCamelCase__     )

           if self.safety_checker is not None:
                  lowerCamelCase ,      lowerCamelCase   :      int			 =					cpu_offload_with_hook(self.safety_checker     ,  UpperCamelCase__     ,  prev_module_hook=UpperCamelCase__     )

           # We'll offload the last model manually.
           lowerCamelCase   :      Optional[Any]			 =					hook







    @property
    # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
    def     _lowercase     (    self     )     ->      List[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__     ,  UpperCamelCase__ = None     ,  UpperCamelCase__ = 512     ,  UpperCamelCase__ = 512     ,  UpperCamelCase__ = 100     ,  UpperCamelCase__ = 4.0     ,  UpperCamelCase__ = 1     ,  UpperCamelCase__ = None     ,  UpperCamelCase__ = None     ,  UpperCamelCase__ = "pil"     ,  UpperCamelCase__ = True     ,  )     ->      Union[str, Any]:

           if isinstance(UpperCamelCase__     ,  UpperCamelCase__     ):
                  lowerCamelCase   :      Optional[int]			 =					1
           elif isinstance(UpperCamelCase__     ,  UpperCamelCase__     ):
                  lowerCamelCase   :      Any			 =					len(UpperCamelCase__     )
           else:
                  raise ValueError(F'''`prompt` has to be of type `str` or `list` but is {type(UpperCamelCase__     )}'''     )

           lowerCamelCase   :      Tuple			 =					self._execution_device

           lowerCamelCase   :      int			 =					batch_size * num_images_per_prompt
           lowerCamelCase   :      Dict			 =					guidance_scale > 1.0

           lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase   :      List[Any]			 =					self._encode_prompt(
               UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     )

           if isinstance(UpperCamelCase__     ,  UpperCamelCase__     ):
                  lowerCamelCase   :      int			 =					torch.cat(UpperCamelCase__     ,  dim=0     )
           if isinstance(UpperCamelCase__     ,  UpperCamelCase__     ):
                  lowerCamelCase   :      Union[str, Any]			 =					torch.cat(UpperCamelCase__     ,  dim=0     )

           if do_classifier_free_guidance:
                  lowerCamelCase   :      List[str]			 =					image_embeds.repeat_interleave(UpperCamelCase__     ,  dim=0     )
                  lowerCamelCase   :      Optional[Any]			 =					negative_image_embeds.repeat_interleave(UpperCamelCase__     ,  dim=0     )

           lowerCamelCase   :      Optional[Any]			 =					torch.cat([negative_image_embeds, image_embeds]     ,  dim=0     ).to(
               dtype=prompt_embeds.dtype     ,  device=UpperCamelCase__     )

           self.scheduler.set_timesteps(UpperCamelCase__     ,  device=UpperCamelCase__     )
           lowerCamelCase   :      int			 =					self.scheduler.timesteps

           lowerCamelCase   :      Optional[Any]			 =					self.unet.config.in_channels

           lowerCamelCase ,      lowerCamelCase   :      List[str]			 =					get_new_h_w(UpperCamelCase__     ,  UpperCamelCase__     ,  self.movq_scale_factor     )

           # create initial latent
           lowerCamelCase   :      List[Any]			 =					self.prepare_latents(
               (batch_size, num_channels_latents, height, width)     ,  text_encoder_hidden_states.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   :      Dict			 =					torch.cat([latents] * 2     ) if do_classifier_free_guidance else latents

                  lowerCamelCase   :      str			 =					{"text_embeds": prompt_embeds, "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   :      List[str]			 =					noise_pred.split(latents.shape[1]     ,  dim=1     )
                         lowerCamelCase ,      lowerCamelCase   :      Tuple			 =					noise_pred.chunk(2     )
                         lowerCamelCase ,      lowerCamelCase   :      Tuple			 =					variance_pred.chunk(2     )
                         lowerCamelCase   :      str			 =					noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
                         lowerCamelCase   :      Optional[Any]			 =					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   :      Optional[int]			 =					self.scheduler.step(
                      UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  generator=UpperCamelCase__     ,  ).prev_sample
           # post-processing
           lowerCamelCase   :      Any			 =					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   :      List[str]			 =					image * 0.5 + 0.5
                  lowerCamelCase   :      Union[str, Any]			 =					image.clamp(0     ,  1     )
                  lowerCamelCase   :      Optional[Any]			 =					image.cpu().permute(0     ,  2     ,  3     ,  1     ).float().numpy()

           if output_type == "pil":
                  lowerCamelCase   :      Union[str, Any]			 =					self.numpy_to_pil(UpperCamelCase__     )

           if not return_dict:
                  return (image,)

           return ImagePipelineOutput(images=UpperCamelCase__     )













from typing import Any





def      lowerCAmelCase_ (       __a	,    __a	,    __a	,    __a	,    __a	,    )     ->		list:







  """simple docstring"""





  _validation(
      __a	,    __a	,    __a	,    __a	,    __a	,    )
  # Creates data structures and fill initial step
  lowerCamelCase__:      dict			  ={}
  lowerCamelCase__:      dict			  ={}
  for state in states_space:
    lowerCamelCase__:      Optional[Any]			  =observations_space[0]
    lowerCamelCase__:      List[Any]			  =(
        initial_probabilities[state] * emission_probabilities[state][observation]
    )
    lowerCamelCase__:      int			  =None

  # Fills the data structure with the probabilities of
  # different transitions and pointers to previous states
  for o in range(1	,    len(__a		)		):
    lowerCamelCase__:      Tuple			  =observations_space[o]
    lowerCamelCase__:      Optional[Any]			  =observations_space[o - 1]
    for state in states_space:
      # Calculates the argmax for probability function
      lowerCamelCase__:      Tuple			  =""
      lowerCamelCase__:      Optional[Any]			  =-1
      for k_state in states_space:
        lowerCamelCase__:      int			  =(
            probabilities[(k_state, prior_observation)]
            * transition_probabilities[k_state][state]
            * emission_probabilities[state][observation]
        )
        if probability > max_probability:
          lowerCamelCase__:      List[str]			  =probability
          lowerCamelCase__:      int			  =k_state

            # Update probabilities and pointers dicts
      lowerCamelCase__:      Any			  =(
          probabilities[(arg_max, prior_observation)]
          * transition_probabilities[arg_max][state]
          * emission_probabilities[state][observation]
      )

      lowerCamelCase__:      int			  =arg_max

    # The final observation
  lowerCamelCase__:      Any			  =observations_space[len(__a		) - 1]

  # argmax for given final observation
  lowerCamelCase__:      Optional[Any]			  =""
  lowerCamelCase__:      int			  =-1
  for k_state in states_space:
    lowerCamelCase__:      Tuple			  =probabilities[(k_state, final_observation)]
    if probability > max_probability:
      lowerCamelCase__:      List[Any]			  =probability
      lowerCamelCase__:      Dict			  =k_state
  lowerCamelCase__:      str			  =arg_max

  # Process pointers backwards
  lowerCamelCase__:      Union[str, Any]			  =last_state
  lowerCamelCase__:      List[str]			  =[]
  for o in range(len(__a		) - 1	,    -1	,    -1		):
    result.append(__a		)
    lowerCamelCase__:      Union[str, Any]			  =pointers[previous, observations_space[o]]
  result.reverse()

  return result





def      lowerCAmelCase_ (       __a	,    __a	,    __a	,    __a	,    __a	,    )     ->		None:







  """simple docstring"""





  _validate_not_empty(
      __a	,    __a	,    __a	,    __a	,    __a	,    )
  _validate_lists(__a	,    __a		)
  _validate_dicts(
      __a	,    __a	,    __a		)





def      lowerCAmelCase_ (       __a	,    __a	,    __a	,    __a	,    __a	,    )     ->		None:







  """simple docstring"""





  if not all(
      [
          observations_space,
          states_space,
          initial_probabilities,
          transition_probabilities,
          emission_probabilities,
      ]		):
    raise ValueError("There's an empty parameter"		)





def      lowerCAmelCase_ (       __a	,    __a		)     ->		None:







  """simple docstring"""





  _validate_list(__a	,    "observations_space"		)
  _validate_list(__a	,    "states_space"		)





def      lowerCAmelCase_ (       __a	,    __a		)     ->		None:







  """simple docstring"""





  if not isinstance(_object	,    __a		):
    lowerCamelCase__:      Tuple			  =F"""{var_name} must be a list"""
    raise ValueError(__a		)
  else:
    for x in _object:
      if not isinstance(__a	,    __a		):
        lowerCamelCase__:      str			  =F"""{var_name} must be a list of strings"""
        raise ValueError(__a		)





def      lowerCAmelCase_ (       __a	,    __a	,    __a	,    )     ->		None:







  """simple docstring"""





  _validate_dict(__a	,    "initial_probabilities"	,    __a		)
  _validate_nested_dict(__a	,    "transition_probabilities"		)
  _validate_nested_dict(__a	,    "emission_probabilities"		)





def      lowerCAmelCase_ (       __a	,    __a		)     ->		None:







  """simple docstring"""





  _validate_dict(_object	,    __a	,    __a		)
  for x in _object.values():
    _validate_dict(__a	,    __a	,    __a	,    __a		)





def      lowerCAmelCase_ (       __a	,    __a	,    __a	,    __a = False		)     ->		None:







  """simple docstring"""





  if not isinstance(_object	,    __a		):
    lowerCamelCase__:      Optional[int]			  =F"""{var_name} must be a dict"""
    raise ValueError(__a		)
  if not all(isinstance(__a	,    __a		) for x in _object		):
    lowerCamelCase__:      Tuple			  =F"""{var_name} all keys must be strings"""
    raise ValueError(__a		)
  if not all(isinstance(__a	,    __a		) for x in _object.values()		):
    lowerCamelCase__:      Dict			  ="nested dictionary " if nested else ""
    lowerCamelCase__:      List[str]			  =F"""{var_name} {nested_text}all values must be {value_type.__name__}"""
    raise ValueError(__a		)


if __name__ == "__main__":
     from doctest import testmod

     testmod()

