"""simple docstring"""





import argparse
from collections import defaultdict

import yaml


_UpperCamelCase     :		int											=					"docs/source/en/_toctree.yml"





def        a_  (   _lowerCAmelCase						:				str  ):





       '''simple docstring'''

       lowercase__    :	Optional[Any]     					=			defaultdict(_lowerCAmelCase  )
       lowercase__    :	Tuple     					=			[]
       lowercase__    :	int     					=			[]
       for doc in doc_list:
              if "local" in doc:
                     counts[doc["local"]] += 1

              if doc["title"].lower() == "overview":
                     overview_doc.append({'local': doc['local'], 'title': doc['title']}  )
              else:
                     new_doc_list.append(_lowerCAmelCase  )

       lowercase__    :	Dict     					=			new_doc_list
       lowercase__    :	Tuple     					=			[key for key, value in counts.items() if value > 1]

       lowercase__    :	Tuple     					=			[]
       for duplicate_key in duplicates:
              lowercase__    :	Any     					=			list({doc['title'] for doc in doc_list if doc['local'] == duplicate_key}  )
              if len(_lowerCAmelCase  ) > 1:
                     raise ValueError(
                         f"""{duplicate_key} is present several times in the documentation table of content at """
                         '`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the '
                         'others.'  )
              # Only add this once
              new_doc.append({'local': duplicate_key, 'title': titles[0]}  )

       # Add none duplicate-keys
       new_doc.extend([doc for doc in doc_list if 'local' not in counts or counts[doc['local']] == 1]  )
       lowercase__    :	Optional[int]     					=			sorted(_lowerCAmelCase     ,				key=lambda _lowerCAmelCase  : s["title"].lower()  )

       # "overview" gets special treatment and is always first
       if len(_lowerCAmelCase  ) > 1:
              raise ValueError('{doc_list} has two \'overview\' docs which is not allowed.'  )

       overview_doc.extend(_lowerCAmelCase  )

       # Sort
       return overview_doc





def        a_  (   _lowerCAmelCase						:				Optional[Any]=False  ):





       '''simple docstring'''

       with open(_lowerCAmelCase     ,				encoding='utf-8'  ) as f:
              lowercase__    :	int     					=			yaml.safe_load(f.read()  )

       # Get to the API doc
       lowercase__    :	List[Any]     					=			0
       while content[api_idx]["title"] != "API":
              api_idx += 1
       lowercase__    :	str     					=			content[api_idx]['sections']

       # Then to the model doc
       lowercase__    :	List[str]     					=			0
       while api_doc[scheduler_idx]["title"] != "Schedulers":
              scheduler_idx += 1

       lowercase__    :	Tuple     					=			api_doc[scheduler_idx]['sections']
       lowercase__    :	int     					=			clean_doc_toc(_lowerCAmelCase  )

       lowercase__    :	Optional[Any]     					=			False
       if new_scheduler_doc != scheduler_doc:
              lowercase__    :	str     					=			True
              if overwrite:
                     lowercase__    :	int     					=			new_scheduler_doc

       if diff:
              if overwrite:
                     lowercase__    :	Any     					=			api_doc
                     with open(_lowerCAmelCase     ,				'w'     ,				encoding='utf-8'  ) as f:
                            f.write(yaml.dump(_lowerCAmelCase     ,				allow_unicode=_lowerCAmelCase  )  )
              else:
                     raise ValueError(
                         'The model doc part of the table of content is not properly sorted, run `make style` to fix this.'  )





def        a_  (   _lowerCAmelCase						:				Tuple=False  ):





       '''simple docstring'''

       with open(_lowerCAmelCase     ,				encoding='utf-8'  ) as f:
              lowercase__    :	int     					=			yaml.safe_load(f.read()  )

       # Get to the API doc
       lowercase__    :	Any     					=			0
       while content[api_idx]["title"] != "API":
              api_idx += 1
       lowercase__    :	int     					=			content[api_idx]['sections']

       # Then to the model doc
       lowercase__    :	str     					=			0
       while api_doc[pipeline_idx]["title"] != "Pipelines":
              pipeline_idx += 1

       lowercase__    :	Optional[Any]     					=			False
       lowercase__    :	Optional[Any]     					=			api_doc[pipeline_idx]['sections']
       lowercase__    :	int     					=			[]

       # sort sub pipeline docs
       for pipeline_doc in pipeline_docs:
              if "section" in pipeline_doc:
                     lowercase__    :	Any     					=			pipeline_doc['section']
                     lowercase__    :	int     					=			clean_doc_toc(_lowerCAmelCase  )
                     if overwrite:
                            lowercase__    :	List[Any]     					=			new_sub_pipeline_doc
              new_pipeline_docs.append(_lowerCAmelCase  )

       # sort overall pipeline doc
       lowercase__    :	str     					=			clean_doc_toc(_lowerCAmelCase  )

       if new_pipeline_docs != pipeline_docs:
              lowercase__    :	Union[str, Any]     					=			True
              if overwrite:
                     lowercase__    :	int     					=			new_pipeline_docs

       if diff:
              if overwrite:
                     lowercase__    :	Optional[Any]     					=			api_doc
                     with open(_lowerCAmelCase     ,				'w'     ,				encoding='utf-8'  ) as f:
                            f.write(yaml.dump(_lowerCAmelCase     ,				allow_unicode=_lowerCAmelCase  )  )
              else:
                     raise ValueError(
                         'The model doc part of the table of content is not properly sorted, run `make style` to fix this.'  )


if __name__ == "__main__":
       _UpperCamelCase     :		Dict											=					argparse.ArgumentParser()
       parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")
       _UpperCamelCase     :		Tuple											=					parser.parse_args()

       check_scheduler_doc(args.fix_and_overwrite)
       check_pipeline_doc(args.fix_and_overwrite)


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


      def 	_UpperCAmelCase (     self	,    a							) ->      str:
             for model_result in results.values():
                    for batch_size, sequence_length in zip(model_result['bs']	,    model_result['ss']							):
                           lowercase__    :	str     					=			model_result['result'][batch_size][sequence_length]
                           self.assertIsNotNone(a							)


      def 	_UpperCAmelCase (     self							) ->      int:
             lowercase__    :	Dict     					=			'sshleifer/tiny-gpt2'
             lowercase__    :	int     					=			TensorFlowBenchmarkArguments(
                 models=[MODEL_ID]	,    training=a	,    inference=a	,    sequence_lengths=[8]	,    batch_sizes=[1]	,    eager_mode=a	,    multi_process=a	,    )
             lowercase__    :	str     					=			TensorFlowBenchmark(a							)
             lowercase__    :	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 	_UpperCAmelCase (     self							) ->      int:
             lowercase__    :	List[str]     					=			'sgugger/tiny-distilbert-classification'
             lowercase__    :	int     					=			TensorFlowBenchmarkArguments(
                 models=[MODEL_ID]	,    training=a	,    inference=a	,    sequence_lengths=[8]	,    batch_sizes=[1]	,    multi_process=a	,    only_pretrain_model=a	,    )
             lowercase__    :	Optional[Any]     					=			TensorFlowBenchmark(a							)
             lowercase__    :	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 	_UpperCAmelCase (     self							) ->      Union[str, Any]:
             lowercase__    :	Optional[int]     					=			'sshleifer/tiny-gpt2'
             lowercase__    :	Union[str, Any]     					=			TensorFlowBenchmarkArguments(
                 models=[MODEL_ID]	,    training=a	,    inference=a	,    sequence_lengths=[8]	,    batch_sizes=[1]	,    multi_process=a	,    )
             lowercase__    :	Optional[Any]     					=			TensorFlowBenchmark(a							)
             lowercase__    :	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 	_UpperCAmelCase (     self							) ->      Optional[Any]:
             lowercase__    :	Any     					=			'sshleifer/tiny-gpt2'
             lowercase__    :	List[Any]     					=			AutoConfig.from_pretrained(a							)
             lowercase__    :	Any     					=			TensorFlowBenchmarkArguments(
                 models=[MODEL_ID]	,    training=a	,    inference=a	,    sequence_lengths=[8]	,    batch_sizes=[1]	,    eager_mode=a	,    multi_process=a	,    )
             lowercase__    :	Tuple     					=			TensorFlowBenchmark(a	,    [config]							)
             lowercase__    :	Dict     					=			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							) ->      int:
             lowercase__    :	Optional[Any]     					=			'sshleifer/tiny-gpt2'
             lowercase__    :	List[str]     					=			AutoConfig.from_pretrained(a							)
             lowercase__    :	Any     					=			TensorFlowBenchmarkArguments(
                 models=[MODEL_ID]	,    training=a	,    inference=a	,    sequence_lengths=[8]	,    batch_sizes=[1]	,    multi_process=a	,    )
             lowercase__    :	List[str]     					=			TensorFlowBenchmark(a	,    [config]							)
             lowercase__    :	Any     					=			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							) ->      Tuple:
             lowercase__    :	Optional[Any]     					=			'sshleifer/tiny-gpt2'
             lowercase__    :	Any     					=			TensorFlowBenchmarkArguments(
                 models=[MODEL_ID]	,    training=a	,    inference=a	,    sequence_lengths=[8]	,    batch_sizes=[1]	,    multi_process=a	,    )
             lowercase__    :	Optional[Any]     					=			TensorFlowBenchmark(a							)
             lowercase__    :	Tuple     					=			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							) ->      str:
             lowercase__    :	Optional[Any]     					=			'sshleifer/tiny-gpt2'
             lowercase__    :	Optional[int]     					=			AutoConfig.from_pretrained(a							)
             lowercase__    :	str     					=			TensorFlowBenchmarkArguments(
                 models=[MODEL_ID]	,    training=a	,    inference=a	,    sequence_lengths=[8]	,    batch_sizes=[1]	,    multi_process=a	,    )
             lowercase__    :	str     					=			TensorFlowBenchmark(a	,    [config]							)
             lowercase__    :	Optional[int]     					=			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							) ->      Optional[Any]:
             lowercase__    :	List[str]     					=			'patrickvonplaten/t5-tiny-random'
             lowercase__    :	Any     					=			AutoConfig.from_pretrained(a							)
             lowercase__    :	List[str]     					=			TensorFlowBenchmarkArguments(
                 models=[MODEL_ID]	,    training=a	,    inference=a	,    sequence_lengths=[8]	,    batch_sizes=[1]	,    multi_process=a	,    )
             lowercase__    :	int     					=			TensorFlowBenchmark(a	,    configs=[config]							)
             lowercase__    :	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 	_UpperCAmelCase (     self							) ->      Any:
             lowercase__    :	Any     					=			'sshleifer/tiny-gpt2'
             lowercase__    :	Optional[Any]     					=			TensorFlowBenchmarkArguments(
                 models=[MODEL_ID]	,    training=a	,    inference=a	,    sequence_lengths=[8]	,    batch_sizes=[1]	,    use_xla=a	,    multi_process=a	,    )
             lowercase__    :	Any     					=			TensorFlowBenchmark(a							)
             lowercase__    :	Dict     					=			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							) ->      List[str]:
             lowercase__    :	Any     					=			'sshleifer/tiny-gpt2'
             with tempfile.TemporaryDirectory() as tmp_dir:
                    lowercase__    :	List[Any]     					=			TensorFlowBenchmarkArguments(
                        models=[MODEL_ID]	,    inference=a	,    save_to_csv=a	,    sequence_lengths=[8]	,    batch_sizes=[1]	,    inference_time_csv_file=os.path.join(a	,    'inf_time.csv'							)	,    inference_memory_csv_file=os.path.join(a	,    'inf_mem.csv'							)	,    env_info_csv_file=os.path.join(a	,    'env.csv'							)	,    multi_process=a	,    )
                    lowercase__    :	Union[str, Any]     					=			TensorFlowBenchmark(a							)
                    benchmark.run()
                    self.assertTrue(Path(os.path.join(a	,    'inf_time.csv'							)							).exists()							)
                    self.assertTrue(Path(os.path.join(a	,    'inf_mem.csv'							)							).exists()							)
                    self.assertTrue(Path(os.path.join(a	,    'env.csv'							)							).exists()							)


      def 	_UpperCAmelCase (     self							) ->      Dict:
             lowercase__    :	Tuple     					=			'sshleifer/tiny-gpt2'

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

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

