





'''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 			_lowerCAmelCase		(			A__							,     unittest.TestCase	):






     """simple docstring"""






     snake_case_						      =   ShapEPipeline
     snake_case_						      =   ["prompt"]
     snake_case_						      =   ["prompt"]
     snake_case_						      =   [
         "num_images_per_prompt",
         "num_inference_steps",
         "generator",
         "latents",
         "guidance_scale",
         "frame_size",
         "output_type",
         "return_dict",
     ]
     snake_case_						      =   False



     @property
     def      lowerCAmelCase			(   self							:			Optional[int]						)->	Union[str, Any]:
        return 32



     @property
     def      lowerCAmelCase			(   self							:			Dict						)->	List[str]:
        return 32



     @property
     def      lowerCAmelCase			(   self							:			Dict						)->	Union[str, Any]:
        return self.time_input_dim * 4



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



     @property
     def      lowerCAmelCase			(   self							:			Dict						)->	str:
        snake_case 						=   CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""						)
        return tokenizer



     @property
     def      lowerCAmelCase			(   self							:			int						)->	List[str]:
        torch.manual_seed(0						)
        snake_case 						=   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-05   ,       num_attention_heads=4   ,       num_hidden_layers=5   ,       pad_token_id=1   ,       vocab_size=10_00   ,       )
        return CLIPTextModelWithProjection(__snake_case						)



     @property
     def      lowerCAmelCase			(   self							:			List[str]						)->	Union[str, Any]:
        torch.manual_seed(0						)

        snake_case 						=   {
            """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,
        }

        snake_case 						=   PriorTransformer(**__snake_case						)
        return model



     @property
     def      lowerCAmelCase			(   self							:			int						)->	Any:
        torch.manual_seed(0						)

        snake_case 						=   {
            """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,
            ),
        }
        snake_case 						=   ShapERenderer(**__snake_case						)
        return model



     def      lowerCAmelCase			(   self							:			Optional[int]						)->	List[str]:
        snake_case 						=   self.dummy_prior
        snake_case 						=   self.dummy_text_encoder
        snake_case 						=   self.dummy_tokenizer
        snake_case 						=   self.dummy_renderer

        snake_case 						=   HeunDiscreteScheduler(
            beta_schedule="""exp"""   ,       num_train_timesteps=10_24   ,       prediction_type="""sample"""   ,       use_karras_sigmas=__snake_case   ,       clip_sample=__snake_case   ,       clip_sample_range=1.0   ,       )
        snake_case 						=   {
            """prior""": prior,
            """text_encoder""": text_encoder,
            """tokenizer""": tokenizer,
            """renderer""": renderer,
            """scheduler""": scheduler,
        }

        return components



     def      lowerCAmelCase			(   self							:			Tuple   ,       __snake_case							:			List[Any]   ,       __snake_case							:			Any=0						)->	str:
        if str(__snake_case						).startswith("""mps"""						):
           snake_case 						=   torch.manual_seed(__snake_case						)
        else:
           snake_case 						=   torch.Generator(device=__snake_case						).manual_seed(__snake_case						)
        snake_case 						=   {
            """prompt""": """horse""",
            """generator""": generator,
            """num_inference_steps""": 1,
            """frame_size""": 32,
            """output_type""": """np""",
        }
        return inputs



     def      lowerCAmelCase			(   self							:			List[Any]						)->	Tuple:
        snake_case 						=   """cpu"""

        snake_case 						=   self.get_dummy_components()

        snake_case 						=   self.pipeline_class(**__snake_case						)
        snake_case 						=   pipe.to(__snake_case						)

        pipe.set_progress_bar_config(disable=__snake_case						)

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

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

        snake_case 						=   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      lowerCAmelCase			(   self							:			int						)->	Tuple:
        # 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							:			Tuple						)->	Optional[Any]:
        snake_case 						=   torch_device == """cpu"""
        snake_case 						=   True

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



     def      lowerCAmelCase			(   self							:			Tuple						)->	Dict:
        snake_case 						=   self.get_dummy_components()
        snake_case 						=   self.pipeline_class(**__snake_case						)
        snake_case 						=   pipe.to(__snake_case						)
        pipe.set_progress_bar_config(disable=__snake_case						)

        snake_case 						=   1
        snake_case 						=   2

        snake_case 						=   self.get_dummy_inputs(__snake_case						)

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

        snake_case 						=   pipe(**__snake_case   ,       num_images_per_prompt=__snake_case						)[0]

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




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






     """simple docstring"""






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



     def      lowerCAmelCase			(   self							:			Optional[Any]						)->	Union[str, Any]:
        snake_case 						=   load_numpy(
            """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
            """/shap_e/test_shap_e_np_out.npy"""						)
        snake_case 						=   ShapEPipeline.from_pretrained("""openai/shap-e"""						)
        snake_case 						=   pipe.to(__snake_case						)
        pipe.set_progress_bar_config(disable=__snake_case						)

        snake_case 						=   torch.Generator(device=__snake_case						).manual_seed(0						)

        snake_case 						=   pipe(
            """a shark"""   ,       generator=__snake_case   ,       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(__snake_case   ,       __snake_case						)








'''simple docstring'''




import json
import os
import unittest

from transformers import DebertaTokenizer, DebertaTokenizerFast
from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin
class 			_lowerCAmelCase		(			A__							,     unittest.TestCase	):






     """simple docstring"""






     snake_case_						      =   DebertaTokenizer
     snake_case_						      =   True
     snake_case_						      =   DebertaTokenizerFast



     def      lowerCAmelCase			(   self							:			Any						)->	Optional[int]:
        super().setUp()

        # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
        snake_case 						=   [
            """l""",
            """o""",
            """w""",
            """e""",
            """r""",
            """s""",
            """t""",
            """i""",
            """d""",
            """n""",
            """\u0120""",
            """\u0120l""",
            """\u0120n""",
            """\u0120lo""",
            """\u0120low""",
            """er""",
            """\u0120lowest""",
            """\u0120newer""",
            """\u0120wider""",
            """[UNK]""",
        ]
        snake_case 						=   dict(zip(__snake_case   ,       range(len(__snake_case						)						)						)						)
        snake_case 						=   ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""]
        snake_case 						=   {"""unk_token""": """[UNK]"""}

        snake_case 						=   os.path.join(self.tmpdirname   ,       VOCAB_FILES_NAMES["""vocab_file"""]						)
        snake_case 						=   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(__snake_case						) + """\n"""						)
        with open(self.merges_file   ,       """w"""   ,       encoding="""utf-8"""						) as fp:
           fp.write("""\n""".join(__snake_case						)						)



     def      lowerCAmelCase			(   self							:			Optional[Any]   ,       **__snake_case							:			Union[str, Any]						)->	int:
        kwargs.update(self.special_tokens_map						)
        return self.tokenizer_class.from_pretrained(self.tmpdirname   ,       **__snake_case						)



     def      lowerCAmelCase			(   self							:			Any   ,       __snake_case							:			int						)->	Any:
        snake_case 						=   """lower newer"""
        snake_case 						=   """lower newer"""
        return input_text, output_text



     def      lowerCAmelCase			(   self							:			str						)->	Any:
        snake_case 						=   self.get_tokenizer()
        snake_case 						=   """lower newer"""
        snake_case 						=   ["""l""", """o""", """w""", """er""", """\u0120""", """n""", """e""", """w""", """er"""]
        snake_case 						=   tokenizer.tokenize(__snake_case						)
        self.assertListEqual(__snake_case   ,       __snake_case						)

        snake_case 						=   tokens + [tokenizer.unk_token]
        snake_case 						=   [0, 1, 2, 15, 10, 9, 3, 2, 15, 19]
        self.assertListEqual(tokenizer.convert_tokens_to_ids(__snake_case						)   ,       __snake_case						)



     def      lowerCAmelCase			(   self							:			Any						)->	List[str]:
        snake_case 						=   self.get_tokenizer()
        snake_case 						=   tokenizer("""Hello"""   ,       """World"""						)
        snake_case 						=   [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
        self.assertListEqual(tokd["""token_type_ids"""]   ,       __snake_case						)



     @slow
     def      lowerCAmelCase			(   self							:			Optional[int]						)->	Union[str, Any]:
        snake_case 						=   self.tokenizer_class.from_pretrained("""microsoft/deberta-base"""						)

        snake_case 						=   tokenizer.encode("""sequence builders"""   ,       add_special_tokens=__snake_case						)
        snake_case 						=   tokenizer.encode("""multi-sequence build"""   ,       add_special_tokens=__snake_case						)

        snake_case 						=   tokenizer.encode(
            """sequence builders"""   ,       add_special_tokens=__snake_case   ,       add_prefix_space=__snake_case						)
        snake_case 						=   tokenizer.encode(
            """sequence builders"""   ,       """multi-sequence build"""   ,       add_special_tokens=__snake_case   ,       add_prefix_space=__snake_case						)

        snake_case 						=   tokenizer.build_inputs_with_special_tokens(__snake_case						)
        snake_case 						=   tokenizer.build_inputs_with_special_tokens(__snake_case   ,       __snake_case						)

        assert encoded_sentence == encoded_text_from_decode
        assert encoded_pair == encoded_pair_from_decode



     @slow
     def      lowerCAmelCase			(   self							:			Any						)->	Union[str, Any]:
        snake_case 						=   [self.tokenizer_class]
        if self.test_rust_tokenizer:
           tokenizer_classes.append(self.rust_tokenizer_class						)

        for tokenizer_class in tokenizer_classes:
           snake_case 						=   tokenizer_class.from_pretrained("""microsoft/deberta-base"""						)

           snake_case 						=   [
               """ALBERT: A Lite BERT for Self-supervised Learning of Language Representations""",
               """ALBERT incorporates two parameter reduction techniques""",
               """The first one is a factorized embedding parameterization. By decomposing the large vocabulary"""
               """ embedding matrix into two small matrices, we separate the size of the hidden layers from the size of"""
               """ vocabulary embedding.""",
           ]

           snake_case 						=   tokenizer(__snake_case   ,       padding=__snake_case						)
           snake_case 						=   [tokenizer.decode(__snake_case   ,       skip_special_tokens=__snake_case						) for seq in encoding["""input_ids"""]]

           # fmt: off
           snake_case 						=   {
               """input_ids""": [
                   [1, 21_18, 1_11_26, 5_65, 35, 83, 2_51_91, 1_63, 1_88_54, 13, 1_21_56, 12, 1_61_01, 2_53_76, 1_38_07, 9, 2_22_05, 2_78_93, 16_35, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [1, 21_18, 1_11_26, 5_65, 2_45_36, 80, 4_37_97, 48_78, 73_73, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [1, 1_33, 78, 65, 16, 10, 37_24, 15_38, 3_31_83, 1_13_03, 4_37_97, 19_38, 4, 8_70, 2_41_65, 2_91_05, 5, 7_39, 3_26_44, 3_31_83, 1_13_03, 3_61_73, 88, 80, 6_50, 78_21, 4_59_40, 6, 52, 25_59, 5, 18_36, 9, 5, 73_97, 1_31_71, 31, 5, 18_36, 9, 3_26_44, 3_31_83, 1_13_03, 4, 2]
               ],
               """token_type_ids""": [
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
               ],
               """attention_mask""": [
                   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
               ]
           }
           # fmt: on

           snake_case 						=   [
               """ALBERT: A Lite BERT for Self-supervised Learning of Language Representations""",
               """ALBERT incorporates two parameter reduction techniques""",
               """The first one is a factorized embedding parameterization. By decomposing the large vocabulary"""
               """ embedding matrix into two small matrices, we separate the size of the hidden layers from the size of"""
               """ vocabulary embedding.""",
           ]

           self.assertDictEqual(encoding.data   ,       __snake_case						)

           for expected, decoded in zip(__snake_case   ,       __snake_case						):
              self.assertEqual(__snake_case   ,       __snake_case						)

