






"""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 						snake_case  :





    def __init__(							self     : Any    ,		A     : Any    ,		A     : Optional[Any]=1_3    ,		A     : int=3_0    ,		A     : Any=2    ,		A     : Optional[int]=3    ,		A     : Any=True    ,		A     : int=True    ,		A     : Optional[Any]=3_2    ,		A     : Any=5    ,		A     : int=4    ,		A     : List[str]=3_7    ,		A     : int="gelu"    ,		A     : Any=0.1    ,		A     : int=0.1    ,		A     : Union[str, Any]=1_0    ,		A     : Dict=0.02    ,		A     : List[str]=3    ,		A     : List[Any]=0.6    ,		A     : List[Any]=None    ,		):


      '''simple docstring'''




      a					:      Union[str, Any]    =       parent
      a					:      Optional[int]    =       batch_size
      a					:      int    =       image_size
      a					:      Dict    =       patch_size
      a					:      str    =       num_channels
      a					:      Union[str, Any]    =       is_training
      a					:      List[str]    =       use_labels
      a					:      List[Any]    =       hidden_size
      a					:      List[Any]    =       num_hidden_layers
      a					:      List[Any]    =       num_attention_heads
      a					:      List[str]    =       intermediate_size
      a					:      Optional[Any]    =       hidden_act
      a					:      Union[str, Any]    =       hidden_dropout_prob
      a					:      str    =       attention_probs_dropout_prob
      a					:      List[Any]    =       type_sequence_label_size
      a					:      Union[str, Any]    =       initializer_range
      a					:      Optional[int]    =       mask_ratio
      a					:      str    =       scope

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





    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




      a					:      int    =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

      a					:      List[Any]    =       self.get_config()

      return config, pixel_values, labels





    def     lowerCamelCase__				(							self     : Optional[Any]      ):


      '''simple docstring'''




      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=A    ,		initializer_range=self.initializer_range    ,		mask_ratio=self.mask_ratio    ,		)





    def     lowerCamelCase__				(							self     : int    ,		A     : Any    ,		A     : List[str]    ,		A     : List[Any]      ):


      '''simple docstring'''




      a					:      List[Any]    =       ViTMAEModel(config=A      )
      model.to(A      )
      model.eval()
      a					:      Tuple    =       model(A      )
      self.parent.assertEqual(result.last_hidden_state.shape    ,		(self.batch_size, self.seq_length, self.hidden_size)      )





    def     lowerCamelCase__				(							self     : int    ,		A     : int    ,		A     : Optional[int]    ,		A     : str      ):


      '''simple docstring'''




      a					:      Tuple    =       ViTMAEForPreTraining(A      )
      model.to(A      )
      model.eval()
      a					:      Optional[Any]    =       model(A      )
      a					:      List[str]    =       (self.image_size // self.patch_size) ** 2
      a					:      List[str]    =       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					:      str    =       1
      a					:      Optional[int]    =       ViTMAEForPreTraining(A      )
      model.to(A      )
      model.eval()
      a					:      List[Any]    =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
      a					:      Tuple    =       model(A      )
      a					:      Any    =       self.patch_size**2
      self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, num_patches, expected_num_channels)      )





    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




      a					:      Optional[int]    =       self.prepare_config_and_inputs()
      a,					a,					a					:      str    =       config_and_inputs
      a					:      Union[str, Any]    =       {'pixel_values': pixel_values}
      return config, inputs_dict






@require_torch
class 						snake_case  (  UpperCAmelCase       ,			UpperCAmelCase       ,			unittest.TestCase	):
    __magic_name__   					=					(ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else ()
    __magic_name__   					=					{'''feature-extraction''': ViTMAEModel} if is_torch_available() else {}

    __magic_name__   					=					False
    __magic_name__   					=					False
    __magic_name__   					=					False
    __magic_name__   					=					False





    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




      a					:      str    =       ViTMAEModelTester(self      )
      a					:      int    =       ConfigTester(self    ,		config_class=A    ,		has_text_modality=A    ,		hidden_size=3_7      )





    def     lowerCamelCase__				(							self     : str      ):


      '''simple docstring'''




      self.config_tester.run_common_tests()





    @unittest.skip(reason='ViTMAE does not use inputs_embeds'      )
    def     lowerCamelCase__				(							self     : Optional[int]      ):


      '''simple docstring'''




      pass





    def     lowerCamelCase__				(							self     : List[Any]      ):


      '''simple docstring'''




      a,					a					:      int    =       self.model_tester.prepare_config_and_inputs_for_common()

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





    def     lowerCamelCase__				(							self     : Optional[int]      ):


      '''simple docstring'''




      a,					a					:      Union[str, Any]    =       self.model_tester.prepare_config_and_inputs_for_common()

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

        a					:      str    =       ['pixel_values']
        self.assertListEqual(arg_names[:1]    ,		A      )





    def     lowerCamelCase__				(							self     : Optional[Any]      ):


      '''simple docstring'''




      a					:      Optional[int]    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*A      )





    def     lowerCamelCase__				(							self     : Optional[int]      ):


      '''simple docstring'''




      a					:      List[Any]    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_pretraining(*A      )





    def     lowerCamelCase__				(							self     : Tuple    ,		A     : Union[str, Any]    ,		A     : Any    ,		A     : int      ):


      '''simple docstring'''




      np.random.seed(2      )

      a					:      Any    =       int((pt_model.config.image_size // pt_model.config.patch_size) ** 2      )
      a					:      Tuple    =       np.random.uniform(size=(self.model_tester.batch_size, num_patches)      )
      a					:      Dict    =       torch.from_numpy(A      )

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

      super().check_pt_tf_models(A    ,		A    ,		A      )





    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




      a,					a					:      Union[str, Any]    =       self.model_tester.prepare_config_and_inputs_for_common()

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

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

        with tempfile.TemporaryDirectory() as tmpdirname:
          model.save_pretrained(A      )
          a					:      List[str]    =       model_class.from_pretrained(A      )
          model.to(A      )
          # make random mask reproducible
          torch.manual_seed(2      )
          with torch.no_grad():
            a					:      List[str]    =       model(**self._prepare_for_class(A    ,		A      )      )

          # Make sure we don't have nans
          a					:      List[str]    =       after_outputs[0].cpu().numpy()
          a					:      Any    =       0
          a					:      str    =       np.amax(np.abs(out_a - out_a      )      )
          self.assertLessEqual(A    ,		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     lowerCamelCase__				(							self     : List[Any]      ):


      '''simple docstring'''




      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     lowerCamelCase__				(							self     : int      ):


      '''simple docstring'''




      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     lowerCamelCase__				(							self     : Union[str, Any]      ):


      '''simple docstring'''




      pass





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


      '''simple docstring'''




      pass





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


      '''simple docstring'''




      pass





    @slow
    def     lowerCamelCase__				(							self     : str      ):


      '''simple docstring'''




      for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a					:      List[str]    =       ViTMAEModel.from_pretrained(A      )
        self.assertIsNotNone(A      )




def       snake_case  ():







  '''simple docstring'''

  a					:      List[str]    =       Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
  return image






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





    @cached_property
    def     lowerCamelCase__				(							self     : int      ):


      '''simple docstring'''




      return ViTImageProcessor.from_pretrained('facebook/vit-mae-base'      ) if is_vision_available() else None





    @slow
    def     lowerCamelCase__				(							self     : Dict      ):


      '''simple docstring'''




      np.random.seed(2      )

      a					:      Optional[Any]    =       ViTMAEForPreTraining.from_pretrained('facebook/vit-mae-base'      ).to(A      )

      a					:      Optional[int]    =       self.default_image_processor
      a					:      Optional[int]    =       prepare_img()
      a					:      Dict    =       image_processor(images=A    ,		return_tensors='pt'      ).to(A      )

      # 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					:      List[str]    =       ViTMAEConfig()
      a					:      Optional[Any]    =       int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2      )
      a					:      List[Any]    =       np.random.uniform(size=(1, num_patches)      )

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

      # verify the logits
      a					:      Optional[Any]    =       torch.Size((1, 1_9_6, 7_6_8)      )
      self.assertEqual(outputs.logits.shape    ,		A      )

      a					:      int    =       torch.tensor(
          [[-0.05_48, -1.70_23, -0.93_25], [0.37_21, -0.56_70, -0.22_33], [0.82_35, -1.38_78, -0.35_24]]      )

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












"""simple docstring"""


def       snake_case  (A_				:int				):







  '''simple docstring'''

  if isinstance(A_     ,      A_				):
    raise TypeError('\'float\' object cannot be interpreted as an integer'				)
  if isinstance(A_     ,      A_				):
    raise TypeError('\'str\' object cannot be interpreted as an integer'				)

  if num == 0:
    return "0b0"

  a					:      List[Any]    =       False

  if num < 0:
    a					:      Optional[int]    =       True
    a					:      Dict    =       -num

  a					:      list[int]    =       []
  while num > 0:
    binary.insert(0     ,      num % 2				)
    num >>= 1

  if negative:
    return "-0b" + "".join(str(A_				) for e in binary				)

  return "0b" + "".join(str(A_				) for e in binary				)


if __name__ == "__main__":
   import doctest

   doctest.testmod()




