





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







					'''simple docstring'''

					def __init__(						self    :  Optional[int] ,					__lowercase    :  Union[str, Any] ,					__lowercase    :  Optional[int]=13 ,					__lowercase    :  Union[str, Any]=2 ,					__lowercase    :  Union[str, Any]=24 ,					__lowercase    :  Tuple=16 ,					__lowercase    :  Any=True ,					__lowercase    :  List[str]=True ,					__lowercase    :  List[Any]=32 ,					__lowercase    :  Tuple=5 ,					__lowercase    :  List[str]=4 ,					__lowercase    :  Optional[Any]=37 ,					__lowercase    :  Tuple="gelu" ,					__lowercase    :  Any=0.1 ,					__lowercase    :  Optional[int]=0.1 ,					__lowercase    :  Tuple=10 ,					__lowercase    :  List[str]=0.02 ,					__lowercase    :  str=None ,					__lowercase    :  Any=2 ,					__lowercase    :  Optional[int]=2 ,					):






						'''simple docstring'''







						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        SCREAMING_SNAKE_CASE							(						self    :  int		):






						'''simple docstring'''







						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        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






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





					def        SCREAMING_SNAKE_CASE							(						self    :  List[str] ,					__lowercase    :  int ,					__lowercase    :  Dict ,					__lowercase    :  Optional[Any]		):






						'''simple docstring'''







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





					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






						'''simple docstring'''







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







@require_torch
class   _UpperCamelCase  (       A_						,       A_						,       unittest.TestCase  ):







					'''simple docstring'''

					lowerCamelCase     :     Any			    =       (
					    (
					        ASTModel,
					        ASTForAudioClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					lowerCamelCase     :     Optional[Any]			    =       (
					    {'audio-classification': ASTForAudioClassification, 'feature-extraction': ASTModel}
					    if is_torch_available()
					    else {}
					)
					lowerCamelCase     :     Union[str, Any]			    =       False
					lowerCamelCase     :     Tuple			    =       False
					lowerCamelCase     :     List[str]			    =       False
					lowerCamelCase     :     List[Any]			    =       False





					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any] ,					__lowercase    :  Union[str, Any] ,					__lowercase    :  List[str] ,					__lowercase    :  int ,					__lowercase    :  str ,					__lowercase    :  Dict		):






						'''simple docstring'''







						if pipeline_test_casse_name == "AudioClassificationPipelineTests":
							return True

						return False





					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






						'''simple docstring'''







						UpperCAmelCase_								=     ASTModelTester(self		)
						UpperCAmelCase_								=     ConfigTester(self ,					config_class=__lowercase ,					has_text_modality=__lowercase ,					hidden_size=37		)





					def        SCREAMING_SNAKE_CASE							(						self    :  str		):






						'''simple docstring'''







						self.config_tester.run_common_tests()





					@unittest.skip(reason="""AST does not use inputs_embeds"""		)
					def        SCREAMING_SNAKE_CASE							(						self    :  Optional[Any]		):






						'''simple docstring'''







						pass





					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






						'''simple docstring'''







						UpperCAmelCase_						,	UpperCAmelCase_								=     self.model_tester.prepare_config_and_inputs_for_common()

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





					def        SCREAMING_SNAKE_CASE							(						self    :  Optional[Any]		):






						'''simple docstring'''







						UpperCAmelCase_						,	UpperCAmelCase_								=     self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							UpperCAmelCase_								=     model_class(__lowercase		)
							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] ,					__lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  str		):






						'''simple docstring'''







						UpperCAmelCase_								=     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__lowercase		)





					@slow
					def        SCREAMING_SNAKE_CASE							(						self    :  str		):






						'''simple docstring'''







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




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

	UpperCAmelCase_						,	UpperCAmelCase_								=     torchaudio.load(A   )

	return audio, sampling_rate







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







					'''simple docstring'''

					@cached_property
					def        SCREAMING_SNAKE_CASE							(						self    :  Any		):






						'''simple docstring'''







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





					@slow
					def        SCREAMING_SNAKE_CASE							(						self    :  Any		):






						'''simple docstring'''







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

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

						# forward pass
						with torch.no_grad():
							UpperCAmelCase_								=     model(**__lowercase		)

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

						UpperCAmelCase_								=     torch.tensor([-0.8760, -7.0042, -8.6602]		).to(__lowercase		)

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









from typing import Dict, List, Optional, Union

import numpy as np

from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy


UpperCamelCase__   : List[str]					  = logging.get_logger(__name__)







class   _UpperCamelCase  (       A_  ):







					'''simple docstring'''

					def __init__(						self    :  Union[str, Any] ,					__lowercase    :  int ,					__lowercase    :  int ,					__lowercase    :  float ,					**__lowercase    :  Dict		):






						'''simple docstring'''







						UpperCAmelCase_								=     feature_size
						UpperCAmelCase_								=     sampling_rate
						UpperCAmelCase_								=     padding_value

						UpperCAmelCase_								=     kwargs.pop("""padding_side""" ,					"""right"""		)
						UpperCAmelCase_								=     kwargs.pop("""return_attention_mask""" ,					__lowercase		)

						super().__init__(**__lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  List[str] ,					__lowercase    :  Union[
					        BatchFeature,
					        List[BatchFeature],
					        Dict[str, BatchFeature],
					        Dict[str, List[BatchFeature]],
					        List[Dict[str, BatchFeature]],
					    ] ,					__lowercase    :  Union[bool, str, PaddingStrategy] = True ,					__lowercase    :  Optional[int] = None ,					__lowercase    :  bool = False ,					__lowercase    :  Optional[int] = None ,					__lowercase    :  Optional[bool] = None ,					__lowercase    :  Optional[Union[str, TensorType]] = None ,					):






						'''simple docstring'''







						if isinstance(__lowercase ,					(list, tuple)		) and isinstance(processed_features[0] ,					(dict, BatchFeature)		):
							UpperCAmelCase_								=     {
							    key: [example[key] for example in processed_features] for key in processed_features[0].keys()
							}

						# The model's main input name, usually `input_values`, has be passed for padding
						if self.model_input_names[0] not in processed_features:
							raise ValueError(
							    """You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`"""
							    F""" to this method that includes {self.model_input_names[0]}, but you provided"""
							    F""" {list(processed_features.keys()		)}"""		)

						UpperCAmelCase_								=     processed_features[self.model_input_names[0]]
						UpperCAmelCase_								=     (
						    return_attention_mask if return_attention_mask is not None else self.return_attention_mask
						)

						if len(__lowercase		) == 0:
							if return_attention_mask:
								UpperCAmelCase_								=     []
							return processed_features

						# If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays
						# and rebuild them afterwards if no return_tensors is specified
						# Note that we lose the specific device the tensor may be on for PyTorch

						UpperCAmelCase_								=     required_input[0]
						if isinstance(__lowercase ,					(list, tuple)		):
							# first_element might be an empty list/tuple in some edge cases so we grab the first non empty element.
							UpperCAmelCase_								=     0
							while len(required_input[index]		) == 0:
								index += 1
							if index < len(__lowercase		):
								UpperCAmelCase_								=     required_input[index][0]

						if return_tensors is None:
							if is_tf_tensor(__lowercase		):
								UpperCAmelCase_								=     """tf"""
							elif is_torch_tensor(__lowercase		):
								UpperCAmelCase_								=     """pt"""
							elif isinstance(__lowercase ,					(int, float, list, tuple, np.ndarray)		):
								UpperCAmelCase_								=     """np"""
							else:
								raise ValueError(
								    F"""type of {first_element} unknown: {type(__lowercase		)}. """
								    """Should be one of a python, numpy, pytorch or tensorflow object."""		)

						for key, value in processed_features.items():
							if isinstance(value[0] ,					(int, float)		):
								UpperCAmelCase_								=     to_numpy(__lowercase		)
							else:
								UpperCAmelCase_								=     [to_numpy(__lowercase		) for v in value]

        # Convert padding_strategy in PaddingStrategy
						UpperCAmelCase_								=     self._get_padding_strategies(padding=__lowercase ,					max_length=__lowercase		)

						UpperCAmelCase_								=     processed_features[self.model_input_names[0]]

						UpperCAmelCase_								=     len(__lowercase		)
						if not all(len(__lowercase		) == batch_size for v in processed_features.values()		):
							raise ValueError("""Some items in the output dictionary have a different batch size than others."""		)

						UpperCAmelCase_								=     []
						for i in range(__lowercase		):
							UpperCAmelCase_								=     {k: v[i] for k, v in processed_features.items()}
							# truncation
							UpperCAmelCase_								=     self._truncate(
							    __lowercase ,					max_length=__lowercase ,					pad_to_multiple_of=__lowercase ,					truncation=__lowercase ,					)
							truncated_inputs.append(__lowercase		)

						if padding_strategy == PaddingStrategy.LONGEST:
							# make sure that `max_length` cannot be longer than the longest truncated length
							UpperCAmelCase_								=     max(len(input_slice[self.model_input_names[0]]		) for input_slice in truncated_inputs		)
							UpperCAmelCase_								=     PaddingStrategy.MAX_LENGTH

						UpperCAmelCase_								=     {}
						for i in range(__lowercase		):
							# padding
							UpperCAmelCase_								=     self._pad(
							    truncated_inputs[i] ,					max_length=__lowercase ,					padding_strategy=__lowercase ,					pad_to_multiple_of=__lowercase ,					return_attention_mask=__lowercase ,					)

							for key, value in outputs.items():
								if key not in batch_outputs:
									UpperCAmelCase_								=     []
								if value.dtype is np.dtype(np.floataa		):
									UpperCAmelCase_								=     value.astype(np.floataa		)
								batch_outputs[key].append(__lowercase		)

						return BatchFeature(__lowercase ,					tensor_type=__lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  str ,					__lowercase    :  Union[Dict[str, np.ndarray], BatchFeature] ,					__lowercase    :  Optional[int] = None ,					__lowercase    :  PaddingStrategy = PaddingStrategy.DO_NOT_PAD ,					__lowercase    :  Optional[int] = None ,					__lowercase    :  Optional[bool] = None ,					):






						'''simple docstring'''







						UpperCAmelCase_								=     processed_features[self.model_input_names[0]]

						if padding_strategy == PaddingStrategy.LONGEST:
							UpperCAmelCase_								=     len(__lowercase		)

						if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
							UpperCAmelCase_								=     ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

						UpperCAmelCase_								=     padding_strategy != PaddingStrategy.DO_NOT_PAD and len(__lowercase		) < max_length

						if return_attention_mask and "attention_mask" not in processed_features:
							UpperCAmelCase_								=     np.ones(len(__lowercase		) ,					dtype=np.intaa		)

						if needs_to_be_padded:
							UpperCAmelCase_								=     max_length - len(__lowercase		)
							if self.padding_side == "right":
								if return_attention_mask:
									UpperCAmelCase_								=     np.pad(
									    processed_features["""attention_mask"""] ,					(0, difference)		)
								UpperCAmelCase_								=     ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference)
								UpperCAmelCase_								=     np.pad(
								    __lowercase ,					__lowercase ,					"""constant""" ,					constant_values=self.padding_value		)
							elif self.padding_side == "left":
								if return_attention_mask:
									UpperCAmelCase_								=     np.pad(
									    processed_features["""attention_mask"""] ,					(difference, 0)		)
								UpperCAmelCase_								=     ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0)
								UpperCAmelCase_								=     np.pad(
								    __lowercase ,					__lowercase ,					"""constant""" ,					constant_values=self.padding_value		)
							else:
								raise ValueError("""Invalid padding strategy:""" + str(self.padding_side		)		)

						return processed_features





					def        SCREAMING_SNAKE_CASE							(						self    :  Optional[int] ,					__lowercase    :  Union[Dict[str, np.ndarray], BatchFeature] ,					__lowercase    :  Optional[int] = None ,					__lowercase    :  Optional[int] = None ,					__lowercase    :  Optional[bool] = None ,					):






						'''simple docstring'''







						if not truncation:
							return processed_features
						elif truncation and max_length is None:
							raise ValueError("""When setting ``truncation=True``, make sure that ``max_length`` is defined."""		)

						UpperCAmelCase_								=     processed_features[self.model_input_names[0]]

						# find `max_length` that fits `pad_to_multiple_of`
						if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
							UpperCAmelCase_								=     ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

						UpperCAmelCase_								=     len(__lowercase		) > max_length

						if needs_to_be_truncated:
							UpperCAmelCase_								=     processed_features[self.model_input_names[0]][:max_length]
							if "attention_mask" in processed_features:
								UpperCAmelCase_								=     processed_features["""attention_mask"""][:max_length]

						return processed_features





					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any] ,					__lowercase    :  int=False ,					__lowercase    :  List[str]=None		):






						'''simple docstring'''







						if padding is not False:
							if padding is True:
								UpperCAmelCase_								=     PaddingStrategy.LONGEST  # Default to pad to the longest sequence in the batch
							elif not isinstance(__lowercase ,					__lowercase		):
								UpperCAmelCase_								=     PaddingStrategy(__lowercase		)
							elif isinstance(__lowercase ,					__lowercase		):
								UpperCAmelCase_								=     padding
						else:
							UpperCAmelCase_								=     PaddingStrategy.DO_NOT_PAD

						# Set max length if needed
						if max_length is None:
							if padding_strategy == PaddingStrategy.MAX_LENGTH:
								raise ValueError(
								    F"""When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined"""		)

        # Test if we have a padding value
						if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None):
							raise ValueError(
							    """Asking to pad but the feature_extractor does not have a padding value. Please select a value to use"""
							    """ as `padding_value`. For example: `feature_extractor.padding_value = 0.0`."""		)

						return padding_strategy


