






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     :     int      ,		UpperCamelCase__     :     Dict      ,		UpperCamelCase__     :     List[Any]=1_3      ,		UpperCamelCase__     :     List[str]=3_0      ,		UpperCamelCase__     :     str=2      ,		UpperCamelCase__     :     Optional[Any]=3      ,		UpperCamelCase__     :     Dict=True      ,		UpperCamelCase__     :     Tuple=True      ,		UpperCamelCase__     :     Optional[int]=3_2      ,		UpperCamelCase__     :     List[str]=5      ,		UpperCamelCase__     :     Optional[int]=4      ,		UpperCamelCase__     :     int=3_7      ,		UpperCamelCase__     :     str="gelu"      ,		UpperCamelCase__     :     List[str]=0.1      ,		UpperCamelCase__     :     List[str]=0.1      ,		UpperCamelCase__     :     List[str]=1_0      ,		UpperCamelCase__     :     str=0.02      ,		UpperCamelCase__     :     Tuple=3      ,		UpperCamelCase__     :     Tuple=None      ,		UpperCamelCase__     :     Dict=2      ,		):






											'''simple docstring'''







											snake_case__							=							parent
											snake_case__							=							batch_size
											snake_case__							=							image_size
											snake_case__							=							patch_size
											snake_case__							=							num_channels
											snake_case__							=							is_training
											snake_case__							=							use_labels
											snake_case__							=							hidden_size
											snake_case__							=							num_hidden_layers
											snake_case__							=							num_attention_heads
											snake_case__							=							intermediate_size
											snake_case__							=							hidden_act
											snake_case__							=							hidden_dropout_prob
											snake_case__							=							attention_probs_dropout_prob
											snake_case__							=							type_sequence_label_size
											snake_case__							=							initializer_range
											snake_case__							=							scope
											snake_case__							=							encoder_stride

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



				def        __magic_name__				(   self     :     str):






											'''simple docstring'''







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

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

											snake_case__							=							self.get_config()

											return config, pixel_values, labels



				def        __magic_name__				(   self     :     Tuple):






											'''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=_SCREAMING_SNAKE_CASE      ,		initializer_range=self.initializer_range      ,		encoder_stride=self.encoder_stride      ,		)



				def        __magic_name__				(   self     :     List[Any]      ,		UpperCamelCase__     :     str      ,		UpperCamelCase__     :     str      ,		UpperCamelCase__     :     Optional[int]):






											'''simple docstring'''







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



				def        __magic_name__				(   self     :     List[Any]      ,		UpperCamelCase__     :     List[str]      ,		UpperCamelCase__     :     int      ,		UpperCamelCase__     :     Optional[int]):






											'''simple docstring'''







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

											# test greyscale images
											snake_case__							=							1
											snake_case__							=							DeiTForMaskedImageModeling(_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()

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



				def        __magic_name__				(   self     :     str      ,		UpperCamelCase__     :     Tuple      ,		UpperCamelCase__     :     List[Any]      ,		UpperCamelCase__     :     str):






											'''simple docstring'''







											snake_case__							=							self.type_sequence_label_size
											snake_case__							=							DeiTForImageClassification(_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()
											snake_case__							=							model(_SCREAMING_SNAKE_CASE      ,		labels=_SCREAMING_SNAKE_CASE)
											self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.type_sequence_label_size))

											# test greyscale images
											snake_case__							=							1
											snake_case__							=							DeiTForImageClassification(_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()

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



				def        __magic_name__				(   self     :     Optional[int]):






											'''simple docstring'''







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


@require_torch
class  _lowerCAmelCase      (		lowercase_  ,   lowercase_  ,   unittest.TestCase   ):



				"""simple docstring"""



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

				_lowercase					: Any	     =	False
				_lowercase					: Dict	     =	False
				_lowercase					: List[str]	     =	False



				def        __magic_name__				(   self     :     List[str]):






											'''simple docstring'''







											snake_case__							=							DeiTModelTester(self)
											snake_case__							=							ConfigTester(self      ,		config_class=_SCREAMING_SNAKE_CASE      ,		has_text_modality=_SCREAMING_SNAKE_CASE      ,		hidden_size=3_7)



				def        __magic_name__				(   self     :     Optional[Any]):






											'''simple docstring'''







											self.config_tester.run_common_tests()



				@unittest.skip(reason="""DeiT does not use inputs_embeds""")
				def        __magic_name__				(   self     :     List[Any]):






											'''simple docstring'''







											pass



				def        __magic_name__				(   self     :     List[Any]):






											'''simple docstring'''







											snake_case__	,			snake_case__							=							self.model_tester.prepare_config_and_inputs_for_common()

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



				def        __magic_name__				(   self     :     int):






											'''simple docstring'''







											snake_case__	,			snake_case__							=							self.model_tester.prepare_config_and_inputs_for_common()

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

																		snake_case__							=							["""pixel_values"""]
																		self.assertListEqual(arg_names[:1]      ,		_SCREAMING_SNAKE_CASE)



				def        __magic_name__				(   self     :     int):






											'''simple docstring'''







											snake_case__							=							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE)



				def        __magic_name__				(   self     :     Optional[int]):






											'''simple docstring'''







											snake_case__							=							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_image_modeling(*_SCREAMING_SNAKE_CASE)



				def        __magic_name__				(   self     :     Optional[int]):






											'''simple docstring'''







											snake_case__							=							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE)



				def        __magic_name__				(   self     :     Dict      ,		UpperCamelCase__     :     Dict      ,		UpperCamelCase__     :     Union[str, Any]      ,		UpperCamelCase__     :     Any=False):






											'''simple docstring'''







											snake_case__							=							super()._prepare_for_class(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		return_labels=_SCREAMING_SNAKE_CASE)

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

											return inputs_dict



				def        __magic_name__				(   self     :     List[Any]):






											'''simple docstring'''







											if not self.model_tester.is_training:
																		return

											snake_case__	,			snake_case__							=							self.model_tester.prepare_config_and_inputs_for_common()
											snake_case__							=							True

											for model_class in self.all_model_classes:
																		# DeiTForImageClassificationWithTeacher supports inference-only
																		if (
																		    model_class in get_values(_SCREAMING_SNAKE_CASE)
																		    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
																		):
																									continue
																		snake_case__							=							model_class(_SCREAMING_SNAKE_CASE)
																		model.to(_SCREAMING_SNAKE_CASE)
																		model.train()
																		snake_case__							=							self._prepare_for_class(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		return_labels=_SCREAMING_SNAKE_CASE)
																		snake_case__							=							model(**_SCREAMING_SNAKE_CASE).loss
																		loss.backward()



				def        __magic_name__				(   self     :     str):






											'''simple docstring'''







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

											snake_case__							=							False
											snake_case__							=							True

											for model_class in self.all_model_classes:
																		if model_class in get_values(_SCREAMING_SNAKE_CASE) or not model_class.supports_gradient_checkpointing:
																									continue
																		# DeiTForImageClassificationWithTeacher supports inference-only
																		if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
																									continue
																		snake_case__							=							model_class(_SCREAMING_SNAKE_CASE)
																		model.gradient_checkpointing_enable()
																		model.to(_SCREAMING_SNAKE_CASE)
																		model.train()
																		snake_case__							=							self._prepare_for_class(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		return_labels=_SCREAMING_SNAKE_CASE)
																		snake_case__							=							model(**_SCREAMING_SNAKE_CASE).loss
																		loss.backward()



				def        __magic_name__				(   self     :     Dict):






											'''simple docstring'''







											snake_case__	,			snake_case__							=							self.model_tester.prepare_config_and_inputs_for_common()

											snake_case__							=							[
											    {"""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(_SCREAMING_SNAKE_CASE),
																		        *get_values(_SCREAMING_SNAKE_CASE),
																		    ]
																		    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
																		):
																									continue

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

																																snake_case__							=							model_class(_SCREAMING_SNAKE_CASE)
																																model.to(_SCREAMING_SNAKE_CASE)
																																model.train()

																																snake_case__							=							self._prepare_for_class(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		return_labels=_SCREAMING_SNAKE_CASE)

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

																																snake_case__							=							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=_SCREAMING_SNAKE_CASE) as warning_list:
																																							snake_case__							=							model(**_SCREAMING_SNAKE_CASE).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        __magic_name__				(   self     :     Union[str, Any]):






											'''simple docstring'''







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


def _UpperCAmelCase (						):
							snake_case__							=							Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
							return image



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



				"""simple docstring"""



				@cached_property
				def        __magic_name__				(   self     :     Tuple):






											'''simple docstring'''







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



				@slow
				def        __magic_name__				(   self     :     Any):






											'''simple docstring'''







											snake_case__							=							DeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224""").to(
											    _SCREAMING_SNAKE_CASE)

											snake_case__							=							self.default_image_processor
											snake_case__							=							prepare_img()
											snake_case__							=							image_processor(images=_SCREAMING_SNAKE_CASE      ,		return_tensors="""pt""").to(_SCREAMING_SNAKE_CASE)

											# forward pass
											with torch.no_grad():
																		snake_case__							=							model(**_SCREAMING_SNAKE_CASE)

											# verify the logits
											snake_case__							=							torch.Size((1, 1_0_0_0))
											self.assertEqual(outputs.logits.shape      ,		_SCREAMING_SNAKE_CASE)

											snake_case__							=							torch.tensor([-1.02_66, 0.19_12, -1.28_61]).to(_SCREAMING_SNAKE_CASE)

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



				@slow
				@require_accelerate
				@require_torch_gpu
				def        __magic_name__				(   self     :     int):






											'''simple docstring'''







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

											snake_case__							=							prepare_img()
											snake_case__							=							image_processor(images=_SCREAMING_SNAKE_CASE      ,		return_tensors="""pt""")
											snake_case__							=							inputs.pixel_values.to(_SCREAMING_SNAKE_CASE)

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




def 			lowerCAmelCase__					(     a__:						int     ,    a__:						int			)    ->						int:


							'''simple docstring'''





							return x if y == 0 else greatest_common_divisor(a__     ,    x % y			)

def 			lowerCAmelCase__					(     a__:						int     ,    a__:						int			)    ->						int:


							'''simple docstring'''





							return (x * y) // greatest_common_divisor(a__     ,    a__			)

def 			lowerCAmelCase__					(     a__:						int = 2_0			)    ->						int:


							'''simple docstring'''





							_UpperCAmelCase											=     1
							for i in range(1     ,    n + 1			):
														_UpperCAmelCase											=     lcm(a__     ,    a__			)
							return g


if __name__ == "__main__":
		print(f'''{solution() = }''')
