"""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  snake_case__      :


 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    ,		):
        __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 						a__   ( self						):
        __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 						a__   ( self						):
        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    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase						):
        __a       =    ASTModel(config=lowerCamelCase						)
        model.to(lowerCamelCase						)
        model.eval()
        __a       =    model(lowerCamelCase						)
        self.parent.assertEqual(result.last_hidden_state.shape    ,		(self.batch_size, self.seq_length, self.hidden_size)						)






 def 						a__   ( self						):
        __a       =    self.prepare_config_and_inputs()
        (
            (
            __a
        ) ,   (
            __a
        ) ,   (
            __a
        ) ,   
        )       =    config_and_inputs
        __a       =    {"input_values": input_values}
        return config, inputs_dict






@require_torch
class  snake_case__      (       snake_case_,						snake_case_,						unittest.TestCase       ):
 _snake_case       :					Tuple							  =						(
     (
         ASTModel,
         ASTForAudioClassification,
     )
     if is_torch_available()
     else ()
 )
 _snake_case       :					Any							  =						(
     {"""audio-classification""": ASTForAudioClassification, """feature-extraction""": ASTModel}
     if is_torch_available()
     else {}
 )
 _snake_case       :					int							  =						False
 _snake_case       :					Optional[Any]							  =						False
 _snake_case       :					List[str]							  =						False
 _snake_case       :					Dict							  =						False


 def 						a__   ( self    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase						):
        if pipeline_test_casse_name == "AudioClassificationPipelineTests":
               return True

        return False


 def 						a__   ( self						):
        __a       =    ASTModelTester(self						)
        __a       =    ConfigTester(self    ,		config_class=lowerCamelCase    ,		has_text_modality=lowerCamelCase    ,		hidden_size=37						)


 def 						a__   ( self						):
        self.config_tester.run_common_tests()


 @unittest.skip(reason="AST does not use inputs_embeds"						)
 def 						a__   ( self						):
        pass


 def 						a__   ( self						):
        __a ,   __a       =    self.model_tester.prepare_config_and_inputs_for_common()

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


 def 						a__   ( self						):
        __a ,   __a       =    self.model_tester.prepare_config_and_inputs_for_common()

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


 def 						a__   ( self						):
        __a       =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCamelCase						)






 @slow
 def 						a__   ( self						):
        for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __a       =    ASTModel.from_pretrained(lowerCamelCase						)
               self.assertIsNotNone(lowerCamelCase						)



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

       __a ,   __a       =    torchaudio.load(a				)

       return audio, sampling_rate






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


 @cached_property
 def 						a__   ( self						):
        return (
            ASTFeatureExtractor.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"						)
            if is_torchaudio_available()
            else None
        )






 @slow
 def 						a__   ( self						):
        __a       =    self.default_feature_extractor
        __a       =    ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"						).to(lowerCamelCase						)

        __a       =    self.default_feature_extractor
        __a ,   __a       =    prepare_audio()
        __a       =    audio.squeeze().numpy()
        __a       =    feature_extractor(lowerCamelCase    ,		sampling_rate=lowerCamelCase    ,		return_tensors="pt"						).to(lowerCamelCase						)

        # forward pass
        with torch.no_grad():
               __a       =    model(**lowerCamelCase						)

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

        __a       =    torch.tensor([-0.8760, -7.0042, -8.6602]						).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_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


SCREAMING_SNAKE_CASE__:Optional[Any]									=			{"""configuration_reformer""": ["""REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ReformerConfig"""]}

try:
   if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   SCREAMING_SNAKE_CASE__:Optional[Any]									=			["""ReformerTokenizer"""]

try:
   if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   SCREAMING_SNAKE_CASE__:List[str]									=			["""ReformerTokenizerFast"""]

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   SCREAMING_SNAKE_CASE__:int									=			[
       """REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
       """ReformerAttention""",
       """ReformerForMaskedLM""",
       """ReformerForQuestionAnswering""",
       """ReformerForSequenceClassification""",
       """ReformerLayer""",
       """ReformerModel""",
       """ReformerModelWithLMHead""",
       """ReformerPreTrainedModel""",
   ]


if TYPE_CHECKING:
   from .configuration_reformer import REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ReformerConfig

   try:
      if not is_sentencepiece_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_reformer import ReformerTokenizer

   try:
      if not is_tokenizers_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_reformer_fast import ReformerTokenizerFast

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_reformer import (
          REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
          ReformerAttention,
          ReformerForMaskedLM,
          ReformerForQuestionAnswering,
          ReformerForSequenceClassification,
          ReformerLayer,
          ReformerModel,
          ReformerModelWithLMHead,
          ReformerPreTrainedModel,
      )

else:
   import sys

   SCREAMING_SNAKE_CASE__:int									=			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
