

from __future__ import annotations

import copy
import inspect
import json
import math
import os
import tempfile
import unittest
from importlib import import_module

import numpy as np

from transformers import ViTMAEConfig
from transformers.file_utils import cached_property, is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, require_vision, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import TFViTMAEForPreTraining, TFViTMAEModel


if is_vision_available():
					from PIL import Image

					from transformers import ViTImageProcessor

class 				a  :


						def __init__(       self						,					A_						,					A_=13						,					A_=30						,					A_=2						,					A_=3						,					A_=True						,					A_=True						,					A_=32						,					A_=2						,					A_=4						,					A_=37						,					A_="gelu"						,					A_=0.1						,					A_=0.1						,					A_=10						,					A_=0.02						,					A_=3						,					A_=0.6						,					A_=None						,					):







										'''simple docstring'''


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

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


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


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

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

										_UpperCAmelCase       :		int 		=    self.get_config()

										return config, pixel_values, labels


						def        _UpperCAmelCase				(       self	):







										'''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						,					decoder_hidden_size=self.hidden_size						,					decoder_num_hidden_layers=self.num_hidden_layers						,					decoder_num_attention_heads=self.num_attention_heads						,					decoder_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        _UpperCAmelCase				(       self						,					A_						,					A_						,					A_	):







										'''simple docstring'''


										_UpperCAmelCase       :		Optional[Any] 		=    TFViTMAEModel(config=A_	)
										_UpperCAmelCase       :		Any 		=    model(A_						,					training=A_	)
										self.parent.assertEqual(result.last_hidden_state.shape						,					(self.batch_size, self.seq_length, self.hidden_size)	)


						def        _UpperCAmelCase				(       self						,					A_						,					A_						,					A_	):







										'''simple docstring'''


										_UpperCAmelCase       :		Any 		=    TFViTMAEForPreTraining(A_	)
										_UpperCAmelCase       :		Union[str, Any] 		=    model(A_						,					training=A_	)
										# expected sequence length = num_patches
										_UpperCAmelCase       :		int 		=    (self.image_size // self.patch_size) ** 2
										_UpperCAmelCase       :		Optional[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
										_UpperCAmelCase       :		Union[str, Any] 		=    1
										_UpperCAmelCase       :		Union[str, Any] 		=    TFViTMAEForPreTraining(A_	)

										_UpperCAmelCase       :		Tuple 		=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
										_UpperCAmelCase       :		str 		=    model(A_						,					training=A_	)
										_UpperCAmelCase       :		int 		=    self.patch_size**2
										self.parent.assertEqual(result.logits.shape						,					(self.batch_size, num_patches, expected_num_channels)	)





						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										_UpperCAmelCase       :		str 		=    self.prepare_config_and_inputs()
										((_UpperCAmelCase)		,							(_UpperCAmelCase)		,							(_UpperCAmelCase))       :		Optional[Any] 		=    config_and_inputs
										_UpperCAmelCase       :		Any 		=    {"pixel_values": pixel_values}
										return config, inputs_dict







@require_tf
class 				a  (	UpperCAmelCase							, UpperCAmelCase							, unittest.TestCase				):
						_lowercase    						=			(TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else ()
						_lowercase    						=			{"feature-extraction": TFViTMAEModel} if is_tf_available() else {}

						_lowercase    						=			False
						_lowercase    						=			False
						_lowercase    						=			False
						_lowercase    						=			False


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										_UpperCAmelCase       :		str 		=    TFViTMAEModelTester(self	)
										_UpperCAmelCase       :		Optional[int] 		=    ConfigTester(self						,					config_class=A_						,					has_text_modality=A_						,					hidden_size=37	)


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										self.config_tester.run_common_tests()


						@unittest.skip(reason="ViTMAE does not use inputs_embeds"	)
						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										pass


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


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

										for model_class in self.all_model_classes:
														_UpperCAmelCase       :		str 		=    model_class(A_	)
														self.assertIsInstance(model.get_input_embeddings()						,					(tf.keras.layers.Layer)	)
														_UpperCAmelCase       :		Union[str, Any] 		=    model.get_output_embeddings()
														self.assertTrue(x is None or isinstance(A_						,					tf.keras.layers.Layer	)	)


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


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

										for model_class in self.all_model_classes:
														_UpperCAmelCase       :		str 		=    model_class(A_	)
														_UpperCAmelCase       :		Tuple 		=    inspect.signature(model.call	)
														# signature.parameters is an OrderedDict => so arg_names order is deterministic
														_UpperCAmelCase       :		Dict 		=    [*signature.parameters.keys()]

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


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										_UpperCAmelCase       :		Dict 		=    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*A_	)


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										_UpperCAmelCase       :		Dict 		=    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_pretraining(*A_	)


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										np.random.seed(2	)

										_UpperCAmelCase		,							_UpperCAmelCase       :		Dict 		=    self.model_tester.prepare_config_and_inputs_for_common()

										_UpperCAmelCase       :		List[Any] 		=    int((config.image_size // config.patch_size) ** 2	)
										_UpperCAmelCase       :		List[Any] 		=    np.random.uniform(size=(self.model_tester.batch_size, num_patches)	)

										for model_class in self.all_model_classes:
														_UpperCAmelCase       :		List[str] 		=    model_class(A_	)
														_UpperCAmelCase       :		Tuple 		=    self._prepare_for_class(A_						,					A_	)

														_UpperCAmelCase       :		str 		=    model(A_						,					noise=A_	)

														_UpperCAmelCase       :		int 		=    copy.deepcopy(self._prepare_for_class(A_						,					A_	)	)
														_UpperCAmelCase       :		str 		=    model(**A_						,					noise=A_	)
														_UpperCAmelCase       :		List[str] 		=    outputs_dict[0].numpy()
														_UpperCAmelCase       :		Union[str, Any] 		=    outputs_keywords[0].numpy()

														self.assertLess(np.sum(np.abs(output_dict - output_keywords	)	)						,					1e-6	)


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										np.random.seed(2	)

										_UpperCAmelCase		,							_UpperCAmelCase       :		List[str] 		=    self.model_tester.prepare_config_and_inputs_for_common()

										_UpperCAmelCase       :		Dict 		=    int((config.image_size // config.patch_size) ** 2	)
										_UpperCAmelCase       :		Union[str, Any] 		=    np.random.uniform(size=(self.model_tester.batch_size, num_patches)	)

										def prepare_numpy_arrays(A_	):
														_UpperCAmelCase       :		List[Any] 		=    {}
														for k, v in inputs_dict.items():
																		if tf.is_tensor(A_	):
																						_UpperCAmelCase       :		Optional[int] 		=    v.numpy()
																		else:
																						_UpperCAmelCase       :		List[Any] 		=    np.array(A_	)

														return inputs_np_dict

										for model_class in self.all_model_classes:
														_UpperCAmelCase       :		Optional[int] 		=    model_class(A_	)

														_UpperCAmelCase       :		Dict 		=    self._prepare_for_class(A_						,					A_	)
														_UpperCAmelCase       :		int 		=    prepare_numpy_arrays(A_	)

														_UpperCAmelCase       :		List[Any] 		=    model(A_						,					noise=A_	)
														_UpperCAmelCase       :		Union[str, Any] 		=    model(**A_						,					noise=A_	)
														self.assert_outputs_same(A_						,					A_	)


						def        _UpperCAmelCase				(       self						,					A_						,					A_						,					A_	):







										'''simple docstring'''


										np.random.seed(2	)

										_UpperCAmelCase       :		Tuple 		=    int((tf_model.config.image_size // tf_model.config.patch_size) ** 2	)
										_UpperCAmelCase       :		int 		=    np.random.uniform(size=(self.model_tester.batch_size, num_patches)	)
										_UpperCAmelCase       :		Union[str, Any] 		=    tf.constant(A_	)

										# Add `noise` argument.
										# PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument
										_UpperCAmelCase       :		Optional[int] 		=    tf_noise

										super().check_pt_tf_models(A_						,					A_						,					A_	)


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										np.random.seed(2	)

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

										_UpperCAmelCase       :		Tuple 		=    {
										    module_member
										    for model_class in self.all_model_classes
										    for module in (import_module(model_class.__module__	),)
										    for module_member_name in dir(A_	)
										    if module_member_name.endswith("MainLayer"	)
										    # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`.
										    and module_member_name[: -len("MainLayer"	)] == model_class.__name__[: -len("Model"	)]
										    for module_member in (getattr(A_						,					A_	),)
										    if isinstance(A_						,					A_	)
										    and tf.keras.layers.Layer in module_member.__bases__
										    and getattr(A_						,					"_keras_serializable"						,					A_	)
										}

										_UpperCAmelCase       :		Optional[Any] 		=    int((config.image_size // config.patch_size) ** 2	)
										_UpperCAmelCase       :		List[Any] 		=    np.random.uniform(size=(self.model_tester.batch_size, num_patches)	)
										_UpperCAmelCase       :		int 		=    tf.convert_to_tensor(A_	)
										inputs_dict.update({"noise": noise}	)

										for main_layer_class in tf_main_layer_classes:
														_UpperCAmelCase       :		List[str] 		=    main_layer_class(A_	)

														_UpperCAmelCase       :		Any 		=    {
														    name: tf.keras.Input(tensor.shape[1:]						,					dtype=tensor.dtype	) for name, tensor in inputs_dict.items()
														}

														_UpperCAmelCase       :		Any 		=    tf.keras.Model(A_						,					outputs=main_layer(A_	)	)
														_UpperCAmelCase       :		Optional[Any] 		=    model(A_	)

														with tempfile.TemporaryDirectory() as tmpdirname:
																		_UpperCAmelCase       :		str 		=    os.path.join(A_						,					"keras_model.h5"	)
																		model.save(A_	)
																		_UpperCAmelCase       :		Optional[int] 		=    tf.keras.models.load_model(
																		    A_						,					custom_objects={main_layer_class.__name__: main_layer_class}	)
																		assert isinstance(A_						,					tf.keras.Model	)
																		_UpperCAmelCase       :		List[Any] 		=    model(A_	)
																		self.assert_outputs_same(A_						,					A_	)


						@slow
						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										np.random.seed(2	)

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

										_UpperCAmelCase       :		List[str] 		=    int((config.image_size // config.patch_size) ** 2	)
										_UpperCAmelCase       :		Any 		=    np.random.uniform(size=(self.model_tester.batch_size, num_patches)	)

										for model_class in self.all_model_classes:
														_UpperCAmelCase       :		List[Any] 		=    model_class(A_	)
														_UpperCAmelCase       :		List[str] 		=    self._prepare_for_class(A_						,					A_	)
														_UpperCAmelCase       :		str 		=    model(A_						,					noise=A_	)

														if model_class.__name__ == "TFViTMAEModel":
																		_UpperCAmelCase       :		int 		=    outputs.last_hidden_state.numpy()
																		_UpperCAmelCase       :		int 		=    0
														else:
																		_UpperCAmelCase       :		List[Any] 		=    outputs.logits.numpy()
																		_UpperCAmelCase       :		Optional[int] 		=    0

														with tempfile.TemporaryDirectory() as tmpdirname:
																		model.save_pretrained(A_						,					saved_model=A_	)
																		_UpperCAmelCase       :		Optional[int] 		=    model_class.from_pretrained(A_	)
																		_UpperCAmelCase       :		Any 		=    model(A_						,					noise=A_	)

																		if model_class.__name__ == "TFViTMAEModel":
																						_UpperCAmelCase       :		int 		=    after_outputs["last_hidden_state"].numpy()
																						_UpperCAmelCase       :		Any 		=    0
																		else:
																						_UpperCAmelCase       :		Dict 		=    after_outputs["logits"].numpy()
																						_UpperCAmelCase       :		str 		=    0

																		_UpperCAmelCase       :		int 		=    np.amax(np.abs(out_a - out_a	)	)
																		self.assertLessEqual(A_						,					1e-5	)


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										np.random.seed(2	)

										_UpperCAmelCase		,							_UpperCAmelCase       :		Tuple 		=    self.model_tester.prepare_config_and_inputs_for_common()

										_UpperCAmelCase       :		Any 		=    int((config.image_size // config.patch_size) ** 2	)
										_UpperCAmelCase       :		str 		=    np.random.uniform(size=(self.model_tester.batch_size, num_patches)	)

										for model_class in self.all_model_classes:
														_UpperCAmelCase       :		Any 		=    model_class(A_	)
														_UpperCAmelCase       :		Union[str, Any] 		=    self._prepare_for_class(A_						,					A_	)

														_UpperCAmelCase       :		Any 		=    model(A_						,					noise=A_	)
														_UpperCAmelCase       :		Any 		=    model.get_config()
														# make sure that returned config is jsonifiable, which is required by keras
														json.dumps(A_	)
														_UpperCAmelCase       :		str 		=    model_class.from_config(model.get_config()	)
														# make sure it also accepts a normal config
														_UpperCAmelCase       :		Optional[int] 		=    model_class.from_config(model.config	)
														_UpperCAmelCase       :		Optional[Any] 		=    new_model(A_	)  # Build model
														new_model.set_weights(model.get_weights()	)
														_UpperCAmelCase       :		Optional[int] 		=    new_model(A_						,					noise=A_	)

														self.assert_outputs_same(A_						,					A_	)


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







										'''simple docstring'''


										pass


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







										'''simple docstring'''


										pass





						@slow
						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										_UpperCAmelCase       :		str 		=    TFViTMAEModel.from_pretrained("google/vit-base-patch16-224"	)
										self.assertIsNotNone(A_	)







def 							__SCREAMING_SNAKE_CASE							(				)   ->       Dict:
				_UpperCAmelCase       :		str 		=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
				return image





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


						@cached_property
						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


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





						@slow
						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										np.random.seed(2	)

										_UpperCAmelCase       :		Any 		=    TFViTMAEForPreTraining.from_pretrained("facebook/vit-mae-base"	)

										_UpperCAmelCase       :		Tuple 		=    self.default_image_processor
										_UpperCAmelCase       :		List[Any] 		=    prepare_img()
										_UpperCAmelCase       :		Dict 		=    image_processor(images=A_						,					return_tensors="tf"	)

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

										# forward pass
										_UpperCAmelCase       :		int 		=    model(**A_						,					noise=A_	)

										# verify the logits
										_UpperCAmelCase       :		List[Any] 		=    tf.convert_to_tensor([1, 196, 768]	)
										self.assertEqual(outputs.logits.shape						,					A_	)

										_UpperCAmelCase       :		List[str] 		=    tf.convert_to_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]]	)

										tf.debugging.assert_near(outputs.logits[0, :3, :3]						,					A_						,					atol=1e-4	)





import warnings

from ...utils import logging
from .image_processing_perceiver import PerceiverImageProcessor


SCREAMING_SNAKE_CASE_        =      logging.get_logger(__name__)

class 				a  (	UpperCAmelCase				):


						def __init__(       self						,					*A_						,					**A_	):







										'''simple docstring'''


										warnings.warn(
										    "The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers."
										    " Please use PerceiverImageProcessor instead."						,					A_						,					)
										super().__init__(*A_						,					**A_	)


