

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





       '''simple docstring'''




       UpperCamelCase__ :     List[Any]				    =       UNetaDModel
       UpperCamelCase__ :     Optional[Any]				    =       '''sample'''

       @property
       def UpperCAmelCase_						(						self       :						Optional[int]							)			->    Optional[Any]:
             snake_case__					=   4
             snake_case__					=   14
             snake_case__					=   16

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

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

       @property
       def UpperCAmelCase_						(						self       :						Dict							)			->    Union[str, Any]:
             return (4, 14, 16)

       @property
       def UpperCAmelCase_						(						self       :						Optional[int]							)			->    List[str]:
             return (4, 14, 16)

       def UpperCAmelCase_						(						self       :						str							)			->    Optional[Any]:
             pass

       def UpperCAmelCase_						(						self       :						List[Any]							)			->    int:
             pass

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Optional[int]							)			->    Tuple:
             super().test_determinism()

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Dict							)			->    Union[str, Any]:
             super().test_outputs_equivalence()

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

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Union[str, Any]							)			->    int:
             super().test_from_save_pretrained_variant()

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

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Optional[int]							)			->    Dict:
             super().test_output()

       def UpperCAmelCase_						(						self       :						Union[str, Any]							)			->    Tuple:
             snake_case__					=   {
                 """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""",
             }
             snake_case__					=   self.dummy_input
             return init_dict, inputs_dict

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Tuple							)			->    Tuple:
             snake_case__	,			snake_case__					=   UNetaDModel.from_pretrained(
                 """bglick13/hopper-medium-v2-value-function-hor32"""    ,		output_loading_info=lowerCAmelCase__    ,		subfolder="""unet"""							)
             self.assertIsNotNone(lowerCAmelCase__							)
             self.assertEqual(len(loading_info["""missing_keys"""]							)    ,		0							)

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

             with torch.no_grad():
                   snake_case__					=   model(lowerCAmelCase__    ,		lowerCAmelCase__							).sample.permute(0    ,		2    ,		1							)

             snake_case__					=   output[0, -3:, -3:].flatten()
             # fmt: off
             snake_case__					=   torch.tensor([-2.137_172, 1.1_426_016, 0.3_688_687, -0.766_922, 0.7_303_146, 0.11_038_864, -0.4_760_633, 0.13_270_172, 0.02_591_348]							)
             # fmt: on
             self.assertTrue(torch.allclose(lowerCAmelCase__    ,		lowerCAmelCase__    ,		rtol=1E-3							)							)

       def UpperCAmelCase_						(						self       :						Tuple							)			->    Optional[int]:
             # Not implemented yet for this UNet
             pass

       @slow
       def UpperCAmelCase_						(						self       :						Optional[Any]							)			->    Optional[Any]:
             snake_case__					=   """harmonai/maestro-150k"""
             snake_case__					=   UNetaDModel.from_pretrained(lowerCAmelCase__    ,		subfolder="""unet"""							)
             model.to(lowerCAmelCase__							)

             snake_case__					=   65536
             snake_case__					=   torch.sin(torch.arange(lowerCAmelCase__							)[None, None, :].repeat(1    ,		2    ,		1							)							).to(lowerCAmelCase__							)
             snake_case__					=   torch.tensor([1]							).to(lowerCAmelCase__							)

             with torch.no_grad():
                   snake_case__					=   model(lowerCAmelCase__    ,		lowerCAmelCase__							).sample

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

             assert (output_sum - 224.0_896).abs() < 0.5
             assert (output_max - 0.0_607).abs() < 4E-4



class     SCREAMING_SNAKE_CASE__     (						__a ,	__a ,	unittest.TestCase		):





       '''simple docstring'''




       UpperCamelCase__ :     Optional[int]				    =       UNetaDModel
       UpperCamelCase__ :     Dict				    =       '''sample'''

       @property
       def UpperCAmelCase_						(						self       :						Optional[int]							)			->    Union[str, Any]:
             snake_case__					=   4
             snake_case__					=   14
             snake_case__					=   16

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

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

       @property
       def UpperCAmelCase_						(						self       :						Union[str, Any]							)			->    Dict:
             return (4, 14, 16)

       @property
       def UpperCAmelCase_						(						self       :						List[Any]							)			->    Dict:
             return (4, 14, 1)

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

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

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

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Tuple							)			->    Union[str, Any]:
             super().test_from_save_pretrained_variant()

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

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						str							)			->    Optional[int]:
             # UNetRL is a value-function is different output shape
             snake_case__	,			snake_case__					=   self.prepare_init_args_and_inputs_for_common()
             snake_case__					=   self.model_class(**lowerCAmelCase__							)
             model.to(lowerCAmelCase__							)
             model.eval()

             with torch.no_grad():
                   snake_case__					=   model(**lowerCAmelCase__							)

                   if isinstance(lowerCAmelCase__    ,		lowerCAmelCase__							):
                         snake_case__					=   output.sample

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

       def UpperCAmelCase_						(						self       :						str							)			->    Optional[Any]:
             pass

       def UpperCAmelCase_						(						self       :						int							)			->    Union[str, Any]:
             pass

       def UpperCAmelCase_						(						self       :						Any							)			->    Optional[int]:
             snake_case__					=   {
                 """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""",
             }
             snake_case__					=   self.dummy_input
             return init_dict, inputs_dict

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Optional[Any]							)			->    Dict:
             snake_case__	,			snake_case__					=   UNetaDModel.from_pretrained(
                 """bglick13/hopper-medium-v2-value-function-hor32"""    ,		output_loading_info=lowerCAmelCase__    ,		subfolder="""value_function"""							)
             self.assertIsNotNone(lowerCAmelCase__							)
             self.assertEqual(len(vf_loading_info["""missing_keys"""]							)    ,		0							)

             value_function.to(lowerCAmelCase__							)
             snake_case__					=   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       :						Dict							)			->    Optional[int]:
             snake_case__	,			snake_case__					=   UNetaDModel.from_pretrained(
                 """bglick13/hopper-medium-v2-value-function-hor32"""    ,		output_loading_info=lowerCAmelCase__    ,		subfolder="""value_function"""							)
             torch.manual_seed(0							)
             if torch.cuda.is_available():
                   torch.cuda.manual_seed_all(0							)

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

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

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

       def UpperCAmelCase_						(						self       :						int							)			->    Union[str, Any]:
             # Not implemented yet for this UNet
             pass









import unittest

import numpy as np
import torch

from diffusers import DDPMPipeline, DDPMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device


enable_full_determinism()
class     SCREAMING_SNAKE_CASE__     (						unittest.TestCase		):





       '''simple docstring'''




       @property
       def UpperCAmelCase_						(						self       :						Any							)			->    List[str]:
             torch.manual_seed(0							)
             snake_case__					=   UNetaDModel(
                 block_out_channels=(32, 64)    ,		layers_per_block=2    ,		sample_size=32    ,		in_channels=3    ,		out_channels=3    ,		down_block_types=("""DownBlock2D""", """AttnDownBlock2D""")    ,		up_block_types=("""AttnUpBlock2D""", """UpBlock2D""")    ,		)
             return model

       def UpperCAmelCase_						(						self       :						Optional[int]							)			->    int:
             snake_case__					=   """cpu"""
             snake_case__					=   self.dummy_uncond_unet
             snake_case__					=   DDPMScheduler()

             snake_case__					=   DDPMPipeline(unet=lowerCAmelCase__    ,		scheduler=lowerCAmelCase__							)
             ddpm.to(lowerCAmelCase__							)
             ddpm.set_progress_bar_config(disable=lowerCAmelCase__							)

             snake_case__					=   torch.Generator(device=lowerCAmelCase__							).manual_seed(0							)
             snake_case__					=   ddpm(generator=lowerCAmelCase__    ,		num_inference_steps=2    ,		output_type="""numpy"""							).images

             snake_case__					=   torch.Generator(device=lowerCAmelCase__							).manual_seed(0							)
             snake_case__					=   ddpm(generator=lowerCAmelCase__    ,		num_inference_steps=2    ,		output_type="""numpy"""    ,		return_dict=lowerCAmelCase__							)[0]

             snake_case__					=   image[0, -3:, -3:, -1]
             snake_case__					=   image_from_tuple[0, -3:, -3:, -1]

             assert image.shape == (1, 32, 32, 3)
             snake_case__					=   np.array(
                 [9.956E-01, 5.785E-01, 4.675E-01, 9.930E-01, 0.0, 1.000, 1.199E-03, 2.648E-04, 5.101E-04]							)

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

       def UpperCAmelCase_						(						self       :						Tuple							)			->    str:
             snake_case__					=   self.dummy_uncond_unet
             snake_case__					=   DDPMScheduler(prediction_type="""sample"""							)

             snake_case__					=   DDPMPipeline(unet=lowerCAmelCase__    ,		scheduler=lowerCAmelCase__							)
             ddpm.to(lowerCAmelCase__							)
             ddpm.set_progress_bar_config(disable=lowerCAmelCase__							)

             snake_case__					=   torch.manual_seed(0							)
             snake_case__					=   ddpm(generator=lowerCAmelCase__    ,		num_inference_steps=2    ,		output_type="""numpy"""							).images

             snake_case__					=   torch.manual_seed(0							)
             snake_case__					=   ddpm(generator=lowerCAmelCase__    ,		num_inference_steps=2    ,		output_type="""numpy"""							)[0]

             snake_case__					=   image[0, -3:, -3:, -1]
             snake_case__					=   image_eps[0, -3:, -3:, -1]

             assert image.shape == (1, 32, 32, 3)
             snake_case__					=   1E-2 if torch_device != """mps""" else 3E-2
             assert np.abs(image_slice.flatten() - image_eps_slice.flatten()							).max() < tolerance



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





       '''simple docstring'''




       def UpperCAmelCase_						(						self       :						Tuple							)			->    Optional[int]:
             snake_case__					=   """google/ddpm-cifar10-32"""

             snake_case__					=   UNetaDModel.from_pretrained(lowerCAmelCase__							)
             snake_case__					=   DDPMScheduler.from_pretrained(lowerCAmelCase__							)

             snake_case__					=   DDPMPipeline(unet=lowerCAmelCase__    ,		scheduler=lowerCAmelCase__							)
             ddpm.to(lowerCAmelCase__							)
             ddpm.set_progress_bar_config(disable=lowerCAmelCase__							)

             snake_case__					=   torch.manual_seed(0							)
             snake_case__					=   ddpm(generator=lowerCAmelCase__    ,		output_type="""numpy"""							).images

             snake_case__					=   image[0, -3:, -3:, -1]

             assert image.shape == (1, 32, 32, 3)
             snake_case__					=   np.array([0.4_200, 0.3_588, 0.1_939, 0.3_847, 0.3_382, 0.2_647, 0.4_155, 0.3_582, 0.3_385]							)
             assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2






