



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      :







					'''simple docstring'''







					def __init__(			self    ,	lowerCAmelCase    ,	lowerCAmelCase=13    ,	lowerCAmelCase=30    ,	lowerCAmelCase=2    ,	lowerCAmelCase=3    ,	lowerCAmelCase=True    ,	lowerCAmelCase=True    ,	lowerCAmelCase=32    ,	lowerCAmelCase=5    ,	lowerCAmelCase=4    ,	lowerCAmelCase=37    ,	lowerCAmelCase="gelu"    ,	lowerCAmelCase=0.1    ,	lowerCAmelCase=0.1    ,	lowerCAmelCase=10    ,	lowerCAmelCase=0.02    ,	lowerCAmelCase=3    ,	lowerCAmelCase=None    ,	lowerCAmelCase=2    ,	):
							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     ):
							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     ):
							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=lowerCAmelCase    ,	initializer_range=self.initializer_range    ,	encoder_stride=self.encoder_stride    ,	)



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     ):
							UpperCAmelCase_						=				DeiTModel(config=lowerCAmelCase     )
							model.to(lowerCAmelCase     )
							model.eval()
							UpperCAmelCase_						=				model(lowerCAmelCase     )
							self.parent.assertEqual(result.last_hidden_state.shape    ,	(self.batch_size, self.seq_length, self.hidden_size)     )



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     ):
							UpperCAmelCase_						=				DeiTForMaskedImageModeling(config=lowerCAmelCase     )
							model.to(lowerCAmelCase     )
							model.eval()
							UpperCAmelCase_						=				model(lowerCAmelCase     )
							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(lowerCAmelCase     )
							model.to(lowerCAmelCase     )
							model.eval()

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



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     ):
							UpperCAmelCase_						=				self.type_sequence_label_size
							UpperCAmelCase_						=				DeiTForImageClassification(lowerCAmelCase     )
							model.to(lowerCAmelCase     )
							model.eval()
							UpperCAmelCase_						=				model(lowerCAmelCase    ,	labels=lowerCAmelCase     )
							self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.type_sequence_label_size)     )

							# test greyscale images
							UpperCAmelCase_						=				1
							UpperCAmelCase_						=				DeiTForImageClassification(lowerCAmelCase     )
							model.to(lowerCAmelCase     )
							model.eval()

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



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





@require_torch
class 	lowerCamelCase      (						lowercase__,     lowercase__,     unittest.TestCase  ):







					'''simple docstring'''







					lowerCAmelCase_				:    Optional[int]									=  (
					    (
					        DeiTModel,
					        DeiTForImageClassification,
					        DeiTForImageClassificationWithTeacher,
					        DeiTForMaskedImageModeling,
					    )
					    if is_torch_available()
					    else ()
					)
					lowerCAmelCase_				:    Optional[Any]									=  (
					    {
					        'feature-extraction': DeiTModel,
					        'image-classification': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
					    }
					    if is_torch_available()
					    else {}
					)

					lowerCAmelCase_				:    Union[str, Any]									=  False
					lowerCAmelCase_				:    int									=  False
					lowerCAmelCase_				:    Dict									=  False



					def    A__		(			self     ):
							UpperCAmelCase_						=				DeiTModelTester(self     )
							UpperCAmelCase_						=				ConfigTester(self    ,	config_class=lowerCAmelCase    ,	has_text_modality=lowerCAmelCase    ,	hidden_size=37     )



					def    A__		(			self     ):
							self.config_tester.run_common_tests()



					@unittest.skip(reason="DeiT does not use inputs_embeds"     )
					def    A__		(			self     ):
							pass



					def    A__		(			self     ):
							UpperCAmelCase_					,						UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs_for_common()

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



					def    A__		(			self     ):
							UpperCAmelCase_					,						UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									UpperCAmelCase_						=				model_class(lowerCAmelCase     )
									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]    ,	lowerCAmelCase     )



					def    A__		(			self     ):
							UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCAmelCase     )



					def    A__		(			self     ):
							UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase     )



					def    A__		(			self     ):
							UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase     )



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase=False     ):
							UpperCAmelCase_						=				super()._prepare_for_class(lowerCAmelCase    ,	lowerCAmelCase    ,	return_labels=lowerCAmelCase     )

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

							return inputs_dict



					def    A__		(			self     ):
							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:
									# DeiTForImageClassificationWithTeacher supports inference-only
									if (
									    model_class in get_values(lowerCAmelCase     )
									    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
									):
											continue
									UpperCAmelCase_						=				model_class(lowerCAmelCase     )
									model.to(lowerCAmelCase     )
									model.train()
									UpperCAmelCase_						=				self._prepare_for_class(lowerCAmelCase    ,	lowerCAmelCase    ,	return_labels=lowerCAmelCase     )
									UpperCAmelCase_						=				model(**lowerCAmelCase     ).loss
									loss.backward()



					def    A__		(			self     ):
							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(lowerCAmelCase     ) or not model_class.supports_gradient_checkpointing:
											continue
									# DeiTForImageClassificationWithTeacher supports inference-only
									if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
											continue
									UpperCAmelCase_						=				model_class(lowerCAmelCase     )
									model.gradient_checkpointing_enable()
									model.to(lowerCAmelCase     )
									model.train()
									UpperCAmelCase_						=				self._prepare_for_class(lowerCAmelCase    ,	lowerCAmelCase    ,	return_labels=lowerCAmelCase     )
									UpperCAmelCase_						=				model(**lowerCAmelCase     ).loss
									loss.backward()



					def    A__		(			self     ):
							UpperCAmelCase_					,						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(lowerCAmelCase     ),
									        *get_values(lowerCAmelCase     ),
									    ]
									    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(lowerCAmelCase     )
													model.to(lowerCAmelCase     )
													model.train()

													UpperCAmelCase_						=				self._prepare_for_class(lowerCAmelCase    ,	lowerCAmelCase    ,	return_labels=lowerCAmelCase     )

													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=lowerCAmelCase     ) as warning_list:
															UpperCAmelCase_						=				model(**lowerCAmelCase     ).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     ):
							for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									UpperCAmelCase_						=				DeiTModel.from_pretrained(lowerCAmelCase     )
									self.assertIsNotNone(lowerCAmelCase     )







def snake_case__  (			)		->							Any:
		UpperCAmelCase_						=				Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
		return image



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







					'''simple docstring'''







					@cached_property
					def    A__		(			self     ):
							return (
							    DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224"     )
							    if is_vision_available()
							    else None
							)



					@slow
					def    A__		(			self     ):
							UpperCAmelCase_						=				DeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"     ).to(
							    lowerCAmelCase     )

							UpperCAmelCase_						=				self.default_image_processor
							UpperCAmelCase_						=				prepare_img()
							UpperCAmelCase_						=				image_processor(images=lowerCAmelCase    ,	return_tensors="pt"     ).to(lowerCAmelCase     )

							# forward pass
							with torch.no_grad():
									UpperCAmelCase_						=				model(**lowerCAmelCase     )

							# verify the logits
							UpperCAmelCase_						=				torch.Size((1, 1000)     )
							self.assertEqual(outputs.logits.shape    ,	lowerCAmelCase     )

							UpperCAmelCase_						=				torch.tensor([-1.0266, 0.1912, -1.2861]     ).to(lowerCAmelCase     )

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



					@slow
					@require_accelerate
					@require_torch_gpu
					def    A__		(			self     ):
							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=lowerCAmelCase    ,	return_tensors="pt"     )
							UpperCAmelCase_						=				inputs.pixel_values.to(lowerCAmelCase     )

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






from typing import TYPE_CHECKING

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


SCREAMING_SNAKE_CASE     				=						{
    "configuration_gpt_bigcode": ["GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP", "GPTBigCodeConfig"],
}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			SCREAMING_SNAKE_CASE     				=						[
			    "GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST",
			    "GPTBigCodeForSequenceClassification",
			    "GPTBigCodeForTokenClassification",
			    "GPTBigCodeForCausalLM",
			    "GPTBigCodeModel",
			    "GPTBigCodePreTrainedModel",
			]

if TYPE_CHECKING:
			from .configuration_gpt_bigcode import GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTBigCodeConfig

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_gpt_bigcode import (
						    GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST,
						    GPTBigCodeForCausalLM,
						    GPTBigCodeForSequenceClassification,
						    GPTBigCodeForTokenClassification,
						    GPTBigCodeModel,
						    GPTBigCodePreTrainedModel,
						)


else:
			import sys

			SCREAMING_SNAKE_CASE     				=						_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

