
import inspect
import unittest
import warnings

from transformers import DeiTConfig
from transformers.models.auto import get_values
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch
		from torch import nn

		from transformers import (
		    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
		    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
		    MODEL_MAPPING,
		    DeiTForImageClassification,
		    DeiTForImageClassificationWithTeacher,
		    DeiTForMaskedImageModeling,
		    DeiTModel,
		)
		from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import DeiTImageProcessor





class        lowerCAmelCase__      :

						def __init__(			self       ,							a       ,							a=13       ,							a=30       ,							a=2       ,							a=3       ,							a=True       ,							a=True       ,							a=32       ,							a=5       ,							a=4       ,							a=37       ,							a="gelu"       ,							a=0.1       ,							a=0.1       ,							a=10       ,							a=0.02       ,							a=3       ,							a=None       ,							a=2       ,							)					->      Dict:


								'''simple docstring'''

								_UpperCamelCase						    =	parent
								_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						    =	encoder_stride

								# in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
								_UpperCamelCase						    =	(image_size // patch_size) ** 2
								_UpperCamelCase						    =	num_patches + 2

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


								'''simple docstring'''

								_UpperCamelCase						    =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

								_UpperCamelCase						    =	None
								if self.use_labels:
										_UpperCamelCase						    =	ids_tensor([self.batch_size]       ,							self.type_sequence_label_size				)

								_UpperCamelCase						    =	self.get_config()

								return config, pixel_values, labels

						def  A_				(			self				)					->      Dict:


								'''simple docstring'''

								return DeiTConfig(
								    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       ,							encoder_stride=self.encoder_stride       ,							)

						def  A_				(			self       ,							a       ,							a       ,							a				)					->      Union[str, Any]:


								'''simple docstring'''

								_UpperCamelCase						    =	DeiTModel(config=lowercase__				)
								model.to(lowercase__				)
								model.eval()
								_UpperCamelCase						    =	model(lowercase__				)
								self.parent.assertEqual(result.last_hidden_state.shape       ,							(self.batch_size, self.seq_length, self.hidden_size)				)

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


								'''simple docstring'''

								_UpperCamelCase						    =	DeiTForMaskedImageModeling(config=lowercase__				)
								model.to(lowercase__				)
								model.eval()
								_UpperCamelCase						    =	model(lowercase__				)
								self.parent.assertEqual(
								    result.reconstruction.shape       ,							(self.batch_size, self.num_channels, self.image_size, self.image_size)				)

								# test greyscale images
								_UpperCamelCase						    =	1
								_UpperCamelCase						    =	DeiTForMaskedImageModeling(lowercase__				)
								model.to(lowercase__				)
								model.eval()

								_UpperCamelCase						    =	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
								_UpperCamelCase						    =	model(lowercase__				)
								self.parent.assertEqual(result.reconstruction.shape       ,							(self.batch_size, 1, self.image_size, self.image_size)				)

						def  A_				(			self       ,							a       ,							a       ,							a				)					->      Any:


								'''simple docstring'''

								_UpperCamelCase						    =	self.type_sequence_label_size
								_UpperCamelCase						    =	DeiTForImageClassification(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)				)

								# test greyscale images
								_UpperCamelCase						    =	1
								_UpperCamelCase						    =	DeiTForImageClassification(lowercase__				)
								model.to(lowercase__				)
								model.eval()

								_UpperCamelCase						    =	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
								_UpperCamelCase						    =	model(lowercase__       ,							labels=lowercase__				)
								self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.type_sequence_label_size)				)







						def  A_				(			self				)					->      Any:


								'''simple docstring'''

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





@require_torch
class        lowerCAmelCase__      (   _UpperCAmelCase		,	_UpperCAmelCase		,	unittest.TestCase	):
						UpperCamelCase_							: Union[str, Any]	=		(
						    (
						        DeiTModel,
						        DeiTForImageClassification,
						        DeiTForImageClassificationWithTeacher,
						        DeiTForMaskedImageModeling,
						    )
						    if is_torch_available()
						    else ()
						)
						UpperCamelCase_							: int	=		(
						    {
						        "feature-extraction": DeiTModel,
						        "image-classification": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
						    }
						    if is_torch_available()
						    else {}
						)

						UpperCamelCase_							: Any	=		False
						UpperCamelCase_							: List[str]	=		False
						UpperCamelCase_							: Any	=		False

						def  A_				(			self				)					->      str:


								'''simple docstring'''

								_UpperCamelCase						    =	DeiTModelTester(self				)
								_UpperCamelCase						    =	ConfigTester(self       ,							config_class=lowercase__       ,							has_text_modality=lowercase__       ,							hidden_size=37				)

						def  A_				(			self				)					->      str:


								'''simple docstring'''

								self.config_tester.run_common_tests()

						@unittest.skip(reason="""DeiT does not use inputs_embeds"""				)
						def  A_				(			self				)					->      Optional[int]:


								'''simple docstring'''

								pass

						def  A_				(			self				)					->      Union[str, Any]:


								'''simple docstring'''

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


								'''simple docstring'''

								_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  A_				(			self				)					->      Union[str, Any]:


								'''simple docstring'''

								_UpperCamelCase						    =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowercase__				)

						def  A_				(			self				)					->      str:


								'''simple docstring'''

								_UpperCamelCase						    =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*lowercase__				)

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


								'''simple docstring'''

								_UpperCamelCase						    =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*lowercase__				)

						def  A_				(			self       ,							a       ,							a       ,							a=False				)					->      Tuple:


								'''simple docstring'''

								_UpperCamelCase						    =	super()._prepare_for_class(lowercase__       ,							lowercase__       ,							return_labels=lowercase__				)

								if return_labels:
										if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
												del inputs_dict["labels"]

								return inputs_dict

						def  A_				(			self				)					->      Union[str, Any]:


								'''simple docstring'''

								if not self.model_tester.is_training:
										return

								_UpperCamelCase						    =	self.model_tester.prepare_config_and_inputs_for_common()
								_UpperCamelCase						    =	True

								for model_class in self.all_model_classes:
										# DeiTForImageClassificationWithTeacher supports inference-only
										if (
										    model_class in get_values(lowercase__				)
										    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
										):
												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  A_				(			self				)					->      str:


								'''simple docstring'''

								_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
										# DeiTForImageClassificationWithTeacher supports inference-only
										if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
												continue
										_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  A_				(			self				)					->      Union[str, Any]:


								'''simple docstring'''

								_UpperCamelCase						    =	self.model_tester.prepare_config_and_inputs_for_common()

								_UpperCamelCase						    =	[
								    {"title": "multi_label_classification", "num_labels": 2, "dtype": torch.float},
								    {"title": "single_label_classification", "num_labels": 1, "dtype": torch.long},
								    {"title": "regression", "num_labels": 1, "dtype": torch.float},
								]

								for model_class in self.all_model_classes:
										if (
										    model_class
										    not in [
										        *get_values(lowercase__				),
										        *get_values(lowercase__				),
										    ]
										    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
										):
												continue

										for problem_type in problem_types:
												with self.subTest(msg=F'Testing {model_class} with {problem_type["title"]}'				):
														_UpperCamelCase						    =	problem_type["title"]
														_UpperCamelCase						    =	problem_type["num_labels"]

														_UpperCamelCase						    =	model_class(lowercase__				)
														model.to(lowercase__				)
														model.train()

														_UpperCamelCase						    =	self._prepare_for_class(lowercase__       ,							lowercase__       ,							return_labels=lowercase__				)

														if problem_type["num_labels"] > 1:
																_UpperCamelCase						    =	inputs["labels"].unsqueeze(1				).repeat(1       ,							problem_type["""num_labels"""]				)

														_UpperCamelCase						    =	inputs["labels"].to(problem_type["""dtype"""]				)

														# This tests that we do not trigger the warning form PyTorch "Using a target size that is different
														# to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
														# they have the same size." which is a symptom something in wrong for the regression problem.
														# See https://github.com/huggingface/transformers/issues/11780
														with warnings.catch_warnings(record=lowercase__				) as warning_list:
																_UpperCamelCase						    =	model(**lowercase__				).loss
														for w in warning_list:
																if "Using a target size that is different to the input size" in str(w.message				):
																		raise ValueError(
																		    F'Something is going wrong in the regression problem: intercepted {w.message}'				)

														loss.backward()







						@slow
						def  A_				(			self				)					->      Union[str, Any]:


								'''simple docstring'''

								for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										_UpperCamelCase						    =	DeiTModel.from_pretrained(lowercase__				)
										self.assertIsNotNone(lowercase__				)




def 							__A()		->				Optional[Any]:

		"""simple docstring"""






		_UpperCamelCase						    =	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
		return image







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

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


								'''simple docstring'''

								return (
								    DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224"""				)
								    if is_vision_available()
								    else None
								)

						@slow
						def  A_				(			self				)					->      Dict:


								'''simple docstring'''

								_UpperCamelCase						    =	DeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""				).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__				)

								# verify the logits
								_UpperCamelCase						    =	torch.Size((1, 10_00)				)
								self.assertEqual(outputs.logits.shape       ,							lowercase__				)

								_UpperCamelCase						    =	torch.tensor([-1.0266, 0.1912, -1.2861]				).to(lowercase__				)

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







						@slow
						@require_accelerate
						@require_torch_gpu
						def  A_				(			self				)					->      List[Any]:


								'''simple docstring'''

								_UpperCamelCase						    =	DeiTModel.from_pretrained(
								    """facebook/deit-base-distilled-patch16-224"""       ,							torch_dtype=torch.floataa       ,							device_map="""auto"""				)
								_UpperCamelCase						    =	self.default_image_processor

								_UpperCamelCase						    =	prepare_img()
								_UpperCamelCase						    =	image_processor(images=lowercase__       ,							return_tensors="""pt"""				)
								_UpperCamelCase						    =	inputs.pixel_values.to(lowercase__				)

								# forward pass to make sure inference works in fp16
								with torch.no_grad():
										_UpperCamelCase						    =	model(lowercase__				)





import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


lowerCamelCase__	=							logging.get_logger(__name__)

lowerCamelCase__	=							{
    "ut/deta": "https://huggingface.co/ut/deta/resolve/main/config.json",
}







class        lowerCAmelCase__      (   __lowercase	):
						UpperCamelCase_							: int	=		"deta"
						UpperCamelCase_							: Union[str, Any]	=		{
						    "hidden_size": "d_model",
						    "num_attention_heads": "encoder_attention_heads",
						}

						def __init__(			self       ,							a=None       ,							a=9_00       ,							a=20_48       ,							a=6       ,							a=20_48       ,							a=8       ,							a=6       ,							a=10_24       ,							a=8       ,							a=0.0       ,							a=True       ,							a="relu"       ,							a=2_56       ,							a=0.1       ,							a=0.0       ,							a=0.0       ,							a=0.02       ,							a=1.0       ,							a=True       ,							a=False       ,							a="sine"       ,							a=5       ,							a=4       ,							a=4       ,							a=True       ,							a=3_00       ,							a=True       ,							a=True       ,							a=1       ,							a=5       ,							a=2       ,							a=1       ,							a=1       ,							a=5       ,							a=2       ,							a=0.1       ,							a=0.25       ,							**a       ,							)					->      Dict:


								'''simple docstring'''

								if backbone_config is None:
										logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""				)
										_UpperCamelCase						    =	CONFIG_MAPPING["""resnet"""](out_features=["""stage2""", """stage3""", """stage4"""]				)
								else:
										if isinstance(a       ,							a				):
												_UpperCamelCase						    =	backbone_config.pop("""model_type"""				)
												_UpperCamelCase						    =	CONFIG_MAPPING[backbone_model_type]
												_UpperCamelCase						    =	config_class.from_dict(a				)

								_UpperCamelCase						    =	backbone_config
								_UpperCamelCase						    =	num_queries
								_UpperCamelCase						    =	max_position_embeddings
								_UpperCamelCase						    =	d_model
								_UpperCamelCase						    =	encoder_ffn_dim
								_UpperCamelCase						    =	encoder_layers
								_UpperCamelCase						    =	encoder_attention_heads
								_UpperCamelCase						    =	decoder_ffn_dim
								_UpperCamelCase						    =	decoder_layers
								_UpperCamelCase						    =	decoder_attention_heads
								_UpperCamelCase						    =	dropout
								_UpperCamelCase						    =	attention_dropout
								_UpperCamelCase						    =	activation_dropout
								_UpperCamelCase						    =	activation_function
								_UpperCamelCase						    =	init_std
								_UpperCamelCase						    =	init_xavier_std
								_UpperCamelCase						    =	encoder_layerdrop
								_UpperCamelCase						    =	auxiliary_loss
								_UpperCamelCase						    =	position_embedding_type
								# deformable attributes
								_UpperCamelCase						    =	num_feature_levels
								_UpperCamelCase						    =	encoder_n_points
								_UpperCamelCase						    =	decoder_n_points
								_UpperCamelCase						    =	two_stage
								_UpperCamelCase						    =	two_stage_num_proposals
								_UpperCamelCase						    =	with_box_refine
								_UpperCamelCase						    =	assign_first_stage
								if two_stage is True and with_box_refine is False:
										raise ValueError("""If two_stage is True, with_box_refine must be True."""				)
								# Hungarian matcher
								_UpperCamelCase						    =	class_cost
								_UpperCamelCase						    =	bbox_cost
								_UpperCamelCase						    =	giou_cost
								# Loss coefficients
								_UpperCamelCase						    =	mask_loss_coefficient
								_UpperCamelCase						    =	dice_loss_coefficient
								_UpperCamelCase						    =	bbox_loss_coefficient
								_UpperCamelCase						    =	giou_loss_coefficient
								_UpperCamelCase						    =	eos_coefficient
								_UpperCamelCase						    =	focal_alpha
								super().__init__(is_encoder_decoder=a       ,							**a				)

						@property
						def  A_				(			self				)					->      int:


								'''simple docstring'''

								return self.encoder_attention_heads

						@property
						def  A_				(			self				)					->      int:


								'''simple docstring'''

								return self.d_model







						def  A_				(			self				)					->      Tuple:


								'''simple docstring'''

								_UpperCamelCase						    =	copy.deepcopy(self.__dict__				)
								_UpperCamelCase						    =	self.backbone_config.to_dict()
								_UpperCamelCase						    =	self.__class__.model_type
								return output



