





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 SCREAMING_SNAKE_CASE_  :






						def __init__( self : List[str]						,				_A : int						,				_A : List[str]=13						,				_A : Any=30						,				_A : str=2						,				_A : Optional[Any]=3						,				_A : Optional[Any]=True						,				_A : Union[str, Any]=True						,				_A : Optional[int]=32						,				_A : int=2						,				_A : Optional[int]=4						,				_A : Optional[Any]=37						,				_A : Any="gelu"						,				_A : Union[str, Any]=0.1						,				_A : List[Any]=0.1						,				_A : Dict=10						,				_A : List[Any]=0.0_2						,				_A : Any=3						,				_A : Tuple=0.6						,				_A : Optional[int]=None						,				)							->      Optional[int]:







							"""simple docstring"""


							snake_case_						:     Union[str, Any]     =		parent
							snake_case_						:     Dict     =		batch_size
							snake_case_						:     Union[str, Any]     =		image_size
							snake_case_						:     str     =		patch_size
							snake_case_						:     List[Any]     =		num_channels
							snake_case_						:     Union[str, Any]     =		is_training
							snake_case_						:     int     =		use_labels
							snake_case_						:     List[str]     =		hidden_size
							snake_case_						:     Any     =		num_hidden_layers
							snake_case_						:     List[str]     =		num_attention_heads
							snake_case_						:     Union[str, Any]     =		intermediate_size
							snake_case_						:     int     =		hidden_act
							snake_case_						:     List[str]     =		hidden_dropout_prob
							snake_case_						:     Union[str, Any]     =		attention_probs_dropout_prob
							snake_case_						:     Optional[Any]     =		type_sequence_label_size
							snake_case_						:     Any     =		initializer_range
							snake_case_						:     Optional[int]     =		mask_ratio
							snake_case_						:     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)
							snake_case_						:     Optional[Any]     =		(image_size // patch_size) ** 2
							snake_case_						:     Dict     =		int(math.ceil((1 - mask_ratio) * (num_patches + 1)		)		)






						def UpperCAmelCase_			( self : str		)							->      Optional[Any]:







							"""simple docstring"""


							snake_case_						:     Any     =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

							snake_case_						:     int     =		self.get_config()

							return config, pixel_values, labels






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







							"""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 : Dict						,				_A : str						,				_A : List[Any]						,				_A : Optional[int]		)							->      Union[str, Any]:







							"""simple docstring"""


							snake_case_						:     int     =		TFViTMAEModel(config=_A		)
							snake_case_						:     str     =		model(_A						,				training=_A		)
							self.parent.assertEqual(result.last_hidden_state.shape						,				(self.batch_size, self.seq_length, self.hidden_size)		)






						def UpperCAmelCase_			( self : Tuple						,				_A : List[str]						,				_A : Dict						,				_A : Dict		)							->      str:







							"""simple docstring"""


							snake_case_						:     int     =		TFViTMAEForPreTraining(_A		)
							snake_case_						:     int     =		model(_A						,				training=_A		)
							# expected sequence length = num_patches
							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_						:     str     =		1
							snake_case_						:     Dict     =		TFViTMAEForPreTraining(_A		)

							snake_case_						:     Tuple     =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
							snake_case_						:     List[Any]     =		model(_A						,				training=_A		)
							snake_case_						:     Dict     =		self.patch_size**2
							self.parent.assertEqual(result.logits.shape						,				(self.batch_size, num_patches, expected_num_channels)		)






						def UpperCAmelCase_			( self : int		)							->      int:







							"""simple docstring"""


							snake_case_						:     Any     =		self.prepare_config_and_inputs()
							(snake_case_)						:     Tuple     =		config_and_inputs
							snake_case_						:     str     =		{'pixel_values': pixel_values}
							return config, inputs_dict
@require_tf
class SCREAMING_SNAKE_CASE_  (   snake_case_    ,			snake_case_    ,			unittest.TestCase   ):
						__magic_name__:     int	 =    (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else ()
						__magic_name__:     List[Any]	 =    {"feature-extraction": TFViTMAEModel} if is_tf_available() else {}

						__magic_name__:     str	 =    False
						__magic_name__:     Union[str, Any]	 =    False
						__magic_name__:     Optional[Any]	 =    False
						__magic_name__:     List[Any]	 =    False






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







							"""simple docstring"""


							snake_case_						:     List[Any]     =		TFViTMAEModelTester(self		)
							snake_case_						:     Any     =		ConfigTester(self						,				config_class=_A						,				has_text_modality=_A						,				hidden_size=37		)






						def UpperCAmelCase_			( self : str		)							->      int:







							"""simple docstring"""


							self.config_tester.run_common_tests()






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







							"""simple docstring"""


							pass






						def UpperCAmelCase_			( self : Optional[int]		)							->      Dict:







							"""simple docstring"""


							snake_case_						:     str     =		self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								snake_case_						:     int     =		model_class(_A		)
								self.assertIsInstance(model.get_input_embeddings()						,				(tf.keras.layers.Layer)		)
								snake_case_						:     int     =		model.get_output_embeddings()
								self.assertTrue(x is None or isinstance(_A						,				tf.keras.layers.Layer		)		)






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







							"""simple docstring"""


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

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

								snake_case_						:     Dict     =		['pixel_values']
								self.assertListEqual(arg_names[:1]						,				_A		)






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







							"""simple docstring"""


							snake_case_						:     List[str]     =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_A		)






						def UpperCAmelCase_			( self : Dict		)							->      str:







							"""simple docstring"""


							snake_case_						:     Tuple     =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*_A		)






						def UpperCAmelCase_			( self : List[str]		)							->      List[str]:







							"""simple docstring"""


							np.random.seed(2		)

							snake_case_						:     Tuple     =		self.model_tester.prepare_config_and_inputs_for_common()

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

							for model_class in self.all_model_classes:
								snake_case_						:     Tuple     =		model_class(_A		)
								snake_case_						:     int     =		self._prepare_for_class(_A						,				_A		)

								snake_case_						:     str     =		model(_A						,				noise=_A		)

								snake_case_						:     List[str]     =		copy.deepcopy(self._prepare_for_class(_A						,				_A		)		)
								snake_case_						:     str     =		model(**_A						,				noise=_A		)
								snake_case_						:     Dict     =		outputs_dict[0].numpy()
								snake_case_						:     List[str]     =		outputs_keywords[0].numpy()

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






						def UpperCAmelCase_			( self : Tuple		)							->      Optional[Any]:







							"""simple docstring"""


							np.random.seed(2		)

							snake_case_						:     Tuple     =		self.model_tester.prepare_config_and_inputs_for_common()

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

							def prepare_numpy_arrays(_A : Tuple		):
								snake_case_						:     Tuple     =		{}
								for k, v in inputs_dict.items():
									if tf.is_tensor(_A		):
										snake_case_						:     List[Any]     =		v.numpy()
									else:
										snake_case_						:     List[Any]     =		np.array(_A		)

								return inputs_np_dict

							for model_class in self.all_model_classes:
								snake_case_						:     Tuple     =		model_class(_A		)

								snake_case_						:     Union[str, Any]     =		self._prepare_for_class(_A						,				_A		)
								snake_case_						:     str     =		prepare_numpy_arrays(_A		)

								snake_case_						:     Optional[Any]     =		model(_A						,				noise=_A		)
								snake_case_						:     List[Any]     =		model(**_A						,				noise=_A		)
								self.assert_outputs_same(_A						,				_A		)






						def UpperCAmelCase_			( self : Union[str, Any]						,				_A : List[Any]						,				_A : int						,				_A : str		)							->      Tuple:







							"""simple docstring"""


							np.random.seed(2		)

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

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

							super().check_pt_tf_models(_A						,				_A						,				_A		)






						def UpperCAmelCase_			( self : Tuple		)							->      int:







							"""simple docstring"""


							np.random.seed(2		)

							snake_case_						:     str     =		self.model_tester.prepare_config_and_inputs_for_common()

							snake_case_						:     Optional[int]     =		{
							    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		)
							}

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

							for main_layer_class in tf_main_layer_classes:
								snake_case_						:     List[str]     =		main_layer_class(_A		)

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

								snake_case_						:     Optional[int]     =		tf.keras.Model(_A						,				outputs=main_layer(_A		)		)
								snake_case_						:     Union[str, Any]     =		model(_A		)

								with tempfile.TemporaryDirectory() as tmpdirname:
									snake_case_						:     Dict     =		os.path.join(_A						,				'keras_model.h5'		)
									model.save(_A		)
									snake_case_						:     str     =		tf.keras.models.load_model(
									    _A						,				custom_objects={main_layer_class.__name__: main_layer_class}		)
									assert isinstance(_A						,				tf.keras.Model		)
									snake_case_						:     Dict     =		model(_A		)
									self.assert_outputs_same(_A						,				_A		)






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







							"""simple docstring"""


							np.random.seed(2		)

							snake_case_						:     Optional[int]     =		self.model_tester.prepare_config_and_inputs_for_common()

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

							for model_class in self.all_model_classes:
								snake_case_						:     Dict     =		model_class(_A		)
								snake_case_						:     Optional[int]     =		self._prepare_for_class(_A						,				_A		)
								snake_case_						:     Dict     =		model(_A						,				noise=_A		)

								if model_class.__name__ == "TFViTMAEModel":
									snake_case_						:     Union[str, Any]     =		outputs.last_hidden_state.numpy()
									snake_case_						:     Dict     =		0
								else:
									snake_case_						:     Dict     =		outputs.logits.numpy()
									snake_case_						:     int     =		0

								with tempfile.TemporaryDirectory() as tmpdirname:
									model.save_pretrained(_A						,				saved_model=_A		)
									snake_case_						:     Optional[int]     =		model_class.from_pretrained(_A		)
									snake_case_						:     Union[str, Any]     =		model(_A						,				noise=_A		)

									if model_class.__name__ == "TFViTMAEModel":
										snake_case_						:     Union[str, Any]     =		after_outputs['last_hidden_state'].numpy()
										snake_case_						:     List[str]     =		0
									else:
										snake_case_						:     Dict     =		after_outputs['logits'].numpy()
										snake_case_						:     int     =		0

									snake_case_						:     List[str]     =		np.amax(np.abs(out_a - out_a		)		)
									self.assertLessEqual(_A						,				1E-5		)






						def UpperCAmelCase_			( self : str		)							->      List[str]:







							"""simple docstring"""


							np.random.seed(2		)

							snake_case_						:     Tuple     =		self.model_tester.prepare_config_and_inputs_for_common()

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

							for model_class in self.all_model_classes:
								snake_case_						:     Any     =		model_class(_A		)
								snake_case_						:     str     =		self._prepare_for_class(_A						,				_A		)

								snake_case_						:     List[Any]     =		model(_A						,				noise=_A		)
								snake_case_						:     Optional[Any]     =		model.get_config()
								# make sure that returned config is jsonifiable, which is required by keras
								json.dumps(_A		)
								snake_case_						:     Any     =		model_class.from_config(model.get_config()		)
								# make sure it also accepts a normal config
								snake_case_						:     str     =		model_class.from_config(model.config		)
								snake_case_						:     Optional[int]     =		new_model(_A		)  # Build model
								new_model.set_weights(model.get_weights()		)
								snake_case_						:     str     =		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 : Tuple		)							->      List[str]:







							"""simple docstring"""


							pass






						@unittest.skip(reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load'		)
						def UpperCAmelCase_			( self : Optional[Any]		)							->      Optional[Any]:







							"""simple docstring"""


							pass






						@slow
						def UpperCAmelCase_			( self : Dict		)							->      int:







							"""simple docstring"""


							snake_case_						:     Any     =		TFViTMAEModel.from_pretrained('google/vit-base-patch16-224'		)
							self.assertIsNotNone(_A		)


def 							SCREAMING_SNAKE_CASE__  (      ):
	snake_case_						:     Dict     =		Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
	return image


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






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







							"""simple docstring"""


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






						@slow
						def UpperCAmelCase_			( self : Dict		)							->      Union[str, Any]:







							"""simple docstring"""


							np.random.seed(2		)

							snake_case_						:     Tuple     =		TFViTMAEForPreTraining.from_pretrained('facebook/vit-mae-base'		)

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

							# forward pass
							snake_case_						:     Dict     =		model(**_A						,				noise=_A		)

							# verify the logits
							snake_case_						:     List[str]     =		tf.convert_to_tensor([1, 196, 768]		)
							self.assertEqual(outputs.logits.shape						,				_A		)

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

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








# this script reports modified .py files under the desired list of top-level sub-dirs passed as a list of arguments, e.g.:
#   python ./utils/get_modified_files.py utils src tests examples
#
# it uses git to find the forking point and which files were modified - i.e. files not under git won't be considered
# since the output of this script is fed into Makefile commands it doesn't print a newline after the results

import re
import subprocess
import sys


_SCREAMING_SNAKE_CASE 		=						subprocess.check_output("""git merge-base main HEAD""".split()).decode("""utf-8""")
_SCREAMING_SNAKE_CASE 		=						(
    subprocess.check_output(F'''git diff --diff-filter=d --name-only {fork_point_sha}'''.split()).decode("""utf-8""").split()
)

_SCREAMING_SNAKE_CASE 		=						"""|""".join(sys.argv[1:])
_SCREAMING_SNAKE_CASE 		=						re.compile(RF'''^({joined_dirs}).*?\.py$''')

_SCREAMING_SNAKE_CASE 		=						[x for x in modified_files if regex.match(x)]
print(""" """.join(relevant_modified_files), end="""""")

