
"""simple docstring"""





import json
import os
import subprocess
import unittest
from ast import literal_eval

import pytest
from parameterized import parameterized_class

from . import is_sagemaker_available


if is_sagemaker_available():
							from sagemaker import Session, TrainingJobAnalytics
							from sagemaker.huggingface import HuggingFace
@pytest.mark.skipif(
    literal_eval(os.getenv('''TEST_SAGEMAKER'''     ,				'''False''' ) ) is not True     ,				reason='''Skipping test because should only be run when releasing minor transformers version'''     ,				)
@pytest.mark.usefixtures('''sm_env''' )
@parameterized_class(
    [
        {
            '''framework''': '''pytorch''',
            '''script''': '''run_glue.py''',
            '''model_name_or_path''': '''distilbert-base-cased''',
            '''instance_type''': '''ml.g4dn.xlarge''',
            '''results''': {'''train_runtime''': 650, '''eval_accuracy''': 0.6, '''eval_loss''': 0.9},
        },
        {
            '''framework''': '''tensorflow''',
            '''script''': '''run_tf.py''',
            '''model_name_or_path''': '''distilbert-base-cased''',
            '''instance_type''': '''ml.g4dn.xlarge''',
            '''results''': {'''train_runtime''': 600, '''eval_accuracy''': 0.3, '''eval_loss''': 0.9},
        },
    ] )
class 						SCREAMING_SNAKE_CASE_ (						unittest.TestCase ):




		"""simple docstring"""





		def 	snake_case_     (     self):
				if self.framework == "pytorch":
						subprocess.run(
						    f"cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py".split()							,  encoding="""utf-8"""							,  check=lowerCAmelCase__							,  )
				assert hasattr(self							,  """env""")




		def 	snake_case_     (     self							,  lowerCAmelCase__=1):
				# creates estimator
				return HuggingFace(
				    entry_point=self.script							,  source_dir=self.env.test_path							,  role=self.env.role							,  image_uri=self.env.image_uri							,  base_job_name=f"{self.env.base_job_name}-single"							,  instance_count=lowerCAmelCase__							,  instance_type=self.instance_type							,  debugger_hook_config=lowerCAmelCase__							,  hyperparameters={**self.env.hyperparameters, """model_name_or_path""": self.model_name_or_path}							,  metric_definitions=self.env.metric_definitions							,  py_version="""py36"""							,  )




		def 	snake_case_     (     self							,  lowerCAmelCase__):
				TrainingJobAnalytics(lowerCAmelCase__).export_csv(f"{self.env.test_path}/{job_name}_metrics.csv")




		def 	snake_case_     (     self):
				# create estimator
				__SCREAMING_SNAKE_CASE            = self.create_estimator()

				# run training
				estimator.fit()

				# result dataframe
				__SCREAMING_SNAKE_CASE            = TrainingJobAnalytics(estimator.latest_training_job.name).dataframe()

				# extract kpis
				__SCREAMING_SNAKE_CASE            = list(result_metrics_df[result_metrics_df.metric_name == """eval_accuracy"""]["""value"""])
				__SCREAMING_SNAKE_CASE            = list(result_metrics_df[result_metrics_df.metric_name == """eval_loss"""]["""value"""])
				# get train time from SageMaker job, this includes starting, preprocessing, stopping
				__SCREAMING_SNAKE_CASE            = (
				    Session().describe_training_job(estimator.latest_training_job.name).get("""TrainingTimeInSeconds"""							,  9_9_9_9_9_9)
				)

				# assert kpis
				assert train_runtime <= self.results["train_runtime"]
				assert all(t >= self.results["""eval_accuracy"""] for t in eval_accuracy)
				assert all(t <= self.results["""eval_loss"""] for t in eval_loss)

				# dump tests result into json file to share in PR
				with open(f"{estimator.latest_training_job.name}.json"							,  """w""") as outfile:
						json.dump({"""train_time""": train_runtime, """eval_accuracy""": eval_accuracy, """eval_loss""": eval_loss}							,  lowerCAmelCase__)






"""simple docstring"""





from typing import List, Optional, Union

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


__magic_name__       =			logging.get_logger(__name__)

__magic_name__       =			{
    "huggingface/informer-tourism-monthly": (
        "https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json"
    ),
    # See all Informer models at https://huggingface.co/models?filter=informer
}
class 						SCREAMING_SNAKE_CASE_ (						__a ):




		"""simple docstring"""





		__lowercase							:  Dict						   =		'''informer'''
		__lowercase							:  Union[str, Any]						   =		{
		    '''hidden_size''': '''d_model''',
		    '''num_attention_heads''': '''encoder_attention_heads''',
		    '''num_hidden_layers''': '''encoder_layers''',
		}




		def __init__(     self							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = "student_t"							,  lowerCAmelCase__ = "nll"							,  lowerCAmelCase__ = 1							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = "mean"							,  lowerCAmelCase__ = 0							,  lowerCAmelCase__ = 0							,  lowerCAmelCase__ = 0							,  lowerCAmelCase__ = 0							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = 6_4							,  lowerCAmelCase__ = 3_2							,  lowerCAmelCase__ = 3_2							,  lowerCAmelCase__ = 2							,  lowerCAmelCase__ = 2							,  lowerCAmelCase__ = 2							,  lowerCAmelCase__ = 2							,  lowerCAmelCase__ = True							,  lowerCAmelCase__ = "gelu"							,  lowerCAmelCase__ = 0.05							,  lowerCAmelCase__ = 0.1							,  lowerCAmelCase__ = 0.1							,  lowerCAmelCase__ = 0.1							,  lowerCAmelCase__ = 0.1							,  lowerCAmelCase__ = 1_0_0							,  lowerCAmelCase__ = 0.02							,  lowerCAmelCase__=True							,  lowerCAmelCase__ = "prob"							,  lowerCAmelCase__ = 5							,  lowerCAmelCase__ = True							,  **lowerCAmelCase__							,  ):
				# time series specific configuration
				__SCREAMING_SNAKE_CASE            = prediction_length
				__SCREAMING_SNAKE_CASE            = context_length or prediction_length
				__SCREAMING_SNAKE_CASE            = distribution_output
				__SCREAMING_SNAKE_CASE            = loss
				__SCREAMING_SNAKE_CASE            = input_size
				__SCREAMING_SNAKE_CASE            = num_time_features
				__SCREAMING_SNAKE_CASE            = lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7]
				__SCREAMING_SNAKE_CASE            = scaling
				__SCREAMING_SNAKE_CASE            = num_dynamic_real_features
				__SCREAMING_SNAKE_CASE            = num_static_real_features
				__SCREAMING_SNAKE_CASE            = num_static_categorical_features

				# set cardinality
				if cardinality and num_static_categorical_features > 0:
						if len(lowerCAmelCase__) != num_static_categorical_features:
								raise ValueError(
								    """The cardinality should be a list of the same length as `num_static_categorical_features`""")
						__SCREAMING_SNAKE_CASE            = cardinality
				else:
						__SCREAMING_SNAKE_CASE            = [0]

				# set embedding_dimension
				if embedding_dimension and num_static_categorical_features > 0:
						if len(lowerCAmelCase__) != num_static_categorical_features:
								raise ValueError(
								    """The embedding dimension should be a list of the same length as `num_static_categorical_features`""")
						__SCREAMING_SNAKE_CASE            = embedding_dimension
				else:
						__SCREAMING_SNAKE_CASE            = [min(5_0							,  (cat + 1) // 2) for cat in self.cardinality]

				__SCREAMING_SNAKE_CASE            = num_parallel_samples

				# Transformer architecture configuration
				__SCREAMING_SNAKE_CASE            = input_size * len(self.lags_sequence) + self._number_of_features
				__SCREAMING_SNAKE_CASE            = d_model
				__SCREAMING_SNAKE_CASE            = encoder_attention_heads
				__SCREAMING_SNAKE_CASE            = decoder_attention_heads
				__SCREAMING_SNAKE_CASE            = encoder_ffn_dim
				__SCREAMING_SNAKE_CASE            = decoder_ffn_dim
				__SCREAMING_SNAKE_CASE            = encoder_layers
				__SCREAMING_SNAKE_CASE            = decoder_layers

				__SCREAMING_SNAKE_CASE            = dropout
				__SCREAMING_SNAKE_CASE            = attention_dropout
				__SCREAMING_SNAKE_CASE            = activation_dropout
				__SCREAMING_SNAKE_CASE            = encoder_layerdrop
				__SCREAMING_SNAKE_CASE            = decoder_layerdrop

				__SCREAMING_SNAKE_CASE            = activation_function
				__SCREAMING_SNAKE_CASE            = init_std

				__SCREAMING_SNAKE_CASE            = use_cache

				# Informer
				__SCREAMING_SNAKE_CASE            = attention_type
				__SCREAMING_SNAKE_CASE            = sampling_factor
				__SCREAMING_SNAKE_CASE            = distil

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




		@property
		def 	snake_case_     (     self):
				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
				)




