

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						:







    """simple docstring"""





    def __init__(      self :       List[str],lowercase_ :       str,lowercase_ :       Any=1_3,lowercase_ :       Dict=2,lowercase_ :       Optional[Any]=2_4,lowercase_ :       Optional[int]=1_6,lowercase_ :       List[Any]=True,lowercase_ :       Any=True,lowercase_ :       int=3_2,lowercase_ :       str=5,lowercase_ :       Union[str, Any]=4,lowercase_ :       Any=3_7,lowercase_ :       List[Any]="gelu",lowercase_ :       Optional[int]=0.1,lowercase_ :       Any=0.1,lowercase_ :       str=1_0,lowercase_ :       Any=0.02,lowercase_ :       int=None,lowercase_ :       str=2,lowercase_ :       Any=2,)->    List[str]:





        '''simple docstring'''







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

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


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





        '''simple docstring'''







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

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

        A__          =   self.get_config()

        return config, input_values, labels


    def 						snake_case__     (      self :       Optional[int]		)->    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=lowercase_,initializer_range=self.initializer_range,frequency_stride=self.frequency_stride,time_stride=self.time_stride,)


    def 						snake_case__     (      self :       List[Any],lowercase_ :       Tuple,lowercase_ :       int,lowercase_ :       Union[str, Any]		)->    Tuple:





        '''simple docstring'''







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


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





        '''simple docstring'''







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




@require_torch
class  A						(							_UpperCAmelCase    ,							_UpperCAmelCase    ,							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 						snake_case__     (      self :       Union[str, Any],lowercase_ :       Tuple,lowercase_ :       Tuple,lowercase_ :       List[str],lowercase_ :       Any,lowercase_ :       int		)->    Tuple:





        '''simple docstring'''







        if pipeline_test_casse_name == "AudioClassificationPipelineTests":
            return True

        return False


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





        '''simple docstring'''







        A__          =   ASTModelTester(self		)
        A__          =   ConfigTester(self,config_class=lowercase_,has_text_modality=lowercase_,hidden_size=3_7		)


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





        '''simple docstring'''







        self.config_tester.run_common_tests()


    @unittest.skip(reason='AST does not use inputs_embeds'		)
    def 						snake_case__     (      self :       List[Any]		)->    Tuple:





        '''simple docstring'''







        pass


    def 						snake_case__     (      self :       Any		)->    Any:





        '''simple docstring'''







        A__      ,						A__          =   self.model_tester.prepare_config_and_inputs_for_common()

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


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





        '''simple docstring'''







        A__      ,						A__          =   self.model_tester.prepare_config_and_inputs_for_common()

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

            A__          =   ['input_values']
            self.assertListEqual(arg_names[:1],lowercase_		)


    def 						snake_case__     (      self :       Dict		)->    List[Any]:





        '''simple docstring'''







        A__          =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase_		)


    @slow
    def 						snake_case__     (      self :       Optional[Any]		)->    List[str]:





        '''simple docstring'''







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




def 						_snake_case(    )							->      Optional[Any]:





    '''simple docstring'''






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

    A__      ,						A__          =   torchaudio.load(SCREAMING_SNAKE_CASE__      )

    return audio, sampling_rate


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







    """simple docstring"""





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





        '''simple docstring'''







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


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





        '''simple docstring'''







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

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

        # forward pass
        with torch.no_grad():
            A__          =   model(**lowercase_		)

        # verify the logits
        A__          =   torch.Size((1, 5_2_7)		)
        self.assertEqual(outputs.logits.shape,lowercase_		)

        A__          =   torch.tensor([-0.8_760, -7.0_042, -8.6_602]		).to(lowercase_		)

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








from typing import TYPE_CHECKING

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


UpperCAmelCase	:	Any		     =       {
    """configuration_convbert""": ["""CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ConvBertConfig""", """ConvBertOnnxConfig"""],
    """tokenization_convbert""": ["""ConvBertTokenizer"""],
}

try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCAmelCase	:	List[Any]		     =       ["""ConvBertTokenizerFast"""]

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCAmelCase	:	List[str]		     =       [
         """CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
         """ConvBertForMaskedLM""",
         """ConvBertForMultipleChoice""",
         """ConvBertForQuestionAnswering""",
         """ConvBertForSequenceClassification""",
         """ConvBertForTokenClassification""",
         """ConvBertLayer""",
         """ConvBertModel""",
         """ConvBertPreTrainedModel""",
         """load_tf_weights_in_convbert""",
     ]


try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCAmelCase	:	Union[str, Any]		     =       [
         """TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
         """TFConvBertForMaskedLM""",
         """TFConvBertForMultipleChoice""",
         """TFConvBertForQuestionAnswering""",
         """TFConvBertForSequenceClassification""",
         """TFConvBertForTokenClassification""",
         """TFConvBertLayer""",
         """TFConvBertModel""",
         """TFConvBertPreTrainedModel""",
     ]


if TYPE_CHECKING:
     from .configuration_convbert import CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvBertConfig, ConvBertOnnxConfig
     from .tokenization_convbert import ConvBertTokenizer

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_convbert_fast import ConvBertTokenizerFast

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_convbert import (
              CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
              ConvBertForMaskedLM,
              ConvBertForMultipleChoice,
              ConvBertForQuestionAnswering,
              ConvBertForSequenceClassification,
              ConvBertForTokenClassification,
              ConvBertLayer,
              ConvBertModel,
              ConvBertPreTrainedModel,
              load_tf_weights_in_convbert,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_convbert import (
              TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
              TFConvBertForMaskedLM,
              TFConvBertForMultipleChoice,
              TFConvBertForQuestionAnswering,
              TFConvBertForSequenceClassification,
              TFConvBertForTokenClassification,
              TFConvBertLayer,
              TFConvBertModel,
              TFConvBertPreTrainedModel,
          )


else:
     import sys

     UpperCAmelCase	:	Union[str, Any]		     =       _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)






