




"""simple docstring"""




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_            =   './tests/fixtures/elise_format0.mid'

@unittest.skip("The note-seq package currently throws an error on import"						)
class        UpperCAmelCase_  (	snake_case ,			unittest.TestCase						):
		UpperCamelCase									=SpectrogramDiffusionPipeline
		UpperCamelCase									=PipelineTesterMixin.required_optional_params - {
		    "callback",
		    "latents",
		    "callback_steps",
		    "output_type",
		    "num_images_per_prompt",
		}
		UpperCamelCase									=False
		UpperCamelCase									=False
		UpperCamelCase									=TOKENS_TO_AUDIO_GENERATION_PARAMS
		UpperCamelCase									=TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS
		def 					_lowerCamelCase    (		self   )		->   Optional[int]:
					torch.manual_seed(0   )
					__lowercase	:       Any				=   SpectrogramNotesEncoder(
					    max_length=20_48							,		vocab_size=15_36							,		d_model=7_68							,		dropout_rate=0.1							,		num_layers=1							,		num_heads=1							,		d_kv=4							,		d_ff=20_48							,		feed_forward_proj='''gated-gelu'''							,		)

					__lowercase	:       List[Any]				=   SpectrogramContEncoder(
					    input_dims=1_28							,		targets_context_length=2_56							,		d_model=7_68							,		dropout_rate=0.1							,		num_layers=1							,		num_heads=1							,		d_kv=4							,		d_ff=20_48							,		feed_forward_proj='''gated-gelu'''							,		)

					__lowercase	:       Optional[int]				=   TaFilmDecoder(
					    input_dims=1_28							,		targets_length=2_56							,		max_decoder_noise_time=2_0_0_0_0.0							,		d_model=7_68							,		num_layers=1							,		num_heads=1							,		d_kv=4							,		d_ff=20_48							,		dropout_rate=0.1							,		)

					__lowercase	:       int				=   DDPMScheduler()

					__lowercase	:       Tuple				=   {
					    '''notes_encoder''': notes_encoder.eval(),
					    '''continuous_encoder''': continuous_encoder.eval(),
					    '''decoder''': decoder.eval(),
					    '''scheduler''': scheduler,
					    '''melgan''': None,
					}
					return components
		def 					_lowerCamelCase    (		self							,		UpperCamelCase_							,		UpperCamelCase_=0   )		->   str:
					if str(UpperCamelCase_   ).startswith('''mps'''   ):
								__lowercase	:       Optional[int]				=   torch.manual_seed(UpperCamelCase_   )
					else:
								__lowercase	:       int				=   torch.Generator(device=UpperCamelCase_   ).manual_seed(UpperCamelCase_   )
					__lowercase	:       List[Any]				=   {
					    '''input_tokens''': [
					        [11_34, 90, 11_35, 11_33, 10_80, 1_12, 11_32, 10_80, 11_33, 10_79, 1_33, 11_32, 10_79, 11_33, 1] + [0] * 20_33
					    ],
					    '''generator''': generator,
					    '''num_inference_steps''': 4,
					    '''output_type''': '''mel''',
					}
					return inputs
		def 					_lowerCamelCase    (		self   )		->   Union[str, Any]:
					__lowercase	:       int				=   '''cpu'''  # ensure determinism for the device-dependent torch.Generator
					__lowercase	:       List[str]				=   self.get_dummy_components()
					__lowercase	:       Tuple				=   SpectrogramDiffusionPipeline(**UpperCamelCase_   )
					__lowercase	:       Optional[Any]				=   pipe.to(UpperCamelCase_   )
					pipe.set_progress_bar_config(disable=UpperCamelCase_   )

					__lowercase	:       Dict				=   self.get_dummy_inputs(UpperCamelCase_   )
					__lowercase	:       int				=   pipe(**UpperCamelCase_   )
					__lowercase	:       Optional[int]				=   output.audios

					__lowercase	:       Dict				=   mel[0, -3:, -3:]

					assert mel_slice.shape == (3, 3)
					__lowercase	:       Optional[Any]				=   np.array(
					    [-1_1.5_1_2_9_2_5, -4.7_8_8_2_1_5, -0.4_6_1_7_2_9_0_5, -2.0_5_1_7_1_5, -1_0.5_3_9_1_4_7, -1_0.9_7_0_9_6_3, -9.0_9_1_6_3_4, 4.0, 4.0]   )
					assert np.abs(mel_slice.flatten() - expected_slice   ).max() < 1E-2
		@skip_mps
		def 					_lowerCamelCase    (		self   )		->   List[str]:
					return super().test_save_load_local()
		@skip_mps
		def 					_lowerCamelCase    (		self   )		->   List[Any]:
					return super().test_dict_tuple_outputs_equivalent()
		@skip_mps
		def 					_lowerCamelCase    (		self   )		->   Optional[Any]:
					return super().test_save_load_optional_components()
		@skip_mps
		def 					_lowerCamelCase    (		self   )		->   str:
					return super().test_attention_slicing_forward_pass()
		def 					_lowerCamelCase    (		self   )		->   Tuple:
					pass
		def 					_lowerCamelCase    (		self   )		->   int:
					pass
		@skip_mps
		def 					_lowerCamelCase    (		self   )		->   Optional[int]:
					return super().test_progress_bar()


@slow
@require_torch_gpu
@require_onnxruntime
@require_note_seq
class        UpperCAmelCase_  (	unittest.TestCase						):
		def 					_lowerCamelCase    (		self   )		->   Dict:
					# clean up the VRAM after each test
					super().tearDown()
					gc.collect()
					torch.cuda.empty_cache()
		def 					_lowerCamelCase    (		self   )		->   Any:
					# TODO - test that pipeline can decode tokens in a callback
					# so that music can be played live
					__lowercase	:       Any				=   torch_device

					__lowercase	:       Optional[Any]				=   SpectrogramDiffusionPipeline.from_pretrained('''google/music-spectrogram-diffusion'''   )
					__lowercase	:       Any				=   pipe.melgan
					__lowercase	:       List[str]				=   None

					__lowercase	:       Dict				=   pipe.to(UpperCamelCase_   )
					pipe.set_progress_bar_config(disable=UpperCamelCase_   )

					def callback(UpperCamelCase_							,		UpperCamelCase_   ):
								# decode mel to audio
								__lowercase	:       Union[str, Any]				=   melgan(input_features=mel_output.astype(np.floataa   )   )[0]
								assert len(audio[0]   ) == 8_19_20 * (step + 1)
								# simulate that audio is played
								return audio

					__lowercase	:       Dict				=   MidiProcessor()
					__lowercase	:       Tuple				=   processor(UpperCamelCase_   )

					__lowercase	:       Dict				=   input_tokens[:3]
					__lowercase	:       Union[str, Any]				=   torch.manual_seed(0   )
					pipe(UpperCamelCase_							,		num_inference_steps=5							,		generator=UpperCamelCase_							,		callback=UpperCamelCase_							,		output_type='''mel'''   )
		def 					_lowerCamelCase    (		self   )		->   int:
					__lowercase	:       int				=   torch_device

					__lowercase	:       List[Any]				=   SpectrogramDiffusionPipeline.from_pretrained('''google/music-spectrogram-diffusion'''   )
					__lowercase	:       str				=   pipe.to(UpperCamelCase_   )
					pipe.set_progress_bar_config(disable=UpperCamelCase_   )
					__lowercase	:       Optional[Any]				=   MidiProcessor()

					__lowercase	:       List[Any]				=   processor(UpperCamelCase_   )
					# just run two denoising loops
					__lowercase	:       int				=   input_tokens[:2]

					__lowercase	:       Dict				=   torch.manual_seed(0   )
					__lowercase	:       int				=   pipe(UpperCamelCase_							,		num_inference_steps=2							,		generator=UpperCamelCase_   )

					__lowercase	:       Union[str, Any]				=   output.audios[0]

					assert abs(np.abs(UpperCamelCase_   ).sum() - 3_6_1_2.8_4_1   ) < 1E-1
		def 					_lowerCamelCase    (		self   )		->   Optional[int]:
					__lowercase	:       Union[str, Any]				=   torch_device

					__lowercase	:       Optional[int]				=   SpectrogramDiffusionPipeline.from_pretrained('''google/music-spectrogram-diffusion'''   )
					__lowercase	:       List[str]				=   pipe.to(UpperCamelCase_   )
					pipe.set_progress_bar_config(disable=UpperCamelCase_   )

					__lowercase	:       Union[str, Any]				=   MidiProcessor()

					__lowercase	:       Tuple				=   processor(UpperCamelCase_   )

					# just run 4 denoising loops
					__lowercase	:       Any				=   input_tokens[:4]

					__lowercase	:       Union[str, Any]				=   torch.manual_seed(0   )
					__lowercase	:       Tuple				=   pipe(UpperCamelCase_							,		num_inference_steps=1_00							,		generator=UpperCamelCase_   )

					__lowercase	:       Tuple				=   output.audios[0]
					assert abs(np.abs(UpperCamelCase_   ).sum() - 9_3_8_9.1_1_1_1   ) < 5E-2









"""simple docstring"""




import json
import os
import shutil
import tempfile
import unittest

from transformers import is_speech_available, is_torch_available
from transformers.models.speechta import SpeechTaTokenizer
from transformers.testing_utils import get_tests_dir, require_torch
from transformers.utils import FEATURE_EXTRACTOR_NAME


if is_speech_available() and is_torch_available():
			from transformers import SpeechTaFeatureExtractor, SpeechTaProcessor

			from .test_feature_extraction_speechta import floats_list


a_            =   get_tests_dir('fixtures/test_sentencepiece_bpe_char.model')

@require_torch
class        UpperCAmelCase_  (	unittest.TestCase						):
		def 					_lowerCamelCase    (		self   )		->   Dict:
					__lowercase	:       Union[str, Any]				=   tempfile.mkdtemp()

					__lowercase	:       List[str]				=   SpeechTaTokenizer(UpperCamelCase_   )
					tokenizer.save_pretrained(self.tmpdirname   )

					__lowercase	:       Dict				=   {
					    '''feature_size''': 1,
					    '''padding_value''': 0.0,
					    '''sampling_rate''': 1_60_00,
					    '''do_normalize''': False,
					    '''num_mel_bins''': 80,
					    '''hop_length''': 16,
					    '''win_length''': 64,
					    '''win_function''': '''hann_window''',
					    '''fmin''': 80,
					    '''fmax''': 76_00,
					    '''mel_floor''': 1E-10,
					    '''reduction_factor''': 2,
					    '''return_attention_mask''': True,
					}

					__lowercase	:       List[Any]				=   os.path.join(self.tmpdirname							,		UpperCamelCase_   )
					with open(self.feature_extraction_file							,		'''w'''							,		encoding='''utf-8'''   ) as fp:
								fp.write(json.dumps(UpperCamelCase_   ) + '''\n'''   )
		def 					_lowerCamelCase    (		self							,		**UpperCamelCase_   )		->   Union[str, Any]:
					return SpeechTaTokenizer.from_pretrained(self.tmpdirname							,		**UpperCamelCase_   )
		def 					_lowerCamelCase    (		self							,		**UpperCamelCase_   )		->   Optional[Any]:
					return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname							,		**UpperCamelCase_   )
		def 					_lowerCamelCase    (		self   )		->   Any:
					shutil.rmtree(self.tmpdirname   )
		def 					_lowerCamelCase    (		self   )		->   Union[str, Any]:
					__lowercase	:       List[str]				=   self.get_tokenizer()
					__lowercase	:       int				=   self.get_feature_extractor()

					__lowercase	:       Any				=   SpeechTaProcessor(tokenizer=UpperCamelCase_							,		feature_extractor=UpperCamelCase_   )

					processor.save_pretrained(self.tmpdirname   )
					__lowercase	:       List[Any]				=   SpeechTaProcessor.from_pretrained(self.tmpdirname   )

					self.assertEqual(processor.tokenizer.get_vocab()							,		tokenizer.get_vocab()   )
					self.assertIsInstance(processor.tokenizer							,		UpperCamelCase_   )

					self.assertEqual(processor.feature_extractor.to_json_string()							,		feature_extractor.to_json_string()   )
					self.assertIsInstance(processor.feature_extractor							,		UpperCamelCase_   )
		def 					_lowerCamelCase    (		self   )		->   Optional[Any]:
					__lowercase	:       Optional[int]				=   SpeechTaProcessor(tokenizer=self.get_tokenizer()							,		feature_extractor=self.get_feature_extractor()   )
					processor.save_pretrained(self.tmpdirname   )

					__lowercase	:       str				=   self.get_tokenizer(bos_token='''(BOS)'''							,		eos_token='''(EOS)'''   )
					__lowercase	:       Dict				=   self.get_feature_extractor(do_normalize=UpperCamelCase_							,		padding_value=1.0   )

					__lowercase	:       Tuple				=   SpeechTaProcessor.from_pretrained(
					    self.tmpdirname							,		bos_token='''(BOS)'''							,		eos_token='''(EOS)'''							,		do_normalize=UpperCamelCase_							,		padding_value=1.0   )

					self.assertEqual(processor.tokenizer.get_vocab()							,		tokenizer_add_kwargs.get_vocab()   )
					self.assertIsInstance(processor.tokenizer							,		UpperCamelCase_   )

					self.assertEqual(processor.feature_extractor.to_json_string()							,		feature_extractor_add_kwargs.to_json_string()   )
					self.assertIsInstance(processor.feature_extractor							,		UpperCamelCase_   )
		def 					_lowerCamelCase    (		self   )		->   List[str]:
					__lowercase	:       Optional[Any]				=   self.get_feature_extractor()
					__lowercase	:       Optional[Any]				=   self.get_tokenizer()

					__lowercase	:       Union[str, Any]				=   SpeechTaProcessor(tokenizer=UpperCamelCase_							,		feature_extractor=UpperCamelCase_   )

					__lowercase	:       int				=   floats_list((3, 10_00)   )

					__lowercase	:       str				=   feature_extractor(audio=UpperCamelCase_							,		return_tensors='''np'''   )
					__lowercase	:       Dict				=   processor(audio=UpperCamelCase_							,		return_tensors='''np'''   )

					for key in input_feat_extract.keys():
								self.assertAlmostEqual(input_feat_extract[key].sum()							,		input_processor[key].sum()							,		delta=1E-2   )
		def 					_lowerCamelCase    (		self   )		->   List[Any]:
					__lowercase	:       str				=   self.get_feature_extractor()
					__lowercase	:       str				=   self.get_tokenizer()

					__lowercase	:       Union[str, Any]				=   SpeechTaProcessor(tokenizer=UpperCamelCase_							,		feature_extractor=UpperCamelCase_   )

					__lowercase	:       Optional[int]				=   floats_list((3, 10_00)   )

					__lowercase	:       List[str]				=   feature_extractor(audio_target=UpperCamelCase_							,		return_tensors='''np'''   )
					__lowercase	:       List[str]				=   processor(audio_target=UpperCamelCase_							,		return_tensors='''np'''   )

					for key in input_feat_extract.keys():
								self.assertAlmostEqual(input_feat_extract[key].sum()							,		input_processor[key].sum()							,		delta=1E-2   )
		def 					_lowerCamelCase    (		self   )		->   Any:
					__lowercase	:       List[Any]				=   self.get_feature_extractor()
					__lowercase	:       List[str]				=   self.get_tokenizer()

					__lowercase	:       Union[str, Any]				=   SpeechTaProcessor(tokenizer=UpperCamelCase_							,		feature_extractor=UpperCamelCase_   )

					__lowercase	:       List[str]				=   '''This is a test string'''

					__lowercase	:       Any				=   processor(text=UpperCamelCase_   )
					__lowercase	:       Optional[int]				=   tokenizer(UpperCamelCase_   )

					for key in encoded_tok.keys():
								self.assertListEqual(encoded_tok[key]							,		encoded_processor[key]   )
		def 					_lowerCamelCase    (		self   )		->   str:
					__lowercase	:       int				=   self.get_feature_extractor()
					__lowercase	:       List[str]				=   self.get_tokenizer()

					__lowercase	:       List[Any]				=   SpeechTaProcessor(tokenizer=UpperCamelCase_							,		feature_extractor=UpperCamelCase_   )

					__lowercase	:       Union[str, Any]				=   '''This is a test string'''

					__lowercase	:       Tuple				=   processor(text_target=UpperCamelCase_   )
					__lowercase	:       Optional[int]				=   tokenizer(UpperCamelCase_   )

					for key in encoded_tok.keys():
								self.assertListEqual(encoded_tok[key]							,		encoded_processor[key]   )
		def 					_lowerCamelCase    (		self   )		->   Optional[Any]:
					__lowercase	:       Optional[int]				=   self.get_feature_extractor()
					__lowercase	:       str				=   self.get_tokenizer()

					__lowercase	:       int				=   SpeechTaProcessor(tokenizer=UpperCamelCase_							,		feature_extractor=UpperCamelCase_   )

					__lowercase	:       Optional[Any]				=   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

					__lowercase	:       str				=   processor.batch_decode(UpperCamelCase_   )
					__lowercase	:       int				=   tokenizer.batch_decode(UpperCamelCase_   )

					self.assertListEqual(UpperCamelCase_							,		UpperCamelCase_   )
		def 					_lowerCamelCase    (		self   )		->   Optional[int]:
					__lowercase	:       Union[str, Any]				=   self.get_feature_extractor()
					__lowercase	:       int				=   self.get_tokenizer()

					__lowercase	:       Optional[int]				=   SpeechTaProcessor(tokenizer=UpperCamelCase_							,		feature_extractor=UpperCamelCase_   )

					self.assertListEqual(
					    processor.model_input_names							,		feature_extractor.model_input_names							,		msg='''`processor` and `feature_extractor` model input names do not match'''							,		)



