

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    : str				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : Union[str, Any]=1_3				,				UpperCAmelCase__    : str=2				,				UpperCAmelCase__    : Optional[int]=2_4				,				UpperCAmelCase__    : Optional[Any]=1_6				,				UpperCAmelCase__    : int=True				,				UpperCAmelCase__    : Optional[Any]=True				,				UpperCAmelCase__    : Any=3_2				,				UpperCAmelCase__    : Tuple=5				,				UpperCAmelCase__    : str=4				,				UpperCAmelCase__    : int=3_7				,				UpperCAmelCase__    : Optional[Any]="gelu"				,				UpperCAmelCase__    : List[str]=0.1				,				UpperCAmelCase__    : Tuple=0.1				,				UpperCAmelCase__    : List[Any]=1_0				,				UpperCAmelCase__    : Any=0.02				,				UpperCAmelCase__    : Optional[int]=None				,				UpperCAmelCase__    : List[str]=2				,				UpperCAmelCase__    : List[Any]=2				,				)					->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Any       =   parent
        SCREAMING_SNAKE_CASE :      Tuple       =   batch_size
        SCREAMING_SNAKE_CASE :      Tuple       =   patch_size
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   max_length
        SCREAMING_SNAKE_CASE :      Any       =   num_mel_bins
        SCREAMING_SNAKE_CASE :      Dict       =   is_training
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   use_labels
        SCREAMING_SNAKE_CASE :      List[Any]       =   hidden_size
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   num_hidden_layers
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   num_attention_heads
        SCREAMING_SNAKE_CASE :      int       =   intermediate_size
        SCREAMING_SNAKE_CASE :      Optional[int]       =   hidden_act
        SCREAMING_SNAKE_CASE :      Dict       =   hidden_dropout_prob
        SCREAMING_SNAKE_CASE :      Optional[int]       =   attention_probs_dropout_prob
        SCREAMING_SNAKE_CASE :      Optional[int]       =   type_sequence_label_size
        SCREAMING_SNAKE_CASE :      Optional[int]       =   initializer_range
        SCREAMING_SNAKE_CASE :      str       =   scope
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   frequency_stride
        SCREAMING_SNAKE_CASE :      Any       =   time_stride

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


  def  _lowercase      (			self    : Tuple		)					->Union[str, Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      str       =   floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]		)

        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   None
        if self.use_labels:
              SCREAMING_SNAKE_CASE :      Union[str, Any]       =   ids_tensor([self.batch_size]				,				self.type_sequence_label_size		)

        SCREAMING_SNAKE_CASE :      Any       =   self.get_config()

        return config, input_values, labels


  def  _lowercase      (			self    : Tuple		)					->Optional[int]:
        """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=__a				,				initializer_range=self.initializer_range				,				frequency_stride=self.frequency_stride				,				time_stride=self.time_stride				,				)


  def  _lowercase      (			self    : Any				,				UpperCAmelCase__    : Tuple				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : Optional[int]		)					->Union[str, Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      int       =   ASTModel(config=__a		)
        model.to(__a		)
        model.eval()
        SCREAMING_SNAKE_CASE :      List[Any]       =   model(__a		)
        self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.hidden_size)		)




  def  _lowercase      (			self    : List[str]		)					->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      str       =   self.prepare_config_and_inputs()
        (
            (
            SCREAMING_SNAKE_CASE						
        )     ,	(
            SCREAMING_SNAKE_CASE						
        )     ,	(
            SCREAMING_SNAKE_CASE						
        )     ,							
        ) :      Optional[int]       =   config_and_inputs
        SCREAMING_SNAKE_CASE :      Tuple       =   {"""input_values""": input_values}
        return config, inputs_dict







@require_torch
class        a__       (   UpperCAmelCase	,			UpperCAmelCase	,			unittest.TestCase       ):







  """simple docstring"""




  UpperCAmelCase__      : Any			       =(
      (
          ASTModel,
          ASTForAudioClassification,
      )
      if is_torch_available()
      else ()
  )
  UpperCAmelCase__      : Optional[Any]			       =(
      {"""audio-classification""": ASTForAudioClassification, """feature-extraction""": ASTModel}
      if is_torch_available()
      else {}
  )
  UpperCAmelCase__      : Optional[int]			       =False
  UpperCAmelCase__      : Optional[int]			       =False
  UpperCAmelCase__      : Optional[Any]			       =False
  UpperCAmelCase__      : Dict			       =False


  def  _lowercase      (			self    : Dict				,				UpperCAmelCase__    : Tuple				,				UpperCAmelCase__    : List[Any]				,				UpperCAmelCase__    : Dict				,				UpperCAmelCase__    : Any				,				UpperCAmelCase__    : List[str]		)					->Optional[Any]:
        """simple docstring"""







        if pipeline_test_casse_name == "AudioClassificationPipelineTests":
              return True

        return False


  def  _lowercase      (			self    : Dict		)					->Any:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Optional[int]       =   ASTModelTester(self		)
        SCREAMING_SNAKE_CASE :      Tuple       =   ConfigTester(self				,				config_class=__a				,				has_text_modality=__a				,				hidden_size=3_7		)


  def  _lowercase      (			self    : Tuple		)					->int:
        """simple docstring"""







        self.config_tester.run_common_tests()


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







        pass


  def  _lowercase      (			self    : str		)					->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      int       =   self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE :      List[Any]       =   model_class(__a		)
              self.assertIsInstance(model.get_input_embeddings()				,				(nn.Module)		)
              SCREAMING_SNAKE_CASE :      Union[str, Any]       =   model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(__a				,				nn.Linear		)		)


  def  _lowercase      (			self    : str		)					->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      Tuple       =   self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE :      Tuple       =   model_class(__a		)
              SCREAMING_SNAKE_CASE :      Union[str, Any]       =   inspect.signature(model.forward		)
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              SCREAMING_SNAKE_CASE :      Union[str, Any]       =   [*signature.parameters.keys()]

              SCREAMING_SNAKE_CASE :      List[str]       =   ["""input_values"""]
              self.assertListEqual(arg_names[:1]				,				__a		)


  def  _lowercase      (			self    : List[str]		)					->Optional[int]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      List[str]       =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__a		)




  @slow
  def  _lowercase      (			self    : List[Any]		)					->Optional[Any]:
        """simple docstring"""







        for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE :      int       =   ASTModel.from_pretrained(__a		)
              self.assertIsNotNone(__a		)






def 					__lowercase      (     )     ->						List[str]:
      SCREAMING_SNAKE_CASE :      Dict       =   hf_hub_download(
          repo_id="""nielsr/audio-spectogram-transformer-checkpoint"""						,    filename="""sample_audio.flac"""						,    repo_type="""dataset"""			)

      SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      Tuple       =   torchaudio.load(__snake_case			)

      return audio, sampling_rate







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







  """simple docstring"""




  @cached_property
  def  _lowercase      (			self    : Optional[int]		)					->Dict:
        """simple docstring"""







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




  @slow
  def  _lowercase      (			self    : List[Any]		)					->Optional[int]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      int       =   self.default_feature_extractor
        SCREAMING_SNAKE_CASE :      int       =   ASTForAudioClassification.from_pretrained("""MIT/ast-finetuned-audioset-10-10-0.4593"""		).to(__a		)

        SCREAMING_SNAKE_CASE :      Optional[int]       =   self.default_feature_extractor
        SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      Union[str, Any]       =   prepare_audio()
        SCREAMING_SNAKE_CASE :      Optional[int]       =   audio.squeeze().numpy()
        SCREAMING_SNAKE_CASE :      Optional[int]       =   feature_extractor(__a				,				sampling_rate=__a				,				return_tensors="""pt"""		).to(__a		)

        # forward pass
        with torch.no_grad():
              SCREAMING_SNAKE_CASE :      Optional[int]       =   model(**__a		)

        # verify the logits
        SCREAMING_SNAKE_CASE :      Tuple       =   torch.Size((1, 5_2_7)		)
        self.assertEqual(outputs.logits.shape				,				__a		)

        SCREAMING_SNAKE_CASE :      Tuple       =   torch.tensor([-0.87_60, -7.00_42, -8.66_02]		).to(__a		)

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







from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase__ :     Dict				    =    logging.get_logger(__name__)

UpperCAmelCase__ :     int				    =    {
    """abeja/gpt-neox-japanese-2.7b""": """https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json""",
}







class        a__       (   UpperCAmelCase       ):







  """simple docstring"""




  UpperCAmelCase__      : Any			       ="""gpt_neox_japanese"""


  def __init__(			self    : Any				,				UpperCAmelCase__    : Any=3_2_0_0_0				,				UpperCAmelCase__    : Dict=2_5_6_0				,				UpperCAmelCase__    : List[str]=3_2				,				UpperCAmelCase__    : Optional[int]=3_2				,				UpperCAmelCase__    : List[str]=4				,				UpperCAmelCase__    : List[str]="gelu"				,				UpperCAmelCase__    : Optional[int]=1.00				,				UpperCAmelCase__    : List[Any]=1_0_0_0_0				,				UpperCAmelCase__    : Tuple=2_0_4_8				,				UpperCAmelCase__    : Optional[int]=0.02				,				UpperCAmelCase__    : Dict=1e-5				,				UpperCAmelCase__    : Dict=True				,				UpperCAmelCase__    : Tuple=3_1_9_9_6				,				UpperCAmelCase__    : Tuple=3_1_9_9_9				,				UpperCAmelCase__    : Dict=0.1				,				UpperCAmelCase__    : Union[str, Any]=0.0				,				**UpperCAmelCase__    : Optional[Any]				,				)					->Optional[Any]:
        """simple docstring"""







        super().__init__(bos_token_id=UpperCAmelCase__				,				eos_token_id=UpperCAmelCase__				,				**UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      List[str]       =   vocab_size
        SCREAMING_SNAKE_CASE :      List[str]       =   max_position_embeddings
        SCREAMING_SNAKE_CASE :      Tuple       =   hidden_size
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   num_hidden_layers
        SCREAMING_SNAKE_CASE :      Optional[int]       =   num_attention_heads
        SCREAMING_SNAKE_CASE :      Any       =   intermediate_multiple_size
        SCREAMING_SNAKE_CASE :      Tuple       =   hidden_act
        SCREAMING_SNAKE_CASE :      Any       =   rotary_pct
        SCREAMING_SNAKE_CASE :      Tuple       =   rotary_emb_base
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   initializer_range
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   layer_norm_eps
        SCREAMING_SNAKE_CASE :      str       =   use_cache
        SCREAMING_SNAKE_CASE :      Any       =   attention_dropout
        SCREAMING_SNAKE_CASE :      str       =   hidden_dropout




