"""simple docstring"""


import unittest

from parameterized import parameterized

from transformers import OpenLlamaConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel


class 				a :


    def __init__(    self		:     List[Any]      , __lowerCAmelCase		:     str      , __lowerCAmelCase		:     Union[str, Any]=13      , __lowerCAmelCase		:     Tuple=7      , __lowerCAmelCase		:     Optional[Any]=True      , __lowerCAmelCase		:     List[str]=True      , __lowerCAmelCase		:     Optional[Any]=False      , __lowerCAmelCase		:     List[str]=True      , __lowerCAmelCase		:     List[str]=99      , __lowerCAmelCase		:     Any=32      , __lowerCAmelCase		:     Optional[Any]=5      , __lowerCAmelCase		:     str=4      , __lowerCAmelCase		:     Any=37      , __lowerCAmelCase		:     Union[str, Any]="gelu"      , __lowerCAmelCase		:     str=0.1      , __lowerCAmelCase		:     int=0.1      , __lowerCAmelCase		:     List[Any]=512      , __lowerCAmelCase		:     Dict=16      , __lowerCAmelCase		:     Dict=2      , __lowerCAmelCase		:     int=0.02      , __lowerCAmelCase		:     List[str]=3      , __lowerCAmelCase		:     Dict=4      , __lowerCAmelCase		:     Dict=None      , ):
      _UpperCAmelCase         =					parent
      _UpperCAmelCase         =					batch_size
      _UpperCAmelCase         =					seq_length
      _UpperCAmelCase         =					is_training
      _UpperCAmelCase         =					use_input_mask
      _UpperCAmelCase         =					use_token_type_ids
      _UpperCAmelCase         =					use_labels
      _UpperCAmelCase         =					vocab_size
      _UpperCAmelCase         =					hidden_size
      _UpperCAmelCase         =					num_hidden_layers
      _UpperCAmelCase         =					num_attention_heads
      _UpperCAmelCase         =					intermediate_size
      _UpperCAmelCase         =					hidden_act
      _UpperCAmelCase         =					hidden_dropout_prob
      _UpperCAmelCase         =					attention_probs_dropout_prob
      _UpperCAmelCase         =					max_position_embeddings
      _UpperCAmelCase         =					type_vocab_size
      _UpperCAmelCase         =					type_sequence_label_size
      _UpperCAmelCase         =					initializer_range
      _UpperCAmelCase         =					num_labels
      _UpperCAmelCase         =					num_choices
      _UpperCAmelCase         =					scope


    def    lowerCAmelCase_     (    self		:     Dict    ):
      _UpperCAmelCase         =					ids_tensor([self.batch_size, self.seq_length]      , self.vocab_size    )

      _UpperCAmelCase         =					None
      if self.use_input_mask:
        _UpperCAmelCase         =					random_attention_mask([self.batch_size, self.seq_length]    )

      _UpperCAmelCase         =					None
      if self.use_token_type_ids:
        _UpperCAmelCase         =					ids_tensor([self.batch_size, self.seq_length]      , self.type_vocab_size    )

      _UpperCAmelCase         =					None
      _UpperCAmelCase         =					None
      _UpperCAmelCase         =					None
      if self.use_labels:
        _UpperCAmelCase         =					ids_tensor([self.batch_size]      , self.type_sequence_label_size    )
        _UpperCAmelCase         =					ids_tensor([self.batch_size, self.seq_length]      , self.num_labels    )
        _UpperCAmelCase         =					ids_tensor([self.batch_size]      , self.num_choices    )

      _UpperCAmelCase         =					self.get_config()

      return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


    def    lowerCAmelCase_     (    self		:     Optional[Any]    ):
      return OpenLlamaConfig(
          vocab_size=self.vocab_size      , hidden_size=self.hidden_size      , num_hidden_layers=self.num_hidden_layers      , num_attention_heads=self.num_attention_heads      , intermediate_size=self.intermediate_size      , hidden_act=self.hidden_act      , hidden_dropout_prob=self.hidden_dropout_prob      , attention_probs_dropout_prob=self.attention_probs_dropout_prob      , max_position_embeddings=self.max_position_embeddings      , type_vocab_size=self.type_vocab_size      , is_decoder=__lowerCAmelCase      , initializer_range=self.initializer_range      , use_stable_embedding=__lowerCAmelCase      , )


    def    lowerCAmelCase_     (    self		:     Any      , __lowerCAmelCase		:     str      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     Dict      , __lowerCAmelCase		:     Optional[int]      , __lowerCAmelCase		:     str      , __lowerCAmelCase		:     Union[str, Any]      , __lowerCAmelCase		:     Optional[Any]    ):
      _UpperCAmelCase         =					OpenLlamaModel(config=__lowerCAmelCase    )
      model.to(__lowerCAmelCase    )
      model.eval()
      _UpperCAmelCase         =					model(__lowerCAmelCase      , attention_mask=__lowerCAmelCase    )
      _UpperCAmelCase         =					model(__lowerCAmelCase    )
      self.parent.assertEqual(result.last_hidden_state.shape      , (self.batch_size, self.seq_length, self.hidden_size)    )


    def    lowerCAmelCase_     (    self		:     str      , __lowerCAmelCase		:     Tuple      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     Any      , __lowerCAmelCase		:     Tuple      , __lowerCAmelCase		:     str      , __lowerCAmelCase		:     Tuple      , __lowerCAmelCase		:     List[str]      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     Optional[Any]      , ):
      _UpperCAmelCase         =					True
      _UpperCAmelCase         =					OpenLlamaModel(__lowerCAmelCase    )
      model.to(__lowerCAmelCase    )
      model.eval()
      _UpperCAmelCase         =					model(
          __lowerCAmelCase      , attention_mask=__lowerCAmelCase      , encoder_hidden_states=__lowerCAmelCase      , encoder_attention_mask=__lowerCAmelCase      , )
      _UpperCAmelCase         =					model(
          __lowerCAmelCase      , attention_mask=__lowerCAmelCase      , encoder_hidden_states=__lowerCAmelCase      , )
      _UpperCAmelCase         =					model(__lowerCAmelCase      , attention_mask=__lowerCAmelCase    )
      self.parent.assertEqual(result.last_hidden_state.shape      , (self.batch_size, self.seq_length, self.hidden_size)    )


    def    lowerCAmelCase_     (    self		:     int      , __lowerCAmelCase		:     Tuple      , __lowerCAmelCase		:     List[Any]      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     Optional[Any]      , __lowerCAmelCase		:     Dict      , __lowerCAmelCase		:     Union[str, Any]      , __lowerCAmelCase		:     str      , __lowerCAmelCase		:     Tuple      , __lowerCAmelCase		:     int      , ):
      _UpperCAmelCase         =					OpenLlamaForCausalLM(config=__lowerCAmelCase    )
      model.to(__lowerCAmelCase    )
      model.eval()
      _UpperCAmelCase         =					model(__lowerCAmelCase      , attention_mask=__lowerCAmelCase      , labels=__lowerCAmelCase    )
      self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.seq_length, self.vocab_size)    )


    def    lowerCAmelCase_     (    self		:     Any      , __lowerCAmelCase		:     Union[str, Any]      , __lowerCAmelCase		:     Optional[Any]      , __lowerCAmelCase		:     str      , __lowerCAmelCase		:     Dict      , __lowerCAmelCase		:     Union[str, Any]      , __lowerCAmelCase		:     Dict      , __lowerCAmelCase		:     List[Any]      , __lowerCAmelCase		:     Optional[int]      , __lowerCAmelCase		:     List[str]      , ):
      _UpperCAmelCase         =					True
      _UpperCAmelCase         =					True
      _UpperCAmelCase         =					OpenLlamaForCausalLM(config=__lowerCAmelCase    )
      model.to(__lowerCAmelCase    )
      model.eval()

      # first forward pass
      _UpperCAmelCase         =					model(
          __lowerCAmelCase      , attention_mask=__lowerCAmelCase      , encoder_hidden_states=__lowerCAmelCase      , encoder_attention_mask=__lowerCAmelCase      , use_cache=__lowerCAmelCase      , )
      _UpperCAmelCase         =					outputs.past_key_values

      # create hypothetical multiple next token and extent to next_input_ids
      _UpperCAmelCase         =					ids_tensor((self.batch_size, 3)      , config.vocab_size    )
      _UpperCAmelCase         =					ids_tensor((self.batch_size, 3)      , vocab_size=2    )

      # append to next input_ids and
      _UpperCAmelCase         =					torch.cat([input_ids, next_tokens]      , dim=-1    )
      _UpperCAmelCase         =					torch.cat([input_mask, next_mask]      , dim=-1    )

      _UpperCAmelCase         =					model(
          __lowerCAmelCase      , attention_mask=__lowerCAmelCase      , encoder_hidden_states=__lowerCAmelCase      , encoder_attention_mask=__lowerCAmelCase      , output_hidden_states=__lowerCAmelCase      , )["""hidden_states"""][0]
      _UpperCAmelCase         =					model(
          __lowerCAmelCase      , attention_mask=__lowerCAmelCase      , encoder_hidden_states=__lowerCAmelCase      , encoder_attention_mask=__lowerCAmelCase      , past_key_values=__lowerCAmelCase      , output_hidden_states=__lowerCAmelCase      , )["""hidden_states"""][0]

      # select random slice
      _UpperCAmelCase         =					ids_tensor((1,)      , output_from_past.shape[-1]    ).item()
      _UpperCAmelCase         =					output_from_no_past[:, -3:, random_slice_idx].detach()
      _UpperCAmelCase         =					output_from_past[:, :, random_slice_idx].detach()

      self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]    )

      # test that outputs are equal for slice
      self.parent.assertTrue(torch.allclose(__lowerCAmelCase      , __lowerCAmelCase      , atol=1e-3    )    )


    def    lowerCAmelCase_     (    self		:     List[Any]    ):
      _UpperCAmelCase         =					self.prepare_config_and_inputs()
      (
          (
          _UpperCAmelCase
      )       , (
          _UpperCAmelCase
      )       , (
          _UpperCAmelCase
      )       , (
          _UpperCAmelCase
      )       , (
          _UpperCAmelCase
      )       , (
          _UpperCAmelCase
      )       , (
          _UpperCAmelCase
      )       , 
      )         =					config_and_inputs
      _UpperCAmelCase         =					{"""input_ids""": input_ids, """attention_mask""": input_mask}
      return config, inputs_dict



@require_torch
class 				a (						lowerCAmelCase_		,							lowerCAmelCase_		,							lowerCAmelCase_		,							unittest.TestCase      ):
    _snake_case   :     str   							=							(
        (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
    )
    _snake_case   :     Dict   							=							(OpenLlamaForCausalLM,) if is_torch_available() else ()
    _snake_case   :     Tuple   							=							(
        {
            'feature-extraction': OpenLlamaModel,
            'text-classification': OpenLlamaForSequenceClassification,
            'text-generation': OpenLlamaForCausalLM,
            'zero-shot': OpenLlamaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    _snake_case   :     Optional[int]   							=							False
    _snake_case   :     str   							=							False


    def    lowerCAmelCase_     (    self		:     Tuple    ):
      _UpperCAmelCase         =					OpenLlamaModelTester(self    )
      _UpperCAmelCase         =					ConfigTester(self      , config_class=__lowerCAmelCase      , hidden_size=37    )


    def    lowerCAmelCase_     (    self		:     List[str]    ):
      self.config_tester.run_common_tests()


    def    lowerCAmelCase_     (    self		:     Any    ):
      _UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__lowerCAmelCase    )


    def    lowerCAmelCase_     (    self		:     int    ):
      _UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
        _UpperCAmelCase         =					type
        self.model_tester.create_and_check_model(*__lowerCAmelCase    )


    def    lowerCAmelCase_     (    self		:     str    ):
      _UpperCAmelCase       , _UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs_for_common()
      _UpperCAmelCase         =					3
      _UpperCAmelCase         =					input_dict["""input_ids"""]
      _UpperCAmelCase         =					input_ids.ne(1    ).to(__lowerCAmelCase    )
      _UpperCAmelCase         =					ids_tensor([self.model_tester.batch_size]      , self.model_tester.type_sequence_label_size    )
      _UpperCAmelCase         =					OpenLlamaForSequenceClassification(__lowerCAmelCase    )
      model.to(__lowerCAmelCase    )
      model.eval()
      _UpperCAmelCase         =					model(__lowerCAmelCase      , attention_mask=__lowerCAmelCase      , labels=__lowerCAmelCase    )
      self.assertEqual(result.logits.shape      , (self.model_tester.batch_size, self.model_tester.num_labels)    )


    def    lowerCAmelCase_     (    self		:     str    ):
      _UpperCAmelCase       , _UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs_for_common()
      _UpperCAmelCase         =					3
      _UpperCAmelCase         =					"""single_label_classification"""
      _UpperCAmelCase         =					input_dict["""input_ids"""]
      _UpperCAmelCase         =					input_ids.ne(1    ).to(__lowerCAmelCase    )
      _UpperCAmelCase         =					ids_tensor([self.model_tester.batch_size]      , self.model_tester.type_sequence_label_size    )
      _UpperCAmelCase         =					OpenLlamaForSequenceClassification(__lowerCAmelCase    )
      model.to(__lowerCAmelCase    )
      model.eval()
      _UpperCAmelCase         =					model(__lowerCAmelCase      , attention_mask=__lowerCAmelCase      , labels=__lowerCAmelCase    )
      self.assertEqual(result.logits.shape      , (self.model_tester.batch_size, self.model_tester.num_labels)    )


    def    lowerCAmelCase_     (    self		:     Dict    ):
      _UpperCAmelCase       , _UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs_for_common()
      _UpperCAmelCase         =					3
      _UpperCAmelCase         =					"""multi_label_classification"""
      _UpperCAmelCase         =					input_dict["""input_ids"""]
      _UpperCAmelCase         =					input_ids.ne(1    ).to(__lowerCAmelCase    )
      _UpperCAmelCase         =					ids_tensor(
          [self.model_tester.batch_size, config.num_labels]      , self.model_tester.type_sequence_label_size    ).to(torch.float    )
      _UpperCAmelCase         =					OpenLlamaForSequenceClassification(__lowerCAmelCase    )
      model.to(__lowerCAmelCase    )
      model.eval()
      _UpperCAmelCase         =					model(__lowerCAmelCase      , attention_mask=__lowerCAmelCase      , labels=__lowerCAmelCase    )
      self.assertEqual(result.logits.shape      , (self.model_tester.batch_size, self.model_tester.num_labels)    )


    @unittest.skip("""Open-Llama buffers include complex numbers, which breaks this test"""    )
    def    lowerCAmelCase_     (    self		:     Any    ):
      pass


    @parameterized.expand([("""linear""",), ("""dynamic""",)]    )
    def    lowerCAmelCase_     (    self		:     List[Any]      , __lowerCAmelCase		:     Tuple    ):
      _UpperCAmelCase       , _UpperCAmelCase         =					self.model_tester.prepare_config_and_inputs_for_common()
      _UpperCAmelCase         =					ids_tensor([1, 10]      , config.vocab_size    )
      _UpperCAmelCase         =					ids_tensor([1, int(config.max_position_embeddings * 1.5    )]      , config.vocab_size    )

      set_seed(42    )  # Fixed seed at init time so the two models get the same random weights
      _UpperCAmelCase         =					OpenLlamaModel(__lowerCAmelCase    )
      original_model.to(__lowerCAmelCase    )
      original_model.eval()
      _UpperCAmelCase         =					original_model(__lowerCAmelCase    ).last_hidden_state
      _UpperCAmelCase         =					original_model(__lowerCAmelCase    ).last_hidden_state

      set_seed(42    )  # Fixed seed at init time so the two models get the same random weights
      _UpperCAmelCase         =					{"""type""": scaling_type, """factor""": 10.0}
      _UpperCAmelCase         =					OpenLlamaModel(__lowerCAmelCase    )
      scaled_model.to(__lowerCAmelCase    )
      scaled_model.eval()
      _UpperCAmelCase         =					scaled_model(__lowerCAmelCase    ).last_hidden_state
      _UpperCAmelCase         =					scaled_model(__lowerCAmelCase    ).last_hidden_state

      # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
      # maximum sequence length, so the outputs for the short input should match.
      if scaling_type == "dynamic":
        self.assertTrue(torch.allclose(__lowerCAmelCase      , __lowerCAmelCase      , atol=1e-5    )    )
      else:
        self.assertFalse(torch.allclose(__lowerCAmelCase      , __lowerCAmelCase      , atol=1e-5    )    )

      # The output should be different for long inputs
      self.assertFalse(torch.allclose(__lowerCAmelCase      , __lowerCAmelCase      , atol=1e-5    )    )

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

UpperCAmelCase__     						=  logging.get_logger(__name__)






def 			__UpperCAmelCase    (	lowercase      ,lowercase						):







  """simple docstring"""
  def run_func(lowercase						):
    @wraps(lowercase						)
    def run_in_eager_mode(*lowercase      ,**lowercase						):
      return func(*lowercase      ,**lowercase						)

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

    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    (	lowercase      ,lowercase      ,lowercase						):







  """simple docstring"""
  _UpperCAmelCase         =					random.Random()
  _UpperCAmelCase         =					[rng.randint(0      ,vocab_size - 1						) for i in range(batch_size * sequence_length						)]
  return tf.constant(lowercase      ,shape=(batch_size, sequence_length)      ,dtype=tf.intaa						)


class 				a (						lowerCAmelCase_      ):
    _snake_case   :     TensorFlowBenchmarkArguments
    _snake_case   :     PretrainedConfig
    _snake_case   :     str   							=							"TensorFlow"


    @property
    def    lowerCAmelCase_     (    self		:     Union[str, Any]    ):
      return tf.__version__


    def    lowerCAmelCase_     (    self		:     List[str]      , __lowerCAmelCase		:     str      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     int    ):
      # initialize GPU on separate process
      _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(__lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase    )
      return self._measure_speed(_inference    )


    def    lowerCAmelCase_     (    self		:     Optional[int]      , __lowerCAmelCase		:     str      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     int    ):
      _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(__lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase    )
      return self._measure_speed(_train    )


    def    lowerCAmelCase_     (    self		:     Optional[Any]      , __lowerCAmelCase		:     str      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     int    ):
      # 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    )
      _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(__lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase    )
      return self._measure_memory(_inference    )


    def    lowerCAmelCase_     (    self		:     str      , __lowerCAmelCase		:     str      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     int    ):
      if self.args.is_gpu:
        tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx]      , __lowerCAmelCase    )
      _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(__lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase    )
      return self._measure_memory(_train    )


    def    lowerCAmelCase_     (    self		:     List[Any]      , __lowerCAmelCase		:     str      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     int    ):
      _UpperCAmelCase         =					self.config_dict[model_name]

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

      _UpperCAmelCase         =					(
          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:
          _UpperCAmelCase         =					"""TF""" + config.architectures[0]  # prepend 'TF' for tensorflow model
          _UpperCAmelCase         =					__import__("""transformers"""      , fromlist=[model_class]    )
          _UpperCAmelCase         =					getattr(__lowerCAmelCase      , __lowerCAmelCase    )
          _UpperCAmelCase         =					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:
        _UpperCAmelCase         =					TF_MODEL_MAPPING[config.__class__](__lowerCAmelCase    )

      # encoder-decoder has vocab size saved differently
      _UpperCAmelCase         =					config.vocab_size if hasattr(__lowerCAmelCase      , """vocab_size"""    ) else config.encoder.vocab_size
      _UpperCAmelCase         =					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    )

      _UpperCAmelCase         =					encoder_decoder_forward if config.is_encoder_decoder else encoder_forward

      return _inference


    def    lowerCAmelCase_     (    self		:     List[str]      , __lowerCAmelCase		:     str      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     int    ):
      _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(__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:
          _UpperCAmelCase         =					"""TF""" + config.architectures[0]  # prepend 'TF' for tensorflow model
          _UpperCAmelCase         =					__import__("""transformers"""      , fromlist=[model_class]    )
          _UpperCAmelCase         =					getattr(__lowerCAmelCase      , __lowerCAmelCase    )
          _UpperCAmelCase         =					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:
        _UpperCAmelCase         =					TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](__lowerCAmelCase    )

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

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

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

      _UpperCAmelCase         =					encoder_decoder_train if config.is_encoder_decoder else encoder_train

      return _train


    def    lowerCAmelCase_     (    self		:     Union[str, Any]      , __lowerCAmelCase		:     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
          _UpperCAmelCase         =					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    lowerCAmelCase_     (    self		:     List[Any]      , __lowerCAmelCase		:     Callable[[], None]    ):
      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(__lowerCAmelCase    )
              _UpperCAmelCase         =					meminfo.used
              _UpperCAmelCase         =					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."""    )
              _UpperCAmelCase         =					None
            else:
              _UpperCAmelCase         =					measure_peak_memory_cpu(__lowerCAmelCase    )
              _UpperCAmelCase         =					Memory(__lowerCAmelCase    ) if isinstance(__lowerCAmelCase      , __lowerCAmelCase    ) else memory_bytes
          if self.args.trace_memory_line_by_line:
            _UpperCAmelCase         =					stop_memory_tracing(__lowerCAmelCase    )
            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
