




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





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




				"""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 	A_(       self			)     ->				Tuple:




				"""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 	A_(       self			)     ->				List[Any]:




				"""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=lowerCamelCase__			,    initializer_range=self.initializer_range			,    out_indices=self.out_indices			,    )





	def 	A_(       self			,    SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE			)     ->				str:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_			=	DataaVecVisionModel(config=lowerCamelCase__			)
				model.to(lowerCamelCase__			)
				model.eval()
				SCREAMING_SNAKE_CASE_			=	model(lowerCamelCase__			)
				# 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 	A_(       self			,    SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE			)     ->				List[Any]:




				"""simple docstring"""



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





	def 	A_(       self			,    SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE			)     ->				int:




				"""simple docstring"""



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





	def 	A_(       self			)     ->				Optional[int]:




				"""simple docstring"""



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




@require_torch
class 			a_					(    _a     ,						_a     ,						unittest.TestCase    ):
	A						       =   (
	    (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
	    if is_torch_available()
	    else ()
	)
	A						       =   (
	    {
	        '''feature-extraction''': DataaVecVisionModel,
	        '''image-classification''': DataaVecVisionForImageClassification,
	        '''image-segmentation''': DataaVecVisionForSemanticSegmentation,
	    }
	    if is_torch_available()
	    else {}
	)

	A						       =   False
	A						       =   False
	A						       =   False





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




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_			=	DataaVecVisionModelTester(self			)
				SCREAMING_SNAKE_CASE_			=	ConfigTester(
				    self			,    config_class=lowerCamelCase__			,    has_text_modality=lowerCamelCase__			,    hidden_size=37			)





	def 	A_(       self			)     ->				Dict:




				"""simple docstring"""



				self.config_tester.run_common_tests()





	def 	A_(       self			)     ->				List[Any]:




				"""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 	A_(       self			)     ->				Optional[Any]:




				"""simple docstring"""



				pass





	def 	A_(       self			)     ->				List[Any]:




				"""simple docstring"""



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





	def 	A_(       self			)     ->				Tuple:




				"""simple docstring"""



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





	def 	A_(       self			)     ->				int:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_			=	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowerCamelCase__			)





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




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_			=	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_segmentation(*lowerCamelCase__			)





	def 	A_(       self			)     ->				Dict:




				"""simple docstring"""



				if not self.model_tester.is_training:
							return

				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(lowerCamelCase__			)]:
										continue

							SCREAMING_SNAKE_CASE_			=	model_class(lowerCamelCase__			)
							model.to(lowerCamelCase__			)
							model.train()
							SCREAMING_SNAKE_CASE_			=	self._prepare_for_class(lowerCamelCase__			,    lowerCamelCase__			,    return_labels=lowerCamelCase__			)
							SCREAMING_SNAKE_CASE_			=	model(**lowerCamelCase__			).loss
							loss.backward()





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




				"""simple docstring"""



				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(lowerCamelCase__			)] 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_			=	inputs_dict["pixel_values"].shape
										SCREAMING_SNAKE_CASE_			=	torch.zeros(
										    [self.model_tester.batch_size, height, width]			,    device=lowerCamelCase__			).long()
							SCREAMING_SNAKE_CASE_			=	model_class(lowerCamelCase__			)
							model.gradient_checkpointing_enable()
							model.to(lowerCamelCase__			)
							model.train()
							SCREAMING_SNAKE_CASE_			=	self._prepare_for_class(lowerCamelCase__			,    lowerCamelCase__			,    return_labels=lowerCamelCase__			)
							SCREAMING_SNAKE_CASE_			=	model(**lowerCamelCase__			).loss
							loss.backward()





	def 	A_(       self			)     ->				List[Any]:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_			=	self.model_tester.prepare_config_and_inputs_for_common()

				SCREAMING_SNAKE_CASE_			=	_config_zero_init(lowerCamelCase__			)
				for model_class in self.all_model_classes:
							SCREAMING_SNAKE_CASE_			=	model_class(config=lowerCamelCase__			)
							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 	A_(       self			,    SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE=2e-4			,    SCREAMING_SNAKE_CASE="outputs"			,    SCREAMING_SNAKE_CASE=None			)     ->				Tuple:




				"""simple docstring"""



				super().check_pt_tf_outputs(lowerCamelCase__			,    lowerCamelCase__			,    lowerCamelCase__			,    lowerCamelCase__			,    lowerCamelCase__			,    lowerCamelCase__			)





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




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_			=	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__			)





	@slow
	def 	A_(       self			)     ->				Any:




				"""simple docstring"""



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




def lowercase						(   ) ->       int:


			'''simple docstring'''







			SCREAMING_SNAKE_CASE_			=	Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'   )
			return image



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





	@cached_property
	def 	A_(       self			)     ->				Any:




				"""simple docstring"""



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





	@slow
	def 	A_(       self			)     ->				List[str]:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_			=	DataaVecVisionForImageClassification.from_pretrained('facebook/data2vec-vision-base-ft1k'			).to(
				    lowerCamelCase__			)

				SCREAMING_SNAKE_CASE_			=	self.default_image_processor
				SCREAMING_SNAKE_CASE_			=	prepare_img()
				SCREAMING_SNAKE_CASE_			=	image_processor(images=lowerCamelCase__			,    return_tensors='pt'			).to(lowerCamelCase__			)

				# forward pass
				with torch.no_grad():
							SCREAMING_SNAKE_CASE_			=	model(**lowerCamelCase__			)
				SCREAMING_SNAKE_CASE_			=	outputs.logits

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

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

				self.assertTrue(torch.allclose(logits[0, :3]			,    lowerCamelCase__			,    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()			,    lowerCamelCase__			)






SCREAMING_SNAKE_CASE__    :    List[str]								=					"\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n"

SCREAMING_SNAKE_CASE__    :    List[str]								=					[{"type": "code", "content": INSTALL_CONTENT}]
SCREAMING_SNAKE_CASE__    :    List[Any]								=					{
    "{processor_class}": "FakeProcessorClass",
    "{model_class}": "FakeModelClass",
    "{object_class}": "FakeObjectClass",
}
