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			,     __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			,     )				->				List[str]:
												UpperCamelCase_		:							List[Any]  =		parent
												UpperCamelCase_		:							int  =		batch_size
												UpperCamelCase_		:							Tuple  =		image_size
												UpperCamelCase_		:							Optional[int]  =		patch_size
												UpperCamelCase_		:							List[Any]  =		num_channels
												UpperCamelCase_		:							Dict  =		is_training
												UpperCamelCase_		:							Optional[int]  =		use_labels
												UpperCamelCase_		:							Optional[Any]  =		hidden_size
												UpperCamelCase_		:							Optional[Any]  =		num_hidden_layers
												UpperCamelCase_		:							int  =		num_attention_heads
												UpperCamelCase_		:							Dict  =		intermediate_size
												UpperCamelCase_		:							str  =		hidden_act
												UpperCamelCase_		:							Any  =		hidden_dropout_prob
												UpperCamelCase_		:							List[Any]  =		attention_probs_dropout_prob
												UpperCamelCase_		:							List[Any]  =		type_sequence_label_size
												UpperCamelCase_		:							List[Any]  =		initializer_range
												UpperCamelCase_		:							Dict  =		scope
												UpperCamelCase_		:							Union[str, Any]  =		encoder_stride

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





						def        A_							(self						)				->				Dict:
												UpperCamelCase_		:							List[Any]  =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

												UpperCamelCase_		:							Optional[Any]  =		self.get_config()

												return config, pixel_values, labels





						def        A_							(self						)				->				Optional[int]:
												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        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)				->				int:
												UpperCamelCase_		:							Optional[Any]  =		TFDeiTModel(config=__UpperCamelCase						)
												UpperCamelCase_		:							Any  =		model(__UpperCamelCase						)
												self.parent.assertEqual(result.last_hidden_state.shape			,     (self.batch_size, self.seq_length, self.hidden_size)						)





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)				->				Optional[Any]:
												UpperCamelCase_		:							int  =		TFDeiTForMaskedImageModeling(config=__UpperCamelCase						)
												UpperCamelCase_		:							Any  =		model(__UpperCamelCase						)
												self.parent.assertEqual(
												    result.reconstruction.shape			,     (self.batch_size, self.num_channels, self.image_size, self.image_size)						)

												# test greyscale images
												UpperCamelCase_		:							Tuple  =		1
												UpperCamelCase_		:							List[str]  =		TFDeiTForMaskedImageModeling(__UpperCamelCase						)

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





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)				->				List[str]:
												UpperCamelCase_		:							Union[str, Any]  =		self.type_sequence_label_size
												UpperCamelCase_		:							str  =		TFDeiTForImageClassification(__UpperCamelCase						)
												UpperCamelCase_		:							Tuple  =		model(__UpperCamelCase			,     labels=__UpperCamelCase						)
												self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.type_sequence_label_size)						)

												# test greyscale images
												UpperCamelCase_		:							Union[str, Any]  =		1
												UpperCamelCase_		:							List[Any]  =		TFDeiTForImageClassification(__UpperCamelCase						)

												UpperCamelCase_		:							str  =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
												UpperCamelCase_		:							Optional[Any]  =		model(__UpperCamelCase			,     labels=__UpperCamelCase						)
												self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.type_sequence_label_size)						)





						def        A_							(self						)				->				List[str]:
												UpperCamelCase_		:							Tuple  =		self.prepare_config_and_inputs()
												UpperCamelCase_,UpperCamelCase_,UpperCamelCase_		:							Optional[int]  =		config_and_inputs
												UpperCamelCase_		:							Tuple  =		{"""pixel_values""": pixel_values}
												return config, inputs_dict


@require_tf
class        UpperCamelCase						(				__a	,	__a	,	unittest.TestCase				):
						a__		:List[str]      =			(
						    (
						        TFDeiTModel,
						        TFDeiTForImageClassification,
						        TFDeiTForImageClassificationWithTeacher,
						        TFDeiTForMaskedImageModeling,
						    )
						    if is_tf_available()
						    else ()
						)
						a__		:List[str]      =			(
						    {
						        '''feature-extraction''': TFDeiTModel,
						        '''image-classification''': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
						    }
						    if is_tf_available()
						    else {}
						)

						a__		:Dict      =			False
						a__		:List[str]      =			False
						a__		:List[Any]      =			False
						a__		:Dict      =			False





						def        A_							(self						)				->				Dict:
												UpperCamelCase_		:							Union[str, Any]  =		TFDeiTModelTester(self						)
												UpperCamelCase_		:							int  =		ConfigTester(self			,     config_class=__UpperCamelCase			,     has_text_modality=__UpperCamelCase			,     hidden_size=37						)





						def        A_							(self						)				->				List[Any]:
												self.config_tester.run_common_tests()





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





						def        A_							(self						)				->				int:
												UpperCamelCase_,UpperCamelCase_		:							Any  =		self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		UpperCamelCase_		:							List[Any]  =		model_class(__UpperCamelCase						)
																		self.assertIsInstance(model.get_input_embeddings()			,     (tf.keras.layers.Layer)						)
																		UpperCamelCase_		:							Union[str, Any]  =		model.get_output_embeddings()
																		self.assertTrue(x is None or isinstance(__UpperCamelCase			,     tf.keras.layers.Dense						)						)





						def        A_							(self						)				->				str:
												UpperCamelCase_,UpperCamelCase_		:							int  =		self.model_tester.prepare_config_and_inputs_for_common()

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

																		UpperCamelCase_		:							Union[str, Any]  =		["""pixel_values"""]
																		self.assertListEqual(arg_names[:1]			,     __UpperCamelCase						)





						def        A_							(self						)				->				Union[str, Any]:
												UpperCamelCase_		:							Union[str, Any]  =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__UpperCamelCase						)





						def        A_							(self						)				->				Tuple:
												UpperCamelCase_		:							Optional[Any]  =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCamelCase						)





						def        A_							(self						)				->				Dict:
												UpperCamelCase_		:							Tuple  =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase						)





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase=False						)				->				List[Any]:
												UpperCamelCase_		:							List[Any]  =		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        A_							(self						)				->				Union[str, Any]:
												for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		UpperCamelCase_		:							List[str]  =		TFDeiTModel.from_pretrained(__UpperCamelCase						)
																		self.assertIsNotNone(__UpperCamelCase						)






def     lowerCAmelCase_			(   ):
						UpperCamelCase_		:							List[str]  =		Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
						return image







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





						@cached_property
						def        A_							(self						)				->				int:
												return (
												    DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224"""						)
												    if is_vision_available()
												    else None
												)





						@slow
						def        A_							(self						)				->				List[Any]:
												UpperCamelCase_		:							Union[str, Any]  =		TFDeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""						)

												UpperCamelCase_		:							Optional[int]  =		self.default_image_processor
												UpperCamelCase_		:							Tuple  =		prepare_img()
												UpperCamelCase_		:							List[str]  =		image_processor(images=__UpperCamelCase			,     return_tensors="""tf"""						)

												# forward pass
												UpperCamelCase_		:							str  =		model(**__UpperCamelCase						)

												# verify the logits
												UpperCamelCase_		:							List[Any]  =		tf.TensorShape((1, 1_000)						)
												self.assertEqual(outputs.logits.shape			,     __UpperCamelCase						)

												UpperCamelCase_		:							Union[str, Any]  =		tf.constant([-1.0_266, 0.1_912, -1.2_861]						)

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







import unittest

from transformers import load_tool
from transformers.utils import is_torch_available


if is_torch_available():
	import torch

from transformers.testing_utils import require_torch

from .test_tools_common import ToolTesterMixin

@require_torch
class        UpperCamelCase						(				unittest.TestCase	,	__a				):





						def        A_							(self						)				->				Tuple:
												UpperCamelCase_		:							Any  =		load_tool("""text-to-speech"""						)
												self.tool.setup()





						def        A_							(self						)				->				Dict:
												# SpeechT5 isn't deterministic
												torch.manual_seed(0						)
												UpperCamelCase_		:							Optional[Any]  =		self.tool("""hey"""						)
												UpperCamelCase_		:							Optional[Any]  =		result.to_raw()
												self.assertTrue(
												    torch.allclose(
												        resulting_tensor[:3]			,     torch.tensor([-0.0_005_966_668_832_115_829, -0.0_003_657_640_190_795_064, -0.00_013_439_502_799_883_485]						)			,     )						)





						def        A_							(self						)				->				Dict:
												# SpeechT5 isn't deterministic
												torch.manual_seed(0						)
												UpperCamelCase_		:							Any  =		self.tool("""hey"""						)
												UpperCamelCase_		:							Tuple  =		result.to_raw()
												self.assertTrue(
												    torch.allclose(
												        resulting_tensor[:3]			,     torch.tensor([-0.0_005_966_668_832_115_829, -0.0_003_657_640_190_795_064, -0.00_013_439_502_799_883_485]						)			,     )						)






