




import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ASTConfig
from transformers.testing_utils import require_torch, require_torchaudio, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_torchaudio_available

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


if is_torch_available():
		import torch
		from torch import nn

		from transformers import ASTForAudioClassification, ASTModel
		from transformers.models.audio_spectrogram_transformer.modeling_audio_spectrogram_transformer import (
		    AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
		)


if is_torchaudio_available():
		import torchaudio

		from transformers import ASTFeatureExtractor




class 			UpperCAmelCase_      :




							'''simple docstring'''



							def __init__(					self       :	List[Any]       ,     a       :	Optional[int]       ,     a       :	List[Any]=13       ,     a       :	Optional[int]=2       ,     a       :	int=24       ,     a       :	Any=16       ,     a       :	Dict=True       ,     a       :	Dict=True       ,     a       :	Any=32       ,     a       :	Dict=5       ,     a       :	List[Any]=4       ,     a       :	Union[str, Any]=37       ,     a       :	List[Any]="gelu"       ,     a       :	Optional[Any]=0.1       ,     a       :	Any=0.1       ,     a       :	Optional[Any]=10       ,     a       :	List[str]=0.02       ,     a       :	Union[str, Any]=None       ,     a       :	Optional[Any]=2       ,     a       :	Any=2       ,     )			->					Optional[int]:
										SCREAMING_SNAKE_CASE           =					parent
										SCREAMING_SNAKE_CASE           =					batch_size
										SCREAMING_SNAKE_CASE           =					patch_size
										SCREAMING_SNAKE_CASE           =					max_length
										SCREAMING_SNAKE_CASE           =					num_mel_bins
										SCREAMING_SNAKE_CASE           =					is_training
										SCREAMING_SNAKE_CASE           =					use_labels
										SCREAMING_SNAKE_CASE           =					hidden_size
										SCREAMING_SNAKE_CASE           =					num_hidden_layers
										SCREAMING_SNAKE_CASE           =					num_attention_heads
										SCREAMING_SNAKE_CASE           =					intermediate_size
										SCREAMING_SNAKE_CASE           =					hidden_act
										SCREAMING_SNAKE_CASE           =					hidden_dropout_prob
										SCREAMING_SNAKE_CASE           =					attention_probs_dropout_prob
										SCREAMING_SNAKE_CASE           =					type_sequence_label_size
										SCREAMING_SNAKE_CASE           =					initializer_range
										SCREAMING_SNAKE_CASE           =					scope
										SCREAMING_SNAKE_CASE           =					frequency_stride
										SCREAMING_SNAKE_CASE           =					time_stride

										# in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
										SCREAMING_SNAKE_CASE           =					(self.num_mel_bins - self.patch_size) // self.frequency_stride + 1
										SCREAMING_SNAKE_CASE           =					(self.max_length - self.patch_size) // self.time_stride + 1
										SCREAMING_SNAKE_CASE           =					frequency_out_dimension * time_out_dimension
										SCREAMING_SNAKE_CASE           =					num_patches + 2





							def    _UpperCAmelCase     (					self       :	int			)			->					List[str]:
										SCREAMING_SNAKE_CASE           =					floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]			)

										SCREAMING_SNAKE_CASE           =					None
										if self.use_labels:
													SCREAMING_SNAKE_CASE           =					ids_tensor([self.batch_size]       ,     self.type_sequence_label_size			)

										SCREAMING_SNAKE_CASE           =					self.get_config()

										return config, input_values, labels





							def    _UpperCAmelCase     (					self       :	Union[str, Any]			)			->					Optional[int]:
										return ASTConfig(
										    patch_size=self.patch_size       ,     max_length=self.max_length       ,     num_mel_bins=self.num_mel_bins       ,     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       ,     is_decoder=UpperCamelCase__       ,     initializer_range=self.initializer_range       ,     frequency_stride=self.frequency_stride       ,     time_stride=self.time_stride       ,     )





							def    _UpperCAmelCase     (					self       :	Dict       ,     a       :	Optional[Any]       ,     a       :	Any       ,     a       :	Tuple			)			->					List[Any]:
										SCREAMING_SNAKE_CASE           =					ASTModel(config=UpperCamelCase__			)
										model.to(UpperCamelCase__			)
										model.eval()
										SCREAMING_SNAKE_CASE           =					model(UpperCamelCase__			)
										self.parent.assertEqual(result.last_hidden_state.shape       ,     (self.batch_size, self.seq_length, self.hidden_size)			)







							def    _UpperCAmelCase     (					self       :	int			)			->					Optional[int]:
										SCREAMING_SNAKE_CASE           =					self.prepare_config_and_inputs()
										(
										    SCREAMING_SNAKE_CASE							
										)           =					config_and_inputs
										SCREAMING_SNAKE_CASE           =					{'''input_values''': input_values}
										return config, inputs_dict





@require_torch
class 			UpperCAmelCase_      (   SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		,      unittest.TestCase       ):




							'''simple docstring'''



							a__						  =     (
							    (
							        ASTModel,
							        ASTForAudioClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							a__						  =     (
							    {'''audio-classification''': ASTForAudioClassification, '''feature-extraction''': ASTModel}
							    if is_torch_available()
							    else {}
							)
							a__						  =     False
							a__						  =     False
							a__						  =     False
							a__						  =     False





							def    _UpperCAmelCase     (					self       :	Tuple       ,     a       :	int       ,     a       :	List[Any]       ,     a       :	Union[str, Any]       ,     a       :	Union[str, Any]       ,     a       :	List[Any]			)			->					str:
										if pipeline_test_casse_name == "AudioClassificationPipelineTests":
													return True

										return False





							def    _UpperCAmelCase     (					self       :	Dict			)			->					int:
										SCREAMING_SNAKE_CASE           =					ASTModelTester(self			)
										SCREAMING_SNAKE_CASE           =					ConfigTester(self       ,     config_class=UpperCamelCase__       ,     has_text_modality=UpperCamelCase__       ,     hidden_size=37			)





							def    _UpperCAmelCase     (					self       :	Tuple			)			->					str:
										self.config_tester.run_common_tests()





							@unittest.skip(reason="""AST does not use inputs_embeds"""			)
							def    _UpperCAmelCase     (					self       :	int			)			->					List[Any]:
										pass





							def    _UpperCAmelCase     (					self       :	Union[str, Any]			)			->					Optional[Any]:
										SCREAMING_SNAKE_CASE           =					self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													SCREAMING_SNAKE_CASE           =					model_class(UpperCamelCase__			)
													self.assertIsInstance(model.get_input_embeddings()       ,     (nn.Module)			)
													SCREAMING_SNAKE_CASE           =					model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(UpperCamelCase__       ,     nn.Linear			)			)





							def    _UpperCAmelCase     (					self       :	Union[str, Any]			)			->					List[str]:
										SCREAMING_SNAKE_CASE           =					self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													SCREAMING_SNAKE_CASE           =					model_class(UpperCamelCase__			)
													SCREAMING_SNAKE_CASE           =					inspect.signature(model.forward			)
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													SCREAMING_SNAKE_CASE           =					[*signature.parameters.keys()]

													SCREAMING_SNAKE_CASE           =					['''input_values''']
													self.assertListEqual(arg_names[:1]       ,     UpperCamelCase__			)





							def    _UpperCAmelCase     (					self       :	Any			)			->					int:
										SCREAMING_SNAKE_CASE           =					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*UpperCamelCase__			)







							@slow
							def    _UpperCAmelCase     (					self       :	Union[str, Any]			)			->					int:
										for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													SCREAMING_SNAKE_CASE           =					ASTModel.from_pretrained(UpperCamelCase__			)
													self.assertIsNotNone(UpperCamelCase__			)





def 					lowerCamelCase_    (    ):
			'''simple docstring'''
			SCREAMING_SNAKE_CASE           =					hf_hub_download(
			    repo_id="""nielsr/audio-spectogram-transformer-checkpoint"""					,	filename="""sample_audio.flac"""					,	repo_type="""dataset"""   )

			SCREAMING_SNAKE_CASE           =					torchaudio.load(__UpperCamelCase   )

			return audio, sampling_rate




@require_torch
@require_torchaudio
class 			UpperCAmelCase_      (   unittest.TestCase       ):




							'''simple docstring'''



							@cached_property
							def    _UpperCAmelCase     (					self       :	Union[str, Any]			)			->					str:
										return (
										    ASTFeatureExtractor.from_pretrained("""MIT/ast-finetuned-audioset-10-10-0.4593"""			)
										    if is_torchaudio_available()
										    else None
										)







							@slow
							def    _UpperCAmelCase     (					self       :	int			)			->					str:
										SCREAMING_SNAKE_CASE           =					self.default_feature_extractor
										SCREAMING_SNAKE_CASE           =					ASTForAudioClassification.from_pretrained("""MIT/ast-finetuned-audioset-10-10-0.4593"""			).to(UpperCamelCase__			)

										SCREAMING_SNAKE_CASE           =					self.default_feature_extractor
										SCREAMING_SNAKE_CASE           =					prepare_audio()
										SCREAMING_SNAKE_CASE           =					audio.squeeze().numpy()
										SCREAMING_SNAKE_CASE           =					feature_extractor(UpperCamelCase__       ,     sampling_rate=UpperCamelCase__       ,     return_tensors="""pt"""			).to(UpperCamelCase__			)

										# forward pass
										with torch.no_grad():
													SCREAMING_SNAKE_CASE           =					model(**UpperCamelCase__			)

										# verify the logits
										SCREAMING_SNAKE_CASE           =					torch.Size((1, 527)			)
										self.assertEqual(outputs.logits.shape       ,     UpperCamelCase__			)

										SCREAMING_SNAKE_CASE           =					torch.tensor([-0.8760, -7.0042, -8.6602]			).to(UpperCamelCase__			)

										self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,     UpperCamelCase__       ,     atol=1E-4			)			)












import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
		from transformers import AutoModelForSeqaSeqLM, AutoTokenizer




@require_torch
@require_sentencepiece
@require_tokenizers
class 			UpperCAmelCase_      (   unittest.TestCase       ):




							'''simple docstring'''



							@slow
							def    _UpperCAmelCase     (					self       :	Optional[int]			)			->					Tuple:
										SCREAMING_SNAKE_CASE           =					AutoModelForSeqaSeqLM.from_pretrained("""google/mt5-small"""       ,     return_dict=a			).to(a			)
										SCREAMING_SNAKE_CASE           =					AutoTokenizer.from_pretrained("""google/mt5-small"""			)

										SCREAMING_SNAKE_CASE           =					tokenizer("""Hello there"""       ,     return_tensors="""pt"""			).input_ids
										SCREAMING_SNAKE_CASE           =					tokenizer("""Hi I am"""       ,     return_tensors="""pt"""			).input_ids

										SCREAMING_SNAKE_CASE           =					model(input_ids.to(a			)       ,     labels=labels.to(a			)			).loss
										SCREAMING_SNAKE_CASE           =					-(labels.shape[-1] * loss.item())

										SCREAMING_SNAKE_CASE           =					-84.9127
										self.assertTrue(abs(mtf_score - EXPECTED_SCORE			) < 1E-4			)






