





"""simple docstring"""


import csv
from collections import defaultdict
from dataclasses import dataclass, field
from typing import List, Optional

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import ScalarFormatter

from transformers import HfArgumentParser

def      UpperCamelCase							(SCREAMING_SNAKE_CASE=None							,     SCREAMING_SNAKE_CASE=None     ):
						return field(default_factory=lambda: default							,     metadata=SCREAMING_SNAKE_CASE     )






@dataclass
class 							lowercase__			:

			"""simple docstring"""
			__lowerCAmelCase     :				str										=  field(
			    metadata={"""help""": """The csv file to plot."""}					,     )
			__lowerCAmelCase     :				bool										=  field(
			    default=__SCREAMING_SNAKE_CASE					,     metadata={"""help""": """Whether to plot along batch size or sequence length. Defaults to sequence length."""}					,     )
			__lowerCAmelCase     :				bool										=  field(
			    default=__SCREAMING_SNAKE_CASE					,     metadata={"""help""": """Whether the csv file has time results or memory results. Defaults to memory results."""}					,     )
			__lowerCAmelCase     :				bool										=  field(
			    default=__SCREAMING_SNAKE_CASE					,     metadata={"""help""": """Disable logarithmic scale when plotting"""}					,     )
			__lowerCAmelCase     :				bool										=  field(
			    default=__SCREAMING_SNAKE_CASE					,     metadata={
			        """help""": """Whether the csv file has training results or inference results. Defaults to inference results."""
			    }					,     )
			__lowerCAmelCase     :				Optional[str]										=  field(
			    default=__SCREAMING_SNAKE_CASE					,     metadata={"""help""": """Filename under which the plot will be saved. If unused no plot is saved."""}					,     )
			__lowerCAmelCase     :				Optional[List[str]]										=  list_field(
			    default=__SCREAMING_SNAKE_CASE					,     metadata={"""help""": """List of model names that are used instead of the ones in the csv file."""}	)


def      UpperCamelCase							(SCREAMING_SNAKE_CASE     ):
						try:
												int(SCREAMING_SNAKE_CASE     )
												return True
						except ValueError:
												return False

def      UpperCamelCase							(SCREAMING_SNAKE_CASE     ):
						try:
												float(SCREAMING_SNAKE_CASE     )
												return True
						except ValueError:
												return False






class 							lowercase__			:

			"""simple docstring"""







			def __init__(      self						,   _A    ):






									'''simple docstring'''







									UpperCamelCase				:		str				   =			args
									UpperCamelCase				:		List[str]				   =			defaultdict(lambda: {"bsz": [], "seq_len": [], "result": {}}    )

									with open(self.args.csv_file						,   newline=""""""    ) as csv_file:
															UpperCamelCase				:		Tuple				   =			csv.DictReader(_A    )
															for row in reader:
																					UpperCamelCase				:		Any				   =			row["""model"""]
																					self.result_dict[model_name]["bsz"].append(int(row["""batch_size"""]    )    )
																					self.result_dict[model_name]["seq_len"].append(int(row["""sequence_length"""]    )    )
																					if can_convert_to_int(row["""result"""]    ):
																											# value is not None
																											UpperCamelCase				:		str				   =			int(row["""result"""]    )
																					elif can_convert_to_float(row["""result"""]    ):
																											# value is not None
																											UpperCamelCase				:		List[str]				   =			float(row["""result"""]    )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase   ,						UpperCamelCase				:		Any				   =			plt.subplots()
									UpperCamelCase				:		Optional[Any]				   =			"""Time usage""" if self.args.is_time else """Memory usage"""
									UpperCamelCase				:		List[str]				   =			title_str + """ for training""" if self.args.is_train else title_str + """ for inference"""

									if not self.args.no_log_scale:
															# set logarithm scales
															ax.set_xscale("""log"""    )
															ax.set_yscale("""log"""    )

									for axis in [ax.xaxis, ax.yaxis]:
															axis.set_major_formatter(ScalarFormatter()    )

									for model_name_idx, model_name in enumerate(self.result_dict.keys()    ):
															UpperCamelCase				:		str				   =			sorted(set(self.result_dict[model_name]["""bsz"""]    )    )
															UpperCamelCase				:		Optional[Any]				   =			sorted(set(self.result_dict[model_name]["""seq_len"""]    )    )
															UpperCamelCase				:		Optional[Any]				   =			self.result_dict[model_name]["""result"""]

															((UpperCamelCase)   ,						(UpperCamelCase))				:		Optional[Any]				   =			(
															    (batch_sizes, sequence_lengths) if self.args.plot_along_batch else (sequence_lengths, batch_sizes)
															)

															UpperCamelCase				:		int				   =			(
															    model_name if self.args.short_model_names is None else self.args.short_model_names[model_name_idx]
															)

															for inner_loop_value in inner_loop_array:
																					if self.args.plot_along_batch:
																											UpperCamelCase				:		int				   =			np.asarray(
																											    [results[(x, inner_loop_value)] for x in x_axis_array if (x, inner_loop_value) in results]						,   dtype=_A						,   )
																					else:
																											UpperCamelCase				:		Optional[int]				   =			np.asarray(
																											    [results[(inner_loop_value, x)] for x in x_axis_array if (inner_loop_value, x) in results]						,   dtype=np.floataa						,   )

																					((UpperCamelCase)   ,						(UpperCamelCase))				:		Any				   =			(
																					    ("""batch_size""", """len""") if self.args.plot_along_batch else ("""in #tokens""", """bsz""")
																					)

																					UpperCamelCase				:		Optional[Any]				   =			np.asarray(_A						,   _A    )[: len(_A    )]
																					plt.scatter(
																					    _A						,   _A						,   label=f"""{label_model_name} - {inner_loop_label}: {inner_loop_value}"""    )
																					plt.plot(_A						,   _A						,   """--"""    )

															title_str += f""" {label_model_name} vs."""

									UpperCamelCase				:		Union[str, Any]				   =			title_str[:-4]
									UpperCamelCase				:		int				   =			"""Time in s""" if self.args.is_time else """Memory in MB"""

									# plot
									plt.title(_A    )
									plt.xlabel(_A    )
									plt.ylabel(_A    )
									plt.legend()

									if self.args.figure_png_file is not None:
															plt.savefig(self.args.figure_png_file    )
									else:
															plt.show()


def      UpperCamelCase							():
						UpperCamelCase				:		Tuple				   =			HfArgumentParser(SCREAMING_SNAKE_CASE     )
						UpperCamelCase				:		Optional[int]				   =			parser.parse_args_into_dataclasses()[0]
						UpperCamelCase				:		int				   =			Plot(args=SCREAMING_SNAKE_CASE     )
						plot.plot()


if __name__ == "__main__":
		main()










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



