



import warnings
from typing import Dict

import numpy as np

from ..utils import ExplicitEnum, add_end_docstrings, is_tf_available, is_torch_available
from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline


if is_tf_available():
     from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

if is_torch_available():
     from ..models.auto.modeling_auto import MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

def     UpperCamelCase_   (   lowerCAmelCase__				:    Any						)						->       List[str]:

  """simple docstring"""




  return 1.0 / (1.0 + np.exp(-_outputs						))

def     UpperCamelCase_   (   lowerCAmelCase__				:    int						)						->       Optional[int]:

  """simple docstring"""




  lowerCAmelCase_       :    Any								=			np.max(_outputs   ,       axis=-1   ,       keepdims=lowerCAmelCase__						)
  lowerCAmelCase_       :    Tuple								=			np.exp(_outputs - maxes						)
  return shifted_exp / shifted_exp.sum(axis=-1   ,       keepdims=lowerCAmelCase__						)







class 				UpperCamelCase__       (     lowerCAmelCase_  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					"""sigmoid"""
      _SCREAMING_SNAKE_CASE      =					"""softmax"""
      _SCREAMING_SNAKE_CASE      =					"""none"""







@add_end_docstrings(
    lowerCAmelCase_, r"""
        return_all_scores (`bool`, *optional*, defaults to `False`):
            Whether to return all prediction scores or just the one of the predicted class.
        function_to_apply (`str`, *optional*, defaults to `\"default\"`):
            The function to apply to the model outputs in order to retrieve the scores. Accepts four different values:

            - `\"default\"`: if the model has a single label, will apply the sigmoid function on the output. If the model
              has several labels, will apply the softmax function on the output.
            - `\"sigmoid\"`: Applies the sigmoid function on the output.
            - `\"softmax\"`: Applies the softmax function on the output.
            - `\"none\"`: Does not apply any function on the output.
    """, )
class 				UpperCamelCase__       (     lowerCAmelCase_  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					False
      _SCREAMING_SNAKE_CASE      =					ClassificationFunction.NONE



      def __init__(				self    :				int   ,      **SCREAMING_SNAKE_CASE_    :				Union[str, Any]	):
        super().__init__(**__SCREAMING_SNAKE_CASE	)

        self.check_model_type(
            TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
            if self.framework == 'tf'
            else MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]   ,      SCREAMING_SNAKE_CASE_    :				List[str]=None   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=None   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=""   ,      **SCREAMING_SNAKE_CASE_    :				Optional[int]	):
        lowerCAmelCase_       :    Tuple								=			tokenizer_kwargs

        lowerCAmelCase_       :    Any								=			{}
        if hasattr(self.model.config   ,      'return_all_scores'	) and return_all_scores is None:
          lowerCAmelCase_       :    List[Any]								=			self.model.config.return_all_scores

        if isinstance(__SCREAMING_SNAKE_CASE   ,      __SCREAMING_SNAKE_CASE	) or top_k is None:
          lowerCAmelCase_       :    Optional[int]								=			top_k
          lowerCAmelCase_       :    Dict								=			False
        elif return_all_scores is not None:
          warnings.warn(
              '`return_all_scores` is now deprecated,  if want a similar functionality use `top_k=None` instead of'
              ' `return_all_scores=True` or `top_k=1` instead of `return_all_scores=False`.'   ,      __SCREAMING_SNAKE_CASE   ,      )
          if return_all_scores:
            lowerCAmelCase_       :    Optional[int]								=			None
          else:
            lowerCAmelCase_       :    Optional[Any]								=			1

        if isinstance(__SCREAMING_SNAKE_CASE   ,      __SCREAMING_SNAKE_CASE	):
          lowerCAmelCase_       :    Dict								=			ClassificationFunction[function_to_apply.upper()]

        if function_to_apply is not None:
          lowerCAmelCase_       :    Optional[Any]								=			function_to_apply
        return preprocess_params, {}, postprocess_params



      def __call__(				self    :				int   ,      *SCREAMING_SNAKE_CASE_    :				List[Any]   ,      **SCREAMING_SNAKE_CASE_    :				Optional[Any]	):
        lowerCAmelCase_       :    Optional[int]								=			super().__call__(*__SCREAMING_SNAKE_CASE   ,      **__SCREAMING_SNAKE_CASE	)
        # TODO try and retrieve it in a nicer way from _sanitize_parameters.
        lowerCAmelCase_       :    int								=			'top_k' not in kwargs
        if isinstance(args[0]   ,      __SCREAMING_SNAKE_CASE	) and _legacy:
          # This pipeline is odd, and return a list when single item is run
          return [result]
        else:
          return result



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[int]   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]   ,      **SCREAMING_SNAKE_CASE_    :				str	):
        lowerCAmelCase_       :    Optional[Any]								=			self.framework
        if isinstance(__SCREAMING_SNAKE_CASE   ,      __SCREAMING_SNAKE_CASE	):
          return self.tokenizer(**__SCREAMING_SNAKE_CASE   ,      return_tensors=__SCREAMING_SNAKE_CASE   ,      **__SCREAMING_SNAKE_CASE	)
        elif isinstance(__SCREAMING_SNAKE_CASE   ,      __SCREAMING_SNAKE_CASE	) and len(__SCREAMING_SNAKE_CASE	) == 1 and isinstance(inputs[0]   ,      __SCREAMING_SNAKE_CASE	) and len(inputs[0]	) == 2:
          # It used to be valid to use a list of list of list for text pairs, keeping this path for BC
          return self.tokenizer(
              text=inputs[0][0]   ,      text_pair=inputs[0][1]   ,      return_tensors=__SCREAMING_SNAKE_CASE   ,      **__SCREAMING_SNAKE_CASE	)
        elif isinstance(__SCREAMING_SNAKE_CASE   ,      __SCREAMING_SNAKE_CASE	):
          # This is likely an invalid usage of the pipeline attempting to pass text pairs.
          raise ValueError(
              'The pipeline received invalid inputs, if you are trying to send text pairs, you can try to send a'
              ' dictionary `{\"text\": \"My text\", \"text_pair\": \"My pair\"}` in order to send a text pair.'	)
        return self.tokenizer(__SCREAMING_SNAKE_CASE   ,      return_tensors=__SCREAMING_SNAKE_CASE   ,      **__SCREAMING_SNAKE_CASE	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]   ,      SCREAMING_SNAKE_CASE_    :				List[Any]	):
        return self.model(**__SCREAMING_SNAKE_CASE	)





      def 					SCREAMING_SNAKE_CASE__			(				self    :				Any   ,      SCREAMING_SNAKE_CASE_    :				Dict   ,      SCREAMING_SNAKE_CASE_    :				Any=None   ,      SCREAMING_SNAKE_CASE_    :				List[str]=1   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=True	):

        if function_to_apply is None:
          if self.model.config.problem_type == "multi_label_classification" or self.model.config.num_labels == 1:
            lowerCAmelCase_       :    Optional[int]								=			ClassificationFunction.SIGMOID
          elif self.model.config.problem_type == "single_label_classification" or self.model.config.num_labels > 1:
            lowerCAmelCase_       :    Any								=			ClassificationFunction.SOFTMAX
          elif hasattr(self.model.config   ,      'function_to_apply'	) and function_to_apply is None:
            lowerCAmelCase_       :    Optional[int]								=			self.model.config.function_to_apply
          else:
            lowerCAmelCase_       :    Union[str, Any]								=			ClassificationFunction.NONE

        lowerCAmelCase_       :    str								=			model_outputs['logits'][0]
        lowerCAmelCase_       :    Union[str, Any]								=			outputs.numpy()

        if function_to_apply == ClassificationFunction.SIGMOID:
          lowerCAmelCase_       :    Any								=			sigmoid(__SCREAMING_SNAKE_CASE	)
        elif function_to_apply == ClassificationFunction.SOFTMAX:
          lowerCAmelCase_       :    List[Any]								=			softmax(__SCREAMING_SNAKE_CASE	)
        elif function_to_apply == ClassificationFunction.NONE:
          lowerCAmelCase_       :    Any								=			outputs
        else:
          raise ValueError(F"Unrecognized `function_to_apply` argument: {function_to_apply}"	)

        if top_k == 1 and _legacy:
          return {"label": self.model.config.idalabel[scores.argmax().item()], "score": scores.max().item()}

        lowerCAmelCase_       :    Optional[Any]								=			[
            {'label': self.model.config.idalabel[i], 'score': score.item()} for i, score in enumerate(__SCREAMING_SNAKE_CASE	)
        ]
        if not _legacy:
          dict_scores.sort(key=lambda SCREAMING_SNAKE_CASE_	: x["score"]   ,      reverse=__SCREAMING_SNAKE_CASE	)
          if top_k is not None:
            lowerCAmelCase_       :    List[str]								=			dict_scores[:top_k]
        return dict_scores










"""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__		:    str				   =     logging.get_logger(__name__)

def     UpperCamelCase_   (   lowerCAmelCase__				:    bool   ,       lowerCAmelCase__				:    bool						)						->       List[Any]:

  """simple docstring"""




  def run_func(lowerCAmelCase__				:    int						):
    @wraps(lowerCAmelCase__						)
    def run_in_eager_mode(*lowerCAmelCase__				:    List[Any]   ,       **lowerCAmelCase__				:    int						):
      return func(*lowerCAmelCase__   ,       **lowerCAmelCase__						)

    @wraps(lowerCAmelCase__						)
    @tf.function(experimental_compile=lowerCAmelCase__						)
    def run_in_graph_mode(*lowerCAmelCase__				:    Dict   ,       **lowerCAmelCase__				:    Any						):
      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     UpperCamelCase_   (   lowerCAmelCase__				:    int   ,       lowerCAmelCase__				:    int   ,       lowerCAmelCase__				:    int						)						->       ["tf.Tensor"]:

  """simple docstring"""




  lowerCAmelCase_       :    Dict								=			random.Random()
  lowerCAmelCase_       :    List[str]								=			[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 				UpperCamelCase__       (     lowercase_  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					42
      _SCREAMING_SNAKE_CASE      =					42
      _SCREAMING_SNAKE_CASE      =					"TensorFlow"



      @property
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]	):
        return tf.__version__



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]   ,      SCREAMING_SNAKE_CASE_    :				str   ,      SCREAMING_SNAKE_CASE_    :				int   ,      SCREAMING_SNAKE_CASE_    :				int	):
        # initialize GPU on separate process
        lowerCAmelCase_       :    List[Any]								=			self.args.strategy
        if strategy is None:
          raise ValueError('A device strategy has to be initialized before using TensorFlow.'	)
        lowerCAmelCase_       :    List[str]								=			self._prepare_inference_func(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	)
        return self._measure_speed(_inference	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Any   ,      SCREAMING_SNAKE_CASE_    :				str   ,      SCREAMING_SNAKE_CASE_    :				int   ,      SCREAMING_SNAKE_CASE_    :				int	):
        lowerCAmelCase_       :    Optional[int]								=			self.args.strategy
        if strategy is None:
          raise ValueError('A device strategy has to be initialized before using TensorFlow.'	)
        lowerCAmelCase_       :    Any								=			self._prepare_train_func(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	)
        return self._measure_speed(_train	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]   ,      SCREAMING_SNAKE_CASE_    :				str   ,      SCREAMING_SNAKE_CASE_    :				int   ,      SCREAMING_SNAKE_CASE_    :				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]   ,      SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    Any								=			self.args.strategy
        if strategy is None:
          raise ValueError('A device strategy has to be initialized before using TensorFlow.'	)
        lowerCAmelCase_       :    Optional[Any]								=			self._prepare_inference_func(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	)
        return self._measure_memory(_inference	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Any   ,      SCREAMING_SNAKE_CASE_    :				str   ,      SCREAMING_SNAKE_CASE_    :				int   ,      SCREAMING_SNAKE_CASE_    :				int	):
        if self.args.is_gpu:
          tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx]   ,      SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    Any								=			self.args.strategy
        if strategy is None:
          raise ValueError('A device strategy has to be initialized before using TensorFlow.'	)

        lowerCAmelCase_       :    Optional[int]								=			self._prepare_train_func(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	)
        return self._measure_memory(_train	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Any   ,      SCREAMING_SNAKE_CASE_    :				str   ,      SCREAMING_SNAKE_CASE_    :				int   ,      SCREAMING_SNAKE_CASE_    :				int	):
        lowerCAmelCase_       :    Any								=			self.config_dict[model_name]

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

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

        # encoder-decoder has vocab size saved differently
        lowerCAmelCase_       :    List[Any]								=			config.vocab_size if hasattr(SCREAMING_SNAKE_CASE_   ,      'vocab_size'	) else config.encoder.vocab_size
        lowerCAmelCase_       :    Tuple								=			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_	)

        lowerCAmelCase_       :    List[Any]								=			encoder_decoder_forward if config.is_encoder_decoder else encoder_forward

        return _inference



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[int]   ,      SCREAMING_SNAKE_CASE_    :				str   ,      SCREAMING_SNAKE_CASE_    :				int   ,      SCREAMING_SNAKE_CASE_    :				int	):
        lowerCAmelCase_       :    Union[str, Any]								=			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.'	)

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

        # encoder-decoder has vocab size saved differently
        lowerCAmelCase_       :    int								=			config.vocab_size if hasattr(SCREAMING_SNAKE_CASE_   ,      'vocab_size'	) else config.encoder.vocab_size
        lowerCAmelCase_       :    Optional[Any]								=			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():
          lowerCAmelCase_       :    str								=			model(SCREAMING_SNAKE_CASE_   ,      decoder_input_ids=SCREAMING_SNAKE_CASE_   ,      labels=SCREAMING_SNAKE_CASE_   ,      training=SCREAMING_SNAKE_CASE_	)[0]
          lowerCAmelCase_       :    Optional[int]								=			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():
          lowerCAmelCase_       :    Dict								=			model(SCREAMING_SNAKE_CASE_   ,      labels=SCREAMING_SNAKE_CASE_   ,      training=SCREAMING_SNAKE_CASE_	)[0]
          lowerCAmelCase_       :    str								=			tf.gradients(SCREAMING_SNAKE_CASE_   ,      model.trainable_variables	)
          return gradients

        lowerCAmelCase_       :    int								=			encoder_decoder_train if config.is_encoder_decoder else encoder_train

        return _train



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]   ,      SCREAMING_SNAKE_CASE_    :				Tuple	):
        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
            lowerCAmelCase_       :    Dict								=			timeit.repeat(
                SCREAMING_SNAKE_CASE_   ,      repeat=self.args.repeat   ,      number=1_0   ,      )

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





      def 					SCREAMING_SNAKE_CASE__			(				self    :				int   ,      SCREAMING_SNAKE_CASE_    :				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.'	)
              lowerCAmelCase_       :    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.'	)
                lowerCAmelCase_       :    Tuple								=			'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()
                lowerCAmelCase_       :    int								=			nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx	)
                lowerCAmelCase_       :    Union[str, Any]								=			nvml.nvmlDeviceGetMemoryInfo(SCREAMING_SNAKE_CASE_	)
                lowerCAmelCase_       :    List[Any]								=			meminfo.used
                lowerCAmelCase_       :    int								=			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.'	)
                lowerCAmelCase_       :    Optional[int]								=			None
              else:
                lowerCAmelCase_       :    Union[str, Any]								=			measure_peak_memory_cpu(SCREAMING_SNAKE_CASE_	)
                lowerCAmelCase_       :    Any								=			Memory(SCREAMING_SNAKE_CASE_	) if isinstance(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	) else memory_bytes
            if self.args.trace_memory_line_by_line:
              lowerCAmelCase_       :    List[Any]								=			stop_memory_tracing(SCREAMING_SNAKE_CASE_	)
              if memory is None:
                lowerCAmelCase_       :    Union[str, Any]								=			summary.total
            else:
              lowerCAmelCase_       :    List[str]								=			None

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





