




'''simple docstring'''




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						, UpperCAmelCase_						, UpperCAmelCase_=13						, UpperCAmelCase_=30						, UpperCAmelCase_=2						, UpperCAmelCase_=3						, UpperCAmelCase_=True						, UpperCAmelCase_=True						, UpperCAmelCase_=32						, UpperCAmelCase_=2						, UpperCAmelCase_=4						, UpperCAmelCase_=37						, UpperCAmelCase_="gelu"						, UpperCAmelCase_=0.1						, UpperCAmelCase_=0.1						, UpperCAmelCase_=10						, UpperCAmelCase_=0.02						, UpperCAmelCase_=3						, UpperCAmelCase_=None						, UpperCAmelCase_=2						, ):
						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  _lowercase    (						self				):
						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  _lowercase    (						self				):
						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  _lowercase    (						self						, UpperCAmelCase_						, UpperCAmelCase_						, UpperCAmelCase_				):
						snake_case_	=      TFDeiTModel(config=__UpperCamelCase				)
						snake_case_	=      model(__UpperCamelCase				)
						self.parent.assertEqual(result.last_hidden_state.shape						, (self.batch_size, self.seq_length, self.hidden_size)				)







	def  _lowercase    (						self						, UpperCAmelCase_						, UpperCAmelCase_						, UpperCAmelCase_				):
						snake_case_	=      TFDeiTForMaskedImageModeling(config=__UpperCamelCase				)
						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
						snake_case_	=      1
						snake_case_	=      TFDeiTForMaskedImageModeling(__UpperCamelCase				)

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







	def  _lowercase    (						self						, UpperCAmelCase_						, UpperCAmelCase_						, UpperCAmelCase_				):
						snake_case_	=      self.type_sequence_label_size
						snake_case_	=      TFDeiTForImageClassification(__UpperCamelCase				)
						snake_case_	=      model(__UpperCamelCase						, labels=__UpperCamelCase				)
						self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.type_sequence_label_size)				)

						# test greyscale images
						snake_case_	=      1
						snake_case_	=      TFDeiTForImageClassification(__UpperCamelCase				)

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







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






@require_tf
class      UpperCAmelCase_     (  __a       ,	__a       ,	unittest.TestCase     ):


	"""simple docstring"""


	snake_case									=		(
	    (
	        TFDeiTModel,
	        TFDeiTForImageClassification,
	        TFDeiTForImageClassificationWithTeacher,
	        TFDeiTForMaskedImageModeling,
	    )
	    if is_tf_available()
	    else ()
	)
	snake_case									=		(
	    {
	        '''feature-extraction''': TFDeiTModel,
	        '''image-classification''': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
	    }
	    if is_tf_available()
	    else {}
	)

	snake_case									=		False
	snake_case									=		False
	snake_case									=		False
	snake_case									=		False







	def  _lowercase    (						self				):
						snake_case_	=      TFDeiTModelTester(self				)
						snake_case_	=      ConfigTester(self						, config_class=__UpperCamelCase						, has_text_modality=__UpperCamelCase						, hidden_size=37				)







	def  _lowercase    (						self				):
						self.config_tester.run_common_tests()







	@unittest.skip(reason="DeiT does not use inputs_embeds"				)
	def  _lowercase    (						self				):
						pass







	def  _lowercase    (						self				):
						snake_case_	=      self.model_tester.prepare_config_and_inputs_for_common()

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







	def  _lowercase    (						self				):
						snake_case_	=      self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											snake_case_	=      model_class(__UpperCamelCase				)
											snake_case_	=      inspect.signature(model.call				)
											# 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]						, __UpperCamelCase				)







	def  _lowercase    (						self				):
						snake_case_	=      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__UpperCamelCase				)







	def  _lowercase    (						self				):
						snake_case_	=      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCamelCase				)







	def  _lowercase    (						self				):
						snake_case_	=      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase				)







	def  _lowercase    (						self						, UpperCAmelCase_						, UpperCAmelCase_						, UpperCAmelCase_=False				):
						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  _lowercase    (						self				):
						for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											snake_case_	=      TFDeiTModel.from_pretrained(__UpperCamelCase				)
											self.assertIsNotNone(__UpperCamelCase				)



def      __snake_case       (   ):
					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  _lowercase    (						self				):
						return (
						    DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224"				)
						    if is_vision_available()
						    else None
						)







	@slow
	def  _lowercase    (						self				):
						snake_case_	=      TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"				)

						snake_case_	=      self.default_image_processor
						snake_case_	=      prepare_img()
						snake_case_	=      image_processor(images=__UpperCamelCase						, return_tensors="tf"				)

						# forward pass
						snake_case_	=      model(**__UpperCamelCase				)

						# verify the logits
						snake_case_	=      tf.TensorShape((1, 10_00)				)
						self.assertEqual(outputs.logits.shape						, __UpperCamelCase				)

						snake_case_	=      tf.constant([-1.0_266, 0.1_912, -1.2_861]				)

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






import numpy as np

import datasets


SCREAMING_SNAKE_CASE		:     Optional[int]         =	"\nCompute the Mahalanobis Distance\n\nMahalonobis distance is the distance between a point and a distribution.\nAnd not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance.\nIt was introduced by Prof. P. C. Mahalanobis in 1936\nand has been used in various statistical applications ever since\n[source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/]\n"

SCREAMING_SNAKE_CASE		:     Dict         =	"\\n@article{de2000mahalanobis,\n  title={The mahalanobis distance},\n  author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L},\n  journal={Chemometrics and intelligent laboratory systems},\n  volume={50},\n  number={1},\n  pages={1--18},\n  year={2000},\n  publisher={Elsevier}\n}\n"

SCREAMING_SNAKE_CASE		:     List[str]         =	"\nArgs:\n    X: List of datapoints to be compared with the `reference_distribution`.\n    reference_distribution: List of datapoints from the reference distribution we want to compare to.\nReturns:\n    mahalanobis: The Mahalonobis distance for each datapoint in `X`.\nExamples:\n\n    >>> mahalanobis_metric = datasets.load_metric(\"mahalanobis\")\n    >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]])\n    >>> print(results)\n    {'mahalanobis': array([0.5])}\n"

@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION	,	_KWARGS_DESCRIPTION				)
class        UpperCamelCase						(				datasets.Metric				):





						def        A_							(self						)				->				str:
												return datasets.MetricInfo(
												    description=_DESCRIPTION			,     citation=_CITATION			,     inputs_description=_KWARGS_DESCRIPTION			,     features=datasets.Features(
												        {
												            """X""": datasets.Sequence(datasets.Value("""float"""			,     id="""sequence"""						)			,     id="""X"""						),
												        }						)			,     )





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase						)				->				int:
												# convert to numpy arrays
												UpperCamelCase_		:							int  =		np.array(__UpperCamelCase						)
												UpperCamelCase_		:							Union[str, Any]  =		np.array(__UpperCamelCase						)

												# Assert that arrays are 2D
												if len(X.shape						) != 2:
																		raise ValueError("""Expected `X` to be a 2D vector"""						)
												if len(reference_distribution.shape						) != 2:
																		raise ValueError("""Expected `reference_distribution` to be a 2D vector"""						)
												if reference_distribution.shape[0] < 2:
																		raise ValueError(
																		    """Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension"""						)

												# Get mahalanobis distance for each prediction
												UpperCamelCase_		:							str  =		X - np.mean(__UpperCamelCase						)
												UpperCamelCase_		:							Dict  =		np.cov(reference_distribution.T						)
												try:
																		UpperCamelCase_		:							str  =		np.linalg.inv(__UpperCamelCase						)
												except np.linalg.LinAlgError:
																		UpperCamelCase_		:							List[Any]  =		np.linalg.pinv(__UpperCamelCase						)
												UpperCamelCase_		:							Tuple  =		np.dot(__UpperCamelCase			,     __UpperCamelCase						)
												UpperCamelCase_		:							Tuple  =		np.dot(__UpperCamelCase			,     X_minus_mu.T						).diagonal()

												return {"mahalanobis": mahal_dist}






