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






							def __init__(					self						:			str						, snake_case__						:			Optional[int]						, snake_case__						:			Union[str, Any]=1_0_0						, snake_case__						:			Tuple=1_3						, snake_case__						:			Optional[int]=3_0						, snake_case__						:			Optional[int]=2						, snake_case__						:			Optional[Any]=3						, snake_case__						:			List[Any]=True						, snake_case__						:			Union[str, Any]=True						, snake_case__						:			Any=3_2						, snake_case__						:			Tuple=4						, snake_case__						:			Any=4						, snake_case__						:			Union[str, Any]=3_7						, snake_case__						:			List[str]="gelu"						, snake_case__						:			Dict=0.1						, snake_case__						:			List[str]=0.1						, snake_case__						:			Tuple=1_0						, snake_case__						:			List[str]=0.02						, snake_case__						:			Dict=3						, snake_case__						:			Tuple=None						, snake_case__						:			Any=[0, 1, 2, 3]						, ):






												"""simple docstring"""



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

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






							def 							__lowercase  (					self						:			str    ):






												"""simple docstring"""



												__a:						Any		    =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

												__a:						int		    =					self.get_config()

												return config, pixel_values, labels, pixel_labels






							def 							__lowercase  (					self						:			Tuple    ):






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






							def 							__lowercase  (					self						:			Optional[Any]						, snake_case__						:			Dict						, snake_case__						:			int						, snake_case__						:			int						, snake_case__						:			Optional[int]    ):






												"""simple docstring"""



												__a:						Tuple		    =					DataaVecVisionModel(config=snake_case__    )
												model.to(snake_case__    )
												model.eval()
												__a:						Optional[Any]		    =					model(snake_case__    )
												# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
												__a:						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 							__lowercase  (					self						:			List[str]						, snake_case__						:			Any						, snake_case__						:			int						, snake_case__						:			Optional[int]						, snake_case__						:			List[Any]    ):






												"""simple docstring"""



												__a:						Union[str, Any]		    =					self.type_sequence_label_size
												__a:						Tuple		    =					DataaVecVisionForImageClassification(snake_case__    )
												model.to(snake_case__    )
												model.eval()
												__a:						Tuple		    =					model(snake_case__						, labels=snake_case__    )
												self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.type_sequence_label_size)    )






							def 							__lowercase  (					self						:			List[Any]						, snake_case__						:			Tuple						, snake_case__						:			int						, snake_case__						:			List[str]						, snake_case__						:			Any    ):






												"""simple docstring"""



												__a:						int		    =					self.num_labels
												__a:						List[Any]		    =					DataaVecVisionForSemanticSegmentation(snake_case__    )
												model.to(snake_case__    )
												model.eval()
												__a:						Optional[Any]		    =					model(snake_case__    )
												self.parent.assertEqual(
												    result.logits.shape						, (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)    )
												__a:						Dict		    =					model(snake_case__						, labels=snake_case__    )
												self.parent.assertEqual(
												    result.logits.shape						, (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)    )






							def 							__lowercase  (					self						:			Any    ):






												"""simple docstring"""



												__a:						Dict		    =					self.prepare_config_and_inputs()
												__a    ,				__a    ,				__a    ,				__a:						List[Any]		    =					config_and_inputs
												__a:						Optional[Any]		    =					{"""pixel_values""": pixel_values}
												return config, inputs_dict






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

							SCREAMING_SNAKE_CASE		     =						False
							SCREAMING_SNAKE_CASE		     =						False
							SCREAMING_SNAKE_CASE		     =						False






							def 							__lowercase  (					self						:			Dict    ):






												"""simple docstring"""



												__a:						List[Any]		    =					DataaVecVisionModelTester(self    )
												__a:						Optional[int]		    =					ConfigTester(
												    self						, config_class=snake_case__						, has_text_modality=snake_case__						, hidden_size=3_7    )






							def 							__lowercase  (					self						:			int    ):






												"""simple docstring"""



												self.config_tester.run_common_tests()






							def 							__lowercase  (					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 							__lowercase  (					self						:			Any    ):






												"""simple docstring"""



												pass






							def 							__lowercase  (					self						:			int    ):






												"""simple docstring"""



												__a    ,				__a:						Optional[Any]		    =					self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																	__a:						Union[str, Any]		    =					model_class(snake_case__    )
																	self.assertIsInstance(model.get_input_embeddings()						, (nn.Module)    )
																	__a:						int		    =					model.get_output_embeddings()
																	self.assertTrue(x is None or isinstance(snake_case__						, nn.Linear    )    )






							def 							__lowercase  (					self						:			List[str]    ):






												"""simple docstring"""



												__a    ,				__a:						Any		    =					self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																	__a:						Union[str, Any]		    =					model_class(snake_case__    )
																	__a:						Optional[Any]		    =					inspect.signature(model.forward    )
																	# signature.parameters is an OrderedDict => so arg_names order is deterministic
																	__a:						Optional[Any]		    =					[*signature.parameters.keys()]

																	__a:						Union[str, Any]		    =					["""pixel_values"""]
																	self.assertListEqual(arg_names[:1]						, snake_case__    )






							def 							__lowercase  (					self						:			Union[str, Any]    ):






												"""simple docstring"""



												__a:						Union[str, Any]		    =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*snake_case__    )






							def 							__lowercase  (					self						:			Dict    ):






												"""simple docstring"""



												__a:						int		    =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_segmentation(*snake_case__    )






							def 							__lowercase  (					self						:			Tuple    ):






												"""simple docstring"""



												if not self.model_tester.is_training:
																	return

												__a    ,				__a:						Optional[int]		    =					self.model_tester.prepare_config_and_inputs_for_common()
												__a:						Dict		    =					True

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

																	__a:						Any		    =					model_class(snake_case__    )
																	model.to(snake_case__    )
																	model.train()
																	__a:						str		    =					self._prepare_for_class(snake_case__						, snake_case__						, return_labels=snake_case__    )
																	__a:						Tuple		    =					model(**snake_case__    ).loss
																	loss.backward()






							def 							__lowercase  (					self						:			Union[str, Any]    ):






												"""simple docstring"""



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

												__a:						Tuple		    =					False
												__a:						Dict		    =					True

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






							def 							__lowercase  (					self						:			Optional[Any]    ):






												"""simple docstring"""



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

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






												"""simple docstring"""



												super().check_pt_tf_outputs(snake_case__						, snake_case__						, snake_case__						, snake_case__						, snake_case__						, snake_case__    )






							def 							__lowercase  (					self						:			Union[str, Any]    ):






												"""simple docstring"""



												__a:						Dict		    =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*snake_case__    )






							@slow
							def 							__lowercase  (					self						:			List[Any]    ):






												"""simple docstring"""



												for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	__a:						str		    =					DataaVecVisionModel.from_pretrained(snake_case__    )
																	self.assertIsNotNone(snake_case__    )






def     lowerCamelCase_    (	)     ->			Dict:


					'''simple docstring'''
					__a:						List[str]		    =					Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
					return image




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






							@cached_property
							def 							__lowercase  (					self						:			Dict    ):






												"""simple docstring"""



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






							@slow
							def 							__lowercase  (					self						:			Any    ):






												"""simple docstring"""



												__a:						Optional[int]		    =					DataaVecVisionForImageClassification.from_pretrained("""facebook/data2vec-vision-base-ft1k"""    ).to(
												    snake_case__    )

												__a:						Tuple		    =					self.default_image_processor
												__a:						int		    =					prepare_img()
												__a:						Optional[Any]		    =					image_processor(images=snake_case__						, return_tensors="""pt"""    ).to(snake_case__    )

												# forward pass
												with torch.no_grad():
																	__a:						str		    =					model(**snake_case__    )
												__a:						Dict		    =					outputs.logits

												# verify the logits
												__a:						Tuple		    =					torch.Size((1, 1_0_0_0)    )
												self.assertEqual(logits.shape						, snake_case__    )

												__a:						List[str]		    =					torch.tensor([0.3_277, -0.1_395, 0.0_911]    ).to(snake_case__    )

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

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


"""simple docstring"""
from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase		:						int =					logging.get_logger(__name__)

lowercase		:						Any =					{
    'facebook/regnet-y-040': 'https://huggingface.co/facebook/regnet-y-040/blob/main/config.json',
}




class    _snake_case  (						_lowercase       ):
							SCREAMING_SNAKE_CASE		     =						'''regnet'''
							SCREAMING_SNAKE_CASE		     =						['''x''', '''y''']






							def __init__(					self						:			Union[str, Any]						, snake_case__						:			Any=3						, snake_case__						:			str=3_2						, snake_case__						:			Tuple=[1_2_8, 1_9_2, 5_1_2, 1_0_8_8]						, snake_case__						:			Union[str, Any]=[2, 6, 1_2, 2]						, snake_case__						:			str=6_4						, snake_case__						:			Optional[int]="y"						, snake_case__						:			Tuple="relu"						, **snake_case__						:			str						, ):






												"""simple docstring"""



												super().__init__(**snake_case__    )
												if layer_type not in self.layer_types:
																	raise ValueError(f"""layer_type={layer_type} is not one of {','.join(self.layer_types    )}"""    )
												__a:						Any		    =					num_channels
												__a:						List[Any]		    =					embedding_size
												__a:						Optional[int]		    =					hidden_sizes
												__a:						Optional[int]		    =					depths
												__a:						Optional[Any]		    =					groups_width
												__a:						Tuple		    =					layer_type
												__a:						str		    =					hidden_act
												# always downsample in the first stage
												__a:						List[str]		    =					True
