


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						:




							"""simple docstring"""


							def __init__(				self			:    Any       ,      lowerCamelCase			:    Any       ,      lowerCamelCase			:    List[Any]=13       ,      lowerCamelCase			:    Tuple=30       ,      lowerCamelCase			:    Tuple=2       ,      lowerCamelCase			:    Optional[Any]=3       ,      lowerCamelCase			:    int=True       ,      lowerCamelCase			:    List[Any]=True       ,      lowerCamelCase			:    int=32       ,      lowerCamelCase			:    Tuple=5       ,      lowerCamelCase			:    Dict=4       ,      lowerCamelCase			:    Optional[Any]=37       ,      lowerCamelCase			:    Optional[int]="gelu"       ,      lowerCamelCase			:    Dict=0.1       ,      lowerCamelCase			:    Optional[Any]=0.1       ,      lowerCamelCase			:    Dict=10       ,      lowerCamelCase			:    Dict=0.02       ,      lowerCamelCase			:    List[Any]=3       ,      lowerCamelCase			:    Union[str, Any]=0.6       ,      lowerCamelCase			:    Tuple=None       ,      )      ->       List[Any]:
														__snake_case						:     List[Any]									=  parent
														__snake_case						:     int									=  batch_size
														__snake_case						:     str									=  image_size
														__snake_case						:     Optional[int]									=  patch_size
														__snake_case						:     List[str]									=  num_channels
														__snake_case						:     List[str]									=  is_training
														__snake_case						:     Optional[int]									=  use_labels
														__snake_case						:     str									=  hidden_size
														__snake_case						:     Dict									=  num_hidden_layers
														__snake_case						:     Union[str, Any]									=  num_attention_heads
														__snake_case						:     str									=  intermediate_size
														__snake_case						:     Optional[int]									=  hidden_act
														__snake_case						:     Optional[Any]									=  hidden_dropout_prob
														__snake_case						:     List[Any]									=  attention_probs_dropout_prob
														__snake_case						:     Optional[Any]									=  type_sequence_label_size
														__snake_case						:     List[Any]									=  initializer_range
														__snake_case						:     Optional[Any]									=  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						:     List[Any]									=  int(math.ceil((1 - mask_ratio) * (num_patches + 1)							)							)

							def        __snake_case   (				self			:    Dict							)      ->       int:
														__snake_case						:     Union[str, Any]									=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

														__snake_case						:     Tuple									=  self.get_config()

														return config, pixel_values, labels

							def        __snake_case   (				self			:    Any							)      ->       Any:
														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        __snake_case   (				self			:    Optional[int]       ,      lowerCamelCase			:    Optional[Any]       ,      lowerCamelCase			:    Optional[int]       ,      lowerCamelCase			:    List[str]							)      ->       Tuple:
														__snake_case						:     Optional[int]									=  ViTMAEModel(config=__UpperCAmelCase							)
														model.to(__UpperCAmelCase							)
														model.eval()
														__snake_case						:     Optional[Any]									=  model(__UpperCAmelCase							)
														self.parent.assertEqual(result.last_hidden_state.shape       ,      (self.batch_size, self.seq_length, self.hidden_size)							)

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






							def        __snake_case   (				self			:    List[Any]							)      ->       Union[str, Any]:
														__snake_case						:     List[str]									=  self.prepare_config_and_inputs()
														__snake_case						:     Tuple									=  config_and_inputs
														__snake_case						:     List[str]									=  {'''pixel_values''': pixel_values}
														return config, inputs_dict

@require_torch
class 							a						(_SCREAMING_SNAKE_CASE	,							_SCREAMING_SNAKE_CASE	,							unittest.TestCase ):




							"""simple docstring"""


							__UpperCAmelCase       :				Tuple	    =    (ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else ()
							__UpperCAmelCase       :				Union[str, Any]	    =    {'''feature-extraction''': ViTMAEModel} if is_torch_available() else {}

							__UpperCAmelCase       :				Optional[Any]	    =    False
							__UpperCAmelCase       :				Union[str, Any]	    =    False
							__UpperCAmelCase       :				Tuple	    =    False
							__UpperCAmelCase       :				Any	    =    False

							def        __snake_case   (				self			:    Optional[int]							)      ->       Union[str, Any]:
														__snake_case						:     int									=  ViTMAEModelTester(self							)
														__snake_case						:     Optional[Any]									=  ConfigTester(self       ,      config_class=__UpperCAmelCase       ,      has_text_modality=__UpperCAmelCase       ,      hidden_size=37							)

							def        __snake_case   (				self			:    Any							)      ->       Tuple:
														self.config_tester.run_common_tests()

							@unittest.skip(reason="ViTMAE does not use inputs_embeds"							)
							def        __snake_case   (				self			:    int							)      ->       Optional[Any]:
														pass

							def        __snake_case   (				self			:    int							)      ->       int:
														__snake_case						:     str									=  self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					__snake_case						:     str									=  model_class(__UpperCAmelCase							)
																					self.assertIsInstance(model.get_input_embeddings()       ,      (nn.Module)							)
																					__snake_case						:     int									=  model.get_output_embeddings()
																					self.assertTrue(x is None or isinstance(__UpperCAmelCase       ,      nn.Linear							)							)

							def        __snake_case   (				self			:    Dict							)      ->       Optional[int]:
														__snake_case						:     List[Any]									=  self.model_tester.prepare_config_and_inputs_for_common()

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

																					__snake_case						:     Optional[int]									=  ['''pixel_values''']
																					self.assertListEqual(arg_names[:1]       ,      __UpperCAmelCase							)

							def        __snake_case   (				self			:    str							)      ->       Dict:
														__snake_case						:     int									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*__UpperCAmelCase							)

							def        __snake_case   (				self			:    List[Any]							)      ->       Dict:
														__snake_case						:     List[Any]									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_pretraining(*__UpperCAmelCase							)

							def        __snake_case   (				self			:    int       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    int       ,      lowerCamelCase			:    Any							)      ->       Union[str, Any]:
														# make masks reproducible
														np.random.seed(2							)

														__snake_case						:     Any									=  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						:     Union[str, 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						:     List[str]									=  pt_noise

														super().check_pt_tf_models(__UpperCAmelCase       ,      __UpperCAmelCase       ,      __UpperCAmelCase							)

							def        __snake_case   (				self			:    int							)      ->       List[Any]:
														__snake_case						:     Tuple									=  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						:     Optional[Any]									=  outputs[0].cpu().numpy()
																					__snake_case						:     Union[str, Any]									=  0

																					with tempfile.TemporaryDirectory() as tmpdirname:
																												model.save_pretrained(__UpperCAmelCase							)
																												__snake_case						:     Union[str, Any]									=  model_class.from_pretrained(__UpperCAmelCase							)
																												model.to(__UpperCAmelCase							)
																												# make random mask reproducible
																												torch.manual_seed(2							)
																												with torch.no_grad():
																																			__snake_case						:     Optional[Any]									=  model(**self._prepare_for_class(__UpperCAmelCase       ,      __UpperCAmelCase							)							)

																												# Make sure we don't have nans
																												__snake_case						:     List[str]									=  after_outputs[0].cpu().numpy()
																												__snake_case						:     List[Any]									=  0
																												__snake_case						:     List[str]									=  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        __snake_case   (				self			:    Union[str, Any]							)      ->       Optional[int]:
														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        __snake_case   (				self			:    List[Any]							)      ->       Tuple:
														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        __snake_case   (				self			:    str							)      ->       Optional[Any]:
														pass

							@unittest.skip(reason="ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load"							)
							def        __snake_case   (				self			:    str							)      ->       List[str]:
														pass

							@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."							)
							def        __snake_case   (				self			:    str							)      ->       Optional[Any]:
														pass






							@slow
							def        __snake_case   (				self			:    Optional[Any]							)      ->       str:
														for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					__snake_case						:     str									=  ViTMAEModel.from_pretrained(__UpperCAmelCase							)
																					self.assertIsNotNone(__UpperCAmelCase							)







def 							lowerCAmelCase_	(   ):
							__snake_case						:     Union[str, Any]									=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
							return image

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




							"""simple docstring"""


							@cached_property
							def        __snake_case   (				self			:    Dict							)      ->       int:
														return ViTImageProcessor.from_pretrained("facebook/vit-mae-base"							) if is_vision_available() else None






							@slow
							def        __snake_case   (				self			:    List[Any]							)      ->       List[Any]:
														# make random mask reproducible across the PT and TF model
														np.random.seed(2							)

														__snake_case						:     List[str]									=  ViTMAEForPreTraining.from_pretrained("facebook/vit-mae-base"							).to(__UpperCAmelCase							)

														__snake_case						:     str									=  self.default_image_processor
														__snake_case						:     List[Any]									=  prepare_img()
														__snake_case						:     int									=  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						:     int									=  ViTMAEConfig()
														__snake_case						:     List[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						:     Optional[Any]									=  model(**__UpperCAmelCase       ,      noise=torch.from_numpy(__UpperCAmelCase							).to(device=__UpperCAmelCase							)							)

														# verify the logits
														__snake_case						:     Tuple									=  torch.Size((1, 196, 768)							)
														self.assertEqual(outputs.logits.shape       ,      __UpperCAmelCase							)

														__snake_case						:     List[str]									=  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(__UpperCAmelCase							)       ,      atol=1E-4							)							)




import math

import tensorflow as tf
from packaging import version






def        snake_case__		(    lowerCamelCase_		):
							A      :					Optional[int]      		=					tf.convert_to_tensor(lowerCamelCase_		)
							A      :					Union[str, Any]      		=					0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0		)     ,				x.dtype		)		))

							return x * cdf






def        snake_case__		(    lowerCamelCase_		):
							A      :					List[str]      		=					tf.convert_to_tensor(lowerCamelCase_		)
							A      :					Any      		=					tf.cast(math.pi     ,				x.dtype		)
							A      :					int      		=					tf.cast(0.044_715     ,				x.dtype		)
							A      :					Tuple      		=					0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi		) * (x + coeff * tf.pow(lowerCamelCase_     ,				3		))		))

							return x * cdf






def        snake_case__		(    lowerCamelCase_		):
							A      :					Union[str, Any]      		=					tf.convert_to_tensor(lowerCamelCase_		)

							return x * tf.tanh(tf.math.softplus(lowerCamelCase_		)		)






def        snake_case__		(    lowerCamelCase_		):
							A      :					Optional[int]      		=					tf.convert_to_tensor(lowerCamelCase_		)
							A      :					List[Any]      		=					tf.cast(0.044_715     ,				x.dtype		)
							A      :					str      		=					tf.cast(0.7_978_845_608     ,				x.dtype		)

							return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)		))






def        snake_case__		(    lowerCamelCase_		):
							A      :					List[str]      		=					tf.convert_to_tensor(lowerCamelCase_		)
							A      :					Any      		=					tf.cast(1.702     ,				x.dtype		)
							return x * tf.math.sigmoid(coeff * x		)






def        snake_case__		(    lowerCamelCase_		):
							return tf.clip_by_value(_gelu(lowerCamelCase_		)     ,				-10     ,				10		)






def        snake_case__		(    lowerCamelCase_     ,				lowerCamelCase_=-1		):
							A		,		A      :					Dict      		=					tf.split(lowerCamelCase_     ,				2     ,				axis=lowerCamelCase_		)
							return a * tf.math.sigmoid(lowerCamelCase_		)


if version.parse(tf.version.VERSION) >= version.parse("2.4"):






		def        snake_case__		(    lowerCamelCase_		):
									return tf.keras.activations.gelu(lowerCamelCase_     ,				approximate=lowerCamelCase_		)

		lowercase			:				Tuple   =     tf.keras.activations.gelu

		lowercase			:				Union[str, Any]   =     approximate_gelu_wrap
else:
		lowercase			:				str   =     _gelu
		lowercase			:				Dict   =     _gelu_new


lowercase			:				Union[str, Any]   =     {
    "gelu": gelu,
    "gelu_10": gelu_aa,
    "gelu_fast": gelu_fast,
    "gelu_new": gelu_new,
    "glu": glu,
    "mish": mish,
    "quick_gelu": quick_gelu,
    "relu": tf.keras.activations.relu,
    "sigmoid": tf.keras.activations.sigmoid,
    "silu": tf.keras.activations.swish,
    "swish": tf.keras.activations.swish,
    "tanh": tf.keras.activations.tanh,
}






def        snake_case__		(    lowerCamelCase_		):
							if activation_string in ACTaFN:
														return ACTaFN[activation_string]
							else:
														raise KeyError(F'function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()		)}'		)






