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





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE      )					->							int:






         """simple docstring"""






         for model_result in results.values():
             for batch_size, sequence_length in zip(model_result['''bs''']		,     model_result['''ss''']      ):
                 A			:  List[str]   =		model_result['''result'''][batch_size][sequence_length]
                 self.assertIsNotNone(SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							int:






         """simple docstring"""






         A			:  Any   =		'''sshleifer/tiny-gpt2'''
         A			:  Tuple   =		TensorFlowBenchmarkArguments(
             models=[MODEL_ID]		,     training=SCREAMING_SNAKE_CASE		,     inference=SCREAMING_SNAKE_CASE		,     sequence_lengths=[8]		,     batch_sizes=[1]		,     eager_mode=SCREAMING_SNAKE_CASE		,     multi_process=SCREAMING_SNAKE_CASE		,     )
         A			:  Any   =		TensorFlowBenchmark(SCREAMING_SNAKE_CASE      )
         A			:  Tuple   =		benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result      )
         self.check_results_dict_not_empty(results.memory_inference_result      )





     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """simple docstring"""






         A			:  int   =		'''sgugger/tiny-distilbert-classification'''
         A			:  Optional[Any]   =		TensorFlowBenchmarkArguments(
             models=[MODEL_ID]		,     training=SCREAMING_SNAKE_CASE		,     inference=SCREAMING_SNAKE_CASE		,     sequence_lengths=[8]		,     batch_sizes=[1]		,     multi_process=SCREAMING_SNAKE_CASE		,     only_pretrain_model=SCREAMING_SNAKE_CASE		,     )
         A			:  Union[str, Any]   =		TensorFlowBenchmark(SCREAMING_SNAKE_CASE      )
         A			:  int   =		benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result      )
         self.check_results_dict_not_empty(results.memory_inference_result      )





     def 							__lowerCAmelCase				(				self      )					->							int:






         """simple docstring"""






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






         """simple docstring"""






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





     def 							__lowerCAmelCase				(				self      )					->							Optional[Any]:






         """simple docstring"""






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





     def 							__lowerCAmelCase				(				self      )					->							Dict:






         """simple docstring"""






         A			:  List[Any]   =		'''sshleifer/tiny-gpt2'''
         A			:  str   =		TensorFlowBenchmarkArguments(
             models=[MODEL_ID]		,     training=SCREAMING_SNAKE_CASE		,     inference=SCREAMING_SNAKE_CASE		,     sequence_lengths=[8]		,     batch_sizes=[1]		,     multi_process=SCREAMING_SNAKE_CASE		,     )
         A			:  List[Any]   =		TensorFlowBenchmark(SCREAMING_SNAKE_CASE      )
         A			:  str   =		benchmark.run()
         self.check_results_dict_not_empty(results.time_train_result      )
         self.check_results_dict_not_empty(results.memory_train_result      )





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






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





     def 							__lowerCAmelCase				(				self      )					->							str:






         """simple docstring"""






         A			:  Optional[Any]   =		'''patrickvonplaten/t5-tiny-random'''
         A			:  List[str]   =		AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE      )
         A			:  Any   =		TensorFlowBenchmarkArguments(
             models=[MODEL_ID]		,     training=SCREAMING_SNAKE_CASE		,     inference=SCREAMING_SNAKE_CASE		,     sequence_lengths=[8]		,     batch_sizes=[1]		,     multi_process=SCREAMING_SNAKE_CASE		,     )
         A			:  Dict   =		TensorFlowBenchmark(SCREAMING_SNAKE_CASE		,     configs=[config]      )
         A			:  int   =		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 							__lowerCAmelCase				(				self      )					->							Optional[Any]:






         """simple docstring"""






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






         """simple docstring"""






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





     def 							__lowerCAmelCase				(				self      )					->							str:






         """simple docstring"""






         A			:  List[Any]   =		'''sshleifer/tiny-gpt2'''

         def _check_summary_is_not_empty(SCREAMING_SNAKE_CASE      ):
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE		,     '''sequential'''      )      )
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE		,     '''cumulative'''      )      )
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE		,     '''current'''      )      )
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE		,     '''total'''      )      )

         with tempfile.TemporaryDirectory() as tmp_dir:
             A			:  Any   =		TensorFlowBenchmarkArguments(
                 models=[MODEL_ID]		,     inference=SCREAMING_SNAKE_CASE		,     sequence_lengths=[8]		,     batch_sizes=[1]		,     log_filename=os.path.join(SCREAMING_SNAKE_CASE		,     '''log.txt'''      )		,     log_print=SCREAMING_SNAKE_CASE		,     trace_memory_line_by_line=SCREAMING_SNAKE_CASE		,     eager_mode=SCREAMING_SNAKE_CASE		,     multi_process=SCREAMING_SNAKE_CASE		,     )
             A			:  Any   =		TensorFlowBenchmark(SCREAMING_SNAKE_CASE      )
             A			:  Union[str, Any]   =		benchmark.run()
             _check_summary_is_not_empty(result.inference_summary      )
             self.assertTrue(Path(os.path.join(SCREAMING_SNAKE_CASE		,     '''log.txt'''      )      ).exists()      )






'''simple docstring'''




# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re

from ..utils import cached_file


# docstyle-ignore
lowercase     :							Tuple       						=     '\nHuman: <<task>>\n\nAssistant: '


lowercase     :							Union[str, Any]       						=     'huggingface-tools/default-prompts'
lowercase     :							int       						=     {'chat': 'chat_prompt_template.txt', 'run': 'run_prompt_template.txt'}







def 						lowerCAmelCase_							(  snake_case__						,							snake_case__						,							snake_case__="run"		):




    '''simple docstring'''



    if prompt_or_repo_id is None:
        A			:  List[Any]   =		DEFAULT_PROMPTS_REPO

    # prompt is considered a repo ID when it does not contain any kind of space
    if re.search('''\\s'''						,							snake_case__		) is not None:
        return prompt_or_repo_id

    A			:  int   =		cached_file(
        snake_case__						,							PROMPT_FILES[mode]						,							repo_type='''dataset'''						,							user_agent={'''agent''': agent_name}		)
    with open(snake_case__						,							'''r'''						,							encoding='''utf-8'''		) as f:
        return f.read()




