


'''simple docstring'''






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 		a__    (							a__    ,    unittest.TestCase  ):





							'''simple docstring'''






							lowercase__      :       List[str]     						=				ShapEImgaImgPipeline
							lowercase__      :       Dict     						=				["image"]
							lowercase__      :       Optional[int]     						=				["image"]
							lowercase__      :       Optional[Any]     						=				[
							    "num_images_per_prompt",
							    "num_inference_steps",
							    "generator",
							    "latents",
							    "guidance_scale",
							    "frame_size",
							    "output_type",
							    "return_dict",
							]
							lowercase__      :       List[str]     						=				False







							@property
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							int:
														return 32







							@property
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							int:
														return 32







							@property
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Dict:
														return self.time_input_dim * 4







							@property
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Optional[int]:
														return 8







							@property
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							List[str]:
														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(lowerCamelCase_       )
														return model







							@property
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							int:
														lowerCAmelCase__	       =     CLIPImageProcessor(
														    crop_size=2_24 ,   do_center_crop=lowerCamelCase_ ,   do_normalize=lowerCamelCase_ ,   do_resize=lowerCamelCase_ ,   image_mean=[0.48_145_466, 0.4_578_275, 0.40_821_073] ,   image_std=[0.26_862_954, 0.26_130_258, 0.27_577_711] ,   resample=3 ,   size=2_24 ,   )

														return image_processor







							@property
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Dict:
														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(**lowerCamelCase_       )
														return model







							@property
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							int:
														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(**lowerCamelCase_       )
														return model







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Optional[int]:
														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=lowerCamelCase_ ,   clip_sample=lowerCamelCase_ ,   clip_sample_range=1.0 ,   )
														lowerCAmelCase__	       =     {
														    '''prior''': prior,
														    '''image_encoder''': image_encoder,
														    '''image_processor''': image_processor,
														    '''renderer''': renderer,
														    '''scheduler''': scheduler,
														}

														return components







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_=0       )	->							Union[str, Any]:
														lowerCAmelCase__	       =     floats_tensor((1, 3, 64, 64) ,   rng=random.Random(lowerCamelCase_       )       ).to(lowerCamelCase_       )

														if str(lowerCamelCase_       ).startswith('''mps'''       ):
																					lowerCAmelCase__	       =     torch.manual_seed(lowerCamelCase_       )
														else:
																					lowerCAmelCase__	       =     torch.Generator(device=lowerCamelCase_       ).manual_seed(lowerCamelCase_       )
														lowerCAmelCase__	       =     {
														    '''image''': input_image,
														    '''generator''': generator,
														    '''num_inference_steps''': 1,
														    '''frame_size''': 32,
														    '''output_type''': '''np''',
														}
														return inputs







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							List[str]:
														lowerCAmelCase__	       =     '''cpu'''

														lowerCAmelCase__	       =     self.get_dummy_components()

														lowerCAmelCase__	       =     self.pipeline_class(**lowerCamelCase_       )
														lowerCAmelCase__	       =     pipe.to(lowerCamelCase_       )

														pipe.set_progress_bar_config(disable=lowerCamelCase_       )

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

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

														lowerCAmelCase__	       =     np.array(
														    [
														        0.00_039_216,
														        0.00_039_216,
														        0.00_039_216,
														        0.00_039_216,
														        0.00_039_216,
														        0.00_039_216,
														        0.00_039_216,
														        0.00_039_216,
														        0.00_039_216,
														    ]       )

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







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							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   __SCREAMING_SNAKE_CASE							(		self       )	->							Optional[Any]:
														lowerCAmelCase__	       =     torch_device == '''cpu'''
														lowerCAmelCase__	       =     True
														self._test_inference_batch_single_identical(
														    batch_size=2 ,   test_max_difference=lowerCamelCase_ ,   relax_max_difference=lowerCamelCase_ ,   )







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							str:
														lowerCAmelCase__	       =     self.get_dummy_components()
														lowerCAmelCase__	       =     self.pipeline_class(**lowerCamelCase_       )
														lowerCAmelCase__	       =     pipe.to(lowerCamelCase_       )
														pipe.set_progress_bar_config(disable=lowerCamelCase_       )

														lowerCAmelCase__	       =     1
														lowerCAmelCase__	       =     2

														lowerCAmelCase__	       =     self.get_dummy_inputs(lowerCamelCase_       )

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

														lowerCAmelCase__	       =     pipe(**lowerCamelCase_ ,   num_images_per_prompt=lowerCamelCase_       )[0]

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




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





							'''simple docstring'''






							def   __SCREAMING_SNAKE_CASE							(		self       )	->							List[Any]:
														# clean up the VRAM after each test
														super().tearDown()
														gc.collect()
														torch.cuda.empty_cache()







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Dict:
														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(lowerCamelCase_       )
														pipe.set_progress_bar_config(disable=lowerCamelCase_       )

														lowerCAmelCase__	       =     torch.Generator(device=lowerCamelCase_       ).manual_seed(0       )

														lowerCAmelCase__	       =     pipe(
														    lowerCamelCase_ ,   generator=lowerCamelCase_ ,   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(lowerCamelCase_ ,   lowerCamelCase_       )



'''simple docstring'''






import json
import os
import unittest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin




@require_tokenizers
class 		a__    (							a__    ,    unittest.TestCase  ):





							'''simple docstring'''






							lowercase__      :       List[Any]     						=				CLIPTokenizer
							lowercase__      :       List[str]     						=				CLIPTokenizerFast
							lowercase__      :       Dict     						=				True
							lowercase__      :       Any     						=				{}
							lowercase__      :       Optional[int]     						=				False







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Any:
														super().setUp()

														# fmt: off
														lowerCAmelCase__	       =     ['''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''lo''', '''l</w>''', '''w</w>''', '''r</w>''', '''t</w>''', '''low</w>''', '''er</w>''', '''lowest</w>''', '''newer</w>''', '''wider''', '''<unk>''', '''<|startoftext|>''', '''<|endoftext|>''']
														# fmt: on
														lowerCAmelCase__	       =     dict(zip(lowerCamelCase_ ,   range(len(lowerCamelCase_       )       )       )       )
														lowerCAmelCase__	       =     ['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''']
														lowerCAmelCase__	       =     {'''unk_token''': '''<unk>'''}

														lowerCAmelCase__	       =     os.path.join(self.tmpdirname ,   VOCAB_FILES_NAMES['''vocab_file''']       )
														lowerCAmelCase__	       =     os.path.join(self.tmpdirname ,   VOCAB_FILES_NAMES['''merges_file''']       )
														with open(self.vocab_file ,   '''w''' ,   encoding='''utf-8'''       ) as fp:
																					fp.write(json.dumps(lowerCamelCase_       ) + '''\n'''       )
														with open(self.merges_file ,   '''w''' ,   encoding='''utf-8'''       ) as fp:
																					fp.write('''\n'''.join(lowerCamelCase_       )       )







							def   __SCREAMING_SNAKE_CASE							(		self ,   **lowerCamelCase_       )	->							Any:
														kwargs.update(self.special_tokens_map       )
														return CLIPTokenizer.from_pretrained(self.tmpdirname ,   **lowerCamelCase_       )







							def   __SCREAMING_SNAKE_CASE							(		self ,   **lowerCamelCase_       )	->							int:
														kwargs.update(self.special_tokens_map       )
														return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,   **lowerCamelCase_       )







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_       )	->							Any:
														lowerCAmelCase__	       =     '''lower newer'''
														lowerCAmelCase__	       =     '''lower newer'''
														return input_text, output_text







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Union[str, Any]:
														lowerCAmelCase__	       =     CLIPTokenizer(self.vocab_file ,   self.merges_file ,   **self.special_tokens_map       )
														lowerCAmelCase__	       =     '''lower newer'''
														lowerCAmelCase__	       =     ['''lo''', '''w''', '''er</w>''', '''n''', '''e''', '''w''', '''er</w>''']
														lowerCAmelCase__	       =     tokenizer.tokenize(lowerCamelCase_       )
														self.assertListEqual(lowerCamelCase_ ,   lowerCamelCase_       )

														lowerCAmelCase__	       =     tokens + [tokenizer.unk_token]
														lowerCAmelCase__	       =     [10, 2, 16, 9, 3, 2, 16, 20]
														self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase_       ) ,   lowerCamelCase_       )







							@require_ftfy
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Tuple:
														for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
																					with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})"""       ):
																												lowerCAmelCase__	       =     self.tokenizer_class.from_pretrained(lowerCamelCase_ ,   **lowerCamelCase_       )
																												lowerCAmelCase__	       =     self.rust_tokenizer_class.from_pretrained(lowerCamelCase_ ,   **lowerCamelCase_       )

																												lowerCAmelCase__	       =     '''A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.'''
																												lowerCAmelCase__	       =     tokenizer_s.tokenize(lowerCamelCase_       )
																												lowerCAmelCase__	       =     tokenizer_r.tokenize(lowerCamelCase_       )

																												self.assertListEqual(lowerCamelCase_ ,   lowerCamelCase_       )

																												# Test that the tokenization is identical on an example containing a character (Latin Small Letter A
																												# with Tilde) encoded in 2 different ways
																												lowerCAmelCase__	       =     '''xa\u0303y''' + ''' ''' + '''x\xe3y'''
																												lowerCAmelCase__	       =     tokenizer_s.tokenize(lowerCamelCase_       )
																												lowerCAmelCase__	       =     tokenizer_r.tokenize(lowerCamelCase_       )

																												self.assertListEqual(lowerCamelCase_ ,   lowerCamelCase_       )

																												# Test that the tokenization is identical on unicode of space type
																												lowerCAmelCase__	       =     [
																												    '''\u0009''',  # (horizontal tab, '\t')
																												    '''\u000B''',  # (vertical tab)
																												    '''\u000C''',  # (form feed)
																												    '''\u0020''',  # (space, ' ')
																												    '''\u200E''',  # (left-to-right mark):w
																												    '''\u200F''',  # (right-to-left mark)
																												]
																												for unicode_seq in spaces_unicodes:
																																			lowerCAmelCase__	       =     tokenizer_s.tokenize(lowerCamelCase_       )
																																			lowerCAmelCase__	       =     tokenizer_r.tokenize(lowerCamelCase_       )

																																			self.assertListEqual(lowerCamelCase_ ,   lowerCamelCase_       )

																												# Test that the tokenization is identical on unicode of line break type
																												lowerCAmelCase__	       =     [
																												    '''\u000A''',  # (line feed, '\n')
																												    '''\r\n''',  # (carriage return and line feed, '\r\n')
																												    '''\u000D''',  # (carriage return, '\r')
																												    '''\r''',  # (carriage return, '\r')
																												    '''\u000D''',  # (carriage return, '\r')
																												    '''\u2028''',  # (line separator)
																												    '''\u2029''',  # (paragraph separator)
																												    # "\u0085", # (next line)
																												]

																												# The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms
																												# it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a
																												# space (and thus into an empty list).

																												for unicode_seq in line_break_unicodes:
																																			lowerCAmelCase__	       =     tokenizer_s.tokenize(lowerCamelCase_       )
																																			lowerCAmelCase__	       =     tokenizer_r.tokenize(lowerCamelCase_       )

																																			self.assertListEqual(lowerCamelCase_ ,   lowerCamelCase_       )







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							str:
														# Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space`
														for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
																					with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})"""       ):
																												lowerCAmelCase__	       =     '''hello'''  # `hello` is a token in the vocabulary of `pretrained_name`
																												lowerCAmelCase__	       =     F"""{text_of_1_token} {text_of_1_token}"""

																												lowerCAmelCase__	       =     self.rust_tokenizer_class.from_pretrained(
																												    lowerCamelCase_ ,   use_fast=lowerCamelCase_ ,   )
																												lowerCAmelCase__	       =     tokenizer_r(lowerCamelCase_ ,   return_offsets_mapping=lowerCamelCase_ ,   add_special_tokens=lowerCamelCase_       )
																												self.assertEqual(encoding.offset_mapping[0] ,   (0, len(lowerCamelCase_       ))       )
																												self.assertEqual(
																												    encoding.offset_mapping[1] ,   (len(lowerCamelCase_       ) + 1, len(lowerCamelCase_       ) + 1 + len(lowerCamelCase_       )) ,   )

																												lowerCAmelCase__	       =     F""" {text}"""

																												lowerCAmelCase__	       =     self.rust_tokenizer_class.from_pretrained(
																												    lowerCamelCase_ ,   use_fast=lowerCamelCase_ ,   )
																												lowerCAmelCase__	       =     tokenizer_r(lowerCamelCase_ ,   return_offsets_mapping=lowerCamelCase_ ,   add_special_tokens=lowerCamelCase_       )
																												self.assertEqual(encoding.offset_mapping[0] ,   (1, 1 + len(lowerCamelCase_       ))       )
																												self.assertEqual(
																												    encoding.offset_mapping[1] ,   (1 + len(lowerCamelCase_       ) + 1, 1 + len(lowerCamelCase_       ) + 1 + len(lowerCamelCase_       )) ,   )







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Union[str, Any]:
														# Test related to the breaking change introduced in transformers v4.17.0
														# We need to check that an error in raised when the user try to load a previous version of the tokenizer.
														with self.assertRaises(lowerCamelCase_       ) as context:
																					self.rust_tokenizer_class.from_pretrained('''robot-test/old-clip-tokenizer'''       )

														self.assertTrue(
														    context.exception.args[0].startswith(
														        '''The `backend_tokenizer` provided does not match the expected format.'''       )       )







							@require_ftfy
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							List[Any]:
														super().test_tokenization_python_rust_equals()







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Any:
														# CLIP always lower cases letters
														pass