





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


UpperCAmelCase__  :	str     ='''./tests/fixtures/elise_format0.mid'''

@unittest.skip("""The note-seq package currently throws an error on import"""			)
class 					__A   (  a_			,    unittest.TestCase			):
				__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 _snake_case				(						self							):
								torch.manual_seed(0							)
								lowerCamelCase		       =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"""      ,       )

								lowerCamelCase		       =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"""      ,       )

								lowerCamelCase		       =TaFilmDecoder(
								    input_dims=128      ,       targets_length=256      ,       max_decoder_noise_time=20000.0      ,       d_model=768      ,       num_layers=1      ,       num_heads=1      ,       d_kv=4      ,       d_ff=2048      ,       dropout_rate=0.1      ,       )

								lowerCamelCase		       =DDPMScheduler()

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


				def _snake_case				(						self      ,       UpperCAmelCase_      ,       UpperCAmelCase_=0							):

								if str(lowercase_							).startswith("""mps"""							):
												lowerCamelCase		       =torch.manual_seed(lowercase_							)
								else:
												lowerCamelCase		       =torch.Generator(device=lowercase_							).manual_seed(lowercase_							)
								lowerCamelCase		       ={
								    """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 _snake_case				(						self							):
								lowerCamelCase		       ="""cpu"""  # ensure determinism for the device-dependent torch.Generator
								lowerCamelCase		       =self.get_dummy_components()
								lowerCamelCase		       =SpectrogramDiffusionPipeline(**lowercase_							)
								lowerCamelCase		       =pipe.to(lowercase_							)
								pipe.set_progress_bar_config(disable=lowercase_							)

								lowerCamelCase		       =self.get_dummy_inputs(lowercase_							)
								lowerCamelCase		       =pipe(**lowercase_							)
								lowerCamelCase		       =output.audios

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

								assert mel_slice.shape == (3, 3)
								lowerCamelCase		       =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 _snake_case				(						self							):
								return super().test_save_load_local()


				@skip_mps
				def _snake_case				(						self							):
								return super().test_dict_tuple_outputs_equivalent()


				@skip_mps
				def _snake_case				(						self							):
								return super().test_save_load_optional_components()


				@skip_mps
				def _snake_case				(						self							):
								return super().test_attention_slicing_forward_pass()


				def _snake_case				(						self							):
								pass


				def _snake_case				(						self							):
								pass


				@skip_mps
				def _snake_case				(						self							):
								return super().test_progress_bar()




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


				def _snake_case				(						self							):
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()


				def _snake_case				(						self							):
								lowerCamelCase		       =torch_device

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

								lowerCamelCase		       =pipe.to(lowercase_							)
								pipe.set_progress_bar_config(disable=lowercase_							)

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

								lowerCamelCase		       =MidiProcessor()
								lowerCamelCase		       =processor(lowercase_							)

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


				def _snake_case				(						self							):
								lowerCamelCase		       =torch_device

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

								lowerCamelCase		       =processor(lowercase_							)
								# just run two denoising loops
								lowerCamelCase		       =input_tokens[:2]

								lowerCamelCase		       =torch.manual_seed(0							)
								lowerCamelCase		       =pipe(lowercase_      ,       num_inference_steps=2      ,       generator=lowercase_							)

								lowerCamelCase		       =output.audios[0]

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


				def _snake_case				(						self							):
								lowerCamelCase		       =torch_device

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

								lowerCamelCase		       =MidiProcessor()

								lowerCamelCase		       =processor(lowercase_							)

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

								lowerCamelCase		       =torch.manual_seed(0							)
								lowerCamelCase		       =pipe(lowercase_      ,       num_inference_steps=100      ,       generator=lowercase_							)

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













from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
		import PIL


lowerCamelCase_						     =  logging.get_logger(__name__)




class 							a_ (						a_		):


							'''simple docstring'''






							__a:	Optional[Any]     =    ['''pixel_values''']

							def __init__(		self			,			lowercase_ = True			,			lowercase_ = None			,			lowercase_ = True			,			lowercase_ = None			,			lowercase_ = PILImageResampling.BICUBIC			,			lowercase_ = True			,			lowercase_ = 1 / 2_5_5			,			lowercase_ = True			,			lowercase_ = None			,			lowercase_ = None			,			**lowercase_			,			)					->					None:
														'''simple docstring'''

														super().__init__(**lowercase_   )
														lowerCAmelCase_										=     crop_size if crop_size is not None else {'height': 2_5_6, 'width': 2_5_6}
														lowerCAmelCase_										=     get_size_dict(lowercase_			,			param_name='crop_size'   )
														lowerCAmelCase_										=     size if size is not None else {'height': 2_2_4, 'width': 2_2_4}
														lowerCAmelCase_										=     get_size_dict(lowercase_   )

														lowerCAmelCase_										=     do_center_crop
														lowerCAmelCase_										=     crop_size
														lowerCAmelCase_										=     do_resize
														lowerCAmelCase_										=     size
														lowerCAmelCase_										=     resample
														lowerCAmelCase_										=     do_rescale
														lowerCAmelCase_										=     rescale_factor
														lowerCAmelCase_										=     do_normalize
														lowerCAmelCase_										=     image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
														lowerCAmelCase_										=     image_std if image_std is not None else IMAGENET_DEFAULT_STD

							def      _lowercase       (		self			,			lowercase_			,			lowercase_			,			lowercase_ = None			,			lowercase_ = None			,			**lowercase_			,			)					->					np.ndarray:
														'''simple docstring'''

														lowerCAmelCase_										=     self.size if size is None else size
														lowerCAmelCase_										=     get_size_dict(lowercase_   )
														lowerCAmelCase_										=     get_size_dict(lowercase_			,			param_name='crop_size'   )

														lowerCAmelCase_						,   lowerCAmelCase_										=     get_image_size(lowercase_   )
														lowerCAmelCase_										=     min(lowercase_			,			lowercase_   )
														lowerCAmelCase_										=     (size['height'] / crop_size['height']) * min_dim
														lowerCAmelCase_										=     (size['width'] / crop_size['width']) * min_dim
														return center_crop(lowercase_			,			size=(cropped_height, cropped_width)			,			data_format=lowercase_			,			**lowercase_   )

							def      _lowercase       (		self			,			lowercase_			,			lowercase_			,			lowercase_ = PIL.Image.BICUBIC			,			lowercase_ = None			,			**lowercase_			,			)					->					np.ndarray:
														'''simple docstring'''

														lowerCAmelCase_										=     get_size_dict(lowercase_   )
														if "height" not in size or "width" not in size:
																					raise ValueError(f'''The size dictionary must contain the keys \'height\' and \'width\'. Got {size.keys()}'''   )
														return resize(
														    lowercase_			,			size=(size['height'], size['width'])			,			resample=lowercase_			,			data_format=lowercase_			,			**lowercase_   )

							def      _lowercase       (		self			,			lowercase_			,			lowercase_			,			lowercase_ = None			,			**lowercase_			,			)					->					Any:
														'''simple docstring'''

														return rescale(lowercase_			,			scale=lowercase_			,			data_format=lowercase_			,			**lowercase_   )

							def      _lowercase       (		self			,			lowercase_			,			lowercase_			,			lowercase_			,			lowercase_ = None			,			**lowercase_			,			)					->					np.ndarray:
														'''simple docstring'''

														return normalize(lowercase_			,			mean=lowercase_			,			std=lowercase_			,			data_format=lowercase_			,			**lowercase_   )






							def      _lowercase       (		self			,			lowercase_			,			lowercase_ = None			,			lowercase_ = None			,			lowercase_ = None			,			lowercase_ = None			,			lowercase_ = None			,			lowercase_ = None			,			lowercase_ = None			,			lowercase_ = None			,			lowercase_ = None			,			lowercase_ = None			,			lowercase_ = None			,			lowercase_ = ChannelDimension.FIRST			,			**lowercase_			,			)					->					PIL.Image.Image:
														'''simple docstring'''

														lowerCAmelCase_										=     do_center_crop if do_center_crop is not None else self.do_center_crop
														lowerCAmelCase_										=     crop_size if crop_size is not None else self.crop_size
														lowerCAmelCase_										=     get_size_dict(lowercase_			,			param_name='crop_size'   )
														lowerCAmelCase_										=     do_resize if do_resize is not None else self.do_resize
														lowerCAmelCase_										=     size if size is not None else self.size
														lowerCAmelCase_										=     get_size_dict(lowercase_   )
														lowerCAmelCase_										=     resample if resample is not None else self.resample
														lowerCAmelCase_										=     do_rescale if do_rescale is not None else self.do_rescale
														lowerCAmelCase_										=     rescale_factor if rescale_factor is not None else self.rescale_factor
														lowerCAmelCase_										=     do_normalize if do_normalize is not None else self.do_normalize
														lowerCAmelCase_										=     image_mean if image_mean is not None else self.image_mean
														lowerCAmelCase_										=     image_std if image_std is not None else self.image_std

														lowerCAmelCase_										=     make_list_of_images(lowercase_   )

														if not valid_images(lowercase_   ):
																					raise ValueError(
																					    'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
																					    'torch.Tensor, tf.Tensor or jax.ndarray.'   )

														if do_center_crop and crop_size is None:
																					raise ValueError('If `do_center_crop` is set to `True`, `crop_size` must be provided.'   )

														if do_resize and size is None:
																					raise ValueError('Size must be specified if do_resize is True.'   )

														if do_rescale and rescale_factor is None:
																					raise ValueError('Rescale factor must be specified if do_rescale is True.'   )

														if do_normalize and (image_mean is None or image_std is None):
																					raise ValueError('Image mean and image standard deviation must be specified if do_normalize is True.'   )

														# All transformations expect numpy arrays.
														lowerCAmelCase_										=     [to_numpy_array(lowercase_   ) for image in images]

														if do_center_crop:
																					lowerCAmelCase_										=     [self.center_crop(lowercase_			,			lowercase_			,			size=lowercase_   ) for image in images]

														if do_resize:
																					lowerCAmelCase_										=     [self.resize(image=lowercase_			,			size=lowercase_			,			resample=lowercase_   ) for image in images]

														if do_rescale:
																					lowerCAmelCase_										=     [self.rescale(image=lowercase_			,			scale=lowercase_   ) for image in images]

														if do_normalize:
																					lowerCAmelCase_										=     [self.normalize(image=lowercase_			,			mean=lowercase_			,			std=lowercase_   ) for image in images]

														lowerCAmelCase_										=     [to_channel_dimension_format(lowercase_			,			lowercase_   ) for image in images]

														lowerCAmelCase_										=     {'pixel_values': images}
														return BatchFeature(data=lowercase_			,			tensor_type=lowercase_   )



