


'''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  :     Optional[int]	,  lowerCamelCase__  :     int	,  lowerCamelCase__  :     Any=13	,  lowerCamelCase__  :     str=30	,  lowerCamelCase__  :     str=2	,  lowerCamelCase__  :     Optional[int]=3	,  lowerCamelCase__  :     List[Any]=True	,  lowerCamelCase__  :     Optional[int]=True	,  lowerCamelCase__  :     Optional[int]=32	,  lowerCamelCase__  :     int=5	,  lowerCamelCase__  :     Optional[int]=4	,  lowerCamelCase__  :     Any=37	,  lowerCamelCase__  :     int="gelu"	,  lowerCamelCase__  :     Optional[int]=0.1	,  lowerCamelCase__  :     Optional[int]=0.1	,  lowerCamelCase__  :     Tuple=10	,  lowerCamelCase__  :     Any=0.0_2	,  lowerCamelCase__  :     Optional[Any]=3	,  lowerCamelCase__  :     Dict=None	,  lowerCamelCase__  :     Dict=2	,  )   ->List[Any]:



					'''simple docstring'''







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

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




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



					'''simple docstring'''







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

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

					_UpperCAmelCase		:       str								=      self.get_config()

					return config, pixel_values, labels




			def 							lowerCAmelCase__    (		self  :     Any )   ->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=lowerCamelCase__	,  initializer_range=self.initializer_range	,  encoder_stride=self.encoder_stride	,  )




			def 							lowerCAmelCase__    (		self  :     Dict	,  lowerCamelCase__  :     str	,  lowerCamelCase__  :     List[str]	,  lowerCamelCase__  :     List[str] )   ->str:



					'''simple docstring'''







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




			def 							lowerCAmelCase__    (		self  :     int	,  lowerCamelCase__  :     List[Any]	,  lowerCamelCase__  :     Tuple	,  lowerCamelCase__  :     Union[str, Any] )   ->Optional[int]:



					'''simple docstring'''







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

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

					_UpperCAmelCase		:       int								=      floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
					_UpperCAmelCase		:       Any								=      model(lowerCamelCase__ )
					self.parent.assertEqual(result.reconstruction.shape	,  (self.batch_size, 1, self.image_size, self.image_size) )




			def 							lowerCAmelCase__    (		self  :     Optional[int]	,  lowerCamelCase__  :     str	,  lowerCamelCase__  :     Any	,  lowerCamelCase__  :     int )   ->Tuple:



					'''simple docstring'''







					_UpperCAmelCase		:       Dict								=      self.type_sequence_label_size
					_UpperCAmelCase		:       Optional[Any]								=      DeiTForImageClassification(lowerCamelCase__ )
					model.to(lowerCamelCase__ )
					model.eval()
					_UpperCAmelCase		:       Any								=      model(lowerCamelCase__	,  labels=lowerCamelCase__ )
					self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.type_sequence_label_size) )

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

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






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



					'''simple docstring'''







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






@require_torch
class 	lowerCAmelCase__			(			UpperCAmelCase__    ,       UpperCAmelCase__    ,       unittest.TestCase	):
			lowerCAmelCase :	Tuple          =     (
			    (
			        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 :	Tuple          =     False
			lowerCAmelCase :	Optional[Any]          =     False
			lowerCAmelCase :	Optional[Any]          =     False




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



					'''simple docstring'''







					_UpperCAmelCase		:       Union[str, Any]								=      DeiTModelTester(self )
					_UpperCAmelCase		:       Tuple								=      ConfigTester(self	,  config_class=lowerCamelCase__	,  has_text_modality=lowerCamelCase__	,  hidden_size=37 )




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



					'''simple docstring'''







					self.config_tester.run_common_tests()




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



					'''simple docstring'''







					pass




			def 							lowerCAmelCase__    (		self  :     str )   ->Dict:



					'''simple docstring'''







					_UpperCAmelCase					,    _UpperCAmelCase		:       Any								=      self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							_UpperCAmelCase		:       Optional[Any]								=      model_class(lowerCamelCase__ )
							self.assertIsInstance(model.get_input_embeddings()	,  (nn.Module) )
							_UpperCAmelCase		:       Dict								=      model.get_output_embeddings()
							self.assertTrue(x is None or isinstance(lowerCamelCase__	,  nn.Linear ) )




			def 							lowerCAmelCase__    (		self  :     int )   ->Dict:



					'''simple docstring'''







					_UpperCAmelCase					,    _UpperCAmelCase		:       int								=      self.model_tester.prepare_config_and_inputs_for_common()

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

							_UpperCAmelCase		:       Tuple								=      ["pixel_values"]
							self.assertListEqual(arg_names[:1]	,  lowerCamelCase__ )




			def 							lowerCAmelCase__    (		self  :     Dict )   ->Optional[Any]:



					'''simple docstring'''







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




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



					'''simple docstring'''







					_UpperCAmelCase		:       Any								=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase__ )




			def 							lowerCAmelCase__    (		self  :     int )   ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:       List[str]								=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__ )




			def 							lowerCAmelCase__    (		self  :     Optional[int]	,  lowerCamelCase__  :     List[Any]	,  lowerCamelCase__  :     Optional[int]	,  lowerCamelCase__  :     List[str]=False )   ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:       int								=      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 							lowerCAmelCase__    (		self  :     Tuple )   ->str:



					'''simple docstring'''







					if not self.model_tester.is_training:
							return

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




			def 							lowerCAmelCase__    (		self  :     Dict )   ->str:



					'''simple docstring'''







					_UpperCAmelCase					,    _UpperCAmelCase		:       Optional[Any]								=      self.model_tester.prepare_config_and_inputs_for_common()
					if not self.model_tester.is_training:
							return

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




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



					'''simple docstring'''







					_UpperCAmelCase					,    _UpperCAmelCase		:       int								=      self.model_tester.prepare_config_and_inputs_for_common()

					_UpperCAmelCase		:       Union[str, Any]								=      [
					    {"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		:       Any								=      problem_type["title"]
											_UpperCAmelCase		:       Dict								=      problem_type["num_labels"]

											_UpperCAmelCase		:       str								=      model_class(lowerCamelCase__ )
											model.to(lowerCamelCase__ )
											model.train()

											_UpperCAmelCase		:       Tuple								=      self._prepare_for_class(lowerCamelCase__	,  lowerCamelCase__	,  return_labels=lowerCamelCase__ )

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

											_UpperCAmelCase		:       Union[str, 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:
													_UpperCAmelCase		:       Union[str, Any]								=      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 							lowerCAmelCase__    (		self  :     str )   ->Optional[int]:



					'''simple docstring'''







					for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							_UpperCAmelCase		:       int								=      DeiTModel.from_pretrained(lowerCamelCase__ )
							self.assertIsNotNone(lowerCamelCase__ )






def  __lowerCAmelCase ():
		_UpperCAmelCase		:       List[Any]								=      Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
		return image






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




			@cached_property
			def 							lowerCAmelCase__    (		self  :     Any )   ->str:



					'''simple docstring'''







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




			@slow
			def 							lowerCAmelCase__    (		self  :     str )   ->List[str]:



					'''simple docstring'''







					_UpperCAmelCase		:       Union[str, Any]								=      DeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224" ).to(
					    lowerCamelCase__ )

					_UpperCAmelCase		:       Optional[int]								=      self.default_image_processor
					_UpperCAmelCase		:       Optional[Any]								=      prepare_img()
					_UpperCAmelCase		:       Dict								=      image_processor(images=lowerCamelCase__	,  return_tensors="pt" ).to(lowerCamelCase__ )

					# forward pass
					with torch.no_grad():
							_UpperCAmelCase		:       List[Any]								=      model(**lowerCamelCase__ )

					# verify the logits
					_UpperCAmelCase		:       int								=      torch.Size((1, 10_00) )
					self.assertEqual(outputs.logits.shape	,  lowerCamelCase__ )

					_UpperCAmelCase		:       Any								=      torch.tensor([-1.0_2_6_6, 0.1_9_1_2, -1.2_8_6_1] ).to(lowerCamelCase__ )

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






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



					'''simple docstring'''







					_UpperCAmelCase		:       Dict								=      DeiTModel.from_pretrained(
					    "facebook/deit-base-distilled-patch16-224"	,  torch_dtype=torch.floataa	,  device_map="auto" )
					_UpperCAmelCase		:       Optional[int]								=      self.default_image_processor

					_UpperCAmelCase		:       int								=      prepare_img()
					_UpperCAmelCase		:       Dict								=      image_processor(images=lowerCamelCase__	,  return_tensors="pt" )
					_UpperCAmelCase		:       Any								=      inputs.pixel_values.to(lowerCamelCase__ )

					# forward pass to make sure inference works in fp16
					with torch.no_grad():
							_UpperCAmelCase		:       Optional[int]								=      model(lowerCamelCase__ )







'''simple docstring'''


from __future__ import annotations

import numpy as np






def  __lowerCAmelCase (__lowerCAmelCase   ):
		return np.maximum(0      , __lowerCAmelCase   )


if __name__ == "__main__":
						print(np.array(relu([-1, 0, 5])))  # --> [0, 0, 5]



