





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

			"""simple docstring"""







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






									'''simple docstring'''







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

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







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Optional[int]				   =			floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]    )

									UpperCamelCase				:		int				   =			None
									if self.use_labels:
															UpperCamelCase				:		Tuple				   =			ids_tensor([self.batch_size]						,   self.type_sequence_label_size    )

									UpperCamelCase				:		Dict				   =			self.get_config()

									return config, input_values, labels







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







			def   _a      (      self						,   _A						,   _A						,   _A    ):






									'''simple docstring'''







									UpperCamelCase				:		List[str]				   =			ASTModel(config=_A    )
									model.to(_A    )
									model.eval()
									UpperCamelCase				:		int				   =			model(_A    )
									self.parent.assertEqual(result.last_hidden_state.shape						,   (self.batch_size, self.seq_length, self.hidden_size)    )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Optional[Any]				   =			self.prepare_config_and_inputs()
									(
									    (
									    UpperCamelCase
									)   ,						(
									    UpperCamelCase
									)   ,						(
									    UpperCamelCase
									)   ,						
									)				:		List[str]				   =			config_and_inputs
									UpperCamelCase				:		Union[str, Any]				   =			{"""input_values""": input_values}
									return config, inputs_dict






@require_torch
class 							lowercase__			(   __SCREAMING_SNAKE_CASE					,     __SCREAMING_SNAKE_CASE					,     unittest.TestCase	):

			"""simple docstring"""
			__lowerCAmelCase     :				List[Any]										=  (
			    (
			        ASTModel,
			        ASTForAudioClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			__lowerCAmelCase     :				Any										=  (
			    {"""audio-classification""": ASTForAudioClassification, """feature-extraction""": ASTModel}
			    if is_torch_available()
			    else {}
			)
			__lowerCAmelCase     :				Union[str, Any]										=  False
			__lowerCAmelCase     :				Any										=  False
			__lowerCAmelCase     :				List[Any]										=  False
			__lowerCAmelCase     :				Union[str, Any]										=  False







			def   _a      (      self						,   _A						,   _A						,   _A						,   _A						,   _A    ):






									'''simple docstring'''







									if pipeline_test_casse_name == "AudioClassificationPipelineTests":
															return True

									return False







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		str				   =			ASTModelTester(self    )
									UpperCamelCase				:		int				   =			ConfigTester(self						,   config_class=_A						,   has_text_modality=_A						,   hidden_size=3_7    )







			def   _a      (      self    ):






									'''simple docstring'''







									self.config_tester.run_common_tests()







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






									'''simple docstring'''







									pass







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase   ,						UpperCamelCase				:		int				   =			self.model_tester.prepare_config_and_inputs_for_common()

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







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase   ,						UpperCamelCase				:		Union[str, Any]				   =			self.model_tester.prepare_config_and_inputs_for_common()

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

															UpperCamelCase				:		Any				   =			["""input_values"""]
															self.assertListEqual(arg_names[:1]						,   _A    )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		int				   =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_A    )







			@slow
			def   _a      (      self    ):






									'''simple docstring'''







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


def      UpperCamelCase							():
						UpperCamelCase				:		Optional[Any]				   =			hf_hub_download(
						    repo_id="""nielsr/audio-spectogram-transformer-checkpoint"""							,     filename="""sample_audio.flac"""							,     repo_type="""dataset"""     )

						UpperCamelCase   ,						UpperCamelCase				:		Dict				   =			torchaudio.load(SCREAMING_SNAKE_CASE     )

						return audio, sampling_rate






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

			"""simple docstring"""







			@cached_property
			def   _a      (      self    ):






									'''simple docstring'''







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







			@slow
			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Union[str, Any]				   =			self.default_feature_extractor
									UpperCamelCase				:		Any				   =			ASTForAudioClassification.from_pretrained("""MIT/ast-finetuned-audioset-10-10-0.4593"""    ).to(_A    )

									UpperCamelCase				:		Union[str, Any]				   =			self.default_feature_extractor
									UpperCamelCase   ,						UpperCamelCase				:		List[Any]				   =			prepare_audio()
									UpperCamelCase				:		Optional[Any]				   =			audio.squeeze().numpy()
									UpperCamelCase				:		Union[str, Any]				   =			feature_extractor(_A						,   sampling_rate=_A						,   return_tensors="""pt"""    ).to(_A    )

									# forward pass
									with torch.no_grad():
															UpperCamelCase				:		Optional[Any]				   =			model(**_A    )

									# verify the logits
									UpperCamelCase				:		List[Any]				   =			torch.Size((1, 5_2_7)    )
									self.assertEqual(outputs.logits.shape						,   _A    )

									UpperCamelCase				:		Optional[Any]				   =			torch.tensor([-0.87_60, -7.00_42, -8.66_02]    ).to(_A    )

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










UpperCAmelCase_							=				8.31_44_62  # Unit - J mol-1 K-1





def  __magic_name__  (				lowercase				,       lowercase				,       lowercase				)				-> float:

							"""simple docstring"""



							if moles < 0 or kelvin < 0 or volume < 0:
														raise ValueError("""Invalid inputs. Enter positive value."""				)
							return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume





def  __magic_name__  (				lowercase				,       lowercase				,       lowercase				)				-> float:

							"""simple docstring"""



							if moles < 0 or kelvin < 0 or pressure < 0:
														raise ValueError("""Invalid inputs. Enter positive value."""				)
							return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure


if __name__ == "__main__":
							from doctest import testmod

							testmod()