



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_						(    a__			,       unittest.TestCase				):
       UpperCAmelCase__							:   List[Any]  	=    ShapEImgaImgPipeline
       UpperCAmelCase__							:   str  	=    ["image"]
       UpperCAmelCase__							:   int  	=    ["image"]
       UpperCAmelCase__							:   Optional[int]  	=    [
           "num_images_per_prompt",
           "num_inference_steps",
           "generator",
           "latents",
           "guidance_scale",
           "frame_size",
           "output_type",
           "return_dict",
       ]
       UpperCAmelCase__							:   Tuple  	=    False
       @property
       def    snake_case_			(     self			)	->						Union[str, Any]:
          return 32
       @property
       def    snake_case_			(     self			)	->						str:
          return 32
       @property
       def    snake_case_			(     self			)	->						int:
          return self.time_input_dim * 4
       @property
       def    snake_case_			(     self			)	->						Optional[int]:
          return 8
       @property
       def    snake_case_			(     self			)	->						Optional[int]:
          torch.manual_seed(0			)
          UpperCamelCase       :    Union[str, Any]		      =      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,     )

          UpperCamelCase       :    List[Any]		      =      CLIPVisionModel(SCREAMING_SNAKE_CASE_			)
          return model
       @property
       def    snake_case_			(     self			)	->						str:
          UpperCamelCase       :    Dict		      =      CLIPImageProcessor(
              crop_size=224,     do_center_crop=SCREAMING_SNAKE_CASE_,     do_normalize=SCREAMING_SNAKE_CASE_,     do_resize=SCREAMING_SNAKE_CASE_,     image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73],     image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11],     resample=3,     size=224,     )

          return image_processor
       @property
       def    snake_case_			(     self			)	->						Any:
          torch.manual_seed(0			)

          UpperCamelCase       :    Dict		      =      {
              '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,
          }

          UpperCamelCase       :    Any		      =      PriorTransformer(**SCREAMING_SNAKE_CASE_			)
          return model
       @property
       def    snake_case_			(     self			)	->						Optional[int]:
          torch.manual_seed(0			)

          UpperCamelCase       :    Dict		      =      {
              '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,
              ),
          }
          UpperCamelCase       :    List[Any]		      =      ShapERenderer(**SCREAMING_SNAKE_CASE_			)
          return model
       def    snake_case_			(     self			)	->						Dict:
          UpperCamelCase       :    Dict		      =      self.dummy_prior
          UpperCamelCase       :    Tuple		      =      self.dummy_image_encoder
          UpperCamelCase       :    Optional[Any]		      =      self.dummy_image_processor
          UpperCamelCase       :    Any		      =      self.dummy_renderer

          UpperCamelCase       :    Optional[int]		      =      HeunDiscreteScheduler(
              beta_schedule='exp',     num_train_timesteps=1024,     prediction_type='sample',     use_karras_sigmas=SCREAMING_SNAKE_CASE_,     clip_sample=SCREAMING_SNAKE_CASE_,     clip_sample_range=1.0,     )
          UpperCamelCase       :    Optional[int]		      =      {
              'prior': prior,
              'image_encoder': image_encoder,
              'image_processor': image_processor,
              'renderer': renderer,
              'scheduler': scheduler,
          }

          return components
       def    snake_case_			(     self,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_=0			)	->						List[Any]:
          UpperCamelCase       :    Any		      =      floats_tensor((1, 3, 64, 64),     rng=random.Random(SCREAMING_SNAKE_CASE_			)			).to(SCREAMING_SNAKE_CASE_			)

          if str(SCREAMING_SNAKE_CASE_			).startswith('mps'			):
             UpperCamelCase       :    Dict		      =      torch.manual_seed(SCREAMING_SNAKE_CASE_			)
          else:
             UpperCamelCase       :    Dict		      =      torch.Generator(device=SCREAMING_SNAKE_CASE_			).manual_seed(SCREAMING_SNAKE_CASE_			)
          UpperCamelCase       :    Optional[int]		      =      {
              'image': input_image,
              'generator': generator,
              'num_inference_steps': 1,
              'frame_size': 32,
              'output_type': 'np',
          }
          return inputs
       def    snake_case_			(     self			)	->						Union[str, Any]:
          UpperCamelCase       :    Tuple		      =      'cpu'

          UpperCamelCase       :    List[str]		      =      self.get_dummy_components()

          UpperCamelCase       :    List[str]		      =      self.pipeline_class(**SCREAMING_SNAKE_CASE_			)
          UpperCamelCase       :    str		      =      pipe.to(SCREAMING_SNAKE_CASE_			)

          pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_			)

          UpperCamelCase       :    List[Any]		      =      pipe(**self.get_dummy_inputs(SCREAMING_SNAKE_CASE_			)			)
          UpperCamelCase       :    Optional[int]		      =      output.images[0]
          UpperCamelCase       :    Any		      =      image[0, -3:, -3:, -1]

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

          UpperCamelCase       :    Any		      =      np.array(
              [
                  0.00_03_92_16,
                  0.00_03_92_16,
                  0.00_03_92_16,
                  0.00_03_92_16,
                  0.00_03_92_16,
                  0.00_03_92_16,
                  0.00_03_92_16,
                  0.00_03_92_16,
                  0.00_03_92_16,
              ]			)

          assert np.abs(image_slice.flatten() - expected_slice			).max() < 1e-2
       def    snake_case_			(     self			)	->						List[Any]:
          # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
          self._test_inference_batch_consistent(batch_sizes=[1, 2]			)
       def    snake_case_			(     self			)	->						Optional[Any]:
          UpperCamelCase       :    Optional[Any]		      =      torch_device == 'cpu'
          UpperCamelCase       :    Any		      =      True
          self._test_inference_batch_single_identical(
              batch_size=2,     test_max_difference=SCREAMING_SNAKE_CASE_,     relax_max_difference=SCREAMING_SNAKE_CASE_,     )






       def    snake_case_			(     self			)	->						Optional[int]:
          UpperCamelCase       :    Union[str, Any]		      =      self.get_dummy_components()
          UpperCamelCase       :    Tuple		      =      self.pipeline_class(**SCREAMING_SNAKE_CASE_			)
          UpperCamelCase       :    Optional[Any]		      =      pipe.to(SCREAMING_SNAKE_CASE_			)
          pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_			)

          UpperCamelCase       :    Any		      =      1
          UpperCamelCase       :    Optional[Any]		      =      2

          UpperCamelCase       :    Optional[int]		      =      self.get_dummy_inputs(SCREAMING_SNAKE_CASE_			)

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

          UpperCamelCase       :    Any		      =      pipe(**SCREAMING_SNAKE_CASE_,     num_images_per_prompt=SCREAMING_SNAKE_CASE_			)[0]

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






@slow
@require_torch_gpu
class 							lowerCAmelCase_						(    unittest.TestCase				):
       def    snake_case_			(     self			)	->						List[Any]:
          # clean up the VRAM after each test
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()






       def    snake_case_			(     self			)	->						str:
          UpperCamelCase       :    Dict		      =      load_image(
              'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/shap_e/corgi.png'			)
          UpperCamelCase       :    Optional[Any]		      =      load_numpy(
              'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
              '/shap_e/test_shap_e_img2img_out.npy'			)
          UpperCamelCase       :    Optional[int]		      =      ShapEImgaImgPipeline.from_pretrained('openai/shap-e-img2img'			)
          UpperCamelCase       :    List[str]		      =      pipe.to(SCREAMING_SNAKE_CASE_			)
          pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_			)

          UpperCamelCase       :    Dict		      =      torch.Generator(device=SCREAMING_SNAKE_CASE_			).manual_seed(0			)

          UpperCamelCase       :    int		      =      pipe(
              SCREAMING_SNAKE_CASE_,     generator=SCREAMING_SNAKE_CASE_,     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(SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_			)




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

import sentencepiece as spm

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


lowerCamelCase__  =					logging.get_logger(__name__)

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

lowerCamelCase__  =					{
    '''vocab_file''': {
        '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model''',
        '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model''',
    }
}

lowerCamelCase__  =					{
    '''xlnet-base-cased''': None,
    '''xlnet-large-cased''': None,
}

# Segments (not really needed)
lowerCamelCase__  =					0
lowerCamelCase__  =					1
lowerCamelCase__  =					2
lowerCamelCase__  =					3
lowerCamelCase__  =					4




class 					_UpperCAmelCase			(     lowerCAmelCase  ):



 '''simple docstring'''




 __A			       =					VOCAB_FILES_NAMES
 __A			       =					PRETRAINED_VOCAB_FILES_MAP
 __A			       =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 __A			       =					'''left'''







 def __init__(		self				:   int							, lowercase_				:   Optional[int]							, lowercase_				:   Dict=False							, lowercase_				:   Any=True							, lowercase_				:   Any=False							, lowercase_				:   Any="<s>"							, lowercase_				:   Dict="</s>"							, lowercase_				:   Optional[Any]="<unk>"							, lowercase_				:   Optional[int]="<sep>"							, lowercase_				:   Optional[Any]="<pad>"							, lowercase_				:   Tuple="<cls>"							, lowercase_				:   Union[str, Any]="<mask>"							, lowercase_				:   Dict=["<eop>", "<eod>"]							, lowercase_				:   Optional[Dict[str, Any]] = None							, **lowercase_				:   int							, )						->	None:


   """simple docstring"""







   _UpperCamelCase           =    AddedToken(lowercase_							, lstrip=lowercase_							, rstrip=lowercase_) if isinstance(lowercase_							, lowercase_) else mask_token

   _UpperCamelCase           =    {} if sp_model_kwargs is None else sp_model_kwargs

   super().__init__(
       do_lower_case=lowercase_							, remove_space=lowercase_							, keep_accents=lowercase_							, bos_token=lowercase_							, eos_token=lowercase_							, unk_token=lowercase_							, sep_token=lowercase_							, pad_token=lowercase_							, cls_token=lowercase_							, mask_token=lowercase_							, additional_special_tokens=lowercase_							, sp_model_kwargs=self.sp_model_kwargs							, **lowercase_							, )

   _UpperCamelCase           =    3

   _UpperCamelCase           =    do_lower_case
   _UpperCamelCase           =    remove_space
   _UpperCamelCase           =    keep_accents
   _UpperCamelCase           =    vocab_file

   _UpperCamelCase           =    spm.SentencePieceProcessor(**self.sp_model_kwargs)
   self.sp_model.Load(lowercase_)







 @property
 def 		__UpperCAmelCase				(		self				:   List[str])						->	Dict:


   """simple docstring"""







   return len(self.sp_model)







 def 		__UpperCAmelCase				(		self				:   List[str])						->	Union[str, Any]:


   """simple docstring"""







   _UpperCamelCase           =    {self.convert_ids_to_tokens(lowercase_): i for i in range(self.vocab_size)}
   vocab.update(self.added_tokens_encoder)
   return vocab







 def __getstate__(		self				:   Optional[int])						->	Any:


   """simple docstring"""







   _UpperCamelCase           =    self.__dict__.copy()
   _UpperCamelCase           =    None
   return state







 def __setstate__(		self				:   Tuple							, lowercase_				:   List[str])						->	Dict:


   """simple docstring"""







   _UpperCamelCase           =    d

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

   _UpperCamelCase           =    spm.SentencePieceProcessor(**self.sp_model_kwargs)
   self.sp_model.Load(self.vocab_file)







 def 		__UpperCAmelCase				(		self				:   str							, lowercase_				:   Optional[Any])						->	Tuple:


   """simple docstring"""







   if self.remove_space:
     _UpperCamelCase           =    " ".join(inputs.strip().split())
   else:
     _UpperCamelCase           =    inputs
   _UpperCamelCase           =    outputs.replace("``"							, "\"").replace("''"							, "\"")

   if not self.keep_accents:
     _UpperCamelCase           =    unicodedata.normalize("NFKD"							, lowercase_)
     _UpperCamelCase           =    "".join([c for c in outputs if not unicodedata.combining(lowercase_)])
   if self.do_lower_case:
     _UpperCamelCase           =    outputs.lower()

   return outputs







 def 		__UpperCAmelCase				(		self				:   str							, lowercase_				:   str)						->	List[str]:


   """simple docstring"""







   _UpperCamelCase           =    self.preprocess_text(lowercase_)
   _UpperCamelCase           =    self.sp_model.encode(lowercase_							, out_type=lowercase_)
   _UpperCamelCase           =    []
   for piece in pieces:
     if len(lowercase_) > 1 and piece[-1] == str(",") and piece[-2].isdigit():
       _UpperCamelCase           =    self.sp_model.EncodeAsPieces(piece[:-1].replace(lowercase_							, ""))
       if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
         if len(cur_pieces[0]) == 1:
           _UpperCamelCase           =    cur_pieces[1:]
         else:
           _UpperCamelCase           =    cur_pieces[0][1:]
       cur_pieces.append(piece[-1])
       new_pieces.extend(lowercase_)
     else:
       new_pieces.append(lowercase_)

   return new_pieces







 def 		__UpperCAmelCase				(		self				:   Optional[Any]							, lowercase_				:   Tuple)						->	List[str]:


   """simple docstring"""







   return self.sp_model.PieceToId(lowercase_)







 def 		__UpperCAmelCase				(		self				:   Optional[Any]							, lowercase_				:   int)						->	Any:


   """simple docstring"""







   return self.sp_model.IdToPiece(lowercase_)







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


   """simple docstring"""







   _UpperCamelCase           =    "".join(lowercase_).replace(lowercase_							, " ").strip()
   return out_string







 def 		__UpperCAmelCase				(		self				:   Optional[Any]							, lowercase_				:   List[int]							, lowercase_				:   bool = False							, lowercase_				:   bool = None							, lowercase_				:   bool = True							, **lowercase_				:   int							, )						->	str:


   """simple docstring"""







   _UpperCamelCase           =    kwargs.pop("use_source_tokenizer"							, lowercase_)

   _UpperCamelCase           =    self.convert_ids_to_tokens(lowercase_							, skip_special_tokens=lowercase_)

   # To avoid mixing byte-level and unicode for byte-level BPT
   # we need to build string separately for added tokens and byte-level tokens
   # cf. https://github.com/huggingface/transformers/issues/1133
   _UpperCamelCase           =    []
   _UpperCamelCase           =    []
   for token in filtered_tokens:
     if skip_special_tokens and token in self.all_special_ids:
       continue
     if token in self.added_tokens_encoder:
       if current_sub_text:
         sub_texts.append(self.convert_tokens_to_string(lowercase_))
         _UpperCamelCase           =    []
       sub_texts.append(lowercase_)
     else:
       current_sub_text.append(lowercase_)
   if current_sub_text:
     sub_texts.append(self.convert_tokens_to_string(lowercase_))

   # Mimic the behavior of the Rust tokenizer:
   # By default, there are no spaces between special tokens
   _UpperCamelCase           =    "".join(lowercase_)

   _UpperCamelCase           =    (
       clean_up_tokenization_spaces
       if clean_up_tokenization_spaces is not None
       else self.clean_up_tokenization_spaces
   )
   if clean_up_tokenization_spaces:
     _UpperCamelCase           =    self.clean_up_tokenization(lowercase_)
     return clean_text
   else:
     return text







 def 		__UpperCAmelCase				(		self				:   str							, lowercase_				:   List[int]							, lowercase_				:   Optional[List[int]] = None)						->	List[int]:


   """simple docstring"""







   _UpperCamelCase           =    [self.sep_token_id]
   _UpperCamelCase           =    [self.cls_token_id]
   if token_ids_a is None:
     return token_ids_a + sep + cls
   return token_ids_a + sep + token_ids_a + sep + cls







 def 		__UpperCAmelCase				(		self				:   Tuple							, lowercase_				:   List[int]							, lowercase_				:   Optional[List[int]] = None							, lowercase_				:   bool = False)						->	List[int]:


   """simple docstring"""







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

   if token_ids_a is not None:
     return ([0] * len(lowercase_)) + [1] + ([0] * len(lowercase_)) + [1, 1]
   return ([0] * len(lowercase_)) + [1, 1]







 def 		__UpperCAmelCase				(		self				:   str							, lowercase_				:   List[int]							, lowercase_				:   Optional[List[int]] = None)						->	List[int]:


   """simple docstring"""







   _UpperCamelCase           =    [self.sep_token_id]
   _UpperCamelCase           =    [2]

   if token_ids_a is None:
     return len(token_ids_a + sep) * [0] + cls_segment_id
   return len(token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1] + cls_segment_id







 def 		__UpperCAmelCase				(		self				:   Tuple							, lowercase_				:   str							, lowercase_				:   Optional[str] = None)						->	Tuple[str]:


   """simple docstring"""







   if not os.path.isdir(lowercase_):
     logger.error(f'Vocabulary path ({save_directory}) should be a directory')
     return
   _UpperCamelCase           =    os.path.join(
       lowercase_							, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"])

   if os.path.abspath(self.vocab_file) != os.path.abspath(lowercase_) and os.path.isfile(self.vocab_file):
     copyfile(self.vocab_file							, lowercase_)
   elif not os.path.isfile(self.vocab_file):
     with open(lowercase_							, "wb") as fi:
       _UpperCamelCase           =    self.sp_model.serialized_model_proto()
       fi.write(lowercase_)

   return (out_vocab_file,)




