



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







    def __init__(				self			:  Dict						,      __snake_case			:  Optional[int]						,      __snake_case			:  Optional[Any]=13						,      __snake_case			:  List[Any]=2						,      __snake_case			:  Dict=24						,      __snake_case			:  int=16						,      __snake_case			:  Any=True						,      __snake_case			:  str=True						,      __snake_case			:  Any=32						,      __snake_case			:  str=5						,      __snake_case			:  Tuple=4						,      __snake_case			:  Tuple=37						,      __snake_case			:  Tuple="gelu"						,      __snake_case			:  List[str]=0.1						,      __snake_case			:  Tuple=0.1						,      __snake_case			:  Dict=10						,      __snake_case			:  List[Any]=0.02						,      __snake_case			:  Optional[Any]=None						,      __snake_case			:  Union[str, Any]=2						,      __snake_case			:  Any=2						,      ):
      a						: str					       =					parent
      a						: Optional[int]					       =					batch_size
      a						: Optional[Any]					       =					patch_size
      a						: Tuple					       =					max_length
      a						: Tuple					       =					num_mel_bins
      a						: Tuple					       =					is_training
      a						: Dict					       =					use_labels
      a						: Optional[int]					       =					hidden_size
      a						: Optional[int]					       =					num_hidden_layers
      a						: Union[str, Any]					       =					num_attention_heads
      a						: Union[str, Any]					       =					intermediate_size
      a						: Optional[Any]					       =					hidden_act
      a						: List[str]					       =					hidden_dropout_prob
      a						: Any					       =					attention_probs_dropout_prob
      a						: Union[str, Any]					       =					type_sequence_label_size
      a						: str					       =					initializer_range
      a						: Tuple					       =					scope
      a						: int					       =					frequency_stride
      a						: Dict					       =					time_stride

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







    def       lowercase_	(				self			:  int     ):
      a						: str					       =					floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]     )

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

      a						: str					       =					self.get_config()

      return config, input_values, labels







    def       lowercase_	(				self			:  Tuple     ):
      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=__snake_case						,      initializer_range=self.initializer_range						,      frequency_stride=self.frequency_stride						,      time_stride=self.time_stride						,      )







    def       lowercase_	(				self			:  Optional[int]						,      __snake_case			:  Dict						,      __snake_case			:  str						,      __snake_case			:  int     ):
      a						: Optional[int]					       =					ASTModel(config=__snake_case     )
      model.to(__snake_case     )
      model.eval()
      a						: Optional[int]					       =					model(__snake_case     )
      self.parent.assertEqual(result.last_hidden_state.shape						,      (self.batch_size, self.seq_length, self.hidden_size)     )







    def       lowercase_	(				self			:  Tuple     ):
      a						: str					       =					self.prepare_config_and_inputs()
      (
          (
          a
      )       ,     (
          a
      )       ,     (
          a
      )       ,     
      )						: Optional[int]					       =					config_and_inputs
      a						: Any					       =					{'input_values': input_values}
      return config, inputs_dict

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







    def       lowercase_	(				self			:  Dict						,      __snake_case			:  Union[str, Any]						,      __snake_case			:  Optional[Any]						,      __snake_case			:  Any						,      __snake_case			:  Optional[Any]						,      __snake_case			:  List[str]     ):
      if pipeline_test_casse_name == "AudioClassificationPipelineTests":
        return True

      return False







    def       lowercase_	(				self			:  str     ):
      a						: Union[str, Any]					       =					ASTModelTester(self     )
      a						: Optional[int]					       =					ConfigTester(self						,      config_class=__snake_case						,      has_text_modality=__snake_case						,      hidden_size=37     )







    def       lowercase_	(				self			:  Dict     ):
      self.config_tester.run_common_tests()







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







    def       lowercase_	(				self			:  List[str]     ):
      a       ,     a						: Union[str, Any]					       =					self.model_tester.prepare_config_and_inputs_for_common()

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







    def       lowercase_	(				self			:  List[str]     ):
      a       ,     a						: Optional[Any]					       =					self.model_tester.prepare_config_and_inputs_for_common()

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

        a						: int					       =					['input_values']
        self.assertListEqual(arg_names[:1]						,      __snake_case     )







    def       lowercase_	(				self			:  Union[str, Any]     ):
      a						: Tuple					       =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__snake_case     )







    @slow
    def       lowercase_	(				self			:  Optional[int]     ):
      for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a						: Optional[int]					       =					ASTModel.from_pretrained(__snake_case     )
        self.assertIsNotNone(__snake_case     )
def 				lowerCamelCase__	(						):
  a						: Optional[int]					       =					hf_hub_download(
      repo_id='nielsr/audio-spectogram-transformer-checkpoint'			,      filename='sample_audio.flac'			,      repo_type='dataset'    )

  a       ,     a						: List[Any]					       =					torchaudio.load(_A    )

  return audio, sampling_rate

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







    @cached_property
    def       lowercase_	(				self			:  Any     ):
      return (
          ASTFeatureExtractor.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593'     )
          if is_torchaudio_available()
          else None
      )







    @slow
    def       lowercase_	(				self			:  Any     ):
      a						: List[str]					       =					self.default_feature_extractor
      a						: Tuple					       =					ASTForAudioClassification.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593'     ).to(__snake_case     )

      a						: Dict					       =					self.default_feature_extractor
      a       ,     a						: str					       =					prepare_audio()
      a						: Any					       =					audio.squeeze().numpy()
      a						: Union[str, Any]					       =					feature_extractor(__snake_case						,      sampling_rate=__snake_case						,      return_tensors='pt'     ).to(__snake_case     )

      # forward pass
      with torch.no_grad():
        a						: Optional[int]					       =					model(**__snake_case     )

      # verify the logits
      a						: int					       =					torch.Size((1, 5_27)     )
      self.assertEqual(outputs.logits.shape						,      __snake_case     )

      a						: Union[str, Any]					       =					torch.tensor([-0.8760, -7.0042, -8.6602]     ).to(__snake_case     )

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




'''simple docstring'''






import json
import os
import unittest

from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin

class 							a__( lowerCamelCase__		,			unittest.TestCase     ):
    lowercase__			=   CTRLTokenizer
    lowercase__			=   False
    lowercase__			=   False







    def       lowercase_	(				self			:  Dict     ):
      super().setUp()

      # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
      a						: Tuple					       =					['adapt', 're@@', 'a@@', 'apt', 'c@@', 't', '<unk>']
      a						: Union[str, Any]					       =					dict(zip(__snake_case						,      range(len(__snake_case     )     )     )     )
      a						: Union[str, Any]					       =					['#version: 0.2', 'a p', 'ap t</w>', 'r e', 'a d', 'ad apt</w>', '']
      a						: Optional[Any]					       =					{'unk_token': '<unk>'}

      a						: Dict					       =					os.path.join(self.tmpdirname						,      VOCAB_FILES_NAMES['vocab_file']     )
      a						: Optional[Any]					       =					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(__snake_case     ) + '\n'     )
      with open(self.merges_file						,      'w'						,      encoding='utf-8'     ) as fp:
        fp.write('\n'.join(__snake_case     )     )







    def       lowercase_	(				self			:  int						,      **__snake_case			:  str     ):
      kwargs.update(self.special_tokens_map     )
      return CTRLTokenizer.from_pretrained(self.tmpdirname						,      **__snake_case     )







    def       lowercase_	(				self			:  Optional[int]						,      __snake_case			:  Any     ):
      a						: int					       =					'adapt react readapt apt'
      a						: Any					       =					'adapt react readapt apt'
      return input_text, output_text







    def       lowercase_	(				self			:  Dict     ):
      a						: Dict					       =					CTRLTokenizer(self.vocab_file						,      self.merges_file						,      **self.special_tokens_map     )
      a						: List[str]					       =					'adapt react readapt apt'
      a						: Dict					       =					'adapt re@@ a@@ c@@ t re@@ adapt apt'.split()
      a						: Any					       =					tokenizer.tokenize(__snake_case     )
      self.assertListEqual(__snake_case						,      __snake_case     )

      a						: Dict					       =					tokens + [tokenizer.unk_token]

      a						: Optional[Any]					       =					[0, 1, 2, 4, 5, 1, 0, 3, 6]
      self.assertListEqual(tokenizer.convert_tokens_to_ids(__snake_case     )						,      __snake_case     )