

"""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     _UpperCAmelCase			:
			def __init__(		self      ,		_A      ,		_A=1_00      ,		_A=13      ,		_A=30      ,		_A=2      ,		_A=3      ,		_A=True      ,		_A=True      ,		_A=32      ,		_A=4      ,		_A=4      ,		_A=37      ,		_A="gelu"      ,		_A=0.1      ,		_A=0.1      ,		_A=10      ,		_A=0.02      ,		_A=3      ,		_A=None      ,		_A=[0, 1, 2, 3]      ,		)    ->    Optional[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							int									=					parent
				_UpperCAmelCase    :							Dict									=					1_00
				_UpperCAmelCase    :							Union[str, Any]									=					batch_size
				_UpperCAmelCase    :							str									=					image_size
				_UpperCAmelCase    :							List[str]									=					patch_size
				_UpperCAmelCase    :							Optional[Any]									=					num_channels
				_UpperCAmelCase    :							str									=					is_training
				_UpperCAmelCase    :							Optional[Any]									=					use_labels
				_UpperCAmelCase    :							int									=					hidden_size
				_UpperCAmelCase    :							List[Any]									=					num_hidden_layers
				_UpperCAmelCase    :							int									=					num_attention_heads
				_UpperCAmelCase    :							Tuple									=					intermediate_size
				_UpperCAmelCase    :							Optional[int]									=					hidden_act
				_UpperCAmelCase    :							Dict									=					hidden_dropout_prob
				_UpperCAmelCase    :							Any									=					attention_probs_dropout_prob
				_UpperCAmelCase    :							Any									=					type_sequence_label_size
				_UpperCAmelCase    :							str									=					initializer_range
				_UpperCAmelCase    :							List[str]									=					scope
				_UpperCAmelCase    :							List[Any]									=					out_indices
				_UpperCAmelCase    :							Union[str, Any]									=					num_labels

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






				'''simple docstring'''







				_UpperCAmelCase    :							str									=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

				_UpperCAmelCase    :							Any									=					None
				_UpperCAmelCase    :							Optional[Any]									=					None
				if self.use_labels:
					_UpperCAmelCase    :							Tuple									=					ids_tensor([self.batch_size]      ,		self.type_sequence_label_size     )
					_UpperCAmelCase    :							Tuple									=					ids_tensor([self.batch_size, self.image_size, self.image_size]      ,		self.num_labels     )

				_UpperCAmelCase    :							Optional[Any]									=					self.get_config()

				return config, pixel_values, labels, pixel_labels
			def 							__snake_case (		self     )    ->    str:






				'''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      ,		_A      ,		_A      ,		_A      ,		_A     )    ->    Tuple:






				'''simple docstring'''







				_UpperCAmelCase    :							Tuple									=					DataaVecVisionModel(config=_A     )
				model.to(_A     )
				model.eval()
				_UpperCAmelCase    :							Union[str, Any]									=					model(_A     )
				# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
				_UpperCAmelCase    :							Union[str, Any]									=					(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      ,		_A      ,		_A      ,		_A      ,		_A     )    ->    str:






				'''simple docstring'''







				_UpperCAmelCase    :							List[str]									=					self.type_sequence_label_size
				_UpperCAmelCase    :							List[Any]									=					DataaVecVisionForImageClassification(_A     )
				model.to(_A     )
				model.eval()
				_UpperCAmelCase    :							Tuple									=					model(_A      ,		labels=_A     )
				self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.type_sequence_label_size)     )
			def 							__snake_case (		self      ,		_A      ,		_A      ,		_A      ,		_A     )    ->    Optional[int]:






				'''simple docstring'''







				_UpperCAmelCase    :							int									=					self.num_labels
				_UpperCAmelCase    :							List[str]									=					DataaVecVisionForSemanticSegmentation(_A     )
				model.to(_A     )
				model.eval()
				_UpperCAmelCase    :							str									=					model(_A     )
				self.parent.assertEqual(
				    result.logits.shape      ,		(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)     )
				_UpperCAmelCase    :							Tuple									=					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     )    ->    str:






				'''simple docstring'''







				_UpperCAmelCase    :							Optional[int]									=					self.prepare_config_and_inputs()
				_UpperCAmelCase						,   _UpperCAmelCase						,   _UpperCAmelCase						,   _UpperCAmelCase    :							List[Any]									=					config_and_inputs
				_UpperCAmelCase    :							Union[str, Any]									=					{"""pixel_values""": pixel_values}
				return config, inputs_dict






@require_torch
class     _UpperCAmelCase			(		__a    ,     __a    ,     unittest.TestCase):
			__a :       List[str]									=							(
			    (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
			    if is_torch_available()
			    else ()
			)
			__a :       Dict									=							(
			    {
			        """feature-extraction""": DataaVecVisionModel,
			        """image-classification""": DataaVecVisionForImageClassification,
			        """image-segmentation""": DataaVecVisionForSemanticSegmentation,
			    }
			    if is_torch_available()
			    else {}
			)

			__a :       List[Any]									=							False
			__a :       int									=							False
			__a :       Optional[int]									=							False
			def 							__snake_case (		self     )    ->    List[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							Any									=					DataaVecVisionModelTester(self     )
				_UpperCAmelCase    :							int									=					ConfigTester(
				    self      ,		config_class=_A      ,		has_text_modality=_A      ,		hidden_size=37     )
			def 							__snake_case (		self     )    ->    List[Any]:






				'''simple docstring'''







				self.config_tester.run_common_tests()
			def 							__snake_case (		self     )    ->    Optional[int]:






				'''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]:






				'''simple docstring'''







				pass
			def 							__snake_case (		self     )    ->    str:






				'''simple docstring'''







				_UpperCAmelCase						,   _UpperCAmelCase    :							Union[str, Any]									=					self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					_UpperCAmelCase    :							Dict									=					model_class(_A     )
					self.assertIsInstance(model.get_input_embeddings()      ,		(nn.Module)     )
					_UpperCAmelCase    :							List[str]									=					model.get_output_embeddings()
					self.assertTrue(x is None or isinstance(_A      ,		nn.Linear     )     )
			def 							__snake_case (		self     )    ->    Optional[int]:






				'''simple docstring'''







				_UpperCAmelCase						,   _UpperCAmelCase    :							Optional[int]									=					self.model_tester.prepare_config_and_inputs_for_common()

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

					_UpperCAmelCase    :							str									=					["""pixel_values"""]
					self.assertListEqual(arg_names[:1]      ,		_A     )
			def 							__snake_case (		self     )    ->    Optional[int]:






				'''simple docstring'''







				_UpperCAmelCase    :							Tuple									=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_A     )
			def 							__snake_case (		self     )    ->    Any:






				'''simple docstring'''







				_UpperCAmelCase    :							Union[str, Any]									=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_segmentation(*_A     )
			def 							__snake_case (		self     )    ->    Dict:






				'''simple docstring'''







				if not self.model_tester.is_training:
					return

				_UpperCAmelCase						,   _UpperCAmelCase    :							Dict									=					self.model_tester.prepare_config_and_inputs_for_common()
				_UpperCAmelCase    :							Any									=					True

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

					_UpperCAmelCase    :							str									=					model_class(_A     )
					model.to(_A     )
					model.train()
					_UpperCAmelCase    :							Optional[int]									=					self._prepare_for_class(_A      ,		_A      ,		return_labels=_A     )
					_UpperCAmelCase    :							List[str]									=					model(**_A     ).loss
					loss.backward()
			def 							__snake_case (		self     )    ->    List[Any]:






				'''simple docstring'''







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

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






				'''simple docstring'''







				_UpperCAmelCase						,   _UpperCAmelCase    :							Union[str, Any]									=					self.model_tester.prepare_config_and_inputs_for_common()

				_UpperCAmelCase    :							Optional[int]									=					_config_zero_init(_A     )
				for model_class in self.all_model_classes:
					_UpperCAmelCase    :							int									=					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      ,		_A      ,		_A      ,		_A      ,		_A=2e-4      ,		_A="outputs"      ,		_A=None     )    ->    List[str]:






				'''simple docstring'''







				super().check_pt_tf_outputs(_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A     )
			def 							__snake_case (		self     )    ->    int:






				'''simple docstring'''







				_UpperCAmelCase    :							Any									=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*_A     )
			@slow
			def 							__snake_case (		self     )    ->    List[str]:






				'''simple docstring'''







				for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					_UpperCAmelCase    :							Dict									=					DataaVecVisionModel.from_pretrained(_A     )
					self.assertIsNotNone(_A     )
def       UpperCamelCase					(			)				->			Any:
	_UpperCAmelCase    :							str									=					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]:






				'''simple docstring'''







				return (
				    BeitImageProcessor.from_pretrained("""facebook/data2vec-vision-base-ft1k"""     ) if is_vision_available() else None
				)
			@slow
			def 							__snake_case (		self     )    ->    Tuple:






				'''simple docstring'''







				_UpperCAmelCase    :							Union[str, Any]									=					DataaVecVisionForImageClassification.from_pretrained("""facebook/data2vec-vision-base-ft1k"""     ).to(
				    _A     )

				_UpperCAmelCase    :							Union[str, Any]									=					self.default_image_processor
				_UpperCAmelCase    :							List[Any]									=					prepare_img()
				_UpperCAmelCase    :							Tuple									=					image_processor(images=_A      ,		return_tensors="""pt"""     ).to(_A     )

				# forward pass
				with torch.no_grad():
					_UpperCAmelCase    :							Tuple									=					model(**_A     )
				_UpperCAmelCase    :							Dict									=					outputs.logits

				# verify the logits
				_UpperCAmelCase    :							Union[str, Any]									=					torch.Size((1, 10_00)     )
				self.assertEqual(logits.shape      ,		_A     )

				_UpperCAmelCase    :							List[Any]									=					torch.tensor([0.3277, -0.1395, 0.0911]     ).to(_A     )

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

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








"""simple docstring"""






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowerCamelCase__				:  Union[str, Any]													=  {
    '''configuration_dpr''': ['''DPR_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DPRConfig'''],
    '''tokenization_dpr''': [
        '''DPRContextEncoderTokenizer''',
        '''DPRQuestionEncoderTokenizer''',
        '''DPRReaderOutput''',
        '''DPRReaderTokenizer''',
    ],
}


try:
						if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase__				:  List[Any]													=  [
						    '''DPRContextEncoderTokenizerFast''',
						    '''DPRQuestionEncoderTokenizerFast''',
						    '''DPRReaderTokenizerFast''',
						]

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase__				:  Dict													=  [
						    '''DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''DPRContextEncoder''',
						    '''DPRPretrainedContextEncoder''',
						    '''DPRPreTrainedModel''',
						    '''DPRPretrainedQuestionEncoder''',
						    '''DPRPretrainedReader''',
						    '''DPRQuestionEncoder''',
						    '''DPRReader''',
						]

try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase__				:  Any													=  [
						    '''TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''TFDPRContextEncoder''',
						    '''TFDPRPretrainedContextEncoder''',
						    '''TFDPRPretrainedQuestionEncoder''',
						    '''TFDPRPretrainedReader''',
						    '''TFDPRQuestionEncoder''',
						    '''TFDPRReader''',
						]


if TYPE_CHECKING:
						from .configuration_dpr import DPR_PRETRAINED_CONFIG_ARCHIVE_MAP, DPRConfig
						from .tokenization_dpr import (
						    DPRContextEncoderTokenizer,
						    DPRQuestionEncoderTokenizer,
						    DPRReaderOutput,
						    DPRReaderTokenizer,
						)

						try:
												if not is_tokenizers_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_dpr_fast import (
												    DPRContextEncoderTokenizerFast,
												    DPRQuestionEncoderTokenizerFast,
												    DPRReaderTokenizerFast,
												)

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_dpr import (
												    DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
												    DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
												    DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
												    DPRContextEncoder,
												    DPRPretrainedContextEncoder,
												    DPRPreTrainedModel,
												    DPRPretrainedQuestionEncoder,
												    DPRPretrainedReader,
												    DPRQuestionEncoder,
												    DPRReader,
												)

						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_dpr import (
												    TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TFDPRContextEncoder,
												    TFDPRPretrainedContextEncoder,
												    TFDPRPretrainedQuestionEncoder,
												    TFDPRPretrainedReader,
												    TFDPRQuestionEncoder,
												    TFDPRReader,
												)

else:
						import sys

						lowerCamelCase__				:  Optional[Any]													=  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





