

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

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






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

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

  def    __lowercase       (  self		:			Any    ):






   '''simple docstring'''
   UpperCAmelCase__					:  int					   =					floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]    )

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

   UpperCAmelCase__					:  Tuple					   =					self.get_config()

   return config, input_values, labels

  def    __lowercase       (  self		:			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		:			Optional[Any]					,A		:			Union[str, Any]					,A		:			str					,A		:			Union[str, Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Optional[Any]					   =					ASTModel(config=A    )
   model.to(A    )
   model.eval()
   UpperCAmelCase__					:  Optional[int]					   =					model(A    )
   self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, self.seq_length, self.hidden_size)    )




  def    __lowercase       (  self		:			Optional[int]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  int					   =					self.prepare_config_and_inputs()
   (
       (
       UpperCAmelCase__
   )   ,      (
       UpperCAmelCase__
   )   ,      (
       UpperCAmelCase__
   )   ,      
   )					:  Any					   =					config_and_inputs
   UpperCAmelCase__					:  Tuple					   =					{"""input_values""": input_values}
   return config, inputs_dict







@require_torch
class      __lowercase			(    __lowerCamelCase	,			__lowerCamelCase	,			unittest.TestCase					):
  snake_case_				       =       (
      (
          ASTModel,
          ASTForAudioClassification,
      )
      if is_torch_available()
      else ()
  )
  snake_case_				       =       (
      {"""audio-classification""": ASTForAudioClassification, """feature-extraction""": ASTModel}
      if is_torch_available()
      else {}
  )
  snake_case_				       =       False
  snake_case_				       =       False
  snake_case_				       =       False
  snake_case_				       =       False

  def    __lowercase       (  self		:			List[Any]					,A		:			List[str]					,A		:			Union[str, Any]					,A		:			str					,A		:			int					,A		:			Optional[Any]    ):






   '''simple docstring'''
   if pipeline_test_casse_name == "AudioClassificationPipelineTests":
    return True

   return False

  def    __lowercase       (  self		:			Dict    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Dict					   =					ASTModelTester(self    )
   UpperCAmelCase__					:  int					   =					ConfigTester(self					,config_class=A					,has_text_modality=A					,hidden_size=37    )

  def    __lowercase       (  self		:			str    ):






   '''simple docstring'''
   self.config_tester.run_common_tests()

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






   '''simple docstring'''
   pass

  def    __lowercase       (  self		:			int    ):






   '''simple docstring'''
   UpperCAmelCase__   ,      UpperCAmelCase__					:  Tuple					   =					self.model_tester.prepare_config_and_inputs_for_common()

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

  def    __lowercase       (  self		:			int    ):






   '''simple docstring'''
   UpperCAmelCase__   ,      UpperCAmelCase__					:  str					   =					self.model_tester.prepare_config_and_inputs_for_common()

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

    UpperCAmelCase__					:  int					   =					["""input_values"""]
    self.assertListEqual(arg_names[:1]					,A    )

  def    __lowercase       (  self		:			Any    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Optional[int]					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*A    )




  @slow
  def    __lowercase       (  self		:			Union[str, Any]    ):






   '''simple docstring'''
   for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    UpperCAmelCase__					:  List[Any]					   =					ASTModel.from_pretrained(A    )
    self.assertIsNotNone(A    )







def   lowerCAmelCase						(					):
 '''simple docstring'''





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

 UpperCAmelCase__   ,      UpperCAmelCase__					:  Union[str, Any]					   =					torchaudio.load(__UpperCamelCase      )

 return audio, sampling_rate







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

  @cached_property
  def    __lowercase       (  self		:			Optional[int]    ):






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




  @slow
  def    __lowercase       (  self		:			str    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[str]					   =					self.default_feature_extractor
   UpperCAmelCase__					:  Union[str, Any]					   =					ASTForAudioClassification.from_pretrained("""MIT/ast-finetuned-audioset-10-10-0.4593"""    ).to(A    )

   UpperCAmelCase__					:  Dict					   =					self.default_feature_extractor
   UpperCAmelCase__   ,      UpperCAmelCase__					:  Dict					   =					prepare_audio()
   UpperCAmelCase__					:  List[Any]					   =					audio.squeeze().numpy()
   UpperCAmelCase__					:  str					   =					feature_extractor(A					,sampling_rate=A					,return_tensors="""pt"""    ).to(A    )

   # forward pass
   with torch.no_grad():
    UpperCAmelCase__					:  Optional[Any]					   =					model(**A    )

   # verify the logits
   UpperCAmelCase__					:  Dict					   =					torch.Size((1, 527)    )
   self.assertEqual(outputs.logits.shape					,A    )

   UpperCAmelCase__					:  Tuple					   =					torch.tensor([-0.8_7_6_0, -7.0_0_4_2, -8.6_6_0_2]    ).to(A    )

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








"""simple docstring"""




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


__UpperCAmelCase =				logging.get_logger(__name__)

__UpperCAmelCase =				{
    'microsoft/markuplm-base': 'https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json',
    'microsoft/markuplm-large': 'https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json',
}







class      __lowercase			(    __lowerCamelCase					):
  snake_case_				       =       """markuplm"""

  def __init__(  self		:			str					,A		:			List[Any]=30_522					,A		:			Tuple=768					,A		:			str=12					,A		:			int=12					,A		:			int=3_072					,A		:			Optional[int]="gelu"					,A		:			Optional[int]=0.1					,A		:			Optional[int]=0.1					,A		:			Any=512					,A		:			Any=2					,A		:			str=0.0_2					,A		:			int=1e-12					,A		:			int=0					,A		:			str=0					,A		:			List[Any]=2					,A		:			List[str]=256					,A		:			Union[str, Any]=1_024					,A		:			List[Any]=216					,A		:			Any=1_001					,A		:			Optional[int]=32					,A		:			Any=50					,A		:			int="absolute"					,A		:			Dict=True					,A		:			int=None					,**A		:			Optional[int]					,):






   '''simple docstring'''
   super().__init__(
       pad_token_id=A					,bos_token_id=A					,eos_token_id=A					,**A					,)
   UpperCAmelCase__					:  Dict					   =					vocab_size
   UpperCAmelCase__					:  Union[str, Any]					   =					hidden_size
   UpperCAmelCase__					:  Optional[int]					   =					num_hidden_layers
   UpperCAmelCase__					:  Tuple					   =					num_attention_heads
   UpperCAmelCase__					:  Optional[Any]					   =					hidden_act
   UpperCAmelCase__					:  Any					   =					intermediate_size
   UpperCAmelCase__					:  Dict					   =					hidden_dropout_prob
   UpperCAmelCase__					:  Optional[Any]					   =					attention_probs_dropout_prob
   UpperCAmelCase__					:  Optional[int]					   =					max_position_embeddings
   UpperCAmelCase__					:  Dict					   =					type_vocab_size
   UpperCAmelCase__					:  Optional[Any]					   =					initializer_range
   UpperCAmelCase__					:  str					   =					layer_norm_eps
   UpperCAmelCase__					:  Any					   =					position_embedding_type
   UpperCAmelCase__					:  int					   =					use_cache
   UpperCAmelCase__					:  List[str]					   =					classifier_dropout
   # additional properties
   UpperCAmelCase__					:  Optional[int]					   =					max_depth
   UpperCAmelCase__					:  List[str]					   =					max_xpath_tag_unit_embeddings
   UpperCAmelCase__					:  Any					   =					max_xpath_subs_unit_embeddings
   UpperCAmelCase__					:  str					   =					tag_pad_id
   UpperCAmelCase__					:  Dict					   =					subs_pad_id
   UpperCAmelCase__					:  List[str]					   =					xpath_unit_hidden_size





