
"""simple docstring"""




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 			lowerCAmelCase_		(a__				,      unittest.TestCase						):


				"""simple docstring"""



				__UpperCamelCase   :		Dict       =     DanceDiffusionPipeline
				__UpperCamelCase   :		str       =     UNCONDITIONAL_AUDIO_GENERATION_PARAMS
				__UpperCamelCase   :		Any       =     PipelineTesterMixin.required_optional_params - {
				    '''callback''',
				    '''latents''',
				    '''callback_steps''',
				    '''output_type''',
				    '''num_images_per_prompt''',
				}
				__UpperCamelCase   :		str       =     UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
				__UpperCamelCase   :		int       =     False
				__UpperCamelCase   :		str       =     False



				def 			__magic_name__					(self					)      ->	Any:



											"""simple docstring"""




											torch.manual_seed(0					)
											SCREAMING_SNAKE_CASE__		: Dict     =     UNetaDModel(
											    block_out_channels=(32, 32, 64)     , extra_in_channels=16     , sample_size=5_12     , sample_rate=1_60_00     , in_channels=2     , out_channels=2     , flip_sin_to_cos=SCREAMING_SNAKE_CASE__     , use_timestep_embedding=SCREAMING_SNAKE_CASE__     , time_embedding_type="""fourier"""     , mid_block_type="""UNetMidBlock1D"""     , down_block_types=("""DownBlock1DNoSkip""", """DownBlock1D""", """AttnDownBlock1D""")     , up_block_types=("""AttnUpBlock1D""", """UpBlock1D""", """UpBlock1DNoSkip""")     , )
											SCREAMING_SNAKE_CASE__		: Tuple     =     IPNDMScheduler()

											SCREAMING_SNAKE_CASE__		: str     =     {
											    """unet""": unet,
											    """scheduler""": scheduler,
											}
											return components



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__=0					)      ->	Tuple:



											"""simple docstring"""




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



				def 			__magic_name__					(self					)      ->	Dict:



											"""simple docstring"""




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

											SCREAMING_SNAKE_CASE__		: str     =     self.get_dummy_inputs(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     pipe(**SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     output.audios

											SCREAMING_SNAKE_CASE__		: Tuple     =     audio[0, -3:, -3:]

											assert audio.shape == (1, 2, components["unet"].sample_size)
											SCREAMING_SNAKE_CASE__		: Any     =     np.array([-0.7265, 1.0000, -0.8388, 0.1175, 0.9498, -1.0000]					)
											assert np.abs(audio_slice.flatten() - expected_slice					).max() < 1E-2



				@skip_mps
				def 			__magic_name__					(self					)      ->	Dict:



											"""simple docstring"""




											return super().test_save_load_local()



				@skip_mps
				def 			__magic_name__					(self					)      ->	Dict:



											"""simple docstring"""




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



				@skip_mps
				def 			__magic_name__					(self					)      ->	Any:



											"""simple docstring"""




											return super().test_save_load_optional_components()



				@skip_mps
				def 			__magic_name__					(self					)      ->	Any:



											"""simple docstring"""




											return super().test_attention_slicing_forward_pass()



				def 			__magic_name__					(self					)      ->	str:



											"""simple docstring"""




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






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


				"""simple docstring"""



				def 			__magic_name__					(self					)      ->	str:



											"""simple docstring"""




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



				def 			__magic_name__					(self					)      ->	Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[Any]     =     torch_device

											SCREAMING_SNAKE_CASE__		: Tuple     =     DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k"""					)
											SCREAMING_SNAKE_CASE__		: Dict     =     pipe.to(SCREAMING_SNAKE_CASE__					)
											pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__					)

											SCREAMING_SNAKE_CASE__		: List[str]     =     torch.manual_seed(0					)
											SCREAMING_SNAKE_CASE__		: int     =     pipe(generator=SCREAMING_SNAKE_CASE__     , num_inference_steps=1_00     , audio_length_in_s=4.096					)
											SCREAMING_SNAKE_CASE__		: int     =     output.audios

											SCREAMING_SNAKE_CASE__		: List[str]     =     audio[0, -3:, -3:]

											assert audio.shape == (1, 2, pipe.unet.sample_size)
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     np.array([-0.0192, -0.0231, -0.0318, -0.0059, 0.0002, -0.0020]					)

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



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[int]     =     torch_device

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

											SCREAMING_SNAKE_CASE__		: Dict     =     torch.manual_seed(0					)
											SCREAMING_SNAKE_CASE__		: Any     =     pipe(generator=SCREAMING_SNAKE_CASE__     , num_inference_steps=1_00     , audio_length_in_s=4.096					)
											SCREAMING_SNAKE_CASE__		: int     =     output.audios

											SCREAMING_SNAKE_CASE__		: Tuple     =     audio[0, -3:, -3:]

											assert audio.shape == (1, 2, pipe.unet.sample_size)
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     np.array([-0.0367, -0.0488, -0.0771, -0.0525, -0.0444, -0.0341]					)

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






"""simple docstring"""




UpperCAmelCase__   :					Dict												=				[
    (1_0_0_0, 'M'),
    (9_0_0, 'CM'),
    (5_0_0, 'D'),
    (4_0_0, 'CD'),
    (1_0_0, 'C'),
    (9_0, 'XC'),
    (5_0, 'L'),
    (4_0, 'XL'),
    (1_0, 'X'),
    (9, 'IX'),
    (5, 'V'),
    (4, 'IV'),
    (1, 'I'),
]

def lowercase_						(     _snake_case		):
							SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     {"""I""": 1, """V""": 5, """X""": 10, """L""": 50, """C""": 100, """D""": 500, """M""": 1_000}
							SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     0
							SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     0
							while place < len(_snake_case		):
														if (place + 1 < len(_snake_case		)) 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 lowercase_						(     _snake_case		):
							SCREAMING_SNAKE_CASE__		: Optional[Any]     =     []
							for arabic, roman in ROMAN:
														((SCREAMING_SNAKE_CASE__)   ,   (SCREAMING_SNAKE_CASE__))		: List[Any]     =     divmod(_snake_case     ,_snake_case		)
														result.append(roman * factor		)
														if number == 0:
																					break
							return "".join(_snake_case		)


if __name__ == "__main__":
					import doctest

					doctest.testmod()




