




'''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      SCREAMING_SNAKE_CASE    (						snake_case_							,      unittest.TestCase				):
      __magic_name__  :       List[Any]   							=					DanceDiffusionPipeline
      __magic_name__  :       Optional[int]   							=					UNCONDITIONAL_AUDIO_GENERATION_PARAMS
      __magic_name__  :       Optional[int]   							=					PipelineTesterMixin.required_optional_params - {
          '''callback''',
          '''latents''',
          '''callback_steps''',
          '''output_type''',
          '''num_images_per_prompt''',
      }
      __magic_name__  :       List[Any]   							=					UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
      __magic_name__  :       int   							=					False
      __magic_name__  :       Optional[int]   							=					False






      def 							lowercase_						( self   :  List[str]       ):


           '''simple docstring'''

           torch.manual_seed(0       )
           a_     :							str     						=  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=lowercase__						,						use_timestep_embedding=lowercase__						,						time_embedding_type="""fourier"""						,						mid_block_type="""UNetMidBlock1D"""						,						down_block_types=("""DownBlock1DNoSkip""", """DownBlock1D""", """AttnDownBlock1D""")						,						up_block_types=("""AttnUpBlock1D""", """UpBlock1D""", """UpBlock1DNoSkip""")						,						)
           a_     :							List[str]     						=  IPNDMScheduler()

           a_     :							Any     						=  {
               """unet""": unet,
               """scheduler""": scheduler,
           }
           return components






      def 							lowercase_						( self   :  int						,						lowercase__   :  int						,						lowercase__   :  Dict=0       ):


           '''simple docstring'''

           if str(lowercase__       ).startswith("""mps"""       ):
                a_     :							Union[str, Any]     						=  torch.manual_seed(lowercase__       )
           else:
                a_     :							List[Any]     						=  torch.Generator(device=lowercase__       ).manual_seed(lowercase__       )
           a_     :							Optional[Any]     						=  {
               """batch_size""": 1,
               """generator""": generator,
               """num_inference_steps""": 4,
           }
           return inputs






      def 							lowercase_						( self   :  Tuple       ):


           '''simple docstring'''

           a_     :							int     						=  """cpu"""  # ensure determinism for the device-dependent torch.Generator
           a_     :							List[str]     						=  self.get_dummy_components()
           a_     :							Union[str, Any]     						=  DanceDiffusionPipeline(**lowercase__       )
           a_     :							Optional[Any]     						=  pipe.to(lowercase__       )
           pipe.set_progress_bar_config(disable=lowercase__       )

           a_     :							List[Any]     						=  self.get_dummy_inputs(lowercase__       )
           a_     :							Union[str, Any]     						=  pipe(**lowercase__       )
           a_     :							int     						=  output.audios

           a_     :							str     						=  audio[0, -3:, -3:]

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


           '''simple docstring'''

           return super().test_save_load_local()






      @skip_mps
      def 							lowercase_						( self   :  int       ):


           '''simple docstring'''

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






      @skip_mps
      def 							lowercase_						( self   :  Union[str, Any]       ):


           '''simple docstring'''

           return super().test_save_load_optional_components()






      @skip_mps
      def 							lowercase_						( self   :  str       ):


           '''simple docstring'''

           return super().test_attention_slicing_forward_pass()






      def 							lowercase_						( self   :  Tuple       ):


           '''simple docstring'''

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







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






      def 							lowercase_						( self   :  Optional[Any]       ):


           '''simple docstring'''

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






      def 							lowercase_						( self   :  Optional[Any]       ):


           '''simple docstring'''

           a_     :							Dict     						=  torch_device

           a_     :							str     						=  DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k"""       )
           a_     :							List[str]     						=  pipe.to(lowercase__       )
           pipe.set_progress_bar_config(disable=lowercase__       )

           a_     :							Any     						=  torch.manual_seed(0       )
           a_     :							Optional[int]     						=  pipe(generator=lowercase__						,						num_inference_steps=100						,						audio_length_in_s=4.096       )
           a_     :							str     						=  output.audios

           a_     :							Tuple     						=  audio[0, -3:, -3:]

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


           '''simple docstring'''

           a_     :							Any     						=  torch_device

           a_     :							str     						=  DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k"""						,						torch_dtype=torch.floataa       )
           a_     :							Dict     						=  pipe.to(lowercase__       )
           pipe.set_progress_bar_config(disable=lowercase__       )

           a_     :							str     						=  torch.manual_seed(0       )
           a_     :							List[str]     						=  pipe(generator=lowercase__						,						num_inference_steps=100						,						audio_length_in_s=4.096       )
           a_     :							Tuple     						=  output.audios

           a_     :							str     						=  audio[0, -3:, -3:]

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


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


lowerCAmelCase_      :  Optional[Any]				     =  {}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCAmelCase_      :  Any				     =  ['GPTSw3Tokenizer']


if TYPE_CHECKING:
 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_gpt_swa import GPTSwaTokenizer

else:
 import sys

 lowerCAmelCase_      :  int				     =  _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
