




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


SCREAMING_SNAKE_CASE_						:					List[str]   =       logging.get_logger(__name__)







class 		__UpperCAmelCase(		lowercase__ ):





     """simple docstring"""



     __UpperCAmelCase	: PyTorchBenchmarkArguments
     __UpperCAmelCase	: PretrainedConfig
     __UpperCAmelCase	: str						=    "PyTorch"

     @property
     def 				__SCREAMING_SNAKE_CASE		(      self :       str    )			->			Any:






         """simple docstring"""
         return torch.__version__

     def 				__SCREAMING_SNAKE_CASE		(      self :       Dict						,				A :       str						,				A :       int						,				A :       int    )			->			float:






         """simple docstring"""
         a__      =       self._prepare_inference_func(A						,				A						,				A    )
         return self._measure_speed(_inference    )

     def 				__SCREAMING_SNAKE_CASE		(      self :       Optional[int]						,				A :       str						,				A :       int						,				A :       int    )			->			[Memory, Optional[MemorySummary]]:






         """simple docstring"""
         a__      =       self._prepare_inference_func(A						,				A						,				A    )
         return self._measure_memory(_inference    )

     def 				__SCREAMING_SNAKE_CASE		(      self :       Any						,				A :       str						,				A :       int						,				A :       int    )			->			float:






         """simple docstring"""
         a__      =       self._prepare_train_func(A						,				A						,				A    )
         return self._measure_speed(_train    )

     def 				__SCREAMING_SNAKE_CASE		(      self :       List[Any]						,				A :       str						,				A :       int						,				A :       int    )			->			[Memory, Optional[MemorySummary]]:






         """simple docstring"""
         a__      =       self._prepare_train_func(A						,				A						,				A    )
         return self._measure_memory(_train    )

     def 				__SCREAMING_SNAKE_CASE		(      self :       List[str]						,				A :       str						,				A :       int						,				A :       int    )			->			Callable[[], None]:






         """simple docstring"""
         a__      =       self.config_dict[model_name]

         if self.args.torchscript:
             a__      =       True

         a__      =       (
             hasattr(A						,				"""architectures"""    )
             and isinstance(config.architectures						,				A    )
             and len(config.architectures    ) > 0
         )
         if not self.args.only_pretrain_model and has_model_class_in_config:
             try:
                 a__      =       config.architectures[0]
                 a__      =       __import__("""transformers"""						,				fromlist=[model_class]    )
                 a__      =       getattr(A						,				A    )
                 a__      =       model_cls(A    )
             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__      =       MODEL_MAPPING[config.__class__](A    )

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

         # encoder-decoder has vocab size saved differently
         a__      =       config.vocab_size if hasattr(A						,				"""vocab_size"""    ) else config.encoder.vocab_size
         a__      =       torch.randint(A						,				(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__      =       torch.jit.trace(A						,				A    )
         else:
             a__      =       model

         def encoder_decoder_forward():
             with torch.no_grad():
                 a__      =       inference_model(A						,				decoder_input_ids=A    )
             return outputs

         def encoder_forward():
             with torch.no_grad():
                 a__      =       inference_model(A    )
             return outputs

         a__      =       encoder_decoder_forward if config.is_encoder_decoder else encoder_forward
         return _forward

     def 				__SCREAMING_SNAKE_CASE		(      self :       List[str]						,				A :       str						,				A :       int						,				A :       int    )			->			Callable[[], None]:






         """simple docstring"""
         a__      =       self.config_dict[model_name]

         a__      =       (
             hasattr(A						,				"""architectures"""    )
             and isinstance(config.architectures						,				A    )
             and len(config.architectures    ) > 0
         )
         if not self.args.only_pretrain_model and has_model_class_in_config:
             try:
                 a__      =       config.architectures[0]
                 a__      =       __import__("""transformers"""						,				fromlist=[model_class]    )
                 a__      =       getattr(A						,				A    )
                 a__      =       model_cls(A    )
             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__      =       MODEL_WITH_LM_HEAD_MAPPING[config.__class__](A    )

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

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

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

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

         a__      =       (
             compute_loss_and_backprob_encoder_decoder
             if config.is_encoder_decoder
             else compute_loss_and_backprob_encoder
         )
         return _train

     def 				__SCREAMING_SNAKE_CASE		(      self :       str						,				A :       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(
                     A						,				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__      =       timeit.repeat(
                 A						,				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(A    ) / 10.0
         except RuntimeError as e:
             self.print_fn(f"""Doesn't fit on GPU. {e}"""    )
             return "N/A"






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






         """simple docstring"""
         try:
             if self.args.trace_memory_line_by_line:
                 a__      =       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__      =       """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__      =       nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx    )
                     a__      =       nvml.nvmlDeviceGetMemoryInfo(A    )
                     a__      =       meminfo.used
                     a__      =       Memory(A    )
                     # shutdown nvml
                     nvml.nvmlShutdown()
             else:
                 # cpu
                 a__      =       measure_peak_memory_cpu(A    )
                 a__      =       Memory(A    ) if isinstance(A						,				A    ) else memory_bytes

             if self.args.trace_memory_line_by_line:
                 a__      =       stop_memory_tracing(A    )
             else:
                 a__      =       None

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












from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
 import PIL


SCREAMING_SNAKE_CASE_						:					Any			   =       logging.get_logger(__name__)







class 		__UpperCAmelCase(		lowercase__ ):





     """simple docstring"""



     __UpperCAmelCase	: Optional[Any]						=    ["""pixel_values"""]

     def __init__(      self :       int						,				A :       bool = True						,				A :       Dict[str, int] = None						,				A :       PILImageResampling = PIL.Image.NEAREST						,				A :       bool = False						,				A :       Dict[str, int] = None						,				A :       Union[int, float] = 1 / 2_55						,				A :       bool = False						,				A :       bool = True						,				A :       bool = True						,				A :       Optional[Union[float, List[float]]] = None						,				A :       Optional[Union[float, List[float]]] = None						,				A :       bool = True						,				**A :       Optional[int]						,				)			->			None:






         """simple docstring"""
         super().__init__(**A    )
         a__      =       size if size is not None else {"""height""": 3_46, """width""": 3_46}
         a__      =       get_size_dict(A    )
         a__      =       crop_size if crop_size is not None else {"""height""": 2_89, """width""": 2_89}
         a__      =       get_size_dict(A						,				param_name="""crop_size"""    )

         a__      =       do_resize
         a__      =       size
         a__      =       resample
         a__      =       do_center_crop
         a__      =       crop_size
         a__      =       do_rescale
         a__      =       rescale_factor
         a__      =       rescale_offset
         a__      =       do_normalize
         a__      =       image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
         a__      =       image_std if image_std is not None else IMAGENET_STANDARD_STD
         a__      =       include_top

     def 				__SCREAMING_SNAKE_CASE		(      self :       Optional[Any]						,				A :       np.ndarray						,				A :       Dict[str, int]						,				A :       PILImageResampling = PIL.Image.NEAREST						,				A :       Optional[Union[str, ChannelDimension]] = None						,				**A :       Dict						,				)			->			np.ndarray:






         """simple docstring"""
         a__      =       get_size_dict(A    )
         if "height" not in size or "width" not in size:
             raise ValueError(f"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}"""    )
         return resize(
             A						,				size=(size["""height"""], size["""width"""])						,				resample=A						,				data_format=A						,				**A    )

     def 				__SCREAMING_SNAKE_CASE		(      self :       Tuple						,				A :       np.ndarray						,				A :       Dict[str, int]						,				A :       Optional[Union[str, ChannelDimension]] = None						,				**A :       Optional[Any]						,				)			->			np.ndarray:






         """simple docstring"""
         a__      =       get_size_dict(A    )
         if "height" not in size or "width" not in size:
             raise ValueError(f"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}"""    )
         return center_crop(A						,				size=(size["""height"""], size["""width"""])						,				data_format=A						,				**A    )

     def 				__SCREAMING_SNAKE_CASE		(      self :       Dict						,				A :       np.ndarray						,				A :       Union[int, float]						,				A :       bool = True						,				A :       Optional[Union[str, ChannelDimension]] = None						,				**A :       List[str]						,				)			->			Any:






         """simple docstring"""
         if offset:
             a__      =       (image - 1_2_7.5) * scale
             if data_format is not None:
                 a__      =       to_channel_dimension_format(A						,				A    )
             a__      =       rescaled_image.astype(np.floataa    )
         else:
             a__      =       rescale(A						,				scale=A						,				data_format=A						,				**A    )
         return rescaled_image

     def 				__SCREAMING_SNAKE_CASE		(      self :       Optional[int]						,				A :       np.ndarray						,				A :       Union[float, List[float]]						,				A :       Union[float, List[float]]						,				A :       Optional[Union[str, ChannelDimension]] = None						,				**A :       str						,				)			->			np.ndarray:






         """simple docstring"""
         return normalize(A						,				mean=A						,				std=A						,				data_format=A						,				**A    )






     def 				__SCREAMING_SNAKE_CASE		(      self :       Union[str, Any]						,				A :       ImageInput						,				A :       bool = None						,				A :       Dict[str, int] = None						,				A :       Any=None						,				A :       bool = None						,				A :       Dict[str, int] = None						,				A :       bool = None						,				A :       float = None						,				A :       bool = None						,				A :       bool = None						,				A :       Optional[Union[float, List[float]]] = None						,				A :       Optional[Union[float, List[float]]] = None						,				A :       bool = None						,				A :       Optional[Union[str, TensorType]] = None						,				A :       ChannelDimension = ChannelDimension.FIRST						,				**A :       Tuple						,				)			->			PIL.Image.Image:






         """simple docstring"""
         a__      =       do_resize if do_resize is not None else self.do_resize
         a__      =       resample if resample is not None else self.resample
         a__      =       do_center_crop if do_center_crop is not None else self.do_center_crop
         a__      =       do_rescale if do_rescale is not None else self.do_rescale
         a__      =       rescale_factor if rescale_factor is not None else self.rescale_factor
         a__      =       rescale_offset if rescale_offset is not None else self.rescale_offset
         a__      =       do_normalize if do_normalize is not None else self.do_normalize
         a__      =       image_mean if image_mean is not None else self.image_mean
         a__      =       image_std if image_std is not None else self.image_std
         a__      =       include_top if include_top is not None else self.include_top

         a__      =       size if size is not None else self.size
         a__      =       get_size_dict(A    )
         a__      =       crop_size if crop_size is not None else self.crop_size
         a__      =       get_size_dict(A						,				param_name="""crop_size"""    )

         a__      =       make_list_of_images(A    )

         if not valid_images(A    ):
             raise ValueError(
                 """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
                 """torch.Tensor, tf.Tensor or jax.ndarray."""    )

         if do_resize and size is None or resample is None:
             raise ValueError("""Size and resample must be specified if do_resize is True."""    )

         if do_center_crop and crop_size is None:
             raise ValueError("""Crop size must be specified if do_center_crop is True."""    )

         if do_rescale and rescale_factor is None:
             raise ValueError("""Rescale factor must be specified if do_rescale is True."""    )

         if do_normalize and (image_mean is None or image_std is None):
             raise ValueError("""Image mean and std must be specified if do_normalize is True."""    )

         # All transformations expect numpy arrays.
         a__      =       [to_numpy_array(A    ) for image in images]

         if do_resize:
             a__      =       [self.resize(image=A						,				size=A						,				resample=A    ) for image in images]

         if do_center_crop:
             a__      =       [self.center_crop(image=A						,				size=A    ) for image in images]

         if do_rescale:
             a__      =       [self.rescale(image=A						,				scale=A						,				offset=A    ) for image in images]

         if do_normalize:
             a__      =       [self.normalize(image=A						,				mean=A						,				std=A    ) for image in images]

         if include_top:
             a__      =       [self.normalize(image=A						,				mean=[0, 0, 0]						,				std=A    ) for image in images]

         a__      =       [to_channel_dimension_format(A						,				A    ) for image in images]

         a__      =       {"""pixel_values""": images}
         return BatchFeature(data=A						,				tensor_type=A    )






