






"""simple docstring"""



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 				__SCREAMING_SNAKE_CASE    (					UpperCamelCase							,		UpperCamelCase							,		unittest.TestCase		):
							snake_case_  =				UNetaDModel
							snake_case_  =				'sample'


							@property
							def     _UpperCamelCase							(      self   :    Any				):
													'''simple docstring'''





													A__    :	List[Any]			=	4
													A__    :	Optional[int]			=	14
													A__    :	Tuple			=	16

													A__    :	Dict			=	floats_tensor((batch_size, num_features, seq_len)				).to(snake_case				)
													A__    :	List[Any]			=	torch.tensor([10] * batch_size				).to(snake_case				)

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


							@property
							def     _UpperCamelCase							(      self   :    Optional[int]				):
													'''simple docstring'''





													return (4, 14, 16)


							@property
							def     _UpperCamelCase							(      self   :    Dict				):
													'''simple docstring'''





													return (4, 14, 16)


							def     _UpperCamelCase							(      self   :    List[Any]				):
													'''simple docstring'''





													pass


							def     _UpperCamelCase							(      self   :    Optional[int]				):
													'''simple docstring'''





													pass


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





													super().test_determinism()


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





													super().test_outputs_equivalence()


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





													super().test_from_save_pretrained()


							@unittest.skipIf(torch_device == """mps"""      ,     """mish op not supported in MPS"""				)
							def     _UpperCamelCase							(      self   :    List[str]				):
													'''simple docstring'''





													super().test_from_save_pretrained_variant()


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





													super().test_model_from_pretrained()


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





													super().test_output()


							def     _UpperCamelCase							(      self   :    List[str]				):
													'''simple docstring'''





													A__    :	Optional[Any]			=	{
													    """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__    :	Union[str, Any]			=	self.dummy_input
													return init_dict, inputs_dict


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





													A__	,	A__    :	int			=	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				)
													A__    :	Any			=	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     _UpperCamelCase							(      self   :    Union[str, Any]				):
													'''simple docstring'''





													A__    :	Any			=	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__    :	Union[str, Any]			=	model.config.in_channels
													A__    :	str			=	16
													A__    :	Any			=	torch.randn((1, seq_len, num_features)				).permute(
													    0      ,     2      ,     1				)  # match original, we can update values and remove
													A__    :	Dict			=	torch.full((num_features,)      ,     0				)

													with torch.no_grad():
																			A__    :	Optional[int]			=	model(snake_case      ,     snake_case				).sample.permute(0      ,     2      ,     1				)

													A__    :	str			=	output[0, -3:, -3:].flatten()
													# fmt: off
													A__    :	int			=	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(snake_case      ,     snake_case      ,     rtol=1e-3				)				)


							def     _UpperCamelCase							(      self   :    Dict				):
													'''simple docstring'''





													pass


							@slow
							def     _UpperCamelCase							(      self   :    Optional[int]				):
													'''simple docstring'''





													A__    :	Optional[Any]			=	"""harmonai/maestro-150k"""
													A__    :	Dict			=	UNetaDModel.from_pretrained(snake_case      ,     subfolder="""unet"""				)
													model.to(snake_case				)

													A__    :	Optional[Any]			=	6_5536
													A__    :	List[str]			=	torch.sin(torch.arange(snake_case				)[None, None, :].repeat(1      ,     2      ,     1				)				).to(snake_case				)
													A__    :	Optional[int]			=	torch.tensor([1]				).to(snake_case				)

													with torch.no_grad():
																			A__    :	Tuple			=	model(snake_case      ,     snake_case				).sample

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

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






class 				__SCREAMING_SNAKE_CASE    (					UpperCamelCase							,		UpperCamelCase							,		unittest.TestCase		):
							snake_case_  =				UNetaDModel
							snake_case_  =				'sample'


							@property
							def     _UpperCamelCase							(      self   :    Optional[Any]				):
													'''simple docstring'''





													A__    :	Optional[Any]			=	4
													A__    :	List[Any]			=	14
													A__    :	str			=	16

													A__    :	List[str]			=	floats_tensor((batch_size, num_features, seq_len)				).to(snake_case				)
													A__    :	List[str]			=	torch.tensor([10] * batch_size				).to(snake_case				)

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


							@property
							def     _UpperCamelCase							(      self   :    Dict				):
													'''simple docstring'''





													return (4, 14, 16)


							@property
							def     _UpperCamelCase							(      self   :    int				):
													'''simple docstring'''





													return (4, 14, 1)


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





													super().test_determinism()


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





													super().test_outputs_equivalence()


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





													super().test_from_save_pretrained()


							@unittest.skipIf(torch_device == """mps"""      ,     """mish op not supported in MPS"""				)
							def     _UpperCamelCase							(      self   :    List[str]				):
													'''simple docstring'''





													super().test_from_save_pretrained_variant()


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





													super().test_model_from_pretrained()


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





													A__	,	A__    :	Any			=	self.prepare_init_args_and_inputs_for_common()
													A__    :	List[Any]			=	self.model_class(**snake_case				)
													model.to(snake_case				)
													model.eval()

													with torch.no_grad():
																			A__    :	Dict			=	model(**snake_case				)

																			if isinstance(snake_case      ,     snake_case				):
																									A__    :	Optional[int]			=	output.sample

													self.assertIsNotNone(snake_case				)
													A__    :	Union[str, Any]			=	torch.Size((inputs_dict["""sample"""].shape[0], 1)				)
													self.assertEqual(output.shape      ,     snake_case      ,     """Input and output shapes do not match"""				)


							def     _UpperCamelCase							(      self   :    Any				):
													'''simple docstring'''





													pass


							def     _UpperCamelCase							(      self   :    Optional[int]				):
													'''simple docstring'''





													pass


							def     _UpperCamelCase							(      self   :    int				):
													'''simple docstring'''





													A__    :	Union[str, Any]			=	{
													    """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__    :	str			=	self.dummy_input
													return init_dict, inputs_dict


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





													A__	,	A__    :	List[Any]			=	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				)
													A__    :	List[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     _UpperCamelCase							(      self   :    int				):
													'''simple docstring'''





													A__	,	A__    :	Tuple			=	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				)

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

													with torch.no_grad():
																			A__    :	Tuple			=	value_function(snake_case      ,     snake_case				).sample

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


							def     _UpperCamelCase							(      self   :    Tuple				):
													'''simple docstring'''





													pass














"""simple docstring"""



def   _lowerCAmelCase		(				UpperCAmelCase__							:       float,    UpperCAmelCase__							:       float,    UpperCAmelCase__							:       float   )		->list[int]:
						if hue < 0 or hue > 3_6_0:
												raise Exception("""hue should be between 0 and 360"""   )

						if saturation < 0 or saturation > 1:
												raise Exception("""saturation should be between 0 and 1"""   )

						if value < 0 or value > 1:
												raise Exception("""value should be between 0 and 1"""   )

						A__    :	int			=	value * saturation
						A__    :	Any			=	hue / 6_0
						A__    :	Any			=	chroma * (1 - abs(hue_section % 2 - 1   ))
						A__    :	Optional[Any]			=	value - chroma

						if hue_section >= 0 and hue_section <= 1:
												A__    :	Dict			=	round(2_5_5 * (chroma + match_value)   )
												A__    :	Optional[Any]			=	round(2_5_5 * (second_largest_component + match_value)   )
												A__    :	Any			=	round(2_5_5 * (match_value)   )
						elif hue_section > 1 and hue_section <= 2:
												A__    :	Optional[int]			=	round(2_5_5 * (second_largest_component + match_value)   )
												A__    :	Union[str, Any]			=	round(2_5_5 * (chroma + match_value)   )
												A__    :	Union[str, Any]			=	round(2_5_5 * (match_value)   )
						elif hue_section > 2 and hue_section <= 3:
												A__    :	Tuple			=	round(2_5_5 * (match_value)   )
												A__    :	List[str]			=	round(2_5_5 * (chroma + match_value)   )
												A__    :	Union[str, Any]			=	round(2_5_5 * (second_largest_component + match_value)   )
						elif hue_section > 3 and hue_section <= 4:
												A__    :	Any			=	round(2_5_5 * (match_value)   )
												A__    :	Dict			=	round(2_5_5 * (second_largest_component + match_value)   )
												A__    :	List[Any]			=	round(2_5_5 * (chroma + match_value)   )
						elif hue_section > 4 and hue_section <= 5:
												A__    :	Any			=	round(2_5_5 * (second_largest_component + match_value)   )
												A__    :	Dict			=	round(2_5_5 * (match_value)   )
												A__    :	Tuple			=	round(2_5_5 * (chroma + match_value)   )
						else:
												A__    :	Tuple			=	round(2_5_5 * (chroma + match_value)   )
												A__    :	Optional[int]			=	round(2_5_5 * (match_value)   )
												A__    :	str			=	round(2_5_5 * (second_largest_component + match_value)   )

						return [red, green, blue]




def   _lowerCAmelCase		(				UpperCAmelCase__							:       int,    UpperCAmelCase__							:       int,    UpperCAmelCase__							:       int   )		->list[float]:
						if red < 0 or red > 2_5_5:
												raise Exception("""red should be between 0 and 255"""   )

						if green < 0 or green > 2_5_5:
												raise Exception("""green should be between 0 and 255"""   )

						if blue < 0 or blue > 2_5_5:
												raise Exception("""blue should be between 0 and 255"""   )

						A__    :	int			=	red / 2_5_5
						A__    :	List[str]			=	green / 2_5_5
						A__    :	Optional[int]			=	blue / 2_5_5
						A__    :	Optional[int]			=	max(UpperCAmelCase__,    UpperCAmelCase__,    UpperCAmelCase__   )
						A__    :	Tuple			=	value - min(UpperCAmelCase__,    UpperCAmelCase__,    UpperCAmelCase__   )
						A__    :	Any			=	0 if value == 0 else chroma / value

						if chroma == 0:
												A__    :	Optional[Any]			=	0.0
						elif value == float_red:
												A__    :	Optional[Any]			=	6_0 * (0 + (float_green - float_blue) / chroma)
						elif value == float_green:
												A__    :	Tuple			=	6_0 * (2 + (float_blue - float_red) / chroma)
						else:
												A__    :	int			=	6_0 * (4 + (float_red - float_green) / chroma)

						A__    :	Optional[int]			=	(hue + 3_6_0) % 3_6_0

						return [hue, saturation, value]




def   _lowerCAmelCase		(				UpperCAmelCase__							:       list[float],    UpperCAmelCase__							:       list[float]   )		->bool:
						A__    :	Optional[Any]			=	abs(hsv_a[0] - hsv_a[0]   ) < 0.2
						A__    :	str			=	abs(hsv_a[1] - hsv_a[1]   ) < 0.002
						A__    :	Optional[int]			=	abs(hsv_a[2] - hsv_a[2]   ) < 0.002

						return check_hue and check_saturation and check_value






