




from typing import List, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowerCamelCase      :  Optional[Any]      						=						logging.get_logger(__name__)

_lowerCamelCase      :  Tuple      						=						{
    "huggingface/autoformer-tourism-monthly": "https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json",
}
class        __snake_case  (__snake_case		):
				lowerCAmelCase__				=     "autoformer"
				lowerCAmelCase__				=     {
				    "hidden_size": "d_model",
				    "num_attention_heads": "encoder_attention_heads",
				    "num_hidden_layers": "encoder_layers",
				}



				def __init__(      self      :		str		,	_UpperCAmelCase      :		List[Any] = None		,	_UpperCAmelCase      :		Optional[int] = None		,	_UpperCAmelCase      :		str = "student_t"		,	_UpperCAmelCase      :		List[Any] = "nll"		,	_UpperCAmelCase      :		List[str] = 1		,	_UpperCAmelCase      :		str = [1, 2, 3, 4, 5, 6, 7]		,	_UpperCAmelCase      :		Optional[Any] = True		,	_UpperCAmelCase      :		Optional[Any] = 0		,	_UpperCAmelCase      :		Tuple = 0		,	_UpperCAmelCase      :		Optional[Any] = 0		,	_UpperCAmelCase      :		Optional[Any] = 0		,	_UpperCAmelCase      :		Optional[Any] = None		,	_UpperCAmelCase      :		Optional[Any] = None		,	_UpperCAmelCase      :		Tuple = 64		,	_UpperCAmelCase      :		Optional[Any] = 2		,	_UpperCAmelCase      :		Union[str, Any] = 2		,	_UpperCAmelCase      :		Dict = 2		,	_UpperCAmelCase      :		Optional[int] = 2		,	_UpperCAmelCase      :		int = 32		,	_UpperCAmelCase      :		int = 32		,	_UpperCAmelCase      :		str = "gelu"		,	_UpperCAmelCase      :		List[str] = 0.1		,	_UpperCAmelCase      :		List[str] = 0.1		,	_UpperCAmelCase      :		Tuple = 0.1		,	_UpperCAmelCase      :		List[str] = 0.1		,	_UpperCAmelCase      :		List[Any] = 0.1		,	_UpperCAmelCase      :		Any = 100		,	_UpperCAmelCase      :		Tuple = 0.02		,	_UpperCAmelCase      :		Union[str, Any] = True		,	_UpperCAmelCase      :		Tuple=True		,	_UpperCAmelCase      :		Union[str, Any] = 10		,	_UpperCAmelCase      :		Optional[Any] = 25		,	_UpperCAmelCase      :		Tuple = 3		,	**_UpperCAmelCase      :		Union[str, Any]		,	)  ->    Dict:





								'''simple docstring'''




								_lowerCAmelCase    :    List[Any]										=      prediction_length
								_lowerCAmelCase    :    Tuple										=      context_length if context_length is not None else prediction_length
								_lowerCAmelCase    :    Optional[int]										=      distribution_output
								_lowerCAmelCase    :    Tuple										=      loss
								_lowerCAmelCase    :    Union[str, Any]										=      input_size
								_lowerCAmelCase    :    Tuple										=      num_time_features
								_lowerCAmelCase    :    Any										=      lags_sequence
								_lowerCAmelCase    :    Dict										=      scaling
								_lowerCAmelCase    :    str										=      num_dynamic_real_features
								_lowerCAmelCase    :    Optional[Any]										=      num_static_real_features
								_lowerCAmelCase    :    Any										=      num_static_categorical_features
								if cardinality is not None and num_static_categorical_features > 0:
												if len(__UpperCamelCase      ) != num_static_categorical_features:
																raise ValueError(
																    """The cardinality should be a list of the same length as `num_static_categorical_features`"""      )
												_lowerCAmelCase    :    int										=      cardinality
								else:
												_lowerCAmelCase    :    List[str]										=      [0]
								if embedding_dimension is not None and num_static_categorical_features > 0:
												if len(__UpperCamelCase      ) != num_static_categorical_features:
																raise ValueError(
																    """The embedding dimension should be a list of the same length as `num_static_categorical_features`"""      )
												_lowerCAmelCase    :    str										=      embedding_dimension
								else:
												_lowerCAmelCase    :    Union[str, Any]										=      [min(50		,	(cat + 1) // 2      ) for cat in self.cardinality]
								_lowerCAmelCase    :    Any										=      num_parallel_samples

								# Transformer architecture configuration
								_lowerCAmelCase    :    Any										=      input_size * len(self.lags_sequence      ) + self._number_of_features
								_lowerCAmelCase    :    List[Any]										=      d_model
								_lowerCAmelCase    :    List[Any]										=      encoder_attention_heads
								_lowerCAmelCase    :    List[Any]										=      decoder_attention_heads
								_lowerCAmelCase    :    List[str]										=      encoder_ffn_dim
								_lowerCAmelCase    :    List[str]										=      decoder_ffn_dim
								_lowerCAmelCase    :    List[str]										=      encoder_layers
								_lowerCAmelCase    :    List[Any]										=      decoder_layers

								_lowerCAmelCase    :    Union[str, Any]										=      dropout
								_lowerCAmelCase    :    int										=      attention_dropout
								_lowerCAmelCase    :    Union[str, Any]										=      activation_dropout
								_lowerCAmelCase    :    Union[str, Any]										=      encoder_layerdrop
								_lowerCAmelCase    :    List[str]										=      decoder_layerdrop

								_lowerCAmelCase    :    List[str]										=      activation_function
								_lowerCAmelCase    :    List[Any]										=      init_std

								_lowerCAmelCase    :    Any										=      use_cache

								# Autoformer
								_lowerCAmelCase    :    Dict										=      label_length
								_lowerCAmelCase    :    Any										=      moving_average
								_lowerCAmelCase    :    Tuple										=      autocorrelation_factor

								super().__init__(is_encoder_decoder=__UpperCamelCase		,	**__UpperCamelCase      )



				@property
				def       SCREAMING_SNAKE_CASE       (      self      :		Tuple      )  ->    Tuple:





								'''simple docstring'''




								return (
								    sum(self.embedding_dimension      )
								    + self.num_dynamic_real_features
								    + self.num_time_features
								    + self.num_static_real_features
								    + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
								)







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        __snake_case  (unittest.TestCase		):



				def       SCREAMING_SNAKE_CASE       (      self      :		Tuple		,	_UpperCAmelCase      :		List[str]      )  ->    List[str]:





								'''simple docstring'''




								for model_result in results.values():
												for batch_size, sequence_length in zip(model_result["""bs"""]		,	model_result["""ss"""]      ):
																_lowerCAmelCase    :    List[Any]										=      model_result["""result"""][batch_size][sequence_length]
																self.assertIsNotNone(_UpperCAmelCase      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Dict      )  ->    List[Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    Any										=      """sshleifer/tiny-gpt2"""
								_lowerCAmelCase    :    Dict										=      PyTorchBenchmarkArguments(
								    models=[MODEL_ID]		,	training=_UpperCAmelCase		,	inference=_UpperCAmelCase		,	sequence_lengths=[8]		,	batch_sizes=[1]		,	multi_process=_UpperCAmelCase		,	)
								_lowerCAmelCase    :    str										=      PyTorchBenchmark(_UpperCAmelCase      )
								_lowerCAmelCase    :    Optional[int]										=      benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result      )
								self.check_results_dict_not_empty(results.memory_inference_result      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Optional[int]      )  ->    Any:





								'''simple docstring'''




								_lowerCAmelCase    :    List[str]										=      """sgugger/tiny-distilbert-classification"""
								_lowerCAmelCase    :    Tuple										=      PyTorchBenchmarkArguments(
								    models=[MODEL_ID]		,	training=_UpperCAmelCase		,	inference=_UpperCAmelCase		,	sequence_lengths=[8]		,	batch_sizes=[1]		,	multi_process=_UpperCAmelCase		,	only_pretrain_model=_UpperCAmelCase		,	)
								_lowerCAmelCase    :    List[Any]										=      PyTorchBenchmark(_UpperCAmelCase      )
								_lowerCAmelCase    :    Optional[Any]										=      benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result      )
								self.check_results_dict_not_empty(results.memory_inference_result      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Dict      )  ->    int:





								'''simple docstring'''




								_lowerCAmelCase    :    Tuple										=      """sshleifer/tiny-gpt2"""
								_lowerCAmelCase    :    Union[str, Any]										=      PyTorchBenchmarkArguments(
								    models=[MODEL_ID]		,	training=_UpperCAmelCase		,	inference=_UpperCAmelCase		,	torchscript=_UpperCAmelCase		,	sequence_lengths=[8]		,	batch_sizes=[1]		,	multi_process=_UpperCAmelCase		,	)
								_lowerCAmelCase    :    List[str]										=      PyTorchBenchmark(_UpperCAmelCase      )
								_lowerCAmelCase    :    Optional[Any]										=      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       SCREAMING_SNAKE_CASE       (      self      :		Dict      )  ->    str:





								'''simple docstring'''




								_lowerCAmelCase    :    Optional[Any]										=      """sshleifer/tiny-gpt2"""
								_lowerCAmelCase    :    Optional[Any]										=      PyTorchBenchmarkArguments(
								    models=[MODEL_ID]		,	training=_UpperCAmelCase		,	inference=_UpperCAmelCase		,	fpaa=_UpperCAmelCase		,	sequence_lengths=[8]		,	batch_sizes=[1]		,	multi_process=_UpperCAmelCase		,	)
								_lowerCAmelCase    :    Union[str, Any]										=      PyTorchBenchmark(_UpperCAmelCase      )
								_lowerCAmelCase    :    Optional[Any]										=      benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result      )
								self.check_results_dict_not_empty(results.memory_inference_result      )



				def       SCREAMING_SNAKE_CASE       (      self      :		str      )  ->    int:





								'''simple docstring'''




								_lowerCAmelCase    :    List[Any]										=      """sshleifer/tiny-gpt2"""
								_lowerCAmelCase    :    int										=      AutoConfig.from_pretrained(_UpperCAmelCase      )
								# set architectures equal to `None`
								_lowerCAmelCase    :    Optional[int]										=      None
								_lowerCAmelCase    :    Union[str, Any]										=      PyTorchBenchmarkArguments(
								    models=[MODEL_ID]		,	training=_UpperCAmelCase		,	inference=_UpperCAmelCase		,	sequence_lengths=[8]		,	batch_sizes=[1]		,	multi_process=_UpperCAmelCase		,	)
								_lowerCAmelCase    :    Tuple										=      PyTorchBenchmark(_UpperCAmelCase		,	configs=[config]      )
								_lowerCAmelCase    :    List[Any]										=      benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result      )
								self.check_results_dict_not_empty(results.memory_inference_result      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Dict      )  ->    str:





								'''simple docstring'''




								_lowerCAmelCase    :    str										=      """sshleifer/tiny-gpt2"""
								_lowerCAmelCase    :    List[str]										=      PyTorchBenchmarkArguments(
								    models=[MODEL_ID]		,	training=_UpperCAmelCase		,	inference=_UpperCAmelCase		,	sequence_lengths=[8]		,	batch_sizes=[1]		,	multi_process=_UpperCAmelCase		,	)
								_lowerCAmelCase    :    List[str]										=      PyTorchBenchmark(_UpperCAmelCase      )
								_lowerCAmelCase    :    Union[str, Any]										=      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       SCREAMING_SNAKE_CASE       (      self      :		Any      )  ->    List[Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    int										=      """sshleifer/tiny-gpt2"""
								_lowerCAmelCase    :    Dict										=      PyTorchBenchmarkArguments(
								    models=[MODEL_ID]		,	training=_UpperCAmelCase		,	inference=_UpperCAmelCase		,	sequence_lengths=[8]		,	batch_sizes=[1]		,	fpaa=_UpperCAmelCase		,	multi_process=_UpperCAmelCase		,	)
								_lowerCAmelCase    :    Union[str, Any]										=      PyTorchBenchmark(_UpperCAmelCase      )
								_lowerCAmelCase    :    List[str]										=      benchmark.run()
								self.check_results_dict_not_empty(results.time_train_result      )
								self.check_results_dict_not_empty(results.memory_train_result      )



				def       SCREAMING_SNAKE_CASE       (      self      :		List[Any]      )  ->    List[str]:





								'''simple docstring'''




								_lowerCAmelCase    :    Union[str, Any]										=      """sshleifer/tiny-gpt2"""
								_lowerCAmelCase    :    List[Any]										=      AutoConfig.from_pretrained(_UpperCAmelCase      )
								_lowerCAmelCase    :    List[Any]										=      PyTorchBenchmarkArguments(
								    models=[MODEL_ID]		,	training=_UpperCAmelCase		,	inference=_UpperCAmelCase		,	sequence_lengths=[8]		,	batch_sizes=[1]		,	multi_process=_UpperCAmelCase		,	)
								_lowerCAmelCase    :    List[str]										=      PyTorchBenchmark(_UpperCAmelCase		,	configs=[config]      )
								_lowerCAmelCase    :    Optional[int]										=      benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result      )
								self.check_results_dict_not_empty(results.memory_inference_result      )



				def       SCREAMING_SNAKE_CASE       (      self      :		int      )  ->    Union[str, Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    Any										=      """sshleifer/tinier_bart"""
								_lowerCAmelCase    :    Tuple										=      AutoConfig.from_pretrained(_UpperCAmelCase      )
								_lowerCAmelCase    :    Dict										=      PyTorchBenchmarkArguments(
								    models=[MODEL_ID]		,	training=_UpperCAmelCase		,	inference=_UpperCAmelCase		,	sequence_lengths=[8]		,	batch_sizes=[1]		,	multi_process=_UpperCAmelCase		,	)
								_lowerCAmelCase    :    Tuple										=      PyTorchBenchmark(_UpperCAmelCase		,	configs=[config]      )
								_lowerCAmelCase    :    Any										=      benchmark.run()
								self.check_results_dict_not_empty(results.time_inference_result      )
								self.check_results_dict_not_empty(results.memory_inference_result      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Optional[int]      )  ->    List[Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    List[Any]										=      """sshleifer/tiny-gpt2"""
								_lowerCAmelCase    :    Optional[int]										=      AutoConfig.from_pretrained(_UpperCAmelCase      )
								_lowerCAmelCase    :    Any										=      PyTorchBenchmarkArguments(
								    models=[MODEL_ID]		,	training=_UpperCAmelCase		,	inference=_UpperCAmelCase		,	sequence_lengths=[8]		,	batch_sizes=[1]		,	multi_process=_UpperCAmelCase		,	)
								_lowerCAmelCase    :    Optional[Any]										=      PyTorchBenchmark(_UpperCAmelCase		,	configs=[config]      )
								_lowerCAmelCase    :    Optional[Any]										=      benchmark.run()
								self.check_results_dict_not_empty(results.time_train_result      )
								self.check_results_dict_not_empty(results.memory_train_result      )



				def       SCREAMING_SNAKE_CASE       (      self      :		int      )  ->    Union[str, Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    Any										=      """sshleifer/tinier_bart"""
								_lowerCAmelCase    :    Dict										=      AutoConfig.from_pretrained(_UpperCAmelCase      )
								_lowerCAmelCase    :    Any										=      PyTorchBenchmarkArguments(
								    models=[MODEL_ID]		,	training=_UpperCAmelCase		,	inference=_UpperCAmelCase		,	sequence_lengths=[8]		,	batch_sizes=[1]		,	multi_process=_UpperCAmelCase		,	)
								_lowerCAmelCase    :    Optional[Any]										=      PyTorchBenchmark(_UpperCAmelCase		,	configs=[config]      )
								_lowerCAmelCase    :    int										=      benchmark.run()
								self.check_results_dict_not_empty(results.time_train_result      )
								self.check_results_dict_not_empty(results.memory_train_result      )



				def       SCREAMING_SNAKE_CASE       (      self      :		int      )  ->    int:





								'''simple docstring'''




								_lowerCAmelCase    :    Tuple										=      """sshleifer/tiny-gpt2"""
								with tempfile.TemporaryDirectory() as tmp_dir:
												_lowerCAmelCase    :    Optional[int]										=      PyTorchBenchmarkArguments(
												    models=[MODEL_ID]		,	training=_UpperCAmelCase		,	inference=_UpperCAmelCase		,	save_to_csv=_UpperCAmelCase		,	sequence_lengths=[8]		,	batch_sizes=[1]		,	inference_time_csv_file=os.path.join(_UpperCAmelCase		,	"""inf_time.csv"""      )		,	train_memory_csv_file=os.path.join(_UpperCAmelCase		,	"""train_mem.csv"""      )		,	inference_memory_csv_file=os.path.join(_UpperCAmelCase		,	"""inf_mem.csv"""      )		,	train_time_csv_file=os.path.join(_UpperCAmelCase		,	"""train_time.csv"""      )		,	env_info_csv_file=os.path.join(_UpperCAmelCase		,	"""env.csv"""      )		,	multi_process=_UpperCAmelCase		,	)
												_lowerCAmelCase    :    List[Any]										=      PyTorchBenchmark(_UpperCAmelCase      )
												benchmark.run()
												self.assertTrue(Path(os.path.join(_UpperCAmelCase		,	"""inf_time.csv"""      )      ).exists()      )
												self.assertTrue(Path(os.path.join(_UpperCAmelCase		,	"""train_time.csv"""      )      ).exists()      )
												self.assertTrue(Path(os.path.join(_UpperCAmelCase		,	"""inf_mem.csv"""      )      ).exists()      )
												self.assertTrue(Path(os.path.join(_UpperCAmelCase		,	"""train_mem.csv"""      )      ).exists()      )
												self.assertTrue(Path(os.path.join(_UpperCAmelCase		,	"""env.csv"""      )      ).exists()      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Union[str, Any]      )  ->    Dict:





								'''simple docstring'''




								_lowerCAmelCase    :    str										=      """sshleifer/tiny-gpt2"""

								def _check_summary_is_not_empty(_UpperCAmelCase      :		int      ):
												self.assertTrue(hasattr(_UpperCAmelCase		,	"""sequential"""      )      )
												self.assertTrue(hasattr(_UpperCAmelCase		,	"""cumulative"""      )      )
												self.assertTrue(hasattr(_UpperCAmelCase		,	"""current"""      )      )
												self.assertTrue(hasattr(_UpperCAmelCase		,	"""total"""      )      )

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

