


import colorsys
import io

import cva
import matplotlib as mpl
import matplotlib.colors as mplc
import matplotlib.figure as mplfigure
import numpy as np
import torch
from matplotlib.backends.backend_agg import FigureCanvasAgg

from utils import img_tensorize


__lowerCamelCase   	=  1000


class 	a__						:

       def __init__( self   :  int   ,				lowerCamelCase_   :  List[str]   ,				lowerCamelCase_   :  Optional[Any]=1.2   ,				lowerCamelCase_   :  Any="g"   ,				lowerCamelCase_   :  int=0.5   ,				lowerCamelCase_   :  Optional[Any]="-"   ,				lowerCamelCase_   :  Union[str, Any]="test_out.jpg"   ,				lowerCamelCase_   :  List[str]=True   ,				lowerCamelCase_   :  int=False   ,				lowerCamelCase_   :  Dict=None   ,				lowerCamelCase_   :  Any=None   ,				lowerCamelCase_   :  Union[str, Any]=0.7   ,				):
            if isinstance(lowerCamelCase_   ,				torch.Tensor     ):
                 a_     :  int				      =     img.numpy().astype("""np.uint8"""     )
            if isinstance(lowerCamelCase_   ,				lowerCamelCase_     ):
                 a_     :  str				      =     img_tensorize(lowerCamelCase_     )
            assert isinstance(lowerCamelCase_   ,				np.ndarray     )

            a_      ,      a_     :  Optional[Any]				      =     img.shape[1], img.shape[0]
            a_     :  str				      =     mplfigure.Figure(frameon=lowerCamelCase_     )
            a_     :  Dict				      =     fig.get_dpi()
            a_     :  Dict				      =     (width * scale + 1E-2) / dpi
            a_     :  List[str]				      =     (height * scale + 1E-2) / dpi
            fig.set_size_inches(lowerCamelCase_   ,				lowerCamelCase_     )
            a_     :  Any				      =     fig.add_axes([0.0, 0.0, 1.0, 1.0]     )
            ax.axis("""off"""     )
            ax.set_xlim(0.0   ,				lowerCamelCase_     )
            ax.set_ylim(lowerCamelCase_     )

            a_     :  Optional[Any]				      =     saveas
            a_     :  List[Any]				      =     rgb
            a_     :  List[str]				      =     pynb
            a_     :  Union[str, Any]				      =     img
            a_     :  Union[str, Any]				      =     edgecolor
            a_     :  Optional[Any]				      =     0.5
            a_     :  Tuple				      =     linestyle
            a_     :  List[str]				      =     int(np.sqrt(min(lowerCamelCase_   ,				lowerCamelCase_     )     ) * scale // 3     )
            a_     :  Any				      =     width
            a_     :  List[Any]				      =     height
            a_     :  int				      =     scale
            a_     :  List[Any]				      =     fig
            a_     :  List[Any]				      =     ax
            a_     :  int				      =     pad
            a_     :  str				      =     idaobj
            a_     :  Optional[int]				      =     idaattr
            a_     :  Dict				      =     FigureCanvasAgg(lowerCamelCase_     )

       def     UpperCAmelCase( self   :  List[str]   ,				lowerCamelCase_   :  str   ,				lowerCamelCase_   :  Optional[Any]=None     ):
            if color is None:
                 a_     :  Union[str, Any]				      =     self.edgecolor
            ((a_)      ,      (a_)      ,      (a_)      ,      (a_))     :  Any				      =     box
            a_     :  Optional[int]				      =     xa - xa
            a_     :  Optional[int]				      =     ya - ya
            self.ax.add_patch(
                mpl.patches.Rectangle(
                    (xa, ya)   ,				lowerCamelCase_   ,				lowerCamelCase_   ,				fill=lowerCamelCase_   ,				edgecolor=lowerCamelCase_   ,				linewidth=self.font_size // 3   ,				alpha=self.alpha   ,				linestyle=self.linestyle   ,				)     )

       def     UpperCAmelCase( self   :  Tuple   ,				lowerCamelCase_   :  Dict   ,				lowerCamelCase_   :  Any=None   ,				lowerCamelCase_   :  Tuple=None   ,				lowerCamelCase_   :  int=None   ,				lowerCamelCase_   :  Optional[int]=None     ):
            if len(boxes.shape     ) > 2:
                 a_     :  Optional[Any]				      =     boxes[0]
            if len(obj_ids.shape     ) > 1:
                 a_     :  int				      =     obj_ids[0]
            if len(obj_scores.shape     ) > 1:
                 a_     :  Dict				      =     obj_scores[0]
            if len(attr_ids.shape     ) > 1:
                 a_     :  Union[str, Any]				      =     attr_ids[0]
            if len(attr_scores.shape     ) > 1:
                 a_     :  Any				      =     attr_scores[0]
            if isinstance(lowerCamelCase_   ,				torch.Tensor     ):
                 a_     :  Union[str, Any]				      =     boxes.numpy()
            if isinstance(lowerCamelCase_   ,				lowerCamelCase_     ):
                 a_     :  Optional[int]				      =     np.array(lowerCamelCase_     )
            assert isinstance(lowerCamelCase_   ,				np.ndarray     )
            a_     :  Tuple				      =     np.prod(boxes[:, 2:] - boxes[:, :2]   ,				axis=1     )
            a_     :  Any				      =     np.argsort(-areas     ).tolist()
            a_     :  Tuple				      =     boxes[sorted_idxs] if boxes is not None else None
            a_     :  str				      =     obj_ids[sorted_idxs] if obj_ids is not None else None
            a_     :  Optional[Any]				      =     obj_scores[sorted_idxs] if obj_scores is not None else None
            a_     :  Optional[Any]				      =     attr_ids[sorted_idxs] if attr_ids is not None else None
            a_     :  int				      =     attr_scores[sorted_idxs] if attr_scores is not None else None

            a_     :  List[Any]				      =     [self._random_color(maximum=1     ) for _ in range(len(lowerCamelCase_     )     )]
            a_     :  int				      =     [assigned_colors[idx] for idx in sorted_idxs]
            if obj_ids is not None:
                 a_     :  Optional[Any]				      =     self._create_text_labels_attr(lowerCamelCase_   ,				lowerCamelCase_   ,				lowerCamelCase_   ,				lowerCamelCase_     )
                 for i in range(len(lowerCamelCase_     )     ):
                      a_     :  int				      =     assigned_colors[i]
                      self.add_box(boxes[i]   ,				lowerCamelCase_     )
                      self.draw_labels(labels[i]   ,				boxes[i]   ,				lowerCamelCase_     )

       def     UpperCAmelCase( self   :  Union[str, Any]   ,				lowerCamelCase_   :  Dict   ,				lowerCamelCase_   :  Optional[int]   ,				lowerCamelCase_   :  List[str]     ):
            a_      ,      a_      ,      a_      ,      a_     :  Union[str, Any]				      =     box
            a_     :  List[Any]				      =     (xa, ya)
            a_     :  Optional[Any]				      =     (ya - ya) * (xa - xa)
            a_     :  Optional[int]				      =     _SMALL_OBJ * self.scale
            if instance_area < small or ya - ya < 4_0 * self.scale:
                 if ya >= self.height - 5:
                      a_     :  Optional[int]				      =     (xa, ya)
                 else:
                      a_     :  Tuple				      =     (xa, ya)

            a_     :  str				      =     (ya - ya) / np.sqrt(self.height * self.width     )
            a_     :  Any				      =     self._change_color_brightness(lowerCamelCase_   ,				brightness_factor=0.7     )
            a_     :  Dict				      =     np.clip((height_ratio - 0.0_2) / 0.0_8 + 1   ,				1.2   ,				2     )
            font_size *= 0.7_5 * self.font_size

            self.draw_text(
                text=lowerCamelCase_   ,				position=lowerCamelCase_   ,				color=lowerCamelCase_   ,				)

       def     UpperCAmelCase( self   :  str   ,				lowerCamelCase_   :  Optional[int]   ,				lowerCamelCase_   :  Any   ,				lowerCamelCase_   :  Any="g"   ,				lowerCamelCase_   :  List[str]="left"   ,				):
            a_     :  str				      =     0
            a_     :  Any				      =     self.font_size
            a_     :  List[Any]				      =     np.maximum(list(mplc.to_rgb(lowerCamelCase_     )     )   ,				0.2     )
            a_     :  Optional[Any]				      =     max(0.8   ,				np.max(lowerCamelCase_     )     )
            a_     :  int				      =     {
                """facecolor""": """black""",
                """alpha""": self.alpha,
                """pad""": self.pad,
                """edgecolor""": """none""",
            }
            a_      ,      a_     :  int				      =     position
            self.ax.text(
                lowerCamelCase_   ,				lowerCamelCase_   ,				lowerCamelCase_   ,				size=font_size * self.scale   ,				family="""sans-serif"""   ,				bbox=lowerCamelCase_   ,				verticalalignment="""top"""   ,				horizontalalignment=lowerCamelCase_   ,				color=lowerCamelCase_   ,				zorder=1_0   ,				rotation=lowerCamelCase_   ,				)

       def     UpperCAmelCase( self   :  int   ,				lowerCamelCase_   :  Tuple=None     ):
            if saveas is None:
                 a_     :  Dict				      =     self.saveas
            if saveas.lower().endswith(""".jpg"""     ) or saveas.lower().endswith(""".png"""     ):
                 cva.imwrite(
                     lowerCamelCase_   ,				self._get_buffer()[:, :, ::-1]   ,				)
            else:
                 self.fig.savefig(lowerCamelCase_     )

       def     UpperCAmelCase( self   :  str   ,				lowerCamelCase_   :  Dict   ,				lowerCamelCase_   :  Optional[int]   ,				lowerCamelCase_   :  int   ,				lowerCamelCase_   :  List[str]     ):
            a_     :  List[Any]				      =     [self.idaobj[i] for i in classes]
            a_     :  Union[str, Any]				      =     [self.idaattr[i] for i in attr_classes]
            a_     :  Optional[Any]				      =     [
                F'''{label} {score:.2f} {attr} {attr_score:.2f}'''
                for label, score, attr, attr_score in zip(lowerCamelCase_   ,				lowerCamelCase_   ,				lowerCamelCase_   ,				lowerCamelCase_     )
            ]
            return labels

       def     UpperCAmelCase( self   :  Optional[int]   ,				lowerCamelCase_   :  Union[str, Any]   ,				lowerCamelCase_   :  Union[str, Any]     ):
            a_     :  int				      =     [self.idaobj[i] for i in classes]
            if scores is not None:
                 if labels is None:
                      a_     :  Any				      =     ["""{:.0f}%""".format(s * 1_0_0     ) for s in scores]
                 else:
                      a_     :  Optional[Any]				      =     ["""{} {:.0f}%""".format(lowerCamelCase_   ,				s * 1_0_0     ) for li, s in zip(lowerCamelCase_   ,				lowerCamelCase_     )]
            return labels

       def     UpperCAmelCase( self   :  List[Any]   ,				lowerCamelCase_   :  Union[str, Any]=2_5_5     ):
            a_     :  str				      =     np.random.randint(0   ,				len(_COLORS     )     )
            a_     :  Union[str, Any]				      =     _COLORS[idx] * maximum
            if not self.rgb:
                 a_     :  Dict				      =     ret[::-1]
            return ret

       def     UpperCAmelCase( self   :  Optional[Any]     ):
            if not self.pynb:
                 a_      ,      a_     :  int				      =     self.canvas.print_to_buffer()
                 if (width, height) != (self.width, self.height):
                      a_     :  str				      =     cva.resize(self.img   ,				(width, height)     )
                 else:
                      a_     :  Union[str, Any]				      =     self.img
            else:
                 a_     :  Union[str, Any]				      =     io.BytesIO()  # works for cairo backend
                 self.canvas.print_rgba(lowerCamelCase_     )
                 a_      ,      a_     :  str				      =     self.width, self.height
                 a_     :  Any				      =     buf.getvalue()
                 a_     :  Dict				      =     self.img

            a_     :  Tuple				      =     np.frombuffer(lowerCamelCase_   ,				dtype="""uint8"""     )
            a_     :  Union[str, Any]				      =     buffer.reshape(lowerCamelCase_   ,				lowerCamelCase_   ,				4     )
            a_      ,      a_     :  List[Any]				      =     np.split(lowerCamelCase_   ,				[3]   ,				axis=2     )

            try:
                 import numexpr as ne  # fuse them with numexpr

                 a_     :  Dict				      =     ne.evaluate("""img * (1 - alpha / 255.0) + rgb * (alpha / 255.0)"""     )
            except ImportError:
                 a_     :  List[str]				      =     alpha.astype("""float32"""     ) / 2_5_5.0
                 a_     :  Union[str, Any]				      =     img * (1 - alpha) + rgb * alpha

            return visualized_image.astype("""uint8"""     )

       def     UpperCAmelCase( self   :  List[Any]   ,				lowerCamelCase_   :  Optional[int]   ,				lowerCamelCase_   :  int     ):
            assert brightness_factor >= -1.0 and brightness_factor <= 1.0
            a_     :  Optional[int]				      =     mplc.to_rgb(lowerCamelCase_     )
            a_     :  str				      =     colorsys.rgb_to_hls(*mplc.to_rgb(lowerCamelCase_     )     )
            a_     :  Union[str, Any]				      =     polygon_color[1] + (brightness_factor * polygon_color[1])
            a_     :  List[Any]				      =     0.0 if modified_lightness < 0.0 else modified_lightness
            a_     :  str				      =     1.0 if modified_lightness > 1.0 else modified_lightness
            a_     :  Dict				      =     colorsys.hls_to_rgb(polygon_color[0]   ,				lowerCamelCase_   ,				polygon_color[2]     )
            return modified_color

__lowerCamelCase   	=  (
    np.array(
        [
            0.000,
            0.447,
            0.741,
            0.850,
            0.325,
            0.098,
            0.929,
            0.694,
            0.125,
            0.494,
            0.184,
            0.556,
            0.466,
            0.674,
            0.188,
            0.301,
            0.745,
            0.933,
            0.635,
            0.078,
            0.184,
            0.300,
            0.300,
            0.300,
            0.600,
            0.600,
            0.600,
            1.000,
            0.000,
            0.000,
            1.000,
            0.500,
            0.000,
            0.749,
            0.749,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            1.000,
            0.667,
            0.000,
            1.000,
            0.333,
            0.333,
            0.000,
            0.333,
            0.667,
            0.000,
            0.333,
            1.000,
            0.000,
            0.667,
            0.333,
            0.000,
            0.667,
            0.667,
            0.000,
            0.667,
            1.000,
            0.000,
            1.000,
            0.333,
            0.000,
            1.000,
            0.667,
            0.000,
            1.000,
            1.000,
            0.000,
            0.000,
            0.333,
            0.500,
            0.000,
            0.667,
            0.500,
            0.000,
            1.000,
            0.500,
            0.333,
            0.000,
            0.500,
            0.333,
            0.333,
            0.500,
            0.333,
            0.667,
            0.500,
            0.333,
            1.000,
            0.500,
            0.667,
            0.000,
            0.500,
            0.667,
            0.333,
            0.500,
            0.667,
            0.667,
            0.500,
            0.667,
            1.000,
            0.500,
            1.000,
            0.000,
            0.500,
            1.000,
            0.333,
            0.500,
            1.000,
            0.667,
            0.500,
            1.000,
            1.000,
            0.500,
            0.000,
            0.333,
            1.000,
            0.000,
            0.667,
            1.000,
            0.000,
            1.000,
            1.000,
            0.333,
            0.000,
            1.000,
            0.333,
            0.333,
            1.000,
            0.333,
            0.667,
            1.000,
            0.333,
            1.000,
            1.000,
            0.667,
            0.000,
            1.000,
            0.667,
            0.333,
            1.000,
            0.667,
            0.667,
            1.000,
            0.667,
            1.000,
            1.000,
            1.000,
            0.000,
            1.000,
            1.000,
            0.333,
            1.000,
            1.000,
            0.667,
            1.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.167,
            0.000,
            0.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.167,
            0.000,
            0.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.143,
            0.143,
            0.143,
            0.857,
            0.857,
            0.857,
            1.000,
            1.000,
            1.000,
        ]
    )
    .astype(np.floataa)
    .reshape(-1, 3)
)







import timeit
from typing import Callable, Optional

from ..configuration_utils import PretrainedConfig
from ..models.auto.modeling_auto import MODEL_MAPPING, MODEL_WITH_LM_HEAD_MAPPING
from ..utils import is_pyanvml_available, is_torch_available, logging
from .benchmark_utils import (
    Benchmark,
    Memory,
    MemorySummary,
    measure_peak_memory_cpu,
    start_memory_tracing,
    stop_memory_tracing,
)


if is_torch_available():
       import torch

       from .benchmark_args import PyTorchBenchmarkArguments


if is_pyanvml_available():
       import pyanvml.pyanvml as nvml


__lowerCamelCase   	=  logging.get_logger(__name__)


class 	a__						(    lowerCAmelCase_     ):
       lowerCamelCase__:  PyTorchBenchmarkArguments
       lowerCamelCase__:  PretrainedConfig
       lowerCamelCase__:  str				     =  "PyTorch"

       @property
       def     UpperCAmelCase( self   :  str     ):
            return torch.__version__

       def     UpperCAmelCase( self   :  str   ,				lowerCamelCase_   :  str   ,				lowerCamelCase_   :  int   ,				lowerCamelCase_   :  int     ):
            a_     :  Tuple				      =     self._prepare_inference_func(lowerCamelCase_   ,				lowerCamelCase_   ,				lowerCamelCase_     )
            return self._measure_speed(_inference     )

       def     UpperCAmelCase( self   :  Optional[Any]   ,				lowerCamelCase_   :  str   ,				lowerCamelCase_   :  int   ,				lowerCamelCase_   :  int     ):
            a_     :  List[str]				      =     self._prepare_inference_func(lowerCamelCase_   ,				lowerCamelCase_   ,				lowerCamelCase_     )
            return self._measure_memory(_inference     )

       def     UpperCAmelCase( self   :  List[str]   ,				lowerCamelCase_   :  str   ,				lowerCamelCase_   :  int   ,				lowerCamelCase_   :  int     ):
            a_     :  str				      =     self._prepare_train_func(lowerCamelCase_   ,				lowerCamelCase_   ,				lowerCamelCase_     )
            return self._measure_speed(_train     )

       def     UpperCAmelCase( self   :  str   ,				lowerCamelCase_   :  str   ,				lowerCamelCase_   :  int   ,				lowerCamelCase_   :  int     ):
            a_     :  List[Any]				      =     self._prepare_train_func(lowerCamelCase_   ,				lowerCamelCase_   ,				lowerCamelCase_     )
            return self._measure_memory(_train     )

       def     UpperCAmelCase( self   :  Union[str, Any]   ,				lowerCamelCase_   :  str   ,				lowerCamelCase_   :  int   ,				lowerCamelCase_   :  int     ):
            a_     :  Union[str, Any]				      =     self.config_dict[model_name]

            if self.args.torchscript:
                 a_     :  Optional[Any]				      =     True

            a_     :  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_     :  Union[str, Any]				      =     config.architectures[0]
                      a_     :  Any				      =     __import__("""transformers"""   ,				fromlist=[model_class]     )
                      a_     :  List[str]				      =     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_     :  str				      =     MODEL_MAPPING[config.__class__](lowerCamelCase_     )

            model.eval()
            model.to(self.args.device     )

            # encoder-decoder has vocab size saved differently
            a_     :  Union[str, Any]				      =     config.vocab_size if hasattr(lowerCamelCase_   ,				"""vocab_size"""     ) else config.encoder.vocab_size
            a_     :  Dict				      =     torch.randint(lowerCamelCase_   ,				(batch_size, sequence_length)   ,				dtype=torch.long   ,				device=self.args.device     )

            if self.args.fpaa:
                 logger.info("""Running training in Mixed Precision..."""     )
                 if not self.args.is_gpu:
                      raise ValueError("""Mixed precision is possible only for GPU."""     )
                 # amp seems to have memory leaks so that memory usage
                 # is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
                 model.half()

            if self.args.torchscript:
                 with torch.no_grad():
                      a_     :  Optional[int]				      =     torch.jit.trace(lowerCamelCase_   ,				lowerCamelCase_     )
            else:
                 a_     :  Optional[Any]				      =     model

            def encoder_decoder_forward():
                 with torch.no_grad():
                      a_     :  Any				      =     inference_model(lowerCamelCase_   ,				decoder_input_ids=lowerCamelCase_     )
                 return outputs

            def encoder_forward():
                 with torch.no_grad():
                      a_     :  Dict				      =     inference_model(lowerCamelCase_     )
                 return outputs

            a_     :  Union[str, Any]				      =     encoder_decoder_forward if config.is_encoder_decoder else encoder_forward
            return _forward

       def     UpperCAmelCase( self   :  int   ,				lowerCamelCase_   :  str   ,				lowerCamelCase_   :  int   ,				lowerCamelCase_   :  int     ):
            a_     :  Optional[int]				      =     self.config_dict[model_name]

            a_     :  str				      =     (
                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_     :  Optional[int]				      =     config.architectures[0]
                      a_     :  List[Any]				      =     __import__("""transformers"""   ,				fromlist=[model_class]     )
                      a_     :  List[str]				      =     getattr(lowerCamelCase_   ,				lowerCamelCase_     )
                      a_     :  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_     :  Union[str, Any]				      =     MODEL_WITH_LM_HEAD_MAPPING[config.__class__](lowerCamelCase_     )

            if self.args.torchscript:
                 raise NotImplementedError("""Training for torchscript is currently not implemented"""     )
            else:
                 a_     :  Dict				      =     model

            model.train()
            model.to(self.args.device     )

            # encoder-decoder has vocab size saved differently
            a_     :  Any				      =     config.vocab_size if hasattr(lowerCamelCase_   ,				"""vocab_size"""     ) else config.encoder.vocab_size
            a_     :  Tuple				      =     torch.randint(lowerCamelCase_   ,				(batch_size, sequence_length)   ,				dtype=torch.long   ,				device=self.args.device     )

            if self.args.fpaa:
                 logger.info("""Running training in Mixed Precision..."""     )
                 if not self.args.is_gpu:
                      raise ValueError("""Mixed precision is possible only for GPU."""     )

                 # amp seems to have memory leaks so that memory usage
                 # is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
                 model.half()

            def compute_loss_and_backprob_encoder():
                 a_     :  Dict				      =     train_model(lowerCamelCase_   ,				labels=lowerCamelCase_     )[0]
                 loss.backward()
                 return loss

            def compute_loss_and_backprob_encoder_decoder():
                 a_     :  Optional[int]				      =     train_model(lowerCamelCase_   ,				decoder_input_ids=lowerCamelCase_   ,				labels=lowerCamelCase_     )[0]
                 loss.backward()
                 return loss

            a_     :  Optional[Any]				      =     (
                compute_loss_and_backprob_encoder_decoder
                if config.is_encoder_decoder
                else compute_loss_and_backprob_encoder
            )
            return _train

       def     UpperCAmelCase( self   :  str   ,				lowerCamelCase_   :  Optional[int]     ):
            try:
                 if self.args.is_tpu or self.args.torchscript:
                      # run additional 10 times to stabilize compilation for tpu and torchscript
                      logger.info("""Do inference on TPU or torchscript. 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_     :  List[str]				      =     timeit.repeat(
                     lowerCamelCase_   ,				repeat=self.args.repeat   ,				number=1_0   ,				)

                 if self.args.is_tpu and self.args.torch_xla_tpu_print_metrics:
                      import torch_xla.debug.metrics as met

                      self.print_fn(met.metrics_report()     )

                 return min(lowerCamelCase_     ) / 1_0.0
            except RuntimeError as e:
                 self.print_fn(F'''Doesn\'t fit on GPU. {e}'''     )
                 return "N/A"

       def     UpperCAmelCase( self   :  Tuple   ,				lowerCamelCase_   :  Callable[[], None]     ):
            try:
                 if self.args.trace_memory_line_by_line:
                      a_     :  Dict				      =     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"""
                          """ `--no-memory` or `args.memory=False`"""     )
                 elif self.args.is_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_     :  str				      =     """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[Any]				      =     nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx     )
                           a_     :  Tuple				      =     nvml.nvmlDeviceGetMemoryInfo(lowerCamelCase_     )
                           a_     :  Any				      =     meminfo.used
                           a_     :  Any				      =     Memory(lowerCamelCase_     )
                           # shutdown nvml
                           nvml.nvmlShutdown()
                 else:
                      # cpu
                      a_     :  Union[str, Any]				      =     measure_peak_memory_cpu(lowerCamelCase_     )
                      a_     :  Optional[Any]				      =     Memory(lowerCamelCase_     ) if isinstance(lowerCamelCase_   ,				lowerCamelCase_     ) else memory_bytes

                 if self.args.trace_memory_line_by_line:
                      a_     :  Dict				      =     stop_memory_tracing(lowerCamelCase_     )
                 else:
                      a_     :  Any				      =     None

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



