


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:


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




											'''simple docstring'''

											__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 							SCREAMING_SNAKE_CASE_  (						self	:		Union[str, Any]						)			->	Any:




											'''simple docstring'''

											__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 							SCREAMING_SNAKE_CASE_  (						self	:		int						)			->	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=SCREAMING_SNAKE_CASE     ,    initializer_range=self.initializer_range     ,    encoder_stride=self.encoder_stride     ,    )


					def 							SCREAMING_SNAKE_CASE_  (						self	:		str     ,    SCREAMING_SNAKE_CASE	:		Optional[int]     ,    SCREAMING_SNAKE_CASE	:		Tuple     ,    SCREAMING_SNAKE_CASE	:		int						)			->	Dict:




											'''simple docstring'''

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


					def 							SCREAMING_SNAKE_CASE_  (						self	:		int     ,    SCREAMING_SNAKE_CASE	:		Dict     ,    SCREAMING_SNAKE_CASE	:		int     ,    SCREAMING_SNAKE_CASE	:		Tuple						)			->	Optional[Any]:




											'''simple docstring'''

											__snake_case				=		TFDeiTForMaskedImageModeling(config=SCREAMING_SNAKE_CASE						)
											__snake_case				=		model(SCREAMING_SNAKE_CASE						)
											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(SCREAMING_SNAKE_CASE						)

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


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




											'''simple docstring'''

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

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

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






					def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[Any]						)			->	Optional[Any]:




											'''simple docstring'''

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






@require_tf
class     UpperCamelCase(  _a     ,							_a     ,							unittest.TestCase   ):
					snake_case_		:				Union[str, Any]										=	(
					    (
					        TFDeiTModel,
					        TFDeiTForImageClassification,
					        TFDeiTForImageClassificationWithTeacher,
					        TFDeiTForMaskedImageModeling,
					    )
					    if is_tf_available()
					    else ()
					)
					snake_case_		:				Optional[Any]										=	(
					    {
					        """feature-extraction""": TFDeiTModel,
					        """image-classification""": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
					    }
					    if is_tf_available()
					    else {}
					)

					snake_case_		:				Union[str, Any]										=	False
					snake_case_		:				int										=	False
					snake_case_		:				str										=	False
					snake_case_		:				str										=	False


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Any						)			->	str:




											'''simple docstring'''

											__snake_case				=		TFDeiTModelTester(self						)
											__snake_case				=		ConfigTester(self     ,    config_class=SCREAMING_SNAKE_CASE     ,    has_text_modality=SCREAMING_SNAKE_CASE     ,    hidden_size=3_7						)


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




											'''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[str]:




											'''simple docstring'''

											pass


					def 							SCREAMING_SNAKE_CASE_  (						self	:		int						)			->	int:




											'''simple docstring'''

											__snake_case		,					__snake_case				=		self.model_tester.prepare_config_and_inputs_for_common()

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


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Tuple						)			->	List[Any]:




											'''simple docstring'''

											__snake_case		,					__snake_case				=		self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	__snake_case				=		model_class(SCREAMING_SNAKE_CASE						)
																	__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]     ,    SCREAMING_SNAKE_CASE						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		str						)			->	int:




											'''simple docstring'''

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


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




											'''simple docstring'''

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


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




											'''simple docstring'''

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


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




											'''simple docstring'''

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

											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	:		Optional[int]						)			->	List[Any]:




											'''simple docstring'''

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





def 	_lowerCAmelCase       (    )						->	Any:


						'''simple docstring'''






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






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


					@cached_property
					def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[Any]						)			->	Tuple:




											'''simple docstring'''

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






					@slow
					def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[Any]						)			->	Union[str, Any]:




											'''simple docstring'''

											__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=SCREAMING_SNAKE_CASE     ,    return_tensors="tf"						)

											# forward pass
											__snake_case				=		model(**SCREAMING_SNAKE_CASE						)

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

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

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









from sklearn.metrics import mean_squared_error

import datasets


A							:					List[Any]     						=     '\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n'

A							:					Optional[int]     						=     '\\nMean Squared Error(MSE) is the average of the square of difference between the predicted\nand actual values.\n'


A							:					str     						=     '\nArgs:\n    predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)\n        Estimated target values.\n    references: array-like of shape (n_samples,) or (n_samples, n_outputs)\n        Ground truth (correct) target values.\n    sample_weight: array-like of shape (n_samples,), default=None\n        Sample weights.\n    multioutput: {"raw_values", "uniform_average"} or array-like of shape (n_outputs,), default="uniform_average"\n        Defines aggregating of multiple output values. Array-like value defines weights used to average errors.\n\n        "raw_values" : Returns a full set of errors in case of multioutput input.\n\n        "uniform_average" : Errors of all outputs are averaged with uniform weight.\n\n    squared : bool, default=True\n        If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value.\n\nReturns:\n    mse : mean squared error.\nExamples:\n\n    >>> mse_metric = datasets.load_metric("mse")\n    >>> predictions = [2.5, 0.0, 2, 8]\n    >>> references = [3, -0.5, 2, 7]\n    >>> results = mse_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'mse\': 0.375}\n    >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False)\n    >>> print(rmse_result)\n    {\'mse\': 0.6123724356957945}\n\n    If you\'re using multi-dimensional lists, then set the config as follows :\n\n    >>> mse_metric = datasets.load_metric("mse", "multilist")\n    >>> predictions = [[0.5, 1], [-1, 1], [7, -6]]\n    >>> references = [[0, 2], [-1, 2], [8, -5]]\n    >>> results = mse_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'mse\': 0.7083333333333334}\n    >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput=\'raw_values\')\n    >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n    {\'mse\': array([0.41666667, 1. ])}\n'






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


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




											'''simple docstring'''

											return datasets.MetricInfo(
											    description=_DESCRIPTION     ,    citation=_CITATION     ,    inputs_description=_KWARGS_DESCRIPTION     ,    features=datasets.Features(self._get_feature_types()						)     ,    reference_urls=[
											        "https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html"
											    ]     ,    )


					def 							SCREAMING_SNAKE_CASE_  (						self	:		List[str]						)			->	Dict:




											'''simple docstring'''

											if self.config_name == "multilist":
																	return {
																	    "predictions": datasets.Sequence(datasets.Value("float"						)						),
																	    "references": datasets.Sequence(datasets.Value("float"						)						),
																	}
											else:
																	return {
																	    "predictions": datasets.Value("float"						),
																	    "references": datasets.Value("float"						),
																	}






					def 							SCREAMING_SNAKE_CASE_  (						self	:		Dict     ,    SCREAMING_SNAKE_CASE	:		int     ,    SCREAMING_SNAKE_CASE	:		List[Any]     ,    SCREAMING_SNAKE_CASE	:		str=None     ,    SCREAMING_SNAKE_CASE	:		int="uniform_average"     ,    SCREAMING_SNAKE_CASE	:		Optional[int]=True						)			->	Tuple:




											'''simple docstring'''

											__snake_case				=		mean_squared_error(
											    SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE     ,    sample_weight=SCREAMING_SNAKE_CASE     ,    multioutput=SCREAMING_SNAKE_CASE     ,    squared=SCREAMING_SNAKE_CASE						)

											return {"mse": mse}





