






'''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        a_		(    _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 						A__		(   self   )						->   Union[str, Any]:



        """simple docstring"""






        torch.manual_seed(0   )
        UpperCamelCase       						=      UNetaDModel(
            block_out_channels=(32, 32, 64)    ,	extra_in_channels=16    ,	sample_size=512    ,	sample_rate=16000    ,	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""")    ,	)
        UpperCamelCase       						=      IPNDMScheduler()

        UpperCamelCase       						=      {
            """unet""": unet,
            """scheduler""": scheduler,
        }
        return components

  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE=0   )						->   int:



        """simple docstring"""






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

  def 						A__		(   self   )						->   List[str]:



        """simple docstring"""






        UpperCamelCase       						=      """cpu"""  # ensure determinism for the device-dependent torch.Generator
        UpperCamelCase       						=      self.get_dummy_components()
        UpperCamelCase       						=      DanceDiffusionPipeline(**_lowerCAmelCase   )
        UpperCamelCase       						=      pipe.to(_lowerCAmelCase   )
        pipe.set_progress_bar_config(disable=_lowerCAmelCase   )

        UpperCamelCase       						=      self.get_dummy_inputs(_lowerCAmelCase   )
        UpperCamelCase       						=      pipe(**_lowerCAmelCase   )
        UpperCamelCase       						=      output.audios

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

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



        """simple docstring"""






        return super().test_save_load_local()

  @skip_mps
  def 						A__		(   self   )						->   Union[str, Any]:



        """simple docstring"""






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

  @skip_mps
  def 						A__		(   self   )						->   Dict:



        """simple docstring"""






        return super().test_save_load_optional_components()

  @skip_mps
  def 						A__		(   self   )						->   Any:



        """simple docstring"""






        return super().test_attention_slicing_forward_pass()


  def 						A__		(   self   )						->   int:



        """simple docstring"""






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






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

  def 						A__		(   self   )						->   Union[str, Any]:



        """simple docstring"""






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

  def 						A__		(   self   )						->   Union[str, Any]:



        """simple docstring"""






        UpperCamelCase       						=      torch_device

        UpperCamelCase       						=      DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k"""   )
        UpperCamelCase       						=      pipe.to(_lowerCAmelCase   )
        pipe.set_progress_bar_config(disable=_lowerCAmelCase   )

        UpperCamelCase       						=      torch.manual_seed(0   )
        UpperCamelCase       						=      pipe(generator=_lowerCAmelCase    ,	num_inference_steps=100    ,	audio_length_in_s=4.0_9_6   )
        UpperCamelCase       						=      output.audios

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

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



        """simple docstring"""






        UpperCamelCase       						=      torch_device

        UpperCamelCase       						=      DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k"""    ,	torch_dtype=torch.floataa   )
        UpperCamelCase       						=      pipe.to(_lowerCAmelCase   )
        pipe.set_progress_bar_config(disable=_lowerCAmelCase   )

        UpperCamelCase       						=      torch.manual_seed(0   )
        UpperCamelCase       						=      pipe(generator=_lowerCAmelCase    ,	num_inference_steps=100    ,	audio_length_in_s=4.0_9_6   )
        UpperCamelCase       						=      output.audios

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

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









'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import _LazyModule


SCREAMING_SNAKE_CASE__											=     {'tokenization_wav2vec2_phoneme': ['Wav2Vec2PhonemeCTCTokenizer']}


if TYPE_CHECKING:
     from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer
else:
     import sys

     SCREAMING_SNAKE_CASE__											=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
