



import gc
import unittest

import numpy as np
import torch

from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()

class        snake_case__    (A__ ,     unittest.TestCase		):







						"""simple docstring"""




						__lowerCAmelCase					:int      =					DanceDiffusionPipeline
						__lowerCAmelCase					:Tuple      =					UNCONDITIONAL_AUDIO_GENERATION_PARAMS
						__lowerCAmelCase					:List[Any]      =					PipelineTesterMixin.required_optional_params - {
						    "callback",
						    "latents",
						    "callback_steps",
						    "output_type",
						    "num_images_per_prompt",
						}
						__lowerCAmelCase					:Optional[Any]      =					UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
						__lowerCAmelCase					:Dict      =					False
						__lowerCAmelCase					:Union[str, Any]      =					False

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Any:
									"""simple docstring"""




									torch.manual_seed(0  )
									a__       :						int							=  UNetaDModel(
									    block_out_channels=(3_2, 3_2, 6_4) ,       extra_in_channels=1_6 ,       sample_size=5_1_2 ,       sample_rate=1_6_0_0_0 ,       in_channels=2 ,       out_channels=2 ,       flip_sin_to_cos=__lowercase ,       use_timestep_embedding=__lowercase ,       time_embedding_type="""fourier""" ,       mid_block_type="""UNetMidBlock1D""" ,       down_block_types=("""DownBlock1DNoSkip""", """DownBlock1D""", """AttnDownBlock1D""") ,       up_block_types=("""AttnUpBlock1D""", """UpBlock1D""", """UpBlock1DNoSkip""") ,       )
									a__       :						List[str]							=  IPNDMScheduler()

									a__       :						List[Any]							=  {
									    """unet""": unet,
									    """scheduler""": scheduler,
									}
									return components

						def 							SCREAMING_SNAKE_CASE__(       self ,       __lowercase ,       __lowercase=0  ) ->				List[Any]:
									"""simple docstring"""




									if str(__lowercase  ).startswith("""mps"""  ):
												a__       :						List[Any]							=  torch.manual_seed(__lowercase  )
									else:
												a__       :						Tuple							=  torch.Generator(device=__lowercase  ).manual_seed(__lowercase  )
									a__       :						str							=  {
									    """batch_size""": 1,
									    """generator""": generator,
									    """num_inference_steps""": 4,
									}
									return inputs

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Any:
									"""simple docstring"""




									a__       :						Any							=  """cpu"""  # ensure determinism for the device-dependent torch.Generator
									a__       :						Dict							=  self.get_dummy_components()
									a__       :						List[str]							=  DanceDiffusionPipeline(**__lowercase  )
									a__       :						str							=  pipe.to(__lowercase  )
									pipe.set_progress_bar_config(disable=__lowercase  )

									a__       :						Union[str, Any]							=  self.get_dummy_inputs(__lowercase  )
									a__       :						Optional[Any]							=  pipe(**__lowercase  )
									a__       :						Union[str, Any]							=  output.audios

									a__       :						Optional[int]							=  audio[0, -3:, -3:]

									assert audio.shape == (1, 2, components["unet"].sample_size)
									a__       :						int							=  np.array([-0.7_2_6_5, 1.0_0_0_0, -0.8_3_8_8, 0.1_1_7_5, 0.9_4_9_8, -1.0_0_0_0]  )
									assert np.abs(audio_slice.flatten() - expected_slice  ).max() < 1E-2

						@skip_mps
						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				str:
									"""simple docstring"""




									return super().test_save_load_local()

						@skip_mps
						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Tuple:
									"""simple docstring"""




									return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3  )

						@skip_mps
						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Any:
									"""simple docstring"""




									return super().test_save_load_optional_components()

						@skip_mps
						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				int:
									"""simple docstring"""




									return super().test_attention_slicing_forward_pass()

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Dict:
									"""simple docstring"""




									super().test_inference_batch_single_identical(expected_max_diff=3E-3  )




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







						"""simple docstring"""




						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[str]:
									"""simple docstring"""




									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[Any]:
									"""simple docstring"""




									a__       :						Optional[int]							=  torch_device

									a__       :						List[str]							=  DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k"""  )
									a__       :						Optional[Any]							=  pipe.to(__lowercase  )
									pipe.set_progress_bar_config(disable=__lowercase  )

									a__       :						Any							=  torch.manual_seed(0  )
									a__       :						Optional[Any]							=  pipe(generator=__lowercase ,       num_inference_steps=1_0_0 ,       audio_length_in_s=4.0_9_6  )
									a__       :						Union[str, Any]							=  output.audios

									a__       :						str							=  audio[0, -3:, -3:]

									assert audio.shape == (1, 2, pipe.unet.sample_size)
									a__       :						List[Any]							=  np.array([-0.0_1_9_2, -0.0_2_3_1, -0.0_3_1_8, -0.0_0_5_9, 0.0_0_0_2, -0.0_0_2_0]  )

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

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Optional[int]:
									"""simple docstring"""




									a__       :						int							=  torch_device

									a__       :						Tuple							=  DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k""" ,       torch_dtype=torch.floataa  )
									a__       :						Optional[Any]							=  pipe.to(__lowercase  )
									pipe.set_progress_bar_config(disable=__lowercase  )

									a__       :						Optional[Any]							=  torch.manual_seed(0  )
									a__       :						List[Any]							=  pipe(generator=__lowercase ,       num_inference_steps=1_0_0 ,       audio_length_in_s=4.0_9_6  )
									a__       :						str							=  output.audios

									a__       :						str							=  audio[0, -3:, -3:]

									assert audio.shape == (1, 2, pipe.unet.sample_size)
									a__       :						Union[str, Any]							=  np.array([-0.0_3_6_7, -0.0_4_8_8, -0.0_7_7_1, -0.0_5_2_5, -0.0_4_4_4, -0.0_3_4_1]  )

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









_lowercase		:				Optional[int]      		=[
    (1000, "M"),
    (900, "CM"),
    (500, "D"),
    (400, "CD"),
    (100, "C"),
    (90, "XC"),
    (50, "L"),
    (40, "XL"),
    (10, "X"),
    (9, "IX"),
    (5, "V"),
    (4, "IV"),
    (1, "I"),
]

def      lowerCAmelCase_					(  _lowercase    :       str)							->	int:







			"""simple docstring"""
			a__       :						Union[str, Any]							=  {"""I""": 1, """V""": 5, """X""": 10, """L""": 50, """C""": 100, """D""": 500, """M""": 1000}
			a__       :						List[Any]							=  0
			a__       :						Dict							=  0
			while place < len(_lowercase):
						if (place + 1 < len(_lowercase)) and (vals[roman[place]] < vals[roman[place + 1]]):
									total += vals[roman[place + 1]] - vals[roman[place]]
									place += 2
						else:
									total += vals[roman[place]]
									place += 1
			return total

def      lowerCAmelCase_					(  _lowercase    :       int)							->	str:







			"""simple docstring"""
			a__       :						Optional[Any]							=  []
			for arabic, roman in ROMAN:
						((a__)	,						(a__))       :						Optional[Any]							=  divmod(_lowercase						,       _lowercase)
						result.append(roman * factor)
						if number == 0:
									break
			return "".join(_lowercase)


if __name__ == "__main__":
		import doctest

		doctest.testmod()




