


import json
import os
import unittest

from transformers import DebertaTokenizer, DebertaTokenizerFast
from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin





class 						__SCREAMING_SNAKE_CASE					(      lowerCamelCase__ ,			unittest.TestCase ):
     UpperCAmelCase 					=				DebertaTokenizer
     UpperCAmelCase 					=				True
     UpperCAmelCase 					=				DebertaTokenizerFast






     def  a_					(			self		)       ->    List[Any]:
          super().setUp()

          # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
          _a						=		[
              "l",
              "o",
              "w",
              "e",
              "r",
              "s",
              "t",
              "i",
              "d",
              "n",
              "\u0120",
              "\u0120l",
              "\u0120n",
              "\u0120lo",
              "\u0120low",
              "er",
              "\u0120lowest",
              "\u0120newer",
              "\u0120wider",
              "[UNK]",
          ]
          _a						=		dict(zip(__UpperCamelCase     ,							range(len(__UpperCamelCase		)		)		)		)
          _a						=		["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""]
          _a						=		{"unk_token": "[UNK]"}

          _a						=		os.path.join(self.tmpdirname     ,							VOCAB_FILES_NAMES["vocab_file"]		)
          _a						=		os.path.join(self.tmpdirname     ,							VOCAB_FILES_NAMES["merges_file"]		)
          with open(self.vocab_file     ,							"w"     ,							encoding="utf-8"		) as fp:
               fp.write(json.dumps(__UpperCamelCase		) + "\n"		)
          with open(self.merges_file     ,							"w"     ,							encoding="utf-8"		) as fp:
               fp.write("\n".join(__UpperCamelCase		)		)






     def  a_					(			self     ,							**__UpperCamelCase		)       ->    List[str]:
          kwargs.update(self.special_tokens_map		)
          return self.tokenizer_class.from_pretrained(self.tmpdirname     ,							**__UpperCamelCase		)






     def  a_					(			self     ,							__UpperCamelCase		)       ->    Any:
          _a						=		"lower newer"
          _a						=		"lower newer"
          return input_text, output_text






     def  a_					(			self		)       ->    List[str]:
          _a						=		self.get_tokenizer()
          _a						=		"lower newer"
          _a						=		["l", "o", "w", "er", "\u0120", "n", "e", "w", "er"]
          _a						=		tokenizer.tokenize(__UpperCamelCase		)
          self.assertListEqual(__UpperCamelCase     ,							__UpperCamelCase		)

          _a						=		tokens + [tokenizer.unk_token]
          _a						=		[0, 1, 2, 15, 10, 9, 3, 2, 15, 19]
          self.assertListEqual(tokenizer.convert_tokens_to_ids(__UpperCamelCase		)     ,							__UpperCamelCase		)






     def  a_					(			self		)       ->    Optional[int]:
          _a						=		self.get_tokenizer()
          _a						=		tokenizer("Hello"     ,							"World"		)
          _a						=		[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
          self.assertListEqual(tokd["token_type_ids"]     ,							__UpperCamelCase		)






     @slow
     def  a_					(			self		)       ->    Dict:
          _a						=		self.tokenizer_class.from_pretrained("microsoft/deberta-base"		)

          _a						=		tokenizer.encode("sequence builders"     ,							add_special_tokens=__UpperCamelCase		)
          _a						=		tokenizer.encode("multi-sequence build"     ,							add_special_tokens=__UpperCamelCase		)

          _a						=		tokenizer.encode(
              "sequence builders"     ,							add_special_tokens=__UpperCamelCase     ,							add_prefix_space=__UpperCamelCase		)
          _a						=		tokenizer.encode(
              "sequence builders"     ,							"multi-sequence build"     ,							add_special_tokens=__UpperCamelCase     ,							add_prefix_space=__UpperCamelCase		)

          _a						=		tokenizer.build_inputs_with_special_tokens(__UpperCamelCase		)
          _a						=		tokenizer.build_inputs_with_special_tokens(__UpperCamelCase     ,							__UpperCamelCase		)

          assert encoded_sentence == encoded_text_from_decode
          assert encoded_pair == encoded_pair_from_decode






     @slow
     def  a_					(			self		)       ->    Optional[int]:
          _a						=		[self.tokenizer_class]
          if self.test_rust_tokenizer:
               tokenizer_classes.append(self.rust_tokenizer_class		)

          for tokenizer_class in tokenizer_classes:
               _a						=		tokenizer_class.from_pretrained("microsoft/deberta-base"		)

               _a						=		[
                   "ALBERT: A Lite BERT for Self-supervised Learning of Language Representations",
                   "ALBERT incorporates two parameter reduction techniques",
                   "The first one is a factorized embedding parameterization. By decomposing the large vocabulary"
                   " embedding matrix into two small matrices, we separate the size of the hidden layers from the size of"
                   " vocabulary embedding.",
               ]

               _a						=		tokenizer(__UpperCamelCase     ,							padding=__UpperCamelCase		)
               _a						=		[tokenizer.decode(__UpperCamelCase     ,							skip_special_tokens=__UpperCamelCase		) for seq in encoding["input_ids"]]

               # fmt: off
               _a						=		{
                   "input_ids": [
                       [1, 2_118, 11_126, 565, 35, 83, 25_191, 163, 18_854, 13, 12_156, 12, 16_101, 25_376, 13_807, 9, 22_205, 27_893, 1_635, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [1, 2_118, 11_126, 565, 24_536, 80, 43_797, 4_878, 7_373, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [1, 133, 78, 65, 16, 10, 3_724, 1_538, 33_183, 11_303, 43_797, 1_938, 4, 870, 24_165, 29_105, 5, 739, 32_644, 33_183, 11_303, 36_173, 88, 80, 650, 7_821, 45_940, 6, 52, 2_559, 5, 1_836, 9, 5, 7_397, 13_171, 31, 5, 1_836, 9, 32_644, 33_183, 11_303, 4, 2]
                   ],
                   "token_type_ids": [
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
                   ],
                   "attention_mask": [
                       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
                   ]
               }
               # fmt: on

               _a						=		[
                   "ALBERT: A Lite BERT for Self-supervised Learning of Language Representations",
                   "ALBERT incorporates two parameter reduction techniques",
                   "The first one is a factorized embedding parameterization. By decomposing the large vocabulary"
                   " embedding matrix into two small matrices, we separate the size of the hidden layers from the size of"
                   " vocabulary embedding.",
               ]

               self.assertDictEqual(encoding.data     ,							__UpperCamelCase		)

               for expected, decoded in zip(__UpperCamelCase     ,							__UpperCamelCase		):
                    self.assertEqual(__UpperCamelCase     ,							__UpperCamelCase		)





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






     def __init__(			self     ,							__UpperCamelCase     ,							__UpperCamelCase=13     ,							__UpperCamelCase=2     ,							__UpperCamelCase=24     ,							__UpperCamelCase=16     ,							__UpperCamelCase=True     ,							__UpperCamelCase=True     ,							__UpperCamelCase=32     ,							__UpperCamelCase=5     ,							__UpperCamelCase=4     ,							__UpperCamelCase=37     ,							__UpperCamelCase="gelu"     ,							__UpperCamelCase=0.1     ,							__UpperCamelCase=0.1     ,							__UpperCamelCase=10     ,							__UpperCamelCase=0.02     ,							__UpperCamelCase=None     ,							__UpperCamelCase=2     ,							__UpperCamelCase=2     ,							)       ->    Optional[int]:
          _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		)       ->    List[str]:
          _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		)       ->    Optional[int]:
          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=__UpperCamelCase     ,							initializer_range=self.initializer_range     ,							frequency_stride=self.frequency_stride     ,							time_stride=self.time_stride     ,							)






     def  a_					(			self     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase		)       ->    Optional[Any]:
          _a						=		ASTModel(config=__UpperCamelCase		)
          model.to(__UpperCamelCase		)
          model.eval()
          _a						=		model(__UpperCamelCase		)
          self.parent.assertEqual(result.last_hidden_state.shape     ,							(self.batch_size, self.seq_length, self.hidden_size)		)






     def  a_					(			self		)       ->    str:
          _a						=		self.prepare_config_and_inputs()
          (
              (
              _a
          )			,			(
              _a
          )			,			(
              _a
          )			,			
          )						=		config_and_inputs
          _a						=		{"input_values": input_values}
          return config, inputs_dict







@require_torch
class 						__SCREAMING_SNAKE_CASE					(      lowerCamelCase__ ,			lowerCamelCase__ ,			unittest.TestCase ):
     UpperCAmelCase 					=				(
         (
             ASTModel,
             ASTForAudioClassification,
         )
         if is_torch_available()
         else ()
     )
     UpperCAmelCase 					=				(
         {'''audio-classification''': ASTForAudioClassification, '''feature-extraction''': ASTModel}
         if is_torch_available()
         else {}
     )
     UpperCAmelCase 					=				False
     UpperCAmelCase 					=				False
     UpperCAmelCase 					=				False
     UpperCAmelCase 					=				False






     def  a_					(			self     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase		)       ->    List[str]:
          if pipeline_test_casse_name == "AudioClassificationPipelineTests":
               return True

          return False






     def  a_					(			self		)       ->    int:
          _a						=		ASTModelTester(self		)
          _a						=		ConfigTester(self     ,							config_class=__UpperCamelCase     ,							has_text_modality=__UpperCamelCase     ,							hidden_size=37		)






     def  a_					(			self		)       ->    str:
          self.config_tester.run_common_tests()






     @unittest.skip(reason="AST does not use inputs_embeds"		)
     def  a_					(			self		)       ->    Union[str, Any]:
          pass






     def  a_					(			self		)       ->    List[Any]:
          _a			,			_a						=		self.model_tester.prepare_config_and_inputs_for_common()

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






     def  a_					(			self		)       ->    Any:
          _a			,			_a						=		self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               _a						=		model_class(__UpperCamelCase		)
               _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]     ,							__UpperCamelCase		)






     def  a_					(			self		)       ->    int:
          _a						=		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__UpperCamelCase		)






     @slow
     def  a_					(			self		)       ->    Optional[Any]:
          for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _a						=		ASTModel.from_pretrained(__UpperCamelCase		)
               self.assertIsNotNone(__UpperCamelCase		)







def __UpperCamelCase   (			)      ->     Dict:







     '''simple docstring'''






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

     _a			,			_a						=		torchaudio.load(__lowerCamelCase				)

     return audio, sampling_rate






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






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






     @slow
     def  a_					(			self		)       ->    Dict:
          _a						=		self.default_feature_extractor
          _a						=		ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"		).to(__UpperCamelCase		)

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

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

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

          _a						=		torch.tensor([-0.87_60, -7.00_42, -8.66_02]		).to(__UpperCamelCase		)

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

