


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





     def      __A					(self       )     ->   Dict:
      torch.manual_seed(0       )
      _lowercase							      =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=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''')		,     )
      _lowercase							      =IPNDMScheduler()

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





     def      __A					(self		,     UpperCAmelCase		,     UpperCAmelCase=0       )     ->   Tuple:
      if str(UpperCAmelCase       ).startswith('''mps'''       ):
       _lowercase							      =torch.manual_seed(UpperCAmelCase       )
      else:
       _lowercase							      =torch.Generator(device=UpperCAmelCase       ).manual_seed(UpperCAmelCase       )
      _lowercase							      ={
          '''batch_size''': 1,
          '''generator''': generator,
          '''num_inference_steps''': 4,
      }
      return inputs





     def      __A					(self       )     ->   Tuple:
      _lowercase							      ='''cpu'''  # ensure determinism for the device-dependent torch.Generator
      _lowercase							      =self.get_dummy_components()
      _lowercase							      =DanceDiffusionPipeline(**UpperCAmelCase       )
      _lowercase							      =pipe.to(UpperCAmelCase       )
      pipe.set_progress_bar_config(disable=UpperCAmelCase       )

      _lowercase							      =self.get_dummy_inputs(UpperCAmelCase       )
      _lowercase							      =pipe(**UpperCAmelCase       )
      _lowercase							      =output.audios

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

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





     @skip_mps
     def      __A					(self       )     ->   Dict:
      return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3       )





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





     @skip_mps
     def      __A					(self       )     ->   Optional[Any]:
      return super().test_attention_slicing_forward_pass()





     def      __A					(self       )     ->   Optional[int]:
      super().test_inference_batch_single_identical(expected_max_diff=3e-3       )







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





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





     def      __A					(self       )     ->   List[str]:
      _lowercase							      =torch_device

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

      _lowercase							      =torch.manual_seed(0       )
      _lowercase							      =pipe(generator=UpperCAmelCase		,     num_inference_steps=1_0_0		,     audio_length_in_s=4.096       )
      _lowercase							      =output.audios

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

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

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

      _lowercase							      =torch.manual_seed(0       )
      _lowercase							      =pipe(generator=UpperCAmelCase		,     num_inference_steps=1_0_0		,     audio_length_in_s=4.096       )
      _lowercase							      =output.audios

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

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





def 				UpperCAmelCase_					(							__snake_case     ,						__snake_case						)     ->			List[Any]:







 """simple docstring"""


 if b == 0:
  return 1
 if (b % 2) == 0:
  return actual_power(__snake_case     ,						int(b / 2						)						) * actual_power(__snake_case     ,						int(b / 2						)						)
 else:
  return a * actual_power(__snake_case     ,						int(b / 2						)						) * actual_power(__snake_case     ,						int(b / 2						)						)
def 				UpperCAmelCase_					(							__snake_case     ,						__snake_case						)     ->			float:







 """simple docstring"""


 if b < 0:
  return 1 / actual_power(__snake_case     ,						__snake_case						)
 return actual_power(__snake_case     ,						__snake_case						)


if __name__ == "__main__":
  print(power(-2, -3))

