'''simple docstring'''







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    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE=13    ,							__SCREAMING_SNAKE_CASE=30    ,							__SCREAMING_SNAKE_CASE=2    ,							__SCREAMING_SNAKE_CASE=3    ,							__SCREAMING_SNAKE_CASE=True    ,							__SCREAMING_SNAKE_CASE=True    ,							__SCREAMING_SNAKE_CASE=32    ,							__SCREAMING_SNAKE_CASE=5    ,							__SCREAMING_SNAKE_CASE=4    ,							__SCREAMING_SNAKE_CASE=37    ,							__SCREAMING_SNAKE_CASE="gelu"    ,							__SCREAMING_SNAKE_CASE=0.1    ,							__SCREAMING_SNAKE_CASE=0.1    ,							__SCREAMING_SNAKE_CASE=10    ,							__SCREAMING_SNAKE_CASE=0.02    ,							__SCREAMING_SNAKE_CASE=3    ,							__SCREAMING_SNAKE_CASE=None    ,							__SCREAMING_SNAKE_CASE=2    ,							):


											"""simple docstring"""





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

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






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Optional[int]													=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

											lowercase_     :					Dict													=	self.get_config()

											return config, pixel_values, labels






					def      _snake_case   (       self		):


											"""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=__lowerCamelCase    ,							initializer_range=self.initializer_range    ,							encoder_stride=self.encoder_stride    ,							)






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_     :					List[Any]													=	DeiTModel(config=__lowerCamelCase		)
											model.to(__lowerCamelCase		)
											model.eval()
											lowercase_     :					Any													=	model(__lowerCamelCase		)
											self.parent.assertEqual(result.last_hidden_state.shape    ,							(self.batch_size, self.seq_length, self.hidden_size)		)






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_     :					Union[str, Any]													=	DeiTForMaskedImageModeling(config=__lowerCamelCase		)
											model.to(__lowerCamelCase		)
											model.eval()
											lowercase_     :					Any													=	model(__lowerCamelCase		)
											self.parent.assertEqual(
											    result.reconstruction.shape    ,							(self.batch_size, self.num_channels, self.image_size, self.image_size)		)

											# test greyscale images
											lowercase_     :					Any													=	1
											lowercase_     :					Optional[Any]													=	DeiTForMaskedImageModeling(__lowerCamelCase		)
											model.to(__lowerCamelCase		)
											model.eval()

											lowercase_     :					Any													=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
											lowercase_     :					List[str]													=	model(__lowerCamelCase		)
											self.parent.assertEqual(result.reconstruction.shape    ,							(self.batch_size, 1, self.image_size, self.image_size)		)






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_     :					List[str]													=	self.type_sequence_label_size
											lowercase_     :					Dict													=	DeiTForImageClassification(__lowerCamelCase		)
											model.to(__lowerCamelCase		)
											model.eval()
											lowercase_     :					int													=	model(__lowerCamelCase    ,							labels=__lowerCamelCase		)
											self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.type_sequence_label_size)		)

											# test greyscale images
											lowercase_     :					str													=	1
											lowercase_     :					List[str]													=	DeiTForImageClassification(__lowerCamelCase		)
											model.to(__lowerCamelCase		)
											model.eval()

											lowercase_     :					List[Any]													=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
											lowercase_     :					List[str]													=	model(__lowerCamelCase    ,							labels=__lowerCamelCase		)
											self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.type_sequence_label_size)		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Any													=	self.prepare_config_and_inputs()
											(
											    lowercase_				
											)     :					str													=	config_and_inputs
											lowercase_     :					Tuple													=	{"""pixel_values""": pixel_values}
											return config, inputs_dict







@require_torch
class 			lowerCAmelCase__       (  _a   ,						_a   ,						unittest.TestCase					):
					lowerCAmelCase_					       =					(
					    (
					        DeiTModel,
					        DeiTForImageClassification,
					        DeiTForImageClassificationWithTeacher,
					        DeiTForMaskedImageModeling,
					    )
					    if is_torch_available()
					    else ()
					)
					lowerCAmelCase_					       =					(
					    {
					        """feature-extraction""": DeiTModel,
					        """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
					    }
					    if is_torch_available()
					    else {}
					)

					lowerCAmelCase_					       =					False
					lowerCAmelCase_					       =					False
					lowerCAmelCase_					       =					False






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					List[Any]													=	DeiTModelTester(self		)
											lowercase_     :					int													=	ConfigTester(self    ,							config_class=__lowerCamelCase    ,							has_text_modality=__lowerCamelCase    ,							hidden_size=37		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											self.config_tester.run_common_tests()






					@unittest.skip(reason='''DeiT does not use inputs_embeds'''		)
					def      _snake_case   (       self		):


											"""simple docstring"""





											pass






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					List[Any]													=	self.model_tester.prepare_config_and_inputs_for_common()

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






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Tuple													=	self.model_tester.prepare_config_and_inputs_for_common()

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

																	lowercase_     :					Optional[int]													=	["""pixel_values"""]
																	self.assertListEqual(arg_names[:1]    ,							__lowerCamelCase		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Optional[int]													=	self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__lowerCamelCase		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					int													=	self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_image_modeling(*__lowerCamelCase		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					str													=	self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase		)






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE=False		):


											"""simple docstring"""





											lowercase_     :					Any													=	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      _snake_case   (       self		):


											"""simple docstring"""





											if not self.model_tester.is_training:
																	return

											lowercase_     :					List[str]													=	self.model_tester.prepare_config_and_inputs_for_common()
											lowercase_     :					Tuple													=	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
																	lowercase_     :					List[str]													=	model_class(__lowerCamelCase		)
																	model.to(__lowerCamelCase		)
																	model.train()
																	lowercase_     :					str													=	self._prepare_for_class(__lowerCamelCase    ,							__lowerCamelCase    ,							return_labels=__lowerCamelCase		)
																	lowercase_     :					Union[str, Any]													=	model(**__lowerCamelCase		).loss
																	loss.backward()






					def      _snake_case   (       self		):


											"""simple docstring"""





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

											lowercase_     :					int													=	False
											lowercase_     :					str													=	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
																	lowercase_     :					Union[str, Any]													=	model_class(__lowerCamelCase		)
																	model.gradient_checkpointing_enable()
																	model.to(__lowerCamelCase		)
																	model.train()
																	lowercase_     :					int													=	self._prepare_for_class(__lowerCamelCase    ,							__lowerCamelCase    ,							return_labels=__lowerCamelCase		)
																	lowercase_     :					Optional[int]													=	model(**__lowerCamelCase		).loss
																	loss.backward()






					def      _snake_case   (       self		):


											"""simple docstring"""





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

											lowercase_     :					int													=	[
											    {"""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']}'''		):
																													lowercase_     :					Any													=	problem_type["""title"""]
																													lowercase_     :					int													=	problem_type["""num_labels"""]

																													lowercase_     :					str													=	model_class(__lowerCamelCase		)
																													model.to(__lowerCamelCase		)
																													model.train()

																													lowercase_     :					List[str]													=	self._prepare_for_class(__lowerCamelCase    ,							__lowerCamelCase    ,							return_labels=__lowerCamelCase		)

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

																													lowercase_     :					Optional[Any]													=	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:
																																			lowercase_     :					Optional[int]													=	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      _snake_case   (       self		):


											"""simple docstring"""





											for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_     :					Dict													=	DeiTModel.from_pretrained(__lowerCamelCase		)
																	self.assertIsNotNone(__lowerCamelCase		)







def  snake_case_		(    ):



						"""simple docstring"""





						lowercase_     :					List[Any]													=	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
						return image






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






					@cached_property
					def      _snake_case   (       self		):


											"""simple docstring"""





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






					@slow
					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Union[str, Any]													=	DeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224'''		).to(
											    __lowerCamelCase		)

											lowercase_     :					Dict													=	self.default_image_processor
											lowercase_     :					Dict													=	prepare_img()
											lowercase_     :					Any													=	image_processor(images=__lowerCamelCase    ,							return_tensors='''pt'''		).to(__lowerCamelCase		)

											# forward pass
											with torch.no_grad():
																	lowercase_     :					Dict													=	model(**__lowerCamelCase		)

											# verify the logits
											lowercase_     :					Dict													=	torch.Size((1, 10_00)		)
											self.assertEqual(outputs.logits.shape    ,							__lowerCamelCase		)

											lowercase_     :					List[Any]													=	torch.tensor([-1.0_266, 0.1_912, -1.2_861]		).to(__lowerCamelCase		)

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






					@slow
					@require_accelerate
					@require_torch_gpu
					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Union[str, Any]													=	DeiTModel.from_pretrained(
											    '''facebook/deit-base-distilled-patch16-224'''    ,							torch_dtype=torch.floataa    ,							device_map='''auto'''		)
											lowercase_     :					Union[str, Any]													=	self.default_image_processor

											lowercase_     :					Union[str, Any]													=	prepare_img()
											lowercase_     :					Any													=	image_processor(images=__lowerCamelCase    ,							return_tensors='''pt'''		)
											lowercase_     :					List[Any]													=	inputs.pixel_values.to(__lowerCamelCase		)

											# forward pass to make sure inference works in fp16
											with torch.no_grad():
																	lowercase_     :					Tuple													=	model(__lowerCamelCase		)





'''simple docstring'''







def  snake_case_		(    __SCREAMING_SNAKE_CASE :			int				,				__SCREAMING_SNAKE_CASE :			float				,				__SCREAMING_SNAKE_CASE :			float							):



						"""simple docstring"""





						return round(float(moles / volume							) * nfactor							)
def  snake_case_		(    __SCREAMING_SNAKE_CASE :			float				,				__SCREAMING_SNAKE_CASE :			float				,				__SCREAMING_SNAKE_CASE :			float							):



						"""simple docstring"""





						return round(float((moles * 0.0821 * temperature) / (volume)							)							)
def  snake_case_		(    __SCREAMING_SNAKE_CASE :			float				,				__SCREAMING_SNAKE_CASE :			float				,				__SCREAMING_SNAKE_CASE :			float							):



						"""simple docstring"""





						return round(float((moles * 0.0821 * temperature) / (pressure)							)							)
def  snake_case_		(    __SCREAMING_SNAKE_CASE :			float				,				__SCREAMING_SNAKE_CASE :			float				,				__SCREAMING_SNAKE_CASE :			float							):



						"""simple docstring"""





						return round(float((pressure * volume) / (0.0821 * moles)							)							)


if __name__ == "__main__":
							import doctest

							doctest.testmod()



