


"""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

__lowercase    =				logging.get_logger(__name__)


def 					lowercase      (    A_  ,       A_	)->				Tuple:
  '''simple docstring'''







  def run_func(A_	):
    @wraps(lowerCamelCase_	)
    def run_in_eager_mode(*A_  ,       **A_	):
      return func(*lowerCamelCase_  ,       **lowerCamelCase_	)

    @wraps(lowerCamelCase_	)
    @tf.function(experimental_compile=lowerCamelCase_	)
    def run_in_graph_mode(*A_  ,       **A_	):
      return func(*lowerCamelCase_  ,       **lowerCamelCase_	)

    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 					lowercase      (    A_  ,       A_  ,       A_	)->				Union[str, Any]:
  '''simple docstring'''







  a						:		int											=      random.Random()
  a						:		int											=      [rng.randint(0  ,       vocab_size - 1	) for i in range(batch_size * sequence_length	)]
  return tf.constant(lowerCamelCase_  ,       shape=(batch_size, sequence_length)  ,       dtype=tf.intaa	)







class 						_A  (  lowerCamelCase__  ):







    """simple docstring"""




    UpperCAmelCase	:							Optional[Any]     =	4_2
    UpperCAmelCase	:							List[str]     =	4_2
    UpperCAmelCase	:							Tuple     =	"""TensorFlow"""






    @property
    def      __snake_case							( self    :   int):
      return tf.__version__






    def      __snake_case							( self    :   Union[str, Any]    ,			__UpperCAmelCase    :   Dict    ,			__UpperCAmelCase    :   Optional[int]    ,			__UpperCAmelCase    :   str):
      a						:		int											=      self.args.strategy
      if strategy is None:
        raise ValueError("A device strategy has to be initialized before using TensorFlow.")
      a						:		List[Any]											=      self._prepare_inference_func(__lowerCamelCase    ,			__lowerCamelCase    ,			__lowerCamelCase)
      return self._measure_speed(_inference)






    def      __snake_case							( self    :   Optional[int]    ,			__UpperCAmelCase    :   Optional[int]    ,			__UpperCAmelCase    :   str    ,			__UpperCAmelCase    :   List[str]):
      a						:		List[Any]											=      self.args.strategy
      if strategy is None:
        raise ValueError("A device strategy has to be initialized before using TensorFlow.")
      a						:		Union[str, Any]											=      self._prepare_train_func(__lowerCamelCase    ,			__lowerCamelCase    ,			__lowerCamelCase)
      return self._measure_speed(_train)






    def      __snake_case							( self    :   List[Any]    ,			__UpperCAmelCase    :   Dict    ,			__UpperCAmelCase    :   Optional[int]    ,			__UpperCAmelCase    :   str):






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






    def      __snake_case							( self    :   Any    ,			__UpperCAmelCase    :   List[Any]    ,			__UpperCAmelCase    :   int    ,			__UpperCAmelCase    :   List[Any]):






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

      a						:		Union[str, Any]											=      self._prepare_train_func(__lowerCamelCase    ,			__lowerCamelCase    ,			__lowerCamelCase)
      return self._measure_memory(_train)






    def      __snake_case							( self    :   List[Any]    ,			__UpperCAmelCase    :   Dict    ,			__UpperCAmelCase    :   Optional[int]    ,			__UpperCAmelCase    :   int):
      a						:		List[str]											=      self.config_dict[model_name]

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

      a						:		int											=      (
          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:
          a						:		List[Any]											=      '''TF''' + config.architectures[0]  # prepend 'TF' for tensorflow model
          a						:		Optional[int]											=      __import__("transformers"    ,			fromlist=[model_class])
          a						:		List[Any]											=      getattr(__lowerCamelCase    ,			__lowerCamelCase)
          a						:		Dict											=      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:
        a						:		List[Any]											=      TF_MODEL_MAPPING[config.__class__](__lowerCamelCase)

      # encoder-decoder has vocab size saved differently
      a						:		List[Any]											=      config.vocab_size if hasattr(__lowerCamelCase    ,			"vocab_size") else config.encoder.vocab_size
      a						:		str											=      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)

      a						:		Union[str, Any]											=      encoder_decoder_forward if config.is_encoder_decoder else encoder_forward

      return _inference






    def      __snake_case							( self    :   Dict    ,			__UpperCAmelCase    :   Tuple    ,			__UpperCAmelCase    :   Optional[int]    ,			__UpperCAmelCase    :   Union[str, Any]):
      a						:		List[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.")

      a						:		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:
          a						:		Dict											=      '''TF''' + config.architectures[0]  # prepend 'TF' for tensorflow model
          a						:		List[str]											=      __import__("transformers"    ,			fromlist=[model_class])
          a						:		Union[str, Any]											=      getattr(__lowerCamelCase    ,			__lowerCamelCase)
          a						:		Union[str, Any]											=      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:
        a						:		Dict											=      TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](__lowerCamelCase)

      # encoder-decoder has vocab size saved differently
      a						:		Tuple											=      config.vocab_size if hasattr(__lowerCamelCase    ,			"vocab_size") else config.encoder.vocab_size
      a						:		Optional[int]											=      random_input_ids(__lowerCamelCase    ,			__lowerCamelCase    ,			__lowerCamelCase)

      @run_with_tf_optimizations(self.args.eager_mode    ,			self.args.use_xla)
      def encoder_decoder_train():
        a						:		Optional[Any]											=      model(__lowerCamelCase    ,			decoder_input_ids=__lowerCamelCase    ,			labels=__lowerCamelCase    ,			training=__lowerCamelCase)[0]
        a						:		int											=      tf.gradients(__lowerCamelCase    ,			model.trainable_variables)
        return gradients

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

      a						:		Optional[int]											=      encoder_decoder_train if config.is_encoder_decoder else encoder_train

      return _train






    def      __snake_case							( self    :   Tuple    ,			__UpperCAmelCase    :   Union[str, Any]):






      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
          a						:		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      __snake_case							( self    :   str    ,			__UpperCAmelCase    :   Optional[int]):
      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.")
            a						:		Tuple											=      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.")
              a						:		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()
              a						:		List[str]											=      nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx)
              a						:		str											=      nvml.nvmlDeviceGetMemoryInfo(__lowerCamelCase)
              a						:		Union[str, Any]											=      meminfo.used
              a						:		int											=      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.")
              a						:		Dict											=      None
            else:
              a						:		int											=      measure_peak_memory_cpu(__lowerCamelCase)
              a						:		int											=      Memory(__lowerCamelCase) if isinstance(__lowerCamelCase    ,			__lowerCamelCase) else memory_bytes
          if self.args.trace_memory_line_by_line:
            a						:		Optional[int]											=      stop_memory_tracing(__lowerCamelCase)
            if memory is None:
              a						:		Tuple											=      summary.total
          else:
            a						:		Tuple											=      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 warnings

from transformers import AutoTokenizer
from transformers.utils import is_torch_available
from transformers.utils.generic import ExplicitEnum

from ...processing_utils import ProcessorMixin


if is_torch_available():
 import torch






class 						_A  (  _a  ):







    """simple docstring"""




    UpperCAmelCase	:							str     =	"""char"""
    UpperCAmelCase	:							Optional[Any]     =	"""bpe"""
    UpperCAmelCase	:							Optional[Any]     =	"""wp"""






__lowercase    =				(DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)






class 						_A  (  _a  ):







    """simple docstring"""




    UpperCAmelCase	:							Optional[Any]     =	["""image_processor""", """char_tokenizer"""]
    UpperCAmelCase	:							Optional[Any]     =	"""ViTImageProcessor"""
    UpperCAmelCase	:							List[Any]     =	"""MgpstrTokenizer"""






    def __init__( self    :   List[Any]    ,			__UpperCAmelCase    :   Union[str, Any]=None    ,			__UpperCAmelCase    :   Dict=None    ,			**__UpperCAmelCase    :   str):
      a						:		Tuple											=      None
      if "feature_extractor" in kwargs:
        warnings.warn(
            "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
            " instead."    ,			__UpperCAmelCase    ,			)
        a						:		List[str]											=      kwargs.pop("feature_extractor")

      a						:		Dict											=      image_processor if image_processor is not None else feature_extractor
      if image_processor is None:
        raise ValueError("You need to specify an `image_processor`.")
      if tokenizer is None:
        raise ValueError("You need to specify a `tokenizer`.")

      a						:		Union[str, Any]											=      tokenizer
      a						:		int											=      AutoTokenizer.from_pretrained("gpt2")
      a						:		str											=      AutoTokenizer.from_pretrained("bert-base-uncased")

      super().__init__(__UpperCAmelCase    ,			__UpperCAmelCase)






    def __call__( self    :   str    ,			__UpperCAmelCase    :   Dict=None    ,			__UpperCAmelCase    :   Tuple=None    ,			__UpperCAmelCase    :   Union[str, Any]=None    ,			**__UpperCAmelCase    :   int):
      if images is None and text is None:
        raise ValueError("You need to specify either an `images` or `text` input to process.")

      if images is not None:
        a						:		List[str]											=      self.image_processor(__UpperCAmelCase    ,			return_tensors=__UpperCAmelCase    ,			**__UpperCAmelCase)
      if text is not None:
        a						:		Optional[Any]											=      self.char_tokenizer(__UpperCAmelCase    ,			return_tensors=__UpperCAmelCase    ,			**__UpperCAmelCase)

      if text is None:
        return inputs
      elif images is None:
        return encodings
      else:
        a						:		Any											=      encodings["input_ids"]
        return inputs






    def      __snake_case							( self    :   List[Any]    ,			__UpperCAmelCase    :   List[str]):
      a							,		a							,		a						:		Tuple											=      sequences
      a						:		Optional[int]											=      char_preds.size(0)

      a							,		a						:		Dict											=      self._decode_helper(__UpperCAmelCase    ,			"char")
      a							,		a						:		Dict											=      self._decode_helper(__UpperCAmelCase    ,			"bpe")
      a							,		a						:		Union[str, Any]											=      self._decode_helper(__UpperCAmelCase    ,			"wp")

      a						:		Any											=      []
      a						:		Union[str, Any]											=      []
      for i in range(__UpperCAmelCase):
        a						:		Any											=      [char_scores[i], bpe_scores[i], wp_scores[i]]
        a						:		Optional[Any]											=      [char_strs[i], bpe_strs[i], wp_strs[i]]
        a						:		List[str]											=      scores.index(max(__UpperCAmelCase))
        final_strs.append(strs[max_score_index])
        final_scores.append(scores[max_score_index])

      a						:		Dict											=      {}
      a						:		List[str]											=      final_strs
      a						:		str											=      final_scores
      a						:		int											=      char_strs
      a						:		int											=      bpe_strs
      a						:		Tuple											=      wp_strs
      return out






    def      __snake_case							( self    :   Any    ,			__UpperCAmelCase    :   List[str]    ,			__UpperCAmelCase    :   List[str]):
      if format == DecodeType.CHARACTER:
        a						:		int											=      self.char_decode
        a						:		int											=      1
        a						:		Dict											=      "[s]"
      elif format == DecodeType.BPE:
        a						:		List[str]											=      self.bpe_decode
        a						:		List[str]											=      2
        a						:		int											=      "#"
      elif format == DecodeType.WORDPIECE:
        a						:		Union[str, Any]											=      self.wp_decode
        a						:		List[str]											=      102
        a						:		int											=      "[SEP]"
      else:
        raise ValueError(f'''Format {format} is not supported.''')

      a							,		a						:		str											=      [], []
      a						:		Optional[int]											=      pred_logits.size(0)
      a						:		List[str]											=      pred_logits.size(1)
      a							,		a						:		Tuple											=      pred_logits.topk(1    ,			dim=-1    ,			largest=__UpperCAmelCase    ,			sorted=__UpperCAmelCase)
      a						:		List[str]											=      preds_index.view(-1    ,			__UpperCAmelCase)[:, 1:]
      a						:		Any											=      decoder(__UpperCAmelCase)
      a							,		a						:		Union[str, Any]											=      torch.nn.functional.softmax(__UpperCAmelCase    ,			dim=2).max(dim=2)
      a						:		Union[str, Any]											=      preds_max_prob[:, 1:]

      for index in range(__UpperCAmelCase):
        a						:		str											=      preds_str[index].find(__UpperCAmelCase)
        a						:		Optional[Any]											=      preds_str[index][:pred_eos]
        a						:		Optional[int]											=      preds_index[index].cpu().tolist()
        a						:		Optional[int]											=      pred_index.index(__UpperCAmelCase) if eos_token in pred_index else -1
        a						:		List[str]											=      preds_max_prob[index][: pred_eos_index + 1]
        a						:		int											=      pred_max_prob.cumprod(dim=0)[-1] if pred_max_prob.nelement() != 0 else 0.0
        dec_strs.append(__UpperCAmelCase)
        conf_scores.append(__UpperCAmelCase)

      return dec_strs, conf_scores






    def      __snake_case							( self    :   Optional[int]    ,			__UpperCAmelCase    :   Any):
      a						:		Dict											=      [seq.replace(" "    ,			"") for seq in self.char_tokenizer.batch_decode(__UpperCAmelCase)]
      return decode_strs






    def      __snake_case							( self    :   Optional[int]    ,			__UpperCAmelCase    :   List[str]):
      return self.bpe_tokenizer.batch_decode(__UpperCAmelCase)






    def      __snake_case							( self    :   Union[str, Any]    ,			__UpperCAmelCase    :   int):
      a						:		Any											=      [seq.replace(" "    ,			"") for seq in self.wp_tokenizer.batch_decode(__UpperCAmelCase)]
      return decode_strs






