




import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_torch_available
from transformers.testing_utils import require_torch, torch_device


if is_torch_available():
					from transformers import PyTorchBenchmark, PyTorchBenchmarkArguments







@require_torch
class     __lowerCAmelCase (       unittest.TestCase ):

			"""simple docstring"""







			def       lowercase_    (      self			,						lowerCamelCase__							)    ->      Dict:



							'''simple docstring'''
							for model_result in results.values():
											for batch_size, sequence_length in zip(model_result['bs']			,						model_result['ss']							):
															__lowerCamelCase		       =    model_result['result'][batch_size][sequence_length]
															self.assertIsNotNone(lowerCamelCase__							)


			def       lowercase_    (      self							)    ->      List[Any]:



							'''simple docstring'''
							__lowerCamelCase		       =    'sshleifer/tiny-gpt2'
							__lowerCamelCase		       =    PyTorchBenchmarkArguments(
							    models=[MODEL_ID]			,						training=lowerCamelCase__			,						inference=lowerCamelCase__			,						sequence_lengths=[8]			,						batch_sizes=[1]			,						multi_process=lowerCamelCase__			,						)
							__lowerCamelCase		       =    PyTorchBenchmark(lowerCamelCase__							)
							__lowerCamelCase		       =    benchmark.run()
							self.check_results_dict_not_empty(results.time_inference_result							)
							self.check_results_dict_not_empty(results.memory_inference_result							)


			def       lowercase_    (      self							)    ->      int:



							'''simple docstring'''
							__lowerCamelCase		       =    'sgugger/tiny-distilbert-classification'
							__lowerCamelCase		       =    PyTorchBenchmarkArguments(
							    models=[MODEL_ID]			,						training=lowerCamelCase__			,						inference=lowerCamelCase__			,						sequence_lengths=[8]			,						batch_sizes=[1]			,						multi_process=lowerCamelCase__			,						only_pretrain_model=lowerCamelCase__			,						)
							__lowerCamelCase		       =    PyTorchBenchmark(lowerCamelCase__							)
							__lowerCamelCase		       =    benchmark.run()
							self.check_results_dict_not_empty(results.time_inference_result							)
							self.check_results_dict_not_empty(results.memory_inference_result							)


			def       lowercase_    (      self							)    ->      Tuple:



							'''simple docstring'''
							__lowerCamelCase		       =    'sshleifer/tiny-gpt2'
							__lowerCamelCase		       =    PyTorchBenchmarkArguments(
							    models=[MODEL_ID]			,						training=lowerCamelCase__			,						inference=lowerCamelCase__			,						torchscript=lowerCamelCase__			,						sequence_lengths=[8]			,						batch_sizes=[1]			,						multi_process=lowerCamelCase__			,						)
							__lowerCamelCase		       =    PyTorchBenchmark(lowerCamelCase__							)
							__lowerCamelCase		       =    benchmark.run()
							self.check_results_dict_not_empty(results.time_inference_result							)
							self.check_results_dict_not_empty(results.memory_inference_result							)


			@unittest.skipIf(torch_device == 'cpu'			,						'Cant do half precision'							)
			def       lowercase_    (      self							)    ->      List[Any]:



							'''simple docstring'''
							__lowerCamelCase		       =    'sshleifer/tiny-gpt2'
							__lowerCamelCase		       =    PyTorchBenchmarkArguments(
							    models=[MODEL_ID]			,						training=lowerCamelCase__			,						inference=lowerCamelCase__			,						fpaa=lowerCamelCase__			,						sequence_lengths=[8]			,						batch_sizes=[1]			,						multi_process=lowerCamelCase__			,						)
							__lowerCamelCase		       =    PyTorchBenchmark(lowerCamelCase__							)
							__lowerCamelCase		       =    benchmark.run()
							self.check_results_dict_not_empty(results.time_inference_result							)
							self.check_results_dict_not_empty(results.memory_inference_result							)


			def       lowercase_    (      self							)    ->      int:



							'''simple docstring'''
							__lowerCamelCase		       =    'sshleifer/tiny-gpt2'
							__lowerCamelCase		       =    AutoConfig.from_pretrained(lowerCamelCase__							)
							# set architectures equal to `None`
							__lowerCamelCase		       =    None
							__lowerCamelCase		       =    PyTorchBenchmarkArguments(
							    models=[MODEL_ID]			,						training=lowerCamelCase__			,						inference=lowerCamelCase__			,						sequence_lengths=[8]			,						batch_sizes=[1]			,						multi_process=lowerCamelCase__			,						)
							__lowerCamelCase		       =    PyTorchBenchmark(lowerCamelCase__			,						configs=[config]							)
							__lowerCamelCase		       =    benchmark.run()
							self.check_results_dict_not_empty(results.time_inference_result							)
							self.check_results_dict_not_empty(results.memory_inference_result							)


			def       lowercase_    (      self							)    ->      List[Any]:



							'''simple docstring'''
							__lowerCamelCase		       =    'sshleifer/tiny-gpt2'
							__lowerCamelCase		       =    PyTorchBenchmarkArguments(
							    models=[MODEL_ID]			,						training=lowerCamelCase__			,						inference=lowerCamelCase__			,						sequence_lengths=[8]			,						batch_sizes=[1]			,						multi_process=lowerCamelCase__			,						)
							__lowerCamelCase		       =    PyTorchBenchmark(lowerCamelCase__							)
							__lowerCamelCase		       =    benchmark.run()
							self.check_results_dict_not_empty(results.time_train_result							)
							self.check_results_dict_not_empty(results.memory_train_result							)


			@unittest.skipIf(torch_device == 'cpu'			,						'Can\'t do half precision'							)
			def       lowercase_    (      self							)    ->      str:



							'''simple docstring'''
							__lowerCamelCase		       =    'sshleifer/tiny-gpt2'
							__lowerCamelCase		       =    PyTorchBenchmarkArguments(
							    models=[MODEL_ID]			,						training=lowerCamelCase__			,						inference=lowerCamelCase__			,						sequence_lengths=[8]			,						batch_sizes=[1]			,						fpaa=lowerCamelCase__			,						multi_process=lowerCamelCase__			,						)
							__lowerCamelCase		       =    PyTorchBenchmark(lowerCamelCase__							)
							__lowerCamelCase		       =    benchmark.run()
							self.check_results_dict_not_empty(results.time_train_result							)
							self.check_results_dict_not_empty(results.memory_train_result							)


			def       lowercase_    (      self							)    ->      Any:



							'''simple docstring'''
							__lowerCamelCase		       =    'sshleifer/tiny-gpt2'
							__lowerCamelCase		       =    AutoConfig.from_pretrained(lowerCamelCase__							)
							__lowerCamelCase		       =    PyTorchBenchmarkArguments(
							    models=[MODEL_ID]			,						training=lowerCamelCase__			,						inference=lowerCamelCase__			,						sequence_lengths=[8]			,						batch_sizes=[1]			,						multi_process=lowerCamelCase__			,						)
							__lowerCamelCase		       =    PyTorchBenchmark(lowerCamelCase__			,						configs=[config]							)
							__lowerCamelCase		       =    benchmark.run()
							self.check_results_dict_not_empty(results.time_inference_result							)
							self.check_results_dict_not_empty(results.memory_inference_result							)


			def       lowercase_    (      self							)    ->      Optional[int]:



							'''simple docstring'''
							__lowerCamelCase		       =    'sshleifer/tinier_bart'
							__lowerCamelCase		       =    AutoConfig.from_pretrained(lowerCamelCase__							)
							__lowerCamelCase		       =    PyTorchBenchmarkArguments(
							    models=[MODEL_ID]			,						training=lowerCamelCase__			,						inference=lowerCamelCase__			,						sequence_lengths=[8]			,						batch_sizes=[1]			,						multi_process=lowerCamelCase__			,						)
							__lowerCamelCase		       =    PyTorchBenchmark(lowerCamelCase__			,						configs=[config]							)
							__lowerCamelCase		       =    benchmark.run()
							self.check_results_dict_not_empty(results.time_inference_result							)
							self.check_results_dict_not_empty(results.memory_inference_result							)


			def       lowercase_    (      self							)    ->      Tuple:



							'''simple docstring'''
							__lowerCamelCase		       =    'sshleifer/tiny-gpt2'
							__lowerCamelCase		       =    AutoConfig.from_pretrained(lowerCamelCase__							)
							__lowerCamelCase		       =    PyTorchBenchmarkArguments(
							    models=[MODEL_ID]			,						training=lowerCamelCase__			,						inference=lowerCamelCase__			,						sequence_lengths=[8]			,						batch_sizes=[1]			,						multi_process=lowerCamelCase__			,						)
							__lowerCamelCase		       =    PyTorchBenchmark(lowerCamelCase__			,						configs=[config]							)
							__lowerCamelCase		       =    benchmark.run()
							self.check_results_dict_not_empty(results.time_train_result							)
							self.check_results_dict_not_empty(results.memory_train_result							)


			def       lowercase_    (      self							)    ->      Optional[int]:



							'''simple docstring'''
							__lowerCamelCase		       =    'sshleifer/tinier_bart'
							__lowerCamelCase		       =    AutoConfig.from_pretrained(lowerCamelCase__							)
							__lowerCamelCase		       =    PyTorchBenchmarkArguments(
							    models=[MODEL_ID]			,						training=lowerCamelCase__			,						inference=lowerCamelCase__			,						sequence_lengths=[8]			,						batch_sizes=[1]			,						multi_process=lowerCamelCase__			,						)
							__lowerCamelCase		       =    PyTorchBenchmark(lowerCamelCase__			,						configs=[config]							)
							__lowerCamelCase		       =    benchmark.run()
							self.check_results_dict_not_empty(results.time_train_result							)
							self.check_results_dict_not_empty(results.memory_train_result							)


			def       lowercase_    (      self							)    ->      Optional[Any]:



							'''simple docstring'''
							__lowerCamelCase		       =    'sshleifer/tiny-gpt2'
							with tempfile.TemporaryDirectory() as tmp_dir:
											__lowerCamelCase		       =    PyTorchBenchmarkArguments(
											    models=[MODEL_ID]			,						training=lowerCamelCase__			,						inference=lowerCamelCase__			,						save_to_csv=lowerCamelCase__			,						sequence_lengths=[8]			,						batch_sizes=[1]			,						inference_time_csv_file=os.path.join(lowerCamelCase__			,						'inf_time.csv'							)			,						train_memory_csv_file=os.path.join(lowerCamelCase__			,						'train_mem.csv'							)			,						inference_memory_csv_file=os.path.join(lowerCamelCase__			,						'inf_mem.csv'							)			,						train_time_csv_file=os.path.join(lowerCamelCase__			,						'train_time.csv'							)			,						env_info_csv_file=os.path.join(lowerCamelCase__			,						'env.csv'							)			,						multi_process=lowerCamelCase__			,						)
											__lowerCamelCase		       =    PyTorchBenchmark(lowerCamelCase__							)
											benchmark.run()
											self.assertTrue(Path(os.path.join(lowerCamelCase__			,						'inf_time.csv'							)							).exists()							)
											self.assertTrue(Path(os.path.join(lowerCamelCase__			,						'train_time.csv'							)							).exists()							)
											self.assertTrue(Path(os.path.join(lowerCamelCase__			,						'inf_mem.csv'							)							).exists()							)
											self.assertTrue(Path(os.path.join(lowerCamelCase__			,						'train_mem.csv'							)							).exists()							)
											self.assertTrue(Path(os.path.join(lowerCamelCase__			,						'env.csv'							)							).exists()							)






			def       lowercase_    (      self							)    ->      List[str]:



							'''simple docstring'''
							__lowerCamelCase		       =    'sshleifer/tiny-gpt2'

							def _check_summary_is_not_empty(lowerCamelCase__							):
											self.assertTrue(hasattr(lowerCamelCase__			,						'sequential'							)							)
											self.assertTrue(hasattr(lowerCamelCase__			,						'cumulative'							)							)
											self.assertTrue(hasattr(lowerCamelCase__			,						'current'							)							)
											self.assertTrue(hasattr(lowerCamelCase__			,						'total'							)							)

							with tempfile.TemporaryDirectory() as tmp_dir:
											__lowerCamelCase		       =    PyTorchBenchmarkArguments(
											    models=[MODEL_ID]			,						training=lowerCamelCase__			,						inference=lowerCamelCase__			,						sequence_lengths=[8]			,						batch_sizes=[1]			,						log_filename=os.path.join(lowerCamelCase__			,						'log.txt'							)			,						log_print=lowerCamelCase__			,						trace_memory_line_by_line=lowerCamelCase__			,						multi_process=lowerCamelCase__			,						)
											__lowerCamelCase		       =    PyTorchBenchmark(lowerCamelCase__							)
											__lowerCamelCase		       =    benchmark.run()
											_check_summary_is_not_empty(result.inference_summary							)
											_check_summary_is_not_empty(result.train_summary							)
											self.assertTrue(Path(os.path.join(lowerCamelCase__			,						'log.txt'							)							).exists()							)








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_speech_available, is_torch_available


__A        =    {
    "configuration_audio_spectrogram_transformer": [
        "AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "ASTConfig",
    ]
}

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__A        =    [
					    "AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "ASTForAudioClassification",
					    "ASTModel",
					    "ASTPreTrainedModel",
					]

try:
					if not is_speech_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__A        =    ["ASTFeatureExtractor"]

if TYPE_CHECKING:
					from .configuration_audio_spectrogram_transformer import (
					    AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
					    ASTConfig,
					)

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_audio_spectrogram_transformer import (
										    AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
										    ASTForAudioClassification,
										    ASTModel,
										    ASTPreTrainedModel,
										)

					try:
										if not is_speech_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .feature_extraction_audio_spectrogram_transformer import ASTFeatureExtractor


else:
					import sys

					__A        =    _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)


