





"""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					:






       def __init__(   self    ,  a__    ,  a__=13    ,  a__=2    ,  a__=24    ,  a__=16    ,  a__=True    ,  a__=True    ,  a__=32    ,  a__=5    ,  a__=4    ,  a__=37    ,  a__="gelu"    ,  a__=0.1    ,  a__=0.1    ,  a__=10    ,  a__=0.02    ,  a__=None    ,  a__=2    ,  a__=2    ,  ):
              _UpperCAmelCase     =     parent
              _UpperCAmelCase     =     batch_size
              _UpperCAmelCase     =     patch_size
              _UpperCAmelCase     =     max_length
              _UpperCAmelCase     =     num_mel_bins
              _UpperCAmelCase     =     is_training
              _UpperCAmelCase     =     use_labels
              _UpperCAmelCase     =     hidden_size
              _UpperCAmelCase     =     num_hidden_layers
              _UpperCAmelCase     =     num_attention_heads
              _UpperCAmelCase     =     intermediate_size
              _UpperCAmelCase     =     hidden_act
              _UpperCAmelCase     =     hidden_dropout_prob
              _UpperCAmelCase     =     attention_probs_dropout_prob
              _UpperCAmelCase     =     type_sequence_label_size
              _UpperCAmelCase     =     initializer_range
              _UpperCAmelCase     =     scope
              _UpperCAmelCase     =     frequency_stride
              _UpperCAmelCase     =     time_stride

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






       def 							__A (   self     ):
              _UpperCAmelCase     =     floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]     )

              _UpperCAmelCase     =     None
              if self.use_labels:
                     _UpperCAmelCase     =     ids_tensor([self.batch_size]    ,  self.type_sequence_label_size     )

              _UpperCAmelCase     =     self.get_config()

              return config, input_values, labels






       def 							__A (   self     ):
              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=a__    ,  initializer_range=self.initializer_range    ,  frequency_stride=self.frequency_stride    ,  time_stride=self.time_stride    ,  )






       def 							__A (   self    ,  a__    ,  a__    ,  a__     ):
              _UpperCAmelCase     =     ASTModel(config=a__     )
              model.to(a__     )
              model.eval()
              _UpperCAmelCase     =     model(a__     )
              self.parent.assertEqual(result.last_hidden_state.shape    ,  (self.batch_size, self.seq_length, self.hidden_size)     )






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



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






       def 							__A (   self    ,  a__    ,  a__    ,  a__    ,  a__    ,  a__     ):
              if pipeline_test_casse_name == "AudioClassificationPipelineTests":
                     return True

              return False






       def 							__A (   self     ):
              _UpperCAmelCase     =     ASTModelTester(self     )
              _UpperCAmelCase     =     ConfigTester(self    ,  config_class=a__    ,  has_text_modality=a__    ,  hidden_size=37     )






       def 							__A (   self     ):
              self.config_tester.run_common_tests()






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






       def 							__A (   self     ):
              _UpperCAmelCase    ,							_UpperCAmelCase     =     self.model_tester.prepare_config_and_inputs_for_common()

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






       def 							__A (   self     ):
              _UpperCAmelCase    ,							_UpperCAmelCase     =     self.model_tester.prepare_config_and_inputs_for_common()

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

                     _UpperCAmelCase     =     ['input_values']
                     self.assertListEqual(arg_names[:1]    ,  a__     )






       def 							__A (   self     ):
              _UpperCAmelCase     =     self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*a__     )






       @slow
       def 							__A (   self     ):
              for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     _UpperCAmelCase     =     ASTModel.from_pretrained(a__     )
                     self.assertIsNotNone(a__     )


def       __lowerCamelCase    (       )		->       List[str]:






       """simple docstring"""




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

       _UpperCAmelCase    ,							_UpperCAmelCase     =     torchaudio.load(SCREAMING_SNAKE_CASE							)

       return audio, sampling_rate



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






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






       @slow
       def 							__A (   self     ):
              _UpperCAmelCase     =     self.default_feature_extractor
              _UpperCAmelCase     =     ASTForAudioClassification.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593'     ).to(a__     )

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

              # forward pass
              with torch.no_grad():
                     _UpperCAmelCase     =     model(**a__     )

              # verify the logits
              _UpperCAmelCase     =     torch.Size((1, 5_27)     )
              self.assertEqual(outputs.logits.shape    ,  a__     )

              _UpperCAmelCase     =     torch.tensor([-0.8_760, -7.0_042, -8.6_602]     ).to(a__     )

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













"""simple docstring"""






def       __lowerCamelCase    (       SCREAMING_SNAKE_CASE,SCREAMING_SNAKE_CASE							)		->       int:






       """simple docstring"""




       return number | (1 << position)

def       __lowerCamelCase    (       SCREAMING_SNAKE_CASE,SCREAMING_SNAKE_CASE							)		->       int:






       """simple docstring"""




       return number & ~(1 << position)

def       __lowerCamelCase    (       SCREAMING_SNAKE_CASE,SCREAMING_SNAKE_CASE							)		->       int:






       """simple docstring"""




       return number ^ (1 << position)

def       __lowerCamelCase    (       SCREAMING_SNAKE_CASE,SCREAMING_SNAKE_CASE							)		->       bool:






       """simple docstring"""




       return ((number >> position) & 1) == 1

def       __lowerCamelCase    (       SCREAMING_SNAKE_CASE,SCREAMING_SNAKE_CASE							)		->       int:






       """simple docstring"""




       return int((number & (1 << position)) != 0							)


if __name__ == "__main__":
    import doctest

    doctest.testmod()






