






"""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     :     List[Any]            =	"""./tests/fixtures/elise_format0.mid"""




@unittest.skip('''The note-seq package currently throws an error on import'''					)
class        lowerCAmelCase__       (  __magic_name__				,		unittest.TestCase					):
   SCREAMING_SNAKE_CASE_					      =SpectrogramDiffusionPipeline
   SCREAMING_SNAKE_CASE_					      =PipelineTesterMixin.required_optional_params - {
       '''callback''',
       '''latents''',
       '''callback_steps''',
       '''output_type''',
       '''num_images_per_prompt''',
   }
   SCREAMING_SNAKE_CASE_					      =False
   SCREAMING_SNAKE_CASE_					      =False
   SCREAMING_SNAKE_CASE_					      =TOKENS_TO_AUDIO_GENERATION_PARAMS
   SCREAMING_SNAKE_CASE_					      =TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS
   def 	__a				(						self	:     Any				):


        '''simple docstring'''
        torch.manual_seed(0				)
        UpperCAmelCase__		:  Optional[Any]				=				SpectrogramNotesEncoder(
            max_length=2_0_4_8				,       vocab_size=1_5_3_6				,       d_model=7_6_8				,       dropout_rate=0.1				,       num_layers=1				,       num_heads=1				,       d_kv=4				,       d_ff=2_0_4_8				,       feed_forward_proj="gated-gelu"				,       )

        UpperCAmelCase__		:  Any				=				SpectrogramContEncoder(
            input_dims=1_2_8				,       targets_context_length=2_5_6				,       d_model=7_6_8				,       dropout_rate=0.1				,       num_layers=1				,       num_heads=1				,       d_kv=4				,       d_ff=2_0_4_8				,       feed_forward_proj="gated-gelu"				,       )

        UpperCAmelCase__		:  List[Any]				=				TaFilmDecoder(
            input_dims=1_2_8				,       targets_length=2_5_6				,       max_decoder_noise_time=2_0000.0				,       d_model=7_6_8				,       num_layers=1				,       num_heads=1				,       d_kv=4				,       d_ff=2_0_4_8				,       dropout_rate=0.1				,       )

        UpperCAmelCase__		:  Any				=				DDPMScheduler()

        UpperCAmelCase__		:  Optional[int]				=				{
            "notes_encoder": notes_encoder.eval(),
            "continuous_encoder": continuous_encoder.eval(),
            "decoder": decoder.eval(),
            "scheduler": scheduler,
            "melgan": None,
        }
        return components
   def 	__a				(						self	:     str				,       snake_case__	:     Optional[int]				,       snake_case__	:     Optional[Any]=0				):


        '''simple docstring'''
        if str(snake_case__				).startswith("mps"				):
             UpperCAmelCase__		:  Any				=				torch.manual_seed(snake_case__				)
        else:
             UpperCAmelCase__		:  Optional[Any]				=				torch.Generator(device=snake_case__				).manual_seed(snake_case__				)
        UpperCAmelCase__		:  Any				=				{
            "input_tokens": [
                [1_1_3_4, 9_0, 1_1_3_5, 1_1_3_3, 1_0_8_0, 1_1_2, 1_1_3_2, 1_0_8_0, 1_1_3_3, 1_0_7_9, 1_3_3, 1_1_3_2, 1_0_7_9, 1_1_3_3, 1] + [0] * 2_0_3_3
            ],
            "generator": generator,
            "num_inference_steps": 4,
            "output_type": "mel",
        }
        return inputs
   def 	__a				(						self	:     str				):


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

        UpperCAmelCase__		:  int				=				self.get_dummy_inputs(snake_case__				)
        UpperCAmelCase__		:  int				=				pipe(**snake_case__				)
        UpperCAmelCase__		:  int				=				output.audios

        UpperCAmelCase__		:  List[Any]				=				mel[0, -3:, -3:]

        assert mel_slice.shape == (3, 3)
        UpperCAmelCase__		:  Optional[Any]				=				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 	__a				(						self	:     Union[str, Any]				):


        '''simple docstring'''
        return super().test_save_load_local()
   @skip_mps
   def 	__a				(						self	:     Tuple				):


        '''simple docstring'''
        return super().test_dict_tuple_outputs_equivalent()
   @skip_mps
   def 	__a				(						self	:     List[Any]				):


        '''simple docstring'''
        return super().test_save_load_optional_components()
   @skip_mps
   def 	__a				(						self	:     int				):


        '''simple docstring'''
        return super().test_attention_slicing_forward_pass()
   def 	__a				(						self	:     List[Any]				):


        '''simple docstring'''
        pass
   def 	__a				(						self	:     Dict				):


        '''simple docstring'''
        pass




   @skip_mps
   def 	__a				(						self	:     Dict				):


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




@slow
@require_torch_gpu
@require_onnxruntime
@require_note_seq
class        lowerCAmelCase__       (  unittest.TestCase					):
   def 	__a				(						self	:     List[str]				):


        '''simple docstring'''
        # clean up the VRAM after each test
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()
   def 	__a				(						self	:     str				):


        '''simple docstring'''
        # TODO - test that pipeline can decode tokens in a callback
        # so that music can be played live
        UpperCAmelCase__		:  int				=				torch_device

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

        UpperCAmelCase__		:  Tuple				=				pipe.to(snake_case__				)
        pipe.set_progress_bar_config(disable=snake_case__				)

        def callback(snake_case__	:     Union[str, Any]				,       snake_case__	:     Any				):
             # decode mel to audio
             UpperCAmelCase__		:  Any				=				melgan(input_features=mel_output.astype(np.floataa				)				)[0]
             assert len(audio[0]				) == 8_1_9_2_0 * (step + 1)
             # simulate that audio is played
             return audio

        UpperCAmelCase__		:  str				=				MidiProcessor()
        UpperCAmelCase__		:  Any				=				processor(snake_case__				)

        UpperCAmelCase__		:  Dict				=				input_tokens[:3]
        UpperCAmelCase__		:  List[Any]				=				torch.manual_seed(0				)
        pipe(snake_case__				,       num_inference_steps=5				,       generator=snake_case__				,       callback=snake_case__				,       output_type="mel"				)
   def 	__a				(						self	:     str				):


        '''simple docstring'''
        UpperCAmelCase__		:  Optional[Any]				=				torch_device

        UpperCAmelCase__		:  int				=				SpectrogramDiffusionPipeline.from_pretrained("google/music-spectrogram-diffusion"				)
        UpperCAmelCase__		:  List[str]				=				pipe.to(snake_case__				)
        pipe.set_progress_bar_config(disable=snake_case__				)
        UpperCAmelCase__		:  Tuple				=				MidiProcessor()

        UpperCAmelCase__		:  Optional[Any]				=				processor(snake_case__				)
        # just run two denoising loops
        UpperCAmelCase__		:  Optional[int]				=				input_tokens[:2]

        UpperCAmelCase__		:  Optional[Any]				=				torch.manual_seed(0				)
        UpperCAmelCase__		:  Dict				=				pipe(snake_case__				,       num_inference_steps=2				,       generator=snake_case__				)

        UpperCAmelCase__		:  str				=				output.audios[0]

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




   def 	__a				(						self	:     Union[str, Any]				):


        '''simple docstring'''
        UpperCAmelCase__		:  Tuple				=				torch_device

        UpperCAmelCase__		:  Optional[int]				=				SpectrogramDiffusionPipeline.from_pretrained("google/music-spectrogram-diffusion"				)
        UpperCAmelCase__		:  Optional[int]				=				pipe.to(snake_case__				)
        pipe.set_progress_bar_config(disable=snake_case__				)

        UpperCAmelCase__		:  Optional[int]				=				MidiProcessor()

        UpperCAmelCase__		:  List[str]				=				processor(snake_case__				)

        # just run 4 denoising loops
        UpperCAmelCase__		:  Tuple				=				input_tokens[:4]

        UpperCAmelCase__		:  Optional[int]				=				torch.manual_seed(0				)
        UpperCAmelCase__		:  int				=				pipe(snake_case__				,       num_inference_steps=1_0_0				,       generator=snake_case__				)

        UpperCAmelCase__		:  Dict				=				output.audios[0]
        assert abs(np.abs(snake_case__				).sum() - 9389.1111				) < 5e-2




'''simple docstring'''
import argparse
import logging
import math
import time

import torch

from transformers import TransfoXLCorpus, TransfoXLLMHeadModel


logging.basicConfig(
    format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO
)
lowerCAmelCase :  Optional[Any]      =      logging.getLogger(__name__)







def 						A_(			):
							UpperCamelCase					       =			argparse.ArgumentParser(description='PyTorch Transformer Language Model')
							parser.add_argument('--model_name'						,			type=A						,			default='transfo-xl-wt103'						,			help='pretrained model name')
							parser.add_argument(
							    '--split'						,			type=A						,			default='test'						,			choices=['all', 'valid', 'test']						,			help='which split to evaluate')
							parser.add_argument('--batch_size'						,			type=A						,			default=10						,			help='batch size')
							parser.add_argument('--tgt_len'						,			type=A						,			default=128						,			help='number of tokens to predict')
							parser.add_argument('--ext_len'						,			type=A						,			default=0						,			help='length of the extended context')
							parser.add_argument('--mem_len'						,			type=A						,			default=1600						,			help='length of the retained previous heads')
							parser.add_argument('--clamp_len'						,			type=A						,			default=1000						,			help='max positional embedding index')
							parser.add_argument('--no_cuda'						,			action='store_true'						,			help='Do not use CUDA even though CUA is available')
							parser.add_argument('--work_dir'						,			type=A						,			required=A						,			help='path to the work_dir')
							parser.add_argument('--no_log'						,			action='store_true'						,			help='do not log the eval result')
							parser.add_argument('--same_length'						,			action='store_true'						,			help='set same length attention with masking')
							parser.add_argument('--server_ip'						,			type=A						,			default=''						,			help='Can be used for distant debugging.')
							parser.add_argument('--server_port'						,			type=A						,			default=''						,			help='Can be used for distant debugging.')
							UpperCamelCase					       =			parser.parse_args()
							assert args.ext_len >= 0, "extended context length must be non-negative"

							if args.server_ip and args.server_port:
														# Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script
														import ptvsd

														print('Waiting for debugger attach')
														ptvsd.enable_attach(address=(args.server_ip, args.server_port)						,			redirect_output=A)
														ptvsd.wait_for_attach()

							UpperCamelCase					       =			torch.device('cuda' if torch.cuda.is_available() and not args.no_cuda else 'cpu')
							logger.info('device: {}'.format(A))

							# Load a pre-processed dataset
							# You can also build the corpus yourself using TransfoXLCorpus methods
							# The pre-processing involve computing word frequencies to prepare the Adaptive input and SoftMax
							# and tokenizing the dataset
							# The pre-processed corpus is a convertion (using the conversion script )
							UpperCamelCase					       =			TransfoXLCorpus.from_pretrained(args.model_name)

							UpperCamelCase					       =			corpus.get_iterator('valid'						,			args.batch_size						,			args.tgt_len						,			device=A						,			ext_len=args.ext_len)
							UpperCamelCase					       =			corpus.get_iterator('test'						,			args.batch_size						,			args.tgt_len						,			device=A						,			ext_len=args.ext_len)

							# Load a pre-trained model
							UpperCamelCase					       =			TransfoXLLMHeadModel.from_pretrained(args.model_name)
							model.to(A)

							logger.info(
							    'Evaluating with bsz {} tgt_len {} ext_len {} mem_len {} clamp_len {}'.format(
							        args.batch_size						,			args.tgt_len						,			args.ext_len						,			args.mem_len						,			args.clamp_len))

							model.reset_memory_length(args.mem_len)
							if args.clamp_len > 0:
														UpperCamelCase					       =			args.clamp_len
							if args.same_length:
														UpperCamelCase					       =			True

							###############################################################################
							# Evaluation code
							###############################################################################
							def evaluate(A		:						Dict):
														# Turn on evaluation mode which disables dropout.
														model.eval()
														UpperCamelCase       ,     UpperCamelCase					       =			0, 0.0
														UpperCamelCase					       =			time.time()
														with torch.no_grad():
																					UpperCamelCase					       =			None
																					for idx, (data, target, seq_len) in enumerate(A):
																												UpperCamelCase					       =			model(A						,			lm_labels=A						,			mems=A)
																												UpperCamelCase       ,     UpperCamelCase       ,     UpperCamelCase					       =			ret
																												UpperCamelCase					       =			loss.mean()
																												total_loss += seq_len * loss.item()
																												total_len += seq_len
																					UpperCamelCase					       =			time.time() - start_time
														logger.info('Time : {:.2f}s, {:.2f}ms/segment'.format(A						,			1000 * total_time / (idx + 1)))
														return total_loss / total_len

							# Run on test data.
							if args.split == "all":
														UpperCamelCase					       =			evaluate(A)
														UpperCamelCase					       =			evaluate(A)
							elif args.split == "valid":
														UpperCamelCase					       =			evaluate(A)
														UpperCamelCase					       =			None
							elif args.split == "test":
														UpperCamelCase					       =			evaluate(A)
														UpperCamelCase					       =			None

							def format_log(A		:						Any						,			A		:						Optional[Any]):
														UpperCamelCase					       =			'| {0} loss {1:5.2f} | {0} ppl {2:9.3f} '.format(A						,			A						,			math.exp(A))
														return log_str

							UpperCamelCase					       =			''
							if valid_loss is not None:
														log_str += format_log(A						,			'valid')
							if test_loss is not None:
														log_str += format_log(A						,			'test')

							logger.info('=' * 100)
							logger.info(A)
							logger.info('=' * 100)


if __name__ == "__main__":
						main()




