





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 	A_       :
	def __init__(    self							,					_A							,					_A=1_3							,					_A=2							,					_A=2_4							,					_A=1_6							,					_A=True							,					_A=True							,					_A=3_2							,					_A=5							,					_A=4							,					_A=3_7							,					_A="gelu"							,					_A=0.1							,					_A=0.1							,					_A=1_0							,					_A=0.02							,					_A=None							,					_A=2							,					_A=2							,					):






				'''simple docstring'''







				UpperCAmelCase   		=				parent
				UpperCAmelCase   		=				batch_size
				UpperCAmelCase   		=				patch_size
				UpperCAmelCase   		=				max_length
				UpperCAmelCase   		=				num_mel_bins
				UpperCAmelCase   		=				is_training
				UpperCAmelCase   		=				use_labels
				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   		=				type_sequence_label_size
				UpperCAmelCase   		=				initializer_range
				UpperCAmelCase   		=				scope
				UpperCAmelCase   		=				frequency_stride
				UpperCAmelCase   		=				time_stride

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






				'''simple docstring'''







				UpperCAmelCase   		=				floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]		)

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

				UpperCAmelCase   		=				self.get_config()

				return config, input_values, labels
	def      _lowercase				(    self		):






				'''simple docstring'''







				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=_A							,					initializer_range=self.initializer_range							,					frequency_stride=self.frequency_stride							,					time_stride=self.time_stride							,					)
	def      _lowercase				(    self							,					_A							,					_A							,					_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				ASTModel(config=_A		)
				model.to(_A		)
				model.eval()
				UpperCAmelCase   		=				model(_A		)
				self.parent.assertEqual(result.last_hidden_state.shape							,					(self.batch_size, self.seq_length, self.hidden_size)		)







	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.prepare_config_and_inputs()
				(
				    (
				    UpperCAmelCase
				)		,			(
				    UpperCAmelCase
				)		,			(
				    UpperCAmelCase
				)		,			
				)   		=				config_and_inputs
				UpperCAmelCase   		=				{'''input_values''': input_values}
				return config, inputs_dict

@require_torch
class 	A_       (a_     ,							a_     ,							unittest.TestCase			):
	UpperCAmelCase__					=			(
	    (
	        ASTModel,
	        ASTForAudioClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	UpperCAmelCase__					=			(
	    {'''audio-classification''': ASTForAudioClassification, '''feature-extraction''': ASTModel}
	    if is_torch_available()
	    else {}
	)
	UpperCAmelCase__					=			False
	UpperCAmelCase__					=			False
	UpperCAmelCase__					=			False
	UpperCAmelCase__					=			False
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					_A							,					_A		):






				'''simple docstring'''







				if pipeline_test_casse_name == "AudioClassificationPipelineTests":
							return True

				return False
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				ASTModelTester(self		)
				UpperCAmelCase   		=				ConfigTester(self							,					config_class=_A							,					has_text_modality=_A							,					hidden_size=3_7		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				self.config_tester.run_common_tests()
	@unittest.skip(reason='''AST does not use inputs_embeds'''		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				pass
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase		,			UpperCAmelCase   		=				self.model_tester.prepare_config_and_inputs_for_common()

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






				'''simple docstring'''







				UpperCAmelCase		,			UpperCAmelCase   		=				self.model_tester.prepare_config_and_inputs_for_common()

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

							UpperCAmelCase   		=				['''input_values''']
							self.assertListEqual(arg_names[:1]							,					_A		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_A		)







	@slow
	def      _lowercase				(    self		):






				'''simple docstring'''







				for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase   		=				ASTModel.from_pretrained(_A		)
							self.assertIsNotNone(_A		)





def 						__SCREAMING_SNAKE_CASE  (				)  ->				List[str]:







			'''simple docstring'''

			UpperCAmelCase   		=				hf_hub_download(
			    repo_id='''nielsr/audio-spectogram-transformer-checkpoint'''			,			filename='''sample_audio.flac'''			,			repo_type='''dataset''' )

			UpperCAmelCase		,			UpperCAmelCase   		=				torchaudio.load(UpperCamelCase__ )

			return audio, sampling_rate






@require_torch
@require_torchaudio
class 	A_       (unittest.TestCase			):
	@cached_property
	def      _lowercase				(    self		):






				'''simple docstring'''







				return (
				    ASTFeatureExtractor.from_pretrained('''MIT/ast-finetuned-audioset-10-10-0.4593'''		)
				    if is_torchaudio_available()
				    else None
				)







	@slow
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.default_feature_extractor
				UpperCAmelCase   		=				ASTForAudioClassification.from_pretrained('''MIT/ast-finetuned-audioset-10-10-0.4593'''		).to(_A		)

				UpperCAmelCase   		=				self.default_feature_extractor
				UpperCAmelCase		,			UpperCAmelCase   		=				prepare_audio()
				UpperCAmelCase   		=				audio.squeeze().numpy()
				UpperCAmelCase   		=				feature_extractor(_A							,					sampling_rate=_A							,					return_tensors='''pt'''		).to(_A		)

				# forward pass
				with torch.no_grad():
							UpperCAmelCase   		=				model(**_A		)

				# verify the logits
				UpperCAmelCase   		=				torch.Size((1, 5_2_7)		)
				self.assertEqual(outputs.logits.shape							,					_A		)

				UpperCAmelCase   		=				torch.tensor([-0.87_60, -7.00_42, -8.66_02]		).to(_A		)

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










from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__A				:    Dict       =   {
    "configuration_roformer": ["ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "RoFormerConfig", "RoFormerOnnxConfig"],
    "tokenization_roformer": ["RoFormerTokenizer"],
}

try:
				if not is_tokenizers_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A				:    Union[str, Any]       =   ["RoFormerTokenizerFast"]

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A				:    str       =   [
				    "ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST",
				    "RoFormerForCausalLM",
				    "RoFormerForMaskedLM",
				    "RoFormerForMultipleChoice",
				    "RoFormerForQuestionAnswering",
				    "RoFormerForSequenceClassification",
				    "RoFormerForTokenClassification",
				    "RoFormerLayer",
				    "RoFormerModel",
				    "RoFormerPreTrainedModel",
				    "load_tf_weights_in_roformer",
				]


try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A				:    int       =   [
				    "TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST",
				    "TFRoFormerForCausalLM",
				    "TFRoFormerForMaskedLM",
				    "TFRoFormerForMultipleChoice",
				    "TFRoFormerForQuestionAnswering",
				    "TFRoFormerForSequenceClassification",
				    "TFRoFormerForTokenClassification",
				    "TFRoFormerLayer",
				    "TFRoFormerModel",
				    "TFRoFormerPreTrainedModel",
				]


try:
				if not is_flax_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A				:    Any       =   [
				    "FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST",
				    "FlaxRoFormerForMaskedLM",
				    "FlaxRoFormerForMultipleChoice",
				    "FlaxRoFormerForQuestionAnswering",
				    "FlaxRoFormerForSequenceClassification",
				    "FlaxRoFormerForTokenClassification",
				    "FlaxRoFormerModel",
				    "FlaxRoFormerPreTrainedModel",
				]


if TYPE_CHECKING:
				from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig
				from .tokenization_roformer import RoFormerTokenizer

				try:
								if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_roformer_fast import RoFormerTokenizerFast

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_roformer import (
								    ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
								    RoFormerForCausalLM,
								    RoFormerForMaskedLM,
								    RoFormerForMultipleChoice,
								    RoFormerForQuestionAnswering,
								    RoFormerForSequenceClassification,
								    RoFormerForTokenClassification,
								    RoFormerLayer,
								    RoFormerModel,
								    RoFormerPreTrainedModel,
								    load_tf_weights_in_roformer,
								)

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_roformer import (
								    TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFRoFormerForCausalLM,
								    TFRoFormerForMaskedLM,
								    TFRoFormerForMultipleChoice,
								    TFRoFormerForQuestionAnswering,
								    TFRoFormerForSequenceClassification,
								    TFRoFormerForTokenClassification,
								    TFRoFormerLayer,
								    TFRoFormerModel,
								    TFRoFormerPreTrainedModel,
								)

				try:
								if not is_flax_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_flax_roformer import (
								    FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
								    FlaxRoFormerForMaskedLM,
								    FlaxRoFormerForMultipleChoice,
								    FlaxRoFormerForQuestionAnswering,
								    FlaxRoFormerForSequenceClassification,
								    FlaxRoFormerForTokenClassification,
								    FlaxRoFormerModel,
								    FlaxRoFormerPreTrainedModel,
								)


else:
				import sys

				__A				:    int       =   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



