

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


UpperCAmelCase				: List[str]         =  './tests/fixtures/elise_format0.mid'







@unittest.skip("The note-seq package currently throws an error on import"      )
class 		lowerCAmelCase__     (   a				,      unittest.TestCase      ):



 """simple docstring"""



 lowerCAmelCase__						      =   SpectrogramDiffusionPipeline
 lowerCAmelCase__						      =   PipelineTesterMixin.required_optional_params - {
     "callback",
     "latents",
     "callback_steps",
     "output_type",
     "num_images_per_prompt",
 }
 lowerCAmelCase__						      =   False
 lowerCAmelCase__						      =   False
 lowerCAmelCase__						      =   TOKENS_TO_AUDIO_GENERATION_PARAMS
 lowerCAmelCase__						      =   TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS
 def    UpperCAmelCase__	(	self      : List[Any]    )						->				Any:


      """simple docstring"""




      torch.manual_seed(0    )
      __SCREAMING_SNAKE_CASE						     =   SpectrogramNotesEncoder(
          max_length=2_048      ,							vocab_size=1_536      ,							d_model=768      ,							dropout_rate=0.1      ,							num_layers=1      ,							num_heads=1      ,							d_kv=4      ,							d_ff=2_048      ,							feed_forward_proj="""gated-gelu"""      ,							)

      __SCREAMING_SNAKE_CASE						     =   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=2_048      ,							feed_forward_proj="""gated-gelu"""      ,							)

      __SCREAMING_SNAKE_CASE						     =   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=2_048      ,							dropout_rate=0.1      ,							)

      __SCREAMING_SNAKE_CASE						     =   DDPMScheduler()

      __SCREAMING_SNAKE_CASE						     =   {
          """notes_encoder""": notes_encoder.eval(),
          """continuous_encoder""": continuous_encoder.eval(),
          """decoder""": decoder.eval(),
          """scheduler""": scheduler,
          """melgan""": None,
      }
      return components
 def    UpperCAmelCase__	(	self      : Optional[Any]      ,							__SCREAMING_SNAKE_CASE      : Optional[int]      ,							__SCREAMING_SNAKE_CASE      : List[Any]=0    )						->				Optional[Any]:


      """simple docstring"""




      if str(__SCREAMING_SNAKE_CASE    ).startswith("""mps"""    ):
           __SCREAMING_SNAKE_CASE						     =   torch.manual_seed(__SCREAMING_SNAKE_CASE    )
      else:
           __SCREAMING_SNAKE_CASE						     =   torch.Generator(device=__SCREAMING_SNAKE_CASE    ).manual_seed(__SCREAMING_SNAKE_CASE    )
      __SCREAMING_SNAKE_CASE						     =   {
          """input_tokens""": [
              [1_134, 90, 1_135, 1_133, 1_080, 112, 1_132, 1_080, 1_133, 1_079, 133, 1_132, 1_079, 1_133, 1] + [0] * 2_033
          ],
          """generator""": generator,
          """num_inference_steps""": 4,
          """output_type""": """mel""",
      }
      return inputs
 def    UpperCAmelCase__	(	self      : Any    )						->				Union[str, Any]:


      """simple docstring"""




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

      __SCREAMING_SNAKE_CASE						     =   self.get_dummy_inputs(__SCREAMING_SNAKE_CASE    )
      __SCREAMING_SNAKE_CASE						     =   pipe(**__SCREAMING_SNAKE_CASE    )
      __SCREAMING_SNAKE_CASE						     =   output.audios

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

      assert mel_slice.shape == (3, 3)
      __SCREAMING_SNAKE_CASE						     =   np.array(
          [-11.512925, -4.788215, -0.46172905, -2.051715, -10.539147, -10.970963, -9.091634, 4.0, 4.0]    )
      assert np.abs(mel_slice.flatten() - expected_slice    ).max() < 1E-2
 @skip_mps
 def    UpperCAmelCase__	(	self      : List[Any]    )						->				Union[str, Any]:


      """simple docstring"""




      return super().test_save_load_local()
 @skip_mps
 def    UpperCAmelCase__	(	self      : Optional[int]    )						->				Dict:


      """simple docstring"""




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


      """simple docstring"""




      return super().test_save_load_optional_components()
 @skip_mps
 def    UpperCAmelCase__	(	self      : Optional[int]    )						->				Tuple:


      """simple docstring"""




      return super().test_attention_slicing_forward_pass()
 def    UpperCAmelCase__	(	self      : Union[str, Any]    )						->				str:


      """simple docstring"""




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


      """simple docstring"""




      pass


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


      """simple docstring"""




      return super().test_progress_bar()







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



 """simple docstring"""



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


      """simple docstring"""




      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()
 def    UpperCAmelCase__	(	self      : Union[str, Any]    )						->				Union[str, Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   torch_device

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

      __SCREAMING_SNAKE_CASE						     =   pipe.to(__SCREAMING_SNAKE_CASE    )
      pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE    )

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

      __SCREAMING_SNAKE_CASE						     =   MidiProcessor()
      __SCREAMING_SNAKE_CASE						     =   processor(__SCREAMING_SNAKE_CASE    )

      __SCREAMING_SNAKE_CASE						     =   input_tokens[:3]
      __SCREAMING_SNAKE_CASE						     =   torch.manual_seed(0    )
      pipe(__SCREAMING_SNAKE_CASE      ,							num_inference_steps=5      ,							generator=__SCREAMING_SNAKE_CASE      ,							callback=__SCREAMING_SNAKE_CASE      ,							output_type="""mel"""    )
 def    UpperCAmelCase__	(	self      : Any    )						->				int:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   torch_device

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

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

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

      __SCREAMING_SNAKE_CASE						     =   output.audios[0]

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


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


      """simple docstring"""




      __SCREAMING_SNAKE_CASE						     =   torch_device

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

      __SCREAMING_SNAKE_CASE						     =   MidiProcessor()

      __SCREAMING_SNAKE_CASE						     =   processor(__SCREAMING_SNAKE_CASE    )

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

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

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









'''simple docstring'''



def      a__ (						):




     """simple docstring"""


     __SCREAMING_SNAKE_CASE						     =   1

     while True:
          if (
              sorted(str(a__		)		)
              == sorted(str(2 * i		)		)
              == sorted(str(3 * i		)		)
              == sorted(str(4 * i		)		)
              == sorted(str(5 * i		)		)
              == sorted(str(6 * i		)		)
          ):
               return i

          i += 1


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






