

'''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__     :




     """simple docstring"""





     def __init__(		self						:	Union[str, Any]      ,						A__						:	Any      ,						A__						:	Union[str, Any]=1_3      ,						A__						:	Optional[Any]=3_0      ,						A__						:	Tuple=2      ,						A__						:	int=3      ,						A__						:	Dict=True      ,						A__						:	Union[str, Any]=True      ,						A__						:	Any=3_2      ,						A__						:	str=5      ,						A__						:	Dict=4      ,						A__						:	List[Any]=3_7      ,						A__						:	List[str]="gelu"      ,						A__						:	Any=0.1      ,						A__						:	Tuple=0.1      ,						A__						:	List[str]=1_0      ,						A__						:	Any=0.02      ,						A__						:	Union[str, Any]=3      ,						A__						:	int=None      ,						A__						:	Optional[int]=2      ,						)       ->			List[Any]:
          '''simple docstring'''




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

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







     def 			__lowerCAmelCase	(		self						:	List[str]	)       ->			Optional[Any]:
          '''simple docstring'''




          a__   :      Union[str, Any]     =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

          a__   :      Tuple     =						self.get_config()

          return config, pixel_values, labels







     def 			__lowerCAmelCase	(		self						:	Union[str, Any]	)       ->			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=__a      ,						initializer_range=self.initializer_range      ,						encoder_stride=self.encoder_stride      ,						)







     def 			__lowerCAmelCase	(		self						:	Optional[Any]      ,						A__						:	Optional[Any]      ,						A__						:	str      ,						A__						:	str	)       ->			Dict:
          '''simple docstring'''




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







     def 			__lowerCAmelCase	(		self						:	List[str]      ,						A__						:	int      ,						A__						:	Dict      ,						A__						:	Union[str, Any]	)       ->			Any:
          '''simple docstring'''




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

          # test greyscale images
          a__   :      List[str]     =						1
          a__   :      Union[str, Any]     =						DeiTForMaskedImageModeling(__a	)
          model.to(__a	)
          model.eval()

          a__   :      Tuple     =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
          a__   :      Tuple     =						model(__a	)
          self.parent.assertEqual(result.reconstruction.shape      ,						(self.batch_size, 1, self.image_size, self.image_size)	)







     def 			__lowerCAmelCase	(		self						:	int      ,						A__						:	Any      ,						A__						:	Union[str, Any]      ,						A__						:	Optional[int]	)       ->			List[Any]:
          '''simple docstring'''




          a__   :      Any     =						self.type_sequence_label_size
          a__   :      List[Any]     =						DeiTForImageClassification(__a	)
          model.to(__a	)
          model.eval()
          a__   :      Dict     =						model(__a      ,						labels=__a	)
          self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.type_sequence_label_size)	)

          # test greyscale images
          a__   :      Any     =						1
          a__   :      Dict     =						DeiTForImageClassification(__a	)
          model.to(__a	)
          model.eval()

          a__   :      str     =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
          a__   :      Union[str, Any]     =						model(__a      ,						labels=__a	)
          self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.type_sequence_label_size)	)







     def 			__lowerCAmelCase	(		self						:	Union[str, Any]	)       ->			Tuple:
          '''simple docstring'''




          a__   :      Union[str, Any]     =						self.prepare_config_and_inputs()
          (
              (
              a__      
          )     ,    (
              a__      
          )     ,    (
              a__      
          )     ,          
          )   :      Dict     =						config_and_inputs
          a__   :      Optional[int]     =						{'''pixel_values''': pixel_values}
          return config, inputs_dict






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




     """simple docstring"""





     __UpperCamelCase									= (
         (
             DeiTModel,
             DeiTForImageClassification,
             DeiTForImageClassificationWithTeacher,
             DeiTForMaskedImageModeling,
         )
         if is_torch_available()
         else ()
     )
     __UpperCamelCase									= (
         {
             """feature-extraction""": DeiTModel,
             """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
         }
         if is_torch_available()
         else {}
     )

     __UpperCamelCase									= False
     __UpperCamelCase									= False
     __UpperCamelCase									= False







     def 			__lowerCAmelCase	(		self						:	Optional[int]	)       ->			Tuple:
          '''simple docstring'''




          a__   :      List[Any]     =						DeiTModelTester(self	)
          a__   :      Tuple     =						ConfigTester(self      ,						config_class=__a      ,						has_text_modality=__a      ,						hidden_size=3_7	)







     def 			__lowerCAmelCase	(		self						:	Tuple	)       ->			Optional[int]:
          '''simple docstring'''




          self.config_tester.run_common_tests()







     @unittest.skip(reason='''DeiT does not use inputs_embeds'''	)
     def 			__lowerCAmelCase	(		self						:	Any	)       ->			Optional[int]:
          '''simple docstring'''




          pass







     def 			__lowerCAmelCase	(		self						:	str	)       ->			Dict:
          '''simple docstring'''




          a__     ,    a__   :      Optional[int]     =						self.model_tester.prepare_config_and_inputs_for_common()

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







     def 			__lowerCAmelCase	(		self						:	Tuple	)       ->			Optional[int]:
          '''simple docstring'''




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

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

               a__   :      Union[str, Any]     =						['''pixel_values''']
               self.assertListEqual(arg_names[:1]      ,						__a	)







     def 			__lowerCAmelCase	(		self						:	Tuple	)       ->			List[Any]:
          '''simple docstring'''




          a__   :      Union[str, Any]     =						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__a	)







     def 			__lowerCAmelCase	(		self						:	str	)       ->			int:
          '''simple docstring'''




          a__   :      str     =						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_image_modeling(*__a	)







     def 			__lowerCAmelCase	(		self						:	List[Any]	)       ->			int:
          '''simple docstring'''




          a__   :      Tuple     =						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*__a	)







     def 			__lowerCAmelCase	(		self						:	int      ,						A__						:	Union[str, Any]      ,						A__						:	Union[str, Any]      ,						A__						:	List[str]=False	)       ->			Optional[int]:
          '''simple docstring'''




          a__   :      Tuple     =						super()._prepare_for_class(__a      ,						__a      ,						return_labels=__a	)

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

          return inputs_dict







     def 			__lowerCAmelCase	(		self						:	List[Any]	)       ->			Union[str, Any]:
          '''simple docstring'''




          if not self.model_tester.is_training:
               return

          a__     ,    a__   :      Tuple     =						self.model_tester.prepare_config_and_inputs_for_common()
          a__   :      Optional[int]     =						True

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







     def 			__lowerCAmelCase	(		self						:	Union[str, Any]	)       ->			List[str]:
          '''simple docstring'''




          a__     ,    a__   :      List[str]     =						self.model_tester.prepare_config_and_inputs_for_common()
          if not self.model_tester.is_training:
               return

          a__   :      List[str]     =						False
          a__   :      int     =						True

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







     def 			__lowerCAmelCase	(		self						:	int	)       ->			List[Any]:
          '''simple docstring'''




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

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

               for problem_type in problem_types:
                    with self.subTest(msg=F'Testing {model_class} with {problem_type["title"]}'	):
                         a__   :      Optional[Any]     =						problem_type['''title''']
                         a__   :      Union[str, Any]     =						problem_type['''num_labels''']

                         a__   :      Any     =						model_class(__a	)
                         model.to(__a	)
                         model.train()

                         a__   :      Dict     =						self._prepare_for_class(__a      ,						__a      ,						return_labels=__a	)

                         if problem_type["num_labels"] > 1:
                              a__   :      Optional[int]     =						inputs['''labels'''].unsqueeze(1	).repeat(1      ,						problem_type['''num_labels''']	)

                         a__   :      str     =						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=__a	) as warning_list:
                              a__   :      Optional[int]     =						model(**__a	).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						:	Dict	)       ->			Union[str, Any]:
          '''simple docstring'''




          for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               a__   :      Tuple     =						DeiTModel.from_pretrained(__a	)
               self.assertIsNotNone(__a	)



def 			__a		(       ):
     a__   :      Union[str, Any]     =						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
     return image






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




     """simple docstring"""





     @cached_property
     def 			__lowerCAmelCase	(		self						:	int	)       ->			Tuple:
          '''simple docstring'''




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







     @slow
     def 			__lowerCAmelCase	(		self						:	List[str]	)       ->			Optional[Any]:
          '''simple docstring'''




          a__   :      Any     =						DeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224'''	).to(
              __a	)

          a__   :      Union[str, Any]     =						self.default_image_processor
          a__   :      Union[str, Any]     =						prepare_img()
          a__   :      Optional[int]     =						image_processor(images=__a      ,						return_tensors='''pt'''	).to(__a	)

          # forward pass
          with torch.no_grad():
               a__   :      Dict     =						model(**__a	)

          # verify the logits
          a__   :      Dict     =						torch.Size((1, 1_0_0_0)	)
          self.assertEqual(outputs.logits.shape      ,						__a	)

          a__   :      Optional[int]     =						torch.tensor([-1.0_266, 0.1_912, -1.2_861]	).to(__a	)

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







     @slow
     @require_accelerate
     @require_torch_gpu
     def 			__lowerCAmelCase	(		self						:	Optional[int]	)       ->			Tuple:
          '''simple docstring'''




          a__   :      Dict     =						DeiTModel.from_pretrained(
              '''facebook/deit-base-distilled-patch16-224'''      ,						torch_dtype=torch.floataa      ,						device_map='''auto'''	)
          a__   :      int     =						self.default_image_processor

          a__   :      Optional[int]     =						prepare_img()
          a__   :      List[str]     =						image_processor(images=__a      ,						return_tensors='''pt'''	)
          a__   :      Optional[int]     =						inputs.pixel_values.to(__a	)

          # forward pass to make sure inference works in fp16
          with torch.no_grad():
               a__   :      Dict     =						model(__a	)




"""simple docstring"""


from __future__ import annotations

import math

import numpy as np
from numpy.linalg import norm


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			return math.sqrt(sum(pow(a - b       ,					2 ) for a, b in zip(_UpperCamelCase       ,					_UpperCamelCase ) ) )


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			if dataset.ndim != value_array.ndim:
						__lowerCAmelCase							       =     (
						    "Wrong input data's dimensions... "
						    f"dataset : {dataset.ndim}, value_array : {value_array.ndim}"
						)
						raise ValueError(_UpperCamelCase )

			try:
						if dataset.shape[1] != value_array.shape[1]:
									__lowerCAmelCase							       =     (
									    "Wrong input data's shape... "
									    f"dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}"
									)
									raise ValueError(_UpperCamelCase )
			except IndexError:
						if dataset.ndim != value_array.ndim:
									raise TypeError("Wrong shape" )

			if dataset.dtype != value_array.dtype:
						__lowerCAmelCase							       =     (
						    "Input data have different datatype... "
						    f"dataset : {dataset.dtype}, value_array : {value_array.dtype}"
						)
						raise TypeError(_UpperCamelCase )

			__lowerCAmelCase							       =     []

			for value in value_array:
						__lowerCAmelCase							       =     euclidean(_UpperCamelCase       ,					dataset[0] )
						__lowerCAmelCase							       =     dataset[0].tolist()

						for dataset_value in dataset[1:]:
									__lowerCAmelCase							       =     euclidean(_UpperCamelCase       ,					_UpperCamelCase )

									if dist > temp_dist:
												__lowerCAmelCase							       =     temp_dist
												__lowerCAmelCase							       =     dataset_value.tolist()

						answer.append([vector, dist] )

			return answer


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			return np.dot(_UpperCamelCase       ,					_UpperCamelCase ) / (norm(_UpperCamelCase ) * norm(_UpperCamelCase ))


if __name__ == "__main__":
	import doctest

	doctest.testmod()




