






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_						(	SCREAMING_SNAKE_CASE__					,  unittest.TestCase					):


						'''simple docstring'''




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






						def       _lowerCAmelCase       (	self				) ->				Any:
								torch.manual_seed(0				)
								snake_case_				:							Any						 =       UNetaDModel(
								    block_out_channels=(32, 32, 64)		,				extra_in_channels=16		,				sample_size=512		,				sample_rate=1_6000		,				in_channels=2		,				out_channels=2		,				flip_sin_to_cos=__UpperCamelCase		,				use_timestep_embedding=__UpperCamelCase		,				time_embedding_type="fourier"		,				mid_block_type="UNetMidBlock1D"		,				down_block_types=("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D")		,				up_block_types=("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip")		,				)
								snake_case_				:							str						 =       IPNDMScheduler()

								snake_case_				:							Tuple						 =       {
								    "unet": unet,
								    "scheduler": scheduler,
								}
								return components






						def       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE=0				) ->				Union[str, Any]:
								if str(__UpperCamelCase				).startswith("mps"				):
										snake_case_				:							Dict						 =       torch.manual_seed(__UpperCamelCase				)
								else:
										snake_case_				:							Any						 =       torch.Generator(device=__UpperCamelCase				).manual_seed(__UpperCamelCase				)
								snake_case_				:							Union[str, Any]						 =       {
								    "batch_size": 1,
								    "generator": generator,
								    "num_inference_steps": 4,
								}
								return inputs






						def       _lowerCAmelCase       (	self				) ->				Tuple:
								snake_case_				:							Any						 =       "cpu"  # ensure determinism for the device-dependent torch.Generator
								snake_case_				:							List[str]						 =       self.get_dummy_components()
								snake_case_				:							Any						 =       DanceDiffusionPipeline(**__UpperCamelCase				)
								snake_case_				:							Dict						 =       pipe.to(__UpperCamelCase				)
								pipe.set_progress_bar_config(disable=__UpperCamelCase				)

								snake_case_				:							Tuple						 =       self.get_dummy_inputs(__UpperCamelCase				)
								snake_case_				:							Dict						 =       pipe(**__UpperCamelCase				)
								snake_case_				:							Optional[Any]						 =       output.audios

								snake_case_				:							Dict						 =       audio[0, -3:, -3:]

								assert audio.shape == (1, 2, components["unet"].sample_size)
								snake_case_				:							Tuple						 =       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				) ->				List[Any]:
								return super().test_save_load_local()






						@skip_mps
						def       _lowerCAmelCase       (	self				) ->				Optional[int]:
								return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3				)






						@skip_mps
						def       _lowerCAmelCase       (	self				) ->				Any:
								return super().test_save_load_optional_components()






						@skip_mps
						def       _lowerCAmelCase       (	self				) ->				List[str]:
								return super().test_attention_slicing_forward_pass()






						def       _lowerCAmelCase       (	self				) ->				List[Any]:
								super().test_inference_batch_single_identical(expected_max_diff=3e-3				)




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


						'''simple docstring'''




						def       _lowerCAmelCase       (	self				) ->				str:
								# clean up the VRAM after each test
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()






						def       _lowerCAmelCase       (	self				) ->				Optional[Any]:
								snake_case_				:							str						 =       torch_device

								snake_case_				:							Any						 =       DanceDiffusionPipeline.from_pretrained("harmonai/maestro-150k"				)
								snake_case_				:							Optional[Any]						 =       pipe.to(__UpperCamelCase				)
								pipe.set_progress_bar_config(disable=__UpperCamelCase				)

								snake_case_				:							List[str]						 =       torch.manual_seed(0				)
								snake_case_				:							Union[str, Any]						 =       pipe(generator=__UpperCamelCase		,				num_inference_steps=100		,				audio_length_in_s=4.096				)
								snake_case_				:							List[Any]						 =       output.audios

								snake_case_				:							Union[str, Any]						 =       audio[0, -3:, -3:]

								assert audio.shape == (1, 2, pipe.unet.sample_size)
								snake_case_				:							Tuple						 =       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				) ->				Tuple:
								snake_case_				:							List[str]						 =       torch_device

								snake_case_				:							Any						 =       DanceDiffusionPipeline.from_pretrained("harmonai/maestro-150k"		,				torch_dtype=torch.floataa				)
								snake_case_				:							List[Any]						 =       pipe.to(__UpperCamelCase				)
								pipe.set_progress_bar_config(disable=__UpperCamelCase				)

								snake_case_				:							Tuple						 =       torch.manual_seed(0				)
								snake_case_				:							str						 =       pipe(generator=__UpperCamelCase		,				num_inference_steps=100		,				audio_length_in_s=4.096				)
								snake_case_				:							int						 =       output.audios

								snake_case_				:							Any						 =       audio[0, -3:, -3:]

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








import math

def      lowerCAmelCase__	(    _a       :			int    ):

		if 1 < number < 4:
				# 2 and 3 are primes
				return True
		elif number < 2 or number % 2 == 0 or number % 3 == 0:
				# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
				return False

		# All primes number are in format of 6k +/- 1
		for i in range(5  ,      int(math.sqrt(_a    ) + 1    )  ,      6    ):
				if number % i == 0 or number % (i + 2) == 0:
						return False
		return True

def      lowerCAmelCase__	(    _a       :			float = 0.1    ):
		snake_case_				:							int						 =       3
		snake_case_				:							Optional[Any]						 =       3

		while primes / (2 * j - 1) >= ratio:
				for i in range(j * j + j + 1  ,      (j + 2) * (j + 2)  ,      j + 1    ):
						primes += is_prime(_a    )
				j += 2
		return j


if __name__ == "__main__":
							import doctest

							doctest.testmod()
