


import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
   import tensorflow as tf

   from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments







@require_tf
class lowerCAmelCase_		(				unittest.TestCase ):

 '''simple docstring'''

 def 		UpperCamelCase__  (			self	,   _UpperCAmelCase      ):
       for model_result in results.values():
             for batch_size, sequence_length in zip(model_result['''bs''']	,   model_result['''ss''']      ):
                   snake_case_ =   model_result["result"][batch_size][sequence_length]
                   self.assertIsNotNone(lowerCAmelCase__      )



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   "sshleifer/tiny-gpt2"
       snake_case_ =   TensorFlowBenchmarkArguments(
           models=[MODEL_ID]	,   training=lowerCAmelCase__	,   inference=lowerCAmelCase__	,   sequence_lengths=[8]	,   batch_sizes=[1]	,   eager_mode=lowerCAmelCase__	,   multi_process=lowerCAmelCase__	,   )
       snake_case_ =   TensorFlowBenchmark(lowerCAmelCase__      )
       snake_case_ =   benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result      )
       self.check_results_dict_not_empty(results.memory_inference_result      )



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   "sgugger/tiny-distilbert-classification"
       snake_case_ =   TensorFlowBenchmarkArguments(
           models=[MODEL_ID]	,   training=lowerCAmelCase__	,   inference=lowerCAmelCase__	,   sequence_lengths=[8]	,   batch_sizes=[1]	,   multi_process=lowerCAmelCase__	,   only_pretrain_model=lowerCAmelCase__	,   )
       snake_case_ =   TensorFlowBenchmark(lowerCAmelCase__      )
       snake_case_ =   benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result      )
       self.check_results_dict_not_empty(results.memory_inference_result      )



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   "sshleifer/tiny-gpt2"
       snake_case_ =   TensorFlowBenchmarkArguments(
           models=[MODEL_ID]	,   training=lowerCAmelCase__	,   inference=lowerCAmelCase__	,   sequence_lengths=[8]	,   batch_sizes=[1]	,   multi_process=lowerCAmelCase__	,   )
       snake_case_ =   TensorFlowBenchmark(lowerCAmelCase__      )
       snake_case_ =   benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result      )
       self.check_results_dict_not_empty(results.memory_inference_result      )



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   "sshleifer/tiny-gpt2"
       snake_case_ =   AutoConfig.from_pretrained(lowerCAmelCase__      )
       snake_case_ =   TensorFlowBenchmarkArguments(
           models=[MODEL_ID]	,   training=lowerCAmelCase__	,   inference=lowerCAmelCase__	,   sequence_lengths=[8]	,   batch_sizes=[1]	,   eager_mode=lowerCAmelCase__	,   multi_process=lowerCAmelCase__	,   )
       snake_case_ =   TensorFlowBenchmark(lowerCAmelCase__	,   [config]      )
       snake_case_ =   benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result      )
       self.check_results_dict_not_empty(results.memory_inference_result      )



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   "sshleifer/tiny-gpt2"
       snake_case_ =   AutoConfig.from_pretrained(lowerCAmelCase__      )
       snake_case_ =   TensorFlowBenchmarkArguments(
           models=[MODEL_ID]	,   training=lowerCAmelCase__	,   inference=lowerCAmelCase__	,   sequence_lengths=[8]	,   batch_sizes=[1]	,   multi_process=lowerCAmelCase__	,   )
       snake_case_ =   TensorFlowBenchmark(lowerCAmelCase__	,   [config]      )
       snake_case_ =   benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result      )
       self.check_results_dict_not_empty(results.memory_inference_result      )



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   "sshleifer/tiny-gpt2"
       snake_case_ =   TensorFlowBenchmarkArguments(
           models=[MODEL_ID]	,   training=lowerCAmelCase__	,   inference=lowerCAmelCase__	,   sequence_lengths=[8]	,   batch_sizes=[1]	,   multi_process=lowerCAmelCase__	,   )
       snake_case_ =   TensorFlowBenchmark(lowerCAmelCase__      )
       snake_case_ =   benchmark.run()
       self.check_results_dict_not_empty(results.time_train_result      )
       self.check_results_dict_not_empty(results.memory_train_result      )



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   "sshleifer/tiny-gpt2"
       snake_case_ =   AutoConfig.from_pretrained(lowerCAmelCase__      )
       snake_case_ =   TensorFlowBenchmarkArguments(
           models=[MODEL_ID]	,   training=lowerCAmelCase__	,   inference=lowerCAmelCase__	,   sequence_lengths=[8]	,   batch_sizes=[1]	,   multi_process=lowerCAmelCase__	,   )
       snake_case_ =   TensorFlowBenchmark(lowerCAmelCase__	,   [config]      )
       snake_case_ =   benchmark.run()
       self.check_results_dict_not_empty(results.time_train_result      )
       self.check_results_dict_not_empty(results.memory_train_result      )



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   "patrickvonplaten/t5-tiny-random"
       snake_case_ =   AutoConfig.from_pretrained(lowerCAmelCase__      )
       snake_case_ =   TensorFlowBenchmarkArguments(
           models=[MODEL_ID]	,   training=lowerCAmelCase__	,   inference=lowerCAmelCase__	,   sequence_lengths=[8]	,   batch_sizes=[1]	,   multi_process=lowerCAmelCase__	,   )
       snake_case_ =   TensorFlowBenchmark(lowerCAmelCase__	,   configs=[config]      )
       snake_case_ =   benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result      )
       self.check_results_dict_not_empty(results.memory_inference_result      )



 @unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices('''GPU'''      )      ) == 0	,   '''Cannot do xla on CPU.'''      )
 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   "sshleifer/tiny-gpt2"
       snake_case_ =   TensorFlowBenchmarkArguments(
           models=[MODEL_ID]	,   training=lowerCAmelCase__	,   inference=lowerCAmelCase__	,   sequence_lengths=[8]	,   batch_sizes=[1]	,   use_xla=lowerCAmelCase__	,   multi_process=lowerCAmelCase__	,   )
       snake_case_ =   TensorFlowBenchmark(lowerCAmelCase__      )
       snake_case_ =   benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result      )
       self.check_results_dict_not_empty(results.memory_inference_result      )



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   "sshleifer/tiny-gpt2"
       with tempfile.TemporaryDirectory() as tmp_dir:
             snake_case_ =   TensorFlowBenchmarkArguments(
                 models=[MODEL_ID]	,   inference=lowerCAmelCase__	,   save_to_csv=lowerCAmelCase__	,   sequence_lengths=[8]	,   batch_sizes=[1]	,   inference_time_csv_file=os.path.join(lowerCAmelCase__	,   '''inf_time.csv'''      )	,   inference_memory_csv_file=os.path.join(lowerCAmelCase__	,   '''inf_mem.csv'''      )	,   env_info_csv_file=os.path.join(lowerCAmelCase__	,   '''env.csv'''      )	,   multi_process=lowerCAmelCase__	,   )
             snake_case_ =   TensorFlowBenchmark(lowerCAmelCase__      )
             benchmark.run()
             self.assertTrue(Path(os.path.join(lowerCAmelCase__	,   '''inf_time.csv'''      )      ).exists()      )
             self.assertTrue(Path(os.path.join(lowerCAmelCase__	,   '''inf_mem.csv'''      )      ).exists()      )
             self.assertTrue(Path(os.path.join(lowerCAmelCase__	,   '''env.csv'''      )      ).exists()      )





 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   "sshleifer/tiny-gpt2"

       def _check_summary_is_not_empty(_UpperCAmelCase      ):
             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:
             snake_case_ =   TensorFlowBenchmarkArguments(
                 models=[MODEL_ID]	,   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__	,   eager_mode=lowerCAmelCase__	,   multi_process=lowerCAmelCase__	,   )
             snake_case_ =   TensorFlowBenchmark(lowerCAmelCase__      )
             snake_case_ =   benchmark.run()
             _check_summary_is_not_empty(result.inference_summary      )
             self.assertTrue(Path(os.path.join(lowerCAmelCase__	,   '''log.txt'''      )      ).exists()      )




from typing import List, Optional, Union

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


UpperCAmelCase    =					logging.get_logger(__name__)

UpperCAmelCase    =					{
    """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 lowerCAmelCase_		(				lowerCamelCase__ ):

 '''simple docstring'''

 __snake_case				=						"informer"
 __snake_case				=						{
     "hidden_size": "d_model",
     "num_attention_heads": "encoder_attention_heads",
     "num_hidden_layers": "encoder_layers",
 }



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

       # set cardinality
       if cardinality 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`'''      )
             snake_case_ =   cardinality
       else:
             snake_case_ =   [0]

       # set embedding_dimension
       if embedding_dimension 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`'''      )
             snake_case_ =   embedding_dimension
       else:
             snake_case_ =   [min(50	,   (cat + 1) // 2      ) for cat in self.cardinality]

       snake_case_ =   num_parallel_samples

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

       snake_case_ =   dropout
       snake_case_ =   attention_dropout
       snake_case_ =   activation_dropout
       snake_case_ =   encoder_layerdrop
       snake_case_ =   decoder_layerdrop

       snake_case_ =   activation_function
       snake_case_ =   init_std

       snake_case_ =   use_cache

       # Informer
       snake_case_ =   attention_type
       snake_case_ =   sampling_factor
       snake_case_ =   distil

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





 @property
 def 		UpperCamelCase__  (			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
       )