






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


    '''simple docstring'''

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

     '''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    a__				(					self     :     Tuple     )							->							str:

     '''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    a__				(					self     :     List[Any]     )							->							Dict:

     '''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    a__				(					self     :     int				,     __lowerCamelCase     :     Dict				,     __lowerCamelCase     :     Union[str, Any]				,     __lowerCamelCase     :     Tuple     )							->							Dict:

     '''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    a__				(					self     :     Optional[int]     )							->							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 				lowercase       (							_lowerCamelCase ,       _lowerCamelCase ,       unittest.TestCase      ):


    '''simple docstring'''

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

    def    a__				(					self     :     Tuple				,     __lowerCamelCase     :     List[Any]				,     __lowerCamelCase     :     Tuple				,     __lowerCamelCase     :     Optional[Any]				,     __lowerCamelCase     :     int				,     __lowerCamelCase     :     List[str]     )							->							List[Any]:

     '''simple docstring'''




     if pipeline_test_casse_name == "AudioClassificationPipelineTests":
      return True

     return False

    def    a__				(					self     :     Optional[Any]     )							->							Any:

     '''simple docstring'''




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

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

     '''simple docstring'''




     self.config_tester.run_common_tests()

    @unittest.skip(reason="AST does not use inputs_embeds"     )
    def    a__				(					self     :     List[str]     )							->							Optional[Any]:

     '''simple docstring'''




     pass

    def    a__				(					self     :     Dict     )							->							List[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    a__				(					self     :     Optional[int]     )							->							Tuple:

     '''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    a__				(					self     :     List[Any]     )							->							Any:

     '''simple docstring'''




     lowerCamelCase__  =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowerCamelCase     )




    @slow
    def    a__				(					self     :     List[Any]     )							->							int:

     '''simple docstring'''




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






def    lowerCamelCase_     (    ):
 lowerCamelCase__  =     hf_hub_download(
     repo_id="nielsr/audio-spectogram-transformer-checkpoint"				,     filename="sample_audio.flac"				,     repo_type="dataset")

 lowerCamelCase__  ,	lowerCamelCase__  =     torchaudio.load(lowercase__)

 return audio, sampling_rate
@require_torch
@require_torchaudio
class 				lowercase       (							unittest.TestCase      ):


    '''simple docstring'''

    @cached_property
    def    a__				(					self     :     Optional[int]     )							->							int:

     '''simple docstring'''




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




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

     '''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.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 typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__A							:							Tuple  							=		{
    """configuration_deberta""": ["""DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DebertaConfig""", """DebertaOnnxConfig"""],
    """tokenization_deberta""": ["""DebertaTokenizer"""],
}

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __A							:							int  							=		["""DebertaTokenizerFast"""]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __A							:							str  							=		[
     """DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST""",
     """DebertaForMaskedLM""",
     """DebertaForQuestionAnswering""",
     """DebertaForSequenceClassification""",
     """DebertaForTokenClassification""",
     """DebertaModel""",
     """DebertaPreTrainedModel""",
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __A							:							Union[str, Any]  							=		[
     """TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST""",
     """TFDebertaForMaskedLM""",
     """TFDebertaForQuestionAnswering""",
     """TFDebertaForSequenceClassification""",
     """TFDebertaForTokenClassification""",
     """TFDebertaModel""",
     """TFDebertaPreTrainedModel""",
 ]


if TYPE_CHECKING:
 from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig, DebertaOnnxConfig
 from .tokenization_deberta import DebertaTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_deberta_fast import DebertaTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_deberta import (
      DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
      DebertaForMaskedLM,
      DebertaForQuestionAnswering,
      DebertaForSequenceClassification,
      DebertaForTokenClassification,
      DebertaModel,
      DebertaPreTrainedModel,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_deberta import (
      TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFDebertaForMaskedLM,
      TFDebertaForQuestionAnswering,
      TFDebertaForSequenceClassification,
      TFDebertaForTokenClassification,
      TFDebertaModel,
      TFDebertaPreTrainedModel,
  )


else:
 import sys

 __A							:							List[Any]  							=		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

