code: 
  
  
  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()
  
  
  lowercase_								=    './tests/fixtures/elise_format0.mid'
  
  
  
  
  @unittest.skip("The note-seq package currently throws an error on import"	)
  class 					_UpperCamelCase (      lowerCamelCase__			,							unittest.TestCase	):
  
  
  
  
  
  
  
  							'''simple docstring'''
  							_A  				=  SpectrogramDiffusionPipeline
  							_A  				=  PipelineTesterMixin.required_optional_params - {
  							    "callback",
  							    "latents",
  							    "callback_steps",
  							    "output_type",
  							    "num_images_per_prompt",
  							}
  							_A  				=  False
  							_A  				=  False
  							_A  				=  TOKENS_TO_AUDIO_GENERATION_PARAMS
  							_A  				=  TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS
  
  
  
  
  
  
  
  							def 		_UpperCAmelCase	(		self  :				int		):
  													torch.manual_seed(0		)
  													_a     =			SpectrogramNotesEncoder(
  													    max_length=2_0_4_8		,				vocab_size=1_5_3_6		,				d_model=7_6_8		,				dropout_rate=0.1		,				num_layers=1		,				num_heads=1		,				d_kv=4		,				d_ff=2_0_4_8		,				feed_forward_proj='gated-gelu'		,				)
  
  													_a     =			SpectrogramContEncoder(
  													    input_dims=1_2_8		,				targets_context_length=2_5_6		,				d_model=7_6_8		,				dropout_rate=0.1		,				num_layers=1		,				num_heads=1		,				d_kv=4		,				d_ff=2_0_4_8		,				feed_forward_proj='gated-gelu'		,				)
  
  													_a     =			TaFilmDecoder(
  													    input_dims=1_2_8		,				targets_length=2_5_6		,				max_decoder_noise_time=20000.0		,				d_model=7_6_8		,				num_layers=1		,				num_heads=1		,				d_kv=4		,				d_ff=2_0_4_8		,				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 		_UpperCAmelCase	(		self  :				Union[str, Any]		,				SCREAMING_SNAKE_CASE_  :				Union[str, Any]		,				SCREAMING_SNAKE_CASE_  :				int=0		):
  													if str(SCREAMING_SNAKE_CASE_		).startswith('mps'		):
  																			_a     =			torch.manual_seed(SCREAMING_SNAKE_CASE_		)
  													else:
  																			_a     =			torch.Generator(device=SCREAMING_SNAKE_CASE_		).manual_seed(SCREAMING_SNAKE_CASE_		)
  													_a     =			{
  													    'input_tokens': [
  													        [1_1_3_4, 9_0, 1_1_3_5, 1_1_3_3, 1_0_8_0, 1_1_2, 1_1_3_2, 1_0_8_0, 1_1_3_3, 1_0_7_9, 1_3_3, 1_1_3_2, 1_0_7_9, 1_1_3_3, 1] + [0] * 2_0_3_3
  													    ],
  													    'generator': generator,
  													    'num_inference_steps': 4,
  													    'output_type': 'mel',
  													}
  													return inputs
  
  
  
  
  
  
  
  							def 		_UpperCAmelCase	(		self  :				str		):
  													_a     =			'cpu'  # ensure determinism for the device-dependent torch.Generator
  													_a     =			self.get_dummy_components()
  													_a     =			SpectrogramDiffusionPipeline(**SCREAMING_SNAKE_CASE_		)
  													_a     =			pipe.to(SCREAMING_SNAKE_CASE_		)
  													pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_		)
  
  													_a     =			self.get_dummy_inputs(SCREAMING_SNAKE_CASE_		)
  													_a     =			pipe(**SCREAMING_SNAKE_CASE_		)
  													_a     =			output.audios
  
  													_a     =			mel[0, -3:, -3:]
  
  													assert mel_slice.shape == (3, 3)
  													_a     =			np.array(
  													    [-11.512925, -4.788215, -0.46172905, -2.051715, -10.539147, -10.970963, -9.091634, 4.0, 4.0]		)
  													assert np.abs(mel_slice.flatten() - expected_slice		).max() < 1e-2
  
  
  
  
  
  
  
  							@skip_mps
  							def 		_UpperCAmelCase	(		self  :				Optional[int]		):
  													return super().test_save_load_local()
  
  
  
  
  
  
  
  							@skip_mps
  							def 		_UpperCAmelCase	(		self  :				Union[str, Any]		):
  													return super().test_dict_tuple_outputs_equivalent()
  
  
  
  
  
  
  
  							@skip_mps
  							def 		_UpperCAmelCase	(		self  :				Optional[int]		):
  													return super().test_save_load_optional_components()
  
  
  
  
  
  
  
  							@skip_mps
  							def 		_UpperCAmelCase	(		self  :				Tuple		):
  													return super().test_attention_slicing_forward_pass()
  
  
  
  
  
  
  
  							def 		_UpperCAmelCase	(		self  :				Optional[Any]		):
  													pass
  
  
  
  
  
  
  
  							def 		_UpperCAmelCase	(		self  :				str		):
  													pass
  
  
  
  
  
  
  
  							@skip_mps
  							def 		_UpperCAmelCase	(		self  :				Tuple		):
  													return super().test_progress_bar()
  
  
  
  
  @slow
  @require_torch_gpu
  @require_onnxruntime
  @require_note_seq
  class 					_UpperCamelCase (      unittest.TestCase	):
  
  
  
  
  
  
  
  							'''simple docstring'''
  
  
  
  
  
  
  
  							def 		_UpperCAmelCase	(		self  :				Tuple		):
  													# clean up the VRAM after each test
  													super().tearDown()
  													gc.collect()
  													torch.cuda.empty_cache()
  
  
  
  
  
  
  
  							def 		_UpperCAmelCase	(		self  :				int		):
  													# 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(SCREAMING_SNAKE_CASE_		)
  													pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_		)
  
  													def callback(SCREAMING_SNAKE_CASE_  :				Union[str, Any]		,				SCREAMING_SNAKE_CASE_  :				List[Any]		):
  																			# decode mel to audio
  																			_a     =			melgan(input_features=mel_output.astype(np.floataa		)		)[0]
  																			assert len(audio[0]		) == 8_1_9_2_0 * (step + 1)
  																			# simulate that audio is played
  																			return audio
  
  													_a     =			MidiProcessor()
  													_a     =			processor(SCREAMING_SNAKE_CASE_		)
  
  													_a     =			input_tokens[:3]
  													_a     =			torch.manual_seed(0		)
  													pipe(SCREAMING_SNAKE_CASE_		,				num_inference_steps=5		,				generator=SCREAMING_SNAKE_CASE_		,				callback=SCREAMING_SNAKE_CASE_		,				output_type='mel'		)
  
  
  
  
  
  
  
  							def 		_UpperCAmelCase	(		self  :				Tuple		):
  													_a     =			torch_device
  
  													_a     =			SpectrogramDiffusionPipeline.from_pretrained('google/music-spectrogram-diffusion'		)
  													_a     =			pipe.to(SCREAMING_SNAKE_CASE_		)
  													pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_		)
  													_a     =			MidiProcessor()
  
  													_a     =			processor(SCREAMING_SNAKE_CASE_		)
  													# just run two denoising loops
  													_a     =			input_tokens[:2]
  
  													_a     =			torch.manual_seed(0		)
  													_a     =			pipe(SCREAMING_SNAKE_CASE_		,				num_inference_steps=2		,				generator=SCREAMING_SNAKE_CASE_		)
  
  													_a     =			output.audios[0]
  
  													assert abs(np.abs(SCREAMING_SNAKE_CASE_		).sum() - 3612.841		) < 1e-1
  
  
  
  
  
  
  
  							def 		_UpperCAmelCase	(		self  :				Dict		):
  													_a     =			torch_device
  
  													_a     =			SpectrogramDiffusionPipeline.from_pretrained('google/music-spectrogram-diffusion'		)
  													_a     =			pipe.to(SCREAMING_SNAKE_CASE_		)
  													pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_		)
  
  													_a     =			MidiProcessor()
  
  													_a     =			processor(SCREAMING_SNAKE_CASE_		)
  
  													# just run 4 denoising loops
  													_a     =			input_tokens[:4]
  
  													_a     =			torch.manual_seed(0		)
  													_a     =			pipe(SCREAMING_SNAKE_CASE_		,				num_inference_steps=1_0_0		,				generator=SCREAMING_SNAKE_CASE_		)
  
  													_a     =			output.audios[0]
  													assert abs(np.abs(SCREAMING_SNAKE_CASE_		).sum() - 9389.1111		) < 5e-2
  
  
  
style_context: 
  
  
  from math import pi
  
  
  def 							SCREAMING_SNAKE_CASE					(  _UpperCAmelCase				)    ->  float:
  						return degree / (180 / pi)
  
  
  if __name__ == "__main__":
  		from doctest import testmod
  
  		testmod()
  
  
  