

'''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 							_snake_case	(    _a		,					unittest.TestCase  ):
   _A    :       Optional[Any]      				=   SpectrogramDiffusionPipeline
   _A    :       Tuple      				=   PipelineTesterMixin.required_optional_params - {
       '''callback''',
       '''latents''',
       '''callback_steps''',
       '''output_type''',
       '''num_images_per_prompt''',
   }
   _A    :       str      				=   False
   _A    :       int      				=   False
   _A    :       Tuple      				=   TOKENS_TO_AUDIO_GENERATION_PARAMS
   _A    :       List[Any]      				=   TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS
   def       __UpperCamelCase  (	self     : Optional[Any]							):
     torch.manual_seed(0							)
     SCREAMING_SNAKE_CASE:str   =   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:Any   =   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:Dict   =   TaFilmDecoder(
         input_dims=128       ,targets_length=256       ,max_decoder_noise_time=20_000.0       ,d_model=768       ,num_layers=1       ,num_heads=1       ,d_kv=4       ,d_ff=2_048       ,dropout_rate=0.1       ,)

     SCREAMING_SNAKE_CASE:Union[str, Any]   =   DDPMScheduler()

     SCREAMING_SNAKE_CASE:Any   =   {
         "notes_encoder": notes_encoder.eval(),
         "continuous_encoder": continuous_encoder.eval(),
         "decoder": decoder.eval(),
         "scheduler": scheduler,
         "melgan": None,
     }
     return components
   def       __UpperCamelCase  (	self     : str       ,SCREAMING_SNAKE_CASE__     : List[Any]       ,SCREAMING_SNAKE_CASE__     : Union[str, Any]=0							):
     if str(SCREAMING_SNAKE_CASE__							).startswith("mps"							):
       SCREAMING_SNAKE_CASE:str   =   torch.manual_seed(SCREAMING_SNAKE_CASE__							)
     else:
       SCREAMING_SNAKE_CASE:str   =   torch.Generator(device=SCREAMING_SNAKE_CASE__							).manual_seed(SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Optional[int]   =   {
         "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							):
     SCREAMING_SNAKE_CASE:Dict   =   "cpu"  # ensure determinism for the device-dependent torch.Generator
     SCREAMING_SNAKE_CASE:List[str]   =   self.get_dummy_components()
     SCREAMING_SNAKE_CASE:int   =   SpectrogramDiffusionPipeline(**SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Tuple   =   pipe.to(SCREAMING_SNAKE_CASE__							)
     pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:List[str]   =   self.get_dummy_inputs(SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   pipe(**SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:int   =   output.audios

     SCREAMING_SNAKE_CASE:Union[str, Any]   =   mel[0, -3:, -3:]

     assert mel_slice.shape == (3, 3)
     SCREAMING_SNAKE_CASE:List[Any]   =   np.array(
         [-11.512_925, -4.788_215, -0.46_172_905, -2.051_715, -10.539_147, -10.970_963, -9.091_634, 4.0, 4.0]							)
     assert np.abs(mel_slice.flatten() - expected_slice							).max() < 1e-2
   @skip_mps
   def       __UpperCamelCase  (	self     : Dict							):
     return super().test_save_load_local()
   @skip_mps
   def       __UpperCamelCase  (	self     : Dict							):
     return super().test_dict_tuple_outputs_equivalent()
   @skip_mps
   def       __UpperCamelCase  (	self     : Union[str, Any]							):
     return super().test_save_load_optional_components()
   @skip_mps
   def       __UpperCamelCase  (	self     : Dict							):
     return super().test_attention_slicing_forward_pass()
   def       __UpperCamelCase  (	self     : Optional[Any]							):
     pass
   def       __UpperCamelCase  (	self     : Union[str, Any]							):
     pass







   @skip_mps
   def       __UpperCamelCase  (	self     : Dict							):
     return super().test_progress_bar()





@slow
@require_torch_gpu
@require_onnxruntime
@require_note_seq
class 							_snake_case	(    unittest.TestCase  ):
   def       __UpperCamelCase  (	self     : Optional[int]							):
     # clean up the VRAM after each test
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()
   def       __UpperCamelCase  (	self     : Any							):
     # TODO - test that pipeline can decode tokens in a callback
     # so that music can be played live
     SCREAMING_SNAKE_CASE:int   =   torch_device

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

     SCREAMING_SNAKE_CASE:Union[str, Any]   =   pipe.to(SCREAMING_SNAKE_CASE__							)
     pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__							)

     def callback(SCREAMING_SNAKE_CASE__     : List[Any]       ,SCREAMING_SNAKE_CASE__     : List[Any]							):
       # decode mel to audio
       SCREAMING_SNAKE_CASE:int   =   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:int   =   MidiProcessor()
     SCREAMING_SNAKE_CASE:int   =   processor(SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:Dict   =   input_tokens[:3]
     SCREAMING_SNAKE_CASE:Optional[int]   =   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     : Union[str, Any]							):
     SCREAMING_SNAKE_CASE:int   =   torch_device

     SCREAMING_SNAKE_CASE:Any   =   SpectrogramDiffusionPipeline.from_pretrained("google/music-spectrogram-diffusion"							)
     SCREAMING_SNAKE_CASE:str   =   pipe.to(SCREAMING_SNAKE_CASE__							)
     pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:List[str]   =   MidiProcessor()

     SCREAMING_SNAKE_CASE:int   =   processor(SCREAMING_SNAKE_CASE__							)
     # just run two denoising loops
     SCREAMING_SNAKE_CASE:str   =   input_tokens[:2]

     SCREAMING_SNAKE_CASE:Union[str, Any]   =   torch.manual_seed(0							)
     SCREAMING_SNAKE_CASE:str   =   pipe(SCREAMING_SNAKE_CASE__       ,num_inference_steps=2       ,generator=SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:Optional[int]   =   output.audios[0]

     assert abs(np.abs(SCREAMING_SNAKE_CASE__							).sum() - 3_612.841							) < 1e-1







   def       __UpperCamelCase  (	self     : Tuple							):
     SCREAMING_SNAKE_CASE:Any   =   torch_device

     SCREAMING_SNAKE_CASE:List[Any]   =   SpectrogramDiffusionPipeline.from_pretrained("google/music-spectrogram-diffusion"							)
     SCREAMING_SNAKE_CASE:Optional[Any]   =   pipe.to(SCREAMING_SNAKE_CASE__							)
     pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:List[str]   =   MidiProcessor()

     SCREAMING_SNAKE_CASE:Optional[Any]   =   processor(SCREAMING_SNAKE_CASE__							)

     # just run 4 denoising loops
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   input_tokens[:4]

     SCREAMING_SNAKE_CASE:List[Any]   =   torch.manual_seed(0							)
     SCREAMING_SNAKE_CASE:Optional[Any]   =   pipe(SCREAMING_SNAKE_CASE__       ,num_inference_steps=100       ,generator=SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:Optional[int]   =   output.audios[0]
     assert abs(np.abs(SCREAMING_SNAKE_CASE__							).sum() - 9_389.1_111							) < 5e-2









'''simple docstring'''

from collections import Counter





def 				A_					(	snake_case	):
  return sum(socks_by_color // 2 for socks_by_color in Counter(snake_case	).values()	)


if __name__ == "__main__":
    import doctest

    doctest.testmod()

    A_      =							[int(x) for x in input("Enter socks by color :").rstrip().split()]
    print(f'''sock_merchant({colors}) = {sock_merchant(colors)}''')






