



'''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 	a__     :


	def __init__(self : Any,					__UpperCAmelCase : Union[str, Any],					__UpperCAmelCase : Union[str, Any]=13,					__UpperCAmelCase : Optional[int]=2,					__UpperCAmelCase : int=24,					__UpperCAmelCase : List[Any]=16,					__UpperCAmelCase : str=True,					__UpperCAmelCase : Tuple=True,					__UpperCAmelCase : str=32,					__UpperCAmelCase : Optional[int]=5,					__UpperCAmelCase : Any=4,					__UpperCAmelCase : Optional[int]=37,					__UpperCAmelCase : Optional[int]="gelu",					__UpperCAmelCase : Optional[Any]=0.1,					__UpperCAmelCase : Optional[Any]=0.1,					__UpperCAmelCase : List[Any]=10,					__UpperCAmelCase : Union[str, Any]=0.02,					__UpperCAmelCase : List[str]=None,					__UpperCAmelCase : Optional[Any]=2,					__UpperCAmelCase : str=2,					)     ->			Optional[int]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						List[str]      					= parent
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= batch_size
					SCREAMING_SNAKE_CASE  :						int      					= patch_size
					SCREAMING_SNAKE_CASE  :						Tuple      					= max_length
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= num_mel_bins
					SCREAMING_SNAKE_CASE  :						List[str]      					= is_training
					SCREAMING_SNAKE_CASE  :						str      					= use_labels
					SCREAMING_SNAKE_CASE  :						List[str]      					= hidden_size
					SCREAMING_SNAKE_CASE  :						Dict      					= num_hidden_layers
					SCREAMING_SNAKE_CASE  :						List[str]      					= num_attention_heads
					SCREAMING_SNAKE_CASE  :						List[Any]      					= intermediate_size
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= hidden_act
					SCREAMING_SNAKE_CASE  :						List[str]      					= hidden_dropout_prob
					SCREAMING_SNAKE_CASE  :						int      					= attention_probs_dropout_prob
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= type_sequence_label_size
					SCREAMING_SNAKE_CASE  :						str      					= initializer_range
					SCREAMING_SNAKE_CASE  :						Tuple      					= scope
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= frequency_stride
					SCREAMING_SNAKE_CASE  :						Any      					= 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  :						List[str]      					= (self.num_mel_bins - self.patch_size) // self.frequency_stride + 1
					SCREAMING_SNAKE_CASE  :						Dict      					= (self.max_length - self.patch_size) // self.time_stride + 1
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= frequency_out_dimension * time_out_dimension
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= num_patches + 2


	def 				lowercase__  (self : Optional[Any]      )     ->			Tuple:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]      )

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

					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= self.get_config()

					return config, input_values, labels


	def 				lowercase__  (self : str      )     ->			Optional[int]:

					"""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=__UpperCAmelCase,					initializer_range=self.initializer_range,					frequency_stride=self.frequency_stride,					time_stride=self.time_stride,					)


	def 				lowercase__  (self : Optional[int],					__UpperCAmelCase : Optional[int],					__UpperCAmelCase : List[str],					__UpperCAmelCase : Tuple      )     ->			Dict:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Dict      					= ASTModel(config=__UpperCAmelCase      )
					model.to(__UpperCAmelCase      )
					model.eval()
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= model(__UpperCAmelCase      )
					self.parent.assertEqual(result.last_hidden_state.shape,					(self.batch_size, self.seq_length, self.hidden_size)      )






	def 				lowercase__  (self : List[Any]      )     ->			int:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= self.prepare_config_and_inputs()
					(
					    (
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					(
					    SCREAMING_SNAKE_CASE
					)					,					
					)  :						List[str]      					= config_and_inputs
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= {'''input_values''': input_values}
					return config, inputs_dict



@require_torch
class 	a__     (				_lowercase,			_lowercase,			unittest.TestCase	):
	__magic_name__		:    Union[str, Any]	    =     (
	    (
	        ASTModel,
	        ASTForAudioClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	__magic_name__		:    List[str]	    =     (
	    {"audio-classification": ASTForAudioClassification, "feature-extraction": ASTModel}
	    if is_torch_available()
	    else {}
	)
	__magic_name__		:    Optional[Any]	    =     False
	__magic_name__		:    Dict	    =     False
	__magic_name__		:    Optional[int]	    =     False
	__magic_name__		:    Optional[Any]	    =     False


	def 				lowercase__  (self : Union[str, Any],					__UpperCAmelCase : Optional[Any],					__UpperCAmelCase : Optional[Any],					__UpperCAmelCase : Dict,					__UpperCAmelCase : Union[str, Any],					__UpperCAmelCase : List[str]      )     ->			Optional[int]:

					"""simple docstring"""

					if pipeline_test_casse_name == "AudioClassificationPipelineTests":
									return True

					return False


	def 				lowercase__  (self : Optional[int]      )     ->			Union[str, Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Any      					= ASTModelTester(self      )
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= ConfigTester(self,					config_class=__UpperCAmelCase,					has_text_modality=__UpperCAmelCase,					hidden_size=37      )


	def 				lowercase__  (self : int      )     ->			str:

					"""simple docstring"""

					self.config_tester.run_common_tests()


	@unittest.skip(reason='''AST does not use inputs_embeds'''      )
	def 				lowercase__  (self : str      )     ->			Optional[int]:

					"""simple docstring"""

					pass


	def 				lowercase__  (self : List[str]      )     ->			Tuple:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE					,					SCREAMING_SNAKE_CASE  :						Any      					= self.model_tester.prepare_config_and_inputs_for_common()

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


	def 				lowercase__  (self : Optional[Any]      )     ->			List[str]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE					,					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= self.model_tester.prepare_config_and_inputs_for_common()

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

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


	def 				lowercase__  (self : Any      )     ->			Optional[int]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*__UpperCAmelCase      )






	@slow
	def 				lowercase__  (self : Union[str, Any]      )     ->			int:

					"""simple docstring"""

					for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= ASTModel.from_pretrained(__UpperCAmelCase      )
									self.assertIsNotNone(__UpperCAmelCase      )






def       __lowercase  ():
				SCREAMING_SNAKE_CASE  :						int      					= hf_hub_download(
				    repo_id='''nielsr/audio-spectogram-transformer-checkpoint'''    ,	filename='''sample_audio.flac'''    ,	repo_type='''dataset'''       )

				SCREAMING_SNAKE_CASE					,					SCREAMING_SNAKE_CASE  :						List[str]      					= torchaudio.load(_SCREAMING_SNAKE_CASE       )

				return audio, sampling_rate




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


	@cached_property
	def 				lowercase__  (self : List[str]      )     ->			List[Any]:

					"""simple docstring"""

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






	@slow
	def 				lowercase__  (self : List[str]      )     ->			str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= self.default_feature_extractor
					SCREAMING_SNAKE_CASE  :						List[Any]      					= ASTForAudioClassification.from_pretrained('''MIT/ast-finetuned-audioset-10-10-0.4593'''      ).to(__UpperCAmelCase      )

					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= self.default_feature_extractor
					SCREAMING_SNAKE_CASE					,					SCREAMING_SNAKE_CASE  :						List[str]      					= prepare_audio()
					SCREAMING_SNAKE_CASE  :						Dict      					= audio.squeeze().numpy()
					SCREAMING_SNAKE_CASE  :						Any      					= feature_extractor(__UpperCAmelCase,					sampling_rate=__UpperCAmelCase,					return_tensors='''pt'''      ).to(__UpperCAmelCase      )

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

					# verify the logits
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= torch.Size((1, 527)      )
					self.assertEqual(outputs.logits.shape,					__UpperCAmelCase      )

					SCREAMING_SNAKE_CASE  :						int      					= torch.tensor([-0.8760, -7.0042, -8.6602]      ).to(__UpperCAmelCase      )

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









'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


snake_case_			=     {
    """configuration_timesformer""": ["""TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TimesformerConfig"""],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	snake_case_			=     [
	    """TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """TimesformerModel""",
	    """TimesformerForVideoClassification""",
	    """TimesformerPreTrainedModel""",
	]

if TYPE_CHECKING:
	from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_timesformer import (
		    TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TimesformerForVideoClassification,
		    TimesformerModel,
		    TimesformerPreTrainedModel,
		)

else:
	import sys

	snake_case_			=     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




