





"""simple docstring"""





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 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''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 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Any											=				'sshleifer/tiny-gpt2'
         lowerCAmelCase__     :Optional[Any]											=				TensorFlowBenchmarkArguments(
             models=[MODEL_ID]       ,					training=__UpperCAmelCase       ,					inference=__UpperCAmelCase       ,					sequence_lengths=[8]       ,					batch_sizes=[1]       ,					eager_mode=__UpperCAmelCase       ,					multi_process=__UpperCAmelCase       ,					)
         lowerCAmelCase__     :Optional[int]											=				TensorFlowBenchmark(__UpperCAmelCase )
         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 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :List[str]											=				'sgugger/tiny-distilbert-classification'
         lowerCAmelCase__     :Union[str, Any]											=				TensorFlowBenchmarkArguments(
             models=[MODEL_ID]       ,					training=__UpperCAmelCase       ,					inference=__UpperCAmelCase       ,					sequence_lengths=[8]       ,					batch_sizes=[1]       ,					multi_process=__UpperCAmelCase       ,					only_pretrain_model=__UpperCAmelCase       ,					)
         lowerCAmelCase__     :Any											=				TensorFlowBenchmark(__UpperCAmelCase )
         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 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :List[Any]											=				'sshleifer/tiny-gpt2'
         lowerCAmelCase__     :Optional[Any]											=				TensorFlowBenchmarkArguments(
             models=[MODEL_ID]       ,					training=__UpperCAmelCase       ,					inference=__UpperCAmelCase       ,					sequence_lengths=[8]       ,					batch_sizes=[1]       ,					multi_process=__UpperCAmelCase       ,					)
         lowerCAmelCase__     :List[str]											=				TensorFlowBenchmark(__UpperCAmelCase )
         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 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Any											=				'sshleifer/tiny-gpt2'
         lowerCAmelCase__     :Tuple											=				AutoConfig.from_pretrained(__UpperCAmelCase )
         lowerCAmelCase__     :str											=				TensorFlowBenchmarkArguments(
             models=[MODEL_ID]       ,					training=__UpperCAmelCase       ,					inference=__UpperCAmelCase       ,					sequence_lengths=[8]       ,					batch_sizes=[1]       ,					eager_mode=__UpperCAmelCase       ,					multi_process=__UpperCAmelCase       ,					)
         lowerCAmelCase__     :int											=				TensorFlowBenchmark(__UpperCAmelCase       ,					[config] )
         lowerCAmelCase__     :List[str]											=				benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result )
         self.check_results_dict_not_empty(results.memory_inference_result )






       def 	snake_case		(       self ):



         '''simple docstring'''

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



         '''simple docstring'''

         lowerCAmelCase__     :Any											=				'sshleifer/tiny-gpt2'
         lowerCAmelCase__     :List[Any]											=				TensorFlowBenchmarkArguments(
             models=[MODEL_ID]       ,					training=__UpperCAmelCase       ,					inference=__UpperCAmelCase       ,					sequence_lengths=[8]       ,					batch_sizes=[1]       ,					multi_process=__UpperCAmelCase       ,					)
         lowerCAmelCase__     :List[str]											=				TensorFlowBenchmark(__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 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Union[str, Any]											=				'sshleifer/tiny-gpt2'
         lowerCAmelCase__     :Any											=				AutoConfig.from_pretrained(__UpperCAmelCase )
         lowerCAmelCase__     :Tuple											=				TensorFlowBenchmarkArguments(
             models=[MODEL_ID]       ,					training=__UpperCAmelCase       ,					inference=__UpperCAmelCase       ,					sequence_lengths=[8]       ,					batch_sizes=[1]       ,					multi_process=__UpperCAmelCase       ,					)
         lowerCAmelCase__     :Any											=				TensorFlowBenchmark(__UpperCAmelCase       ,					[config] )
         lowerCAmelCase__     :str											=				benchmark.run()
         self.check_results_dict_not_empty(results.time_train_result )
         self.check_results_dict_not_empty(results.memory_train_result )






       def 	snake_case		(       self ):



         '''simple docstring'''

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



         '''simple docstring'''

         lowerCAmelCase__     :List[str]											=				'sshleifer/tiny-gpt2'
         lowerCAmelCase__     :Optional[Any]											=				TensorFlowBenchmarkArguments(
             models=[MODEL_ID]       ,					training=__UpperCAmelCase       ,					inference=__UpperCAmelCase       ,					sequence_lengths=[8]       ,					batch_sizes=[1]       ,					use_xla=__UpperCAmelCase       ,					multi_process=__UpperCAmelCase       ,					)
         lowerCAmelCase__     :Any											=				TensorFlowBenchmark(__UpperCAmelCase )
         lowerCAmelCase__     :str											=				benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result )
         self.check_results_dict_not_empty(results.memory_inference_result )






       def 	snake_case		(       self ):



         '''simple docstring'''

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






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :int											=				'sshleifer/tiny-gpt2'

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







"""simple docstring"""





def 					__A  (_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE     )   ->list[str]:





  """simple docstring"""


  return [sentence[i : i + ngram_size] for i in range(len(_SCREAMING_SNAKE_CASE     ) - ngram_size + 1     )]


if __name__ == "__main__":
  from doctest import testmod

  testmod()
