
'''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 		__UpperCAmelCase   (      _lowerCamelCase		,     unittest.TestCase		):
							__lowercase   	= DanceDiffusionPipeline
							__lowercase   	= UNCONDITIONAL_AUDIO_GENERATION_PARAMS
							__lowercase   	= PipelineTesterMixin.required_optional_params - {
							    """callback""",
							    """latents""",
							    """callback_steps""",
							    """output_type""",
							    """num_images_per_prompt""",
							}
							__lowercase   	= UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
							__lowercase   	= False
							__lowercase   	= False


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								torch.manual_seed(0						)
								_snake_case     		=	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=lowerCAmelCase_					,     use_timestep_embedding=lowerCAmelCase_					,     time_embedding_type='fourier'					,     mid_block_type='UNetMidBlock1D'					,     down_block_types=('DownBlock1DNoSkip', 'DownBlock1D', 'AttnDownBlock1D')					,     up_block_types=('AttnUpBlock1D', 'UpBlock1D', 'UpBlock1DNoSkip')					,     )
								_snake_case     		=	IPNDMScheduler()

								_snake_case     		=	{
								    'unet': unet,
								    'scheduler': scheduler,
								}
								return components


							def 				lowerCamelCase       ( self					,     lowerCAmelCase_					,     lowerCAmelCase_=0						):





								"""simple docstring"""






								if str(lowerCAmelCase_						).startswith('mps'						):
									_snake_case     		=	torch.manual_seed(lowerCAmelCase_						)
								else:
									_snake_case     		=	torch.Generator(device=lowerCAmelCase_						).manual_seed(lowerCAmelCase_						)
								_snake_case     		=	{
								    'batch_size': 1,
								    'generator': generator,
								    'num_inference_steps': 4,
								}
								return inputs


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	'cpu'  # ensure determinism for the device-dependent torch.Generator
								_snake_case     		=	self.get_dummy_components()
								_snake_case     		=	DanceDiffusionPipeline(**lowerCAmelCase_						)
								_snake_case     		=	pipe.to(lowerCAmelCase_						)
								pipe.set_progress_bar_config(disable=lowerCAmelCase_						)

								_snake_case     		=	self.get_dummy_inputs(lowerCAmelCase_						)
								_snake_case     		=	pipe(**lowerCAmelCase_						)
								_snake_case     		=	output.audios

								_snake_case     		=	audio[0, -3:, -3:]

								assert audio.shape == (1, 2, components["unet"].sample_size)
								_snake_case     		=	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 				lowerCamelCase       ( self						):





								"""simple docstring"""






								return super().test_save_load_local()


							@skip_mps
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






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


							@skip_mps
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								return super().test_save_load_optional_components()


							@skip_mps
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								return super().test_attention_slicing_forward_pass()




							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






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


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


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






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


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	torch_device

								_snake_case     		=	DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k'						)
								_snake_case     		=	pipe.to(lowerCAmelCase_						)
								pipe.set_progress_bar_config(disable=lowerCAmelCase_						)

								_snake_case     		=	torch.manual_seed(0						)
								_snake_case     		=	pipe(generator=lowerCAmelCase_					,     num_inference_steps=1_00					,     audio_length_in_s=4.096						)
								_snake_case     		=	output.audios

								_snake_case     		=	audio[0, -3:, -3:]

								assert audio.shape == (1, 2, pipe.unet.sample_size)
								_snake_case     		=	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 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	torch_device

								_snake_case     		=	DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k'					,     torch_dtype=torch.floataa						)
								_snake_case     		=	pipe.to(lowerCAmelCase_						)
								pipe.set_progress_bar_config(disable=lowerCAmelCase_						)

								_snake_case     		=	torch.manual_seed(0						)
								_snake_case     		=	pipe(generator=lowerCAmelCase_					,     num_inference_steps=1_00					,     audio_length_in_s=4.096						)
								_snake_case     		=	output.audios

								_snake_case     		=	audio[0, -3:, -3:]

								assert audio.shape == (1, 2, pipe.unet.sample_size)
								_snake_case     		=	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'''







lowercase					:							Tuple   =      {
    "a": "AAAAA",
    "b": "AAAAB",
    "c": "AAABA",
    "d": "AAABB",
    "e": "AABAA",
    "f": "AABAB",
    "g": "AABBA",
    "h": "AABBB",
    "i": "ABAAA",
    "j": "BBBAA",
    "k": "ABAAB",
    "l": "ABABA",
    "m": "ABABB",
    "n": "ABBAA",
    "o": "ABBAB",
    "p": "ABBBA",
    "q": "ABBBB",
    "r": "BAAAA",
    "s": "BAAAB",
    "t": "BAABA",
    "u": "BAABB",
    "v": "BBBAB",
    "w": "BABAA",
    "x": "BABAB",
    "y": "BABBA",
    "z": "BABBB",
    " ": " ",
}


lowercase					:							Union[str, Any]   =      {value: key for key, value in encode_dict.items()}







def        SCREAMING_SNAKE_CASE__			(     __A			) ->			str:
	_snake_case     		=	''
	for letter in word.lower():
		if letter.isalpha() or letter == " ":
			encoded += encode_dict[letter]
		else:
			raise Exception('encode() accepts only letters of the alphabet and spaces'			)
	return encoded







def        SCREAMING_SNAKE_CASE__			(     __A			) ->			str:
	if set(__A			) - {"A", "B", " "} != set():
		raise Exception('decode() accepts only \'A\', \'B\' and spaces'			)
	_snake_case     		=	''
	for word in coded.split():
		while len(__A			) != 0:
			decoded += decode_dict[word[:5]]
			_snake_case     		=	word[5:]
		decoded += " "
	return decoded.strip()


if __name__ == "__main__":
					from doctest import testmod

					testmod()

