

import gc
import unittest

import numpy as np
import torch

from diffusers import DDPMScheduler, MidiProcessor, SpectrogramDiffusionPipeline
from diffusers.pipelines.spectrogram_diffusion import SpectrogramContEncoder, SpectrogramNotesEncoder, TaFilmDecoder
from diffusers.utils import require_torch_gpu, skip_mps, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_note_seq, require_onnxruntime

from ..pipeline_params import TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS, TOKENS_TO_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


__a						:List[Any]            = './tests/fixtures/elise_format0.mid'

@unittest.skip('The note-seq package currently throws an error on import'							)
class      _a				( snake_case_    ,							unittest.TestCase							):




      """simple docstring"""

      _lowerCamelCase					: Optional[int]					=					SpectrogramDiffusionPipeline
      _lowerCamelCase					: Union[str, Any]					=					PipelineTesterMixin.required_optional_params - {
          'callback',
          'latents',
          'callback_steps',
          'output_type',
          'num_images_per_prompt',
      }
      _lowerCamelCase					: Dict					=					False
      _lowerCamelCase					: Optional[int]					=					False
      _lowerCamelCase					: Any					=					TOKENS_TO_AUDIO_GENERATION_PARAMS
      _lowerCamelCase					: Tuple					=					TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS





      def 		__A  (      self       :							Union[str, Any]  ):
            torch.manual_seed(0  )
            A_	     =							SpectrogramNotesEncoder(
                max_length=2048	, vocab_size=1536	, d_model=768	, dropout_rate=0.1	, num_layers=1	, num_heads=1	, d_kv=4	, d_ff=2048	, feed_forward_proj="gated-gelu"	, )

            A_	     =							SpectrogramContEncoder(
                input_dims=128	, targets_context_length=256	, d_model=768	, dropout_rate=0.1	, num_layers=1	, num_heads=1	, d_kv=4	, d_ff=2048	, feed_forward_proj="gated-gelu"	, )

            A_	     =							TaFilmDecoder(
                input_dims=128	, targets_length=256	, max_decoder_noise_time=20_000.0	, d_model=768	, num_layers=1	, num_heads=1	, d_kv=4	, d_ff=2048	, dropout_rate=0.1	, )

            A_	     =							DDPMScheduler()

            A_	     =							{
                "notes_encoder": notes_encoder.eval(),
                "continuous_encoder": continuous_encoder.eval(),
                "decoder": decoder.eval(),
                "scheduler": scheduler,
                "melgan": None,
            }
            return components





      def 		__A  (      self       :							Tuple	, UpperCAmelCase       :							str	, UpperCAmelCase       :							Optional[Any]=0  ):
            if str(UpperCAmelCase  ).startswith("mps"  ):
                  A_	     =							torch.manual_seed(UpperCAmelCase  )
            else:
                  A_	     =							torch.Generator(device=UpperCAmelCase  ).manual_seed(UpperCAmelCase  )
            A_	     =							{
                "input_tokens": [
                    [1134, 90, 1135, 1133, 1080, 112, 1132, 1080, 1133, 1079, 133, 1132, 1079, 1133, 1] + [0] * 2033
                ],
                "generator": generator,
                "num_inference_steps": 4,
                "output_type": "mel",
            }
            return inputs





      def 		__A  (      self       :							int  ):
            A_	     =							"cpu"  # ensure determinism for the device-dependent torch.Generator
            A_	     =							self.get_dummy_components()
            A_	     =							SpectrogramDiffusionPipeline(**UpperCAmelCase  )
            A_	     =							pipe.to(UpperCAmelCase  )
            pipe.set_progress_bar_config(disable=UpperCAmelCase  )

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

            A_	     =							mel[0, -3:, -3:]

            assert mel_slice.shape == (3, 3)
            A_	     =							np.array(
                [-11.512_925, -4.788_215, -0.46_172_905, -2.051_715, -10.539_147, -10.970_963, -9.091_634, 4.0, 4.0]  )
            assert np.abs(mel_slice.flatten() - expected_slice  ).max() < 1E-2





      @skip_mps
      def 		__A  (      self       :							Dict  ):
            return super().test_save_load_local()





      @skip_mps
      def 		__A  (      self       :							Tuple  ):
            return super().test_dict_tuple_outputs_equivalent()





      @skip_mps
      def 		__A  (      self       :							Optional[Any]  ):
            return super().test_save_load_optional_components()





      @skip_mps
      def 		__A  (      self       :							List[str]  ):
            return super().test_attention_slicing_forward_pass()





      def 		__A  (      self       :							Any  ):
            pass





      def 		__A  (      self       :							List[Any]  ):
            pass







      @skip_mps
      def 		__A  (      self       :							List[str]  ):
            return super().test_progress_bar()




@slow
@require_torch_gpu
@require_onnxruntime
@require_note_seq
class      _a				( unittest.TestCase							):




      """simple docstring"""

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





      def 		__A  (      self       :							Dict  ):
            # TODO - test that pipeline can decode tokens in a callback
            # so that music can be played live
            A_	     =							torch_device

            A_	     =							SpectrogramDiffusionPipeline.from_pretrained("google/music-spectrogram-diffusion"  )
            A_	     =							pipe.melgan
            A_	     =							None

            A_	     =							pipe.to(UpperCAmelCase  )
            pipe.set_progress_bar_config(disable=UpperCAmelCase  )

            def callback(UpperCAmelCase       :							Optional[Any]	, UpperCAmelCase       :							Optional[int]  ):
                  # decode mel to audio
                  A_	     =							melgan(input_features=mel_output.astype(np.floataa  )  )[0]
                  assert len(audio[0]  ) == 81920 * (step + 1)
                  # simulate that audio is played
                  return audio

            A_	     =							MidiProcessor()
            A_	     =							processor(UpperCAmelCase  )

            A_	     =							input_tokens[:3]
            A_	     =							torch.manual_seed(0  )
            pipe(UpperCAmelCase	, num_inference_steps=5	, generator=UpperCAmelCase	, callback=UpperCAmelCase	, output_type="mel"  )





      def 		__A  (      self       :							Optional[Any]  ):
            A_	     =							torch_device

            A_	     =							SpectrogramDiffusionPipeline.from_pretrained("google/music-spectrogram-diffusion"  )
            A_	     =							pipe.to(UpperCAmelCase  )
            pipe.set_progress_bar_config(disable=UpperCAmelCase  )
            A_	     =							MidiProcessor()

            A_	     =							processor(UpperCAmelCase  )
            # just run two denoising loops
            A_	     =							input_tokens[:2]

            A_	     =							torch.manual_seed(0  )
            A_	     =							pipe(UpperCAmelCase	, num_inference_steps=2	, generator=UpperCAmelCase  )

            A_	     =							output.audios[0]

            assert abs(np.abs(UpperCAmelCase  ).sum() - 3_612.841  ) < 1E-1







      def 		__A  (      self       :							List[Any]  ):
            A_	     =							torch_device

            A_	     =							SpectrogramDiffusionPipeline.from_pretrained("google/music-spectrogram-diffusion"  )
            A_	     =							pipe.to(UpperCAmelCase  )
            pipe.set_progress_bar_config(disable=UpperCAmelCase  )

            A_	     =							MidiProcessor()

            A_	     =							processor(UpperCAmelCase  )

            # just run 4 denoising loops
            A_	     =							input_tokens[:4]

            A_	     =							torch.manual_seed(0  )
            A_	     =							pipe(UpperCAmelCase	, num_inference_steps=100	, generator=UpperCAmelCase  )

            A_	     =							output.audios[0]
            assert abs(np.abs(UpperCAmelCase  ).sum() - 9_389.1_111  ) < 5E-2


import warnings

from ...utils import logging
from .image_processing_mobilenet_va import MobileNetVaImageProcessor


__a						:Optional[int]            = logging.get_logger(__name__)

class      _a				( snake_case_							):




      """simple docstring"""

      def __init__(      self       :							List[Any]	, *UpperCAmelCase       :							Any	, **UpperCAmelCase       :							int  ):
            warnings.warn(
                "The class MobileNetV2FeatureExtractor is deprecated and will be removed in version 5 of Transformers."
                " Please use MobileNetV2ImageProcessor instead."	, UpperCAmelCase	, )
            super().__init__(*UpperCAmelCase	, **UpperCAmelCase  )