




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        __snake_case  :



				def __init__(      self      :		Any		,	_UpperCAmelCase      :		List[Any]		,	_UpperCAmelCase      :		Union[str, Any]=13		,	_UpperCAmelCase      :		Union[str, Any]=30		,	_UpperCAmelCase      :		str=2		,	_UpperCAmelCase      :		Union[str, Any]=3		,	_UpperCAmelCase      :		Dict=True		,	_UpperCAmelCase      :		Optional[Any]=True		,	_UpperCAmelCase      :		List[str]=32		,	_UpperCAmelCase      :		List[Any]=5		,	_UpperCAmelCase      :		List[Any]=4		,	_UpperCAmelCase      :		Dict=37		,	_UpperCAmelCase      :		str="gelu"		,	_UpperCAmelCase      :		str=0.1		,	_UpperCAmelCase      :		List[str]=0.1		,	_UpperCAmelCase      :		List[Any]=10		,	_UpperCAmelCase      :		Optional[Any]=0.02		,	_UpperCAmelCase      :		List[str]=3		,	_UpperCAmelCase      :		Optional[int]=None		,	_UpperCAmelCase      :		Dict=2		,	)  ->    Dict:





								'''simple docstring'''




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

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



				def       SCREAMING_SNAKE_CASE       (      self      :		Union[str, Any]      )  ->    Optional[int]:





								'''simple docstring'''




								_lowerCAmelCase    :    List[str]										=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

								_lowerCAmelCase    :    Tuple										=      None
								if self.use_labels:
												_lowerCAmelCase    :    Union[str, Any]										=      ids_tensor([self.batch_size]		,	self.type_sequence_label_size      )

								_lowerCAmelCase    :    Any										=      self.get_config()

								return config, pixel_values, labels



				def       SCREAMING_SNAKE_CASE       (      self      :		Dict      )  ->    Optional[int]:





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



				def       SCREAMING_SNAKE_CASE       (      self      :		str		,	_UpperCAmelCase      :		Optional[int]		,	_UpperCAmelCase      :		Union[str, Any]		,	_UpperCAmelCase      :		str      )  ->    Dict:





								'''simple docstring'''




								_lowerCAmelCase    :    Union[str, Any]										=      DeiTModel(config=_UpperCAmelCase      )
								model.to(_UpperCAmelCase      )
								model.eval()
								_lowerCAmelCase    :    Dict										=      model(_UpperCAmelCase      )
								self.parent.assertEqual(result.last_hidden_state.shape		,	(self.batch_size, self.seq_length, self.hidden_size)      )



				def       SCREAMING_SNAKE_CASE       (      self      :		str		,	_UpperCAmelCase      :		Any		,	_UpperCAmelCase      :		Any		,	_UpperCAmelCase      :		List[str]      )  ->    str:





								'''simple docstring'''




								_lowerCAmelCase    :    int										=      DeiTForMaskedImageModeling(config=_UpperCAmelCase      )
								model.to(_UpperCAmelCase      )
								model.eval()
								_lowerCAmelCase    :    Tuple										=      model(_UpperCAmelCase      )
								self.parent.assertEqual(
								    result.reconstruction.shape		,	(self.batch_size, self.num_channels, self.image_size, self.image_size)      )

								# test greyscale images
								_lowerCAmelCase    :    Optional[int]										=      1
								_lowerCAmelCase    :    Tuple										=      DeiTForMaskedImageModeling(_UpperCAmelCase      )
								model.to(_UpperCAmelCase      )
								model.eval()

								_lowerCAmelCase    :    Dict										=      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
								_lowerCAmelCase    :    List[str]										=      model(_UpperCAmelCase      )
								self.parent.assertEqual(result.reconstruction.shape		,	(self.batch_size, 1, self.image_size, self.image_size)      )



				def       SCREAMING_SNAKE_CASE       (      self      :		List[str]		,	_UpperCAmelCase      :		List[str]		,	_UpperCAmelCase      :		Tuple		,	_UpperCAmelCase      :		Any      )  ->    List[Any]:





								'''simple docstring'''




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

								# test greyscale images
								_lowerCAmelCase    :    Tuple										=      1
								_lowerCAmelCase    :    List[Any]										=      DeiTForImageClassification(_UpperCAmelCase      )
								model.to(_UpperCAmelCase      )
								model.eval()

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



				def       SCREAMING_SNAKE_CASE       (      self      :		int      )  ->    Tuple:





								'''simple docstring'''




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






@require_torch
class        __snake_case  (_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       SCREAMING_SNAKE_CASE       (      self      :		Union[str, Any]      )  ->    Optional[int]:





								'''simple docstring'''




								_lowerCAmelCase    :    List[str]										=      DeiTModelTester(self      )
								_lowerCAmelCase    :    Tuple										=      ConfigTester(self		,	config_class=_UpperCAmelCase		,	has_text_modality=_UpperCAmelCase		,	hidden_size=37      )



				def       SCREAMING_SNAKE_CASE       (      self      :		List[str]      )  ->    int:





								'''simple docstring'''




								self.config_tester.run_common_tests()



				@unittest.skip(reason="""DeiT does not use inputs_embeds"""      )
				def       SCREAMING_SNAKE_CASE       (      self      :		str      )  ->    Any:





								'''simple docstring'''




								pass



				def       SCREAMING_SNAKE_CASE       (      self      :		Optional[int]      )  ->    str:





								'''simple docstring'''




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

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



				def       SCREAMING_SNAKE_CASE       (      self      :		str      )  ->    Tuple:





								'''simple docstring'''




								_lowerCAmelCase    :    str										=      self.model_tester.prepare_config_and_inputs_for_common()

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

												_lowerCAmelCase    :    Optional[Any]										=      ["""pixel_values"""]
												self.assertListEqual(arg_names[:1]		,	_UpperCAmelCase      )



				def       SCREAMING_SNAKE_CASE       (      self      :		str      )  ->    List[Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    str										=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_UpperCAmelCase      )



				def       SCREAMING_SNAKE_CASE       (      self      :		str      )  ->    Any:





								'''simple docstring'''




								_lowerCAmelCase    :    int										=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*_UpperCAmelCase      )



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





								'''simple docstring'''




								_lowerCAmelCase    :    Optional[int]										=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase      )



				def       SCREAMING_SNAKE_CASE       (      self      :		int		,	_UpperCAmelCase      :		int		,	_UpperCAmelCase      :		Tuple		,	_UpperCAmelCase      :		Tuple=False      )  ->    str:





								'''simple docstring'''




								_lowerCAmelCase    :    List[str]										=      super()._prepare_for_class(_UpperCAmelCase		,	_UpperCAmelCase		,	return_labels=_UpperCAmelCase      )

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

								return inputs_dict



				def       SCREAMING_SNAKE_CASE       (      self      :		Dict      )  ->    Any:





								'''simple docstring'''




								if not self.model_tester.is_training:
												return

								_lowerCAmelCase    :    List[Any]										=      self.model_tester.prepare_config_and_inputs_for_common()
								_lowerCAmelCase    :    Tuple										=      True

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



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





								'''simple docstring'''




								_lowerCAmelCase    :    Dict										=      self.model_tester.prepare_config_and_inputs_for_common()
								if not self.model_tester.is_training:
												return

								_lowerCAmelCase    :    Tuple										=      False
								_lowerCAmelCase    :    Dict										=      True

								for model_class in self.all_model_classes:
												if model_class in get_values(_UpperCAmelCase      ) or not model_class.supports_gradient_checkpointing:
																continue
												# DeiTForImageClassificationWithTeacher supports inference-only
												if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
																continue
												_lowerCAmelCase    :    Optional[Any]										=      model_class(_UpperCAmelCase      )
												model.gradient_checkpointing_enable()
												model.to(_UpperCAmelCase      )
												model.train()
												_lowerCAmelCase    :    Optional[int]										=      self._prepare_for_class(_UpperCAmelCase		,	_UpperCAmelCase		,	return_labels=_UpperCAmelCase      )
												_lowerCAmelCase    :    Union[str, Any]										=      model(**_UpperCAmelCase      ).loss
												loss.backward()



				def       SCREAMING_SNAKE_CASE       (      self      :		Optional[int]      )  ->    List[Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    Any										=      self.model_tester.prepare_config_and_inputs_for_common()

								_lowerCAmelCase    :    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(_UpperCAmelCase      ),
												        *get_values(_UpperCAmelCase      ),
												    ]
												    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
												):
																continue

												for problem_type in problem_types:
																with self.subTest(msg=f"Testing {model_class} with {problem_type['title']}"      ):
																				_lowerCAmelCase    :    Optional[Any]										=      problem_type["""title"""]
																				_lowerCAmelCase    :    Optional[int]										=      problem_type["""num_labels"""]

																				_lowerCAmelCase    :    List[str]										=      model_class(_UpperCAmelCase      )
																				model.to(_UpperCAmelCase      )
																				model.train()

																				_lowerCAmelCase    :    int										=      self._prepare_for_class(_UpperCAmelCase		,	_UpperCAmelCase		,	return_labels=_UpperCAmelCase      )

																				if problem_type["num_labels"] > 1:
																								_lowerCAmelCase    :    Union[str, Any]										=      inputs["""labels"""].unsqueeze(1      ).repeat(1		,	problem_type["""num_labels"""]      )

																				_lowerCAmelCase    :    Tuple										=      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=_UpperCAmelCase      ) as warning_list:
																								_lowerCAmelCase    :    Dict										=      model(**_UpperCAmelCase      ).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       SCREAMING_SNAKE_CASE       (      self      :		Optional[Any]      )  ->    Dict:





								'''simple docstring'''




								for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												_lowerCAmelCase    :    List[str]										=      DeiTModel.from_pretrained(_UpperCAmelCase      )
												self.assertIsNotNone(_UpperCAmelCase      )






def 							_UpperCAmelCase  ():
				'''simple docstring'''
				_lowerCAmelCase    :    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       SCREAMING_SNAKE_CASE       (      self      :		int      )  ->    str:





								'''simple docstring'''




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



				@slow
				def       SCREAMING_SNAKE_CASE       (      self      :		str      )  ->    str:





								'''simple docstring'''




								_lowerCAmelCase    :    List[str]										=      DeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""      ).to(
								    _UpperCAmelCase      )

								_lowerCAmelCase    :    Any										=      self.default_image_processor
								_lowerCAmelCase    :    Union[str, Any]										=      prepare_img()
								_lowerCAmelCase    :    List[Any]										=      image_processor(images=_UpperCAmelCase		,	return_tensors="""pt"""      ).to(_UpperCAmelCase      )

								# forward pass
								with torch.no_grad():
												_lowerCAmelCase    :    Optional[int]										=      model(**_UpperCAmelCase      )

								# verify the logits
								_lowerCAmelCase    :    Any										=      torch.Size((1, 1000)      )
								self.assertEqual(outputs.logits.shape		,	_UpperCAmelCase      )

								_lowerCAmelCase    :    Any										=      torch.tensor([-1.0266, 0.1912, -1.2861]      ).to(_UpperCAmelCase      )

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



				@slow
				@require_accelerate
				@require_torch_gpu
				def       SCREAMING_SNAKE_CASE       (      self      :		str      )  ->    Union[str, Any]:





								'''simple docstring'''




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

								_lowerCAmelCase    :    int										=      prepare_img()
								_lowerCAmelCase    :    int										=      image_processor(images=_UpperCAmelCase		,	return_tensors="""pt"""      )
								_lowerCAmelCase    :    List[Any]										=      inputs.pixel_values.to(_UpperCAmelCase      )

								# forward pass to make sure inference works in fp16
								with torch.no_grad():
												_lowerCAmelCase    :    Any										=      model(_UpperCAmelCase      )











def 							_UpperCAmelCase  (UpperCamelCase_ : str      ,    UpperCamelCase_ : bool = False       ):
				'''simple docstring'''

				if not isinstance(UpperCamelCase_      ,    UpperCamelCase_       ):
								_lowerCAmelCase    :    Tuple										=      F"Expected string as input, found {type(UpperCamelCase_       )}"
								raise ValueError(UpperCamelCase_       )
				if not isinstance(UpperCamelCase_      ,    UpperCamelCase_       ):
								_lowerCAmelCase    :    Optional[Any]										=      F"Expected boolean as use_pascal parameter, found {type(UpperCamelCase_       )}"
								raise ValueError(UpperCamelCase_       )

				_lowerCAmelCase    :    Any										=      input_str.split("""_"""       )

				_lowerCAmelCase    :    str										=      0 if use_pascal else 1

				_lowerCAmelCase    :    Any										=      words[start_index:]

				_lowerCAmelCase    :    List[str]										=      [word[0].upper() + word[1:] for word in words_to_capitalize]

				_lowerCAmelCase    :    Any										=      """""" if use_pascal else words[0]

				return "".join([initial_word, *capitalized_words]       )


if __name__ == "__main__":
	from doctest import testmod

	testmod()

