


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        _a							:

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





									"""simple docstring"""







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

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

							def       lowerCamelCase_ (							self:	Tuple							)			->   int:





									"""simple docstring"""







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

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

									lowercase__            =      self.get_config()

									return config, pixel_values, labels

							def       lowerCamelCase_ (							self:	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=UpperCamelCase_    ,						initializer_range=self.initializer_range    ,						encoder_stride=self.encoder_stride    ,						)

							def       lowerCamelCase_ (							self:	Optional[int]    ,						UpperCamelCase_:	Union[str, Any]    ,						UpperCamelCase_:	Union[str, Any]    ,						UpperCamelCase_:	Union[str, Any]							)			->   List[Any]:





									"""simple docstring"""







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

							def       lowerCamelCase_ (							self:	List[str]    ,						UpperCamelCase_:	int    ,						UpperCamelCase_:	str    ,						UpperCamelCase_:	int							)			->   Any:





									"""simple docstring"""







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

									# test greyscale images
									lowercase__            =      1
									lowercase__            =      TFDeiTForMaskedImageModeling(UpperCamelCase_							)

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

							def       lowerCamelCase_ (							self:	Optional[int]    ,						UpperCamelCase_:	Union[str, Any]    ,						UpperCamelCase_:	Optional[int]    ,						UpperCamelCase_:	Tuple							)			->   Dict:





									"""simple docstring"""







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

									# test greyscale images
									lowercase__            =      1
									lowercase__            =      TFDeiTForImageClassification(UpperCamelCase_							)

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


							def       lowerCamelCase_ (							self:	Optional[int]							)			->   Union[str, Any]:





									"""simple docstring"""







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


@require_tf
class        _a							(			UpperCamelCase__     ,	UpperCamelCase__     ,	unittest.TestCase			):
							_lowercase	:			Dict        = (
							    (
							        TFDeiTModel,
							        TFDeiTForImageClassification,
							        TFDeiTForImageClassificationWithTeacher,
							        TFDeiTForMaskedImageModeling,
							    )
							    if is_tf_available()
							    else ()
							)
							_lowercase	:			int        = (
							    {
							        '''feature-extraction''': TFDeiTModel,
							        '''image-classification''': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
							    }
							    if is_tf_available()
							    else {}
							)

							_lowercase	:			Tuple        = False
							_lowercase	:			Dict        = False
							_lowercase	:			Any        = False
							_lowercase	:			Optional[int]        = False

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





									"""simple docstring"""







									lowercase__            =      TFDeiTModelTester(self							)
									lowercase__            =      ConfigTester(self    ,						config_class=UpperCamelCase_    ,						has_text_modality=UpperCamelCase_    ,						hidden_size=37							)

							def       lowerCamelCase_ (							self:	List[str]							)			->   int:





									"""simple docstring"""







									self.config_tester.run_common_tests()

							@unittest.skip(reason='''DeiT does not use inputs_embeds'''							)
							def       lowerCamelCase_ (							self:	Optional[int]							)			->   Dict:





									"""simple docstring"""







									pass

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





									"""simple docstring"""







									lowercase__     ,		lowercase__            =      self.model_tester.prepare_config_and_inputs_for_common()

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

							def       lowerCamelCase_ (							self:	Dict							)			->   str:





									"""simple docstring"""







									lowercase__     ,		lowercase__            =      self.model_tester.prepare_config_and_inputs_for_common()

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

											lowercase__            =      ['''pixel_values''']
											self.assertListEqual(arg_names[:1]    ,						UpperCamelCase_							)

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





									"""simple docstring"""







									lowercase__            =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*UpperCamelCase_							)

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





									"""simple docstring"""







									lowercase__            =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase_							)

							def       lowerCamelCase_ (							self:	int							)			->   List[str]:





									"""simple docstring"""







									lowercase__            =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_							)

							def       lowerCamelCase_ (							self:	Any    ,						UpperCamelCase_:	Optional[Any]    ,						UpperCamelCase_:	List[str]    ,						UpperCamelCase_:	Dict=False							)			->   Union[str, Any]:





									"""simple docstring"""







									lowercase__            =      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       lowerCamelCase_ (							self:	List[str]							)			->   str:





									"""simple docstring"""







									for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowercase__            =      TFDeiTModel.from_pretrained(UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)
def 			_a (      ):


		"""simple docstring"""




		lowercase__            =      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''     )
		return image


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

							@cached_property
							def       lowerCamelCase_ (							self:	Dict							)			->   Dict:





									"""simple docstring"""







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


							@slow
							def       lowerCamelCase_ (							self:	Dict							)			->   Dict:





									"""simple docstring"""







									lowercase__            =      TFDeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224'''							)

									lowercase__            =      self.default_image_processor
									lowercase__            =      prepare_img()
									lowercase__            =      image_processor(images=UpperCamelCase_    ,						return_tensors='''tf'''							)

									# forward pass
									lowercase__            =      model(**UpperCamelCase_							)

									# verify the logits
									lowercase__            =      tf.TensorShape((1, 1_000)							)
									self.assertEqual(outputs.logits.shape    ,						UpperCamelCase_							)

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

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






def 			_a (      SCREAMING_SNAKE_CASE						,				SCREAMING_SNAKE_CASE						,				SCREAMING_SNAKE_CASE     ):


		"""simple docstring"""




		if exponent == 1:
				return base
		if exponent % 2 == 0:
				lowercase__            =      _modexpt(SCREAMING_SNAKE_CASE						,				exponent // 2						,				SCREAMING_SNAKE_CASE     ) % modulo_value
				return (x * x) % modulo_value
		else:
				return (base * _modexpt(SCREAMING_SNAKE_CASE						,				exponent - 1						,				SCREAMING_SNAKE_CASE     )) % modulo_value
def 			_a (      SCREAMING_SNAKE_CASE = 17_77						,				SCREAMING_SNAKE_CASE = 18_55						,				SCREAMING_SNAKE_CASE = 8     ):


		"""simple docstring"""




		lowercase__            =      base
		for _ in range(1						,				SCREAMING_SNAKE_CASE     ):
				lowercase__            =      _modexpt(SCREAMING_SNAKE_CASE						,				SCREAMING_SNAKE_CASE						,				10**digits     )

		return result


if __name__ == "__main__":
			print(f"""{solution() = }""")


