


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





     '''simple docstring'''



     def __init__(					self:List[str]     ,		_a:Optional[Any]     ,		_a:Any=13     ,		_a:Union[str, Any]=2     ,		_a:Dict=24     ,		_a:Optional[Any]=16     ,		_a:Dict=True     ,		_a:str=True     ,		_a:Tuple=32     ,		_a:str=5     ,		_a:Dict=4     ,		_a:List[str]=37     ,		_a:Any="gelu"     ,		_a:Optional[int]=0.1     ,		_a:Any=0.1     ,		_a:Any=10     ,		_a:List[str]=0.02     ,		_a:Optional[Any]=None     ,		_a:Union[str, Any]=2     ,		_a:int=2     ,		):
       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 	SCREAMING_SNAKE_CASE__						(					self:Optional[int]   ):
       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 	SCREAMING_SNAKE_CASE__						(					self:Optional[int]   ):
       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 	SCREAMING_SNAKE_CASE__						(					self:str     ,		_a:List[Any]     ,		_a:Dict     ,		_a:Any   ):
       snake_case__						= ASTModel(config=_a   )
       model.to(_a   )
       model.eval()
       snake_case__						= model(_a   )
       self.parent.assertEqual(result.last_hidden_state.shape     ,		(self.batch_size, self.seq_length, self.hidden_size)   )







     def 	SCREAMING_SNAKE_CASE__						(					self:Union[str, Any]   ):
       snake_case__						= self.prepare_config_and_inputs()
       (
           (
           snake_case__
       )				,				(
           snake_case__
       )				,				(
           snake_case__
       )				,				
       )						= config_and_inputs
       snake_case__						= {'''input_values''': input_values}
       return config, inputs_dict




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





     '''simple docstring'''



     __lowercase				:							str	   =       (
         (
             ASTModel,
             ASTForAudioClassification,
         )
         if is_torch_available()
         else ()
     )
     __lowercase				:							Optional[Any]	   =       (
         {'audio-classification': ASTForAudioClassification, 'feature-extraction': ASTModel}
         if is_torch_available()
         else {}
     )
     __lowercase				:							Tuple	   =       False
     __lowercase				:							Optional[int]	   =       False
     __lowercase				:							List[Any]	   =       False
     __lowercase				:							List[Any]	   =       False

     def 	SCREAMING_SNAKE_CASE__						(					self:int     ,		_a:Union[str, Any]     ,		_a:Optional[int]     ,		_a:List[str]     ,		_a:Optional[Any]     ,		_a:Tuple   ):
       if pipeline_test_casse_name == "AudioClassificationPipelineTests":
         return True

       return False

     def 	SCREAMING_SNAKE_CASE__						(					self:Union[str, Any]   ):
       snake_case__						= ASTModelTester(self   )
       snake_case__						= ConfigTester(self     ,		config_class=_a     ,		has_text_modality=_a     ,		hidden_size=37   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Optional[Any]   ):
       self.config_tester.run_common_tests()

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

     def 	SCREAMING_SNAKE_CASE__						(					self:Tuple   ):
       snake_case__				,				snake_case__						= self.model_tester.prepare_config_and_inputs_for_common()

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

     def 	SCREAMING_SNAKE_CASE__						(					self:List[str]   ):
       snake_case__				,				snake_case__						= self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
         snake_case__						= model_class(_a   )
         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]     ,		_a   )

     def 	SCREAMING_SNAKE_CASE__						(					self:List[str]   ):
       snake_case__						= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_a   )







     @slow
     def 	SCREAMING_SNAKE_CASE__						(					self:str   ):
       for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case__						= ASTModel.from_pretrained(_a   )
         self.assertIsNotNone(_a   )







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

  snake_case__				,				snake_case__						= torchaudio.load(__lowerCAmelCase				)

  return audio, sampling_rate


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





     '''simple docstring'''



     @cached_property
     def 	SCREAMING_SNAKE_CASE__						(					self:List[str]   ):
       return (
           ASTFeatureExtractor.from_pretrained('''MIT/ast-finetuned-audioset-10-10-0.4593'''   )
           if is_torchaudio_available()
           else None
       )







     @slow
     def 	SCREAMING_SNAKE_CASE__						(					self:Optional[int]   ):
       snake_case__						= self.default_feature_extractor
       snake_case__						= ASTForAudioClassification.from_pretrained('''MIT/ast-finetuned-audioset-10-10-0.4593'''   ).to(_a   )

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

       # forward pass
       with torch.no_grad():
         snake_case__						= model(**_a   )

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

       snake_case__						= torch.tensor([-0.8760, -7.0042, -8.6602]   ).to(_a   )

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





import os
import sys

lowerCamelCase__							:						Optional[int]							     =   os.path.join(os.path.dirname(__file__), """src""")
sys.path.append(SRC_DIR)


from transformers import (
    AutoConfig,
    AutoModel,
    AutoModelForCausalLM,
    AutoModelForMaskedLM,
    AutoModelForQuestionAnswering,
    AutoModelForSequenceClassification,
    AutoTokenizer,
    add_start_docstrings,
)


lowerCamelCase__							:						Optional[int]							     =   [
    """torch""",
    """numpy""",
    """tokenizers""",
    """filelock""",
    """requests""",
    """tqdm""",
    """regex""",
    """sentencepiece""",
    """sacremoses""",
    """importlib_metadata""",
    """huggingface_hub""",
]







@add_start_docstrings(AutoConfig.__doc__				)
def SCREAMING_SNAKE_CASE	(			*__lowerCAmelCase							,   **__lowerCAmelCase				)    ->				Any:
  return AutoConfig.from_pretrained(*__lowerCAmelCase							,   **__lowerCAmelCase				)







@add_start_docstrings(AutoTokenizer.__doc__				)
def SCREAMING_SNAKE_CASE	(			*__lowerCAmelCase							,   **__lowerCAmelCase				)    ->				List[str]:
  return AutoTokenizer.from_pretrained(*__lowerCAmelCase							,   **__lowerCAmelCase				)







@add_start_docstrings(AutoModel.__doc__				)
def SCREAMING_SNAKE_CASE	(			*__lowerCAmelCase							,   **__lowerCAmelCase				)    ->				Tuple:
  return AutoModel.from_pretrained(*__lowerCAmelCase							,   **__lowerCAmelCase				)







@add_start_docstrings(AutoModelForCausalLM.__doc__				)
def SCREAMING_SNAKE_CASE	(			*__lowerCAmelCase							,   **__lowerCAmelCase				)    ->				Union[str, Any]:
  return AutoModelForCausalLM.from_pretrained(*__lowerCAmelCase							,   **__lowerCAmelCase				)







@add_start_docstrings(AutoModelForMaskedLM.__doc__				)
def SCREAMING_SNAKE_CASE	(			*__lowerCAmelCase							,   **__lowerCAmelCase				)    ->				List[Any]:
  return AutoModelForMaskedLM.from_pretrained(*__lowerCAmelCase							,   **__lowerCAmelCase				)







@add_start_docstrings(AutoModelForSequenceClassification.__doc__				)
def SCREAMING_SNAKE_CASE	(			*__lowerCAmelCase							,   **__lowerCAmelCase				)    ->				List[str]:
  return AutoModelForSequenceClassification.from_pretrained(*__lowerCAmelCase							,   **__lowerCAmelCase				)







@add_start_docstrings(AutoModelForQuestionAnswering.__doc__				)
def SCREAMING_SNAKE_CASE	(			*__lowerCAmelCase							,   **__lowerCAmelCase				)    ->				Union[str, Any]:
  return AutoModelForQuestionAnswering.from_pretrained(*__lowerCAmelCase							,   **__lowerCAmelCase				)

