





'''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()


snake_case_       :    Optional[int]          =					'''./tests/fixtures/elise_format0.mid'''


@unittest.skip("The note-seq package currently throws an error on import"       )
class    __a (snake_case__    ,						unittest.TestCase       ):
	__a   :				Union[str, Any]       =    SpectrogramDiffusionPipeline
	__a   :				Optional[Any]       =    PipelineTesterMixin.required_optional_params - {
	    '''callback''',
	    '''latents''',
	    '''callback_steps''',
	    '''output_type''',
	    '''num_images_per_prompt''',
	}
	__a   :				Dict       =    False
	__a   :				Tuple       =    False
	__a   :				str       =    TOKENS_TO_AUDIO_GENERATION_PARAMS
	__a   :				List[Any]       =    TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS

	def       UpperCAmelCase__		(							self							:       Optional[int]				)   ->    Dict:







							"""simple docstring"""
							torch.manual_seed(0				)
							UpperCAmelCase_     : Dict         =					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'''    ,       )

							UpperCAmelCase_     : Optional[int]         =					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'''    ,       )

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

							UpperCAmelCase_     : Dict         =					DDPMScheduler()

							UpperCAmelCase_     : List[str]         =					{
							    'notes_encoder': notes_encoder.eval(),
							    'continuous_encoder': continuous_encoder.eval(),
							    'decoder': decoder.eval(),
							    'scheduler': scheduler,
							    'melgan': None,
							}
							return components

	def       UpperCAmelCase__		(							self							:       Optional[Any]    ,       __magic_name__							:       Tuple    ,       __magic_name__							:       Dict=0				)   ->    int:







							"""simple docstring"""
							if str(_A				).startswith('''mps'''				):
													UpperCAmelCase_     : Optional[int]         =					torch.manual_seed(_A				)
							else:
													UpperCAmelCase_     : Optional[int]         =					torch.Generator(device=_A				).manual_seed(_A				)
							UpperCAmelCase_     : Dict         =					{
							    '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       UpperCAmelCase__		(							self							:       Union[str, Any]				)   ->    List[Any]:







							"""simple docstring"""
							UpperCAmelCase_     : List[Any]         =					'cpu'  # ensure determinism for the device-dependent torch.Generator
							UpperCAmelCase_     : Dict         =					self.get_dummy_components()
							UpperCAmelCase_     : List[Any]         =					SpectrogramDiffusionPipeline(**_A				)
							UpperCAmelCase_     : List[Any]         =					pipe.to(_A				)
							pipe.set_progress_bar_config(disable=_A				)

							UpperCAmelCase_     : List[str]         =					self.get_dummy_inputs(_A				)
							UpperCAmelCase_     : Any         =					pipe(**_A				)
							UpperCAmelCase_     : Any         =					output.audios

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

							assert mel_slice.shape == (3, 3)
							UpperCAmelCase_     : str         =					np.array(
							    [-11.51_29_25, -4.7_8_8_2_1_5, -0.4_6_1_7_2_9_0_5, -2.0_5_1_7_1_5, -10.53_91_47, -10.97_09_63, -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       UpperCAmelCase__		(							self							:       Dict				)   ->    str:







							"""simple docstring"""
							return super().test_save_load_local()

	@skip_mps
	def       UpperCAmelCase__		(							self							:       int				)   ->    int:







							"""simple docstring"""
							return super().test_dict_tuple_outputs_equivalent()

	@skip_mps
	def       UpperCAmelCase__		(							self							:       Optional[int]				)   ->    Union[str, Any]:







							"""simple docstring"""
							return super().test_save_load_optional_components()

	@skip_mps
	def       UpperCAmelCase__		(							self							:       Optional[int]				)   ->    Any:







							"""simple docstring"""
							return super().test_attention_slicing_forward_pass()

	def       UpperCAmelCase__		(							self							:       Optional[int]				)   ->    int:







							"""simple docstring"""
							pass

	def       UpperCAmelCase__		(							self							:       List[Any]				)   ->    Tuple:







							"""simple docstring"""
							pass



	@skip_mps
	def       UpperCAmelCase__		(							self							:       Tuple				)   ->    List[Any]:







							"""simple docstring"""
							return super().test_progress_bar()





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

	def       UpperCAmelCase__		(							self							:       str				)   ->    Optional[int]:







							"""simple docstring"""
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()

	def       UpperCAmelCase__		(							self							:       List[Any]				)   ->    Tuple:







							"""simple docstring"""
							# TODO - test that pipeline can decode tokens in a callback
							# so that music can be played live
							UpperCAmelCase_     : List[Any]         =					torch_device

							UpperCAmelCase_     : Union[str, Any]         =					SpectrogramDiffusionPipeline.from_pretrained('''google/music-spectrogram-diffusion'''				)
							UpperCAmelCase_     : str         =					pipe.melgan
							UpperCAmelCase_     : Optional[int]         =					None

							UpperCAmelCase_     : Optional[int]         =					pipe.to(_A				)
							pipe.set_progress_bar_config(disable=_A				)

							def callback(__magic_name__							:       Optional[Any]    ,       __magic_name__							:       Union[str, Any]				):
													# decode mel to audio
													UpperCAmelCase_     : 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

							UpperCAmelCase_     : List[str]         =					MidiProcessor()
							UpperCAmelCase_     : Any         =					processor(_A				)

							UpperCAmelCase_     : List[Any]         =					input_tokens[:3]
							UpperCAmelCase_     : Optional[int]         =					torch.manual_seed(0				)
							pipe(_A    ,       num_inference_steps=5    ,       generator=_A    ,       callback=_A    ,       output_type='''mel'''				)

	def       UpperCAmelCase__		(							self							:       Union[str, Any]				)   ->    Tuple:







							"""simple docstring"""
							UpperCAmelCase_     : str         =					torch_device

							UpperCAmelCase_     : Union[str, Any]         =					SpectrogramDiffusionPipeline.from_pretrained('''google/music-spectrogram-diffusion'''				)
							UpperCAmelCase_     : Optional[int]         =					pipe.to(_A				)
							pipe.set_progress_bar_config(disable=_A				)
							UpperCAmelCase_     : Tuple         =					MidiProcessor()

							UpperCAmelCase_     : int         =					processor(_A				)
							# just run two denoising loops
							UpperCAmelCase_     : Union[str, Any]         =					input_tokens[:2]

							UpperCAmelCase_     : Dict         =					torch.manual_seed(0				)
							UpperCAmelCase_     : Any         =					pipe(_A    ,       num_inference_steps=2    ,       generator=_A				)

							UpperCAmelCase_     : Any         =					output.audios[0]

							assert abs(np.abs(_A				).sum() - 36_12.8_41				) < 1E-1



	def       UpperCAmelCase__		(							self							:       Any				)   ->    Optional[int]:







							"""simple docstring"""
							UpperCAmelCase_     : List[Any]         =					torch_device

							UpperCAmelCase_     : Tuple         =					SpectrogramDiffusionPipeline.from_pretrained('''google/music-spectrogram-diffusion'''				)
							UpperCAmelCase_     : Union[str, Any]         =					pipe.to(_A				)
							pipe.set_progress_bar_config(disable=_A				)

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

							UpperCAmelCase_     : List[str]         =					processor(_A				)

							# just run 4 denoising loops
							UpperCAmelCase_     : Optional[int]         =					input_tokens[:4]

							UpperCAmelCase_     : List[Any]         =					torch.manual_seed(0				)
							UpperCAmelCase_     : List[Any]         =					pipe(_A    ,       num_inference_steps=1_00    ,       generator=_A				)

							UpperCAmelCase_     : Union[str, Any]         =					output.audios[0]
							assert abs(np.abs(_A				).sum() - 93_89.11_11				) < 5E-2












'''simple docstring'''







# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
    is_vision_available,
)


snake_case_       :    List[Any]          =					{
    "configuration_tvlt": ["TVLT_PRETRAINED_CONFIG_ARCHIVE_MAP", "TvltConfig"],
    "feature_extraction_tvlt": ["TvltFeatureExtractor"],
    "processing_tvlt": ["TvltProcessor"],
}

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					snake_case_       :    Optional[int]          =					[
					    "TVLT_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "TvltModel",
					    "TvltForPreTraining",
					    "TvltForAudioVisualClassification",
					    "TvltPreTrainedModel",
					]

try:
					if not is_vision_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					snake_case_       :    List[Any]          =					["TvltImageProcessor"]


if TYPE_CHECKING:
					from .configuration_tvlt import TVLT_PRETRAINED_CONFIG_ARCHIVE_MAP, TvltConfig
					from .processing_tvlt import TvltProcessor
					from .feature_extraction_tvlt import TvltFeatureExtractor

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tvlt import (
										    TVLT_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TvltForAudioVisualClassification,
										    TvltForPreTraining,
										    TvltModel,
										    TvltPreTrainedModel,
										)

					try:
										if not is_vision_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .image_processing_tvlt import TvltImageProcessor


else:
					import sys

					snake_case_       :    Any          =					_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





