



"""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    A_    :



						"""simple docstring"""






						def __init__(      self				:int     ,						lowercase_				:Dict     ,						lowercase_				:Tuple=1_00     ,						lowercase_				:Optional[Any]=13     ,						lowercase_				:List[Any]=30     ,						lowercase_				:Dict=2     ,						lowercase_				:Union[str, Any]=3     ,						lowercase_				:List[str]=True     ,						lowercase_				:List[Any]=True     ,						lowercase_				:Any=32     ,						lowercase_				:Union[str, Any]=4     ,						lowercase_				:Optional[Any]=4     ,						lowercase_				:int=37     ,						lowercase_				:Tuple="gelu"     ,						lowercase_				:Dict=0.1     ,						lowercase_				:Union[str, Any]=0.1     ,						lowercase_				:str=10     ,						lowercase_				:Dict=0.02     ,						lowercase_				:Any=3     ,						lowercase_				:Tuple=None     ,						lowercase_				:Tuple=[0, 1, 2, 3]     ,						)       ->    List[Any]:
												UpperCAmelCase    =				parent
												UpperCAmelCase    =				1_00
												UpperCAmelCase    =				batch_size
												UpperCAmelCase    =				image_size
												UpperCAmelCase    =				patch_size
												UpperCAmelCase    =				num_channels
												UpperCAmelCase    =				is_training
												UpperCAmelCase    =				use_labels
												UpperCAmelCase    =				hidden_size
												UpperCAmelCase    =				num_hidden_layers
												UpperCAmelCase    =				num_attention_heads
												UpperCAmelCase    =				intermediate_size
												UpperCAmelCase    =				hidden_act
												UpperCAmelCase    =				hidden_dropout_prob
												UpperCAmelCase    =				attention_probs_dropout_prob
												UpperCAmelCase    =				type_sequence_label_size
												UpperCAmelCase    =				initializer_range
												UpperCAmelCase    =				scope
												UpperCAmelCase    =				out_indices
												UpperCAmelCase    =				num_labels

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






						def 			UpperCAmelCase__  (      self				:Dict      )       ->    Optional[Any]:
												UpperCAmelCase    =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

												UpperCAmelCase    =				self.get_config()

												return config, pixel_values, labels, pixel_labels






						def 			UpperCAmelCase__  (      self				:Any      )       ->    Dict:
												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=lowercase_     ,						initializer_range=self.initializer_range     ,						out_indices=self.out_indices     ,						)






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






						def 			UpperCAmelCase__  (      self				:Optional[Any]     ,						lowercase_				:Union[str, Any]     ,						lowercase_				:Any     ,						lowercase_				:str     ,						lowercase_				:Union[str, Any]      )       ->    str:
												UpperCAmelCase    =				self.num_labels
												UpperCAmelCase    =				DataaVecVisionForSemanticSegmentation(lowercase_      )
												model.to(lowercase_      )
												model.eval()
												UpperCAmelCase    =				model(lowercase_      )
												self.parent.assertEqual(
												    result.logits.shape     ,						(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)      )
												UpperCAmelCase    =				model(lowercase_     ,						labels=lowercase_      )
												self.parent.assertEqual(
												    result.logits.shape     ,						(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)      )






						def 			UpperCAmelCase__  (      self				:str      )       ->    int:
												UpperCAmelCase    =				self.prepare_config_and_inputs()
												UpperCAmelCase   ,					UpperCAmelCase   ,					UpperCAmelCase   ,					UpperCAmelCase    =				config_and_inputs
												UpperCAmelCase    =				{'pixel_values': pixel_values}
												return config, inputs_dict







@require_torch
class    A_    (  SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     unittest.TestCase       ):



						"""simple docstring"""
						__UpperCamelCase	       =							(
						    (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
						    if is_torch_available()
						    else ()
						)
						__UpperCamelCase	       =							(
						    {
						        """feature-extraction""": DataaVecVisionModel,
						        """image-classification""": DataaVecVisionForImageClassification,
						        """image-segmentation""": DataaVecVisionForSemanticSegmentation,
						    }
						    if is_torch_available()
						    else {}
						)

						__UpperCamelCase	       =							False
						__UpperCamelCase	       =							False
						__UpperCamelCase	       =							False






						def 			UpperCAmelCase__  (      self				:Optional[Any]      )       ->    List[Any]:
												UpperCAmelCase    =				DataaVecVisionModelTester(self      )
												UpperCAmelCase    =				ConfigTester(
												    self     ,						config_class=lowercase_     ,						has_text_modality=lowercase_     ,						hidden_size=37      )






						def 			UpperCAmelCase__  (      self				:Dict      )       ->    Optional[Any]:
												self.config_tester.run_common_tests()






						def 			UpperCAmelCase__  (      self				:Union[str, Any]      )       ->    Union[str, Any]:
												# 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 			UpperCAmelCase__  (      self				:Tuple      )       ->    Optional[int]:
												pass






						def 			UpperCAmelCase__  (      self				:Dict      )       ->    Optional[Any]:
												UpperCAmelCase   ,					UpperCAmelCase    =				self.model_tester.prepare_config_and_inputs_for_common()

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






						def 			UpperCAmelCase__  (      self				:List[str]      )       ->    Tuple:
												UpperCAmelCase   ,					UpperCAmelCase    =				self.model_tester.prepare_config_and_inputs_for_common()

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

																		UpperCAmelCase    =				['pixel_values']
																		self.assertListEqual(arg_names[:1]     ,						lowercase_      )






						def 			UpperCAmelCase__  (      self				:Optional[Any]      )       ->    List[Any]:
												UpperCAmelCase    =				self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*lowercase_      )






						def 			UpperCAmelCase__  (      self				:Dict      )       ->    Dict:
												UpperCAmelCase    =				self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_segmentation(*lowercase_      )






						def 			UpperCAmelCase__  (      self				:int      )       ->    int:
												if not self.model_tester.is_training:
																		return

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

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

																		UpperCAmelCase    =				model_class(lowercase_      )
																		model.to(lowercase_      )
																		model.train()
																		UpperCAmelCase    =				self._prepare_for_class(lowercase_     ,						lowercase_     ,						return_labels=lowercase_      )
																		UpperCAmelCase    =				model(**lowercase_      ).loss
																		loss.backward()






						def 			UpperCAmelCase__  (      self				:Tuple      )       ->    Tuple:
												UpperCAmelCase   ,					UpperCAmelCase    =				self.model_tester.prepare_config_and_inputs_for_common()
												if not self.model_tester.is_training:
																		return

												UpperCAmelCase    =				False
												UpperCAmelCase    =				True

												for model_class in self.all_model_classes:
																		if model_class in [*get_values(lowercase_      )] 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":
																								UpperCAmelCase   ,					UpperCAmelCase   ,					UpperCAmelCase   ,					UpperCAmelCase    =				inputs_dict['pixel_values'].shape
																								UpperCAmelCase    =				torch.zeros(
																								    [self.model_tester.batch_size, height, width]     ,						device=lowercase_      ).long()
																		UpperCAmelCase    =				model_class(lowercase_      )
																		model.gradient_checkpointing_enable()
																		model.to(lowercase_      )
																		model.train()
																		UpperCAmelCase    =				self._prepare_for_class(lowercase_     ,						lowercase_     ,						return_labels=lowercase_      )
																		UpperCAmelCase    =				model(**lowercase_      ).loss
																		loss.backward()






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

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






						def 			UpperCAmelCase__  (      self				:Dict      )       ->    int:
												UpperCAmelCase    =				self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*lowercase_      )






						@slow
						def 			UpperCAmelCase__  (      self				:str      )       ->    List[str]:
												for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		UpperCAmelCase    =				DataaVecVisionModel.from_pretrained(lowercase_      )
																		self.assertIsNotNone(lowercase_      )


def 				_lowerCAmelCase   (     ):
						UpperCAmelCase    =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'		)
						return image







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



						"""simple docstring"""






						@cached_property
						def 			UpperCAmelCase__  (      self				:Tuple      )       ->    int:
												return (
												    BeitImageProcessor.from_pretrained('facebook/data2vec-vision-base-ft1k'      ) if is_vision_available() else None
												)






						@slow
						def 			UpperCAmelCase__  (      self				:Optional[Any]      )       ->    Any:
												UpperCAmelCase    =				DataaVecVisionForImageClassification.from_pretrained('facebook/data2vec-vision-base-ft1k'      ).to(
												    lowercase_      )

												UpperCAmelCase    =				self.default_image_processor
												UpperCAmelCase    =				prepare_img()
												UpperCAmelCase    =				image_processor(images=lowercase_     ,						return_tensors='pt'      ).to(lowercase_      )

												# forward pass
												with torch.no_grad():
																		UpperCAmelCase    =				model(**lowercase_      )
												UpperCAmelCase    =				outputs.logits

												# verify the logits
												UpperCAmelCase    =				torch.Size((1, 10_00)      )
												self.assertEqual(logits.shape     ,						lowercase_      )

												UpperCAmelCase    =				torch.tensor([0.3277, -0.1395, 0.0911]      ).to(lowercase_      )

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

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










"""simple docstring"""


import warnings

from .generation import FlaxGenerationMixin







class    A_    (  SCREAMING_SNAKE_CASE_       ):



						"""simple docstring"""
						# warning at import time
						warnings.warn(
						    """Importing `FlaxGenerationMixin` from `src/transformers/generation_flax_utils.py` is deprecated and will """
						    """be removed in Transformers v5. Import as `from transformers import FlaxGenerationMixin` instead."""			,     SCREAMING_SNAKE_CASE_			,     )





