code: 
  
  
  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
  
  
  __snake_case             =	logging.get_logger(__name__)
  
  
  
  
  
  class   __lowerCamelCase (__UpperCAmelCase	):
         _lowercase       							=    42
         _lowercase       							=    42
         _lowercase       							=    "PyTorch"
  
  
  
  
  
  
  
         @property
         def 		snake_case_  ( self:   Optional[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              return torch.__version__
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   int,A_:   Dict,A_:   Optional[int],A_:   List[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self._prepare_inference_func(lowerCamelCase__,lowerCamelCase__,lowerCamelCase__				)
              return self._measure_speed(_inference				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Optional[int],A_:   Optional[Any],A_:   List[Any],A_:   Any				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self._prepare_inference_func(lowerCamelCase__,lowerCamelCase__,lowerCamelCase__				)
              return self._measure_memory(_inference				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Any,A_:   Union[str, Any],A_:   Union[str, Any],A_:   int				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self._prepare_train_func(lowerCamelCase__,lowerCamelCase__,lowerCamelCase__				)
              return self._measure_speed(_train				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Dict,A_:   Optional[Any],A_:   Optional[int],A_:   List[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self._prepare_train_func(lowerCamelCase__,lowerCamelCase__,lowerCamelCase__				)
              return self._measure_memory(_train				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   int,A_:   Optional[int],A_:   Any,A_:   Union[str, Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.config_dict[model_name]
  
              if self.args.torchscript:
                   __UpperCamelCase   = True
  
              __UpperCamelCase   = (
                  hasattr(lowerCamelCase__,'architectures'				)
                  and isinstance(config.architectures,lowerCamelCase__				)
                  and len(config.architectures				) > 0
              )
              if not self.args.only_pretrain_model and has_model_class_in_config:
                   try:
                        __UpperCamelCase   = config.architectures[0]
                        __UpperCamelCase   = __import__('transformers',fromlist=[model_class]				)
                        __UpperCamelCase   = getattr(lowerCamelCase__,lowerCamelCase__				)
                        __UpperCamelCase   = model_cls(lowerCamelCase__				)
                   except ImportError:
                        raise ImportError(
                            F'''{model_class} does not exist. If you just want to test the pretrained model, you might want to'''
                            ' set `--only_pretrain_model` or `args.only_pretrain_model=True`.'				)
              else:
                   __UpperCamelCase   = MODEL_MAPPING[config.__class__](lowerCamelCase__				)
  
              model.eval()
              model.to(self.args.device				)
  
              # encoder-decoder has vocab size saved differently
              __UpperCamelCase   = config.vocab_size if hasattr(lowerCamelCase__,'vocab_size'				) else config.encoder.vocab_size
              __UpperCamelCase   = 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():
                        __UpperCamelCase   = torch.jit.trace(lowerCamelCase__,lowerCamelCase__				)
              else:
                   __UpperCamelCase   = model
  
              def encoder_decoder_forward():
                   with torch.no_grad():
                        __UpperCamelCase   = inference_model(lowerCamelCase__,decoder_input_ids=lowerCamelCase__				)
                   return outputs
  
              def encoder_forward():
                   with torch.no_grad():
                        __UpperCamelCase   = inference_model(lowerCamelCase__				)
                   return outputs
  
              __UpperCamelCase   = encoder_decoder_forward if config.is_encoder_decoder else encoder_forward
              return _forward
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Any,A_:   Tuple,A_:   Optional[Any],A_:   Tuple				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.config_dict[model_name]
  
              __UpperCamelCase   = (
                  hasattr(lowerCamelCase__,'architectures'				)
                  and isinstance(config.architectures,lowerCamelCase__				)
                  and len(config.architectures				) > 0
              )
              if not self.args.only_pretrain_model and has_model_class_in_config:
                   try:
                        __UpperCamelCase   = config.architectures[0]
                        __UpperCamelCase   = __import__('transformers',fromlist=[model_class]				)
                        __UpperCamelCase   = getattr(lowerCamelCase__,lowerCamelCase__				)
                        __UpperCamelCase   = model_cls(lowerCamelCase__				)
                   except ImportError:
                        raise ImportError(
                            F'''{model_class} does not exist. If you just want to test the pretrained model, you might want to'''
                            ' set `--only_pretrain_model` or `args.only_pretrain_model=True`.'				)
              else:
                   __UpperCamelCase   = MODEL_WITH_LM_HEAD_MAPPING[config.__class__](lowerCamelCase__				)
  
              if self.args.torchscript:
                   raise NotImplementedError('Training for torchscript is currently not implemented'				)
              else:
                   __UpperCamelCase   = model
  
              model.train()
              model.to(self.args.device				)
  
              # encoder-decoder has vocab size saved differently
              __UpperCamelCase   = config.vocab_size if hasattr(lowerCamelCase__,'vocab_size'				) else config.encoder.vocab_size
              __UpperCamelCase   = 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():
                   __UpperCamelCase   = train_model(lowerCamelCase__,labels=lowerCamelCase__				)[0]
                   loss.backward()
                   return loss
  
              def compute_loss_and_backprob_encoder_decoder():
                   __UpperCamelCase   = train_model(lowerCamelCase__,decoder_input_ids=lowerCamelCase__,labels=lowerCamelCase__				)[0]
                   loss.backward()
                   return loss
  
              __UpperCamelCase   = (
                  compute_loss_and_backprob_encoder_decoder
                  if config.is_encoder_decoder
                  else compute_loss_and_backprob_encoder
              )
              return _train
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Any,A_:   List[str]				):
  
  
  
  
  
  
              '''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(
                            lowerCamelCase__,repeat=1,number=5,)
  
                   # as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average
                   __UpperCamelCase   = timeit.repeat(
                       lowerCamelCase__,repeat=self.args.repeat,number=10,)
  
                   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 		snake_case_  ( self:   Union[str, Any],A_:   Any				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              try:
                   if self.args.trace_memory_line_by_line:
                        __UpperCamelCase   = start_memory_tracing('transformers'				)
  
                   if self.args.is_tpu:
                        # tpu
                        raise NotImplementedError(
                            'Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking with'
                            ' `--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.'				)
                             __UpperCamelCase   = 'N/A'
                        else:
                             logger.info(
                                 'Measuring total GPU usage on GPU device. Make sure to not have additional processes running'
                                 ' on the same GPU.'				)
                             # init nvml
                             nvml.nvmlInit()
                             func()
                             __UpperCamelCase   = nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx				)
                             __UpperCamelCase   = nvml.nvmlDeviceGetMemoryInfo(lowerCamelCase__				)
                             __UpperCamelCase   = meminfo.used
                             __UpperCamelCase   = Memory(lowerCamelCase__				)
                             # shutdown nvml
                             nvml.nvmlShutdown()
                   else:
                        # cpu
                        __UpperCamelCase   = measure_peak_memory_cpu(lowerCamelCase__				)
                        __UpperCamelCase   = Memory(lowerCamelCase__				) if isinstance(lowerCamelCase__,lowerCamelCase__				) else memory_bytes
  
                   if self.args.trace_memory_line_by_line:
                        __UpperCamelCase   = stop_memory_tracing(lowerCamelCase__				)
                   else:
                        __UpperCamelCase   = None
  
                   return memory, summary
              except RuntimeError as e:
                   self.print_fn(F'''Doesn\'t fit on GPU. {e}'''				)
                   return "N/A", None
  
  
  
style_context: 
  
  
  import unittest
  
  import numpy as np
  
  from transformers.testing_utils import require_torch, require_vision
  from transformers.utils import is_torch_available, is_vision_available
  
  from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs
  
  
  if is_torch_available():
      import torch
  
  if is_vision_available():
      from PIL import Image
  
      from transformers import DeiTImageProcessor
  
  
  
  class   __lowerCamelCase (unittest.TestCase	):
  
  
  
  
  
  
  
         def __init__( self:   List[str],A_:   List[Any],A_:   List[Any]=7,A_:   str=3,A_:   Tuple=18,A_:   Union[str, Any]=30,A_:   Union[str, Any]=400,A_:   List[str]=True,A_:   List[Any]=None,A_:   Tuple=True,A_:   Union[str, Any]=None,A_:   int=True,A_:   Any=[0.5, 0.5, 0.5],A_:   Optional[Any]=[0.5, 0.5, 0.5],):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = size if size is not None else {'height': 20, 'width': 20}
              __UpperCamelCase   = crop_size if crop_size is not None else {'height': 18, 'width': 18}
  
              __UpperCamelCase   = parent
              __UpperCamelCase   = batch_size
              __UpperCamelCase   = num_channels
              __UpperCamelCase   = image_size
              __UpperCamelCase   = min_resolution
              __UpperCamelCase   = max_resolution
              __UpperCamelCase   = do_resize
              __UpperCamelCase   = size
              __UpperCamelCase   = do_center_crop
              __UpperCamelCase   = crop_size
              __UpperCamelCase   = do_normalize
              __UpperCamelCase   = image_mean
              __UpperCamelCase   = image_std
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   List[str]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              return {
                  "do_resize": self.do_resize,
                  "size": self.size,
                  "do_center_crop": self.do_center_crop,
                  "crop_size": self.crop_size,
                  "do_normalize": self.do_normalize,
                  "image_mean": self.image_mean,
                  "image_std": self.image_std,
              }
  
  
  
  @require_torch
  @require_vision
  class   __lowerCamelCase (_a							,			unittest.TestCase	):
         _lowercase       							=    DeiTImageProcessor if is_vision_available() else None
         _lowercase       							=    True
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   List[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = DeiTImageProcessingTester(self				)
  
  
  
  
  
  
  
         @property
         def 		snake_case_  ( self:   Optional[int]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              return self.image_processor_tester.prepare_image_processor_dict()
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Union[str, Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.image_processing_class(**self.image_processor_dict				)
              self.assertTrue(hasattr(A_,'do_resize'				)				)
              self.assertTrue(hasattr(A_,'size'				)				)
              self.assertTrue(hasattr(A_,'do_center_crop'				)				)
              self.assertTrue(hasattr(A_,'center_crop'				)				)
              self.assertTrue(hasattr(A_,'do_normalize'				)				)
              self.assertTrue(hasattr(A_,'image_mean'				)				)
              self.assertTrue(hasattr(A_,'image_std'				)				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Optional[int]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.image_processing_class.from_dict(self.image_processor_dict				)
              self.assertEqual(image_processor.size,{'height': 20, 'width': 20}				)
              self.assertEqual(image_processor.crop_size,{'height': 18, 'width': 18}				)
  
              __UpperCamelCase   = self.image_processing_class.from_dict(self.image_processor_dict,size=42,crop_size=84				)
              self.assertEqual(image_processor.size,{'height': 42, 'width': 42}				)
              self.assertEqual(image_processor.crop_size,{'height': 84, 'width': 84}				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Tuple				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              pass
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Optional[int]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.image_processing_class(**self.image_processor_dict				)
              # create random PIL images
              __UpperCamelCase   = prepare_image_inputs(self.image_processor_tester,equal_resolution=A_				)
              for image in image_inputs:
                   self.assertIsInstance(A_,Image.Image				)
  
              # Test not batched input
              __UpperCamelCase   = image_processing(image_inputs[0],return_tensors='pt'				).pixel_values
              self.assertEqual(
                  encoded_images.shape,(
                      1,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size['height'],
                      self.image_processor_tester.crop_size['width'],
                  ),)
  
              # Test batched
              __UpperCamelCase   = image_processing(A_,return_tensors='pt'				).pixel_values
              self.assertEqual(
                  encoded_images.shape,(
                      self.image_processor_tester.batch_size,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size['height'],
                      self.image_processor_tester.crop_size['width'],
                  ),)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   List[str]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.image_processing_class(**self.image_processor_dict				)
              # create random numpy tensors
              __UpperCamelCase   = prepare_image_inputs(self.image_processor_tester,equal_resolution=A_,numpify=A_				)
              for image in image_inputs:
                   self.assertIsInstance(A_,np.ndarray				)
  
              # Test not batched input
              __UpperCamelCase   = image_processing(image_inputs[0],return_tensors='pt'				).pixel_values
              self.assertEqual(
                  encoded_images.shape,(
                      1,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size['height'],
                      self.image_processor_tester.crop_size['width'],
                  ),)
  
              # Test batched
              __UpperCamelCase   = image_processing(A_,return_tensors='pt'				).pixel_values
              self.assertEqual(
                  encoded_images.shape,(
                      self.image_processor_tester.batch_size,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size['height'],
                      self.image_processor_tester.crop_size['width'],
                  ),)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Any				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.image_processing_class(**self.image_processor_dict				)
              # create random PyTorch tensors
              __UpperCamelCase   = prepare_image_inputs(self.image_processor_tester,equal_resolution=A_,torchify=A_				)
              for image in image_inputs:
                   self.assertIsInstance(A_,torch.Tensor				)
  
              # Test not batched input
              __UpperCamelCase   = image_processing(image_inputs[0],return_tensors='pt'				).pixel_values
              self.assertEqual(
                  encoded_images.shape,(
                      1,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size['height'],
                      self.image_processor_tester.crop_size['width'],
                  ),)
  
              # Test batched
              __UpperCamelCase   = image_processing(A_,return_tensors='pt'				).pixel_values
              self.assertEqual(
                  encoded_images.shape,(
                      self.image_processor_tester.batch_size,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size['height'],
                      self.image_processor_tester.crop_size['width'],
                  ),)
  
  
  