
'''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    ,   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    ,   ):





								"""simple docstring"""






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

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


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






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

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

								_snake_case   						=		self.get_config()

								return config, input_values, labels


							def  lowerCamelCase				(       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=SCREAMING_SNAKE_CASE__    ,   initializer_range=self.initializer_range    ,   frequency_stride=self.frequency_stride    ,   time_stride=self.time_stride    ,   )


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






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




							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






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


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


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								if pipeline_test_casse_name == "AudioClassificationPipelineTests":
									return True

								return False


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		ASTModelTester(self  )
								_snake_case   						=		ConfigTester(self    ,   config_class=SCREAMING_SNAKE_CASE__    ,   has_text_modality=SCREAMING_SNAKE_CASE__    ,   hidden_size=37  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								self.config_tester.run_common_tests()


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





								"""simple docstring"""






								pass


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.model_tester.prepare_config_and_inputs_for_common()

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


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.model_tester.prepare_config_and_inputs_for_common()

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

									_snake_case   						=		['''input_values''']
									self.assertListEqual(arg_names[:1]    ,   SCREAMING_SNAKE_CASE__  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__  )




							@slow
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






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







def 				SCREAMING_SNAKE_CASE__	(   )       ->	str:
	_snake_case   						=		hf_hub_download(
	    repo_id='nielsr/audio-spectogram-transformer-checkpoint'			,				filename='sample_audio.flac'			,				repo_type='dataset'	)

	_snake_case   						=		torchaudio.load(a_	)

	return audio, sampling_rate





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


							@cached_property
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






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




							@slow
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






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

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

								# forward pass
								with torch.no_grad():
									_snake_case   						=		model(**SCREAMING_SNAKE_CASE__  )

								# verify the logits
								_snake_case   						=		torch.Size((1, 5_27)  )
								self.assertEqual(outputs.logits.shape    ,   SCREAMING_SNAKE_CASE__  )

								_snake_case   						=		torch.tensor([-0.8760, -7.0042, -8.6602]  ).to(SCREAMING_SNAKE_CASE__  )

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








"""simple docstring"""
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCamelCase_								=		logging.get_logger(__name__)

lowerCamelCase_								=		{
    "microsoft/beit-base-patch16-224-pt22k": (
        "https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json"
    ),
    # See all BEiT models at https://huggingface.co/models?filter=beit
}






class 					_SCREAMING_SNAKE_CASE(   A  ):
       SCREAMING_SNAKE_CASE_   :      str							=  '''beit'''




       def __init__(						self		,SCREAMING_SNAKE_CASE__=81_92		,SCREAMING_SNAKE_CASE__=7_68		,SCREAMING_SNAKE_CASE__=12		,SCREAMING_SNAKE_CASE__=12		,SCREAMING_SNAKE_CASE__=30_72		,SCREAMING_SNAKE_CASE__="gelu"		,SCREAMING_SNAKE_CASE__=0.0		,SCREAMING_SNAKE_CASE__=0.0		,SCREAMING_SNAKE_CASE__=0.0_2		,SCREAMING_SNAKE_CASE__=1E-12		,SCREAMING_SNAKE_CASE__=2_24		,SCREAMING_SNAKE_CASE__=16		,SCREAMING_SNAKE_CASE__=3		,SCREAMING_SNAKE_CASE__=False		,SCREAMING_SNAKE_CASE__=False		,SCREAMING_SNAKE_CASE__=False		,SCREAMING_SNAKE_CASE__=False		,SCREAMING_SNAKE_CASE__=0.1		,SCREAMING_SNAKE_CASE__=0.1		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=[3, 5, 7, 11]		,SCREAMING_SNAKE_CASE__=[1, 2, 3, 6]		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=0.4		,SCREAMING_SNAKE_CASE__=2_56		,SCREAMING_SNAKE_CASE__=1		,SCREAMING_SNAKE_CASE__=False		,SCREAMING_SNAKE_CASE__=2_55		,**SCREAMING_SNAKE_CASE__		,)			->      Optional[Any]:





             """simple docstring"""






             super().__init__(**SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE   :Any						=  vocab_size
             __SCREAMING_SNAKE_CASE   :Dict						=  hidden_size
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  num_hidden_layers
             __SCREAMING_SNAKE_CASE   :List[str]						=  num_attention_heads
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  intermediate_size
             __SCREAMING_SNAKE_CASE   :Optional[int]						=  hidden_act
             __SCREAMING_SNAKE_CASE   :Tuple						=  hidden_dropout_prob
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  attention_probs_dropout_prob
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  initializer_range
             __SCREAMING_SNAKE_CASE   :str						=  layer_norm_eps

             __SCREAMING_SNAKE_CASE   :int						=  image_size
             __SCREAMING_SNAKE_CASE   :Tuple						=  patch_size
             __SCREAMING_SNAKE_CASE   :Any						=  num_channels
             __SCREAMING_SNAKE_CASE   :Any						=  use_mask_token
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  use_absolute_position_embeddings
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  use_relative_position_bias
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  use_shared_relative_position_bias
             __SCREAMING_SNAKE_CASE   :List[str]						=  layer_scale_init_value
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  drop_path_rate
             __SCREAMING_SNAKE_CASE   :str						=  use_mean_pooling
             # decode head attributes (semantic segmentation)
             __SCREAMING_SNAKE_CASE   :Dict						=  out_indices
             __SCREAMING_SNAKE_CASE   :Optional[int]						=  pool_scales
             # auxiliary head attributes (semantic segmentation)
             __SCREAMING_SNAKE_CASE   :Optional[int]						=  use_auxiliary_head
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  auxiliary_loss_weight
             __SCREAMING_SNAKE_CASE   :Dict						=  auxiliary_channels
             __SCREAMING_SNAKE_CASE   :Optional[int]						=  auxiliary_num_convs
             __SCREAMING_SNAKE_CASE   :List[str]						=  auxiliary_concat_input
             __SCREAMING_SNAKE_CASE   :List[Any]						=  semantic_loss_ignore_index






class 					_SCREAMING_SNAKE_CASE(   A  ):
       SCREAMING_SNAKE_CASE_   :      List[Any]							=  version.parse('''1.11'''  )




       @property
       def     _UpperCamelCase     (						self							)			->      Mapping[str, Mapping[int, str]]:





             """simple docstring"""






             return OrderedDict(
                 [
                     ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
                 ]							)




       @property
       def     _UpperCamelCase     (						self							)			->      float:





             """simple docstring"""






             return 1E-4