

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


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

__a  :Tuple										=       """
    Examples:
        ```py
        >>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline
        >>> import torch

        >>> pipe_prior = KandinskyPriorPipeline.from_pretrained(\"kandinsky-community/Kandinsky-2-1-prior\")
        >>> pipe_prior.to(\"cuda\")

        >>> prompt = \"red cat, 4k photo\"
        >>> out = pipe_prior(prompt)
        >>> image_emb = out.image_embeds
        >>> negative_image_emb = out.negative_image_embeds

        >>> pipe = KandinskyPipeline.from_pretrained(\"kandinsky-community/kandinsky-2-1\")
        >>> pipe.to(\"cuda\")

        >>> image = pipe(
        ...     prompt,
        ...     image_embeds=image_emb,
        ...     negative_image_embeds=negative_image_emb,
        ...     height=768,
        ...     width=768,
        ...     num_inference_steps=100,
        ... ).images

        >>> image[0].save(\"cat.png\")
        ```
"""

def 	__snake_case		(      __UpperCamelCase     :      int							,__UpperCamelCase     :      List[Any]							,__UpperCamelCase     :      Optional[int]=8  ):



      """simple docstring"""

      A_		   =							h // scale_factor**2
      if h % scale_factor**2 != 0:
            new_h += 1
      A_		   =							w // scale_factor**2
      if w % scale_factor**2 != 0:
            new_w += 1
      return new_h * scale_factor, new_w * scale_factor



class    _a (       snake_case__							):




      """simple docstring"""

      def __init__(					self				:							int		,       UpperCAmelCase				:							MultilingualCLIP		,       UpperCAmelCase				:							XLMRobertaTokenizer		,       UpperCAmelCase				:							UNetaDConditionModel		,       UpperCAmelCase				:							Union[DDIMScheduler, DDPMScheduler]		,       UpperCAmelCase				:							VQModel		,       ):
            super().__init__()

            self.register_modules(
                text_encoder=UpperCAmelCase		,       tokenizer=UpperCAmelCase		,       unet=UpperCAmelCase		,       scheduler=UpperCAmelCase		,       movq=UpperCAmelCase		,       )
            A_		   =							2 ** (len(self.movq.config.block_out_channels     ) - 1)





      def 						__A     (					self				:							Optional[Any]		,       UpperCAmelCase				:							List[str]		,       UpperCAmelCase				:							Optional[Any]		,       UpperCAmelCase				:							int		,       UpperCAmelCase				:							Any		,       UpperCAmelCase				:							List[str]		,       UpperCAmelCase				:							Optional[int]     ):
            if latents is None:
                  A_		   =							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}'''     )
                  A_		   =							latents.to(UpperCAmelCase     )

            A_		   =							latents * scheduler.init_noise_sigma
            return latents





      def 						__A     (					self				:							str		,       UpperCAmelCase				:							Optional[Any]		,       UpperCAmelCase				:							List[Any]		,       UpperCAmelCase				:							str		,       UpperCAmelCase				:							Any		,       UpperCAmelCase				:							int=None		,       ):
            A_		   =							len(UpperCAmelCase     ) if isinstance(UpperCAmelCase		,       UpperCAmelCase     ) else 1
            # get prompt text embeddings
            A_		   =							self.tokenizer(
                UpperCAmelCase		,       padding="max_length"		,       truncation=UpperCAmelCase		,       max_length=77		,       return_attention_mask=UpperCAmelCase		,       add_special_tokens=UpperCAmelCase		,       return_tensors="pt"		,       )

            A_		   =							text_inputs.input_ids
            A_		   =							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     ):
                  A_		   =							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}'''     )

            A_		   =							text_input_ids.to(UpperCAmelCase     )
            A_		   =							text_inputs.attention_mask.to(UpperCAmelCase     )

            A_		   =							self.text_encoder(
                input_ids=UpperCAmelCase		,       attention_mask=UpperCAmelCase     )

            A_		   =							prompt_embeds.repeat_interleave(UpperCAmelCase		,       dim=0     )
            A_		   =							text_encoder_hidden_states.repeat_interleave(UpperCAmelCase		,       dim=0     )
            A_		   =							text_mask.repeat_interleave(UpperCAmelCase		,       dim=0     )

            if do_classifier_free_guidance:
                  A_		   =							42
                  if negative_prompt is None:
                        A_		   =							[""] * 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     ):
                        A_		   =							[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:
                        A_		   =							negative_prompt

                  A_		   =							self.tokenizer(
                      UpperCAmelCase		,       padding="max_length"		,       max_length=77		,       truncation=UpperCAmelCase		,       return_attention_mask=UpperCAmelCase		,       add_special_tokens=UpperCAmelCase		,       return_tensors="pt"		,       )
                  A_		   =							uncond_input.input_ids.to(UpperCAmelCase     )
                  A_		   =							uncond_input.attention_mask.to(UpperCAmelCase     )

                  A_		   =							self.text_encoder(
                      input_ids=UpperCAmelCase		,       attention_mask=UpperCAmelCase     )

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

                  A_		   =							negative_prompt_embeds.shape[1]
                  A_		   =							negative_prompt_embeds.repeat(1		,       UpperCAmelCase     )
                  A_		   =							negative_prompt_embeds.view(batch_size * num_images_per_prompt		,       UpperCAmelCase     )

                  A_		   =							uncond_text_encoder_hidden_states.shape[1]
                  A_		   =							uncond_text_encoder_hidden_states.repeat(1		,       UpperCAmelCase		,       1     )
                  A_		   =							uncond_text_encoder_hidden_states.view(
                      batch_size * num_images_per_prompt		,       UpperCAmelCase		,       -1     )
                  A_		   =							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
                  A_		   =							torch.cat([negative_prompt_embeds, prompt_embeds]     )
                  A_		   =							torch.cat([uncond_text_encoder_hidden_states, text_encoder_hidden_states]     )

                  A_		   =							torch.cat([uncond_text_mask, text_mask]     )

            return prompt_embeds, text_encoder_hidden_states, text_mask





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

            A_		   =							torch.device(f'''cuda:{gpu_id}'''     )

            A_		   =							[
                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 						__A     (					self				:							str		,       UpperCAmelCase				:							Any=0     ):
            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."     )

            A_		   =							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)

            A_		   =							None
            for cpu_offloaded_model in [self.text_encoder, self.unet, self.movq]:
                  A_		   =							cpu_offload_with_hook(UpperCAmelCase		,       UpperCAmelCase		,       prev_module_hook=UpperCAmelCase     )

            if self.safety_checker is not None:
                  A_		   =							cpu_offload_with_hook(self.safety_checker		,       UpperCAmelCase		,       prev_module_hook=UpperCAmelCase     )

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





      @property
      # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
      def 						__A     (					self				:							Optional[int]     ):
            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				:							str		,       UpperCAmelCase				:							Union[str, List[str]]		,       UpperCAmelCase				:							Union[torch.FloatTensor, List[torch.FloatTensor]]		,       UpperCAmelCase				:							Union[torch.FloatTensor, List[torch.FloatTensor]]		,       UpperCAmelCase				:							Optional[Union[str, List[str]]] = None		,       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		,       ):

            if isinstance(UpperCAmelCase		,       UpperCAmelCase     ):
                  A_		   =							1
            elif isinstance(UpperCAmelCase		,       UpperCAmelCase     ):
                  A_		   =							len(UpperCAmelCase     )
            else:
                  raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(UpperCAmelCase     )}'''     )

            A_		   =							self._execution_device

            A_		   =							batch_size * num_images_per_prompt
            A_		   =							guidance_scale > 1.0

            A_		   =							self._encode_prompt(
                UpperCAmelCase		,       UpperCAmelCase		,       UpperCAmelCase		,       UpperCAmelCase		,       UpperCAmelCase     )

            if isinstance(UpperCAmelCase		,       UpperCAmelCase     ):
                  A_		   =							torch.cat(UpperCAmelCase		,       dim=0     )
            if isinstance(UpperCAmelCase		,       UpperCAmelCase     ):
                  A_		   =							torch.cat(UpperCAmelCase		,       dim=0     )

            if do_classifier_free_guidance:
                  A_		   =							image_embeds.repeat_interleave(UpperCAmelCase		,       dim=0     )
                  A_		   =							negative_image_embeds.repeat_interleave(UpperCAmelCase		,       dim=0     )

            A_		   =							torch.cat([negative_image_embeds, image_embeds]		,       dim=0     ).to(
                dtype=prompt_embeds.dtype		,       device=UpperCAmelCase     )

            self.scheduler.set_timesteps(UpperCAmelCase		,       device=UpperCAmelCase     )
            A_		   =							self.scheduler.timesteps

            A_		   =							self.unet.config.in_channels

            A_		   =							get_new_h_w(UpperCAmelCase		,       UpperCAmelCase		,       self.movq_scale_factor     )

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

                  A_		   =							{"text_embeds": prompt_embeds, "image_embeds": image_embeds}
                  A_		   =							self.unet(
                      sample=UpperCAmelCase		,       timestep=UpperCAmelCase		,       encoder_hidden_states=UpperCAmelCase		,       added_cond_kwargs=UpperCAmelCase		,       return_dict=UpperCAmelCase		,       )[0]

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

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

            if output_type == "pil":
                  A_		   =							self.numpy_to_pil(UpperCAmelCase     )

            if not return_dict:
                  return (image,)

            return ImagePipelineOutput(images=UpperCAmelCase     )
'''simple docstring'''




import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_distilbert import DistilBertTokenizer


__SCREAMING_SNAKE_CASE       :     str     =  logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE       :     str     =  {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

__SCREAMING_SNAKE_CASE       :     Dict     =  {
    """vocab_file""": {
        """distilbert-base-uncased""": """https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt""",
        """distilbert-base-uncased-distilled-squad""": (
            """https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt"""
        ),
        """distilbert-base-cased""": """https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt""",
        """distilbert-base-cased-distilled-squad""": (
            """https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt"""
        ),
        """distilbert-base-german-cased""": """https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt""",
        """distilbert-base-multilingual-cased""": (
            """https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt"""
        ),
    },
    """tokenizer_file""": {
        """distilbert-base-uncased""": """https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json""",
        """distilbert-base-uncased-distilled-squad""": (
            """https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json"""
        ),
        """distilbert-base-cased""": """https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json""",
        """distilbert-base-cased-distilled-squad""": (
            """https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json"""
        ),
        """distilbert-base-german-cased""": (
            """https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json"""
        ),
        """distilbert-base-multilingual-cased""": (
            """https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json"""
        ),
    },
}

__SCREAMING_SNAKE_CASE       :     Optional[Any]     =  {
    """distilbert-base-uncased""": 512,
    """distilbert-base-uncased-distilled-squad""": 512,
    """distilbert-base-cased""": 512,
    """distilbert-base-cased-distilled-squad""": 512,
    """distilbert-base-german-cased""": 512,
    """distilbert-base-multilingual-cased""": 512,
}


__SCREAMING_SNAKE_CASE       :     List[Any]     =  {
    """distilbert-base-uncased""": {"""do_lower_case""": True},
    """distilbert-base-uncased-distilled-squad""": {"""do_lower_case""": True},
    """distilbert-base-cased""": {"""do_lower_case""": False},
    """distilbert-base-cased-distilled-squad""": {"""do_lower_case""": False},
    """distilbert-base-german-cased""": {"""do_lower_case""": False},
    """distilbert-base-multilingual-cased""": {"""do_lower_case""": False},
}

class 	lowerCamelCase_	(snake_case__    ):
      '''simple docstring'''

      __UpperCamelCase:  Union[str, Any]						=						VOCAB_FILES_NAMES
      __UpperCamelCase:  str						=						PRETRAINED_VOCAB_FILES_MAP
      __UpperCamelCase:  Optional[Any]						=						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      __UpperCamelCase:  Any						=						PRETRAINED_INIT_CONFIGURATION
      __UpperCamelCase:  str						=						["input_ids", "attention_mask"]
      __UpperCamelCase:  List[str]						=						DistilBertTokenizer




      def __init__(       self						:				str						,					A						:				int=None						,					A						:				Tuple=None						,					A						:				Tuple=True						,					A						:				Dict="[UNK]"						,					A						:				List[Any]="[SEP]"						,					A						:				Optional[Any]="[PAD]"						,					A						:				Dict="[CLS]"						,					A						:				Tuple="[MASK]"						,					A						:				str=True						,					A						:				Dict=None						,					**A						:				List[Any]						,					):
             super().__init__(
                 A						,					tokenizer_file=A						,					do_lower_case=A						,					unk_token=A						,					sep_token=A						,					pad_token=A						,					cls_token=A						,					mask_token=A						,					tokenize_chinese_chars=A						,					strip_accents=A						,					**A						,					)

             _UpperCAmelCase     :  str							      =    json.loads(self.backend_tokenizer.normalizer.__getstate__()       )
             if (
                 normalizer_state.get("lowercase"						,					A       ) != do_lower_case
                 or normalizer_state.get("strip_accents"						,					A       ) != strip_accents
                 or normalizer_state.get("handle_chinese_chars"						,					A       ) != tokenize_chinese_chars
             ):
                    _UpperCAmelCase     :  Dict							      =    getattr(A						,					normalizer_state.pop("type"       )       )
                    _UpperCAmelCase     :  int							      =    do_lower_case
                    _UpperCAmelCase     :  Optional[int]							      =    strip_accents
                    _UpperCAmelCase     :  str							      =    tokenize_chinese_chars
                    _UpperCAmelCase     :  List[Any]							      =    normalizer_class(**A       )

             _UpperCAmelCase     :  Dict							      =    do_lower_case




      def      _A	(       self						:				List[Any]						,					A						:				Tuple						,					A						:				Any=None       ):
             _UpperCAmelCase     :  Optional[int]							      =    [self.cls_token_id] + token_ids_a + [self.sep_token_id]

             if token_ids_a:
                    output += token_ids_a + [self.sep_token_id]

             return output




      def      _A	(       self						:				int						,					A						:				List[int]						,					A						:				Optional[List[int]] = None       ):
             _UpperCAmelCase     :  Any							      =    [self.sep_token_id]
             _UpperCAmelCase     :  str							      =    [self.cls_token_id]
             if token_ids_a is None:
                    return len(cls + token_ids_a + sep       ) * [0]
             return len(cls + token_ids_a + sep       ) * [0] + len(token_ids_a + sep       ) * [1]





      def      _A	(       self						:				Dict						,					A						:				str						,					A						:				Optional[str] = None       ):
             _UpperCAmelCase     :  Any							      =    self._tokenizer.model.save(A						,					name=A       )
             return tuple(A       )

