





from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
						import tensorflow as tf

						from transformers import (
						    TFDeiTForImageClassification,
						    TFDeiTForImageClassificationWithTeacher,
						    TFDeiTForMaskedImageModeling,
						    TFDeiTModel,
						)
						from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import DeiTImageProcessor

class 	UpperCamelCase__				:



					'''simple docstring'''

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




											'''simple docstring'''


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

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






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




											'''simple docstring'''


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

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

											SCREAMING_SNAKE_CASE	=		self.get_config()

											return config, pixel_values, labels






					def        SCREAMING_SNAKE_CASE__						(							self		:					Any )  ->	List[Any]:




											'''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		:					Tuple		,lowerCamelCase__		:					Optional[int]		,lowerCamelCase__		:					Optional[int]		,lowerCamelCase__		:					Union[str, Any] )  ->	Any:




											'''simple docstring'''


											SCREAMING_SNAKE_CASE	=		TFDeiTModel(config=UpperCamelCase__ )
											SCREAMING_SNAKE_CASE	=		model(UpperCamelCase__ )
											self.parent.assertEqual(result.last_hidden_state.shape		,(self.batch_size, self.seq_length, self.hidden_size) )






					def        SCREAMING_SNAKE_CASE__						(							self		:					Dict		,lowerCamelCase__		:					Union[str, Any]		,lowerCamelCase__		:					Union[str, Any]		,lowerCamelCase__		:					str )  ->	int:




											'''simple docstring'''


											SCREAMING_SNAKE_CASE	=		TFDeiTForMaskedImageModeling(config=UpperCamelCase__ )
											SCREAMING_SNAKE_CASE	=		model(UpperCamelCase__ )
											self.parent.assertEqual(
											    result.reconstruction.shape		,(self.batch_size, self.num_channels, self.image_size, self.image_size) )

											# test greyscale images
											SCREAMING_SNAKE_CASE	=		1
											SCREAMING_SNAKE_CASE	=		TFDeiTForMaskedImageModeling(UpperCamelCase__ )

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






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




											'''simple docstring'''


											SCREAMING_SNAKE_CASE	=		self.type_sequence_label_size
											SCREAMING_SNAKE_CASE	=		TFDeiTForImageClassification(UpperCamelCase__ )
											SCREAMING_SNAKE_CASE	=		model(UpperCamelCase__		,labels=UpperCamelCase__ )
											self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.type_sequence_label_size) )

											# test greyscale images
											SCREAMING_SNAKE_CASE	=		1
											SCREAMING_SNAKE_CASE	=		TFDeiTForImageClassification(UpperCamelCase__ )

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






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




											'''simple docstring'''


											SCREAMING_SNAKE_CASE	=		self.prepare_config_and_inputs()
											SCREAMING_SNAKE_CASE	=		config_and_inputs
											SCREAMING_SNAKE_CASE	=		{'''pixel_values''': pixel_values}
											return config, inputs_dict

@require_tf
class 	UpperCamelCase__				(   SCREAMING_SNAKE_CASE_							,   SCREAMING_SNAKE_CASE_							,   unittest.TestCase     ):



					'''simple docstring'''

					__snake_case					:			Optional[Any]				=    (
					    (
					        TFDeiTModel,
					        TFDeiTForImageClassification,
					        TFDeiTForImageClassificationWithTeacher,
					        TFDeiTForMaskedImageModeling,
					    )
					    if is_tf_available()
					    else ()
					)
					__snake_case					:			List[Any]				=    (
					    {
					        "feature-extraction": TFDeiTModel,
					        "image-classification": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
					    }
					    if is_tf_available()
					    else {}
					)

					__snake_case					:			Dict				=    False
					__snake_case					:			List[str]				=    False
					__snake_case					:			Any				=    False
					__snake_case					:			Optional[int]				=    False






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




											'''simple docstring'''


											SCREAMING_SNAKE_CASE	=		TFDeiTModelTester(self )
											SCREAMING_SNAKE_CASE	=		ConfigTester(self		,config_class=UpperCamelCase__		,has_text_modality=UpperCamelCase__		,hidden_size=37 )






					def        SCREAMING_SNAKE_CASE__						(							self		:					Any )  ->	int:




											'''simple docstring'''


											self.config_tester.run_common_tests()






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




											'''simple docstring'''


											pass






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




											'''simple docstring'''


											SCREAMING_SNAKE_CASE	=		self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	SCREAMING_SNAKE_CASE	=		model_class(UpperCamelCase__ )
																	self.assertIsInstance(model.get_input_embeddings()		,(tf.keras.layers.Layer) )
																	SCREAMING_SNAKE_CASE	=		model.get_output_embeddings()
																	self.assertTrue(x is None or isinstance(UpperCamelCase__		,tf.keras.layers.Dense ) )






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




											'''simple docstring'''


											SCREAMING_SNAKE_CASE	=		self.model_tester.prepare_config_and_inputs_for_common()

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

																	SCREAMING_SNAKE_CASE	=		['''pixel_values''']
																	self.assertListEqual(arg_names[:1]		,UpperCamelCase__ )






					def        SCREAMING_SNAKE_CASE__						(							self		:					Dict )  ->	Dict:




											'''simple docstring'''


											SCREAMING_SNAKE_CASE	=		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*UpperCamelCase__ )






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




											'''simple docstring'''


											SCREAMING_SNAKE_CASE	=		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase__ )






					def        SCREAMING_SNAKE_CASE__						(							self		:					Optional[int] )  ->	Tuple:




											'''simple docstring'''


											SCREAMING_SNAKE_CASE	=		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__ )






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




											'''simple docstring'''


											SCREAMING_SNAKE_CASE	=		super()._prepare_for_class(UpperCamelCase__		,UpperCamelCase__		,return_labels=UpperCamelCase__ )

											if return_labels:
																	if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call ).parameters:
																							del inputs_dict["labels"]

											return inputs_dict






					@slow
					def        SCREAMING_SNAKE_CASE__						(							self		:					List[str] )  ->	Optional[Any]:




											'''simple docstring'''


											for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	SCREAMING_SNAKE_CASE	=		TFDeiTModel.from_pretrained(UpperCamelCase__ )
																	self.assertIsNotNone(UpperCamelCase__ )



def      __lowercase     (   )				->     str:


						'''simple docstring'''


						SCREAMING_SNAKE_CASE	=		Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
						return image



@require_tf
@require_vision
class 	UpperCamelCase__				(   unittest.TestCase     ):



					'''simple docstring'''

					@cached_property
					def        SCREAMING_SNAKE_CASE__						(							self		:					str )  ->	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		:					Union[str, Any] )  ->	Dict:




											'''simple docstring'''


											SCREAMING_SNAKE_CASE	=		TFDeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224""" )

											SCREAMING_SNAKE_CASE	=		self.default_image_processor
											SCREAMING_SNAKE_CASE	=		prepare_img()
											SCREAMING_SNAKE_CASE	=		image_processor(images=UpperCamelCase__		,return_tensors="""tf""" )

											# forward pass
											SCREAMING_SNAKE_CASE	=		model(**UpperCamelCase__ )

											# verify the logits
											SCREAMING_SNAKE_CASE	=		tf.TensorShape((1, 1000) )
											self.assertEqual(outputs.logits.shape		,UpperCamelCase__ )

											SCREAMING_SNAKE_CASE	=		tf.constant([-1.0266, 0.1912, -1.2861] )

											self.assertTrue(np.allclose(outputs.logits[0, :3]		,UpperCamelCase__		,atol=1e-4 ) )









def      __lowercase     (   _SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE			)				->     float:


      '''simple docstring'''


      _validate_point(_SCREAMING_SNAKE_CASE			)
      _validate_point(_SCREAMING_SNAKE_CASE			)
      if len(_SCREAMING_SNAKE_CASE			) != len(_SCREAMING_SNAKE_CASE			):
            raise ValueError("""Both points must be in the same n-dimensional space"""			)

      return float(sum(abs(a - b			) for a, b in zip(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE			)			)			)



def      __lowercase     (   _SCREAMING_SNAKE_CASE			)				->     None:


      '''simple docstring'''


      if point:
            if isinstance(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE			):
                  for item in point:
                        if not isinstance(_SCREAMING_SNAKE_CASE				,		(int, float)			):
                              SCREAMING_SNAKE_CASE	=		(
                                  """Expected a list of numbers as input, found """
                                  F"""{type(_SCREAMING_SNAKE_CASE			).__name__}"""
                              )
                              raise TypeError(_SCREAMING_SNAKE_CASE			)
            else:
                  SCREAMING_SNAKE_CASE	=		F"""Expected a list of numbers as input, found {type(_SCREAMING_SNAKE_CASE			).__name__}"""
                  raise TypeError(_SCREAMING_SNAKE_CASE			)
      else:
            raise ValueError("""Missing an input"""			)



def      __lowercase     (   _SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE			)				->     float:


      '''simple docstring'''


      _validate_point(_SCREAMING_SNAKE_CASE			)
      _validate_point(_SCREAMING_SNAKE_CASE			)
      if len(_SCREAMING_SNAKE_CASE			) != len(_SCREAMING_SNAKE_CASE			):
            raise ValueError("""Both points must be in the same n-dimensional space"""			)

      return float(sum(abs(x - y			) for x, y in zip(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE			)			)			)


if __name__ == "__main__":
      import doctest

      doctest.testmod()


