



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




  def 						SCREAMING_SNAKE_CASE    (			self					:						str						, __lowerCAmelCase					:						Union[str, Any]		):






         """simple docstring"""






         for model_result in results.values():
                for batch_size, sequence_length in zip(model_result['''bs''']						, model_result['''ss''']		):
                       _lowerCamelCase						:    Any         =			model_result['''result'''][batch_size][sequence_length]
                       self.assertIsNotNone(__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						List[str]		):






         """simple docstring"""






         _lowerCamelCase						:    Any         =			'''sshleifer/tiny-gpt2'''
         _lowerCamelCase						:    Union[str, Any]         =			TensorFlowBenchmarkArguments(
             models=[MODEL_ID]						, training=__lowerCAmelCase						, inference=__lowerCAmelCase						, sequence_lengths=[8]						, batch_sizes=[1]						, eager_mode=__lowerCAmelCase						, multi_process=__lowerCAmelCase						, )
         _lowerCamelCase						:    Any         =			TensorFlowBenchmark(__lowerCAmelCase		)
         _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					:						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:    Tuple         =			'''sgugger/tiny-distilbert-classification'''
         _lowerCamelCase						:    List[str]         =			TensorFlowBenchmarkArguments(
             models=[MODEL_ID]						, training=__lowerCAmelCase						, inference=__lowerCAmelCase						, sequence_lengths=[8]						, batch_sizes=[1]						, multi_process=__lowerCAmelCase						, only_pretrain_model=__lowerCAmelCase						, )
         _lowerCamelCase						:    Optional[int]         =			TensorFlowBenchmark(__lowerCAmelCase		)
         _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]		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			'''sshleifer/tiny-gpt2'''
         _lowerCamelCase						:    int         =			TensorFlowBenchmarkArguments(
             models=[MODEL_ID]						, training=__lowerCAmelCase						, inference=__lowerCAmelCase						, sequence_lengths=[8]						, batch_sizes=[1]						, multi_process=__lowerCAmelCase						, )
         _lowerCamelCase						:    int         =			TensorFlowBenchmark(__lowerCAmelCase		)
         _lowerCamelCase						:    Union[str, 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		):






         """simple docstring"""






         _lowerCamelCase						:    Union[str, Any]         =			'''sshleifer/tiny-gpt2'''
         _lowerCamelCase						:    Tuple         =			AutoConfig.from_pretrained(__lowerCAmelCase		)
         _lowerCamelCase						:    Optional[int]         =			TensorFlowBenchmarkArguments(
             models=[MODEL_ID]						, training=__lowerCAmelCase						, inference=__lowerCAmelCase						, sequence_lengths=[8]						, batch_sizes=[1]						, eager_mode=__lowerCAmelCase						, multi_process=__lowerCAmelCase						, )
         _lowerCamelCase						:    int         =			TensorFlowBenchmark(__lowerCAmelCase						, [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					:						List[Any]		):






         """simple docstring"""






         _lowerCamelCase						:    Union[str, Any]         =			'''sshleifer/tiny-gpt2'''
         _lowerCamelCase						:    int         =			AutoConfig.from_pretrained(__lowerCAmelCase		)
         _lowerCamelCase						:    Optional[Any]         =			TensorFlowBenchmarkArguments(
             models=[MODEL_ID]						, training=__lowerCAmelCase						, inference=__lowerCAmelCase						, sequence_lengths=[8]						, batch_sizes=[1]						, multi_process=__lowerCAmelCase						, )
         _lowerCamelCase						:    Optional[int]         =			TensorFlowBenchmark(__lowerCAmelCase						, [config]		)
         _lowerCamelCase						:    Tuple         =			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					:						Union[str, Any]		):






         """simple docstring"""






         _lowerCamelCase						:    Dict         =			'''sshleifer/tiny-gpt2'''
         _lowerCamelCase						:    Any         =			TensorFlowBenchmarkArguments(
             models=[MODEL_ID]						, training=__lowerCAmelCase						, inference=__lowerCAmelCase						, sequence_lengths=[8]						, batch_sizes=[1]						, multi_process=__lowerCAmelCase						, )
         _lowerCamelCase						:    str         =			TensorFlowBenchmark(__lowerCAmelCase		)
         _lowerCamelCase						:    Dict         =			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					:						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:    str         =			'''sshleifer/tiny-gpt2'''
         _lowerCamelCase						:    int         =			AutoConfig.from_pretrained(__lowerCAmelCase		)
         _lowerCamelCase						:    Dict         =			TensorFlowBenchmarkArguments(
             models=[MODEL_ID]						, training=__lowerCAmelCase						, inference=__lowerCAmelCase						, sequence_lengths=[8]						, batch_sizes=[1]						, multi_process=__lowerCAmelCase						, )
         _lowerCamelCase						:    List[Any]         =			TensorFlowBenchmark(__lowerCAmelCase						, [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					:						Tuple		):






         """simple docstring"""






         _lowerCamelCase						:    Dict         =			'''patrickvonplaten/t5-tiny-random'''
         _lowerCamelCase						:    Tuple         =			AutoConfig.from_pretrained(__lowerCAmelCase		)
         _lowerCamelCase						:    Optional[int]         =			TensorFlowBenchmarkArguments(
             models=[MODEL_ID]						, training=__lowerCAmelCase						, inference=__lowerCAmelCase						, sequence_lengths=[8]						, batch_sizes=[1]						, multi_process=__lowerCAmelCase						, )
         _lowerCamelCase						:    Tuple         =			TensorFlowBenchmark(__lowerCAmelCase						, configs=[config]		)
         _lowerCamelCase						:    Union[str, 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(is_tf_available() and len(tf.config.list_physical_devices('''GPU'''		)		) == 0						, '''Cannot do xla on CPU.'''		)
  def 						SCREAMING_SNAKE_CASE    (			self					:						int		):






         """simple docstring"""






         _lowerCamelCase						:    int         =			'''sshleifer/tiny-gpt2'''
         _lowerCamelCase						:    List[Any]         =			TensorFlowBenchmarkArguments(
             models=[MODEL_ID]						, training=__lowerCAmelCase						, inference=__lowerCAmelCase						, sequence_lengths=[8]						, batch_sizes=[1]						, use_xla=__lowerCAmelCase						, multi_process=__lowerCAmelCase						, )
         _lowerCamelCase						:    Tuple         =			TensorFlowBenchmark(__lowerCAmelCase		)
         _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					:						List[str]		):






         """simple docstring"""






         _lowerCamelCase						:    Any         =			'''sshleifer/tiny-gpt2'''
         with tempfile.TemporaryDirectory() as tmp_dir:
                _lowerCamelCase						:    int         =			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						, )
                _lowerCamelCase						:    List[str]         =			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 						SCREAMING_SNAKE_CASE    (			self					:						str		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			'''sshleifer/tiny-gpt2'''

         def _check_summary_is_not_empty(__lowerCAmelCase					:						Optional[int]		):
                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						:    int         =			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						, )
                _lowerCamelCase						:    int         =			TensorFlowBenchmark(__lowerCAmelCase		)
                _lowerCamelCase						:    int         =			benchmark.run()
                _check_summary_is_not_empty(result.inference_summary		)
                self.assertTrue(Path(os.path.join(__lowerCAmelCase						, '''log.txt'''		)		).exists()		)







"""simple docstring"""






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


lowerCAmelCase__      =    logging.get_logger(__name__)

lowerCAmelCase__      =    {
    '''facebook/xglm-564M''': '''https://huggingface.co/facebook/xglm-564M/resolve/main/config.json''',
    # See all XGLM models at https://huggingface.co/models?filter=xglm
}






class 	__snake_case	(     _lowercase):
  snake_case__    :       List[Any]        =						"xglm"
  snake_case__    :       Dict        =						["past_key_values"]

  snake_case__    :       str        =						{
      "num_attention_heads": "attention_heads",
      "hidden_size": "d_model",
      "num_hidden_layers": "num_layers",
  }




  def __init__(			self					:						List[str]						, __lowerCAmelCase					:						List[Any]=2_5_6_0_0_8						, __lowerCAmelCase					:						int=2_0_4_8						, __lowerCAmelCase					:						Dict=1_0_2_4						, __lowerCAmelCase					:						List[str]=4_0_9_6						, __lowerCAmelCase					:						Tuple=2_4						, __lowerCAmelCase					:						Dict=1_6						, __lowerCAmelCase					:						Tuple="gelu"						, __lowerCAmelCase					:						Tuple=0.1						, __lowerCAmelCase					:						Tuple=0.1						, __lowerCAmelCase					:						Optional[Any]=0.0						, __lowerCAmelCase					:						List[Any]=0.0						, __lowerCAmelCase					:						int=0.02						, __lowerCAmelCase					:						Any=True						, __lowerCAmelCase					:						Tuple=True						, __lowerCAmelCase					:						str=2						, __lowerCAmelCase					:						Dict=1						, __lowerCAmelCase					:						Dict=0						, __lowerCAmelCase					:						List[Any]=2						, **__lowerCAmelCase					:						Optional[Any]						, ):






         """simple docstring"""






         _lowerCamelCase						:    List[Any]         =			vocab_size
         _lowerCamelCase						:    List[Any]         =			max_position_embeddings
         _lowerCamelCase						:    int         =			d_model
         _lowerCamelCase						:    Optional[Any]         =			ffn_dim
         _lowerCamelCase						:    Any         =			num_layers
         _lowerCamelCase						:    Union[str, Any]         =			attention_heads
         _lowerCamelCase						:    List[str]         =			activation_function
         _lowerCamelCase						:    Union[str, Any]         =			dropout
         _lowerCamelCase						:    int         =			attention_dropout
         _lowerCamelCase						:    Optional[int]         =			activation_dropout
         _lowerCamelCase						:    Any         =			layerdrop
         _lowerCamelCase						:    List[str]         =			init_std
         _lowerCamelCase						:    Union[str, Any]         =			scale_embedding  # scale factor will be sqrt(d_model) if True
         _lowerCamelCase						:    str         =			use_cache

         super().__init__(
             pad_token_id=__lowerCAmelCase						, bos_token_id=__lowerCAmelCase						, eos_token_id=__lowerCAmelCase						, decoder_start_token_id=__lowerCAmelCase						, **__lowerCAmelCase						, )


