



'''simple docstring'''



import random
import timeit
from functools import wraps
from typing import Callable, Optional

from ..configuration_utils import PretrainedConfig
from ..models.auto.modeling_tf_auto import TF_MODEL_MAPPING, TF_MODEL_WITH_LM_HEAD_MAPPING
from ..utils import is_pyanvml_available, is_tf_available, logging
from .benchmark_utils import (
    Benchmark,
    Memory,
    MemorySummary,
    measure_peak_memory_cpu,
    start_memory_tracing,
    stop_memory_tracing,
)


if is_tf_available():
							import tensorflow as tf
							from tensorflow.python.framework.errors_impl import ResourceExhaustedError

							from .benchmark_args_tf import TensorFlowBenchmarkArguments

if is_pyanvml_available():
							import pyanvml.pyanvml as nvml

lowerCAmelCase_  :							Tuple             =    logging.get_logger(__name__)
def        UpperCAmelCase (    A	:					bool       ,		A	:					bool					):
							def run_func(A	:					Tuple					):
														@wraps(A					)
														def run_in_eager_mode(*A	:					Union[str, Any]       ,		**A	:					Tuple					):
																					return func(*A       ,		**A					)

														@wraps(A					)
														@tf.function(experimental_compile=A					)
														def run_in_graph_mode(*A	:					Optional[int]       ,		**A	:					Optional[Any]					):
																					return func(*A       ,		**A					)

														if do_eager_mode is True:
																					if use_xla is not False:
																												raise ValueError(
																												    '''Cannot run model in XLA, if `args.eager_mode` is set to `True`. Please set `args.eager_mode=False`.'''					)
																					return run_in_eager_mode
														else:
																					return run_in_graph_mode

							return run_func
def        UpperCAmelCase (    A	:					int       ,		A	:					int       ,		A	:					int					):
							SCREAMING_SNAKE_CASE       :   Tuple         =      random.Random()
							SCREAMING_SNAKE_CASE       :   Tuple         =      [rng.randint(0       ,		vocab_size - 1					) for i in range(batch_size * sequence_length					)]
							return tf.constant(A       ,		shape=(batch_size, sequence_length)       ,		dtype=tf.intaa					)






class 						lowerCamelCase_				(       snake_case_      ):
						_lowerCAmelCase  : TensorFlowBenchmarkArguments
						_lowerCAmelCase  : PretrainedConfig
						_lowerCAmelCase  : str						  =					"TensorFlow"







						@property
						def        __lowercase     (	self :   str					):

													"""simple docstring"""
													return tf.__version__







						def        __lowercase     (	self :   Union[str, Any]					,     lowerCAmelCase__ :   str					,     lowerCAmelCase__ :   int					,     lowerCAmelCase__ :   int					):

													"""simple docstring"""
													# initialize GPU on separate process
													SCREAMING_SNAKE_CASE       :   Tuple         =      self.args.strategy
													if strategy is None:
																				raise ValueError('''A device strategy has to be initialized before using TensorFlow.'''					)
													SCREAMING_SNAKE_CASE       :   int         =      self._prepare_inference_func(lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__					)
													return self._measure_speed(_inference					)







						def        __lowercase     (	self :   Any					,     lowerCAmelCase__ :   str					,     lowerCAmelCase__ :   int					,     lowerCAmelCase__ :   int					):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   Tuple         =      self.args.strategy
													if strategy is None:
																				raise ValueError('''A device strategy has to be initialized before using TensorFlow.'''					)
													SCREAMING_SNAKE_CASE       :   Optional[Any]         =      self._prepare_train_func(lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__					)
													return self._measure_speed(_train					)







						def        __lowercase     (	self :   Union[str, Any]					,     lowerCAmelCase__ :   str					,     lowerCAmelCase__ :   int					,     lowerCAmelCase__ :   int					):

													"""simple docstring"""
													# initialize GPU on separate process
													if self.args.is_gpu:
																				tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx]					,     lowerCAmelCase__					)
													SCREAMING_SNAKE_CASE       :   int         =      self.args.strategy
													if strategy is None:
																				raise ValueError('''A device strategy has to be initialized before using TensorFlow.'''					)
													SCREAMING_SNAKE_CASE       :   Any         =      self._prepare_inference_func(lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__					)
													return self._measure_memory(_inference					)







						def        __lowercase     (	self :   Optional[int]					,     lowerCAmelCase__ :   str					,     lowerCAmelCase__ :   int					,     lowerCAmelCase__ :   int					):

													"""simple docstring"""
													if self.args.is_gpu:
																				tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx]					,     lowerCAmelCase__					)
													SCREAMING_SNAKE_CASE       :   Optional[int]         =      self.args.strategy
													if strategy is None:
																				raise ValueError('''A device strategy has to be initialized before using TensorFlow.'''					)

													SCREAMING_SNAKE_CASE       :   List[Any]         =      self._prepare_train_func(lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__					)
													return self._measure_memory(_train					)







						def        __lowercase     (	self :   Optional[Any]					,     lowerCAmelCase__ :   str					,     lowerCAmelCase__ :   int					,     lowerCAmelCase__ :   int					):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   Dict         =      self.config_dict[model_name]

													if self.args.fpaa:
																				raise NotImplementedError('''Mixed precision is currently not supported.'''					)

													SCREAMING_SNAKE_CASE       :   Optional[Any]         =      (
													    hasattr(lowerCAmelCase__					,     '''architectures'''					)
													    and isinstance(config.architectures					,     lowerCAmelCase__					)
													    and len(config.architectures					) > 0
													)
													if not self.args.only_pretrain_model and has_model_class_in_config:
																				try:
																											SCREAMING_SNAKE_CASE       :   Optional[Any]         =      '''TF''' + config.architectures[0]  # prepend 'TF' for tensorflow model
																											SCREAMING_SNAKE_CASE       :   str         =      __import__('''transformers'''					,     fromlist=[model_class]					)
																											SCREAMING_SNAKE_CASE       :   Optional[Any]         =      getattr(lowerCAmelCase__					,     lowerCAmelCase__					)
																											SCREAMING_SNAKE_CASE       :   List[str]         =      model_cls(lowerCAmelCase__					)
																				except ImportError:
																											raise ImportError(
																											    F"""{model_class} does not exist. If you just want to test the pretrained model, you might want to"""
																											    ''' set `--only_pretrain_model` or `args.only_pretrain_model=True`.'''					)
													else:
																				SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      TF_MODEL_MAPPING[config.__class__](lowerCAmelCase__					)

													# encoder-decoder has vocab size saved differently
													SCREAMING_SNAKE_CASE       :   Any         =      config.vocab_size if hasattr(lowerCAmelCase__					,     '''vocab_size'''					) else config.encoder.vocab_size
													SCREAMING_SNAKE_CASE       :   Optional[Any]         =      random_input_ids(lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__					)

													@run_with_tf_optimizations(self.args.eager_mode					,     self.args.use_xla					)
													def encoder_decoder_forward():
																				return model(lowerCAmelCase__					,     decoder_input_ids=lowerCAmelCase__					,     training=lowerCAmelCase__					)

													@run_with_tf_optimizations(self.args.eager_mode					,     self.args.use_xla					)
													def encoder_forward():
																				return model(lowerCAmelCase__					,     training=lowerCAmelCase__					)

													SCREAMING_SNAKE_CASE       :   Any         =      encoder_decoder_forward if config.is_encoder_decoder else encoder_forward

													return _inference







						def        __lowercase     (	self :   Tuple					,     lowerCAmelCase__ :   str					,     lowerCAmelCase__ :   int					,     lowerCAmelCase__ :   int					):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   str         =      self.config_dict[model_name]

													if self.args.eager_mode is not False:
																				raise ValueError('''Training cannot be done in eager mode. Please make sure that `args.eager_mode = False`.'''					)

													if self.args.fpaa:
																				raise NotImplementedError('''Mixed precision is currently not supported.'''					)

													SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      (
													    hasattr(lowerCAmelCase__					,     '''architectures'''					)
													    and isinstance(config.architectures					,     lowerCAmelCase__					)
													    and len(config.architectures					) > 0
													)
													if not self.args.only_pretrain_model and has_model_class_in_config:
																				try:
																											SCREAMING_SNAKE_CASE       :   List[Any]         =      '''TF''' + config.architectures[0]  # prepend 'TF' for tensorflow model
																											SCREAMING_SNAKE_CASE       :   str         =      __import__('''transformers'''					,     fromlist=[model_class]					)
																											SCREAMING_SNAKE_CASE       :   int         =      getattr(lowerCAmelCase__					,     lowerCAmelCase__					)
																											SCREAMING_SNAKE_CASE       :   Tuple         =      model_cls(lowerCAmelCase__					)
																				except ImportError:
																											raise ImportError(
																											    F"""{model_class} does not exist. If you just want to test the pretrained model, you might want to"""
																											    ''' set `--only_pretrain_model` or `args.only_pretrain_model=True`.'''					)
													else:
																				SCREAMING_SNAKE_CASE       :   List[str]         =      TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](lowerCAmelCase__					)

													# encoder-decoder has vocab size saved differently
													SCREAMING_SNAKE_CASE       :   int         =      config.vocab_size if hasattr(lowerCAmelCase__					,     '''vocab_size'''					) else config.encoder.vocab_size
													SCREAMING_SNAKE_CASE       :   Optional[int]         =      random_input_ids(lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__					)

													@run_with_tf_optimizations(self.args.eager_mode					,     self.args.use_xla					)
													def encoder_decoder_train():
																				SCREAMING_SNAKE_CASE       :   Any         =      model(lowerCAmelCase__					,     decoder_input_ids=lowerCAmelCase__					,     labels=lowerCAmelCase__					,     training=lowerCAmelCase__					)[0]
																				SCREAMING_SNAKE_CASE       :   str         =      tf.gradients(lowerCAmelCase__					,     model.trainable_variables					)
																				return gradients

													@run_with_tf_optimizations(self.args.eager_mode					,     self.args.use_xla					)
													def encoder_train():
																				SCREAMING_SNAKE_CASE       :   List[str]         =      model(lowerCAmelCase__					,     labels=lowerCAmelCase__					,     training=lowerCAmelCase__					)[0]
																				SCREAMING_SNAKE_CASE       :   Any         =      tf.gradients(lowerCAmelCase__					,     model.trainable_variables					)
																				return gradients

													SCREAMING_SNAKE_CASE       :   int         =      encoder_decoder_train if config.is_encoder_decoder else encoder_train

													return _train







						def        __lowercase     (	self :   Optional[int]					,     lowerCAmelCase__ :   Union[str, Any]					):

													"""simple docstring"""
													with self.args.strategy.scope():
																				try:
																											if self.args.is_tpu or self.args.use_xla:
																																		# run additional 10 times to stabilize compilation for tpu
																																		logger.info('''Do inference on TPU. Running model 5 times to stabilize compilation'''					)
																																		timeit.repeat(lowerCAmelCase__					,     repeat=1					,     number=5					)

																											# as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average
																											SCREAMING_SNAKE_CASE       :   List[str]         =      timeit.repeat(
																											    lowerCAmelCase__					,     repeat=self.args.repeat					,     number=10					,     )

																											return min(lowerCAmelCase__					) / 10.0
																				except ResourceExhaustedError as e:
																											self.print_fn(F"""Doesn't fit on GPU. {e}"""					)







						def        __lowercase     (	self :   List[Any]					,     lowerCAmelCase__ :   Callable[[], None]					):

													"""simple docstring"""
													logger.info(
													    '''Note that TensorFlow allocates more memory than '''
													    '''it might need to speed up computation. '''
													    '''The memory reported here corresponds to the memory '''
													    '''reported by `nvidia-smi`, which can vary depending '''
													    '''on total available memory on the GPU that is used.'''					)
													with self.args.strategy.scope():
																				try:
																											if self.args.trace_memory_line_by_line:
																																		if not self.args.eager_mode:
																																									raise ValueError(
																																									    '''`args.eager_mode` is set to `False`. Make sure to run model in eager mode to measure memory'''
																																									    ''' consumption line by line.'''					)
																																		SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      start_memory_tracing('''transformers'''					)

																											if self.args.is_tpu:
																																		# tpu
																																		raise NotImplementedError(
																																		    '''Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking'''
																																		    ''' with `args.memory=False`'''					)
																											elif self.args.is_gpu:
																																		# gpu
																																		if not is_pyanvml_available():
																																									logger.warning(
																																									    '''py3nvml not installed, we won\'t log GPU memory usage. '''
																																									    '''Install py3nvml (pip install py3nvml) to log information about GPU.'''					)
																																									SCREAMING_SNAKE_CASE       :   Optional[Any]         =      '''N/A'''
																																		else:
																																									logger.info(
																																									    '''Measuring total GPU usage on GPU device. Make sure to not have additional processes'''
																																									    ''' running on the same GPU.'''					)
																																									# init nvml
																																									nvml.nvmlInit()
																																									func()
																																									SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx					)
																																									SCREAMING_SNAKE_CASE       :   Optional[Any]         =      nvml.nvmlDeviceGetMemoryInfo(lowerCAmelCase__					)
																																									SCREAMING_SNAKE_CASE       :   Optional[Any]         =      meminfo.used
																																									SCREAMING_SNAKE_CASE       :   Any         =      Memory(lowerCAmelCase__					)
																																									# shutdown nvml
																																									nvml.nvmlShutdown()
																											else:
																																		# cpu
																																		if self.args.trace_memory_line_by_line:
																																									logger.info(
																																									    '''When enabling line by line tracing, the max peak memory for CPU is inaccurate in'''
																																									    ''' TensorFlow.'''					)
																																									SCREAMING_SNAKE_CASE       :   List[Any]         =      None
																																		else:
																																									SCREAMING_SNAKE_CASE       :   List[Any]         =      measure_peak_memory_cpu(lowerCAmelCase__					)
																																									SCREAMING_SNAKE_CASE       :   Optional[int]         =      Memory(lowerCAmelCase__					) if isinstance(lowerCAmelCase__					,     lowerCAmelCase__					) else memory_bytes
																											if self.args.trace_memory_line_by_line:
																																		SCREAMING_SNAKE_CASE       :   Optional[Any]         =      stop_memory_tracing(lowerCAmelCase__					)
																																		if memory is None:
																																									SCREAMING_SNAKE_CASE       :   List[str]         =      summary.total
																											else:
																																		SCREAMING_SNAKE_CASE       :   Optional[int]         =      None

																											return memory, summary
																				except ResourceExhaustedError as e:
																											self.print_fn(F"""Doesn't fit on GPU. {e}"""					)
																											return "N/A", None









'''simple docstring'''



import argparse
from collections import defaultdict

import yaml


lowerCAmelCase_  :							Tuple             =    'docs/source/en/_toctree.yml'
def        UpperCAmelCase (    A	:					Union[str, Any]					):
							SCREAMING_SNAKE_CASE       :   Dict         =      defaultdict(A					)
							for doc in model_doc:
														counts[doc["local"]] += 1
							SCREAMING_SNAKE_CASE       :   Tuple         =      [key for key, value in counts.items() if value > 1]

							SCREAMING_SNAKE_CASE       :   Tuple         =      []
							for duplicate_key in duplicates:
														SCREAMING_SNAKE_CASE       :   Optional[int]         =      list({doc['''title'''] for doc in model_doc if doc['''local'''] == duplicate_key}					)
														if len(A					) > 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 model_doc if counts[doc['''local''']] == 1]					)

							# Sort
							return sorted(A       ,		key=lambda A					: s["title"].lower()					)
def        UpperCAmelCase (    A	:					Any=False					):
							with open(A       ,		encoding='''utf-8'''					) as f:
														SCREAMING_SNAKE_CASE       :   str         =      yaml.safe_load(f.read()					)

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

							# Then to the model doc
							SCREAMING_SNAKE_CASE       :   List[str]         =      0
							while api_doc[model_idx]["title"] != "Models":
														model_idx += 1

							SCREAMING_SNAKE_CASE       :   Dict         =      api_doc[model_idx]['''sections''']

							SCREAMING_SNAKE_CASE       :   List[Any]         =      [(idx, section) for idx, section in enumerate(A					) if '''sections''' in section]
							SCREAMING_SNAKE_CASE       :   int         =      False
							for idx, modality_doc in modalities_docs:
														SCREAMING_SNAKE_CASE       :   List[str]         =      modality_doc['''sections''']
														SCREAMING_SNAKE_CASE       :   List[str]         =      clean_model_doc_toc(A					)

														if old_modality_doc != new_modality_doc:
																					SCREAMING_SNAKE_CASE       :   Dict         =      True
																					if overwrite:
																												SCREAMING_SNAKE_CASE       :   int         =      new_modality_doc

							if diff:
														if overwrite:
																					SCREAMING_SNAKE_CASE       :   List[Any]         =      model_doc
																					SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      api_doc
																					with open(A       ,		'''w'''       ,		encoding='''utf-8'''					) as f:
																												f.write(yaml.dump(A       ,		allow_unicode=A					)					)
														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__":
							lowerCAmelCase_  :							Optional[int]             =    argparse.ArgumentParser()
							parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
							lowerCAmelCase_  :							Any             =    parser.parse_args()

							check_model_doc(args.fix_and_overwrite)




