
"""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				,   lowercase_				,   lowercase_=13				,   lowercase_=2				,   lowercase_=24				,   lowercase_=16				,   lowercase_=True				,   lowercase_=True				,   lowercase_=32				,   lowercase_=5				,   lowercase_=4				,   lowercase_=37				,   lowercase_="gelu"				,   lowercase_=0.1				,   lowercase_=0.1				,   lowercase_=10				,   lowercase_=0.02				,   lowercase_=None				,   lowercase_=2				,   lowercase_=2				,   ):

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

     # in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
     UpperCAmelCase_					:					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_					:					Optional[int]			     =    frequency_out_dimension * time_out_dimension
     UpperCAmelCase_					:					Union[str, Any]			     =    num_patches + 2







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Optional[Any]			     =    floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]  )

     UpperCAmelCase_					:					Optional[Any]			     =    None
     if self.use_labels:
        UpperCAmelCase_					:					Dict			     =    ids_tensor([self.batch_size]				,   self.type_sequence_label_size  )

     UpperCAmelCase_					:					Optional[Any]			     =    self.get_config()

     return config, input_values, labels







  def        UpperCamelCase__					(		self  ):

     """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        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					Dict			     =    ASTModel(config=lowercase_  )
     model.to(lowercase_  )
     model.eval()
     UpperCAmelCase_					:					str			     =    model(lowercase_  )
     self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Union[str, Any]			     =    self.prepare_config_and_inputs()
     (
         (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   
     )					:					Optional[int]			     =    config_and_inputs
     UpperCAmelCase_					:					str			     =    {"input_values": input_values}
     return config, inputs_dict




@require_torch
class A_	(lowercase__				,lowercase__				,unittest.TestCase      ):




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







  def        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_  ):

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

     return False







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Optional[int]			     =    ASTModelTester(self  )
     UpperCAmelCase_					:					Tuple			     =    ConfigTester(self				,   config_class=lowercase_				,   has_text_modality=lowercase_				,   hidden_size=37  )







  def        UpperCamelCase__					(		self  ):

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







  @unittest.skip(reason="AST does not use inputs_embeds"  )
  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     pass







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_     ,   UpperCAmelCase_					:					Union[str, Any]			     =    self.model_tester.prepare_config_and_inputs_for_common()

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







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_     ,   UpperCAmelCase_					:					List[Any]			     =    self.model_tester.prepare_config_and_inputs_for_common()

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

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







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Union[str, Any]			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_  )







  @slow
  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCAmelCase_					:					Optional[Any]			     =    ASTModel.from_pretrained(lowercase_  )
        self.assertIsNotNone(lowercase_  )







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

   UpperCAmelCase_     ,   UpperCAmelCase_					:					Any			     =    torchaudio.load(__lowerCamelCase  )

   return audio, sampling_rate


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




  '''simple docstring'''







  @cached_property
  def        UpperCamelCase__					(		self  ):

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







  @slow
  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					str			     =    self.default_feature_extractor
     UpperCAmelCase_					:					str			     =    ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"  ).to(lowercase_  )

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

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

     # verify the logits
     UpperCAmelCase_					:					List[Any]			     =    torch.Size((1, 527)  )
     self.assertEqual(outputs.logits.shape				,   lowercase_  )

     UpperCAmelCase_					:					List[Any]			     =    torch.tensor([-0.87_60, -7.00_42, -8.66_02]  ).to(lowercase_  )

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









import os
import unittest

from transformers.models.cpmant.tokenization_cpmant import VOCAB_FILES_NAMES, CpmAntTokenizer
from transformers.testing_utils import require_jieba, tooslow

from ...test_tokenization_common import TokenizerTesterMixin

@require_jieba
class 		SCREAMING_SNAKE_CASE__				(       _UpperCAmelCase						,      unittest.TestCase  ):
      A_						:				List[str]							=    CpmAntTokenizer
      A_						:				Optional[int]							=    False


      def    a (self       :      Optional[int]      ):





         """simple docstring"""



         super().setUp()

         __snake_case							       =   [
             '''<d>''',
             '''</d>''',
             '''<s>''',
             '''</s>''',
             '''</_>''',
             '''<unk>''',
             '''<pad>''',
             '''</n>''',
             '''我''',
             '''是''',
             '''C''',
             '''P''',
             '''M''',
             '''A''',
             '''n''',
             '''t''',
         ]
         __snake_case							       =   os.path.join(self.tmpdirname				,			VOCAB_FILES_NAMES['''vocab_file''']      )
         with open(self.vocab_file				,			'''w'''				,			encoding='''utf-8'''      ) as vocab_writer:
            vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens]      )      )


      @tooslow
      def    a (self       :      Dict      ):





         """simple docstring"""



         __snake_case							       =   CpmAntTokenizer.from_pretrained('''openbmb/cpm-ant-10b'''      )
         __snake_case							       =   '''今天天气真好！'''
         __snake_case							       =   ['''今天''', '''天气''', '''真''', '''好''', '''！''']
         __snake_case							       =   tokenizer.tokenize(a__      )
         self.assertListEqual(a__				,			a__      )
         __snake_case							       =   '''今天天气真好！'''
         __snake_case							       =   [tokenizer.bos_token] + tokens

         __snake_case							       =   [6, 9802, 1_4962, 2082, 831, 244]
         self.assertListEqual(tokenizer.convert_tokens_to_ids(a__      )				,			a__      )

         __snake_case							       =   tokenizer.decode(a__      )
         self.assertEqual(a__				,			a__      )

