






import unittest

import torch

from diffusers import UNetaDModel
from diffusers.utils import floats_tensor, slow, torch_device

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


class  __lowercase (		lowerCAmelCase__     , lowerCAmelCase__     , unittest.TestCase					):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE				  =			UNetaDModel
     SCREAMING_SNAKE_CASE				  =			"sample"




     @property
     def 						lowerCAmelCase_       (			self    :     int			):


         """simple docstring"""



         __A             =		4
         __A             =		14
         __A             =		16

         __A             =		floats_tensor((batch_size, num_features, seq_len)			).to(_A			)
         __A             =		torch.tensor([10] * batch_size			).to(_A			)

         return {"sample": noise, "timestep": time_step}




     @property
     def 						lowerCAmelCase_       (			self    :     Dict			):


         """simple docstring"""



         return (4, 14, 16)




     @property
     def 						lowerCAmelCase_       (			self    :     int			):


         """simple docstring"""



         return (4, 14, 16)




     def 						lowerCAmelCase_       (			self    :     List[Any]			):


         """simple docstring"""



         pass




     def 						lowerCAmelCase_       (			self    :     Dict			):


         """simple docstring"""



         pass




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     Optional[int]			):


         """simple docstring"""



         super().test_determinism()




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     Optional[Any]			):


         """simple docstring"""



         super().test_outputs_equivalence()




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     Any			):


         """simple docstring"""



         super().test_from_save_pretrained()




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     List[Any]			):


         """simple docstring"""



         super().test_from_save_pretrained_variant()




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     Optional[Any]			):


         """simple docstring"""



         super().test_model_from_pretrained()




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     Tuple			):


         """simple docstring"""



         super().test_output()




     def 						lowerCAmelCase_       (			self    :     List[str]			):


         """simple docstring"""



         __A             =		{
             '''block_out_channels''': (32, 64, 128, 256),
             '''in_channels''': 14,
             '''out_channels''': 14,
             '''time_embedding_type''': '''positional''',
             '''use_timestep_embedding''': True,
             '''flip_sin_to_cos''': False,
             '''freq_shift''': 1.0,
             '''out_block_type''': '''OutConv1DBlock''',
             '''mid_block_type''': '''MidResTemporalBlock1D''',
             '''down_block_types''': ('''DownResnetBlock1D''', '''DownResnetBlock1D''', '''DownResnetBlock1D''', '''DownResnetBlock1D'''),
             '''up_block_types''': ('''UpResnetBlock1D''', '''UpResnetBlock1D''', '''UpResnetBlock1D'''),
             '''act_fn''': '''mish''',
         }
         __A             =		self.dummy_input
         return init_dict, inputs_dict




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     Optional[int]			):


         """simple docstring"""



         __A             =		UNetaDModel.from_pretrained(
             """bglick13/hopper-medium-v2-value-function-hor32"""	,		output_loading_info=_A	,		subfolder="""unet"""			)
         self.assertIsNotNone(_A			)
         self.assertEqual(len(loading_info["""missing_keys"""]			)	,		0			)

         model.to(_A			)
         __A             =		model(**self.dummy_input			)

         assert image is not None, "Make sure output is not None"




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     Optional[int]			):


         """simple docstring"""



         __A             =		UNetaDModel.from_pretrained("""bglick13/hopper-medium-v2-value-function-hor32"""	,		subfolder="""unet"""			)
         torch.manual_seed(0			)
         if torch.cuda.is_available():
             torch.cuda.manual_seed_all(0			)

         __A             =		model.config.in_channels
         __A             =		16
         __A             =		torch.randn((1, seq_len, num_features)			).permute(
             0	,		2	,		1			)  # match original, we can update values and remove
         __A             =		torch.full((num_features,)	,		0			)

         with torch.no_grad():
             __A             =		model(_A	,		_A			).sample.permute(0	,		2	,		1			)

         __A             =		output[0, -3:, -3:].flatten()
         # fmt: off
         __A             =		torch.tensor([-2.137172, 1.1426016, 0.3688687, -0.766922, 0.7303146, 0.11038864, -0.4760633, 0.13270172, 0.02591348]			)
         # fmt: on
         self.assertTrue(torch.allclose(_A	,		_A	,		rtol=1e-3			)			)




     def 						lowerCAmelCase_       (			self    :     int			):


         """simple docstring"""



         pass





     @slow
     def 						lowerCAmelCase_       (			self    :     Optional[Any]			):


         """simple docstring"""



         __A             =		'''harmonai/maestro-150k'''
         __A             =		UNetaDModel.from_pretrained(_A	,		subfolder="""unet"""			)
         model.to(_A			)

         __A             =		65_536
         __A             =		torch.sin(torch.arange(_A			)[None, None, :].repeat(1	,		2	,		1			)			).to(_A			)
         __A             =		torch.tensor([1]			).to(_A			)

         with torch.no_grad():
             __A             =		model(_A	,		_A			).sample

         __A             =		output.abs().sum()
         __A             =		output.abs().max()

         assert (output_sum - 224.0_896).abs() < 0.5
         assert (output_max - 0.0607).abs() < 4e-4





class  __lowercase (		lowerCAmelCase__     , lowerCAmelCase__     , unittest.TestCase					):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE				  =			UNetaDModel
     SCREAMING_SNAKE_CASE				  =			"sample"




     @property
     def 						lowerCAmelCase_       (			self    :     Union[str, Any]			):


         """simple docstring"""



         __A             =		4
         __A             =		14
         __A             =		16

         __A             =		floats_tensor((batch_size, num_features, seq_len)			).to(_A			)
         __A             =		torch.tensor([10] * batch_size			).to(_A			)

         return {"sample": noise, "timestep": time_step}




     @property
     def 						lowerCAmelCase_       (			self    :     Any			):


         """simple docstring"""



         return (4, 14, 16)




     @property
     def 						lowerCAmelCase_       (			self    :     List[str]			):


         """simple docstring"""



         return (4, 14, 1)




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     Tuple			):


         """simple docstring"""



         super().test_determinism()




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     List[Any]			):


         """simple docstring"""



         super().test_outputs_equivalence()




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     str			):


         """simple docstring"""



         super().test_from_save_pretrained()




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     Optional[int]			):


         """simple docstring"""



         super().test_from_save_pretrained_variant()




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     int			):


         """simple docstring"""



         super().test_model_from_pretrained()




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     Union[str, Any]			):


         """simple docstring"""



         __A             =		self.prepare_init_args_and_inputs_for_common()
         __A             =		self.model_class(**_A			)
         model.to(_A			)
         model.eval()

         with torch.no_grad():
             __A             =		model(**_A			)

             if isinstance(_A	,		_A			):
                 __A             =		output.sample

         self.assertIsNotNone(_A			)
         __A             =		torch.Size((inputs_dict["""sample"""].shape[0], 1)			)
         self.assertEqual(output.shape	,		_A	,		"""Input and output shapes do not match"""			)




     def 						lowerCAmelCase_       (			self    :     Any			):


         """simple docstring"""



         pass




     def 						lowerCAmelCase_       (			self    :     Tuple			):


         """simple docstring"""



         pass




     def 						lowerCAmelCase_       (			self    :     str			):


         """simple docstring"""



         __A             =		{
             '''in_channels''': 14,
             '''out_channels''': 14,
             '''down_block_types''': ['''DownResnetBlock1D''', '''DownResnetBlock1D''', '''DownResnetBlock1D''', '''DownResnetBlock1D'''],
             '''up_block_types''': [],
             '''out_block_type''': '''ValueFunction''',
             '''mid_block_type''': '''ValueFunctionMidBlock1D''',
             '''block_out_channels''': [32, 64, 128, 256],
             '''layers_per_block''': 1,
             '''downsample_each_block''': True,
             '''use_timestep_embedding''': True,
             '''freq_shift''': 1.0,
             '''flip_sin_to_cos''': False,
             '''time_embedding_type''': '''positional''',
             '''act_fn''': '''mish''',
         }
         __A             =		self.dummy_input
         return init_dict, inputs_dict




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     int			):


         """simple docstring"""



         __A             =		UNetaDModel.from_pretrained(
             """bglick13/hopper-medium-v2-value-function-hor32"""	,		output_loading_info=_A	,		subfolder="""value_function"""			)
         self.assertIsNotNone(_A			)
         self.assertEqual(len(vf_loading_info["""missing_keys"""]			)	,		0			)

         value_function.to(_A			)
         __A             =		value_function(**self.dummy_input			)

         assert image is not None, "Make sure output is not None"




     @unittest.skipIf(torch_device == """mps"""	,		"""mish op not supported in MPS"""			)
     def 						lowerCAmelCase_       (			self    :     Dict			):


         """simple docstring"""



         __A             =		UNetaDModel.from_pretrained(
             """bglick13/hopper-medium-v2-value-function-hor32"""	,		output_loading_info=_A	,		subfolder="""value_function"""			)
         torch.manual_seed(0			)
         if torch.cuda.is_available():
             torch.cuda.manual_seed_all(0			)

         __A             =		value_function.config.in_channels
         __A             =		14
         __A             =		torch.randn((1, seq_len, num_features)			).permute(
             0	,		2	,		1			)  # match original, we can update values and remove
         __A             =		torch.full((num_features,)	,		0			)

         with torch.no_grad():
             __A             =		value_function(_A	,		_A			).sample

         # fmt: off
         __A             =		torch.tensor([165.25] * seq_len			)
         # fmt: on
         self.assertTrue(torch.allclose(_A	,		_A	,		rtol=1e-3			)			)





     def 						lowerCAmelCase_       (			self    :     Optional[Any]			):


         """simple docstring"""



         pass












import unittest

from transformers import ErnieMTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


__a :    Optional[Any]								=				get_tests_dir("fixtures/spiece.model")


@require_sentencepiece
@require_tokenizers
class  __lowercase (		lowercase_     , unittest.TestCase					):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE				  =			ErnieMTokenizer
     SCREAMING_SNAKE_CASE				  =			False
     SCREAMING_SNAKE_CASE				  =			True
     SCREAMING_SNAKE_CASE				  =			False
     SCREAMING_SNAKE_CASE				  =			False




     def 						lowerCAmelCase_       (			self    :     Optional[Any]			):


         """simple docstring"""



         super().setUp()

         # We have a SentencePiece fixture for testing
         __A             =		ErnieMTokenizer(UpperCamelCase_	,		unk_token="""<unk>"""	,		pad_token="""<pad>"""			)
         tokenizer.save_pretrained(self.tmpdirname			)




     def 						lowerCAmelCase_       (			self    :     Optional[Any]	,		UpperCamelCase_    :     Tuple			):


         """simple docstring"""



         __A             =		"""this is a test"""
         __A             =		"""this is a test"""
         return input_text, output_text




     def 						lowerCAmelCase_       (			self    :     Optional[int]			):


         """simple docstring"""



         __A             =		"""<pad>"""
         __A             =		0

         self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCamelCase_			)	,		UpperCamelCase_			)
         self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCamelCase_			)	,		UpperCamelCase_			)




     def 						lowerCAmelCase_       (			self    :     Tuple			):


         """simple docstring"""



         __A             =		list(self.get_tokenizer().get_vocab().keys()			)

         self.assertEqual(vocab_keys[0]	,		"""<pad>"""			)
         self.assertEqual(vocab_keys[1]	,		"""<unk>"""			)
         self.assertEqual(vocab_keys[-1]	,		"""▁eloquent"""			)
         self.assertEqual(len(UpperCamelCase_			)	,		30_000			)




     def 						lowerCAmelCase_       (			self    :     Tuple			):


         """simple docstring"""



         self.assertEqual(self.get_tokenizer().vocab_size	,		30_000			)




     def 						lowerCAmelCase_       (			self    :     Union[str, Any]			):


         """simple docstring"""



         if not self.test_rust_tokenizer:
             return

         __A             =		self.get_tokenizer()
         __A             =		self.get_rust_tokenizer()

         __A             =		"""I was born in 92000, and this is falsé."""

         __A             =		tokenizer.tokenize(UpperCamelCase_			)
         __A             =		rust_tokenizer.tokenize(UpperCamelCase_			)
         self.assertListEqual(UpperCamelCase_	,		UpperCamelCase_			)

         __A             =		tokenizer.encode(UpperCamelCase_	,		add_special_tokens=UpperCamelCase_			)
         __A             =		rust_tokenizer.encode(UpperCamelCase_	,		add_special_tokens=UpperCamelCase_			)
         self.assertListEqual(UpperCamelCase_	,		UpperCamelCase_			)

         __A             =		self.get_rust_tokenizer()
         __A             =		tokenizer.encode(UpperCamelCase_			)
         __A             =		rust_tokenizer.encode(UpperCamelCase_			)
         self.assertListEqual(UpperCamelCase_	,		UpperCamelCase_			)




     def 						lowerCAmelCase_       (			self    :     Union[str, Any]			):


         """simple docstring"""



         __A             =		ErnieMTokenizer(UpperCamelCase_	,		do_lower_case=UpperCamelCase_	,		unk_token="""<unk>"""	,		pad_token="""<pad>"""			)

         __A             =		tokenizer.tokenize("""This is a test"""			)
         self.assertListEqual(UpperCamelCase_	,		["""▁this""", """▁is""", """▁a""", """▁test"""]			)

         self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase_			)	,		[48, 25, 21, 1_289]			)

         __A             =		tokenizer.tokenize("""I was born in 92000, and this is falsé."""			)
         # ErnieMTokenizer(paddlenlp implementation) outputs '9' instead of '_9' so to mimic that '_9' is changed to '9'
         self.assertListEqual(
             UpperCamelCase_	,		["""▁i""", """▁was""", """▁born""", """▁in""", """9""", """2000""", """,""", """▁and""", """▁this""", """▁is""", """▁fal""", """s""", """é""", """."""]			)
         __A             =		tokenizer.convert_tokens_to_ids(UpperCamelCase_			)
         self.assertListEqual(UpperCamelCase_	,		[31, 23, 386, 19, 518, 3_050, 15, 17, 48, 25, 8_256, 18, 1, 9]			)

         __A             =		tokenizer.convert_ids_to_tokens(UpperCamelCase_			)
         self.assertListEqual(
             UpperCamelCase_	,		["""▁i""", """▁was""", """▁born""", """▁in""", """9""", """2000""", """,""", """▁and""", """▁this""", """▁is""", """▁fal""", """s""", """<unk>""", """."""]	,		)




     def 						lowerCAmelCase_       (			self    :     List[str]			):


         """simple docstring"""



         __A             =		ErnieMTokenizer(UpperCamelCase_	,		unk_token="""<unk>"""	,		pad_token="""<pad>"""			)

         __A             =		tokenizer.encode("""sequence builders"""			)
         __A             =		tokenizer.encode("""multi-sequence build"""			)

         __A             =		tokenizer.build_inputs_with_special_tokens(UpperCamelCase_			)
         __A             =		tokenizer.build_inputs_with_special_tokens(UpperCamelCase_	,		UpperCamelCase_			)

         assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
         assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + [
             tokenizer.sep_token_id
         ] + text_a + [tokenizer.sep_token_id]





     @slow
     def 						lowerCAmelCase_       (			self    :     List[str]			):


         """simple docstring"""



         __A             =		{"""input_ids""": [[0, 11_062, 82_772, 7, 15, 82_772, 538, 51_529, 237, 17_198, 1_290, 206, 9, 215_175, 1_314, 136, 17_198, 1_290, 206, 9, 56_359, 42, 122_009, 9, 16_466, 16, 87_344, 4_537, 9, 4_717, 78_381, 6, 159_958, 7, 15, 24_480, 618, 4, 527, 22_693, 9, 304, 4, 2_777, 24_480, 9_874, 4, 43_523, 594, 4, 803, 18_392, 33_189, 18, 4, 43_523, 24_447, 5, 5, 5, 16, 100, 24_955, 83_658, 9_626, 144_057, 15, 839, 22_335, 16, 136, 24_955, 83_658, 83_479, 15, 39_102, 724, 16, 678, 645, 6_460, 1_328, 4_589, 42, 122_009, 115_774, 23, 3_559, 1_328, 46_876, 7, 136, 53_894, 1_940, 42_227, 41_159, 17_721, 823, 425, 4, 27_512, 98_722, 206, 136, 5_531, 4_970, 919, 17_336, 5, 2], [0, 20_080, 618, 83, 82_775, 47, 479, 9, 1_517, 73, 53_894, 333, 80_581, 110_117, 18_811, 5_256, 1_295, 51, 152_526, 297, 7_986, 390, 124_416, 538, 35_431, 214, 98, 15_044, 25_737, 136, 7_108, 43_701, 23, 756, 135_355, 7, 5, 2, 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, 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], [0, 581, 63_773, 119_455, 6, 147_797, 88_203, 7, 645, 70, 21, 3_285, 10_269, 5, 2, 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, 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, 1, 1, 1, 1, 1]]}
         # fmt: on

         self.tokenizer_integration_test_util(
             expected_encoding=UpperCamelCase_	,		model_name="""susnato/ernie-m-base_pytorch"""	,		sequences=[
                 """Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides """
                 """general-purpose architectures (BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet...) for Natural """
                 """Language Understanding (NLU) and Natural Language Generation (NLG) with over32+ pretrained """
                 """models in100+ languages and deep interoperability between Jax, PyTorch and TensorFlow.""",
                 """BERT is designed to pre-train deep bidirectional representations from unlabeled text by jointly """
                 """conditioning on both left and right context in all layers.""",
                 """The quick brown fox jumps over the lazy dog.""",
             ]	,		)




