


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


lowerCAmelCase  =				"""./tests/fixtures/elise_format0.mid"""






@unittest.skip('The note-seq package currently throws an error on import'  )
class 					lowerCAmelCase_     (    UpperCAmelCase			,       unittest.TestCase  ):

       '''simple docstring'''





       __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      UpperCamelCase	(   self      )->			List[str]:
            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 ,    _UpperCamelCase ,    _UpperCamelCase=0      )->			Tuple:
            if str(_UpperCamelCase      ).startswith('mps'      ):
                 _A				=       torch.manual_seed(_UpperCamelCase      )
            else:
                 _A				=       torch.Generator(device=_UpperCamelCase      ).manual_seed(_UpperCamelCase      )
            _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      )->			Union[str, Any]:
            _A				=       'cpu'  # ensure determinism for the device-dependent torch.Generator
            _A				=       self.get_dummy_components()
            _A				=       SpectrogramDiffusionPipeline(**_UpperCamelCase      )
            _A				=       pipe.to(_UpperCamelCase      )
            pipe.set_progress_bar_config(disable=_UpperCamelCase      )

            _A				=       self.get_dummy_inputs(_UpperCamelCase      )
            _A				=       pipe(**_UpperCamelCase      )
            _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      )->			int:
            return super().test_save_load_local()






       @skip_mps
       def      UpperCamelCase	(   self      )->			str:
            return super().test_dict_tuple_outputs_equivalent()






       @skip_mps
       def      UpperCamelCase	(   self      )->			List[str]:
            return super().test_save_load_optional_components()






       @skip_mps
       def      UpperCamelCase	(   self      )->			Optional[Any]:
            return super().test_attention_slicing_forward_pass()






       def      UpperCamelCase	(   self      )->			Tuple:
            pass






       def      UpperCamelCase	(   self      )->			List[Any]:
            pass






       @skip_mps
       def      UpperCamelCase	(   self      )->			Union[str, Any]:
            return super().test_progress_bar()






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

       '''simple docstring'''





       def      UpperCamelCase	(   self      )->			str:
            # 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(_UpperCamelCase      )
            pipe.set_progress_bar_config(disable=_UpperCamelCase      )

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

            _A				=       MidiProcessor()
            _A				=       processor(_UpperCamelCase      )

            _A				=       input_tokens[:3]
            _A				=       torch.manual_seed(0      )
            pipe(_UpperCamelCase ,    num_inference_steps=5 ,    generator=_UpperCamelCase ,    callback=_UpperCamelCase ,    output_type='mel'      )






       def      UpperCamelCase	(   self      )->			Union[str, Any]:
            _A				=       torch_device

            _A				=       SpectrogramDiffusionPipeline.from_pretrained('google/music-spectrogram-diffusion'      )
            _A				=       pipe.to(_UpperCamelCase      )
            pipe.set_progress_bar_config(disable=_UpperCamelCase      )
            _A				=       MidiProcessor()

            _A				=       processor(_UpperCamelCase      )
            # just run two denoising loops
            _A				=       input_tokens[:2]

            _A				=       torch.manual_seed(0      )
            _A				=       pipe(_UpperCamelCase ,    num_inference_steps=2 ,    generator=_UpperCamelCase      )

            _A				=       output.audios[0]

            assert abs(np.abs(_UpperCamelCase      ).sum() - 3612.841      ) < 1e-1






       def      UpperCamelCase	(   self      )->			Tuple:
            _A				=       torch_device

            _A				=       SpectrogramDiffusionPipeline.from_pretrained('google/music-spectrogram-diffusion'      )
            _A				=       pipe.to(_UpperCamelCase      )
            pipe.set_progress_bar_config(disable=_UpperCamelCase      )

            _A				=       MidiProcessor()

            _A				=       processor(_UpperCamelCase      )

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

            _A				=       torch.manual_seed(0      )
            _A				=       pipe(_UpperCamelCase ,    num_inference_steps=100 ,    generator=_UpperCamelCase      )

            _A				=       output.audios[0]
            assert abs(np.abs(_UpperCamelCase      ).sum() - 9389.1111      ) < 5e-2









'''simple docstring'''
from itertools import permutations

def 				lowerCamelCase_   (   )				->  Optional[int]:







     """simple docstring"""
     _A				=       list(map(''.join       ,						permutations('0123456789'		)		)		)
     return result[9_9_9_9_9_9]


if __name__ == "__main__":
    print(solution())





