


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







   def __init__(     self						,     UpperCamelCase__						,     UpperCamelCase__=13						,     UpperCamelCase__=30						,     UpperCamelCase__=2						,     UpperCamelCase__=3						,     UpperCamelCase__=True						,     UpperCamelCase__=True						,     UpperCamelCase__=32						,     UpperCamelCase__=5						,     UpperCamelCase__=4						,     UpperCamelCase__=37						,     UpperCamelCase__="gelu"						,     UpperCamelCase__=0.1						,     UpperCamelCase__=0.1						,     UpperCamelCase__=10						,     UpperCamelCase__=0.02						,     UpperCamelCase__=3						,     UpperCamelCase__=0.6						,     UpperCamelCase__=None						,     )     ->		Optional[int]:

       '''simple docstring'''




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

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







   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       snake_case   :				str				 =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

       snake_case   :				Optional[int]				 =   None
       if self.use_labels:
           snake_case   :				int				 =   ids_tensor([self.batch_size]						,     self.type_sequence_label_size						)

       snake_case   :				str				 =   self.get_config()

       return config, pixel_values, labels







   def 			lowerCamelCase		(     self						)     ->		Optional[int]:

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







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		Tuple:

       '''simple docstring'''




       snake_case   :				Tuple				 =   ViTMAEModel(config=UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()
       snake_case   :				Dict				 =   model(UpperCamelCase__						)
       self.parent.assertEqual(result.last_hidden_state.shape						,     (self.batch_size, self.seq_length, self.hidden_size)						)







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		Dict:

       '''simple docstring'''




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







   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       snake_case   :				Any				 =   self.prepare_config_and_inputs()
       snake_case   :				List[Any]				 =   config_and_inputs
       snake_case   :				Any				 =   {"""pixel_values""": pixel_values}
       return config, inputs_dict







@require_torch
class _lowerCAmelCase    (				_SCREAMING_SNAKE_CASE   ,							_SCREAMING_SNAKE_CASE   ,							unittest.TestCase     ):
   __UpperCAmelCase			:	str							  =  (ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else ()
   __UpperCAmelCase			:	Tuple							  =  {'''feature-extraction''': ViTMAEModel} if is_torch_available() else {}

   __UpperCAmelCase			:	Tuple							  =  False
   __UpperCAmelCase			:	Any							  =  False
   __UpperCAmelCase			:	str							  =  False
   __UpperCAmelCase			:	str							  =  False







   def 			lowerCamelCase		(     self						)     ->		List[Any]:

       '''simple docstring'''




       snake_case   :				Optional[int]				 =   ViTMAEModelTester(self						)
       snake_case   :				Optional[int]				 =   ConfigTester(self						,     config_class=UpperCamelCase__						,     has_text_modality=UpperCamelCase__						,     hidden_size=37						)







   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''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						)     ->		Tuple:

       '''simple docstring'''




       snake_case   :				Optional[Any]				 =   self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           snake_case   :				List[str]				 =   model_class(UpperCamelCase__						)
           self.assertIsInstance(model.get_input_embeddings()						,     (nn.Module)						)
           snake_case   :				Tuple				 =   model.get_output_embeddings()
           self.assertTrue(x is None or isinstance(UpperCamelCase__						,     nn.Linear						)						)







   def 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       snake_case   :				List[Any]				 =   self.model_tester.prepare_config_and_inputs_for_common()

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

           snake_case   :				Optional[Any]				 =   ["""pixel_values"""]
           self.assertListEqual(arg_names[:1]						,     UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       snake_case   :				Any				 =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       snake_case   :				Tuple				 =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_pretraining(*UpperCamelCase__						)







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		str:

       '''simple docstring'''




       np.random.seed(2						)

       snake_case   :				Tuple				 =   int((pt_model.config.image_size // pt_model.config.patch_size) ** 2						)
       snake_case   :				Optional[int]				 =   np.random.uniform(size=(self.model_tester.batch_size, num_patches)						)
       snake_case   :				Optional[Any]				 =   torch.from_numpy(UpperCamelCase__						)

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

       super().check_pt_tf_models(UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




       snake_case   :				Optional[Any]				 =   self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           snake_case   :				Optional[int]				 =   model_class(UpperCamelCase__						)
           model.to(UpperCamelCase__						)
           model.eval()
           # make random mask reproducible
           torch.manual_seed(2						)
           with torch.no_grad():
               snake_case   :				Any				 =   model(**self._prepare_for_class(UpperCamelCase__						,     UpperCamelCase__						)						)

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

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

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

       '''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						)     ->		Optional[Any]:

       '''simple docstring'''




       pass







   @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       pass







   @slow
   def 			lowerCamelCase		(     self						)     ->		List[Any]:

       '''simple docstring'''




       for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           snake_case   :				List[Any]				 =   ViTMAEModel.from_pretrained(UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)







def   __lowerCAmelCase						(						)     ->						int:


    """simple docstring"""





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







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







   @cached_property
   def 			lowerCamelCase		(     self						)     ->		Union[str, Any]:

       '''simple docstring'''




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







   @slow
   def 			lowerCamelCase		(     self						)     ->		List[Any]:

       '''simple docstring'''




       np.random.seed(2						)

       snake_case   :				Dict				 =   ViTMAEForPreTraining.from_pretrained("facebook/vit-mae-base"						).to(UpperCamelCase__						)

       snake_case   :				Union[str, Any]				 =   self.default_image_processor
       snake_case   :				Union[str, Any]				 =   prepare_img()
       snake_case   :				Dict				 =   image_processor(images=UpperCamelCase__						,     return_tensors="pt"						).to(UpperCamelCase__						)

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

       # forward pass
       with torch.no_grad():
           snake_case   :				str				 =   model(**UpperCamelCase__						,     noise=torch.from_numpy(UpperCamelCase__						).to(device=UpperCamelCase__						)						)

       # verify the logits
       snake_case   :				str				 =   torch.Size((1, 196, 768)						)
       self.assertEqual(outputs.logits.shape						,     UpperCamelCase__						)

       snake_case   :				str				 =   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(UpperCamelCase__						)						,     atol=1e-4						)						)










"""simple docstring"""



def   __lowerCAmelCase						(						lowercase  :  int						,							lowercase  :  int						,							lowercase  :  list[list[int]]			)     ->						int:


    """simple docstring"""





    def update_area_of_max_square(lowercase  :  int						,							lowercase  :  int			) -> int:
        # BASE CASE
        if row >= rows or col >= cols:
            return 0

        snake_case   :				Dict				 =   update_area_of_max_square(lowercase						,							col + 1			)
        snake_case   :				Tuple				 =   update_area_of_max_square(row + 1						,							col + 1			)
        snake_case   :				Any				 =   update_area_of_max_square(row + 1						,							lowercase			)

        if mat[row][col]:
            snake_case   :				List[Any]				 =   1 + min([right, diagonal, down]			)
            snake_case   :				Any				 =   max(largest_square_area[0]						,							lowercase			)
            return sub_problem_sol
        else:
            return 0

    snake_case   :				int				 =   [0]
    update_area_of_max_square(0						,							0			)
    return largest_square_area[0]







def   __lowerCAmelCase						(						lowercase  :  int						,							lowercase  :  int						,							lowercase  :  list[list[int]]			)     ->						int:


    """simple docstring"""





    def update_area_of_max_square_using_dp_array(
        lowercase  :  int						,							lowercase  :  int						,							lowercase  :  list[list[int]]			) -> int:
        if row >= rows or col >= cols:
            return 0
        if dp_array[row][col] != -1:
            return dp_array[row][col]

        snake_case   :				List[str]				 =   update_area_of_max_square_using_dp_array(lowercase						,							col + 1						,							lowercase			)
        snake_case   :				Optional[int]				 =   update_area_of_max_square_using_dp_array(row + 1						,							col + 1						,							lowercase			)
        snake_case   :				Optional[int]				 =   update_area_of_max_square_using_dp_array(row + 1						,							lowercase						,							lowercase			)

        if mat[row][col]:
            snake_case   :				Dict				 =   1 + min([right, diagonal, down]			)
            snake_case   :				Union[str, Any]				 =   max(largest_square_area[0]						,							lowercase			)
            snake_case   :				str				 =   sub_problem_sol
            return sub_problem_sol
        else:
            return 0

    snake_case   :				Union[str, Any]				 =   [0]
    snake_case   :				int				 =   [[-1] * cols for _ in range(lowercase			)]
    update_area_of_max_square_using_dp_array(0						,							0						,							lowercase			)

    return largest_square_area[0]







def   __lowerCAmelCase						(						lowercase  :  int						,							lowercase  :  int						,							lowercase  :  list[list[int]]			)     ->						int:


    """simple docstring"""





    snake_case   :				int				 =   [[0] * (cols + 1) for _ in range(rows + 1			)]
    snake_case   :				List[Any]				 =   0
    for row in range(rows - 1						,							-1						,							-1			):
        for col in range(cols - 1						,							-1						,							-1			):
            snake_case   :				Tuple				 =   dp_array[row][col + 1]
            snake_case   :				Any				 =   dp_array[row + 1][col + 1]
            snake_case   :				List[str]				 =   dp_array[row + 1][col]

            if mat[row][col] == 1:
                snake_case   :				Optional[int]				 =   1 + min(lowercase						,							lowercase						,							lowercase			)
                snake_case   :				Tuple				 =   max(dp_array[row][col]						,							lowercase			)
            else:
                snake_case   :				List[Any]				 =   0

    return largest_square_area







def   __lowerCAmelCase						(						lowercase  :  int						,							lowercase  :  int						,							lowercase  :  list[list[int]]			)     ->						int:


    """simple docstring"""





    snake_case   :				Any				 =   [0] * (cols + 1)
    snake_case   :				Any				 =   [0] * (cols + 1)
    snake_case   :				Any				 =   0
    for row in range(rows - 1						,							-1						,							-1			):
        for col in range(cols - 1						,							-1						,							-1			):
            snake_case   :				Dict				 =   current_row[col + 1]
            snake_case   :				List[Any]				 =   next_row[col + 1]
            snake_case   :				Dict				 =   next_row[col]

            if mat[row][col] == 1:
                snake_case   :				Union[str, Any]				 =   1 + min(lowercase						,							lowercase						,							lowercase			)
                snake_case   :				Optional[int]				 =   max(current_row[col]						,							lowercase			)
            else:
                snake_case   :				Optional[Any]				 =   0
        snake_case   :				str				 =   current_row

    return largest_square_area


if __name__ == "__main__":
  import doctest

  doctest.testmod()
  print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))





