
import unittest

from transformers import MarkupLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    MarkupLMForQuestionAnswering,
					    MarkupLMForSequenceClassification,
					    MarkupLMForTokenClassification,
					    MarkupLMModel,
					)

# TODO check dependencies
from transformers import MarkupLMFeatureExtractor, MarkupLMProcessor, MarkupLMTokenizer


class 							lowercase   :



		def __init__(					self							, A_							, A_=13							, A_=7							, A_=True							, A_=True							, A_=True							, A_=True							, A_=99							, A_=32							, A_=5							, A_=4							, A_=37							, A_="gelu"							, A_=0.1							, A_=0.1							, A_=512							, A_=16							, A_=2							, A_=0.02							, A_=3							, A_=None							, A_=20							, A_=30							, A_=2							, A_=2							, A_=10							, )    ->   int:
				"""simple docstring"""
				UpperCamelCase            =	parent
				UpperCamelCase            =	batch_size
				UpperCamelCase            =	seq_length
				UpperCamelCase            =	is_training
				UpperCamelCase            =	use_input_mask
				UpperCamelCase            =	use_token_type_ids
				UpperCamelCase            =	use_labels
				UpperCamelCase            =	vocab_size
				UpperCamelCase            =	hidden_size
				UpperCamelCase            =	num_hidden_layers
				UpperCamelCase            =	num_attention_heads
				UpperCamelCase            =	intermediate_size
				UpperCamelCase            =	hidden_act
				UpperCamelCase            =	hidden_dropout_prob
				UpperCamelCase            =	attention_probs_dropout_prob
				UpperCamelCase            =	max_position_embeddings
				UpperCamelCase            =	type_vocab_size
				UpperCamelCase            =	type_sequence_label_size
				UpperCamelCase            =	initializer_range
				UpperCamelCase            =	num_labels
				UpperCamelCase            =	scope
				UpperCamelCase            =	max_xpath_tag_unit_embeddings
				UpperCamelCase            =	max_xpath_subs_unit_embeddings
				UpperCamelCase            =	tag_pad_id
				UpperCamelCase            =	subs_pad_id
				UpperCamelCase            =	max_depth



		def __UpperCamelCase					(					self			)    ->   List[Any]:
				"""simple docstring"""
				UpperCamelCase            =	ids_tensor([self.batch_size, self.seq_length]							, self.vocab_size			)

				UpperCamelCase            =	ids_tensor(
				    [self.batch_size, self.seq_length, self.max_depth]							, self.max_xpath_tag_unit_embeddings			)

				UpperCamelCase            =	ids_tensor(
				    [self.batch_size, self.seq_length, self.max_depth]							, self.max_xpath_subs_unit_embeddings			)

				UpperCamelCase            =	None
				if self.use_input_mask:
						UpperCamelCase            =	ids_tensor([self.batch_size, self.seq_length]							, vocab_size=2			)

				UpperCamelCase            =	None
				if self.use_token_type_ids:
						UpperCamelCase            =	ids_tensor([self.batch_size, self.seq_length]							, self.type_vocab_size			)

				UpperCamelCase            =	None
				UpperCamelCase            =	None
				if self.use_labels:
						UpperCamelCase            =	ids_tensor([self.batch_size]							, self.type_sequence_label_size			)
						UpperCamelCase            =	ids_tensor([self.batch_size, self.seq_length]							, self.num_labels			)

				UpperCamelCase            =	self.get_config()

				return (
				    config,
				    input_ids,
				    xpath_tags_seq,
				    xpath_subs_seq,
				    token_type_ids,
				    input_mask,
				    sequence_labels,
				    token_labels,
				)



		def __UpperCamelCase					(					self			)    ->   Optional[Any]:
				"""simple docstring"""
				return MarkupLMConfig(
				    vocab_size=self.vocab_size							, hidden_size=self.hidden_size							, num_hidden_layers=self.num_hidden_layers							, num_attention_heads=self.num_attention_heads							, intermediate_size=self.intermediate_size							, hidden_act=self.hidden_act							, hidden_dropout_prob=self.hidden_dropout_prob							, attention_probs_dropout_prob=self.attention_probs_dropout_prob							, max_position_embeddings=self.max_position_embeddings							, type_vocab_size=self.type_vocab_size							, initializer_range=self.initializer_range							, max_xpath_tag_unit_embeddings=self.max_xpath_tag_unit_embeddings							, max_xpath_subs_unit_embeddings=self.max_xpath_subs_unit_embeddings							, tag_pad_id=self.tag_pad_id							, subs_pad_id=self.subs_pad_id							, max_depth=self.max_depth							, )



		def __UpperCamelCase					(					self							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, )    ->   Optional[Any]:
				"""simple docstring"""
				UpperCamelCase            =	MarkupLMModel(config=__lowerCamelCase			)
				model.to(__lowerCamelCase			)
				model.eval()
				print('Configs:'							, model.config.tag_pad_id							, model.config.subs_pad_id			)
				UpperCamelCase            =	model(__lowerCamelCase							, attention_mask=__lowerCamelCase							, token_type_ids=__lowerCamelCase			)
				UpperCamelCase            =	model(__lowerCamelCase							, token_type_ids=__lowerCamelCase			)
				UpperCamelCase            =	model(__lowerCamelCase			)
				self.parent.assertEqual(result.last_hidden_state.shape							, (self.batch_size, self.seq_length, self.hidden_size)			)
				self.parent.assertEqual(result.pooler_output.shape							, (self.batch_size, self.hidden_size)			)



		def __UpperCamelCase					(					self							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, )    ->   int:
				"""simple docstring"""
				UpperCamelCase            =	self.num_labels
				UpperCamelCase            =	MarkupLMForSequenceClassification(__lowerCamelCase			)
				model.to(__lowerCamelCase			)
				model.eval()
				UpperCamelCase            =	model(
				    __lowerCamelCase							, xpath_tags_seq=__lowerCamelCase							, xpath_subs_seq=__lowerCamelCase							, attention_mask=__lowerCamelCase							, token_type_ids=__lowerCamelCase							, labels=__lowerCamelCase							, )
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_labels)			)



		def __UpperCamelCase					(					self							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, )    ->   Dict:
				"""simple docstring"""
				UpperCamelCase            =	self.num_labels
				UpperCamelCase            =	MarkupLMForTokenClassification(config=__lowerCamelCase			)
				model.to(__lowerCamelCase			)
				model.eval()
				UpperCamelCase            =	model(
				    __lowerCamelCase							, xpath_tags_seq=__lowerCamelCase							, xpath_subs_seq=__lowerCamelCase							, attention_mask=__lowerCamelCase							, token_type_ids=__lowerCamelCase							, labels=__lowerCamelCase							, )
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.seq_length, self.num_labels)			)



		def __UpperCamelCase					(					self							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, )    ->   str:
				"""simple docstring"""
				UpperCamelCase            =	MarkupLMForQuestionAnswering(config=__lowerCamelCase			)
				model.to(__lowerCamelCase			)
				model.eval()
				UpperCamelCase            =	model(
				    __lowerCamelCase							, xpath_tags_seq=__lowerCamelCase							, xpath_subs_seq=__lowerCamelCase							, attention_mask=__lowerCamelCase							, token_type_ids=__lowerCamelCase							, start_positions=__lowerCamelCase							, end_positions=__lowerCamelCase							, )
				self.parent.assertEqual(result.start_logits.shape							, (self.batch_size, self.seq_length)			)
				self.parent.assertEqual(result.end_logits.shape							, (self.batch_size, self.seq_length)			)



		def __UpperCamelCase					(					self			)    ->   Optional[int]:
				"""simple docstring"""
				UpperCamelCase            =	self.prepare_config_and_inputs()
				(
				    (
				    UpperCamelCase						
				)      ,    (
				    UpperCamelCase						
				)      ,    (
				    UpperCamelCase						
				)      ,    (
				    UpperCamelCase						
				)      ,    (
				    UpperCamelCase						
				)      ,    (
				    UpperCamelCase						
				)      ,    (
				    UpperCamelCase						
				)      ,    (
				    UpperCamelCase						
				)      ,    						
				)            =	config_and_inputs
				UpperCamelCase            =	{
				    'input_ids': input_ids,
				    'xpath_tags_seq': xpath_tags_seq,
				    'xpath_subs_seq': xpath_subs_seq,
				    'token_type_ids': token_type_ids,
				    'attention_mask': input_mask,
				}
				return config, inputs_dict






@require_torch
class 							lowercase   (						_UpperCamelCase    ,  _UpperCamelCase    ,  unittest.TestCase  ):
		__lowercase			:			List[Any]      	=							(
		    (
		        MarkupLMModel,
		        MarkupLMForSequenceClassification,
		        MarkupLMForTokenClassification,
		        MarkupLMForQuestionAnswering,
		    )
		    if is_torch_available()
		    else None
		)
		__lowercase			:			List[Any]      	=							(
		    {
		        """feature-extraction""": MarkupLMModel,
		        """question-answering""": MarkupLMForQuestionAnswering,
		        """text-classification""": MarkupLMForSequenceClassification,
		        """token-classification""": MarkupLMForTokenClassification,
		        """zero-shot""": MarkupLMForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)



		def __UpperCamelCase					(					self							, A_							, A_							, A_							, A_							, A_			)    ->   List[Any]:
				"""simple docstring"""
				# ValueError: Nodes must be of type `List[str]` (single pretokenized example), or `List[List[str]]`
				# (batch of pretokenized examples).
				return True



		def __UpperCamelCase					(					self			)    ->   List[str]:
				"""simple docstring"""
				UpperCamelCase            =	MarkupLMModelTester(self			)
				UpperCamelCase            =	ConfigTester(self							, config_class=__lowerCamelCase							, hidden_size=37			)



		def __UpperCamelCase					(					self			)    ->   Tuple:
				"""simple docstring"""
				self.config_tester.run_common_tests()



		def __UpperCamelCase					(					self			)    ->   List[Any]:
				"""simple docstring"""
				UpperCamelCase            =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*__lowerCamelCase			)



		def __UpperCamelCase					(					self			)    ->   Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase            =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase			)



		def __UpperCamelCase					(					self			)    ->   Tuple:
				"""simple docstring"""
				UpperCamelCase            =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase			)



		def __UpperCamelCase					(					self			)    ->   str:
				"""simple docstring"""
				UpperCamelCase            =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase			)






def  A			(   )  ->    List[str]:
		'''simple docstring'''

		UpperCamelCase            =	'\n    <!DOCTYPE html>\n    <html>\n    <head>\n    <title>Page Title</title>\n    </head>\n    <body>\n\n    <h1>This is a Heading</h1>\n    <p>This is a paragraph.</p>\n\n    </body>\n    </html>\n    '

		return html_string


@require_torch
class 							lowercase   (						unittest.TestCase  ):



		@cached_property
		def __UpperCamelCase					(					self			)    ->   Dict:
				"""simple docstring"""
				# TODO use from_pretrained here
				UpperCamelCase            =	MarkupLMFeatureExtractor()
				UpperCamelCase            =	MarkupLMTokenizer.from_pretrained('microsoft/markuplm-base'			)

				return MarkupLMProcessor(__lowerCamelCase							, __lowerCamelCase			)



		@slow
		def __UpperCamelCase					(					self			)    ->   Tuple:
				"""simple docstring"""
				UpperCamelCase            =	MarkupLMModel.from_pretrained('microsoft/markuplm-base'			).to(__lowerCamelCase			)

				UpperCamelCase            =	self.default_processor

				UpperCamelCase            =	processor(prepare_html_string()							, return_tensors='pt'			)
				UpperCamelCase            =	inputs.to(__lowerCamelCase			)

				# forward pass
				with torch.no_grad():
						UpperCamelCase            =	model(**__lowerCamelCase			)

				# verify the last hidden states
				UpperCamelCase            =	torch.Size([1, 14, 768]			)
				self.assertEqual(outputs.last_hidden_state.shape							, __lowerCamelCase			)

				UpperCamelCase            =	torch.tensor(
				    [[0.0675, -0.0052, 0.5001], [-0.2281, 0.0802, 0.2192], [-0.0583, -0.3311, 0.1185]]			).to(__lowerCamelCase			)

				self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]							, __lowerCamelCase							, atol=1e-4			)			)






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						:							Optional[int]			     =						'./tests/fixtures/elise_format0.mid'




@unittest.skip("""The note-seq package currently throws an error on import"""  )
class        snake_case__				(_UpperCamelCase       ,			unittest.TestCase  ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_			:				Tuple									=   SpectrogramDiffusionPipeline
				SCREAMING_SNAKE_CASE_			:				Dict									=   PipelineTesterMixin.required_optional_params - {
				    """callback""",
				    """latents""",
				    """callback_steps""",
				    """output_type""",
				    """num_images_per_prompt""",
				}
				SCREAMING_SNAKE_CASE_			:				int									=   False
				SCREAMING_SNAKE_CASE_			:				Tuple									=   False
				SCREAMING_SNAKE_CASE_			:				str									=   TOKENS_TO_AUDIO_GENERATION_PARAMS
				SCREAMING_SNAKE_CASE_			:				List[str]									=   TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS





				def 				__UpperCAmelCase				( self							:	Dict				)   ->	str:
							torch.manual_seed(0				)
							a       =     SpectrogramNotesEncoder(
							    max_length=20_48   , vocab_size=15_36   , d_model=7_68   , dropout_rate=0.1   , num_layers=1   , num_heads=1   , d_kv=4   , d_ff=20_48   , feed_forward_proj="gated-gelu"   , )

							a       =     SpectrogramContEncoder(
							    input_dims=1_28   , targets_context_length=2_56   , d_model=7_68   , dropout_rate=0.1   , num_layers=1   , num_heads=1   , d_kv=4   , d_ff=20_48   , feed_forward_proj="gated-gelu"   , )

							a       =     TaFilmDecoder(
							    input_dims=1_28   , targets_length=2_56   , max_decoder_noise_time=20_000.0   , d_model=7_68   , num_layers=1   , num_heads=1   , d_kv=4   , d_ff=20_48   , 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							:	Any   , __lowerCamelCase							:	Optional[Any]   , __lowerCamelCase							:	Union[str, Any]=0				)   ->	Dict:
							if str(__lowerCamelCase				).startswith("mps"				):
										a       =     torch.manual_seed(__lowerCamelCase				)
							else:
										a       =     torch.Generator(device=__lowerCamelCase				).manual_seed(__lowerCamelCase				)
							a       =     {
							    "input_tokens": [
							        [11_34, 90, 11_35, 11_33, 10_80, 1_12, 11_32, 10_80, 11_33, 10_79, 1_33, 11_32, 10_79, 11_33, 1] + [0] * 20_33
							    ],
							    "generator": generator,
							    "num_inference_steps": 4,
							    "output_type": "mel",
							}
							return inputs





				def 				__UpperCAmelCase				( self							:	Dict				)   ->	Union[str, Any]:
							a       =     "cpu"  # ensure determinism for the device-dependent torch.Generator
							a       =     self.get_dummy_components()
							a       =     SpectrogramDiffusionPipeline(**__lowerCamelCase				)
							a       =     pipe.to(__lowerCamelCase				)
							pipe.set_progress_bar_config(disable=__lowerCamelCase				)

							a       =     self.get_dummy_inputs(__lowerCamelCase				)
							a       =     pipe(**__lowerCamelCase				)
							a       =     output.audios

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

							assert mel_slice.shape == (3, 3)
							a       =     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							:	List[Any]				)   ->	Tuple:
							return super().test_save_load_local()





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





				@skip_mps
				def 				__UpperCAmelCase				( self							:	int				)   ->	Any:
							return super().test_save_load_optional_components()





				@skip_mps
				def 				__UpperCAmelCase				( self							:	List[Any]				)   ->	List[str]:
							return super().test_attention_slicing_forward_pass()





				def 				__UpperCAmelCase				( self							:	Union[str, Any]				)   ->	str:
							pass





				def 				__UpperCAmelCase				( self							:	Tuple				)   ->	Tuple:
							pass





				@skip_mps
				def 				__UpperCAmelCase				( self							:	int				)   ->	Optional[Any]:
							return super().test_progress_bar()




@slow
@require_torch_gpu
@require_onnxruntime
@require_note_seq
class        snake_case__				(unittest.TestCase  ):
				"""simple docstring"""



				def 				__UpperCAmelCase				( self							:	Union[str, Any]				)   ->	Dict:
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()





				def 				__UpperCAmelCase				( self							:	Union[str, Any]				)   ->	Any:
							# 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(__lowerCamelCase				)
							pipe.set_progress_bar_config(disable=__lowerCamelCase				)

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

							a       =     MidiProcessor()
							a       =     processor(__lowerCamelCase				)

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





				def 				__UpperCAmelCase				( self							:	Any				)   ->	Union[str, Any]:
							a       =     torch_device

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

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

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

							a       =     output.audios[0]

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





				def 				__UpperCAmelCase				( self							:	List[str]				)   ->	str:
							a       =     torch_device

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

							a       =     MidiProcessor()

							a       =     processor(__lowerCamelCase				)

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

							a       =     torch.manual_seed(0				)
							a       =     pipe(__lowerCamelCase   , num_inference_steps=1_00   , generator=__lowerCamelCase				)

							a       =     output.audios[0]
							assert abs(np.abs(__lowerCamelCase				).sum() - 9_389.1_111				) < 5e-2






