





'''simple docstring'''




import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline
from diffusers.pipelines.shap_e import ShapERenderer
from diffusers.utils import 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 					SCREAMING_SNAKE_CASE       (_snake_case   ,						unittest.TestCase				):
     lowerCAmelCase						     =			ShapEPipeline
     lowerCAmelCase						     =			['''prompt''']
     lowerCAmelCase						     =			['''prompt''']
     lowerCAmelCase						     =			[
         '''num_images_per_prompt''',
         '''num_inference_steps''',
         '''generator''',
         '''latents''',
         '''guidance_scale''',
         '''frame_size''',
         '''output_type''',
         '''return_dict''',
     ]
     lowerCAmelCase						     =			False


     @property
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          return 32


     @property
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          return 32


     @property
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          return self.time_input_dim * 4


     @property
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          return 8


     @property
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Optional[Any]						=			CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip')
          return tokenizer


     @property
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          torch.manual_seed(0)
          __A		:					Tuple						=			CLIPTextConfig(
              bos_token_id=0		,      eos_token_id=2		,      hidden_size=self.text_embedder_hidden_size		,      projection_dim=self.text_embedder_hidden_size		,      intermediate_size=37		,      layer_norm_eps=1e-0_5		,      num_attention_heads=4		,      num_hidden_layers=5		,      pad_token_id=1		,      vocab_size=1000		,      )
          return CLIPTextModelWithProjection(_lowerCamelCase)


     @property
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          torch.manual_seed(0)

          __A		:					List[str]						=			{
              '''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''',
              '''encoder_hid_proj_type''': None,
              '''added_emb_type''': None,
          }

          __A		:					Any						=			PriorTransformer(**_lowerCamelCase)
          return model


     @property
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          torch.manual_seed(0)

          __A		:					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,
              ),
          }
          __A		:					List[str]						=			ShapERenderer(**_lowerCamelCase)
          return model


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Union[str, Any]						=			self.dummy_prior
          __A		:					str						=			self.dummy_text_encoder
          __A		:					Optional[Any]						=			self.dummy_tokenizer
          __A		:					List[Any]						=			self.dummy_renderer

          __A		:					Any						=			HeunDiscreteScheduler(
              beta_schedule='exp'		,      num_train_timesteps=1024		,      prediction_type='sample'		,      use_karras_sigmas=_lowerCamelCase		,      clip_sample=_lowerCamelCase		,      clip_sample_range=1.0		,      )
          __A		:					int						=			{
              '''prior''': prior,
              '''text_encoder''': text_encoder,
              '''tokenizer''': tokenizer,
              '''renderer''': renderer,
              '''scheduler''': scheduler,
          }

          return components


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase=0):

          '''simple docstring'''






          if str(_lowerCamelCase).startswith('mps'):
               __A		:					List[str]						=			torch.manual_seed(_lowerCamelCase)
          else:
               __A		:					Any						=			torch.Generator(device=_lowerCamelCase).manual_seed(_lowerCamelCase)
          __A		:					List[str]						=			{
              '''prompt''': '''horse''',
              '''generator''': generator,
              '''num_inference_steps''': 1,
              '''frame_size''': 32,
              '''output_type''': '''np''',
          }
          return inputs


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Tuple						=			'''cpu'''

          __A		:					Dict						=			self.get_dummy_components()

          __A		:					Tuple						=			self.pipeline_class(**_lowerCamelCase)
          __A		:					Any						=			pipe.to(_lowerCamelCase)

          pipe.set_progress_bar_config(disable=_lowerCamelCase)

          __A		:					List[str]						=			pipe(**self.get_dummy_inputs(_lowerCamelCase))
          __A		:					Union[str, Any]						=			output.images[0]
          __A		:					Tuple						=			image[0, -3:, -3:, -1]

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

          __A		:					Dict						=			np.array(
              [
                  0.00039216,
                  0.00039216,
                  0.00039216,
                  0.00039216,
                  0.00039216,
                  0.00039216,
                  0.00039216,
                  0.00039216,
                  0.00039216,
              ])

          assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          self._test_inference_batch_consistent(batch_sizes=[1, 2])


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Tuple						=			torch_device == '''cpu'''
          __A		:					List[Any]						=			True

          self._test_inference_batch_single_identical(
              batch_size=2		,      test_max_difference=_lowerCamelCase		,      relax_max_difference=_lowerCamelCase		,      )


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Union[str, Any]						=			self.get_dummy_components()
          __A		:					str						=			self.pipeline_class(**_lowerCamelCase)
          __A		:					Tuple						=			pipe.to(_lowerCamelCase)
          pipe.set_progress_bar_config(disable=_lowerCamelCase)

          __A		:					Dict						=			1
          __A		:					Tuple						=			2

          __A		:					str						=			self.get_dummy_inputs(_lowerCamelCase)

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

          __A		:					List[str]						=			pipe(**_lowerCamelCase		,      num_images_per_prompt=_lowerCamelCase)[0]

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




@slow
@require_torch_gpu
class 					SCREAMING_SNAKE_CASE       (unittest.TestCase				):


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Union[str, Any]						=			load_numpy(
              'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
              '/shap_e/test_shap_e_np_out.npy')
          __A		:					str						=			ShapEPipeline.from_pretrained('openai/shap-e')
          __A		:					Any						=			pipe.to(_lowerCamelCase)
          pipe.set_progress_bar_config(disable=_lowerCamelCase)

          __A		:					List[Any]						=			torch.Generator(device=_lowerCamelCase).manual_seed(0)

          __A		:					Union[str, Any]						=			pipe(
              'a shark'		,      generator=_lowerCamelCase		,      guidance_scale=15.0		,      num_inference_steps=64		,      frame_size=64		,      output_type='np'		,      ).images[0]

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

          assert_mean_pixel_difference(_lowerCamelCase		,      _lowerCamelCase)



import json
from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_roberta import RobertaTokenizer


snake_case							:       List[Any]										=	logging.get_logger(__name__)

snake_case							:       Optional[int]										=	{'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''}

snake_case							:       Union[str, Any]										=	{
    '''vocab_file''': {
        '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/vocab.json''',
        '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/vocab.json''',
        '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json''',
        '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/vocab.json''',
        '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json''',
        '''roberta-large-openai-detector''': (
            '''https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json'''
        ),
    },
    '''merges_file''': {
        '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/merges.txt''',
        '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/merges.txt''',
        '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt''',
        '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/merges.txt''',
        '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt''',
        '''roberta-large-openai-detector''': (
            '''https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt'''
        ),
    },
    '''tokenizer_file''': {
        '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/tokenizer.json''',
        '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/tokenizer.json''',
        '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json''',
        '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json''',
        '''roberta-base-openai-detector''': (
            '''https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json'''
        ),
        '''roberta-large-openai-detector''': (
            '''https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json'''
        ),
    },
}

snake_case							:       Optional[int]										=	{
    '''roberta-base''': 5_12,
    '''roberta-large''': 5_12,
    '''roberta-large-mnli''': 5_12,
    '''distilroberta-base''': 5_12,
    '''roberta-base-openai-detector''': 5_12,
    '''roberta-large-openai-detector''': 5_12,
}







class 						_snake_case    ( _snake_case				):
 SCREAMING_SNAKE_CASE__ =			VOCAB_FILES_NAMES
 SCREAMING_SNAKE_CASE__ =			PRETRAINED_VOCAB_FILES_MAP
 SCREAMING_SNAKE_CASE__ =			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 SCREAMING_SNAKE_CASE__ =			['input_ids', 'attention_mask']
 SCREAMING_SNAKE_CASE__ =			RobertaTokenizer




 def __init__(						self   ,    _lowerCamelCase=None   ,    _lowerCamelCase=None   ,    _lowerCamelCase=None   ,    _lowerCamelCase="replace"   ,    _lowerCamelCase="<s>"   ,    _lowerCamelCase="</s>"   ,    _lowerCamelCase="</s>"   ,    _lowerCamelCase="<s>"   ,    _lowerCamelCase="<unk>"   ,    _lowerCamelCase="<pad>"   ,    _lowerCamelCase="<mask>"   ,    _lowerCamelCase=False   ,    _lowerCamelCase=True   ,    **_lowerCamelCase   ,    ):
       super().__init__(
           _lowerCamelCase   ,    _lowerCamelCase   ,    tokenizer_file=_lowerCamelCase   ,    errors=_lowerCamelCase   ,    bos_token=_lowerCamelCase   ,    eos_token=_lowerCamelCase   ,    sep_token=_lowerCamelCase   ,    cls_token=_lowerCamelCase   ,    unk_token=_lowerCamelCase   ,    pad_token=_lowerCamelCase   ,    mask_token=_lowerCamelCase   ,    add_prefix_space=_lowerCamelCase   ,    trim_offsets=_lowerCamelCase   ,    **_lowerCamelCase   ,    )

       a		:List[Any]									=			json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()       )
       if pre_tok_state.get('''add_prefix_space'''   ,    _lowerCamelCase       ) != add_prefix_space:
             a		:Optional[int]									=			getattr(_lowerCamelCase   ,    pre_tok_state.pop('''type'''       )       )
             a		:Union[str, Any]									=			add_prefix_space
             a		:Dict									=			pre_tok_class(**_lowerCamelCase       )

       a		:Union[str, Any]									=			add_prefix_space

       a		:List[Any]									=			'''post_processor'''
       a		:List[str]									=			getattr(self.backend_tokenizer   ,    _lowerCamelCase   ,    _lowerCamelCase       )
       if tokenizer_component_instance:
             a		:Optional[Any]									=			json.loads(tokenizer_component_instance.__getstate__()       )

             # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
             if "sep" in state:
                   a		:Optional[int]									=			tuple(state['''sep''']       )
             if "cls" in state:
                   a		:Union[str, Any]									=			tuple(state['''cls''']       )

             a		:Optional[int]									=			False

             if state.get('''add_prefix_space'''   ,    _lowerCamelCase       ) != add_prefix_space:
                   a		:int									=			add_prefix_space
                   a		:Union[str, Any]									=			True

             if state.get('''trim_offsets'''   ,    _lowerCamelCase       ) != trim_offsets:
                   a		:int									=			trim_offsets
                   a		:List[str]									=			True

             if changes_to_apply:
                   a		:Union[str, Any]									=			getattr(_lowerCamelCase   ,    state.pop('''type'''       )       )
                   a		:str									=			component_class(**_lowerCamelCase       )
                   setattr(self.backend_tokenizer   ,    _lowerCamelCase   ,    _lowerCamelCase       )




 @property
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       if self._mask_token is None:
             if self.verbose:
                   logger.error('''Using mask_token, but it is not set yet.'''       )
             return None
       return str(self._mask_token       )




 @mask_token.setter
 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase       ):
       a		:Union[str, Any]									=			AddedToken(_lowerCamelCase   ,    lstrip=_lowerCamelCase   ,    rstrip=_lowerCamelCase       ) if isinstance(_lowerCamelCase   ,    _lowerCamelCase       ) else value
       a		:int									=			value




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    *_lowerCamelCase   ,    **_lowerCamelCase       ):
       a		:Union[str, Any]									=			kwargs.get('''is_split_into_words'''   ,    _lowerCamelCase       )
       assert self.add_prefix_space or not is_split_into_words, (
           F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True '''
           "to use it with pretokenized inputs."
       )

       return super()._batch_encode_plus(*_lowerCamelCase   ,    **_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    *_lowerCamelCase   ,    **_lowerCamelCase       ):
       a		:Any									=			kwargs.get('''is_split_into_words'''   ,    _lowerCamelCase       )

       assert self.add_prefix_space or not is_split_into_words, (
           F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True '''
           "to use it with pretokenized inputs."
       )

       return super()._encode_plus(*_lowerCamelCase   ,    **_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase = None       ):
       a		:Any									=			self._tokenizer.model.save(_lowerCamelCase   ,    name=_lowerCamelCase       )
       return tuple(_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase=None       ):
       a		:Union[str, Any]									=			[self.bos_token_id] + token_ids_a + [self.eos_token_id]
       if token_ids_a is None:
             return output

       return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase = None       ):
       a		:Dict									=			[self.sep_token_id]
       a		: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]





