





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 							_a(     UpperCAmelCase__						, UpperCAmelCase__						, unittest.TestCase     ):
		lowerCamelCase__  :Any 					=		UNetaDModel
		lowerCamelCase__  :Any 					=		"sample"







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


							'''simple docstring'''

							_snake_case							:       Dict												= 4
							_snake_case							:       Optional[int]												= 1_4
							_snake_case							:       Optional[Any]												= 1_6

							_snake_case							:       List[str]												= floats_tensor((batch_size, num_features, seq_len)					).to(snake_case__					)
							_snake_case							:       List[str]												= torch.tensor([1_0] * batch_size					).to(snake_case__					)

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







		@property
		def 							lowercase							(     self					)   ->   str:


							'''simple docstring'''

							return (4, 1_4, 1_6)







		@property
		def 							lowercase							(     self					)   ->   Tuple:


							'''simple docstring'''

							return (4, 1_4, 1_6)







		def 							lowercase							(     self					)   ->   int:


							'''simple docstring'''

							pass







		def 							lowercase							(     self					)   ->   Tuple:


							'''simple docstring'''

							pass







		@unittest.skipIf(torch_device == "mps"			,    "mish op not supported in MPS"					)
		def 							lowercase							(     self					)   ->   List[str]:


							'''simple docstring'''

							super().test_determinism()







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


							'''simple docstring'''

							super().test_outputs_equivalence()







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


							'''simple docstring'''

							super().test_from_save_pretrained()







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


							'''simple docstring'''

							super().test_from_save_pretrained_variant()







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


							'''simple docstring'''

							super().test_model_from_pretrained()







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


							'''simple docstring'''

							super().test_output()







		def 							lowercase							(     self					)   ->   List[Any]:


							'''simple docstring'''

							_snake_case							:       Tuple												= {
							    "block_out_channels": (3_2, 6_4, 1_2_8, 2_5_6),
							    "in_channels": 1_4,
							    "out_channels": 1_4,
							    "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",
							}
							_snake_case							:       int												= self.dummy_input
							return init_dict, inputs_dict







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


							'''simple docstring'''

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

							model.to(snake_case__					)
							_snake_case							:       Dict												= 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 							lowercase							(     self					)   ->   Any:


							'''simple docstring'''

							_snake_case							:       str												= 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					)

							_snake_case							:       List[str]												= model.config.in_channels
							_snake_case							:       List[Any]												= 1_6
							_snake_case							:       Union[str, Any]												= torch.randn((1, seq_len, num_features)					).permute(
							    0			,    2			,    1					)  # match original, we can update values and remove
							_snake_case							:       Optional[Any]												= torch.full((num_features,)			,    0					)

							with torch.no_grad():
												_snake_case							:       Union[str, Any]												= model(snake_case__			,    snake_case__					).sample.permute(0			,    2			,    1					)

							_snake_case							:       Union[str, Any]												= output[0, -3:, -3:].flatten()
							# fmt: off
							_snake_case							:       str												= torch.tensor([-2.13_71_72, 1.1_42_60_16, 0.3_68_86_87, -0.76_69_22, 0.7_30_31_46, 0.11_03_88_64, -0.4_76_06_33, 0.13_27_01_72, 0.02_59_13_48]					)
							# fmt: on
							self.assertTrue(torch.allclose(snake_case__			,    snake_case__			,    rtol=1E-3					)					)







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


							'''simple docstring'''

							pass







		@slow
		def 							lowercase							(     self					)   ->   Optional[Any]:


							'''simple docstring'''

							_snake_case							:       List[Any]												= "harmonai/maestro-150k"
							_snake_case							:       List[str]												= UNetaDModel.from_pretrained(snake_case__			,    subfolder="unet"					)
							model.to(snake_case__					)

							_snake_case							:       List[Any]												= 6_5_5_3_6
							_snake_case							:       Union[str, Any]												= torch.sin(torch.arange(snake_case__					)[None, None, :].repeat(1			,    2			,    1					)					).to(snake_case__					)
							_snake_case							:       Optional[Any]												= torch.tensor([1]					).to(snake_case__					)

							with torch.no_grad():
												_snake_case							:       Optional[Any]												= model(snake_case__			,    snake_case__					).sample

							_snake_case							:       Optional[Any]												= output.abs().sum()
							_snake_case							:       int												= output.abs().max()

							assert (output_sum - 2_24.08_96).abs() < 0.5
							assert (output_max - 0.06_07).abs() < 4E-4






class 							_a(     UpperCAmelCase__						, UpperCAmelCase__						, unittest.TestCase     ):
		lowerCamelCase__  :Optional[Any] 					=		UNetaDModel
		lowerCamelCase__  :int 					=		"sample"







		@property
		def 							lowercase							(     self					)   ->   int:


							'''simple docstring'''

							_snake_case							:       Optional[Any]												= 4
							_snake_case							:       Union[str, Any]												= 1_4
							_snake_case							:       Optional[Any]												= 1_6

							_snake_case							:       List[str]												= floats_tensor((batch_size, num_features, seq_len)					).to(snake_case__					)
							_snake_case							:       Tuple												= torch.tensor([1_0] * batch_size					).to(snake_case__					)

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







		@property
		def 							lowercase							(     self					)   ->   List[Any]:


							'''simple docstring'''

							return (4, 1_4, 1_6)







		@property
		def 							lowercase							(     self					)   ->   Optional[int]:


							'''simple docstring'''

							return (4, 1_4, 1)







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


							'''simple docstring'''

							super().test_determinism()







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


							'''simple docstring'''

							super().test_outputs_equivalence()







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


							'''simple docstring'''

							super().test_from_save_pretrained()







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


							'''simple docstring'''

							super().test_from_save_pretrained_variant()







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


							'''simple docstring'''

							super().test_model_from_pretrained()







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


							'''simple docstring'''

							_snake_case							,			_snake_case							:       Any												= self.prepare_init_args_and_inputs_for_common()
							_snake_case							:       Any												= self.model_class(**snake_case__					)
							model.to(snake_case__					)
							model.eval()

							with torch.no_grad():
												_snake_case							:       List[str]												= model(**snake_case__					)

												if isinstance(snake_case__			,    snake_case__					):
																	_snake_case							:       Optional[int]												= output.sample

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







		def 							lowercase							(     self					)   ->   List[str]:


							'''simple docstring'''

							pass







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


							'''simple docstring'''

							pass







		def 							lowercase							(     self					)   ->   Any:


							'''simple docstring'''

							_snake_case							:       str												= {
							    "in_channels": 1_4,
							    "out_channels": 1_4,
							    "down_block_types": ["DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D"],
							    "up_block_types": [],
							    "out_block_type": "ValueFunction",
							    "mid_block_type": "ValueFunctionMidBlock1D",
							    "block_out_channels": [3_2, 6_4, 1_2_8, 2_5_6],
							    "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",
							}
							_snake_case							:       Any												= self.dummy_input
							return init_dict, inputs_dict







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


							'''simple docstring'''

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

							value_function.to(snake_case__					)
							_snake_case							:       Optional[Any]												= 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 							lowercase							(     self					)   ->   Optional[int]:


							'''simple docstring'''

							_snake_case							,			_snake_case							:       Union[str, Any]												= UNetaDModel.from_pretrained(
							    "bglick13/hopper-medium-v2-value-function-hor32"			,    output_loading_info=snake_case__			,    subfolder="value_function"					)
							torch.manual_seed(0					)
							if torch.cuda.is_available():
												torch.cuda.manual_seed_all(0					)

							_snake_case							:       int												= value_function.config.in_channels
							_snake_case							:       List[str]												= 1_4
							_snake_case							:       Optional[int]												= torch.randn((1, seq_len, num_features)					).permute(
							    0			,    2			,    1					)  # match original, we can update values and remove
							_snake_case							:       str												= torch.full((num_features,)			,    0					)

							with torch.no_grad():
												_snake_case							:       str												= value_function(snake_case__			,    snake_case__					).sample

							# fmt: off
							_snake_case							:       Optional[Any]												= torch.tensor([1_65.25] * seq_len					)
							# fmt: on
							self.assertTrue(torch.allclose(snake_case__			,    snake_case__			,    rtol=1E-3					)					)







		def 							lowercase							(     self					)   ->   Optional[Any]:


							'''simple docstring'''

							pass





import math
from itertools import permutations


def 				__SCREAMING_SNAKE_CASE    (     snake_case_		):

						'''simple docstring'''


						if 1 < number < 4:
												# 2 and 3 are primes
												return True
						elif number < 2 or number % 2 == 0 or number % 3 == 0:
												# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
												return False

						# All primes number are in format of 6k +/- 1
						for i in range(5      ,       int(math.sqrt(snake_case_		) + 1		)      ,       6		):
												if number % i == 0 or number % (i + 2) == 0:
																		return False
						return True


def 				__SCREAMING_SNAKE_CASE    (     snake_case_      ,       snake_case_		):

						'''simple docstring'''


						_UpperCAmelCase				,					_UpperCAmelCase						=	0, len(snake_case_		) - 1
						while left <= right:
												_UpperCAmelCase						=	(left + right) // 2
												if prime_list[middle] == target:
																		return True
												elif prime_list[middle] < target:
																		_UpperCAmelCase						=	middle + 1
												else:
																		_UpperCAmelCase						=	middle - 1

						return False


def 				__SCREAMING_SNAKE_CASE    (     ):

						'''simple docstring'''


						_UpperCAmelCase						=	[n for n in range(1001      ,       1_0000      ,       2		) if is_prime(snake_case_		)]
						_UpperCAmelCase						=	[]

						for number in prime_list:
												_UpperCAmelCase						=	[]

												for prime_member in permutations(list(str(snake_case_		)		)		):
																		_UpperCAmelCase						=	int("".join(snake_case_		)		)

																		if prime % 2 == 0:
																								continue

																		if search(snake_case_      ,       snake_case_		):
																								tmp_numbers.append(snake_case_		)

												tmp_numbers.sort()
												if len(snake_case_		) >= 3:
																		candidates.append(snake_case_		)

						_UpperCAmelCase						=	[]
						for candidate in candidates:
												_UpperCAmelCase						=	len(snake_case_		)
												_UpperCAmelCase						=	False

												for i in range(snake_case_		):
																		for j in range(i + 1      ,       snake_case_		):
																								for k in range(j + 1      ,       snake_case_		):
																														if (
																														    abs(candidate[i] - candidate[j]		)
																														    == abs(candidate[j] - candidate[k]		)
																														    and len({candidate[i], candidate[j], candidate[k]}		) == 3
																														):
																																				passed.append(
																																				    sorted([candidate[i], candidate[j], candidate[k]]		)		)
																																				_UpperCAmelCase						=	True

																														if found:
																																				break
																								if found:
																														break
																		if found:
																								break

						_UpperCAmelCase						=	set()
						for seq in passed:
												answer.add("".join([str(snake_case_		) for i in seq]		)		)

						return max(int(snake_case_		) for x in answer		)


if __name__ == "__main__":
				print(solution())




