
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_						     =					logging.get_logger(__name__)



def 						__magic_name__			(   __a      :	str	,		__a      :	Union[str, Any]						):



     '''simple docstring'''




     def run_func(__a      :	Any						):
          @wraps(__a						)
          def run_in_eager_mode(*__a      :	Union[str, Any]	,		**__a      :	List[Any]						):
               return func(*__a	,		**__a						)

          @wraps(__a						)
          @tf.function(experimental_compile=__a						)
          def run_in_graph_mode(*__a      :	Union[str, Any]	,		**__a      :	Dict						):
               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 						__magic_name__			(   __a      :	Dict	,		__a      :	Tuple	,		__a      :	Dict						):



     '''simple docstring'''




     UpperCamelCase__				=    random.Random()
     UpperCamelCase__				=    [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      __A(    __lowerCamelCase			):

  """simple docstring"""
  SCREAMING_SNAKE_CASE__         =      42
  SCREAMING_SNAKE_CASE__         =      42
  SCREAMING_SNAKE_CASE__         =      """TensorFlow"""







  @property
  def 	UpperCAmelCase_ (self						):
       return tf.__version__







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    self.args.strategy
       if strategy is None:
            raise ValueError("""A device strategy has to be initialized before using TensorFlow."""						)
       UpperCamelCase__				=    self._prepare_inference_func(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE						)
       return self._measure_speed(_inference						)







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    self.args.strategy
       if strategy is None:
            raise ValueError("""A device strategy has to be initialized before using TensorFlow."""						)
       UpperCamelCase__				=    self._prepare_train_func(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE						)
       return self._measure_speed(_train						)







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):





       if self.args.is_gpu:
            tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx]	,      _SCREAMING_SNAKE_CASE						)
       UpperCamelCase__				=    self.args.strategy
       if strategy is None:
            raise ValueError("""A device strategy has to be initialized before using TensorFlow."""						)
       UpperCamelCase__				=    self._prepare_inference_func(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE						)
       return self._measure_memory(_inference						)







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):





       if self.args.is_gpu:
            tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx]	,      _SCREAMING_SNAKE_CASE						)
       UpperCamelCase__				=    self.args.strategy
       if strategy is None:
            raise ValueError("""A device strategy has to be initialized before using TensorFlow."""						)

       UpperCamelCase__				=    self._prepare_train_func(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE						)
       return self._measure_memory(_train						)







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    self.config_dict[model_name]

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

       UpperCamelCase__				=    (
           hasattr(_SCREAMING_SNAKE_CASE	,      """architectures"""						)
           and isinstance(config.architectures	,      _SCREAMING_SNAKE_CASE						)
           and len(config.architectures						) > 0
       )
       if not self.args.only_pretrain_model and has_model_class_in_config:
            try:
                 UpperCamelCase__				=    '''TF''' + config.architectures[0]  # prepend 'TF' for tensorflow model
                 UpperCamelCase__				=    __import__("""transformers"""	,      fromlist=[model_class]						)
                 UpperCamelCase__				=    getattr(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE						)
                 UpperCamelCase__				=    model_cls(_SCREAMING_SNAKE_CASE						)
            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:
            UpperCamelCase__				=    TF_MODEL_MAPPING[config.__class__](_SCREAMING_SNAKE_CASE						)

       # encoder-decoder has vocab size saved differently
       UpperCamelCase__				=    config.vocab_size if hasattr(_SCREAMING_SNAKE_CASE	,      """vocab_size"""						) else config.encoder.vocab_size
       UpperCamelCase__				=    random_input_ids(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE						)

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

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

       UpperCamelCase__				=    encoder_decoder_forward if config.is_encoder_decoder else encoder_forward

       return _inference







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    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."""						)

       UpperCamelCase__				=    (
           hasattr(_SCREAMING_SNAKE_CASE	,      """architectures"""						)
           and isinstance(config.architectures	,      _SCREAMING_SNAKE_CASE						)
           and len(config.architectures						) > 0
       )
       if not self.args.only_pretrain_model and has_model_class_in_config:
            try:
                 UpperCamelCase__				=    '''TF''' + config.architectures[0]  # prepend 'TF' for tensorflow model
                 UpperCamelCase__				=    __import__("""transformers"""	,      fromlist=[model_class]						)
                 UpperCamelCase__				=    getattr(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE						)
                 UpperCamelCase__				=    model_cls(_SCREAMING_SNAKE_CASE						)
            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:
            UpperCamelCase__				=    TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](_SCREAMING_SNAKE_CASE						)

       # encoder-decoder has vocab size saved differently
       UpperCamelCase__				=    config.vocab_size if hasattr(_SCREAMING_SNAKE_CASE	,      """vocab_size"""						) else config.encoder.vocab_size
       UpperCamelCase__				=    random_input_ids(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE						)

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

       @run_with_tf_optimizations(self.args.eager_mode	,      self.args.use_xla						)
       def encoder_train():
            UpperCamelCase__				=    model(_SCREAMING_SNAKE_CASE	,      labels=_SCREAMING_SNAKE_CASE	,      training=_SCREAMING_SNAKE_CASE						)[0]
            UpperCamelCase__				=    tf.gradients(_SCREAMING_SNAKE_CASE	,      model.trainable_variables						)
            return gradients

       UpperCamelCase__				=    encoder_decoder_train if config.is_encoder_decoder else encoder_train

       return _train







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_						):





       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(_SCREAMING_SNAKE_CASE	,      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
                 UpperCamelCase__				=    timeit.repeat(
                     _SCREAMING_SNAKE_CASE	,      repeat=self.args.repeat	,      number=10	,      )

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







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_						):
       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."""						)
                      UpperCamelCase__				=    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."""						)
                           UpperCamelCase__				=    '''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()
                           UpperCamelCase__				=    nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx						)
                           UpperCamelCase__				=    nvml.nvmlDeviceGetMemoryInfo(_SCREAMING_SNAKE_CASE						)
                           UpperCamelCase__				=    meminfo.used
                           UpperCamelCase__				=    Memory(_SCREAMING_SNAKE_CASE						)
                           # 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."""						)
                           UpperCamelCase__				=    None
                      else:
                           UpperCamelCase__				=    measure_peak_memory_cpu(_SCREAMING_SNAKE_CASE						)
                           UpperCamelCase__				=    Memory(_SCREAMING_SNAKE_CASE						) if isinstance(_SCREAMING_SNAKE_CASE	,      _SCREAMING_SNAKE_CASE						) else memory_bytes
                 if self.args.trace_memory_line_by_line:
                      UpperCamelCase__				=    stop_memory_tracing(_SCREAMING_SNAKE_CASE						)
                      if memory is None:
                           UpperCamelCase__				=    summary.total
                 else:
                      UpperCamelCase__				=    None

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







import importlib
import inspect
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_config_docstrings.py
lowerCamelCase_						     =					'''src/transformers'''


# This is to make sure the transformers module imported is the one in the repo.
lowerCamelCase_						     =					importlib.util.spec_from_file_location(
    '''transformers''',
    os.path.join(PATH_TO_TRANSFORMERS, '''__init__.py'''),
    submodule_search_locations=[PATH_TO_TRANSFORMERS],
)
lowerCamelCase_						     =					spec.loader.load_module()

lowerCamelCase_						     =					transformers.models.auto.configuration_auto.CONFIG_MAPPING

# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.
# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`
lowerCamelCase_						     =					re.compile('''\[(.+?)\]\((https://huggingface\.co/.+?)\)''')


lowerCamelCase_						     =					{
    '''CLIPConfigMixin''',
    '''DecisionTransformerConfigMixin''',
    '''EncoderDecoderConfigMixin''',
    '''RagConfigMixin''',
    '''SpeechEncoderDecoderConfigMixin''',
    '''VisionEncoderDecoderConfigMixin''',
    '''VisionTextDualEncoderConfigMixin''',
}






def 						__magic_name__			(   ):



     '''simple docstring'''




     UpperCamelCase__				=    []

     for config_class in list(CONFIG_MAPPING.values()						):
          UpperCamelCase__				=    False

          # source code of `config_class`
          UpperCamelCase__				=    inspect.getsource(__a						)
          UpperCamelCase__				=    _re_checkpoint.findall(__a						)

          for checkpoint in checkpoints:
               # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
               # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
               UpperCamelCase__		,	UpperCamelCase__				=    checkpoint

               # verify the checkpoint name corresponds to the checkpoint link
               UpperCamelCase__				=    f"https://huggingface.co/{ckpt_name}"
               if ckpt_link == ckpt_link_from_name:
                    UpperCamelCase__				=    True
                    break

          UpperCamelCase__				=    config_class.__name__
          if not checkpoint_found and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
               configs_without_checkpoint.append(__a						)

     if len(__a						) > 0:
          UpperCamelCase__				=    """\n""".join(sorted(__a						)						)
          raise ValueError(f"The following configurations don't contain any valid checkpoint:\n{message}"						)


if __name__ == "__main__":
    check_config_docstrings_have_checkpoints()





