


import collections
import inspect
import unittest

from transformers import FocalNetConfig
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_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch
			from torch import nn

			from transformers import (
			    FocalNetBackbone,
			    FocalNetForImageClassification,
			    FocalNetForMaskedImageModeling,
			    FocalNetModel,
			)
			from transformers.models.focalnet.modeling_focalnet import FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
			from PIL import Image

			from transformers import AutoImageProcessor

class 							a						:




							"""simple docstring"""


							def __init__(				self			:    str       ,      lowerCamelCase			:    List[str]       ,      lowerCamelCase			:    List[Any]=13       ,      lowerCamelCase			:    Union[str, Any]=32       ,      lowerCamelCase			:    Optional[Any]=2       ,      lowerCamelCase			:    Any=3       ,      lowerCamelCase			:    Union[str, Any]=16       ,      lowerCamelCase			:    List[Any]=[32, 64, 128]       ,      lowerCamelCase			:    Optional[Any]=[1, 2, 1]       ,      lowerCamelCase			:    str=[2, 2, 4]       ,      lowerCamelCase			:    str=2       ,      lowerCamelCase			:    str=2.0       ,      lowerCamelCase			:    Optional[int]=True       ,      lowerCamelCase			:    Optional[int]=0.0       ,      lowerCamelCase			:    Dict=0.0       ,      lowerCamelCase			:    Union[str, Any]=0.1       ,      lowerCamelCase			:    List[str]="gelu"       ,      lowerCamelCase			:    str=False       ,      lowerCamelCase			:    Tuple=True       ,      lowerCamelCase			:    int=0.02       ,      lowerCamelCase			:    Optional[Any]=1E-5       ,      lowerCamelCase			:    str=True       ,      lowerCamelCase			:    Optional[Any]=None       ,      lowerCamelCase			:    int=True       ,      lowerCamelCase			:    Tuple=10       ,      lowerCamelCase			:    int=8       ,      lowerCamelCase			:    Any=["stage1", "stage2"]       ,      lowerCamelCase			:    Optional[int]=[1, 2]       ,      )      ->       Optional[Any]:
														__snake_case						:     Optional[Any]									=  parent
														__snake_case						:     List[str]									=  batch_size
														__snake_case						:     List[str]									=  image_size
														__snake_case						:     List[Any]									=  patch_size
														__snake_case						:     str									=  num_channels
														__snake_case						:     Any									=  embed_dim
														__snake_case						:     List[Any]									=  hidden_sizes
														__snake_case						:     List[Any]									=  depths
														__snake_case						:     Union[str, Any]									=  num_heads
														__snake_case						:     int									=  window_size
														__snake_case						:     Any									=  mlp_ratio
														__snake_case						:     int									=  qkv_bias
														__snake_case						:     Dict									=  hidden_dropout_prob
														__snake_case						:     Tuple									=  attention_probs_dropout_prob
														__snake_case						:     Optional[int]									=  drop_path_rate
														__snake_case						:     Tuple									=  hidden_act
														__snake_case						:     str									=  use_absolute_embeddings
														__snake_case						:     List[Any]									=  patch_norm
														__snake_case						:     Union[str, Any]									=  layer_norm_eps
														__snake_case						:     Dict									=  initializer_range
														__snake_case						:     Union[str, Any]									=  is_training
														__snake_case						:     Dict									=  scope
														__snake_case						:     Union[str, Any]									=  use_labels
														__snake_case						:     List[Any]									=  type_sequence_label_size
														__snake_case						:     str									=  encoder_stride
														__snake_case						:     str									=  out_features
														__snake_case						:     Any									=  out_indices

							def        __snake_case   (				self			:    Optional[int]							)      ->       Tuple:
														__snake_case						:     List[str]									=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

														__snake_case						:     Dict									=  self.get_config()

														return config, pixel_values, labels

							def        __snake_case   (				self			:    Union[str, Any]							)      ->       Tuple:
														return FocalNetConfig(
														    image_size=self.image_size       ,      patch_size=self.patch_size       ,      num_channels=self.num_channels       ,      embed_dim=self.embed_dim       ,      hidden_sizes=self.hidden_sizes       ,      depths=self.depths       ,      num_heads=self.num_heads       ,      window_size=self.window_size       ,      mlp_ratio=self.mlp_ratio       ,      qkv_bias=self.qkv_bias       ,      hidden_dropout_prob=self.hidden_dropout_prob       ,      attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,      drop_path_rate=self.drop_path_rate       ,      hidden_act=self.hidden_act       ,      use_absolute_embeddings=self.use_absolute_embeddings       ,      path_norm=self.patch_norm       ,      layer_norm_eps=self.layer_norm_eps       ,      initializer_range=self.initializer_range       ,      encoder_stride=self.encoder_stride       ,      out_features=self.out_features       ,      out_indices=self.out_indices       ,      )

							def        __snake_case   (				self			:    str       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    str       ,      lowerCamelCase			:    Dict							)      ->       List[Any]:
														__snake_case						:     int									=  FocalNetModel(config=lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.eval()
														__snake_case						:     Dict									=  model(lowerCamelCase							)

														__snake_case						:     List[Any]									=  ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths							) - 1))
														__snake_case						:     List[str]									=  int(config.embed_dim * 2 ** (len(config.depths							) - 1)							)

														self.parent.assertEqual(result.last_hidden_state.shape       ,      (self.batch_size, expected_seq_len, expected_dim)							)

							def        __snake_case   (				self			:    List[str]       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    Optional[int]       ,      lowerCamelCase			:    Optional[Any]							)      ->       Optional[Any]:
														__snake_case						:     Union[str, Any]									=  FocalNetBackbone(config=lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.eval()
														__snake_case						:     List[Any]									=  model(lowerCamelCase							)

														# verify feature maps
														self.parent.assertEqual(len(result.feature_maps							)       ,      len(config.out_features							)							)
														self.parent.assertListEqual(list(result.feature_maps[0].shape							)       ,      [self.batch_size, self.image_size, 8, 8]							)

														# verify channels
														self.parent.assertEqual(len(model.channels							)       ,      len(config.out_features							)							)
														self.parent.assertListEqual(model.channels       ,      config.hidden_sizes[:-1]							)

														# verify backbone works with out_features=None
														__snake_case						:     Any									=  None
														__snake_case						:     Optional[int]									=  FocalNetBackbone(config=lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.eval()
														__snake_case						:     List[Any]									=  model(lowerCamelCase							)

														# verify feature maps
														self.parent.assertEqual(len(result.feature_maps							)       ,      1							)
														self.parent.assertListEqual(list(result.feature_maps[0].shape							)       ,      [self.batch_size, self.image_size * 2, 4, 4]							)

														# verify channels
														self.parent.assertEqual(len(model.channels							)       ,      1							)
														self.parent.assertListEqual(model.channels       ,      [config.hidden_sizes[-1]]							)

							def        __snake_case   (				self			:    List[str]       ,      lowerCamelCase			:    int       ,      lowerCamelCase			:    int       ,      lowerCamelCase			:    int							)      ->       List[str]:
														__snake_case						:     int									=  FocalNetForMaskedImageModeling(config=lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.eval()
														__snake_case						:     Any									=  model(lowerCamelCase							)
														self.parent.assertEqual(
														    result.reconstruction.shape       ,      (self.batch_size, self.num_channels, self.image_size, self.image_size)							)

														# test greyscale images
														__snake_case						:     Optional[int]									=  1
														__snake_case						:     Tuple									=  FocalNetForMaskedImageModeling(lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.eval()

														__snake_case						:     Dict									=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
														__snake_case						:     Tuple									=  model(lowerCamelCase							)
														self.parent.assertEqual(result.reconstruction.shape       ,      (self.batch_size, 1, self.image_size, self.image_size)							)

							def        __snake_case   (				self			:    Any       ,      lowerCamelCase			:    Dict       ,      lowerCamelCase			:    Dict       ,      lowerCamelCase			:    Optional[Any]							)      ->       List[str]:
														__snake_case						:     Optional[Any]									=  self.type_sequence_label_size
														__snake_case						:     Union[str, Any]									=  FocalNetForImageClassification(lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.eval()
														__snake_case						:     Dict									=  model(lowerCamelCase       ,      labels=lowerCamelCase							)
														self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.type_sequence_label_size)							)

														# test greyscale images
														__snake_case						:     Dict									=  1
														__snake_case						:     int									=  FocalNetForImageClassification(lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.eval()

														__snake_case						:     Optional[int]									=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
														__snake_case						:     int									=  model(lowerCamelCase							)
														self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.type_sequence_label_size)							)






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

														__snake_case					,     __snake_case					,     __snake_case						:     Union[str, Any]									=  config_and_inputs
														__snake_case						:     List[Any]									=  {"pixel_values": pixel_values}
														return config, inputs_dict

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




							"""simple docstring"""


							__UpperCAmelCase       :				Any	    =    (
							    (
							        FocalNetModel,
							        FocalNetForImageClassification,
							        FocalNetForMaskedImageModeling,
							        FocalNetBackbone,
							    )
							    if is_torch_available()
							    else ()
							)
							__UpperCAmelCase       :				int	    =    (
							    {"feature-extraction": FocalNetModel, "image-classification": FocalNetForImageClassification}
							    if is_torch_available()
							    else {}
							)
							__UpperCAmelCase       :				Optional[Any]	    =    False

							__UpperCAmelCase       :				Any	    =    False
							__UpperCAmelCase       :				Optional[Any]	    =    False
							__UpperCAmelCase       :				Any	    =    False
							__UpperCAmelCase       :				Optional[int]	    =    False

							def        __snake_case   (				self			:    Optional[Any]							)      ->       List[str]:
														__snake_case						:     Optional[int]									=  FocalNetModelTester(self							)
														__snake_case						:     Tuple									=  ConfigTester(self       ,      config_class=lowerCamelCase       ,      embed_dim=37       ,      has_text_modality=lowerCamelCase							)

							def        __snake_case   (				self			:    str							)      ->       str:
														self.create_and_test_config_common_properties()
														self.config_tester.create_and_test_config_to_json_string()
														self.config_tester.create_and_test_config_to_json_file()
														self.config_tester.create_and_test_config_from_and_save_pretrained()
														self.config_tester.create_and_test_config_with_num_labels()
														self.config_tester.check_config_can_be_init_without_params()
														self.config_tester.check_config_arguments_init()

							def        __snake_case   (				self			:    List[str]							)      ->       Any:
														return

							def        __snake_case   (				self			:    int							)      ->       str:
														__snake_case						:     Union[str, Any]									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCamelCase							)

							def        __snake_case   (				self			:    Dict							)      ->       List[str]:
														__snake_case						:     int									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_backbone(*lowerCamelCase							)

							def        __snake_case   (				self			:    str							)      ->       str:
														__snake_case						:     List[str]									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase							)

							def        __snake_case   (				self			:    Tuple							)      ->       Any:
														__snake_case						:     Dict									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*lowerCamelCase							)

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

							@unittest.skip(reason="FocalNet does not use feedforward chunking"							)
							def        __snake_case   (				self			:    List[Any]							)      ->       List[str]:
														pass

							def        __snake_case   (				self			:    str							)      ->       Optional[int]:
														__snake_case					,     __snake_case						:     str									=  self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes[:-1]:
																					__snake_case						:     int									=  model_class(lowerCamelCase							)
																					self.assertIsInstance(model.get_input_embeddings()       ,      (nn.Module)							)
																					__snake_case						:     Optional[Any]									=  model.get_output_embeddings()
																					self.assertTrue(x is None or isinstance(lowerCamelCase       ,      nn.Linear							)							)

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

														for model_class in self.all_model_classes[:-1]:
																					__snake_case						:     Any									=  model_class(lowerCamelCase							)
																					__snake_case						:     Optional[int]									=  inspect.signature(model.forward							)
																					# signature.parameters is an OrderedDict => so arg_names order is deterministic
																					__snake_case						:     Union[str, Any]									=  [*signature.parameters.keys()]

																					__snake_case						:     str									=  ["pixel_values"]
																					self.assertListEqual(arg_names[:1]       ,      lowerCamelCase							)

							def        __snake_case   (				self			:    str       ,      lowerCamelCase			:    List[Any]       ,      lowerCamelCase			:    List[Any]       ,      lowerCamelCase			:    int       ,      lowerCamelCase			:    Union[str, Any]							)      ->       Any:
														__snake_case						:     Tuple									=  model_class(lowerCamelCase							)
														model.to(lowerCamelCase							)
														model.eval()

														with torch.no_grad():
																					__snake_case						:     Tuple									=  model(**self._prepare_for_class(lowerCamelCase       ,      lowerCamelCase							)							)

														__snake_case						:     Optional[int]									=  outputs.hidden_states

														__snake_case						:     Tuple									=  getattr(
														    self.model_tester       ,      "expected_num_hidden_layers"       ,      len(self.model_tester.depths							) + 1							)
														self.assertEqual(len(lowerCamelCase							)       ,      lowerCamelCase							)

														# FocalNet has a different seq_length
														__snake_case						:     Tuple									=  (
														    config.patch_size
														    if isinstance(config.patch_size       ,      collections.abc.Iterable							)
														    else (config.patch_size, config.patch_size)
														)

														__snake_case						:     Optional[Any]									=  (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

														self.assertListEqual(
														    list(hidden_states[0].shape[-2:]							)       ,      [num_patches, self.model_tester.embed_dim]       ,      )

														__snake_case						:     Tuple									=  outputs.reshaped_hidden_states
														self.assertEqual(len(lowerCamelCase							)       ,      lowerCamelCase							)

														__snake_case					,     __snake_case					,     __snake_case					,     __snake_case						:     Tuple									=  reshaped_hidden_states[0].shape
														__snake_case						:     str									=  (
														    reshaped_hidden_states[0].view(lowerCamelCase       ,      lowerCamelCase       ,      height * width							).permute(0       ,      2       ,      1							)
														)
														self.assertListEqual(
														    list(reshaped_hidden_states.shape[-2:]							)       ,      [num_patches, self.model_tester.embed_dim]       ,      )

							def        __snake_case   (				self			:    Union[str, Any]							)      ->       Optional[Any]:
														__snake_case					,     __snake_case						:     Any									=  self.model_tester.prepare_config_and_inputs_for_common()

														__snake_case						:     int									=  (
														    self.model_tester.image_size
														    if isinstance(self.model_tester.image_size       ,      collections.abc.Iterable							)
														    else (self.model_tester.image_size, self.model_tester.image_size)
														)

														for model_class in self.all_model_classes[:-1]:
																					__snake_case						:     Any									=  True
																					self.check_hidden_states_output(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase							)

																					# check that output_hidden_states also work using config
																					del inputs_dict["output_hidden_states"]
																					__snake_case						:     Any									=  True

																					self.check_hidden_states_output(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase							)

							def        __snake_case   (				self			:    str							)      ->       Dict:
														__snake_case					,     __snake_case						:     Union[str, Any]									=  self.model_tester.prepare_config_and_inputs_for_common()
														__snake_case						:     List[str]									=  3

														__snake_case						:     str									=  (
														    self.model_tester.image_size
														    if isinstance(self.model_tester.image_size       ,      collections.abc.Iterable							)
														    else (self.model_tester.image_size, self.model_tester.image_size)
														)
														__snake_case						:     Any									=  (
														    config.patch_size
														    if isinstance(config.patch_size       ,      collections.abc.Iterable							)
														    else (config.patch_size, config.patch_size)
														)

														__snake_case						:     Optional[Any]									=  image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
														__snake_case						:     List[str]									=  image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

														for model_class in self.all_model_classes[:-1]:
																					__snake_case						:     Union[str, Any]									=  True
																					self.check_hidden_states_output(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      (padded_height, padded_width)							)

																					# check that output_hidden_states also work using config
																					del inputs_dict["output_hidden_states"]
																					__snake_case						:     str									=  True
																					self.check_hidden_states_output(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      (padded_height, padded_width)							)

							@slow
							def        __snake_case   (				self			:    Optional[Any]							)      ->       Tuple:
														for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					__snake_case						:     Optional[Any]									=  FocalNetModel.from_pretrained(lowerCamelCase							)
																					self.assertIsNotNone(lowerCamelCase							)






							def        __snake_case   (				self			:    List[str]							)      ->       Union[str, Any]:
														__snake_case					,     __snake_case						:     int									=  self.model_tester.prepare_config_and_inputs_for_common()

														__snake_case						:     str									=  _config_zero_init(lowerCamelCase							)
														for model_class in self.all_model_classes:
																					__snake_case						:     List[str]									=  model_class(config=lowerCamelCase							)
																					for name, param in model.named_parameters():
																												if "embeddings" not in name and param.requires_grad:
																																			self.assertIn(
																																			    ((param.data.mean() * 1E9).round() / 1E9).item()       ,      [0.0, 1.0]       ,      msg=F'Parameter {name} of model {model_class} seems not properly initialized'       ,      )

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




							"""simple docstring"""


							@cached_property
							def        __snake_case   (				self			:    Dict							)      ->       Tuple:
														# TODO update organization
														return AutoImageProcessor.from_pretrained("microsoft/focalnet-tiny"							) if is_vision_available() else None






							@slow
							def        __snake_case   (				self			:    List[str]							)      ->       Tuple:
														__snake_case						:     int									=  FocalNetForImageClassification.from_pretrained("microsoft/focalnet-tiny"							).to(lowerCamelCase							)
														__snake_case						:     Optional[int]									=  self.default_image_processor

														__snake_case						:     Tuple									=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
														__snake_case						:     str									=  image_processor(images=lowerCamelCase       ,      return_tensors="pt"							).to(lowerCamelCase							)

														# forward pass
														with torch.no_grad():
																					__snake_case						:     List[Any]									=  model(**lowerCamelCase							)

														# verify the logits
														__snake_case						:     Optional[int]									=  torch.Size((1, 1000)							)
														self.assertEqual(outputs.logits.shape       ,      lowerCamelCase							)
														__snake_case						:     Any									=  torch.tensor([0.21_66, -0.43_68, 0.21_91]							).to(lowerCamelCase							)
														self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,      lowerCamelCase       ,      atol=1E-4							)							)
														self.assertTrue(outputs.logits.argmax(dim=-1							).item()       ,      281							)

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




							"""simple docstring"""


							__UpperCAmelCase       :				Optional[Any]	    =    (FocalNetBackbone,) if is_torch_available() else ()
							__UpperCAmelCase       :				Optional[int]	    =    FocalNetConfig

							__UpperCAmelCase       :				Dict	    =    False

							def        __snake_case   (				self			:    int							)      ->       Tuple:
														__snake_case						:     Tuple									=  FocalNetModelTester(self							)




"""simple docstring"""




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    :

			def __init__(       self					:    Dict     ,					__UpperCAmelCase					:    Union[str, Any]     ,					__UpperCAmelCase					:    Dict=1_3     ,					__UpperCAmelCase					:    Optional[Any]=3_0     ,					__UpperCAmelCase					:    Optional[Any]=2     ,					__UpperCAmelCase					:    Tuple=3     ,					__UpperCAmelCase					:    List[Any]=True     ,					__UpperCAmelCase					:    Optional[Any]=True     ,					__UpperCAmelCase					:    List[Any]=3_2     ,					__UpperCAmelCase					:    int=5     ,					__UpperCAmelCase					:    Union[str, Any]=4     ,					__UpperCAmelCase					:    Union[str, Any]=3_7     ,					__UpperCAmelCase					:    Optional[int]="gelu"     ,					__UpperCAmelCase					:    Dict=0.1     ,					__UpperCAmelCase					:    List[str]=0.1     ,					__UpperCAmelCase					:    Optional[Any]=1_0     ,					__UpperCAmelCase					:    List[str]=0.02     ,					__UpperCAmelCase					:    int=3     ,					__UpperCAmelCase					:    Any=0.6     ,					__UpperCAmelCase					:    Dict=None     ,					) ->     str:
						SCREAMING_SNAKE_CASE__										=   parent
						SCREAMING_SNAKE_CASE__										=   batch_size
						SCREAMING_SNAKE_CASE__										=   image_size
						SCREAMING_SNAKE_CASE__										=   patch_size
						SCREAMING_SNAKE_CASE__										=   num_channels
						SCREAMING_SNAKE_CASE__										=   is_training
						SCREAMING_SNAKE_CASE__										=   use_labels
						SCREAMING_SNAKE_CASE__										=   hidden_size
						SCREAMING_SNAKE_CASE__										=   num_hidden_layers
						SCREAMING_SNAKE_CASE__										=   num_attention_heads
						SCREAMING_SNAKE_CASE__										=   intermediate_size
						SCREAMING_SNAKE_CASE__										=   hidden_act
						SCREAMING_SNAKE_CASE__										=   hidden_dropout_prob
						SCREAMING_SNAKE_CASE__										=   attention_probs_dropout_prob
						SCREAMING_SNAKE_CASE__										=   type_sequence_label_size
						SCREAMING_SNAKE_CASE__										=   initializer_range
						SCREAMING_SNAKE_CASE__										=   mask_ratio
						SCREAMING_SNAKE_CASE__										=   scope

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

			def        SCREAMING_SNAKE_CASE					(       self					:    Tuple							) ->     str:
						SCREAMING_SNAKE_CASE__										=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

						SCREAMING_SNAKE_CASE__										=   self.get_config()

						return config, pixel_values, labels

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

			def        SCREAMING_SNAKE_CASE					(       self					:    List[Any]     ,					__UpperCAmelCase					:    Optional[int]     ,					__UpperCAmelCase					:    List[str]     ,					__UpperCAmelCase					:    Dict							) ->     Tuple:
						SCREAMING_SNAKE_CASE__										=   ViTMAEForPreTraining(__UpperCAmelCase							)
						model.to(__UpperCAmelCase							)
						model.eval()
						SCREAMING_SNAKE_CASE__										=   model(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   (self.image_size // self.patch_size) ** 2
						SCREAMING_SNAKE_CASE__										=   self.patch_size**2 * self.num_channels
						self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, num_patches, expected_num_channels)							)

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





			def        SCREAMING_SNAKE_CASE					(       self					:    str							) ->     Optional[Any]:
						SCREAMING_SNAKE_CASE__										=   self.prepare_config_and_inputs()
						SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__										=   config_and_inputs
						SCREAMING_SNAKE_CASE__										=   {"""pixel_values""": pixel_values}
						return config, inputs_dict





@require_torch
class  lowerCamelCase    (A__      ,A__      ,unittest.TestCase      ):
			lowerCamelCase__ :		Optional[int]       		=				(ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else ()
			lowerCamelCase__ :		str       		=				{'feature-extraction': ViTMAEModel} if is_torch_available() else {}

			lowerCamelCase__ :		Tuple       		=				False
			lowerCamelCase__ :		List[str]       		=				False
			lowerCamelCase__ :		str       		=				False
			lowerCamelCase__ :		List[Any]       		=				False

			def        SCREAMING_SNAKE_CASE					(       self					:    Dict							) ->     Any:
						SCREAMING_SNAKE_CASE__										=   ViTMAEModelTester(self							)
						SCREAMING_SNAKE_CASE__										=   ConfigTester(self     ,					config_class=__UpperCAmelCase     ,					has_text_modality=__UpperCAmelCase     ,					hidden_size=3_7							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Union[str, Any]							) ->     List[Any]:
						self.config_tester.run_common_tests()

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

			def        SCREAMING_SNAKE_CASE					(       self					:    List[str]							) ->     List[Any]:
						SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs_for_common()

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

			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[Any]							) ->     Optional[int]:
						SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs_for_common()

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

									SCREAMING_SNAKE_CASE__										=   ["""pixel_values"""]
									self.assertListEqual(arg_names[:1]     ,					__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Any							) ->     int:
						SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[int]							) ->     int:
						SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_pretraining(*__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    int     ,					__UpperCAmelCase					:    int     ,					__UpperCAmelCase					:    List[Any]     ,					__UpperCAmelCase					:    Optional[Any]							) ->     List[Any]:
						# make masks reproducible
						np.random.seed(2							)

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

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

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

			def        SCREAMING_SNAKE_CASE					(       self					:    List[Any]							) ->     int:
						SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__										=   self.model_tester.prepare_config_and_inputs_for_common()

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

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

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

												# Make sure we don't have nans
												SCREAMING_SNAKE_CASE__										=   after_outputs[0].cpu().numpy()
												SCREAMING_SNAKE_CASE__										=   0
												SCREAMING_SNAKE_CASE__										=   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
    to get deterministic results."""							)
			def        SCREAMING_SNAKE_CASE					(       self					:    int							) ->     Union[str, Any]:
						pass

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

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

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

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





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

def 						A							(  ):




			'''simple docstring'''


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





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

			@cached_property
			def        SCREAMING_SNAKE_CASE					(       self					:    Any							) ->     str:
						return ViTImageProcessor.from_pretrained("""facebook/vit-mae-base"""							) if is_vision_available() else None





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

						SCREAMING_SNAKE_CASE__										=   ViTMAEForPreTraining.from_pretrained("""facebook/vit-mae-base"""							).to(__UpperCAmelCase							)

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

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

						# verify the logits
						SCREAMING_SNAKE_CASE__										=   torch.Size((1, 1_9_6, 7_6_8)							)
						self.assertEqual(outputs.logits.shape     ,					__UpperCAmelCase							)

						SCREAMING_SNAKE_CASE__										=   torch.tensor(
						    [[-0.0_548, -1.7_023, -0.9_325], [0.3_721, -0.5_670, -0.2_233], [0.8_235, -1.3_878, -0.3_524]]							)

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


