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


  """simple docstring"""

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







    '''simple docstring'''


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

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




  def 		lowerCamelCase__  (		self      :       Optional[int]      ):







    '''simple docstring'''


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

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

    lowercase_     =       self.get_config()

    return config, input_values, labels




  def 		lowerCamelCase__  (		self      :       List[str]      ):







    '''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 		lowerCamelCase__  (		self      :       Any   ,				lowercase_      :       Union[str, Any]   ,				lowercase_      :       Union[str, Any]   ,				lowercase_      :       List[str]      ):







    '''simple docstring'''


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





  def 		lowerCamelCase__  (		self      :       Optional[int]      ):







    '''simple docstring'''


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






@require_torch
class       _a    (		__a ,				__a ,				unittest.TestCase      ):


  """simple docstring"""

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




  def 		lowerCamelCase__  (		self      :       int   ,				lowercase_      :       Any   ,				lowercase_      :       str   ,				lowercase_      :       Optional[int]   ,				lowercase_      :       List[str]   ,				lowercase_      :       List[str]      ):







    '''simple docstring'''


    if pipeline_test_casse_name == "AudioClassificationPipelineTests":
      return True

    return False




  def 		lowerCamelCase__  (		self      :       Union[str, Any]      ):







    '''simple docstring'''


    lowercase_     =       ASTModelTester(self      )
    lowercase_     =       ConfigTester(self   ,				config_class=lowercase_   ,				has_text_modality=lowercase_   ,				hidden_size=37      )




  def 		lowerCamelCase__  (		self      :       str      ):







    '''simple docstring'''


    self.config_tester.run_common_tests()




  @unittest.skip(reason="""AST does not use inputs_embeds"""      )
  def 		lowerCamelCase__  (		self      :       List[Any]      ):







    '''simple docstring'''


    pass




  def 		lowerCamelCase__  (		self      :       Optional[Any]      ):







    '''simple docstring'''


    lowercase_     ,	lowercase_     =       self.model_tester.prepare_config_and_inputs_for_common()

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




  def 		lowerCamelCase__  (		self      :       Union[str, Any]      ):







    '''simple docstring'''


    lowercase_     ,	lowercase_     =       self.model_tester.prepare_config_and_inputs_for_common()

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

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




  def 		lowerCamelCase__  (		self      :       Dict      ):







    '''simple docstring'''


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





  @slow
  def 		lowerCamelCase__  (		self      :       str      ):







    '''simple docstring'''


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






def   A_      (       )			->Tuple:
  lowercase_     =       hf_hub_download(
      repo_id="""nielsr/audio-spectogram-transformer-checkpoint""" ,	filename="""sample_audio.flac""" ,	repo_type="""dataset"""						)

  lowercase_     ,	lowercase_     =       torchaudio.load(SCREAMING_SNAKE_CASE_						)

  return audio, sampling_rate





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


  """simple docstring"""

  @cached_property
  def 		lowerCamelCase__  (		self      :       str      ):







    '''simple docstring'''


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





  @slow
  def 		lowerCamelCase__  (		self      :       List[Any]      ):







    '''simple docstring'''


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

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

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

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

    lowercase_     =       torch.tensor([-0.8_7_6_0, -7.0_0_4_2, -8.6_6_0_2]      ).to(lowercase_      )

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


'''simple docstring'''


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


__snake_case						  =		logging.get_logger(__name__)

__snake_case						  =		{
    """uw-madison/mra-base-512-4""": """https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json""",
}





class       _a    (		__a      ):


  """simple docstring"""

  A_    							=						'''mra'''




  def __init__(		self      :       Any   ,				lowercase_      :       List[str]=50_265   ,				lowercase_      :       Tuple=768   ,				lowercase_      :       Optional[Any]=12   ,				lowercase_      :       str=12   ,				lowercase_      :       Optional[int]=3_072   ,				lowercase_      :       Union[str, Any]="gelu"   ,				lowercase_      :       str=0.1   ,				lowercase_      :       Tuple=0.1   ,				lowercase_      :       str=512   ,				lowercase_      :       Optional[int]=1   ,				lowercase_      :       Tuple=0.0_2   ,				lowercase_      :       List[Any]=1e-5   ,				lowercase_      :       Optional[Any]="absolute"   ,				lowercase_      :       int=4   ,				lowercase_      :       int="full"   ,				lowercase_      :       Tuple=0   ,				lowercase_      :       str=0   ,				lowercase_      :       List[str]=1   ,				lowercase_      :       Tuple=0   ,				lowercase_      :       Optional[Any]=2   ,				**lowercase_      :       List[str]   ,				):







    '''simple docstring'''


    super().__init__(pad_token_id=lowercase_   ,				bos_token_id=lowercase_   ,				eos_token_id=lowercase_   ,				**lowercase_      )

    lowercase_     =       vocab_size
    lowercase_     =       max_position_embeddings
    lowercase_     =       hidden_size
    lowercase_     =       num_hidden_layers
    lowercase_     =       num_attention_heads
    lowercase_     =       intermediate_size
    lowercase_     =       hidden_act
    lowercase_     =       hidden_dropout_prob
    lowercase_     =       attention_probs_dropout_prob
    lowercase_     =       initializer_range
    lowercase_     =       type_vocab_size
    lowercase_     =       layer_norm_eps
    lowercase_     =       position_embedding_type
    lowercase_     =       block_per_row
    lowercase_     =       approx_mode
    lowercase_     =       initial_prior_first_n_blocks
    lowercase_     =       initial_prior_diagonal_n_blocks

