






"""simple docstring"""
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 _lowerCAmelCase	:







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

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







						def  A			(						self       )		->       int:
										_SCREAMING_SNAKE_CASE      :		int							=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

										_SCREAMING_SNAKE_CASE      :		List[Any]							=						None
										_SCREAMING_SNAKE_CASE      :		Tuple							=						None
										if self.use_labels:
														_SCREAMING_SNAKE_CASE      :		Union[str, Any]							=						ids_tensor([self.batch_size]      ,							self.type_sequence_label_size       )
														_SCREAMING_SNAKE_CASE      :		Union[str, Any]							=						ids_tensor([self.batch_size, self.image_size, self.image_size]      ,							self.num_labels       )

										_SCREAMING_SNAKE_CASE      :		Any							=						self.get_config()

										return config, pixel_values, labels, pixel_labels







						def  A			(						self       )		->       int:
										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      ,							lowerCAmelCase_      ,							lowerCAmelCase_      ,							lowerCAmelCase_      ,							lowerCAmelCase_       )		->       Dict:
										_SCREAMING_SNAKE_CASE      :		Optional[int]							=						DataaVecVisionModel(config=lowerCAmelCase_       )
										model.to(lowerCAmelCase_       )
										model.eval()
										_SCREAMING_SNAKE_CASE      :		int							=						model(lowerCAmelCase_       )
										# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
										_SCREAMING_SNAKE_CASE      :		Tuple							=						(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      ,							lowerCAmelCase_      ,							lowerCAmelCase_      ,							lowerCAmelCase_      ,							lowerCAmelCase_       )		->       Any:
										_SCREAMING_SNAKE_CASE      :		Any							=						self.type_sequence_label_size
										_SCREAMING_SNAKE_CASE      :		Union[str, Any]							=						DataaVecVisionForImageClassification(lowerCAmelCase_       )
										model.to(lowerCAmelCase_       )
										model.eval()
										_SCREAMING_SNAKE_CASE      :		Any							=						model(lowerCAmelCase_      ,							labels=lowerCAmelCase_       )
										self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.type_sequence_label_size)       )







						def  A			(						self      ,							lowerCAmelCase_      ,							lowerCAmelCase_      ,							lowerCAmelCase_      ,							lowerCAmelCase_       )		->       Optional[int]:
										_SCREAMING_SNAKE_CASE      :		Optional[int]							=						self.num_labels
										_SCREAMING_SNAKE_CASE      :		str							=						DataaVecVisionForSemanticSegmentation(lowerCAmelCase_       )
										model.to(lowerCAmelCase_       )
										model.eval()
										_SCREAMING_SNAKE_CASE      :		Optional[Any]							=						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      :		Tuple							=						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       )		->       str:
										_SCREAMING_SNAKE_CASE      :		List[Any]							=						self.prepare_config_and_inputs()
										_SCREAMING_SNAKE_CASE		,	_SCREAMING_SNAKE_CASE		,	_SCREAMING_SNAKE_CASE		,	_SCREAMING_SNAKE_CASE      :		Optional[int]							=						config_and_inputs
										_SCREAMING_SNAKE_CASE      :		Optional[int]							=						{'pixel_values': pixel_values}
										return config, inputs_dict







@require_torch
class _lowerCAmelCase	(						__UpperCAmelCase      ,     __UpperCAmelCase      ,     unittest.TestCase					):
						SCREAMING_SNAKE_CASE_:				List[Any]   				=							(
						    (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
						    if is_torch_available()
						    else ()
						)
						SCREAMING_SNAKE_CASE_:				List[str]   				=							(
						    {
						        'feature-extraction': DataaVecVisionModel,
						        'image-classification': DataaVecVisionForImageClassification,
						        'image-segmentation': DataaVecVisionForSemanticSegmentation,
						    }
						    if is_torch_available()
						    else {}
						)

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







						def  A			(						self       )		->       Tuple:
										_SCREAMING_SNAKE_CASE      :		Tuple							=						DataaVecVisionModelTester(self       )
										_SCREAMING_SNAKE_CASE      :		str							=						ConfigTester(
										    self      ,							config_class=lowerCAmelCase_      ,							has_text_modality=lowerCAmelCase_      ,							hidden_size=3_7       )







						def  A			(						self       )		->       int:
										self.config_tester.run_common_tests()







						def  A			(						self       )		->       int:
										# Data2VecVision does not use inputs_embeds
										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       )		->       List[Any]:
										pass







						def  A			(						self       )		->       Tuple:
										_SCREAMING_SNAKE_CASE		,	_SCREAMING_SNAKE_CASE      :		Optional[Any]							=						self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														_SCREAMING_SNAKE_CASE      :		str							=						model_class(lowerCAmelCase_       )
														self.assertIsInstance(model.get_input_embeddings()      ,							(nn.Module)       )
														_SCREAMING_SNAKE_CASE      :		List[str]							=						model.get_output_embeddings()
														self.assertTrue(x is None or isinstance(lowerCAmelCase_      ,							nn.Linear       )       )







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

										for model_class in self.all_model_classes:
														_SCREAMING_SNAKE_CASE      :		Optional[Any]							=						model_class(lowerCAmelCase_       )
														_SCREAMING_SNAKE_CASE      :		List[str]							=						inspect.signature(model.forward       )
														# signature.parameters is an OrderedDict => so arg_names order is deterministic
														_SCREAMING_SNAKE_CASE      :		int							=						[*signature.parameters.keys()]

														_SCREAMING_SNAKE_CASE      :		Tuple							=						['pixel_values']
														self.assertListEqual(arg_names[:1]      ,							lowerCAmelCase_       )







						def  A			(						self       )		->       List[str]:
										_SCREAMING_SNAKE_CASE      :		int							=						self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase_       )







						def  A			(						self       )		->       Tuple:
										_SCREAMING_SNAKE_CASE      :		str							=						self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_segmentation(*lowerCAmelCase_       )







						def  A			(						self       )		->       Tuple:
										if not self.model_tester.is_training:
														return

										_SCREAMING_SNAKE_CASE		,	_SCREAMING_SNAKE_CASE      :		str							=						self.model_tester.prepare_config_and_inputs_for_common()
										_SCREAMING_SNAKE_CASE      :		Dict							=						True

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

														_SCREAMING_SNAKE_CASE      :		Tuple							=						model_class(lowerCAmelCase_       )
														model.to(lowerCAmelCase_       )
														model.train()
														_SCREAMING_SNAKE_CASE      :		Tuple							=						self._prepare_for_class(lowerCAmelCase_      ,							lowerCAmelCase_      ,							return_labels=lowerCAmelCase_       )
														_SCREAMING_SNAKE_CASE      :		str							=						model(**lowerCAmelCase_       ).loss
														loss.backward()







						def  A			(						self       )		->       int:
										_SCREAMING_SNAKE_CASE		,	_SCREAMING_SNAKE_CASE      :		Any							=						self.model_tester.prepare_config_and_inputs_for_common()
										if not self.model_tester.is_training:
														return

										_SCREAMING_SNAKE_CASE      :		Optional[Any]							=						False
										_SCREAMING_SNAKE_CASE      :		Any							=						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		,	_SCREAMING_SNAKE_CASE		,	_SCREAMING_SNAKE_CASE		,	_SCREAMING_SNAKE_CASE      :		Union[str, Any]							=						inputs_dict['pixel_values'].shape
																		_SCREAMING_SNAKE_CASE      :		Union[str, Any]							=						torch.zeros(
																		    [self.model_tester.batch_size, height, width]      ,							device=lowerCAmelCase_       ).long()
														_SCREAMING_SNAKE_CASE      :		str							=						model_class(lowerCAmelCase_       )
														model.gradient_checkpointing_enable()
														model.to(lowerCAmelCase_       )
														model.train()
														_SCREAMING_SNAKE_CASE      :		Dict							=						self._prepare_for_class(lowerCAmelCase_      ,							lowerCAmelCase_      ,							return_labels=lowerCAmelCase_       )
														_SCREAMING_SNAKE_CASE      :		List[str]							=						model(**lowerCAmelCase_       ).loss
														loss.backward()







						def  A			(						self       )		->       Any:
										_SCREAMING_SNAKE_CASE		,	_SCREAMING_SNAKE_CASE      :		Tuple							=						self.model_tester.prepare_config_and_inputs_for_common()

										_SCREAMING_SNAKE_CASE      :		List[str]							=						_config_zero_init(lowerCAmelCase_       )
										for model_class in self.all_model_classes:
														_SCREAMING_SNAKE_CASE      :		Any							=						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      ,							lowerCAmelCase_      ,							lowerCAmelCase_      ,							lowerCAmelCase_      ,							lowerCAmelCase_=2e-4      ,							lowerCAmelCase_="outputs"      ,							lowerCAmelCase_=None       )		->       Optional[Any]:
										# We override with a slightly higher tol value, as semseg models tend to diverge a bit more
										super().check_pt_tf_outputs(lowerCAmelCase_      ,							lowerCAmelCase_      ,							lowerCAmelCase_      ,							lowerCAmelCase_      ,							lowerCAmelCase_      ,							lowerCAmelCase_       )







						def  A			(						self       )		->       List[str]:
										_SCREAMING_SNAKE_CASE      :		Dict							=						self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_       )







						@slow
						def  A			(						self       )		->       Dict:
										for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_SCREAMING_SNAKE_CASE      :		int							=						DataaVecVisionModel.from_pretrained(lowerCAmelCase_       )
														self.assertIsNotNone(lowerCAmelCase_       )







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






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







						@cached_property
						def  A			(						self       )		->       List[Any]:
										return (
										    BeitImageProcessor.from_pretrained('facebook/data2vec-vision-base-ft1k'       ) if is_vision_available() else None
										)







						@slow
						def  A			(						self       )		->       Union[str, Any]:
										_SCREAMING_SNAKE_CASE      :		List[Any]							=						DataaVecVisionForImageClassification.from_pretrained('facebook/data2vec-vision-base-ft1k'       ).to(
										    lowerCAmelCase_       )

										_SCREAMING_SNAKE_CASE      :		Any							=						self.default_image_processor
										_SCREAMING_SNAKE_CASE      :		int							=						prepare_img()
										_SCREAMING_SNAKE_CASE      :		Tuple							=						image_processor(images=lowerCAmelCase_      ,							return_tensors='pt'       ).to(lowerCAmelCase_       )

										# forward pass
										with torch.no_grad():
														_SCREAMING_SNAKE_CASE      :		Tuple							=						model(**lowerCAmelCase_       )
										_SCREAMING_SNAKE_CASE      :		Any							=						outputs.logits

										# verify the logits
										_SCREAMING_SNAKE_CASE      :		int							=						torch.Size((1, 1_0_0_0)       )
										self.assertEqual(logits.shape      ,							lowerCAmelCase_       )

										_SCREAMING_SNAKE_CASE      :		Any							=						torch.tensor([0.3_277, -0.1_395, 0.0_911]       ).to(lowerCAmelCase_       )

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

										_SCREAMING_SNAKE_CASE      :		int							=						[model.config.labelaid[i] for i in ['remote control, remote', 'tabby, tabby cat']]
										self.assertEqual(logits[0].topk(2       ).indices.cpu().tolist()      ,							lowerCAmelCase_       )









"""simple docstring"""
import warnings

from ...utils import logging
from .image_processing_vit import ViTImageProcessor


lowerCAmelCase__							= logging.get_logger(__name__)


class _lowerCAmelCase	(						__UpperCAmelCase					):







						def __init__(						self      ,							*lowerCAmelCase_      ,							**lowerCAmelCase_       )		->       None:
										warnings.warn(
										    'The class ViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
										    ' use ViTImageProcessor instead.'      ,							lowerCAmelCase_      ,							)
										super().__init__(*lowerCAmelCase_      ,							**lowerCAmelCase_       )

