
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        _SCREAMING_SNAKE_CASE	:
      def __init__(      self		,						lowerCamelCase		,						lowerCamelCase=13		,						lowerCamelCase=2		,						lowerCamelCase=24		,						lowerCamelCase=16		,						lowerCamelCase=True		,						lowerCamelCase=True		,						lowerCamelCase=32		,						lowerCamelCase=5		,						lowerCamelCase=4		,						lowerCamelCase=37		,						lowerCamelCase="gelu"		,						lowerCamelCase=0.1		,						lowerCamelCase=0.1		,						lowerCamelCase=10		,						lowerCamelCase=0.0_2		,						lowerCamelCase=None		,						lowerCamelCase=2		,						lowerCamelCase=2		,						):
          snake_case__    =					parent
          snake_case__    =					batch_size
          snake_case__    =					patch_size
          snake_case__    =					max_length
          snake_case__    =					num_mel_bins
          snake_case__    =					is_training
          snake_case__    =					use_labels
          snake_case__    =					hidden_size
          snake_case__    =					num_hidden_layers
          snake_case__    =					num_attention_heads
          snake_case__    =					intermediate_size
          snake_case__    =					hidden_act
          snake_case__    =					hidden_dropout_prob
          snake_case__    =					attention_probs_dropout_prob
          snake_case__    =					type_sequence_label_size
          snake_case__    =					initializer_range
          snake_case__    =					scope
          snake_case__    =					frequency_stride
          snake_case__    =					time_stride

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

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

          snake_case__    =					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=lowerCamelCase		,						initializer_range=self.initializer_range		,						frequency_stride=self.frequency_stride		,						time_stride=self.time_stride		,						)
      def A_      (      self		,						lowerCamelCase		,						lowerCamelCase		,						lowerCamelCase			):
          snake_case__    =					ASTModel(config=lowerCamelCase			)
          model.to(lowerCamelCase			)
          model.eval()
          snake_case__    =					model(lowerCamelCase			)
          self.parent.assertEqual(result.last_hidden_state.shape		,						(self.batch_size, self.seq_length, self.hidden_size)			)



      def A_      (      self			):
          snake_case__    =					self.prepare_config_and_inputs()
          (
              (
              snake_case__
          )    ,       (
              snake_case__
          )    ,       (
              snake_case__
          )    ,       
          )    =					config_and_inputs
          snake_case__    =					{"input_values": input_values}
          return config, inputs_dict





@require_torch
class        _SCREAMING_SNAKE_CASE	(   __UpperCamelCase							,       __UpperCamelCase							,       unittest.TestCase			):
      _A    :		Optional[int]     						=  (
          (
              ASTModel,
              ASTForAudioClassification,
          )
          if is_torch_available()
          else ()
      )
      _A    :		Dict     						=  (
          {'audio-classification': ASTForAudioClassification, 'feature-extraction': ASTModel}
          if is_torch_available()
          else {}
      )
      _A    :		Optional[int]     						=  False
      _A    :		Union[str, Any]     						=  False
      _A    :		Dict     						=  False
      _A    :		str     						=  False
      def A_      (      self		,						lowerCamelCase		,						lowerCamelCase		,						lowerCamelCase		,						lowerCamelCase		,						lowerCamelCase			):
          if pipeline_test_casse_name == "AudioClassificationPipelineTests":
              return True

          return False
      def A_      (      self			):
          snake_case__    =					ASTModelTester(self			)
          snake_case__    =					ConfigTester(self		,						config_class=lowerCamelCase		,						has_text_modality=lowerCamelCase		,						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			):
          snake_case__    ,       snake_case__    =					self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              snake_case__    =					model_class(lowerCamelCase			)
              self.assertIsInstance(model.get_input_embeddings()		,						(nn.Module)			)
              snake_case__    =					model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(lowerCamelCase		,						nn.Linear			)			)
      def A_      (      self			):
          snake_case__    ,       snake_case__    =					self.model_tester.prepare_config_and_inputs_for_common()

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

              snake_case__    =					["input_values"]
              self.assertListEqual(arg_names[:1]		,						lowerCamelCase			)
      def A_      (      self			):
          snake_case__    =					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowerCamelCase			)



      @slow
      def A_      (      self			):
          for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              snake_case__    =					ASTModel.from_pretrained(lowerCamelCase			)
              self.assertIsNotNone(lowerCamelCase			)





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

    snake_case__    ,       snake_case__    =					torchaudio.load(__lowerCAmelCase				)

    return audio, sampling_rate





@require_torch
@require_torchaudio
class        _SCREAMING_SNAKE_CASE	(   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			):
          snake_case__    =					self.default_feature_extractor
          snake_case__    =					ASTForAudioClassification.from_pretrained("MIT/ast-finetuned-audioset-10-10-0.4593"			).to(lowerCamelCase			)

          snake_case__    =					self.default_feature_extractor
          snake_case__    ,       snake_case__    =					prepare_audio()
          snake_case__    =					audio.squeeze().numpy()
          snake_case__    =					feature_extractor(lowerCamelCase		,						sampling_rate=lowerCamelCase		,						return_tensors="pt"			).to(lowerCamelCase			)

          # forward pass
          with torch.no_grad():
              snake_case__    =					model(**lowerCamelCase			)

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

          snake_case__    =					torch.tensor([-0.8_7_6_0, -7.0_0_4_2, -8.6_6_0_2]			).to(lowerCamelCase			)

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





from ...configuration_utils import PretrainedConfig
from ...utils import logging


__magic_name__  							=	logging.get_logger(__name__)

__magic_name__  							=	{
    '''microsoft/biogpt''': '''https://huggingface.co/microsoft/biogpt/resolve/main/config.json''',
    # See all BioGPT models at https://huggingface.co/models?filter=biogpt
}




class        _SCREAMING_SNAKE_CASE	(   __UpperCamelCase			):
      _A    :		Dict     						=  'biogpt'
      def __init__(      self		,						lowerCamelCase=4_23_84		,						lowerCamelCase=10_24		,						lowerCamelCase=24		,						lowerCamelCase=16		,						lowerCamelCase=40_96		,						lowerCamelCase="gelu"		,						lowerCamelCase=0.1		,						lowerCamelCase=0.1		,						lowerCamelCase=10_24		,						lowerCamelCase=0.0_2		,						lowerCamelCase=1e-12		,						lowerCamelCase=True		,						lowerCamelCase=True		,						lowerCamelCase=0.0		,						lowerCamelCase=0.0		,						lowerCamelCase=1		,						lowerCamelCase=0		,						lowerCamelCase=2		,						**lowerCamelCase		,						):
          snake_case__    =					vocab_size
          snake_case__    =					max_position_embeddings
          snake_case__    =					hidden_size
          snake_case__    =					num_hidden_layers
          snake_case__    =					num_attention_heads
          snake_case__    =					intermediate_size
          snake_case__    =					hidden_act
          snake_case__    =					hidden_dropout_prob
          snake_case__    =					attention_probs_dropout_prob
          snake_case__    =					initializer_range
          snake_case__    =					layer_norm_eps
          snake_case__    =					scale_embedding
          snake_case__    =					use_cache
          snake_case__    =					layerdrop
          snake_case__    =					activation_dropout
          super().__init__(pad_token_id=lowerCamelCase		,						bos_token_id=lowerCamelCase		,						eos_token_id=lowerCamelCase		,						**lowerCamelCase			)



