



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:					Union[str, Any]      =						"./tests/fixtures/elise_format0.mid"



@unittest.skip("The note-seq package currently throws an error on import"      )
class       _lowercase				(  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						):
						"""simple docstring"""

						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=2_0000.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 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_=0						):
						"""simple docstring"""

						if str(lowerCamelCase_						).startswith("mps"						):
											a   	=  torch.manual_seed(lowerCamelCase_						)
						else:
											a   	=  torch.Generator(device=lowerCamelCase_						).manual_seed(lowerCamelCase_						)
						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 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  "cpu"  # ensure determinism for the device-dependent torch.Generator
						a   	=  self.get_dummy_components()
						a   	=  SpectrogramDiffusionPipeline(**lowerCamelCase_						)
						a   	=  pipe.to(lowerCamelCase_						)
						pipe.set_progress_bar_config(disable=lowerCamelCase_						)

						a   	=  self.get_dummy_inputs(lowerCamelCase_						)
						a   	=  pipe(**lowerCamelCase_						)
						a   	=  output.audios

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

						assert mel_slice.shape == (3, 3)
						a   	=  np.array(
						    [-11.51_2925, -4.78_8215, -0.4617_2905, -2.05_1715, -10.53_9147, -10.97_0963, -9.09_1634, 4.0, 4.0]						)
						assert np.abs(mel_slice.flatten() - expected_slice						).max() < 1E-2




	@skip_mps
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						return super().test_save_load_local()




	@skip_mps
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						return super().test_dict_tuple_outputs_equivalent()




	@skip_mps
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						return super().test_save_load_optional_components()




	@skip_mps
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						return super().test_attention_slicing_forward_pass()




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						pass




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						pass




	@skip_mps
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						return super().test_progress_bar()




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





	"""simple docstring"""




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

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




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  torch_device

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

						a   	=  pipe.to(lowerCamelCase_						)
						pipe.set_progress_bar_config(disable=lowerCamelCase_						)

						def callback(lowerCamelCase_						,			lowerCamelCase_						):
											# 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(lowerCamelCase_						)

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




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  torch_device

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

						a   	=  processor(lowerCamelCase_						)
						# just run two denoising loops
						a   	=  input_tokens[:2]

						a   	=  torch.manual_seed(0						)
						a   	=  pipe(lowerCamelCase_						,			num_inference_steps=2						,			generator=lowerCamelCase_						)

						a   	=  output.audios[0]

						assert abs(np.abs(lowerCamelCase_						).sum() - 3612.841						) < 1E-1




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  torch_device

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

						a   	=  MidiProcessor()

						a   	=  processor(lowerCamelCase_						)

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

						a   	=  torch.manual_seed(0						)
						a   	=  pipe(lowerCamelCase_						,			num_inference_steps=100						,			generator=lowerCamelCase_						)

						a   	=  output.audios[0]
						assert abs(np.abs(lowerCamelCase_						).sum() - 9389.1111						) < 5E-2





from typing import Optional, Tuple

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_openai import OpenAIGPTTokenizer


_lowercase:					List[str]      =						logging.get_logger(__name__)

_lowercase:					str      =						{"vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_file": "tokenizer.json"}

_lowercase:					Union[str, Any]      =						{
    "vocab_file": {"openai-gpt": "https://huggingface.co/openai-gpt/resolve/main/vocab.json"},
    "merges_file": {"openai-gpt": "https://huggingface.co/openai-gpt/resolve/main/merges.txt"},
    "tokenizer_file": {"openai-gpt": "https://huggingface.co/openai-gpt/resolve/main/tokenizer.json"},
}

_lowercase:					int      =						{
    "openai-gpt": 512,
}



class       _lowercase				(  lowerCAmelCase      ):





	"""simple docstring"""
	__A		      =				VOCAB_FILES_NAMES
	__A		      =				PRETRAINED_VOCAB_FILES_MAP
	__A		      =				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
	__A		      =				["input_ids", "attention_mask"]
	__A		      =				OpenAIGPTTokenizer




	def __init__(self						,			lowerCamelCase_=None						,			lowerCamelCase_=None						,			lowerCamelCase_=None						,			lowerCamelCase_="<unk>"						,			**lowerCamelCase_						):
						"""simple docstring"""

						super().__init__(lowerCamelCase_						,			lowerCamelCase_						,			tokenizer_file=lowerCamelCase_						,			unk_token=lowerCamelCase_						,			**lowerCamelCase_						)




	@property
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						return True




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_ = None						):
						"""simple docstring"""

						a   	=  self._tokenizer.model.save(lowerCamelCase_						,			name=lowerCamelCase_						)
						return tuple(lowerCamelCase_						)
