





import inspect
import unittest

from transformers import DataaVecVisionConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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, _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 (
	    MODEL_MAPPING,
	    DataaVecVisionForImageClassification,
	    DataaVecVisionForSemanticSegmentation,
	    DataaVecVisionModel,
	)
	from transformers.models.dataavec.modeling_dataavec_vision import DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import BeitImageProcessor




class 					UpperCamelCase_    :


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= parent
									SCREAMING_SNAKE_CASE__								= 100
									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__								= scope
									SCREAMING_SNAKE_CASE__								= out_indices
									SCREAMING_SNAKE_CASE__								= num_labels

									# in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
									SCREAMING_SNAKE_CASE__								= (image_size // patch_size) ** 2
									SCREAMING_SNAKE_CASE__								= num_patches + 1


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



									"""simple docstring"""



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

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

									SCREAMING_SNAKE_CASE__								= self.get_config()

									return config, pixel_values, labels, pixel_labels


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



									"""simple docstring"""



									return DataaVecVisionConfig(
									    vocab_size=self.vocab_size							,			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=__A							,			initializer_range=self.initializer_range							,			out_indices=self.out_indices							,			)


							def        _snake_case					(							self      :List[str]							,			__A      :Dict							,			__A      :Union[str, Any]							,			__A      :Tuple							,			__A      :Any						)				->				List[str]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= DataaVecVisionModel(config=__A						)
									model.to(__A						)
									model.eval()
									SCREAMING_SNAKE_CASE__								= model(__A						)
									# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
									SCREAMING_SNAKE_CASE__								= (self.image_size // self.patch_size) ** 2
									self.parent.assertEqual(result.last_hidden_state.shape							,			(self.batch_size, num_patches + 1, self.hidden_size)						)


							def        _snake_case					(							self      :Any							,			__A      :Any							,			__A      :List[str]							,			__A      :str							,			__A      :List[str]						)				->				Dict:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= self.type_sequence_label_size
									SCREAMING_SNAKE_CASE__								= DataaVecVisionForImageClassification(__A						)
									model.to(__A						)
									model.eval()
									SCREAMING_SNAKE_CASE__								= model(__A							,			labels=__A						)
									self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.type_sequence_label_size)						)


							def        _snake_case					(							self      :Union[str, Any]							,			__A      :List[str]							,			__A      :Dict							,			__A      :Optional[Any]							,			__A      :List[str]						)				->				str:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= self.num_labels
									SCREAMING_SNAKE_CASE__								= DataaVecVisionForSemanticSegmentation(__A						)
									model.to(__A						)
									model.eval()
									SCREAMING_SNAKE_CASE__								= model(__A						)
									self.parent.assertEqual(
									    result.logits.shape							,			(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)						)
									SCREAMING_SNAKE_CASE__								= model(__A							,			labels=__A						)
									self.parent.assertEqual(
									    result.logits.shape							,			(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)						)


							def        _snake_case					(							self      :Dict						)				->				Dict:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= self.prepare_config_and_inputs()
									SCREAMING_SNAKE_CASE__		,  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 					UpperCamelCase_    (       UpperCamelCase__			,					UpperCamelCase__			,					unittest.TestCase      ):
							lowerCamelCase_          =   (
							    (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
							    if is_torch_available()
							    else ()
							)
							lowerCamelCase_          =   (
							    {
							        "feature-extraction": DataaVecVisionModel,
							        "image-classification": DataaVecVisionForImageClassification,
							        "image-segmentation": DataaVecVisionForSemanticSegmentation,
							    }
							    if is_torch_available()
							    else {}
							)

							lowerCamelCase_          =   False
							lowerCamelCase_          =   False
							lowerCamelCase_          =   False


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= DataaVecVisionModelTester(self						)
									SCREAMING_SNAKE_CASE__								= ConfigTester(
									    self							,			config_class=__A							,			has_text_modality=__A							,			hidden_size=37						)


							def        _snake_case					(							self      :Dict						)				->				Optional[Any]:



									"""simple docstring"""



									self.config_tester.run_common_tests()


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



									"""simple docstring"""



									pass


							@require_torch_multi_gpu
							@unittest.skip(
							    reason="""Data2VecVision has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""						)
							def        _snake_case					(							self      :List[str]						)				->				List[str]:



									"""simple docstring"""



									pass


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



									"""simple docstring"""



									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(__A						)
											self.assertIsInstance(model.get_input_embeddings()							,			(nn.Module)						)
											SCREAMING_SNAKE_CASE__								= model.get_output_embeddings()
											self.assertTrue(x is None or isinstance(__A							,			nn.Linear						)						)


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



									"""simple docstring"""



									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(__A						)
											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]							,			__A						)


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__A						)


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_segmentation(*__A						)


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



									"""simple docstring"""



									if not self.model_tester.is_training:
											return

									SCREAMING_SNAKE_CASE__		,  SCREAMING_SNAKE_CASE__								= self.model_tester.prepare_config_and_inputs_for_common()
									SCREAMING_SNAKE_CASE__								= True

									for model_class in self.all_model_classes:
											if model_class in [*get_values(__A						)]:
													continue

											SCREAMING_SNAKE_CASE__								= model_class(__A						)
											model.to(__A						)
											model.train()
											SCREAMING_SNAKE_CASE__								= self._prepare_for_class(__A							,			__A							,			return_labels=__A						)
											SCREAMING_SNAKE_CASE__								= model(**__A						).loss
											loss.backward()


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__		,  SCREAMING_SNAKE_CASE__								= self.model_tester.prepare_config_and_inputs_for_common()
									if not self.model_tester.is_training:
											return

									SCREAMING_SNAKE_CASE__								= False
									SCREAMING_SNAKE_CASE__								= True

									for model_class in self.all_model_classes:
											if model_class in [*get_values(__A						)] or not model_class.supports_gradient_checkpointing:
													continue
											# TODO: remove the following 3 lines once we have a MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING
											# this can then be incorporated into _prepare_for_class in test_modeling_common.py
											elif model_class.__name__ == "Data2VecVisionForSemanticSegmentation":
													SCREAMING_SNAKE_CASE__		,  SCREAMING_SNAKE_CASE__		,  SCREAMING_SNAKE_CASE__		,  SCREAMING_SNAKE_CASE__								= inputs_dict["""pixel_values"""].shape
													SCREAMING_SNAKE_CASE__								= torch.zeros(
													    [self.model_tester.batch_size, height, width]							,			device=__A						).long()
											SCREAMING_SNAKE_CASE__								= model_class(__A						)
											model.gradient_checkpointing_enable()
											model.to(__A						)
											model.train()
											SCREAMING_SNAKE_CASE__								= self._prepare_for_class(__A							,			__A							,			return_labels=__A						)
											SCREAMING_SNAKE_CASE__								= model(**__A						).loss
											loss.backward()


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__		,  SCREAMING_SNAKE_CASE__								= self.model_tester.prepare_config_and_inputs_for_common()

									SCREAMING_SNAKE_CASE__								= _config_zero_init(__A						)
									for model_class in self.all_model_classes:
											SCREAMING_SNAKE_CASE__								= model_class(config=__A						)
											for name, param in model.named_parameters():
													# we skip lambda parameters as these require special initial values
													# determined by config.layer_scale_init_value
													if "lambda" in name:
															continue
													if 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'''							,			)


							def        _snake_case					(							self      :Tuple							,			__A      :Any							,			__A      :Union[str, Any]							,			__A      :List[str]							,			__A      :Tuple=2E-4							,			__A      :Union[str, Any]="outputs"							,			__A      :Any=None						)				->				int:



									"""simple docstring"""



									super().check_pt_tf_outputs(__A							,			__A							,			__A							,			__A							,			__A							,			__A						)


							def        _snake_case					(							self      :Any						)				->				Any:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*__A						)


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



									"""simple docstring"""



									for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											SCREAMING_SNAKE_CASE__								= DataaVecVisionModel.from_pretrained(__A						)
											self.assertIsNotNone(__A						)






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




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


							@cached_property
							def        _snake_case					(							self      :List[Any]						)				->				Optional[Any]:



									"""simple docstring"""



									return (
									    BeitImageProcessor.from_pretrained("""facebook/data2vec-vision-base-ft1k"""						) if is_vision_available() else None
									)


							@slow
							def        _snake_case					(							self      :List[Any]						)				->				int:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= DataaVecVisionForImageClassification.from_pretrained("""facebook/data2vec-vision-base-ft1k"""						).to(
									    __A						)

									SCREAMING_SNAKE_CASE__								= self.default_image_processor
									SCREAMING_SNAKE_CASE__								= prepare_img()
									SCREAMING_SNAKE_CASE__								= image_processor(images=__A							,			return_tensors="""pt"""						).to(__A						)

									# forward pass
									with torch.no_grad():
											SCREAMING_SNAKE_CASE__								= model(**__A						)
									SCREAMING_SNAKE_CASE__								= outputs.logits

									# verify the logits
									SCREAMING_SNAKE_CASE__								= torch.Size((1, 1000)						)
									self.assertEqual(logits.shape							,			__A						)

									SCREAMING_SNAKE_CASE__								= torch.tensor([0.3_2_7_7, -0.1_3_9_5, 0.0_9_1_1]						).to(__A						)

									self.assertTrue(torch.allclose(logits[0, :3]							,			__A							,			atol=1E-4						)						)

									SCREAMING_SNAKE_CASE__								= [model.config.labelaid[i] for i in ["""remote control, remote""", """tabby, tabby cat"""]]
									self.assertEqual(logits[0].topk(2						).indices.cpu().tolist()							,			__A						)






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


_lowerCamelCase							=     logging.get_logger(__name__)

_lowerCamelCase							=     {
    'MCG-NJU/videomae-base': 'https://huggingface.co/MCG-NJU/videomae-base/resolve/main/config.json',
}




class 					UpperCamelCase_    (       UpperCamelCase__      ):
							lowerCamelCase_          =   "videomae"


							def __init__(							self      :Dict							,			__A      :List[str]=224							,			__A      :List[Any]=16							,			__A      :int=3							,			__A      :Optional[Any]=16							,			__A      :Optional[int]=2							,			__A      :Optional[Any]=768							,			__A      :List[str]=12							,			__A      :Optional[Any]=12							,			__A      :List[str]=3072							,			__A      :Tuple="gelu"							,			__A      :Optional[int]=0.0							,			__A      :Union[str, Any]=0.0							,			__A      :int=0.0_2							,			__A      :Union[str, Any]=1E-12							,			__A      :Optional[Any]=True							,			__A      :Union[str, Any]=True							,			__A      :Optional[int]=6							,			__A      :Tuple=384							,			__A      :str=4							,			__A      :List[str]=1536							,			__A      :Dict=True							,			**__A      :int							,			)				->				Optional[int]:



									"""simple docstring"""



									super().__init__(**__A						)

									SCREAMING_SNAKE_CASE__								= image_size
									SCREAMING_SNAKE_CASE__								= patch_size
									SCREAMING_SNAKE_CASE__								= num_channels
									SCREAMING_SNAKE_CASE__								= num_frames
									SCREAMING_SNAKE_CASE__								= tubelet_size

									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__								= initializer_range
									SCREAMING_SNAKE_CASE__								= layer_norm_eps
									SCREAMING_SNAKE_CASE__								= qkv_bias
									SCREAMING_SNAKE_CASE__								= use_mean_pooling

									SCREAMING_SNAKE_CASE__								= decoder_num_attention_heads
									SCREAMING_SNAKE_CASE__								= decoder_hidden_size
									SCREAMING_SNAKE_CASE__								= decoder_num_hidden_layers
									SCREAMING_SNAKE_CASE__								= decoder_intermediate_size
									SCREAMING_SNAKE_CASE__								= norm_pix_loss