



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       :



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


           '''simple docstring'''

           __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 		_lowerCamelCase			(		self							:     Union[str, Any]):


           '''simple docstring'''

           __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 		_lowerCamelCase			(		self							:     Any):


           '''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=__SCREAMING_SNAKE_CASE     ,		initializer_range=self.initializer_range     ,		frequency_stride=self.frequency_stride     ,		time_stride=self.time_stride     ,		)



       def 		_lowerCamelCase			(		self							:     Optional[int]     ,		__SCREAMING_SNAKE_CASE							:     List[Any]     ,		__SCREAMING_SNAKE_CASE							:     int     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]):


           '''simple docstring'''

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





       def 		_lowerCamelCase			(		self							:     str):


           '''simple docstring'''

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


@require_torch
class     _A       (	__UpperCAmelCase      ,__UpperCAmelCase      ,unittest.TestCase    ):
       UpperCamelCase__  :    Dict											=						(
           (
               ASTModel,
               ASTForAudioClassification,
           )
           if is_torch_available()
           else ()
       )
       UpperCamelCase__  :    int											=						(
           {'''audio-classification''': ASTForAudioClassification, '''feature-extraction''': ASTModel}
           if is_torch_available()
           else {}
       )
       UpperCamelCase__  :    str											=						False
       UpperCamelCase__  :    Optional[int]											=						False
       UpperCamelCase__  :    List[str]											=						False
       UpperCamelCase__  :    Optional[int]											=						False



       def 		_lowerCamelCase			(		self							:     Optional[int]     ,		__SCREAMING_SNAKE_CASE							:     str     ,		__SCREAMING_SNAKE_CASE							:     List[str]     ,		__SCREAMING_SNAKE_CASE							:     str     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]):


           '''simple docstring'''

           if pipeline_test_casse_name == "AudioClassificationPipelineTests":
               return True

           return False



       def 		_lowerCamelCase			(		self							:     List[Any]):


           '''simple docstring'''

           __a						       =				ASTModelTester(self)
           __a						       =				ConfigTester(self     ,		config_class=__SCREAMING_SNAKE_CASE     ,		has_text_modality=__SCREAMING_SNAKE_CASE     ,		hidden_size=37)



       def 		_lowerCamelCase			(		self							:     Dict):


           '''simple docstring'''

           self.config_tester.run_common_tests()



       @unittest.skip(reason='''AST does not use inputs_embeds''')
       def 		_lowerCamelCase			(		self							:     Dict):


           '''simple docstring'''

           pass



       def 		_lowerCamelCase			(		self							:     List[Any]):


           '''simple docstring'''

           __a       ,   __a						       =				self.model_tester.prepare_config_and_inputs_for_common()

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



       def 		_lowerCamelCase			(		self							:     List[str]):


           '''simple docstring'''

           __a       ,   __a						       =				self.model_tester.prepare_config_and_inputs_for_common()

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



       def 		_lowerCamelCase			(		self							:     int):


           '''simple docstring'''

           __a						       =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE)





       @slow
       def 		_lowerCamelCase			(		self							:     Dict):


           '''simple docstring'''

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





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

    __a       ,   __a						       =				torchaudio.load(_UpperCAmelCase				)

    return audio, sampling_rate

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



       @cached_property
       def 		_lowerCamelCase			(		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 		_lowerCamelCase			(		self							:     Dict):


           '''simple docstring'''

           __a						       =				self.default_feature_extractor
           __a						       =				ASTForAudioClassification.from_pretrained('''MIT/ast-finetuned-audioset-10-10-0.4593''').to(__SCREAMING_SNAKE_CASE)

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

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

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

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

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












import os
import unittest

from transformers.models.bartpho.tokenization_bartpho import VOCAB_FILES_NAMES, BartphoTokenizer
from transformers.testing_utils import get_tests_dir

from ...test_tokenization_common import TokenizerTesterMixin


snake_case_				=			get_tests_dir('fixtures/test_sentencepiece_bpe.model')

class 		SCREAMING_SNAKE_CASE__				(       _UpperCAmelCase						,      unittest.TestCase  ):
      A_						:				List[Any]							=    BartphoTokenizer
      A_						:				List[str]							=    False
      A_						:				Optional[Any]							=    True


      def    a (self       :      Tuple      ):





         """simple docstring"""



         super().setUp()

         __snake_case							       =   ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']
         __snake_case							       =   dict(zip(a__				,			range(len(a__      )      )      )      )
         __snake_case							       =   {'''unk_token''': '''<unk>'''}

         __snake_case							       =   os.path.join(self.tmpdirname				,			VOCAB_FILES_NAMES['''monolingual_vocab_file''']      )
         with open(self.monolingual_vocab_file				,			'''w'''				,			encoding='''utf-8'''      ) as fp:
            for token in vocab_tokens:
               fp.write(f"""{token} {vocab_tokens[token]}\n"""      )

         __snake_case							       =   BartphoTokenizer(a__				,			self.monolingual_vocab_file				,			**self.special_tokens_map      )
         tokenizer.save_pretrained(self.tmpdirname      )


      def    a (self       :      str				,			**a__       :      str      ):





         """simple docstring"""



         kwargs.update(self.special_tokens_map      )
         return BartphoTokenizer.from_pretrained(self.tmpdirname				,			**a__      )


      def    a (self       :      str				,			a__       :      Any      ):





         """simple docstring"""



         __snake_case							       =   '''This is a là test'''
         __snake_case							       =   '''This is a<unk><unk> test'''
         return input_text, output_text


      def    a (self       :      Dict      ):





         """simple docstring"""



         __snake_case							       =   BartphoTokenizer(a__				,			self.monolingual_vocab_file				,			**self.special_tokens_map      )
         __snake_case							       =   '''This is a là test'''
         __snake_case							       =   '''▁This ▁is ▁a ▁l à ▁t est'''.split()
         __snake_case							       =   tokenizer.tokenize(a__      )
         self.assertListEqual(a__				,			a__      )

         __snake_case							       =   tokens + [tokenizer.unk_token]
         __snake_case							       =   [4, 5, 6, 3, 3, 7, 8, 3]
         self.assertListEqual(tokenizer.convert_tokens_to_ids(a__      )				,			a__      )

