





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   :    Optional[Any]							,	__lowerCAmelCase   :    Optional[int]=13							,	__lowerCAmelCase   :    Optional[int]=30							,	__lowerCAmelCase   :    Optional[int]=2							,	__lowerCAmelCase   :    List[Any]=3							,	__lowerCAmelCase   :    Optional[Any]=True							,	__lowerCAmelCase   :    List[str]=True							,	__lowerCAmelCase   :    Tuple=32							,	__lowerCAmelCase   :    int=5							,	__lowerCAmelCase   :    Tuple=4							,	__lowerCAmelCase   :    Optional[int]=37							,	__lowerCAmelCase   :    List[Any]="gelu"							,	__lowerCAmelCase   :    Any=0.1							,	__lowerCAmelCase   :    int=0.1							,	__lowerCAmelCase   :    Dict=10							,	__lowerCAmelCase   :    List[Any]=0.0_2							,	__lowerCAmelCase   :    Union[str, Any]=3							,	__lowerCAmelCase   :    Optional[Any]=0.6							,	__lowerCAmelCase   :    Optional[int]=None							,	)	->	str:







					"""simple docstring"""






					A__        =   parent
					A__        =   batch_size
					A__        =   image_size
					A__        =   patch_size
					A__        =   num_channels
					A__        =   is_training
					A__        =   use_labels
					A__        =   hidden_size
					A__        =   num_hidden_layers
					A__        =   num_attention_heads
					A__        =   intermediate_size
					A__        =   hidden_act
					A__        =   hidden_dropout_prob
					A__        =   attention_probs_dropout_prob
					A__        =   type_sequence_label_size
					A__        =   initializer_range
					A__        =   mask_ratio
					A__        =   scope

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






			def    a_       (			self   :    int   )	->	Union[str, Any]:







					"""simple docstring"""






					A__        =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

					A__        =   None
					if self.use_labels:
							A__        =   ids_tensor([self.batch_size]							,	self.type_sequence_label_size   )

					A__        =   self.get_config()

					return config, pixel_values, labels






			def    a_       (			self   :    Union[str, Any]   )	->	Tuple:







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






			def    a_       (			self   :    int							,	__lowerCAmelCase   :    int							,	__lowerCAmelCase   :    Any							,	__lowerCAmelCase   :    List[Any]   )	->	List[Any]:







					"""simple docstring"""






					A__        =   ViTMAEModel(config=__lowerCAmelCase   )
					model.to(__lowerCAmelCase   )
					model.eval()
					A__        =   model(__lowerCAmelCase   )
					self.parent.assertEqual(result.last_hidden_state.shape							,	(self.batch_size, self.seq_length, self.hidden_size)   )






			def    a_       (			self   :    Union[str, Any]							,	__lowerCAmelCase   :    str							,	__lowerCAmelCase   :    Union[str, Any]							,	__lowerCAmelCase   :    str   )	->	Any:







					"""simple docstring"""






					A__        =   ViTMAEForPreTraining(__lowerCAmelCase   )
					model.to(__lowerCAmelCase   )
					model.eval()
					A__        =   model(__lowerCAmelCase   )
					A__        =   (self.image_size // self.patch_size) ** 2
					A__        =   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__        =   1
					A__        =   ViTMAEForPreTraining(__lowerCAmelCase   )
					model.to(__lowerCAmelCase   )
					model.eval()
					A__        =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
					A__        =   model(__lowerCAmelCase   )
					A__        =   self.patch_size**2
					self.parent.assertEqual(result.logits.shape							,	(self.batch_size, num_patches, expected_num_channels)   )







			def    a_       (			self   :    Optional[int]   )	->	List[str]:







					"""simple docstring"""






					A__        =   self.prepare_config_and_inputs()
					A__      ,			A__      ,			A__        =   config_and_inputs
					A__        =   {"""pixel_values""": pixel_values}
					return config, inputs_dict


@require_torch
class   A			(SCREAMING_SNAKE_CASE   ,     SCREAMING_SNAKE_CASE   ,     unittest.TestCase    ):
			'''simple docstring'''




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

			__lowerCamelCase	:       int							=    False
			__lowerCamelCase	:       Optional[Any]							=    False
			__lowerCamelCase	:       str							=    False
			__lowerCamelCase	:       str							=    False






			def    a_       (			self   :    Optional[int]   )	->	Optional[Any]:







					"""simple docstring"""






					A__        =   ViTMAEModelTester(self   )
					A__        =   ConfigTester(self							,	config_class=__lowerCAmelCase							,	has_text_modality=__lowerCAmelCase							,	hidden_size=37   )






			def    a_       (			self   :    Any   )	->	Any:







					"""simple docstring"""






					self.config_tester.run_common_tests()






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







					"""simple docstring"""






					pass






			def    a_       (			self   :    int   )	->	int:







					"""simple docstring"""






					A__      ,			A__        =   self.model_tester.prepare_config_and_inputs_for_common()

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






			def    a_       (			self   :    str   )	->	Any:







					"""simple docstring"""






					A__      ,			A__        =   self.model_tester.prepare_config_and_inputs_for_common()

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

							A__        =   ["""pixel_values"""]
							self.assertListEqual(arg_names[:1]							,	__lowerCAmelCase   )






			def    a_       (			self   :    Dict   )	->	Tuple:







					"""simple docstring"""






					A__        =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*__lowerCAmelCase   )






			def    a_       (			self   :    List[Any]   )	->	Optional[Any]:







					"""simple docstring"""






					A__        =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_pretraining(*__lowerCAmelCase   )






			def    a_       (			self   :    Optional[int]							,	__lowerCAmelCase   :    str							,	__lowerCAmelCase   :    Any							,	__lowerCAmelCase   :    Optional[int]   )	->	str:







					"""simple docstring"""






					np.random.seed(2   )

					A__        =   int((pt_model.config.image_size // pt_model.config.patch_size) ** 2   )
					A__        =   np.random.uniform(size=(self.model_tester.batch_size, num_patches)   )
					A__        =   torch.from_numpy(__lowerCAmelCase   )

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

					super().check_pt_tf_models(__lowerCAmelCase							,	__lowerCAmelCase							,	__lowerCAmelCase   )






			def    a_       (			self   :    Optional[Any]   )	->	int:







					"""simple docstring"""






					A__      ,			A__        =   self.model_tester.prepare_config_and_inputs_for_common()

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

							A__        =   outputs[0].cpu().numpy()
							A__        =   0

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

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






			@unittest.skip(
			    reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load
    to get deterministic results."""   )
			def    a_       (			self   :    List[str]   )	->	Optional[Any]:







					"""simple docstring"""






					pass






			@unittest.skip(
			    reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load
    to get deterministic results."""   )
			def    a_       (			self   :    str   )	->	Dict:







					"""simple docstring"""






					pass






			@unittest.skip(
			    reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load
    to get deterministic results."""   )
			def    a_       (			self   :    int   )	->	List[Any]:







					"""simple docstring"""






					pass






			@unittest.skip(reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load"""   )
			def    a_       (			self   :    Optional[int]   )	->	Union[str, Any]:







					"""simple docstring"""






					pass






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







					"""simple docstring"""






					pass







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







					"""simple docstring"""






					for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							A__        =   ViTMAEModel.from_pretrained(__lowerCAmelCase   )
							self.assertIsNotNone(__lowerCAmelCase   )







def    __lowerCamelCase					(							)		->				List[Any]:






		"""simple docstring"""

		A__        =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""    )
		return image






@require_torch
@require_vision
class   A			(unittest.TestCase    ):
			'''simple docstring'''




			@cached_property
			def    a_       (			self   :    Optional[Any]   )	->	int:







					"""simple docstring"""






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







			@slow
			def    a_       (			self   :    int   )	->	Union[str, Any]:







					"""simple docstring"""






					np.random.seed(2   )

					A__        =   ViTMAEForPreTraining.from_pretrained("""facebook/vit-mae-base"""   ).to(__lowerCAmelCase   )

					A__        =   self.default_image_processor
					A__        =   prepare_img()
					A__        =   image_processor(images=__lowerCAmelCase							,	return_tensors="""pt"""   ).to(__lowerCAmelCase   )

					# 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__        =   ViTMAEConfig()
					A__        =   int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2   )
					A__        =   np.random.uniform(size=(1, num_patches)   )

					# forward pass
					with torch.no_grad():
							A__        =   model(**__lowerCAmelCase							,	noise=torch.from_numpy(__lowerCAmelCase   ).to(device=__lowerCAmelCase   )   )

					# verify the logits
					A__        =   torch.Size((1, 1_96, 7_68)   )
					self.assertEqual(outputs.logits.shape							,	__lowerCAmelCase   )

					A__        =   torch.tensor(
					    [[-0.0_5_4_8, -1.7_0_2_3, -0.9_3_2_5], [0.3_7_2_1, -0.5_6_7_0, -0.2_2_3_3], [0.8_2_3_5, -1.3_8_7_8, -0.3_5_2_4]]   )

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










import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)


class   A			(unittest.TestCase    ):
			'''simple docstring'''




			def    a_       (			self   :    Any   )	->	Union[str, Any]:







					"""simple docstring"""






					A__        =   ["""a""", """b""", """c"""]

					# Defaults to last layer if both are None
					A__      ,			A__        =   get_aligned_output_features_output_indices(__lowerCAmelCase							,	__lowerCAmelCase							,	__lowerCAmelCase   )
					self.assertEqual(__lowerCAmelCase							,	["""c"""]   )
					self.assertEqual(__lowerCAmelCase							,	[2]   )

					# Out indices set to match out features
					A__      ,			A__        =   get_aligned_output_features_output_indices(["""a""", """c"""]							,	__lowerCAmelCase							,	__lowerCAmelCase   )
					self.assertEqual(__lowerCAmelCase							,	["""a""", """c"""]   )
					self.assertEqual(__lowerCAmelCase							,	[0, 2]   )

					# Out features set to match out indices
					A__      ,			A__        =   get_aligned_output_features_output_indices(__lowerCAmelCase							,	[0, 2]							,	__lowerCAmelCase   )
					self.assertEqual(__lowerCAmelCase							,	["""a""", """c"""]   )
					self.assertEqual(__lowerCAmelCase							,	[0, 2]   )

					# Out features selected from negative indices
					A__      ,			A__        =   get_aligned_output_features_output_indices(__lowerCAmelCase							,	[-3, -1]							,	__lowerCAmelCase   )
					self.assertEqual(__lowerCAmelCase							,	["""a""", """c"""]   )
					self.assertEqual(__lowerCAmelCase							,	[-3, -1]   )






			def    a_       (			self   :    Optional[Any]   )	->	Optional[Any]:







					"""simple docstring"""






					with self.assertRaises(__lowerCAmelCase   ):
							verify_out_features_out_indices(["""a""", """b"""]							,	(0, 1)							,	__lowerCAmelCase   )

					# Out features must be a list
					with self.assertRaises(__lowerCAmelCase   ):
							verify_out_features_out_indices(("""a""", """b""")							,	(0, 1)							,	["""a""", """b"""]   )

					# Out features must be a subset of stage names
					with self.assertRaises(__lowerCAmelCase   ):
							verify_out_features_out_indices(["""a""", """b"""]							,	(0, 1)							,	["""a"""]   )

					# Out indices must be a list or tuple
					with self.assertRaises(__lowerCAmelCase   ):
							verify_out_features_out_indices(__lowerCAmelCase							,	0							,	["""a""", """b"""]   )

					# Out indices must be a subset of stage names
					with self.assertRaises(__lowerCAmelCase   ):
							verify_out_features_out_indices(__lowerCAmelCase							,	(0, 1)							,	["""a"""]   )

					# Out features and out indices must be the same length
					with self.assertRaises(__lowerCAmelCase   ):
							verify_out_features_out_indices(["""a""", """b"""]							,	(0,)							,	["""a""", """b""", """c"""]   )

					# Out features should match out indices
					with self.assertRaises(__lowerCAmelCase   ):
							verify_out_features_out_indices(["""a""", """b"""]							,	(0, 2)							,	["""a""", """b""", """c"""]   )

					# Out features and out indices should be in order
					with self.assertRaises(__lowerCAmelCase   ):
							verify_out_features_out_indices(["""b""", """a"""]							,	(0, 1)							,	["""a""", """b"""]   )

					# Check passes with valid inputs
					verify_out_features_out_indices(["""a""", """b""", """d"""]							,	(0, 1, -1)							,	["""a""", """b""", """c""", """d"""]   )







			def    a_       (			self   :    Union[str, Any]   )	->	List[str]:







					"""simple docstring"""






					A__        =   BackboneMixin()

					A__        =   ["""a""", """b""", """c"""]
					A__        =   ["""a""", """c"""]
					A__        =   [0, 2]

					# Check that the output features and indices are set correctly
					self.assertEqual(backbone.out_features							,	["""a""", """c"""]   )
					self.assertEqual(backbone.out_indices							,	[0, 2]   )

					# Check out features and indices are updated correctly
					A__        =   ["""a""", """b"""]
					self.assertEqual(backbone.out_features							,	["""a""", """b"""]   )
					self.assertEqual(backbone.out_indices							,	[0, 1]   )

					A__        =   [-3, -1]
					self.assertEqual(backbone.out_features							,	["""a""", """c"""]   )
					self.assertEqual(backbone.out_indices							,	[-3, -1]   )



