



'''simple docstring'''






import dataclasses
import json
import warnings
from dataclasses import dataclass, field
from time import time
from typing import List

from ..utils import logging


lowerCAmelCase:      Dict		 =		logging.get_logger(__name__)
def    lowerCamelCase__ (		_A=None				,    _A=None       ):
  return field(default_factory=lambda: default				,    metadata=_A       )

@dataclass
class 							a__:
    lowercase__				=					list_field(
        default=[]	,				metadata={
            """help""": (
                """Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version"""
                """ of all available models"""
            )
        }	,				)

    lowercase__				=					list_field(
        default=[8]	,				metadata={"""help""": """List of batch sizes for which memory and time performance will be evaluated"""}			)

    lowercase__				=					list_field(
        default=[8, 32, 1_28, 5_12]	,				metadata={"""help""": """List of sequence lengths for which memory and time performance will be evaluated"""}	,				)

    lowercase__				=					field(
        default=lowerCamelCase__	,				metadata={"""help""": """Whether to benchmark inference of model. Inference can be disabled via --no-inference."""}	,				)
    lowercase__				=					field(
        default=lowerCamelCase__	,				metadata={"""help""": """Whether to run on available cuda devices. Cuda can be disabled via --no-cuda."""}	,				)
    lowercase__				=					field(
        default=lowerCamelCase__	,				metadata={"""help""": """Whether to run on available tpu devices. TPU can be disabled via --no-tpu."""}			)
    lowercase__				=					field(default=lowerCamelCase__	,				metadata={"""help""": """Use FP16 to accelerate inference."""}			)
    lowercase__				=					field(default=lowerCamelCase__	,				metadata={"""help""": """Benchmark training of model"""}			)
    lowercase__				=					field(default=lowerCamelCase__	,				metadata={"""help""": """Verbose memory tracing"""}			)
    lowercase__				=					field(
        default=lowerCamelCase__	,				metadata={"""help""": """Whether to perform speed measurements. Speed measurements can be disabled via --no-speed."""}	,				)
    lowercase__				=					field(
        default=lowerCamelCase__	,				metadata={
            """help""": """Whether to perform memory measurements. Memory measurements can be disabled via --no-memory"""
        }	,				)
    lowercase__				=					field(default=lowerCamelCase__	,				metadata={"""help""": """Trace memory line by line"""}			)
    lowercase__				=					field(default=lowerCamelCase__	,				metadata={"""help""": """Save result to a CSV file"""}			)
    lowercase__				=					field(default=lowerCamelCase__	,				metadata={"""help""": """Save all print statements in a log file"""}			)
    lowercase__				=					field(default=lowerCamelCase__	,				metadata={"""help""": """Whether to print environment information"""}			)
    lowercase__				=					field(
        default=lowerCamelCase__	,				metadata={
            """help""": (
                """Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use"""
                """ multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled"""
                """ for debugging / testing and on TPU."""
            )
        }	,				)
    lowercase__				=					field(
        default=f'inference_time_{round(time()			)}.csv'	,				metadata={"""help""": """CSV filename used if saving time results to csv."""}	,				)
    lowercase__				=					field(
        default=f'inference_memory_{round(time()			)}.csv'	,				metadata={"""help""": """CSV filename used if saving memory results to csv."""}	,				)
    lowercase__				=					field(
        default=f'train_time_{round(time()			)}.csv'	,				metadata={"""help""": """CSV filename used if saving time results to csv for training."""}	,				)
    lowercase__				=					field(
        default=f'train_memory_{round(time()			)}.csv'	,				metadata={"""help""": """CSV filename used if saving memory results to csv for training."""}	,				)
    lowercase__				=					field(
        default=f'env_info_{round(time()			)}.csv'	,				metadata={"""help""": """CSV filename used if saving environment information."""}	,				)
    lowercase__				=					field(
        default=f'log_{round(time()			)}.csv'	,				metadata={"""help""": """Log filename used if print statements are saved in log."""}	,				)
    lowercase__				=					field(default=3	,				metadata={"""help""": """Times an experiment will be run."""}			)
    lowercase__				=					field(
        default=lowerCamelCase__	,				metadata={
            """help""": (
                """Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain"""
                """ model weights."""
            )
        }	,				)







    def     lowercase_ (   self				:						Any			):

      '''simple docstring'''





      warnings.warn(
          F"""The class {self.__class__} is deprecated. Hugging Face Benchmarking utils"""
          ' are deprecated in general and it is advised to use external Benchmarking libraries '
          ' to benchmark Transformer models.'		,    __snake_case		,    )







    def     lowercase_ (   self				:						Tuple			):

      '''simple docstring'''





      return json.dumps(dataclasses.asdict(self			)		,    indent=2			)







    @property
    def     lowercase_ (   self				:						Any			):

      '''simple docstring'''





      if len(self.models			) <= 0:
        raise ValueError(
            'Please make sure you provide at least one model name / model identifier, *e.g.* `--models'
            ' bert-base-cased` or `args.models = [\'bert-base-cased\'].'			)
      return self.models







    @property
    def     lowercase_ (   self				:						List[Any]			):

      '''simple docstring'''





      if not self.multi_process:
        return False
      elif self.is_tpu:
        logger.info('Multiprocessing is currently not possible on TPU.'			)
        return False
      else:
        return True




'''simple docstring'''






import inspect
import math
import tempfile
import unittest

import numpy as np

from transformers import ViTMAEConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch
     from torch import nn

     from transformers import ViTMAEForPreTraining, ViTMAEModel
     from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import ViTImageProcessor

class 							a__:







    def __init__(   self				:						List[str]		,    __snake_case				:						Optional[Any]		,    __snake_case				:						Any=13		,    __snake_case				:						int=30		,    __snake_case				:						Union[str, Any]=2		,    __snake_case				:						Optional[int]=3		,    __snake_case				:						Dict=True		,    __snake_case				:						List[str]=True		,    __snake_case				:						Any=32		,    __snake_case				:						List[Any]=5		,    __snake_case				:						List[Any]=4		,    __snake_case				:						Optional[int]=37		,    __snake_case				:						int="gelu"		,    __snake_case				:						Optional[Any]=0.1		,    __snake_case				:						List[str]=0.1		,    __snake_case				:						List[str]=10		,    __snake_case				:						int=0.02		,    __snake_case				:						Optional[int]=3		,    __snake_case				:						Tuple=0.6		,    __snake_case				:						Union[str, Any]=None		,    ):
      a		:      List[str]       =  parent
      a		:      Tuple       =  batch_size
      a		:      Union[str, Any]       =  image_size
      a		:      List[str]       =  patch_size
      a		:      Optional[Any]       =  num_channels
      a		:      Optional[Any]       =  is_training
      a		:      List[Any]       =  use_labels
      a		:      Union[str, Any]       =  hidden_size
      a		:      Dict       =  num_hidden_layers
      a		:      Optional[Any]       =  num_attention_heads
      a		:      Optional[Any]       =  intermediate_size
      a		:      int       =  hidden_act
      a		:      Dict       =  hidden_dropout_prob
      a		:      Optional[Any]       =  attention_probs_dropout_prob
      a		:      Optional[Any]       =  type_sequence_label_size
      a		:      Optional[Any]       =  initializer_range
      a		:      Union[str, Any]       =  mask_ratio
      a		:      Optional[int]       =  scope

      # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above
      # (we add 1 for the [CLS] token)
      a		:      Tuple       =  (image_size // patch_size) ** 2
      a		:      List[Any]       =  int(math.ceil((1 - mask_ratio) * (num_patches + 1)			)			)







    def     lowercase_ (   self				:						List[str]			):
      a		:      str       =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

      a		:      Tuple       =  None
      if self.use_labels:
        a		:      List[str]       =  ids_tensor([self.batch_size]		,    self.type_sequence_label_size			)

      a		:      Optional[int]       =  self.get_config()

      return config, pixel_values, labels







    def     lowercase_ (   self				:						str			):
      return ViTMAEConfig(
          image_size=self.image_size		,    patch_size=self.patch_size		,    num_channels=self.num_channels		,    hidden_size=self.hidden_size		,    num_hidden_layers=self.num_hidden_layers		,    num_attention_heads=self.num_attention_heads		,    intermediate_size=self.intermediate_size		,    hidden_act=self.hidden_act		,    hidden_dropout_prob=self.hidden_dropout_prob		,    attention_probs_dropout_prob=self.attention_probs_dropout_prob		,    is_decoder=__snake_case		,    initializer_range=self.initializer_range		,    mask_ratio=self.mask_ratio		,    )







    def     lowercase_ (   self				:						Dict		,    __snake_case				:						Dict		,    __snake_case				:						List[Any]		,    __snake_case				:						Union[str, Any]			):
      a		:      Union[str, Any]       =  ViTMAEModel(config=__snake_case			)
      model.to(__snake_case			)
      model.eval()
      a		:      Optional[Any]       =  model(__snake_case			)
      self.parent.assertEqual(result.last_hidden_state.shape		,    (self.batch_size, self.seq_length, self.hidden_size)			)







    def     lowercase_ (   self				:						Optional[Any]		,    __snake_case				:						List[str]		,    __snake_case				:						Optional[int]		,    __snake_case				:						Dict			):
      a		:      Dict       =  ViTMAEForPreTraining(__snake_case			)
      model.to(__snake_case			)
      model.eval()
      a		:      Dict       =  model(__snake_case			)
      a		:      str       =  (self.image_size // self.patch_size) ** 2
      a		:      int       =  self.patch_size**2 * self.num_channels
      self.parent.assertEqual(result.logits.shape		,    (self.batch_size, num_patches, expected_num_channels)			)

      # test greyscale images
      a		:      Any       =  1
      a		:      Optional[int]       =  ViTMAEForPreTraining(__snake_case			)
      model.to(__snake_case			)
      model.eval()
      a		:      int       =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
      a		:      Optional[Any]       =  model(__snake_case			)
      a		:      Dict       =  self.patch_size**2
      self.parent.assertEqual(result.logits.shape		,    (self.batch_size, num_patches, expected_num_channels)			)







    def     lowercase_ (   self				:						Dict			):
      a		:      List[str]       =  self.prepare_config_and_inputs()
      a     ,			a     ,			a		:      Any       =  config_and_inputs
      a		:      Optional[Any]       =  {'pixel_values': pixel_values}
      return config, inputs_dict

@require_torch
class 							a__(      lowerCamelCase__	,				lowerCamelCase__	,				unittest.TestCase			):
    lowercase__				=					(ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else ()
    lowercase__				=					{"""feature-extraction""": ViTMAEModel} if is_torch_available() else {}

    lowercase__				=					False
    lowercase__				=					False
    lowercase__				=					False
    lowercase__				=					False







    def     lowercase_ (   self				:						Tuple			):
      a		:      List[str]       =  ViTMAEModelTester(self			)
      a		:      List[str]       =  ConfigTester(self		,    config_class=__snake_case		,    has_text_modality=__snake_case		,    hidden_size=37			)







    def     lowercase_ (   self				:						List[str]			):
      self.config_tester.run_common_tests()







    @unittest.skip(reason='ViTMAE does not use inputs_embeds'			)
    def     lowercase_ (   self				:						List[str]			):
      pass







    def     lowercase_ (   self				:						Dict			):
      a     ,			a		:      List[Any]       =  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        a		:      Optional[Any]       =  model_class(__snake_case			)
        self.assertIsInstance(model.get_input_embeddings()		,    (nn.Module)			)
        a		:      Optional[int]       =  model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(__snake_case		,    nn.Linear			)			)







    def     lowercase_ (   self				:						List[str]			):
      a     ,			a		:      Dict       =  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        a		:      Any       =  model_class(__snake_case			)
        a		:      Any       =  inspect.signature(model.forward			)
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        a		:      int       =  [*signature.parameters.keys()]

        a		:      List[Any]       =  ['pixel_values']
        self.assertListEqual(arg_names[:1]		,    __snake_case			)







    def     lowercase_ (   self				:						Any			):
      a		:      Optional[int]       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__snake_case			)







    def     lowercase_ (   self				:						List[Any]			):
      a		:      Dict       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_pretraining(*__snake_case			)







    def     lowercase_ (   self				:						List[str]		,    __snake_case				:						Optional[Any]		,    __snake_case				:						Union[str, Any]		,    __snake_case				:						Tuple			):
      # make masks reproducible
      np.random.seed(2			)

      a		:      Union[str, Any]       =  int((pt_model.config.image_size // pt_model.config.patch_size) ** 2			)
      a		:      int       =  np.random.uniform(size=(self.model_tester.batch_size, num_patches)			)
      a		:      List[Any]       =  torch.from_numpy(__snake_case			)

      # Add `noise` argument.
      # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument
      a		:      List[str]       =  pt_noise

      super().check_pt_tf_models(__snake_case		,    __snake_case		,    __snake_case			)







    def     lowercase_ (   self				:						Optional[int]			):
      a     ,			a		:      List[str]       =  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        a		:      Any       =  model_class(__snake_case			)
        model.to(__snake_case			)
        model.eval()
        # make random mask reproducible
        torch.manual_seed(2			)
        with torch.no_grad():
          a		:      Tuple       =  model(**self._prepare_for_class(__snake_case		,    __snake_case			)			)

        a		:      List[str]       =  outputs[0].cpu().numpy()
        a		:      str       =  0

        with tempfile.TemporaryDirectory() as tmpdirname:
          model.save_pretrained(__snake_case			)
          a		:      Dict       =  model_class.from_pretrained(__snake_case			)
          model.to(__snake_case			)
          # make random mask reproducible
          torch.manual_seed(2			)
          with torch.no_grad():
            a		:      int       =  model(**self._prepare_for_class(__snake_case		,    __snake_case			)			)

          # Make sure we don't have nans
          a		:      List[Any]       =  after_outputs[0].cpu().numpy()
          a		:      Any       =  0
          a		:      Any       =  np.amax(np.abs(out_a - out_a			)			)
          self.assertLessEqual(__snake_case		,    1e-5			)







    @unittest.skip(
        reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results.'			)
    def     lowercase_ (   self				:						List[Any]			):
      pass







    @unittest.skip(
        reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results.'			)
    def     lowercase_ (   self				:						Any			):
      pass







    @unittest.skip(
        reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results.'			)
    def     lowercase_ (   self				:						int			):
      pass







    @unittest.skip(reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load'			)
    def     lowercase_ (   self				:						int			):
      pass







    @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'			)
    def     lowercase_ (   self				:						Any			):
      pass







    @slow
    def     lowercase_ (   self				:						Dict			):
      for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a		:      str       =  ViTMAEModel.from_pretrained(__snake_case			)
        self.assertIsNotNone(__snake_case			)
def    lowerCamelCase__ (		):
  a		:      Tuple       =  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
  return image

@require_torch
@require_vision
class 							a__(      unittest.TestCase			):







    @cached_property
    def     lowercase_ (   self				:						Union[str, Any]			):
      return ViTImageProcessor.from_pretrained('facebook/vit-mae-base'			) if is_vision_available() else None







    @slow
    def     lowercase_ (   self				:						Union[str, Any]			):
      # make random mask reproducible across the PT and TF model
      np.random.seed(2			)

      a		:      Union[str, Any]       =  ViTMAEForPreTraining.from_pretrained('facebook/vit-mae-base'			).to(__snake_case			)

      a		:      str       =  self.default_image_processor
      a		:      Dict       =  prepare_img()
      a		:      Union[str, Any]       =  image_processor(images=__snake_case		,    return_tensors='pt'			).to(__snake_case			)

      # prepare a noise vector that will be also used for testing the TF model
      # (this way we can ensure that the PT and TF models operate on the same inputs)
      a		:      Tuple       =  ViTMAEConfig()
      a		:      Optional[Any]       =  int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2			)
      a		:      List[str]       =  np.random.uniform(size=(1, num_patches)			)

      # forward pass
      with torch.no_grad():
        a		:      Tuple       =  model(**__snake_case		,    noise=torch.from_numpy(__snake_case			).to(device=__snake_case			)			)

      # verify the logits
      a		:      int       =  torch.Size((1, 1_96, 7_68)			)
      self.assertEqual(outputs.logits.shape		,    __snake_case			)

      a		:      Optional[Any]       =  torch.tensor(
          [[-0.0548, -1.7023, -0.9325], [0.3721, -0.5670, -0.2233], [0.8235, -1.3878, -0.3524]]			)

      self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]		,    expected_slice.to(__snake_case			)		,    atol=1e-4			)			)