



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

    def __init__(				self			:				List[str] ,  lowerCAmelCase_			:				List[Any] ,  lowerCAmelCase_			:				Union[str, Any]=1_3 ,  lowerCAmelCase_			:				Dict=2 ,  lowerCAmelCase_			:				Optional[int]=2_4 ,  lowerCAmelCase_			:				Union[str, Any]=1_6 ,  lowerCAmelCase_			:				List[str]=True ,  lowerCAmelCase_			:				Optional[Any]=True ,  lowerCAmelCase_			:				Dict=3_2 ,  lowerCAmelCase_			:				Any=5 ,  lowerCAmelCase_			:				List[str]=4 ,  lowerCAmelCase_			:				Optional[int]=3_7 ,  lowerCAmelCase_			:				Tuple="gelu" ,  lowerCAmelCase_			:				Tuple=0.1 ,  lowerCAmelCase_			:				int=0.1 ,  lowerCAmelCase_			:				Union[str, Any]=1_0 ,  lowerCAmelCase_			:				Optional[Any]=0.02 ,  lowerCAmelCase_			:				int=None ,  lowerCAmelCase_			:				List[Any]=2 ,  lowerCAmelCase_			:				int=2 ,  ):







      """simple docstring"""







      lowercase_								= parent
      lowercase_								= batch_size
      lowercase_								= patch_size
      lowercase_								= max_length
      lowercase_								= num_mel_bins
      lowercase_								= is_training
      lowercase_								= use_labels
      lowercase_								= hidden_size
      lowercase_								= num_hidden_layers
      lowercase_								= num_attention_heads
      lowercase_								= intermediate_size
      lowercase_								= hidden_act
      lowercase_								= hidden_dropout_prob
      lowercase_								= attention_probs_dropout_prob
      lowercase_								= type_sequence_label_size
      lowercase_								= initializer_range
      lowercase_								= scope
      lowercase_								= frequency_stride
      lowercase_								= time_stride

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

    def 					_UpperCAmelCase	(				self			:				Any):







      """simple docstring"""







      lowercase_								= floats_tensor([self.batch_size, self.max_length, self.num_mel_bins])

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

      lowercase_								= self.get_config()

      return config, input_values, labels

    def 					_UpperCAmelCase	(				self			:				int):







      """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=lowerCAmelCase_ ,  initializer_range=self.initializer_range ,  frequency_stride=self.frequency_stride ,  time_stride=self.time_stride ,  )

    def 					_UpperCAmelCase	(				self			:				int ,  lowerCAmelCase_			:				List[str] ,  lowerCAmelCase_			:				Dict ,  lowerCAmelCase_			:				List[Any]):







      """simple docstring"""







      lowercase_								= ASTModel(config=lowerCAmelCase_)
      model.to(lowerCAmelCase_)
      model.eval()
      lowercase_								= model(lowerCAmelCase_)
      self.parent.assertEqual(result.last_hidden_state.shape ,  (self.batch_size, self.seq_length, self.hidden_size))




    def 					_UpperCAmelCase	(				self			:				List[str]):







      """simple docstring"""







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







@require_torch
class    SCREAMING_SNAKE_CASE__ (							__UpperCAmelCase      ,  __UpperCAmelCase      ,  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 					_UpperCAmelCase	(				self			:				Optional[Any] ,  lowerCAmelCase_			:				List[str] ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				List[str] ,  lowerCAmelCase_			:				Optional[Any] ,  lowerCAmelCase_			:				Optional[int]):







      """simple docstring"""







      if pipeline_test_casse_name == "AudioClassificationPipelineTests":
        return True

      return False

    def 					_UpperCAmelCase	(				self			:				Dict):







      """simple docstring"""







      lowercase_								= ASTModelTester(self)
      lowercase_								= ConfigTester(self ,  config_class=lowerCAmelCase_ ,  has_text_modality=lowerCAmelCase_ ,  hidden_size=3_7)

    def 					_UpperCAmelCase	(				self			:				int):







      """simple docstring"""







      self.config_tester.run_common_tests()

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







      """simple docstring"""







      pass

    def 					_UpperCAmelCase	(				self			:				List[Any]):







      """simple docstring"""







      lowercase_					,    lowercase_								= self.model_tester.prepare_config_and_inputs_for_common()

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

    def 					_UpperCAmelCase	(				self			:				Union[str, Any]):







      """simple docstring"""







      lowercase_					,    lowercase_								= self.model_tester.prepare_config_and_inputs_for_common()

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

        lowercase_								= ["""input_values"""]
        self.assertListEqual(arg_names[:1] ,  lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				Tuple):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCAmelCase_)




    @slow
    def 					_UpperCAmelCase	(				self			:				Optional[int]):







      """simple docstring"""







      for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowercase_								= ASTModel.from_pretrained(lowerCAmelCase_)
        self.assertIsNotNone(lowerCAmelCase_)







def 			_SCREAMING_SNAKE_CASE						() ->						Union[str, Any]:







  '''simple docstring'''


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

  lowercase_					,    lowercase_								= torchaudio.load(__lowerCAmelCase							)

  return audio, sampling_rate


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

    @cached_property
    def 					_UpperCAmelCase	(				self			:				Optional[Any]):







      """simple docstring"""







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




    @slow
    def 					_UpperCAmelCase	(				self			:				Any):







      """simple docstring"""







      lowercase_								= self.default_feature_extractor
      lowercase_								= ASTForAudioClassification.from_pretrained("""MIT/ast-finetuned-audioset-10-10-0.4593""").to(lowerCAmelCase_)

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

      # forward pass
      with torch.no_grad():
        lowercase_								= model(**lowerCAmelCase_)

      # verify the logits
      lowercase_								= torch.Size((1, 5_2_7))
      self.assertEqual(outputs.logits.shape ,  lowerCAmelCase_)

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

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






"""simple docstring"""
def 			_SCREAMING_SNAKE_CASE						(__lowerCAmelCase							) ->						list[list[int]]:







  '''simple docstring'''


  lowercase_								= []
  if len(__lowerCAmelCase							) == 1:
    return [nums.copy()]
  for _ in range(len(__lowerCAmelCase							)							):
    lowercase_								= nums.pop(0							)
    lowercase_								= permute(__lowerCAmelCase							)
    for perm in permutations:
      perm.append(__lowerCAmelCase							)
    result.extend(__lowerCAmelCase							)
    nums.append(__lowerCAmelCase							)
  return result


def 			_SCREAMING_SNAKE_CASE						(__lowerCAmelCase							) ->						Tuple:







  '''simple docstring'''


  def backtrack(__lowerCAmelCase							):
    if start == len(__lowerCAmelCase							) - 1:
      output.append(nums[:]							)
    else:
      for i in range(__lowerCAmelCase ,  len(__lowerCAmelCase							)							):
        lowercase_					,    lowercase_								= nums[i], nums[start]
        backtrack(start + 1							)
        lowercase_					,    lowercase_								= nums[i], nums[start]  # backtrack

  lowercase_								= []
  backtrack(0							)
  return output


if __name__ == "__main__":
       import doctest

       # use res to print the data in permute2 function
       UpperCAmelCase						:		Optional[Any]			= permutea([1, 2, 3])
       print(res)
       doctest.testmod()

