


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 	A_ (	__UpperCamelCase ,				unittest.TestCase   ):




						'''simple docstring'''


						__snake_case 	=       DanceDiffusionPipeline
						__snake_case 	=       UNCONDITIONAL_AUDIO_GENERATION_PARAMS
						__snake_case 	=       PipelineTesterMixin.required_optional_params - {
						    """callback""",
						    """latents""",
						    """callback_steps""",
						    """output_type""",
						    """num_images_per_prompt""",
						}
						__snake_case 	=       UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
						__snake_case 	=       False
						__snake_case 	=       False




						def 					_snake_case     (    self:	Union[str, Any]    ):
							torch.manual_seed(0    )
							__lowerCamelCase		:							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=a						,				use_timestep_embedding=a						,				time_embedding_type='fourier'						,				mid_block_type='UNetMidBlock1D'						,				down_block_types=('DownBlock1DNoSkip', 'DownBlock1D', 'AttnDownBlock1D')						,				up_block_types=('AttnUpBlock1D', 'UpBlock1D', 'UpBlock1DNoSkip')						,				)
							__lowerCamelCase		:							Union[str, Any]		 =						IPNDMScheduler()

							__lowerCamelCase		:							int		 =						{
							    'unet': unet,
							    'scheduler': scheduler,
							}
							return components




						def 					_snake_case     (    self:	Optional[Any]						,				a:	Optional[int]						,				a:	Optional[Any]=0    ):
							if str(a    ).startswith('mps'    ):
								__lowerCamelCase		:							Optional[int]		 =						torch.manual_seed(a    )
							else:
								__lowerCamelCase		:							int		 =						torch.Generator(device=a    ).manual_seed(a    )
							__lowerCamelCase		:							List[str]		 =						{
							    'batch_size': 1,
							    'generator': generator,
							    'num_inference_steps': 4,
							}
							return inputs




						def 					_snake_case     (    self:	List[str]    ):
							__lowerCamelCase		:							Tuple		 =						'cpu'  # ensure determinism for the device-dependent torch.Generator
							__lowerCamelCase		:							Optional[int]		 =						self.get_dummy_components()
							__lowerCamelCase		:							Tuple		 =						DanceDiffusionPipeline(**a    )
							__lowerCamelCase		:							Dict		 =						pipe.to(a    )
							pipe.set_progress_bar_config(disable=a    )

							__lowerCamelCase		:							Optional[int]		 =						self.get_dummy_inputs(a    )
							__lowerCamelCase		:							Optional[int]		 =						pipe(**a    )
							__lowerCamelCase		:							List[Any]		 =						output.audios

							__lowerCamelCase		:							int		 =						audio[0, -3:, -3:]

							assert audio.shape == (1, 2, components["unet"].sample_size)
							__lowerCamelCase		:							List[str]		 =						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 					_snake_case     (    self:	int    ):
							return super().test_save_load_local()




						@skip_mps
						def 					_snake_case     (    self:	Any    ):
							return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3    )




						@skip_mps
						def 					_snake_case     (    self:	Optional[int]    ):
							return super().test_save_load_optional_components()




						@skip_mps
						def 					_snake_case     (    self:	Optional[int]    ):
							return super().test_attention_slicing_forward_pass()




						def 					_snake_case     (    self:	Any    ):
							super().test_inference_batch_single_identical(expected_max_diff=3e-3    )



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




						'''simple docstring'''


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




						def 					_snake_case     (    self:	Optional[Any]    ):
							__lowerCamelCase		:							int		 =						torch_device

							__lowerCamelCase		:							List[str]		 =						DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k'    )
							__lowerCamelCase		:							int		 =						pipe.to(a    )
							pipe.set_progress_bar_config(disable=a    )

							__lowerCamelCase		:							Union[str, Any]		 =						torch.manual_seed(0    )
							__lowerCamelCase		:							List[Any]		 =						pipe(generator=a						,				num_inference_steps=100						,				audio_length_in_s=4.0_9_6    )
							__lowerCamelCase		:							str		 =						output.audios

							__lowerCamelCase		:							List[Any]		 =						audio[0, -3:, -3:]

							assert audio.shape == (1, 2, pipe.unet.sample_size)
							__lowerCamelCase		:							str		 =						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 					_snake_case     (    self:	Optional[Any]    ):
							__lowerCamelCase		:							List[str]		 =						torch_device

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

							__lowerCamelCase		:							List[Any]		 =						torch.manual_seed(0    )
							__lowerCamelCase		:							Optional[int]		 =						pipe(generator=a						,				num_inference_steps=100						,				audio_length_in_s=4.0_9_6    )
							__lowerCamelCase		:							Optional[Any]		 =						output.audios

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

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





from maths.is_square_free import is_square_free
from maths.prime_factors import prime_factors
def 				UpperCamelCase__					(  SCREAMING_SNAKE_CASE__						):
 __lowerCamelCase		:							Dict		 =						prime_factors(SCREAMING_SNAKE_CASE__						)
 if is_square_free(SCREAMING_SNAKE_CASE__						):
  return -1 if len(SCREAMING_SNAKE_CASE__						) % 2 else 1
 return 0


if __name__ == "__main__":
       import doctest

       doctest.testmod()

