






"""simple docstring"""
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_       :





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

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





	def      _SCREAMING_SNAKE_CASE      (      self					: str						)						->			int:
				UpperCAmelCase_     : List[Any]			     =					floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]						)

				UpperCAmelCase_     : Union[str, Any]			     =					None
				if self.use_labels:
							UpperCAmelCase_     : Union[str, Any]			     =					ids_tensor([self.batch_size]	,	self.type_sequence_label_size						)

				UpperCAmelCase_     : List[str]			     =					self.get_config()

				return config, input_values, labels





	def      _SCREAMING_SNAKE_CASE      (      self					: Any						)						->			Any:
				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=lowerCAmelCase_	,	initializer_range=self.initializer_range	,	frequency_stride=self.frequency_stride	,	time_stride=self.time_stride	,	)





	def      _SCREAMING_SNAKE_CASE      (      self					: Optional[Any]	,	lowerCAmelCase_					: str	,	lowerCAmelCase_					: Union[str, Any]	,	lowerCAmelCase_					: List[str]						)						->			Optional[int]:
				UpperCAmelCase_     : Union[str, Any]			     =					ASTModel(config=lowerCAmelCase_						)
				model.to(lowerCAmelCase_						)
				model.eval()
				UpperCAmelCase_     : Dict			     =					model(lowerCAmelCase_						)
				self.parent.assertEqual(result.last_hidden_state.shape	,	(self.batch_size, self.seq_length, self.hidden_size)						)





	def      _SCREAMING_SNAKE_CASE      (      self					: Any						)						->			Optional[Any]:
				UpperCAmelCase_     : List[str]			     =					self.prepare_config_and_inputs()
				(
				    (
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	
				)     : Optional[Any]			     =					config_and_inputs
				UpperCAmelCase_     : int			     =					{"input_values": input_values}
				return config, inputs_dict







@require_torch
class 			UpperCamelCase_       (__A				,						__A				,						unittest.TestCase     ):
	__magic_name__	      =				(
	    (
	        ASTModel,
	        ASTForAudioClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	__magic_name__	      =				(
	    {'''audio-classification''': ASTForAudioClassification, '''feature-extraction''': ASTModel}
	    if is_torch_available()
	    else {}
	)
	__magic_name__	      =				False
	__magic_name__	      =				False
	__magic_name__	      =				False
	__magic_name__	      =				False





	def      _SCREAMING_SNAKE_CASE      (      self					: List[str]	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: Optional[int]	,	lowerCAmelCase_					: Optional[int]	,	lowerCAmelCase_					: List[str]	,	lowerCAmelCase_					: str						)						->			List[str]:
				if pipeline_test_casse_name == "AudioClassificationPipelineTests":
							return True

				return False





	def      _SCREAMING_SNAKE_CASE      (      self					: List[Any]						)						->			int:
				UpperCAmelCase_     : Optional[Any]			     =					ASTModelTester(self						)
				UpperCAmelCase_     : Union[str, Any]			     =					ConfigTester(self	,	config_class=lowerCAmelCase_	,	has_text_modality=lowerCAmelCase_	,	hidden_size=37						)





	def      _SCREAMING_SNAKE_CASE      (      self					: List[str]						)						->			Union[str, Any]:
				self.config_tester.run_common_tests()





	@unittest.skip(reason="AST does not use inputs_embeds"						)
	def      _SCREAMING_SNAKE_CASE      (      self					: Tuple						)						->			Optional[Any]:
				pass





	def      _SCREAMING_SNAKE_CASE      (      self					: Union[str, Any]						)						->			Optional[int]:
				UpperCAmelCase_     ,	UpperCAmelCase_     : Optional[Any]			     =					self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							UpperCAmelCase_     : Tuple			     =					model_class(lowerCAmelCase_						)
							self.assertIsInstance(model.get_input_embeddings()	,	(nn.Module)						)
							UpperCAmelCase_     : Union[str, Any]			     =					model.get_output_embeddings()
							self.assertTrue(x is None or isinstance(lowerCAmelCase_	,	nn.Linear						)						)





	def      _SCREAMING_SNAKE_CASE      (      self					: Union[str, Any]						)						->			Any:
				UpperCAmelCase_     ,	UpperCAmelCase_     : Union[str, Any]			     =					self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							UpperCAmelCase_     : str			     =					model_class(lowerCAmelCase_						)
							UpperCAmelCase_     : Union[str, Any]			     =					inspect.signature(model.forward						)
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							UpperCAmelCase_     : List[Any]			     =					[*signature.parameters.keys()]

							UpperCAmelCase_     : Optional[int]			     =					["input_values"]
							self.assertListEqual(arg_names[:1]	,	lowerCAmelCase_						)





	def      _SCREAMING_SNAKE_CASE      (      self					: Union[str, Any]						)						->			Optional[Any]:
				UpperCAmelCase_     : List[str]			     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowerCAmelCase_						)





	@slow
	def      _SCREAMING_SNAKE_CASE      (      self					: Optional[Any]						)						->			int:
				for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase_     : int			     =					ASTModel.from_pretrained(lowerCAmelCase_						)
							self.assertIsNotNone(lowerCAmelCase_						)





def 				snake_case      (   ):
			UpperCAmelCase_     : List[Any]			     =					hf_hub_download(
			    repo_id="nielsr/audio-spectogram-transformer-checkpoint"	,filename="sample_audio.flac"	,repo_type="dataset"						)

			UpperCAmelCase_     ,	UpperCAmelCase_     : Tuple			     =					torchaudio.load(A__						)

			return audio, sampling_rate







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





	@cached_property
	def      _SCREAMING_SNAKE_CASE      (      self					: Dict						)						->			Tuple:
				return (
				    ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"						)
				    if is_torchaudio_available()
				    else None
				)





	@slow
	def      _SCREAMING_SNAKE_CASE      (      self					: Union[str, Any]						)						->			Optional[int]:
				UpperCAmelCase_     : List[Any]			     =					self.default_feature_extractor
				UpperCAmelCase_     : Dict			     =					ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"						).to(lowerCAmelCase_						)

				UpperCAmelCase_     : Tuple			     =					self.default_feature_extractor
				UpperCAmelCase_     ,	UpperCAmelCase_     : List[str]			     =					prepare_audio()
				UpperCAmelCase_     : Any			     =					audio.squeeze().numpy()
				UpperCAmelCase_     : int			     =					feature_extractor(lowerCAmelCase_	,	sampling_rate=lowerCAmelCase_	,	return_tensors="pt"						).to(lowerCAmelCase_						)

				# forward pass
				with torch.no_grad():
							UpperCAmelCase_     : Union[str, Any]			     =					model(**lowerCAmelCase_						)

				# verify the logits
				UpperCAmelCase_     : Optional[Any]			     =					torch.Size((1, 527)						)
				self.assertEqual(outputs.logits.shape	,	lowerCAmelCase_						)

				UpperCAmelCase_     : str			     =					torch.tensor([-0.8_7_6_0, -7.0_0_4_2, -8.6_6_0_2]						).to(lowerCAmelCase_						)

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











"""simple docstring"""
from torch import nn

def 				snake_case      (   A__						):
			if act_fn in ["swish", "silu"]:
						return nn.SiLU()
			elif act_fn == "mish":
						return nn.Mish()
			elif act_fn == "gelu":
						return nn.GELU()
			else:
						raise ValueError(F"""Unsupported activation function: {act_fn}"""						)



