




'''simple docstring'''


import warnings
from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from PIL import Image

from .configuration_utils import ConfigMixin, register_to_config
from .utils import CONFIG_NAME, PIL_INTERPOLATION, deprecate






class 						lowerCAmelCase_	(       UpperCAmelCase_							):







  '''simple docstring'''

  UpperCamelCase_			:      List[str]										=				CONFIG_NAME



  @register_to_config
  def __init__( self				:							Any					,  SCREAMING_SNAKE_CASE_				:							bool = True					,  SCREAMING_SNAKE_CASE_				:							int = 8					,  SCREAMING_SNAKE_CASE_				:							str = "lanczos"					,  SCREAMING_SNAKE_CASE_				:							bool = True					,  SCREAMING_SNAKE_CASE_				:							bool = False					,  )			->		Optional[int]:







       '''simple docstring'''



       super().__init__()



  @staticmethod
  def 					_snake_case     ( SCREAMING_SNAKE_CASE_				:							np.ndarray	)			->		PIL.Image.Image:







       '''simple docstring'''



       if images.ndim == 3:
            A:		Union[str, Any]          =		images[None, ...]
       A:		str          =		(images * 2_55).round().astype('''uint8'''	)
       if images.shape[-1] == 1:
            # special case for grayscale (single channel) images
            A:		Tuple          =		[Image.fromarray(image.squeeze()					,  mode='''L'''	) for image in images]
       else:
            A:		List[Any]          =		[Image.fromarray(SCREAMING_SNAKE_CASE_	) for image in images]

       return pil_images



  @staticmethod
  def 					_snake_case     ( SCREAMING_SNAKE_CASE_				:							Union[List[PIL.Image.Image], PIL.Image.Image]	)			->		np.ndarray:







       '''simple docstring'''



       if not isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	):
            A:		Optional[Any]          =		[images]
       A:		Tuple          =		[np.array(SCREAMING_SNAKE_CASE_	).astype(np.floataa	) / 255.0 for image in images]
       A:		Tuple          =		np.stack(SCREAMING_SNAKE_CASE_					,  axis=0	)

       return images



  @staticmethod
  def 					_snake_case     ( SCREAMING_SNAKE_CASE_				:							np.ndarray	)			->		torch.FloatTensor:







       '''simple docstring'''



       if images.ndim == 3:
            A:		Optional[Any]          =		images[..., None]

       A:		Any          =		torch.from_numpy(images.transpose(0					,  3					,  1					,  2	)	)
       return images



  @staticmethod
  def 					_snake_case     ( SCREAMING_SNAKE_CASE_				:							torch.FloatTensor	)			->		np.ndarray:







       '''simple docstring'''



       A:		Optional[int]          =		images.cpu().permute(0					,  2					,  3					,  1	).float().numpy()
       return images



  @staticmethod
  def 					_snake_case     ( SCREAMING_SNAKE_CASE_				:							str	)			->		Optional[Any]:







       '''simple docstring'''



       return 2.0 * images - 1.0



  @staticmethod
  def 					_snake_case     ( SCREAMING_SNAKE_CASE_				:							List[Any]	)			->		Tuple:







       '''simple docstring'''



       return (images / 2 + 0.5).clamp(0					,  1	)



  @staticmethod
  def 					_snake_case     ( SCREAMING_SNAKE_CASE_				:							PIL.Image.Image	)			->		PIL.Image.Image:







       '''simple docstring'''



       A:		str          =		image.convert('''RGB'''	)
       return image



  def 					_snake_case     ( self				:							List[Any]					,  SCREAMING_SNAKE_CASE_				:							PIL.Image.Image					,  SCREAMING_SNAKE_CASE_				:							Optional[int] = None					,  SCREAMING_SNAKE_CASE_				:							Optional[int] = None					,  )			->		PIL.Image.Image:







       '''simple docstring'''



       if height is None:
            A:		Optional[int]          =		image.height
       if width is None:
            A:		Union[str, Any]          =		image.width

       A     ,    A:		int          =		(
           x - x % self.config.vae_scale_factor for x in (width, height)
       )  # resize to integer multiple of vae_scale_factor
       A:		Union[str, Any]          =		image.resize((width, height)					,  resample=PIL_INTERPOLATION[self.config.resample]	)
       return image



  def 					_snake_case     ( self				:							Any					,  SCREAMING_SNAKE_CASE_				:							Union[torch.FloatTensor, PIL.Image.Image, np.ndarray]					,  SCREAMING_SNAKE_CASE_				:							Optional[int] = None					,  SCREAMING_SNAKE_CASE_				:							Optional[int] = None					,  )			->		torch.Tensor:







       '''simple docstring'''



       A:		Union[str, Any]          =		(PIL.Image.Image, np.ndarray, torch.Tensor)
       if isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	):
            A:		Union[str, Any]          =		[image]
       elif not (isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	) and all(isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	) for i in image	)):
            raise ValueError(
                f"""Input is in incorrect format: {[type(SCREAMING_SNAKE_CASE_	) for i in image]}. Currently, we only support {', '.join(SCREAMING_SNAKE_CASE_	)}"""	)

       if isinstance(image[0]					,  PIL.Image.Image	):
            if self.config.do_convert_rgb:
                 A:		str          =		[self.convert_to_rgb(SCREAMING_SNAKE_CASE_	) for i in image]
            if self.config.do_resize:
                 A:		str          =		[self.resize(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	) for i in image]
            A:		str          =		self.pil_to_numpy(SCREAMING_SNAKE_CASE_	)  # to np
            A:		List[Any]          =		self.numpy_to_pt(SCREAMING_SNAKE_CASE_	)  # to pt

       elif isinstance(image[0]					,  np.ndarray	):
            A:		int          =		np.concatenate(SCREAMING_SNAKE_CASE_					,  axis=0	) if image[0].ndim == 4 else np.stack(SCREAMING_SNAKE_CASE_					,  axis=0	)
            A:		Optional[Any]          =		self.numpy_to_pt(SCREAMING_SNAKE_CASE_	)
            A     ,    A     ,    A     ,    A:		Dict          =		image.shape
            if self.config.do_resize and (
                height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0
            ):
                 raise ValueError(
                     f"""Currently we only support resizing for PIL image - please resize your numpy array to be divisible by {self.config.vae_scale_factor}"""
                     f"""currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor"""	)

       elif isinstance(image[0]					,  torch.Tensor	):
            A:		List[str]          =		torch.cat(SCREAMING_SNAKE_CASE_					,  axis=0	) if image[0].ndim == 4 else torch.stack(SCREAMING_SNAKE_CASE_					,  axis=0	)
            A     ,    A     ,    A     ,    A:		int          =		image.shape

            # don't need any preprocess if the image is latents
            if channel == 4:
                 return image

            if self.config.do_resize and (
                height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0
            ):
                 raise ValueError(
                     f"""Currently we only support resizing for PIL image - please resize your pytorch tensor to be divisible by {self.config.vae_scale_factor}"""
                     f"""currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor"""	)

        # expected range [0,1], normalize to [-1,1]
       A:		str          =		self.config.do_normalize
       if image.min() < 0:
            warnings.warn(
                '''Passing `image` as torch tensor with value range in [-1,1] is deprecated. The expected value range for image tensor is [0,1] '''
                f"""when passing as pytorch tensor or numpy Array. You passed `image` with value range [{image.min()},{image.max()}]"""					,  SCREAMING_SNAKE_CASE_					,  )
            A:		int          =		False

       if do_normalize:
            A:		int          =		self.normalize(SCREAMING_SNAKE_CASE_	)

       return image




  def 					_snake_case     ( self				:							List[Any]					,  SCREAMING_SNAKE_CASE_				:							torch.FloatTensor					,  SCREAMING_SNAKE_CASE_				:							str = "pil"					,  SCREAMING_SNAKE_CASE_				:							Optional[List[bool]] = None					,  )			->		Tuple:







       '''simple docstring'''



       if not isinstance(SCREAMING_SNAKE_CASE_					,  torch.Tensor	):
            raise ValueError(
                f"""Input for postprocessing is in incorrect format: {type(SCREAMING_SNAKE_CASE_	)}. We only support pytorch tensor"""	)
       if output_type not in ["latent", "pt", "np", "pil"]:
            A:		Optional[Any]          =		(
                f"""the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: """
                '''`pil`, `np`, `pt`, `latent`'''
            )
            deprecate('''Unsupported output_type'''					,  '''1.0.0'''					,  SCREAMING_SNAKE_CASE_					,  standard_warn=SCREAMING_SNAKE_CASE_	)
            A:		List[Any]          =		'''np'''

       if output_type == "latent":
            return image

       if do_denormalize is None:
            A:		Union[str, Any]          =		[self.config.do_normalize] * image.shape[0]

       A:		Dict          =		torch.stack(
           [self.denormalize(image[i]	) if do_denormalize[i] else image[i] for i in range(image.shape[0]	)]	)

       if output_type == "pt":
            return image

       A:		Tuple          =		self.pt_to_numpy(SCREAMING_SNAKE_CASE_	)

       if output_type == "np":
            return image

       if output_type == "pil":
            return self.numpy_to_pil(SCREAMING_SNAKE_CASE_	)






class 						lowerCAmelCase_	(       UpperCAmelCase_							):







  '''simple docstring'''

  UpperCamelCase_			:      List[str]										=				CONFIG_NAME



  @register_to_config
  def __init__( self				:							List[str]					,  SCREAMING_SNAKE_CASE_				:							bool = True					,  SCREAMING_SNAKE_CASE_				:							int = 8					,  SCREAMING_SNAKE_CASE_				:							str = "lanczos"					,  SCREAMING_SNAKE_CASE_				:							bool = True					,  )			->		int:







       '''simple docstring'''



       super().__init__()



  @staticmethod
  def 					_snake_case     ( SCREAMING_SNAKE_CASE_				:							Any	)			->		List[str]:







       '''simple docstring'''



       if images.ndim == 3:
            A:		str          =		images[None, ...]
       A:		int          =		(images * 2_55).round().astype('''uint8'''	)
       if images.shape[-1] == 1:
            # special case for grayscale (single channel) images
            A:		Any          =		[Image.fromarray(image.squeeze()					,  mode='''L'''	) for image in images]
       else:
            A:		List[Any]          =		[Image.fromarray(image[:, :, :3]	) for image in images]

       return pil_images



  @staticmethod
  def 					_snake_case     ( SCREAMING_SNAKE_CASE_				:							int	)			->		Tuple:







       '''simple docstring'''



       return image[:, :, 1] * 2**8 + image[:, :, 2]



  def 					_snake_case     ( self				:							int					,  SCREAMING_SNAKE_CASE_				:							Tuple	)			->		Union[str, Any]:







       '''simple docstring'''



       if images.ndim == 3:
            A:		Optional[Any]          =		images[None, ...]
       A:		Any          =		images[:, :, :, 3:]
       if images.shape[-1] == 6:
            A:		Any          =		(images_depth * 2_55).round().astype('''uint8'''	)
            A:		str          =		[
                Image.fromarray(self.rgblike_to_depthmap(SCREAMING_SNAKE_CASE_	)					,  mode='''I;16'''	) for image_depth in images_depth
            ]
       elif images.shape[-1] == 4:
            A:		str          =		(images_depth * 6_5535.0).astype(np.uintaa	)
            A:		Optional[Any]          =		[Image.fromarray(SCREAMING_SNAKE_CASE_					,  mode='''I;16'''	) for image_depth in images_depth]
       else:
            raise Exception('''Not supported'''	)

       return pil_images




  def 					_snake_case     ( self				:							str					,  SCREAMING_SNAKE_CASE_				:							torch.FloatTensor					,  SCREAMING_SNAKE_CASE_				:							str = "pil"					,  SCREAMING_SNAKE_CASE_				:							Optional[List[bool]] = None					,  )			->		List[Any]:







       '''simple docstring'''



       if not isinstance(SCREAMING_SNAKE_CASE_					,  torch.Tensor	):
            raise ValueError(
                f"""Input for postprocessing is in incorrect format: {type(SCREAMING_SNAKE_CASE_	)}. We only support pytorch tensor"""	)
       if output_type not in ["latent", "pt", "np", "pil"]:
            A:		List[Any]          =		(
                f"""the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: """
                '''`pil`, `np`, `pt`, `latent`'''
            )
            deprecate('''Unsupported output_type'''					,  '''1.0.0'''					,  SCREAMING_SNAKE_CASE_					,  standard_warn=SCREAMING_SNAKE_CASE_	)
            A:		Union[str, Any]          =		'''np'''

       if do_denormalize is None:
            A:		Tuple          =		[self.config.do_normalize] * image.shape[0]

       A:		Any          =		torch.stack(
           [self.denormalize(image[i]	) if do_denormalize[i] else image[i] for i in range(image.shape[0]	)]	)

       A:		Tuple          =		self.pt_to_numpy(SCREAMING_SNAKE_CASE_	)

       if output_type == "np":
            if image.shape[-1] == 6:
                 A:		Optional[int]          =		np.stack([self.rgblike_to_depthmap(im[:, :, 3:]	) for im in image]					,  axis=0	)
            else:
                 A:		Optional[int]          =		image[:, :, :, 3:]
            return image[:, :, :, :3], image_depth

       if output_type == "pil":
            return self.numpy_to_pil(SCREAMING_SNAKE_CASE_	), self.numpy_to_depth(SCREAMING_SNAKE_CASE_	)
       else:
            raise Exception(f"""This type {output_type} is not supported"""	)












'''simple docstring'''


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


UpperCamelCase			     =					logging.get_logger(__name__)






class 						lowerCAmelCase_	(       UpperCAmelCase_							):







  '''simple docstring'''

  UpperCamelCase_			:      PyTorchBenchmarkArguments
  UpperCamelCase_			:      PretrainedConfig
  UpperCamelCase_			:      str										=				"PyTorch"



  @property
  def 					_snake_case     ( self				:							int	)			->		List[str]:







       '''simple docstring'''



       return torch.__version__



  def 					_snake_case     ( self				:							Dict					,  SCREAMING_SNAKE_CASE_				:							str					,  SCREAMING_SNAKE_CASE_				:							int					,  SCREAMING_SNAKE_CASE_				:							int	)			->		float:







       '''simple docstring'''



       A:		List[str]          =		self._prepare_inference_func(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	)
       return self._measure_speed(_inference	)



  def 					_snake_case     ( self				:							Dict					,  SCREAMING_SNAKE_CASE_				:							str					,  SCREAMING_SNAKE_CASE_				:							int					,  SCREAMING_SNAKE_CASE_				:							int	)			->		[Memory, Optional[MemorySummary]]:







       '''simple docstring'''



       A:		Any          =		self._prepare_inference_func(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	)
       return self._measure_memory(_inference	)



  def 					_snake_case     ( self				:							List[str]					,  SCREAMING_SNAKE_CASE_				:							str					,  SCREAMING_SNAKE_CASE_				:							int					,  SCREAMING_SNAKE_CASE_				:							int	)			->		float:







       '''simple docstring'''



       A:		List[Any]          =		self._prepare_train_func(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	)
       return self._measure_speed(_train	)



  def 					_snake_case     ( self				:							int					,  SCREAMING_SNAKE_CASE_				:							str					,  SCREAMING_SNAKE_CASE_				:							int					,  SCREAMING_SNAKE_CASE_				:							int	)			->		[Memory, Optional[MemorySummary]]:







       '''simple docstring'''



       A:		List[Any]          =		self._prepare_train_func(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	)
       return self._measure_memory(_train	)



  def 					_snake_case     ( self				:							Union[str, Any]					,  SCREAMING_SNAKE_CASE_				:							str					,  SCREAMING_SNAKE_CASE_				:							int					,  SCREAMING_SNAKE_CASE_				:							int	)			->		Callable[[], None]:







       '''simple docstring'''



       A:		int          =		self.config_dict[model_name]

       if self.args.torchscript:
            A:		int          =		True

       A:		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:
                 A:		Optional[int]          =		config.architectures[0]
                 A:		Optional[int]          =		__import__('''transformers'''					,  fromlist=[model_class]	)
                 A:		Dict          =		getattr(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	)
                 A:		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:
            A:		Union[str, Any]          =		MODEL_MAPPING[config.__class__](SCREAMING_SNAKE_CASE_	)

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

       # encoder-decoder has vocab size saved differently
       A:		Tuple          =		config.vocab_size if hasattr(SCREAMING_SNAKE_CASE_					,  '''vocab_size'''	) else config.encoder.vocab_size
       A:		List[Any]          =		torch.randint(SCREAMING_SNAKE_CASE_					,  (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:		Any          =		torch.jit.trace(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	)
       else:
            A:		Dict          =		model

       def encoder_decoder_forward():
            with torch.no_grad():
                 A:		List[str]          =		inference_model(SCREAMING_SNAKE_CASE_					,  decoder_input_ids=SCREAMING_SNAKE_CASE_	)
            return outputs

       def encoder_forward():
            with torch.no_grad():
                 A:		Union[str, Any]          =		inference_model(SCREAMING_SNAKE_CASE_	)
            return outputs

       A:		List[Any]          =		encoder_decoder_forward if config.is_encoder_decoder else encoder_forward
       return _forward



  def 					_snake_case     ( self				:							Tuple					,  SCREAMING_SNAKE_CASE_				:							str					,  SCREAMING_SNAKE_CASE_				:							int					,  SCREAMING_SNAKE_CASE_				:							int	)			->		Callable[[], None]:







       '''simple docstring'''



       A:		int          =		self.config_dict[model_name]

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

       if self.args.torchscript:
            raise NotImplementedError('''Training for torchscript is currently not implemented'''	)
       else:
            A:		str          =		model

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

       # encoder-decoder has vocab size saved differently
       A:		Tuple          =		config.vocab_size if hasattr(SCREAMING_SNAKE_CASE_					,  '''vocab_size'''	) else config.encoder.vocab_size
       A:		str          =		torch.randint(SCREAMING_SNAKE_CASE_					,  (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:		str          =		train_model(SCREAMING_SNAKE_CASE_					,  labels=SCREAMING_SNAKE_CASE_	)[0]
            loss.backward()
            return loss

       def compute_loss_and_backprob_encoder_decoder():
            A:		Dict          =		train_model(SCREAMING_SNAKE_CASE_					,  decoder_input_ids=SCREAMING_SNAKE_CASE_					,  labels=SCREAMING_SNAKE_CASE_	)[0]
            loss.backward()
            return loss

       A:		str          =		(
           compute_loss_and_backprob_encoder_decoder
           if config.is_encoder_decoder
           else compute_loss_and_backprob_encoder
       )
       return _train



  def 					_snake_case     ( self				:							Any					,  SCREAMING_SNAKE_CASE_				:							Any	)			->		float:







       '''simple docstring'''



       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(
                     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
            A:		Any          =		timeit.repeat(
                SCREAMING_SNAKE_CASE_					,  repeat=self.args.repeat					,  number=10					,  )

            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(SCREAMING_SNAKE_CASE_	) / 10.0
       except RuntimeError as e:
            self.print_fn(f"""Doesn't fit on GPU. {e}"""	)
            return "N/A"




  def 					_snake_case     ( self				:							Optional[int]					,  SCREAMING_SNAKE_CASE_				:							Callable[[], None]	)			->		[Memory, MemorySummary]:







       '''simple docstring'''



       try:
            if self.args.trace_memory_line_by_line:
                 A:		List[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'''
                     ''' `--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:		Optional[int]          =		'''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:		Union[str, Any]          =		nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx	)
                      A:		Union[str, Any]          =		nvml.nvmlDeviceGetMemoryInfo(SCREAMING_SNAKE_CASE_	)
                      A:		Tuple          =		meminfo.used
                      A:		Tuple          =		Memory(SCREAMING_SNAKE_CASE_	)
                      # shutdown nvml
                      nvml.nvmlShutdown()
            else:
                 # cpu
                 A:		int          =		measure_peak_memory_cpu(SCREAMING_SNAKE_CASE_	)
                 A:		str          =		Memory(SCREAMING_SNAKE_CASE_	) if isinstance(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	) else memory_bytes

            if self.args.trace_memory_line_by_line:
                 A:		Dict          =		stop_memory_tracing(SCREAMING_SNAKE_CASE_	)
            else:
                 A:		List[str]          =		None

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






