





"""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    _lowerCAmelCase  :
       """simple docstring"""




       def __init__(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase=1_3       ,					__UpperCAmelCase=2       ,					__UpperCAmelCase=2_4       ,					__UpperCAmelCase=1_6       ,					__UpperCAmelCase=True       ,					__UpperCAmelCase=True       ,					__UpperCAmelCase=3_2       ,					__UpperCAmelCase=5       ,					__UpperCAmelCase=4       ,					__UpperCAmelCase=3_7       ,					__UpperCAmelCase="gelu"       ,					__UpperCAmelCase=0.1       ,					__UpperCAmelCase=0.1       ,					__UpperCAmelCase=1_0       ,					__UpperCAmelCase=0.02       ,					__UpperCAmelCase=None       ,					__UpperCAmelCase=2       ,					__UpperCAmelCase=2       ,					):



         '''simple docstring'''

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

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






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :List[str]											=				floats_tensor([self.batch_size, self.max_length, self.num_mel_bins] )

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

         lowerCAmelCase__     :Tuple											=				self.get_config()

         return config, input_values, labels






       def 	snake_case		(       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=__UpperCAmelCase       ,					initializer_range=self.initializer_range       ,					frequency_stride=self.frequency_stride       ,					time_stride=self.time_stride       ,					)






       def 	snake_case		(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase       ,					__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :Tuple											=				ASTModel(config=__UpperCAmelCase )
         model.to(__UpperCAmelCase )
         model.eval()
         lowerCAmelCase__     :Union[str, Any]											=				model(__UpperCAmelCase )
         self.parent.assertEqual(result.last_hidden_state.shape       ,					(self.batch_size, self.seq_length, self.hidden_size) )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :List[str]											=				self.prepare_config_and_inputs()
         (
             (
             lowerCAmelCase__
         )				,							(
             lowerCAmelCase__
         )				,							(
             lowerCAmelCase__
         )				,							
         )     :List[Any]											=				config_and_inputs
         lowerCAmelCase__     :Any											=				{'input_values': input_values}
         return config, inputs_dict






@require_torch
class    _lowerCAmelCase  (	a    ,  a    ,  unittest.TestCase			):
       """simple docstring"""




       __magic_name__	:Dict						    =   (
           (
               ASTModel,
               ASTForAudioClassification,
           )
           if is_torch_available()
           else ()
       )
       __magic_name__	:str						    =   (
           {"""audio-classification""": ASTForAudioClassification, """feature-extraction""": ASTModel}
           if is_torch_available()
           else {}
       )
       __magic_name__	:str						    =   False
       __magic_name__	:Any						    =   False
       __magic_name__	:Optional[Any]						    =   False
       __magic_name__	:Dict						    =   False






       def 	snake_case		(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase       ,					__UpperCAmelCase       ,					__UpperCAmelCase       ,					__UpperCAmelCase ):



         '''simple docstring'''

         if pipeline_test_casse_name == "AudioClassificationPipelineTests":
           return True

         return False






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Any											=				ASTModelTester(self )
         lowerCAmelCase__     :List[str]											=				ConfigTester(self       ,					config_class=__UpperCAmelCase       ,					has_text_modality=__UpperCAmelCase       ,					hidden_size=3_7 )






       def 	snake_case		(       self ):



         '''simple docstring'''

         self.config_tester.run_common_tests()






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



         '''simple docstring'''

         pass






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__				,							lowerCAmelCase__     :Dict											=				self.model_tester.prepare_config_and_inputs_for_common()

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






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__				,							lowerCAmelCase__     :str											=				self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           lowerCAmelCase__     :Dict											=				model_class(__UpperCAmelCase )
           lowerCAmelCase__     :Union[str, Any]											=				inspect.signature(model.forward )
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           lowerCAmelCase__     :Dict											=				[*signature.parameters.keys()]

           lowerCAmelCase__     :Union[str, Any]											=				['input_values']
           self.assertListEqual(arg_names[:1]       ,					__UpperCAmelCase )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Union[str, Any]											=				self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__UpperCAmelCase )






       @slow
       def 	snake_case		(       self ):



         '''simple docstring'''

         for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowerCAmelCase__     :int											=				ASTModel.from_pretrained(__UpperCAmelCase )
           self.assertIsNotNone(__UpperCAmelCase )






def 					__A  ()   ->Any:





  """simple docstring"""


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

  lowerCAmelCase__				,							lowerCAmelCase__     :Optional[Any]											=				torchaudio.load(_SCREAMING_SNAKE_CASE     )

  return audio, sampling_rate






@require_torch
@require_torchaudio
class    _lowerCAmelCase  (	unittest.TestCase			):
       """simple docstring"""




       @cached_property
       def 	snake_case		(       self ):



         '''simple docstring'''

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






       @slow
       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[int]											=				self.default_feature_extractor
         lowerCAmelCase__     :Dict											=				ASTForAudioClassification.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593' ).to(__UpperCAmelCase )

         lowerCAmelCase__     :Union[str, Any]											=				self.default_feature_extractor
         lowerCAmelCase__				,							lowerCAmelCase__     :Any											=				prepare_audio()
         lowerCAmelCase__     :int											=				audio.squeeze().numpy()
         lowerCAmelCase__     :int											=				feature_extractor(__UpperCAmelCase       ,					sampling_rate=__UpperCAmelCase       ,					return_tensors='pt' ).to(__UpperCAmelCase )

         # forward pass
         with torch.no_grad():
           lowerCAmelCase__     :Tuple											=				model(**__UpperCAmelCase )

         # verify the logits
         lowerCAmelCase__     :Tuple											=				torch.Size((1, 5_2_7) )
         self.assertEqual(outputs.logits.shape       ,					__UpperCAmelCase )

         lowerCAmelCase__     :int											=				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 ) )







"""simple docstring"""





import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFGPTaLMHeadModel, is_keras_nlp_available, is_tf_available
from transformers.models.gpta.tokenization_gpta import GPTaTokenizer
from transformers.testing_utils import require_keras_nlp, require_tf, slow


if is_tf_available():
  import tensorflow as tf

if is_keras_nlp_available():
  from transformers.models.gpta import TFGPTaTokenizer


__A											=						["""gpt2"""]
__A											=						"""gpt2"""

if is_tf_available():






  class    _lowerCAmelCase  (	tf.Module			):
         """simple docstring"""




         def __init__(       self       ,					__UpperCAmelCase ):



           '''simple docstring'''

           super().__init__()
           lowerCAmelCase__     :List[str]											=				tokenizer
           lowerCAmelCase__     :Optional[int]											=				AutoConfig.from_pretrained(__UpperCAmelCase )
           lowerCAmelCase__     :Optional[Any]											=				TFGPTaLMHeadModel.from_config(__UpperCAmelCase )






         @tf.function(input_signature=(tf.TensorSpec((None,)       ,					tf.string       ,					name='text' ),) )
         def 	snake_case		(       self       ,					__UpperCAmelCase ):



           '''simple docstring'''

           lowerCAmelCase__     :str											=				self.tokenizer(__UpperCAmelCase )
           lowerCAmelCase__     :int											=				tokenized['input_ids'].to_tensor()

           lowerCAmelCase__     :Optional[Any]											=				tf.cast(input_ids_dense > 0       ,					tf.intaa )
           # input_mask = tf.reshape(input_mask, [-1, MAX_SEQ_LEN])

           lowerCAmelCase__     :int											=				self.model(input_ids=__UpperCAmelCase       ,					attention_mask=__UpperCAmelCase )['logits']

           return outputs






@require_tf
@require_keras_nlp
class    _lowerCAmelCase  (	unittest.TestCase			):
       """simple docstring"""




       def 	snake_case		(       self ):



         '''simple docstring'''

         super().setUp()

         lowerCAmelCase__     :List[str]											=				[GPTaTokenizer.from_pretrained(__UpperCAmelCase ) for checkpoint in (TOKENIZER_CHECKPOINTS)]
         lowerCAmelCase__     :Union[str, Any]											=				[TFGPTaTokenizer.from_pretrained(__UpperCAmelCase ) for checkpoint in TOKENIZER_CHECKPOINTS]
         assert len(self.tokenizers ) == len(self.tf_tokenizers )

         lowerCAmelCase__     :int											=				[
             'This is a straightforward English test sentence.',
             'This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.',
             'Now we\'re going to add some Chinese: 一 二 三 一二三',
             'And some much more rare Chinese: 齉 堃 齉堃',
             'Je vais aussi écrire en français pour tester les accents',
             'Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ',
         ]
         lowerCAmelCase__     :str											=				list(zip(self.test_sentences       ,					self.test_sentences[::-1] ) )






       def 	snake_case		(       self ):



         '''simple docstring'''

         for tokenizer, tf_tokenizer in zip(self.tokenizers       ,					self.tf_tokenizers ):
           for test_inputs in self.test_sentences:
             lowerCAmelCase__     :int											=				tokenizer([test_inputs]       ,					return_tensors='tf' )
             lowerCAmelCase__     :Optional[int]											=				tf_tokenizer([test_inputs] )

             for key in python_outputs.keys():
               # convert them to numpy to avoid messing with ragged tensors
               lowerCAmelCase__     :Optional[int]											=				python_outputs[key].numpy()
               lowerCAmelCase__     :List[str]											=				tf_outputs[key].numpy()

               self.assertTrue(tf.reduce_all(python_outputs_values.shape == tf_outputs_values.shape ) )
               self.assertTrue(tf.reduce_all(tf.cast(__UpperCAmelCase       ,					tf.intaa ) == tf_outputs_values ) )






       @slow
       def 	snake_case		(       self ):



         '''simple docstring'''

         for tf_tokenizer in self.tf_tokenizers:
           lowerCAmelCase__     :Optional[int]											=				tf.function(__UpperCAmelCase )
           for test_inputs in self.test_sentences:
             lowerCAmelCase__     :Any											=				tf.constant(__UpperCAmelCase )
             lowerCAmelCase__     :int											=				compiled_tokenizer(__UpperCAmelCase )
             lowerCAmelCase__     :Any											=				tf_tokenizer(__UpperCAmelCase )

             for key in eager_outputs.keys():
               self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key] ) )






       @slow
       def 	snake_case		(       self ):



         '''simple docstring'''

         for tf_tokenizer in self.tf_tokenizers:
           lowerCAmelCase__     :List[str]											=				ModelToSave(tokenizer=__UpperCAmelCase )
           lowerCAmelCase__     :Union[str, Any]											=				tf.convert_to_tensor([self.test_sentences[0]] )
           lowerCAmelCase__     :List[str]											=				model.serving(__UpperCAmelCase )  # Build model with some sample inputs
           with TemporaryDirectory() as tempdir:
             lowerCAmelCase__     :Union[str, Any]											=				Path(__UpperCAmelCase ) / 'saved.model'
             tf.saved_model.save(__UpperCAmelCase       ,					__UpperCAmelCase       ,					signatures={'serving_default': model.serving} )
             lowerCAmelCase__     :str											=				tf.saved_model.load(__UpperCAmelCase )
           lowerCAmelCase__     :Dict											=				loaded_model.signatures['serving_default'](__UpperCAmelCase )['output_0']
           # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
           self.assertTrue(tf.reduce_all(out == loaded_output ) )






       @slow
       def 	snake_case		(       self ):



         '''simple docstring'''

         for tf_tokenizer in self.tf_tokenizers:
           lowerCAmelCase__     :List[str]											=				tf.convert_to_tensor([self.test_sentences[0]] )
           lowerCAmelCase__     :List[str]											=				tf_tokenizer(__UpperCAmelCase )  # Build model with some sample inputs

           lowerCAmelCase__     :Union[str, Any]											=				tf_tokenizer.get_config()
           lowerCAmelCase__     :Tuple											=				TFGPTaTokenizer.from_config(__UpperCAmelCase )
           lowerCAmelCase__     :Tuple											=				model_from_config(__UpperCAmelCase )

           for key in from_config_output.keys():
             self.assertTrue(tf.reduce_all(from_config_output[key] == out[key] ) )






       @slow
       def 	snake_case		(       self ):



         '''simple docstring'''

         for tf_tokenizer in self.tf_tokenizers:
           # for the test to run
           lowerCAmelCase__     :int											=				1_2_3_1_2_3

           for max_length in [3, 5, 1_0_2_4]:
             lowerCAmelCase__     :Tuple											=				tf.convert_to_tensor([self.test_sentences[0]] )
             lowerCAmelCase__     :List[str]											=				tf_tokenizer(__UpperCAmelCase       ,					max_length=__UpperCAmelCase )

             lowerCAmelCase__     :Union[str, Any]											=				out['input_ids'].numpy().shape[1]

             assert out_length == max_length
