




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

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer


if TYPE_CHECKING:
       from ...tokenization_utils_base import TextInput
from ...utils import logging


UpperCAmelCase_									=	logging.get_logger(__name__)

UpperCAmelCase_									=	{'vocab_file': 'spiece.model'}

UpperCAmelCase_									=	{
    'vocab_file': {
        't5-small': 'https://huggingface.co/t5-small/resolve/main/spiece.model',
        't5-base': 'https://huggingface.co/t5-base/resolve/main/spiece.model',
        't5-large': 'https://huggingface.co/t5-large/resolve/main/spiece.model',
        't5-3b': 'https://huggingface.co/t5-3b/resolve/main/spiece.model',
        't5-11b': 'https://huggingface.co/t5-11b/resolve/main/spiece.model',
    }
}


# TODO(PVP) - this should be removed in Transformers v5
UpperCAmelCase_									=	{
    't5-small': 512,
    't5-base': 512,
    't5-large': 512,
    't5-3b': 512,
    't5-11b': 512,
}

UpperCAmelCase_									=	'▁'





class     lowerCamelCase__(					__lowerCamelCase):
      UpperCAmelCase__      :	List[str]    		=   VOCAB_FILES_NAMES
      UpperCAmelCase__      :	int    		=   PRETRAINED_VOCAB_FILES_MAP
      UpperCAmelCase__      :	List[str]    		=   PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      UpperCAmelCase__      :	Any    		=   ['input_ids', 'attention_mask']
      def __init__(				self: Dict    ,       UpperCamelCase_: Optional[Any]    ,       UpperCamelCase_: Union[str, Any]="</s>"    ,       UpperCamelCase_: Optional[Any]="<unk>"    ,       UpperCamelCase_: Any="<pad>"    ,       UpperCamelCase_: Optional[Any]=1_00    ,       UpperCamelCase_: Optional[int]=None    ,       UpperCamelCase_: Optional[Dict[str, Any]] = None    ,       UpperCamelCase_: Optional[int]=True    ,       **UpperCamelCase_: str    ,       ):
             # Add extra_ids to the special token list
             if extra_ids > 0 and additional_special_tokens is None:
                    __lowerCamelCase   =						[F'<extra_id_{i}>' for i in range(UpperCamelCase_   )]
             elif extra_ids > 0 and additional_special_tokens is not None:
                    # Check that we have the right number of extra_id special tokens
                    __lowerCamelCase   =						len(set(filter(lambda UpperCamelCase_   : bool("""extra_id""" in str(UpperCamelCase_   )   )    ,       UpperCamelCase_   )   )   )
                    if extra_tokens != extra_ids:
                           raise ValueError(
                               F'Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are'
                               """ provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids"""
                               """ tokens"""   )
             if legacy:
                    logger.warning_once(
                        F'You are using the legacy behaviour of the {self.__class__}. This means that tokens that come after special tokens will not be properly handled. We recommend you to'
                        """ read the related pull request available at https://github.com/huggingface/transformers/pull/24565"""   )

             __lowerCamelCase   =						legacy
             __lowerCamelCase   =						{} if sp_model_kwargs is None else sp_model_kwargs

             super().__init__(
                 eos_token=UpperCamelCase_    ,       unk_token=UpperCamelCase_    ,       pad_token=UpperCamelCase_    ,       extra_ids=UpperCamelCase_    ,       additional_special_tokens=UpperCamelCase_    ,       sp_model_kwargs=self.sp_model_kwargs    ,       legacy=UpperCamelCase_    ,       **UpperCamelCase_    ,       )

             __lowerCamelCase   =						vocab_file
             __lowerCamelCase   =						extra_ids

             __lowerCamelCase   =						spm.SentencePieceProcessor(**self.sp_model_kwargs   )
             self.sp_model.Load(UpperCamelCase_   )
      @staticmethod
      def 			lowerCAmelCase__						(				UpperCamelCase_: Optional[int]    ,       UpperCamelCase_: str    ,       UpperCamelCase_: Optional[int]   ):
             if pretrained_model_name_or_path in TaTokenizer.max_model_input_sizes:
                    __lowerCamelCase   =						TaTokenizer.max_model_input_sizes[pretrained_model_name_or_path]
                    if init_max_model_length is not None and init_max_model_length != max_model_length:
                           return init_max_model_length
                    elif init_max_model_length is None:
                           warnings.warn(
                               """This tokenizer was incorrectly instantiated with a model max length of"""
                               F' {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this'
                               """ behavior is kept to avoid breaking backwards compatibility when padding/encoding with"""
                               """ `truncation is True`.\n- Be aware that you SHOULD NOT rely on"""
                               F' {pretrained_model_name_or_path} automatically truncating your input to'
                               F' {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences'
                               F' longer than {deprecated_max_model_length} you can either instantiate this tokenizer with'
                               """ `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please"""
                               """ instantiate this tokenizer with `model_max_length` set to your preferred value."""    ,       UpperCamelCase_    ,       )

             return max_model_length
      @property
      def 			lowerCAmelCase__						(				self: List[Any]   ):
             return self.sp_model.get_piece_size() + self._extra_ids
      def 			lowerCAmelCase__						(				self: List[str]   ):
             __lowerCamelCase   =						{self.convert_ids_to_tokens(UpperCamelCase_   ): i for i in range(self.vocab_size   )}
             vocab.update(self.added_tokens_encoder   )
             return vocab
      def 			lowerCAmelCase__						(				self: List[str]    ,       UpperCamelCase_: List[int]    ,       UpperCamelCase_: Optional[List[int]] = None    ,       UpperCamelCase_: bool = False   ):
             if already_has_special_tokens:
                    return super().get_special_tokens_mask(
                        token_ids_a=UpperCamelCase_    ,       token_ids_a=UpperCamelCase_    ,       already_has_special_tokens=UpperCamelCase_   )

             # normal case: some special tokens
             if token_ids_a is None:
                    return ([0] * len(UpperCamelCase_   )) + [1]
             return ([0] * len(UpperCamelCase_   )) + [1] + ([0] * len(UpperCamelCase_   )) + [1]
      def 			lowerCAmelCase__						(				self: List[str]   ):
             return list(
                 set(filter(lambda UpperCamelCase_   : bool(re.search(r"""<extra_id_\d+>"""    ,       UpperCamelCase_   )   ) is not None    ,       self.additional_special_tokens   )   )   )
      def 			lowerCAmelCase__						(				self: Union[str, Any]   ):
             return [self._convert_token_to_id(UpperCamelCase_   ) for token in self.get_sentinel_tokens()]
      def 			lowerCAmelCase__						(				self: int    ,       UpperCamelCase_: List[int]   ):
             if len(UpperCamelCase_   ) > 0 and token_ids[-1] == self.eos_token_id:
                    warnings.warn(
                        F'This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated'
                        """ eos tokens being added."""   )
                    return token_ids
             else:
                    return token_ids + [self.eos_token_id]
      def 			lowerCAmelCase__						(				self: Optional[Any]    ,       UpperCamelCase_: List[int]    ,       UpperCamelCase_: Optional[List[int]] = None   ):
             __lowerCamelCase   =						[self.eos_token_id]

             if token_ids_a is None:
                    return len(token_ids_a + eos   ) * [0]
             return len(token_ids_a + eos + token_ids_a + eos   ) * [0]
      def 			lowerCAmelCase__						(				self: Optional[int]    ,       UpperCamelCase_: List[int]    ,       UpperCamelCase_: Optional[List[int]] = None   ):
             __lowerCamelCase   =						self._add_eos_if_not_present(UpperCamelCase_   )
             if token_ids_a is None:
                    return token_ids_a
             else:
                    __lowerCamelCase   =						self._add_eos_if_not_present(UpperCamelCase_   )
                    return token_ids_a + token_ids_a
      def __getstate__(				self: Dict   ):
             __lowerCamelCase   =						self.__dict__.copy()
             __lowerCamelCase   =						None
             return state
      def __setstate__(				self: int    ,       UpperCamelCase_: Dict   ):
             __lowerCamelCase   =						d

             # for backward compatibility
             if not hasattr(self    ,       """sp_model_kwargs"""   ):
                    __lowerCamelCase   =						{}

             __lowerCamelCase   =						spm.SentencePieceProcessor(**self.sp_model_kwargs   )
             self.sp_model.Load(self.vocab_file   )
      def 			lowerCAmelCase__						(				self: List[str]    ,       UpperCamelCase_: "TextInput"    ,       **UpperCamelCase_: Dict   ):
             # Replace the SPIECE_UNDERLINE with a space to make sure SPIECE_UNDERLINE is only used at
             # the beginning of the text
             if not self.legacy:
                    __lowerCamelCase   =						SPIECE_UNDERLINE + text.replace(UpperCamelCase_    ,       """ """   )
             return super().tokenize(UpperCamelCase_    ,       **UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: Dict    ,       UpperCamelCase_: Any    ,       **UpperCamelCase_: List[Any]   ):
             if not self.legacy:
                    __lowerCamelCase   =						text.startswith(UpperCamelCase_   )
                    if is_first:
                           __lowerCamelCase   =						text[1:]

             __lowerCamelCase   =						self.sp_model.encode(UpperCamelCase_    ,       out_type=UpperCamelCase_   )

             if not self.legacy and not is_first and not text.startswith(""" """   ) and tokens[0].startswith(UpperCamelCase_   ):
                    __lowerCamelCase   =						([tokens[0][1:]] if len(tokens[0]   ) > 1 else []) + tokens[1:]
             return tokens
      def 			lowerCAmelCase__						(				self: Tuple    ,       UpperCamelCase_: Tuple   ):
             if token.startswith("""<extra_id_"""   ):
                    __lowerCamelCase   =						re.match(r"""<extra_id_(\d+)>"""    ,       UpperCamelCase_   )
                    __lowerCamelCase   =						int(match.group(1   )   )
                    return self.vocab_size - num - 1
             return self.sp_model.piece_to_id(UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: List[str]    ,       UpperCamelCase_: Optional[int]   ):
             if index < self.sp_model.get_piece_size():
                    __lowerCamelCase   =						self.sp_model.IdToPiece(UpperCamelCase_   )
             else:
                    __lowerCamelCase   =						F'<extra_id_{self.vocab_size - 1 - index}>'
             return token
      def 			lowerCAmelCase__						(				self: Dict    ,       UpperCamelCase_: Dict   ):
             __lowerCamelCase   =						[]
             __lowerCamelCase   =						""""""
             __lowerCamelCase   =						False
             for token in tokens:
                    # make sure that special tokens are not decoded using sentencepiece model
                    if token in self.all_special_tokens:
                           if not prev_is_special:
                                  out_string += " "
                           out_string += self.sp_model.decode(UpperCamelCase_   ) + token
                           __lowerCamelCase   =						True
                           __lowerCamelCase   =						[]
                    else:
                           current_sub_tokens.append(UpperCamelCase_   )
                           __lowerCamelCase   =						False
             out_string += self.sp_model.decode(UpperCamelCase_   )
             return out_string.strip()




      def 			lowerCAmelCase__						(				self: List[str]    ,       UpperCamelCase_: str    ,       UpperCamelCase_: Optional[str] = None   ):
             if not os.path.isdir(UpperCamelCase_   ):
                    logger.error(F'Vocabulary path ({save_directory}) should be a directory'   )
                    return
             __lowerCamelCase   =						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:
                           __lowerCamelCase   =						self.sp_model.serialized_model_proto()
                           fi.write(UpperCamelCase_   )

             return (out_vocab_file,)







import gc
import random
import unittest

import numpy as np
import torch
from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel

from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline
from diffusers.pipelines.shap_e import ShapERenderer
from diffusers.utils import floats_tensor, load_image, load_numpy, slow
from diffusers.utils.testing_utils import require_torch_gpu, torch_device

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference





class     lowerCamelCase__(					__lowerCamelCase ,     unittest.TestCase):
      UpperCAmelCase__      :	Tuple    		=   ShapEImgaImgPipeline
      UpperCAmelCase__      :	Optional[Any]    		=   ['image']
      UpperCAmelCase__      :	int    		=   ['image']
      UpperCAmelCase__      :	Any    		=   [
          'num_images_per_prompt',
          'num_inference_steps',
          'generator',
          'latents',
          'guidance_scale',
          'frame_size',
          'output_type',
          'return_dict',
      ]
      UpperCAmelCase__      :	int    		=   False
      @property
      def 			lowerCAmelCase__						(				self: int   ):
             return 32
      @property
      def 			lowerCAmelCase__						(				self: List[str]   ):
             return 32
      @property
      def 			lowerCAmelCase__						(				self: Any   ):
             return self.time_input_dim * 4
      @property
      def 			lowerCAmelCase__						(				self: Dict   ):
             return 8
      @property
      def 			lowerCAmelCase__						(				self: int   ):
             torch.manual_seed(0   )
             __lowerCamelCase   =						CLIPVisionConfig(
                 hidden_size=self.text_embedder_hidden_size    ,       image_size=64    ,       projection_dim=self.text_embedder_hidden_size    ,       intermediate_size=37    ,       num_attention_heads=4    ,       num_channels=3    ,       num_hidden_layers=5    ,       patch_size=1    ,       )

             __lowerCamelCase   =						CLIPVisionModel(UpperCamelCase_   )
             return model
      @property
      def 			lowerCAmelCase__						(				self: Dict   ):
             __lowerCamelCase   =						CLIPImageProcessor(
                 crop_size=2_24    ,       do_center_crop=UpperCamelCase_    ,       do_normalize=UpperCamelCase_    ,       do_resize=UpperCamelCase_    ,       image_mean=[0.4814_5466, 0.457_8275, 0.4082_1073]    ,       image_std=[0.2686_2954, 0.2613_0258, 0.2757_7711]    ,       resample=3    ,       size=2_24    ,       )

             return image_processor
      @property
      def 			lowerCAmelCase__						(				self: Tuple   ):
             torch.manual_seed(0   )

             __lowerCamelCase   =						{
                 """num_attention_heads""": 2,
                 """attention_head_dim""": 16,
                 """embedding_dim""": self.time_input_dim,
                 """num_embeddings""": 32,
                 """embedding_proj_dim""": self.text_embedder_hidden_size,
                 """time_embed_dim""": self.time_embed_dim,
                 """num_layers""": 1,
                 """clip_embed_dim""": self.time_input_dim * 2,
                 """additional_embeddings""": 0,
                 """time_embed_act_fn""": """gelu""",
                 """norm_in_type""": """layer""",
                 """embedding_proj_norm_type""": """layer""",
                 """encoder_hid_proj_type""": None,
                 """added_emb_type""": None,
             }

             __lowerCamelCase   =						PriorTransformer(**UpperCamelCase_   )
             return model
      @property
      def 			lowerCAmelCase__						(				self: List[Any]   ):
             torch.manual_seed(0   )

             __lowerCamelCase   =						{
                 """param_shapes""": (
                     (self.renderer_dim, 93),
                     (self.renderer_dim, 8),
                     (self.renderer_dim, 8),
                     (self.renderer_dim, 8),
                 ),
                 """d_latent""": self.time_input_dim,
                 """d_hidden""": self.renderer_dim,
                 """n_output""": 12,
                 """background""": (
                     0.1,
                     0.1,
                     0.1,
                 ),
             }
             __lowerCamelCase   =						ShapERenderer(**UpperCamelCase_   )
             return model
      def 			lowerCAmelCase__						(				self: List[str]   ):
             __lowerCamelCase   =						self.dummy_prior
             __lowerCamelCase   =						self.dummy_image_encoder
             __lowerCamelCase   =						self.dummy_image_processor
             __lowerCamelCase   =						self.dummy_renderer

             __lowerCamelCase   =						HeunDiscreteScheduler(
                 beta_schedule="""exp"""    ,       num_train_timesteps=10_24    ,       prediction_type="""sample"""    ,       use_karras_sigmas=UpperCamelCase_    ,       clip_sample=UpperCamelCase_    ,       clip_sample_range=1.0    ,       )
             __lowerCamelCase   =						{
                 """prior""": prior,
                 """image_encoder""": image_encoder,
                 """image_processor""": image_processor,
                 """renderer""": renderer,
                 """scheduler""": scheduler,
             }

             return components
      def 			lowerCAmelCase__						(				self: int    ,       UpperCamelCase_: List[Any]    ,       UpperCamelCase_: Dict=0   ):
             __lowerCamelCase   =						floats_tensor((1, 3, 64, 64)    ,       rng=random.Random(UpperCamelCase_   )   ).to(UpperCamelCase_   )

             if str(UpperCamelCase_   ).startswith("""mps"""   ):
                    __lowerCamelCase   =						torch.manual_seed(UpperCamelCase_   )
             else:
                    __lowerCamelCase   =						torch.Generator(device=UpperCamelCase_   ).manual_seed(UpperCamelCase_   )
             __lowerCamelCase   =						{
                 """image""": input_image,
                 """generator""": generator,
                 """num_inference_steps""": 1,
                 """frame_size""": 32,
                 """output_type""": """np""",
             }
             return inputs
      def 			lowerCAmelCase__						(				self: Tuple   ):
             __lowerCamelCase   =						"""cpu"""

             __lowerCamelCase   =						self.get_dummy_components()

             __lowerCamelCase   =						self.pipeline_class(**UpperCamelCase_   )
             __lowerCamelCase   =						pipe.to(UpperCamelCase_   )

             pipe.set_progress_bar_config(disable=UpperCamelCase_   )

             __lowerCamelCase   =						pipe(**self.get_dummy_inputs(UpperCamelCase_   )   )
             __lowerCamelCase   =						output.images[0]
             __lowerCamelCase   =						image[0, -3:, -3:, -1]

             assert image.shape == (20, 32, 32, 3)

             __lowerCamelCase   =						np.array(
                 [
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                 ]   )

             assert np.abs(image_slice.flatten() - expected_slice   ).max() < 1E-2
      def 			lowerCAmelCase__						(				self: List[str]   ):
             # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
             self._test_inference_batch_consistent(batch_sizes=[1, 2]   )
      def 			lowerCAmelCase__						(				self: Any   ):
             __lowerCamelCase   =						torch_device == """cpu"""
             __lowerCamelCase   =						True
             self._test_inference_batch_single_identical(
                 batch_size=2    ,       test_max_difference=UpperCamelCase_    ,       relax_max_difference=UpperCamelCase_    ,       )




      def 			lowerCAmelCase__						(				self: Any   ):
             __lowerCamelCase   =						self.get_dummy_components()
             __lowerCamelCase   =						self.pipeline_class(**UpperCamelCase_   )
             __lowerCamelCase   =						pipe.to(UpperCamelCase_   )
             pipe.set_progress_bar_config(disable=UpperCamelCase_   )

             __lowerCamelCase   =						1
             __lowerCamelCase   =						2

             __lowerCamelCase   =						self.get_dummy_inputs(UpperCamelCase_   )

             for key in inputs.keys():
                    if key in self.batch_params:
                           __lowerCamelCase   =						batch_size * [inputs[key]]

             __lowerCamelCase   =						pipe(**UpperCamelCase_    ,       num_images_per_prompt=UpperCamelCase_   )[0]

             assert images.shape[0] == batch_size * num_images_per_prompt





@slow
@require_torch_gpu
class     lowerCamelCase__(					unittest.TestCase):
      def 			lowerCAmelCase__						(				self: Union[str, Any]   ):
             # clean up the VRAM after each test
             super().tearDown()
             gc.collect()
             torch.cuda.empty_cache()




      def 			lowerCAmelCase__						(				self: Any   ):
             __lowerCamelCase   =						load_image(
                 """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/shap_e/corgi.png"""   )
             __lowerCamelCase   =						load_numpy(
                 """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
                 """/shap_e/test_shap_e_img2img_out.npy"""   )
             __lowerCamelCase   =						ShapEImgaImgPipeline.from_pretrained("""openai/shap-e-img2img"""   )
             __lowerCamelCase   =						pipe.to(UpperCamelCase_   )
             pipe.set_progress_bar_config(disable=UpperCamelCase_   )

             __lowerCamelCase   =						torch.Generator(device=UpperCamelCase_   ).manual_seed(0   )

             __lowerCamelCase   =						pipe(
                 UpperCamelCase_    ,       generator=UpperCamelCase_    ,       guidance_scale=3.0    ,       num_inference_steps=64    ,       frame_size=64    ,       output_type="""np"""    ,       ).images[0]

             assert images.shape == (20, 64, 64, 3)

             assert_mean_pixel_difference(UpperCamelCase_    ,       UpperCamelCase_   )

