






import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, BatchEncoding, PreTrainedTokenizer
from ...utils import logging


__UpperCAmelCase     							=						logging.get_logger(__name__)

__UpperCAmelCase     							=						'▁'

__UpperCAmelCase     							=						{'vocab_file': 'sentencepiece.bpe.model'}

__UpperCAmelCase     							=						{
    'vocab_file': {
        'facebook/mbart-large-en-ro': (
            'https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model'
        ),
        'facebook/mbart-large-cc25': (
            'https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model'
        ),
    }
}

__UpperCAmelCase     							=						{
    'facebook/mbart-large-en-ro': 1024,
    'facebook/mbart-large-cc25': 1024,
}

# fmt: off
__UpperCAmelCase     							=						['ar_AR', 'cs_CZ', 'de_DE', 'en_XX', 'es_XX', 'et_EE', 'fi_FI', 'fr_XX', 'gu_IN', 'hi_IN', 'it_IT', 'ja_XX', 'kk_KZ', 'ko_KR', 'lt_LT', 'lv_LV', 'my_MM', 'ne_NP', 'nl_XX', 'ro_RO', 'ru_RU', 'si_LK', 'tr_TR', 'vi_VN', 'zh_CN']



class   lowerCamelCase				(_snake_case      ):







  '''simple docstring'''



  _snake_case   :   List[Any]							=  VOCAB_FILES_NAMES
  _snake_case   :   Union[str, Any]							=  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  _snake_case   :   Tuple							=  PRETRAINED_VOCAB_FILES_MAP
  _snake_case   :   Any							=  ['''input_ids''', '''attention_mask''']

  _snake_case   :   List[int]							=  []
  _snake_case   :   List[int]							=  []



  def __init__(							self    ,		_UpperCamelCase    ,		_UpperCamelCase="<s>"    ,		_UpperCamelCase="</s>"    ,		_UpperCamelCase="</s>"    ,		_UpperCamelCase="<s>"    ,		_UpperCamelCase="<unk>"    ,		_UpperCamelCase="<pad>"    ,		_UpperCamelCase="<mask>"    ,		_UpperCamelCase=None    ,		_UpperCamelCase=None    ,		_UpperCamelCase=None    ,		_UpperCamelCase = None    ,		_UpperCamelCase=None    ,		**_UpperCamelCase    ,		)     ->							List[str]:
       # Mask token behave like a normal word, i.e. include the space before it
       UpperCAmelCase_   :				List[Any]						=						AddedToken(_UpperCamelCase    ,		lstrip=_UpperCamelCase    ,		rstrip=_UpperCamelCase       ) if isinstance(_UpperCamelCase    ,		_UpperCamelCase       ) else mask_token

       UpperCAmelCase_   :				Optional[int]						=						{} if sp_model_kwargs is None else sp_model_kwargs

       super().__init__(
           bos_token=_UpperCamelCase    ,		eos_token=_UpperCamelCase    ,		unk_token=_UpperCamelCase    ,		sep_token=_UpperCamelCase    ,		cls_token=_UpperCamelCase    ,		pad_token=_UpperCamelCase    ,		mask_token=_UpperCamelCase    ,		tokenizer_file=_UpperCamelCase    ,		src_lang=_UpperCamelCase    ,		tgt_lang=_UpperCamelCase    ,		additional_special_tokens=_UpperCamelCase    ,		sp_model_kwargs=self.sp_model_kwargs    ,		**_UpperCamelCase    ,		)

       UpperCAmelCase_   :				Any						=						spm.SentencePieceProcessor(**self.sp_model_kwargs       )
       self.sp_model.Load(str(_UpperCamelCase       )       )
       UpperCAmelCase_   :				int						=						vocab_file

       # Original fairseq vocab and spm vocab must be "aligned":
       # Vocab    |    0    |    1    |   2    |    3    |  4  |  5  |  6  |   7   |   8   |  9
       # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
       # fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's'   | '▁de' | '-'
       # spm      | '<unk>' | '<s>'   | '</s>' | ','     | '.' | '▁' | 's' | '▁de' | '-'   | '▁a'

       # Mimic fairseq token-to-id alignment for the first 4 token
       UpperCAmelCase_   :				List[str]						=						{'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3}

       # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
       UpperCAmelCase_   :				str						=						1

       UpperCAmelCase_   :				Optional[int]						=						len(self.sp_model       )
       UpperCAmelCase_   :				str						=						{
           code: self.sp_model_size + i + self.fairseq_offset for i, code in enumerate(_UpperCamelCase       )
       }
       UpperCAmelCase_   :				int						=						{v: k for k, v in self.lang_code_to_id.items()}
       UpperCAmelCase_   :				List[Any]						=						len(self.sp_model       ) + len(self.lang_code_to_id       ) + self.fairseq_offset

       self.fairseq_tokens_to_ids.update(self.lang_code_to_id       )
       UpperCAmelCase_   :				Tuple						=						{v: k for k, v in self.fairseq_tokens_to_ids.items()}
       UpperCAmelCase_   :				int						=						list(self.lang_code_to_id.keys()       )

       if additional_special_tokens is not None:
            # Only add those special tokens if they are not already there.
            self._additional_special_tokens.extend(
                [t for t in additional_special_tokens if t not in self._additional_special_tokens]       )

       UpperCAmelCase_   :				Any						=						src_lang if src_lang is not None else 'en_XX'
       UpperCAmelCase_   :				Any						=						self.lang_code_to_id[self._src_lang]
       UpperCAmelCase_   :				Dict						=						tgt_lang
       self.set_src_lang_special_tokens(self._src_lang       )



  def __getstate__(							self       )     ->							Dict:
       UpperCAmelCase_   :				Optional[int]						=						self.__dict__.copy()
       UpperCAmelCase_   :				str						=						None
       UpperCAmelCase_   :				str						=						self.sp_model.serialized_model_proto()
       return state



  def __setstate__(							self    ,		_UpperCamelCase       )     ->							Dict:
       UpperCAmelCase_   :				Union[str, Any]						=						d

       # for backward compatibility
       if not hasattr(self    ,		'sp_model_kwargs'       ):
            UpperCAmelCase_   :				Any						=						{}

       UpperCAmelCase_   :				Any						=						spm.SentencePieceProcessor(**self.sp_model_kwargs       )
       self.sp_model.LoadFromSerializedProto(self.sp_model_proto       )



  @property
  def  __UpperCAmelCase      (							self       )     ->							Tuple:
       return len(self.sp_model       ) + len(self.lang_code_to_id       ) + self.fairseq_offset + 1  # Plus 1 for the mask token



  @property
  def  __UpperCAmelCase      (							self       )     ->							str:
       return self._src_lang



  @src_lang.setter
  def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							None:
       UpperCAmelCase_   :				Tuple						=						new_src_lang
       self.set_src_lang_special_tokens(self._src_lang       )



  def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase = None    ,		_UpperCamelCase = False       )     ->							List[int]:

       if already_has_special_tokens:
            return super().get_special_tokens_mask(
                token_ids_a=_UpperCamelCase    ,		token_ids_a=_UpperCamelCase    ,		already_has_special_tokens=_UpperCamelCase       )

       UpperCAmelCase_   :				Tuple						=						[1] * len(self.prefix_tokens       )
       UpperCAmelCase_   :				Dict						=						[1] * len(self.suffix_tokens       )
       if token_ids_a is None:
            return prefix_ones + ([0] * len(_UpperCamelCase       )) + suffix_ones
       return prefix_ones + ([0] * len(_UpperCamelCase       )) + ([0] * len(_UpperCamelCase       )) + suffix_ones



  def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase = None       )     ->							List[int]:
       if token_ids_a is None:
            return self.prefix_tokens + token_ids_a + self.suffix_tokens
       # We don't expect to process pairs, but leave the pair logic for API consistency
       return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens



  def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase = None       )     ->							List[int]:
       UpperCAmelCase_   :				int						=						[self.sep_token_id]
       UpperCAmelCase_   :				Optional[int]						=						[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 + sep + token_ids_a + sep       ) * [0]



  def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		**_UpperCamelCase       )     ->							int:
       if src_lang is None or tgt_lang is None:
            raise ValueError('Translation requires a `src_lang` and a `tgt_lang` for this model'       )
       UpperCAmelCase_   :				Optional[int]						=						src_lang
       UpperCAmelCase_   :				Dict						=						self(_UpperCamelCase    ,		add_special_tokens=_UpperCamelCase    ,		return_tensors=_UpperCamelCase    ,		**_UpperCamelCase       )
       UpperCAmelCase_   :				int						=						self.convert_tokens_to_ids(_UpperCamelCase       )
       UpperCAmelCase_   :				Any						=						tgt_lang_id
       return inputs



  def  __UpperCAmelCase      (							self       )     ->							str:
       UpperCAmelCase_   :				str						=						{self.convert_ids_to_tokens(_UpperCamelCase       ): i for i in range(self.vocab_size       )}
       vocab.update(self.added_tokens_encoder       )
       return vocab



  def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							List[str]:
       return self.sp_model.encode(_UpperCamelCase    ,		out_type=_UpperCamelCase       )



  def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Tuple:
       if token in self.fairseq_tokens_to_ids:
            return self.fairseq_tokens_to_ids[token]
       UpperCAmelCase_   :				Dict						=						self.sp_model.PieceToId(_UpperCamelCase       )

       # Need to return unknown token if the SP model returned 0
       return spm_id + self.fairseq_offset if spm_id else self.unk_token_id



  def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							str:
       if index in self.fairseq_ids_to_tokens:
            return self.fairseq_ids_to_tokens[index]
       return self.sp_model.IdToPiece(index - self.fairseq_offset       )



  def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Optional[int]:
       UpperCAmelCase_   :				Optional[int]						=						''.join(_UpperCamelCase       ).replace(_UpperCamelCase    ,		' '       ).strip()
       return out_string



  def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase = None       )     ->							Tuple[str]:
       if not os.path.isdir(_UpperCamelCase       ):
            logger.error(f"Vocabulary path ({save_directory}) should be a directory"       )
            return
       UpperCAmelCase_   :				List[Any]						=						os.path.join(
           _UpperCamelCase    ,		(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']       )

       if os.path.abspath(self.vocab_file       ) != os.path.abspath(_UpperCamelCase       ) and os.path.isfile(self.vocab_file       ):
            copyfile(self.vocab_file    ,		_UpperCamelCase       )
       elif not os.path.isfile(self.vocab_file       ):
            with open(_UpperCamelCase    ,		'wb'       ) as fi:
                 UpperCAmelCase_   :				Optional[Any]						=						self.sp_model.serialized_model_proto()
                 fi.write(_UpperCamelCase       )

       return (out_vocab_file,)



  def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase = "en_XX"    ,		_UpperCamelCase = None    ,		_UpperCamelCase = "ro_RO"    ,		**_UpperCamelCase    ,		)     ->							BatchEncoding:
       UpperCAmelCase_   :				Union[str, Any]						=						src_lang
       UpperCAmelCase_   :				Dict						=						tgt_lang
       return super().prepare_seqaseq_batch(_UpperCamelCase    ,		_UpperCamelCase    ,		**_UpperCamelCase       )



  def  __UpperCAmelCase      (							self       )     ->							Union[str, Any]:
       return self.set_src_lang_special_tokens(self.src_lang       )



  def  __UpperCAmelCase      (							self       )     ->							Optional[Any]:
       return self.set_tgt_lang_special_tokens(self.tgt_lang       )



  def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							None:
       UpperCAmelCase_   :				Any						=						self.lang_code_to_id[src_lang]
       UpperCAmelCase_   :				Optional[Any]						=						[]
       UpperCAmelCase_   :				str						=						[self.eos_token_id, self.cur_lang_code]



  def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							None:
       UpperCAmelCase_   :				Any						=						self.lang_code_to_id[lang]
       UpperCAmelCase_   :				Dict						=						[]
       UpperCAmelCase_   :				int						=						[self.eos_token_id, self.cur_lang_code]














from typing import Callable, List, Optional, Tuple, Union

import torch
from transformers import CLIPTextModel, CLIPTokenizer

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin, TransformeraDModel, VQModel
from ...schedulers import VQDiffusionScheduler
from ...utils import logging
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput


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



class   lowerCamelCase				(_snake_case			,  _snake_case      ):







		'''simple docstring'''



		@register_to_config
		def __init__(							self    ,		_UpperCamelCase    ,		_UpperCamelCase = None    ,		_UpperCamelCase = None       )     ->							int:
							super().__init__()

							UpperCAmelCase_   :				str						=						learnable

							if self.learnable:
												assert hidden_size is not None, "learnable=True requires `hidden_size` to be set"
												assert length is not None, "learnable=True requires `length` to be set"

												UpperCAmelCase_   :				Optional[Any]						=						torch.zeros(_UpperCamelCase    ,		_UpperCamelCase       )
							else:
												UpperCAmelCase_   :				Any						=						None

							UpperCAmelCase_   :				Any						=						torch.nn.Parameter(_UpperCamelCase       )





class   lowerCamelCase				(_snake_case      ):







		'''simple docstring'''



		_snake_case   :   VQModel
		_snake_case   :   CLIPTextModel
		_snake_case   :   CLIPTokenizer
		_snake_case   :   TransformeraDModel
		_snake_case   :   LearnedClassifierFreeSamplingEmbeddings
		_snake_case   :   VQDiffusionScheduler



		def __init__(							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		)     ->							List[Any]:
							super().__init__()

							self.register_modules(
							    vqvae=_UpperCamelCase    ,		transformer=_UpperCamelCase    ,		text_encoder=_UpperCamelCase    ,		tokenizer=_UpperCamelCase    ,		scheduler=_UpperCamelCase    ,		learned_classifier_free_sampling_embeddings=_UpperCamelCase    ,		)



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase       )     ->							List[str]:
							UpperCAmelCase_   :				Union[str, Any]						=						len(_UpperCamelCase       ) if isinstance(_UpperCamelCase    ,		_UpperCamelCase       ) else 1

							# get prompt text embeddings
							UpperCAmelCase_   :				str						=						self.tokenizer(
							    _UpperCamelCase    ,		padding='max_length'    ,		max_length=self.tokenizer.model_max_length    ,		return_tensors='pt'    ,		)
							UpperCAmelCase_   :				Optional[Any]						=						text_inputs.input_ids

							if text_input_ids.shape[-1] > self.tokenizer.model_max_length:
												UpperCAmelCase_   :				List[Any]						=						self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :]       )
												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}"       )
												UpperCAmelCase_   :				str						=						text_input_ids[:, : self.tokenizer.model_max_length]
							UpperCAmelCase_   :				str						=						self.text_encoder(text_input_ids.to(self.device       )       )[0]

							# NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion.
							# While CLIP does normalize the pooled output of the text transformer when combining
							# the image and text embeddings, CLIP does not directly normalize the last hidden state.
							#
							# CLIP normalizing the pooled output.
							# https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053
							UpperCAmelCase_   :				Dict						=						prompt_embeds / prompt_embeds.norm(dim=-1    ,		keepdim=_UpperCamelCase       )

							# duplicate text embeddings for each generation per prompt
							UpperCAmelCase_   :				Dict						=						prompt_embeds.repeat_interleave(_UpperCamelCase    ,		dim=0       )

							if do_classifier_free_guidance:
												if self.learned_classifier_free_sampling_embeddings.learnable:
																	UpperCAmelCase_   :				List[str]						=						self.learned_classifier_free_sampling_embeddings.embeddings
																	UpperCAmelCase_   :				List[str]						=						negative_prompt_embeds.unsqueeze(0       ).repeat(_UpperCamelCase    ,		1    ,		1       )
												else:
																	UpperCAmelCase_   :				List[Any]						=						[''] * batch_size

																	UpperCAmelCase_   :				List[Any]						=						text_input_ids.shape[-1]
																	UpperCAmelCase_   :				Dict						=						self.tokenizer(
																	    _UpperCamelCase    ,		padding='max_length'    ,		max_length=_UpperCamelCase    ,		truncation=_UpperCamelCase    ,		return_tensors='pt'    ,		)
																	UpperCAmelCase_   :				Tuple						=						self.text_encoder(uncond_input.input_ids.to(self.device       )       )[0]
																	# See comment for normalizing text embeddings
																	UpperCAmelCase_   :				Dict						=						negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1    ,		keepdim=_UpperCamelCase       )

												# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
												UpperCAmelCase_   :				List[Any]						=						negative_prompt_embeds.shape[1]
												UpperCAmelCase_   :				Dict						=						negative_prompt_embeds.repeat(1    ,		_UpperCamelCase    ,		1       )
												UpperCAmelCase_   :				Any						=						negative_prompt_embeds.view(batch_size * num_images_per_prompt    ,		_UpperCamelCase    ,		-1       )

												# 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
												UpperCAmelCase_   :				Union[str, Any]						=						torch.cat([negative_prompt_embeds, prompt_embeds]       )

							return prompt_embeds



		@torch.no_grad()
		def __call__(							self    ,		_UpperCamelCase    ,		_UpperCamelCase = 1_0_0    ,		_UpperCamelCase = 5.0    ,		_UpperCamelCase = 1.0    ,		_UpperCamelCase = 1    ,		_UpperCamelCase = None    ,		_UpperCamelCase = None    ,		_UpperCamelCase = "pil"    ,		_UpperCamelCase = True    ,		_UpperCamelCase = None    ,		_UpperCamelCase = 1    ,		)     ->							Union[ImagePipelineOutput, Tuple]:
							if isinstance(_UpperCamelCase    ,		_UpperCamelCase       ):
												UpperCAmelCase_   :				Any						=						1
							elif isinstance(_UpperCamelCase    ,		_UpperCamelCase       ):
												UpperCAmelCase_   :				Tuple						=						len(_UpperCamelCase       )
							else:
												raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(_UpperCamelCase       )}"       )

							UpperCAmelCase_   :				Union[str, Any]						=						batch_size * num_images_per_prompt

							UpperCAmelCase_   :				Optional[int]						=						guidance_scale > 1.0

							UpperCAmelCase_   :				Any						=						self._encode_prompt(_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase       )

							if (callback_steps is None) or (
							    callback_steps is not None and (not isinstance(_UpperCamelCase    ,		_UpperCamelCase       ) or callback_steps <= 0)
							):
												raise ValueError(
												    f"`callback_steps` has to be a positive integer but is {callback_steps} of type"
												    f" {type(_UpperCamelCase       )}."       )

							# get the initial completely masked latents unless the user supplied it

							UpperCAmelCase_   :				Optional[int]						=						(batch_size, self.transformer.num_latent_pixels)
							if latents is None:
												UpperCAmelCase_   :				Tuple						=						self.transformer.num_vector_embeds - 1
												UpperCAmelCase_   :				List[Any]						=						torch.full(_UpperCamelCase    ,		_UpperCamelCase       ).to(self.device       )
							else:
												if latents.shape != latents_shape:
																	raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {latents_shape}"       )
												if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any():
																	raise ValueError(
																	    'Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,'
																	    f" {self.transformer.num_vector_embeds - 1} (inclusive)."       )
												UpperCAmelCase_   :				Any						=						latents.to(self.device       )

							# set timesteps
							self.scheduler.set_timesteps(_UpperCamelCase    ,		device=self.device       )

							UpperCAmelCase_   :				List[str]						=						self.scheduler.timesteps.to(self.device       )

							UpperCAmelCase_   :				Union[str, Any]						=						latents

							for i, t in enumerate(self.progress_bar(_UpperCamelCase       )       ):
												# expand the sample if we are doing classifier free guidance
												UpperCAmelCase_   :				Union[str, Any]						=						torch.cat([sample] * 2       ) if do_classifier_free_guidance else sample

												# predict the un-noised image
												# model_output == `log_p_x_0`
												UpperCAmelCase_   :				Dict						=						self.transformer(_UpperCamelCase    ,		encoder_hidden_states=_UpperCamelCase    ,		timestep=_UpperCamelCase       ).sample

												if do_classifier_free_guidance:
																	UpperCAmelCase_	,  UpperCAmelCase_   :				Optional[Any]						=						model_output.chunk(2       )
																	UpperCAmelCase_   :				Optional[int]						=						model_output_uncond + guidance_scale * (model_output_text - model_output_uncond)
																	model_output -= torch.logsumexp(_UpperCamelCase    ,		dim=1    ,		keepdim=_UpperCamelCase       )

												UpperCAmelCase_   :				str						=						self.truncate(_UpperCamelCase    ,		_UpperCamelCase       )

												# remove `log(0)`'s (`-inf`s)
												UpperCAmelCase_   :				Optional[int]						=						model_output.clamp(-7_0       )

												# compute the previous noisy sample x_t -> x_t-1
												UpperCAmelCase_   :				Union[str, Any]						=						self.scheduler.step(_UpperCamelCase    ,		timestep=_UpperCamelCase    ,		sample=_UpperCamelCase    ,		generator=_UpperCamelCase       ).prev_sample

												# call the callback, if provided
												if callback is not None and i % callback_steps == 0:
																	callback(_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase       )

							UpperCAmelCase_   :				str						=						self.vqvae.config.vq_embed_dim
							UpperCAmelCase_   :				Optional[int]						=						(batch_size, self.transformer.height, self.transformer.width, embedding_channels)
							UpperCAmelCase_   :				int						=						self.vqvae.quantize.get_codebook_entry(_UpperCamelCase    ,		shape=_UpperCamelCase       )
							UpperCAmelCase_   :				Dict						=						self.vqvae.decode(_UpperCamelCase    ,		force_not_quantize=_UpperCamelCase       ).sample

							UpperCAmelCase_   :				List[str]						=						(image / 2 + 0.5).clamp(0    ,		1       )
							UpperCAmelCase_   :				Any						=						image.cpu().permute(0    ,		2    ,		3    ,		1       ).numpy()

							if output_type == "pil":
												UpperCAmelCase_   :				int						=						self.numpy_to_pil(_UpperCamelCase       )

							if not return_dict:
												return (image,)

							return ImagePipelineOutput(images=_UpperCamelCase       )



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase       )     ->							torch.FloatTensor:
							UpperCAmelCase_	,  UpperCAmelCase_   :				int						=						torch.sort(_UpperCamelCase    ,		1    ,		descending=_UpperCamelCase       )
							UpperCAmelCase_   :				Dict						=						torch.exp(_UpperCamelCase       )
							UpperCAmelCase_   :				int						=						sorted_p_x_0.cumsum(dim=1       ) < truncation_rate

							# Ensure that at least the largest probability is not zeroed out
							UpperCAmelCase_   :				Tuple						=						torch.full_like(keep_mask[:, 0:1, :]    ,		_UpperCamelCase       )
							UpperCAmelCase_   :				List[str]						=						torch.cat((all_true, keep_mask)    ,		dim=1       )
							UpperCAmelCase_   :				int						=						keep_mask[:, :-1, :]

							UpperCAmelCase_   :				Any						=						keep_mask.gather(1    ,		indices.argsort(1       )       )

							UpperCAmelCase_   :				str						=						log_p_x_0.clone()

							UpperCAmelCase_   :				Any						=						-torch.inf  # -inf = log(0)

							return rv






