



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   :






			"""simple docstring"""





			def __init__(				self	:       Union[str, Any]			,     lowercase	:       Any			,     lowercase	:       Optional[Any]=13			,     lowercase	:       Optional[Any]=30			,     lowercase	:       Optional[Any]=2			,     lowercase	:       Optional[Any]=3			,     lowercase	:       str=True			,     lowercase	:       Dict=True			,     lowercase	:       int=32			,     lowercase	:       Optional[Any]=5			,     lowercase	:       Dict=4			,     lowercase	:       Any=37			,     lowercase	:       Optional[Any]="gelu"			,     lowercase	:       List[Any]=0.1			,     lowercase	:       Union[str, Any]=0.1			,     lowercase	:       List[str]=10			,     lowercase	:       str=0.02			,     lowercase	:       int=3			,     lowercase	:       Dict=0.6			,     lowercase	:       List[Any]=None			,     )						->					str:
									"""simple docstring"""







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

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





			def 		snake_case__			(				self	:       str   )						->					List[Any]:
									"""simple docstring"""







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

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

									__lowercase					=				self.get_config()

									return config, pixel_values, labels





			def 		snake_case__			(				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=lowercase			,     initializer_range=self.initializer_range			,     mask_ratio=self.mask_ratio			,     )





			def 		snake_case__			(				self	:       List[Any]			,     lowercase	:       Optional[Any]			,     lowercase	:       Union[str, Any]			,     lowercase	:       List[str]   )						->					List[str]:
									"""simple docstring"""







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





			def 		snake_case__			(				self	:       str			,     lowercase	:       List[Any]			,     lowercase	:       Union[str, Any]			,     lowercase	:       Dict   )						->					Union[str, Any]:
									"""simple docstring"""







									__lowercase					=				ViTMAEForPreTraining(lowercase   )
									model.to(lowercase   )
									model.eval()
									__lowercase					=				model(lowercase   )
									__lowercase					=				(self.image_size // self.patch_size) ** 2
									__lowercase					=				self.patch_size**2 * self.num_channels
									self.parent.assertEqual(result.logits.shape			,     (self.batch_size, num_patches, expected_num_channels)   )

									# test greyscale images
									__lowercase					=				1
									__lowercase					=				ViTMAEForPreTraining(lowercase   )
									model.to(lowercase   )
									model.eval()
									__lowercase					=				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
									__lowercase					=				model(lowercase   )
									__lowercase					=				self.patch_size**2
									self.parent.assertEqual(result.logits.shape			,     (self.batch_size, num_patches, expected_num_channels)   )







			def 		snake_case__			(				self	:       int   )						->					Optional[Any]:
									"""simple docstring"""







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





@require_torch
class 			_lowerCAmelCase   ( _UpperCAmelCase    ,					_UpperCAmelCase    ,					unittest.TestCase			):






			"""simple docstring"""





			lowercase__      :  Dict      =			(ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else ()
			lowercase__      :  str      =			{"""feature-extraction""": ViTMAEModel} if is_torch_available() else {}

			lowercase__      :  Optional[int]      =			False
			lowercase__      :  Tuple      =			False
			lowercase__      :  List[Any]      =			False
			lowercase__      :  Dict      =			False





			def 		snake_case__			(				self	:       Union[str, Any]   )						->					Union[str, Any]:
									"""simple docstring"""







									__lowercase					=				ViTMAEModelTester(self   )
									__lowercase					=				ConfigTester(self			,     config_class=lowercase			,     has_text_modality=lowercase			,     hidden_size=37   )





			def 		snake_case__			(				self	:       int   )						->					Optional[int]:
									"""simple docstring"""







									self.config_tester.run_common_tests()





			@unittest.skip(reason="""ViTMAE does not use inputs_embeds"""   )
			def 		snake_case__			(				self	:       Tuple   )						->					List[Any]:
									"""simple docstring"""







									pass





			def 		snake_case__			(				self	:       Union[str, Any]   )						->					Dict:
									"""simple docstring"""







									__lowercase	,			__lowercase					=				self.model_tester.prepare_config_and_inputs_for_common()

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





			def 		snake_case__			(				self	:       int   )						->					Optional[Any]:
									"""simple docstring"""







									__lowercase	,			__lowercase					=				self.model_tester.prepare_config_and_inputs_for_common()

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

															__lowercase					=				["""pixel_values"""]
															self.assertListEqual(arg_names[:1]			,     lowercase   )





			def 		snake_case__			(				self	:       Optional[Any]   )						->					Tuple:
									"""simple docstring"""







									__lowercase					=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowercase   )





			def 		snake_case__			(				self	:       Any   )						->					Tuple:
									"""simple docstring"""







									__lowercase					=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_pretraining(*lowercase   )





			def 		snake_case__			(				self	:       int			,     lowercase	:       Dict			,     lowercase	:       int			,     lowercase	:       List[Any]   )						->					Tuple:
									"""simple docstring"""







									np.random.seed(2   )

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

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

									super().check_pt_tf_models(lowercase			,     lowercase			,     lowercase   )





			def 		snake_case__			(				self	:       int   )						->					Any:
									"""simple docstring"""







									__lowercase	,			__lowercase					=				self.model_tester.prepare_config_and_inputs_for_common()

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

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

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

																					# Make sure we don't have nans
																					__lowercase					=				after_outputs[0].cpu().numpy()
																					__lowercase					=				0
																					__lowercase					=				np.amax(np.abs(out_a - out_a   )   )
																					self.assertLessEqual(lowercase			,     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 		snake_case__			(				self	:       str   )						->					Union[str, 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 		snake_case__			(				self	:       List[Any]   )						->					List[str]:
									"""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 		snake_case__			(				self	:       List[str]   )						->					Dict:
									"""simple docstring"""







									pass





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







									pass





			@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""   )
			def 		snake_case__			(				self	:       Dict   )						->					int:
									"""simple docstring"""







									pass







			@slow
			def 		snake_case__			(				self	:       str   )						->					Any:
									"""simple docstring"""







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






def  UpperCAmelCase__    (    ) ->      Tuple:
						__lowercase					=				Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""   )
						return image







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






			"""simple docstring"""





			@cached_property
			def 		snake_case__			(				self	:       int   )						->					Any:
									"""simple docstring"""







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







			@slow
			def 		snake_case__			(				self	:       Dict   )						->					List[str]:
									"""simple docstring"""







									np.random.seed(2   )

									__lowercase					=				ViTMAEForPreTraining.from_pretrained("""facebook/vit-mae-base"""   ).to(lowercase   )

									__lowercase					=				self.default_image_processor
									__lowercase					=				prepare_img()
									__lowercase					=				image_processor(images=lowercase			,     return_tensors="""pt"""   ).to(lowercase   )

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

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

									# verify the logits
									__lowercase					=				torch.Size((1, 196, 768)   )
									self.assertEqual(outputs.logits.shape			,     lowercase   )

									__lowercase					=				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(lowercase   )			,     atol=1E-4   )   )










import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase__ =						logging.get_logger(__name__)

UpperCamelCase__ =						{
    "asapp/sew-tiny-100k": "https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json",
    # See all SEW models at https://huggingface.co/models?filter=sew
}
class 			_lowerCAmelCase   ( _UpperCAmelCase			):






			"""simple docstring"""





			lowercase__      :  Union[str, Any]      =			"""sew"""





			def __init__(				self	:       List[Any]			,     lowercase	:       int=32			,     lowercase	:       List[str]=768			,     lowercase	:       Dict=12			,     lowercase	:       str=12			,     lowercase	:       str=3_072			,     lowercase	:       Optional[int]=2			,     lowercase	:       List[str]="gelu"			,     lowercase	:       List[str]=0.1			,     lowercase	:       Tuple=0.1			,     lowercase	:       Dict=0.1			,     lowercase	:       Any=0.0			,     lowercase	:       Dict=0.1			,     lowercase	:       Optional[int]=0.1			,     lowercase	:       List[str]=0.02			,     lowercase	:       Dict=1E-5			,     lowercase	:       Tuple="group"			,     lowercase	:       int="gelu"			,     lowercase	:       Any=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)			,     lowercase	:       Optional[int]=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)			,     lowercase	:       Any=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)			,     lowercase	:       List[str]=False			,     lowercase	:       Tuple=128			,     lowercase	:       int=16			,     lowercase	:       Union[str, Any]=True			,     lowercase	:       List[str]=0.05			,     lowercase	:       Optional[int]=10			,     lowercase	:       Any=2			,     lowercase	:       Optional[Any]=0.0			,     lowercase	:       Optional[Any]=10			,     lowercase	:       int=0			,     lowercase	:       Optional[int]="mean"			,     lowercase	:       List[Any]=False			,     lowercase	:       str=False			,     lowercase	:       int=256			,     lowercase	:       str=0			,     lowercase	:       List[Any]=1			,     lowercase	:       List[Any]=2			,     **lowercase	:       List[Any]			,     )						->					Optional[Any]:
									"""simple docstring"""







									super().__init__(**lowercase			,     pad_token_id=lowercase			,     bos_token_id=lowercase			,     eos_token_id=lowercase   )
									__lowercase					=				hidden_size
									__lowercase					=				feat_extract_norm
									__lowercase					=				feat_extract_activation
									__lowercase					=				list(lowercase   )
									__lowercase					=				list(lowercase   )
									__lowercase					=				list(lowercase   )
									__lowercase					=				conv_bias
									__lowercase					=				num_conv_pos_embeddings
									__lowercase					=				num_conv_pos_embedding_groups
									__lowercase					=				len(self.conv_dim   )
									__lowercase					=				num_hidden_layers
									__lowercase					=				intermediate_size
									__lowercase					=				squeeze_factor
									__lowercase					=				hidden_act
									__lowercase					=				num_attention_heads
									__lowercase					=				hidden_dropout
									__lowercase					=				attention_dropout
									__lowercase					=				activation_dropout
									__lowercase					=				feat_proj_dropout
									__lowercase					=				final_dropout
									__lowercase					=				layerdrop
									__lowercase					=				layer_norm_eps
									__lowercase					=				initializer_range
									__lowercase					=				vocab_size

									if (
									    (len(self.conv_stride   ) != self.num_feat_extract_layers)
									    or (len(self.conv_kernel   ) != self.num_feat_extract_layers)
									    or (len(self.conv_dim   ) != self.num_feat_extract_layers)
									):
															raise ValueError(
															    """Configuration for convolutional layers is incorrect."""
															    """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,"""
															    F"but is `len(config.conv_dim) = {len(self.conv_dim   )}`, `len(config.conv_stride)"
															    F"= {len(self.conv_stride   )}`, `len(config.conv_kernel) = {len(self.conv_kernel   )}`."   )

									# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
									__lowercase					=				apply_spec_augment
									__lowercase					=				mask_time_prob
									__lowercase					=				mask_time_length
									__lowercase					=				mask_time_min_masks
									__lowercase					=				mask_feature_prob
									__lowercase					=				mask_feature_length
									__lowercase					=				mask_feature_min_masks

									# ctc loss
									__lowercase					=				ctc_loss_reduction
									__lowercase					=				ctc_zero_infinity

									# sequence classification
									__lowercase					=				use_weighted_layer_sum
									__lowercase					=				classifier_proj_size







			@property
			def 		snake_case__			(				self	:       Dict   )						->					str:
									"""simple docstring"""







									return functools.reduce(operator.mul			,     self.conv_stride			,     1   )





