




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        __lowerCAmelCase					:

			"""simple docstring"""







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



							'''simple docstring'''
							__lowerCamelCase									=       parent
							__lowerCamelCase									=       batch_size
							__lowerCamelCase									=       patch_size
							__lowerCamelCase									=       max_length
							__lowerCamelCase									=       num_mel_bins
							__lowerCamelCase									=       is_training
							__lowerCamelCase									=       use_labels
							__lowerCamelCase									=       hidden_size
							__lowerCamelCase									=       num_hidden_layers
							__lowerCamelCase									=       num_attention_heads
							__lowerCamelCase									=       intermediate_size
							__lowerCamelCase									=       hidden_act
							__lowerCamelCase									=       hidden_dropout_prob
							__lowerCamelCase									=       attention_probs_dropout_prob
							__lowerCamelCase									=       type_sequence_label_size
							__lowerCamelCase									=       initializer_range
							__lowerCamelCase									=       scope
							__lowerCamelCase									=       frequency_stride
							__lowerCamelCase									=       time_stride

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


			def  lowercase_       ( self				)       -> Any:



							'''simple docstring'''
							__lowerCamelCase									=       floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]				)

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

							__lowerCamelCase									=       self.get_config()

							return config, input_values, labels


			def  lowercase_       ( self				)       -> List[Any]:



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


			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__				)       -> Optional[Any]:



							'''simple docstring'''
							__lowerCamelCase									=       ASTModel(config=lowerCamelCase__				)
							model.to(lowerCamelCase__				)
							model.eval()
							__lowerCamelCase									=       model(lowerCamelCase__				)
							self.parent.assertEqual(result.last_hidden_state.shape     ,		(self.batch_size, self.seq_length, self.hidden_size)				)






			def  lowercase_       ( self				)       -> Tuple:



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







@require_torch
class        __lowerCAmelCase					(			__magic_name__     ,							__magic_name__     ,							unittest.TestCase					):

			"""simple docstring"""







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


			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__				)       -> Union[str, Any]:



							'''simple docstring'''
							if pipeline_test_casse_name == "AudioClassificationPipelineTests":
											return True

							return False


			def  lowercase_       ( self				)       -> List[Any]:



							'''simple docstring'''
							__lowerCamelCase									=       ASTModelTester(self				)
							__lowerCamelCase									=       ConfigTester(self     ,		config_class=lowerCamelCase__     ,		has_text_modality=lowerCamelCase__     ,		hidden_size=37				)


			def  lowercase_       ( self				)       -> str:



							'''simple docstring'''
							self.config_tester.run_common_tests()


			@unittest.skip(reason='AST does not use inputs_embeds'				)
			def  lowercase_       ( self				)       -> int:



							'''simple docstring'''
							pass


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



							'''simple docstring'''
							__lowerCamelCase			,		__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs_for_common()

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


			def  lowercase_       ( self				)       -> str:



							'''simple docstring'''
							__lowerCamelCase			,		__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs_for_common()

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

											__lowerCamelCase									=       ['input_values']
											self.assertListEqual(arg_names[:1]     ,		lowerCamelCase__				)


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



							'''simple docstring'''
							__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__				)






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



							'''simple docstring'''
							for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCamelCase									=       ASTModel.from_pretrained(lowerCamelCase__				)
											self.assertIsNotNone(lowerCamelCase__				)







def      lowerCamelCase_						(			)	->			int:





				"""simple docstring"""


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

				__lowerCamelCase			,		__lowerCamelCase									=       torchaudio.load(UpperCamelCase__ )

				return audio, sampling_rate







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

			"""simple docstring"""







			@cached_property
			def  lowercase_       ( self				)       -> 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				)       -> str:



							'''simple docstring'''
							__lowerCamelCase									=       self.default_feature_extractor
							__lowerCamelCase									=       ASTForAudioClassification.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593'				).to(lowerCamelCase__				)

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

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

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

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

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








from ...configuration_utils import PretrainedConfig
from ...utils import logging


__A								=       logging.get_logger(__name__)

__A								=       {
    "facebook/vit-mae-base": "https://huggingface.co/facebook/vit-mae-base/resolve/main/config.json",
    # See all ViT MAE models at https://huggingface.co/models?filter=vit-mae
}







class        __lowerCAmelCase					(			__magic_name__					):

			"""simple docstring"""







			snake_case_  						=			'''vit_mae'''


			def __init__( self     ,		lowerCamelCase__=768     ,		lowerCamelCase__=12     ,		lowerCamelCase__=12     ,		lowerCamelCase__=3_072     ,		lowerCamelCase__="gelu"     ,		lowerCamelCase__=0.0     ,		lowerCamelCase__=0.0     ,		lowerCamelCase__=0.02     ,		lowerCamelCase__=1e-12     ,		lowerCamelCase__=224     ,		lowerCamelCase__=16     ,		lowerCamelCase__=3     ,		lowerCamelCase__=True     ,		lowerCamelCase__=16     ,		lowerCamelCase__=512     ,		lowerCamelCase__=8     ,		lowerCamelCase__=2_048     ,		lowerCamelCase__=0.75     ,		lowerCamelCase__=False     ,		**lowerCamelCase__     ,		)       -> Optional[int]:



							'''simple docstring'''
							super().__init__(**lowerCamelCase__				)

							__lowerCamelCase									=       hidden_size
							__lowerCamelCase									=       num_hidden_layers
							__lowerCamelCase									=       num_attention_heads
							__lowerCamelCase									=       intermediate_size
							__lowerCamelCase									=       hidden_act
							__lowerCamelCase									=       hidden_dropout_prob
							__lowerCamelCase									=       attention_probs_dropout_prob
							__lowerCamelCase									=       initializer_range
							__lowerCamelCase									=       layer_norm_eps
							__lowerCamelCase									=       image_size
							__lowerCamelCase									=       patch_size
							__lowerCamelCase									=       num_channels
							__lowerCamelCase									=       qkv_bias
							__lowerCamelCase									=       decoder_num_attention_heads
							__lowerCamelCase									=       decoder_hidden_size
							__lowerCamelCase									=       decoder_num_hidden_layers
							__lowerCamelCase									=       decoder_intermediate_size
							__lowerCamelCase									=       mask_ratio
							__lowerCamelCase									=       norm_pix_loss


