"""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  _snake_case		:







				"""simple docstring"""






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



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Tuple			  =    parent
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    batch_size
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    image_size
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    patch_size
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    num_channels
											_SCREAMING_SNAKE_CASE		:						Any			  =    is_training
											_SCREAMING_SNAKE_CASE		:						Any			  =    use_labels
											_SCREAMING_SNAKE_CASE		:						int			  =    hidden_size
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    num_hidden_layers
											_SCREAMING_SNAKE_CASE		:						Any			  =    num_attention_heads
											_SCREAMING_SNAKE_CASE		:						int			  =    intermediate_size
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    hidden_act
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    hidden_dropout_prob
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    attention_probs_dropout_prob
											_SCREAMING_SNAKE_CASE		:						Any			  =    type_sequence_label_size
											_SCREAMING_SNAKE_CASE		:						Dict			  =    initializer_range
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    scope
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    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		:						Any			  =    (image_size // patch_size) ** 2
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    num_patches + 2





				def 		_lowerCAmelCase		(      self							:							Optional[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						int			  =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

											_SCREAMING_SNAKE_CASE		:						str			  =    self.get_config()

											return config, pixel_values, labels





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





				def 		_lowerCAmelCase		(      self							:							Tuple      ,    _A							:							Any      ,    _A							:							Tuple      ,    _A							:							List[str]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    TFDeiTModel(config=_A)
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    model(_A)
											self.parent.assertEqual(result.last_hidden_state.shape      ,    (self.batch_size, self.seq_length, self.hidden_size))





				def 		_lowerCAmelCase		(      self							:							List[Any]      ,    _A							:							Optional[Any]      ,    _A							:							Union[str, Any]      ,    _A							:							List[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Any			  =    TFDeiTForMaskedImageModeling(config=_A)
											_SCREAMING_SNAKE_CASE		:						Dict			  =    model(_A)
											self.parent.assertEqual(
											    result.reconstruction.shape      ,    (self.batch_size, self.num_channels, self.image_size, self.image_size))

											# test greyscale images
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    1
											_SCREAMING_SNAKE_CASE		:						str			  =    TFDeiTForMaskedImageModeling(_A)

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





				def 		_lowerCAmelCase		(      self							:							Optional[int]      ,    _A							:							Optional[int]      ,    _A							:							Union[str, Any]      ,    _A							:							Dict):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						int			  =    self.type_sequence_label_size
											_SCREAMING_SNAKE_CASE		:						Any			  =    TFDeiTForImageClassification(_A)
											_SCREAMING_SNAKE_CASE		:						str			  =    model(_A      ,    labels=_A)
											self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.type_sequence_label_size))

											# test greyscale images
											_SCREAMING_SNAKE_CASE		:						Dict			  =    1
											_SCREAMING_SNAKE_CASE		:						str			  =    TFDeiTForImageClassification(_A)

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





				def 		_lowerCAmelCase		(      self							:							Optional[int]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						int			  =    self.prepare_config_and_inputs()
											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Dict			  =    config_and_inputs
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    {"""pixel_values""": pixel_values}
											return config, inputs_dict




@require_tf
class  _snake_case		(    __snake_case   ,       __snake_case   ,       unittest.TestCase ):







				"""simple docstring"""






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

				a			=						False
				a			=						False
				a			=						False
				a			=						False





				def 		_lowerCAmelCase		(      self							:							Any):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Dict			  =    TFDeiTModelTester(self)
											_SCREAMING_SNAKE_CASE		:						int			  =    ConfigTester(self      ,    config_class=_A      ,    has_text_modality=_A      ,    hidden_size=3_7)





				def 		_lowerCAmelCase		(      self							:							Union[str, Any]):



											"""simple docstring"""







											self.config_tester.run_common_tests()





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



											"""simple docstring"""







											pass





				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Tuple			  =    self.model_tester.prepare_config_and_inputs_for_common()

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





				def 		_lowerCAmelCase		(      self							:							List[str]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Dict			  =    self.model_tester.prepare_config_and_inputs_for_common()

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

																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    ["""pixel_values"""]
																		self.assertListEqual(arg_names[:1]      ,    _A)





				def 		_lowerCAmelCase		(      self							:							List[str]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_A)





				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						int			  =    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_image_modeling(*_A)





				def 		_lowerCAmelCase		(      self							:							Dict):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_A)





				def 		_lowerCAmelCase		(      self							:							Optional[int]      ,    _A							:							List[str]      ,    _A							:							Union[str, Any]      ,    _A							:							Union[str, Any]=False):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Dict			  =    super()._prepare_for_class(_A      ,    _A      ,    return_labels=_A)

											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							:							Any):



											"""simple docstring"""







											for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    TFDeiTModel.from_pretrained(_A)
																		self.assertIsNotNone(_A)



def      lowerCamelCase_()->    str:
							_SCREAMING_SNAKE_CASE		:						List[str]			  =    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
							return image




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







				"""simple docstring"""






				@cached_property
				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







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





				@slow
				def 		_lowerCAmelCase		(      self							:							Optional[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						List[str]			  =    TFDeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224""")

											_SCREAMING_SNAKE_CASE		:						Dict			  =    self.default_image_processor
											_SCREAMING_SNAKE_CASE		:						Any			  =    prepare_img()
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    image_processor(images=_A      ,    return_tensors="""tf""")

											# forward pass
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    model(**_A)

											# verify the logits
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    tf.TensorShape((1, 1_0_0_0))
											self.assertEqual(outputs.logits.shape      ,    _A)

											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    tf.constant([-1.0_266, 0.1_912, -1.2_861])

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



"""simple docstring"""





def      lowerCamelCase_(__SCREAMING_SNAKE_CASE  )->    int:

							if not isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE  ):
														raise TypeError("""only integers accepted as input"""  )
							else:
														_SCREAMING_SNAKE_CASE		:						List[Any]			  =    str(abs(__SCREAMING_SNAKE_CASE  )  )
														_SCREAMING_SNAKE_CASE		:						List[str]			  =    [list(__SCREAMING_SNAKE_CASE  ) for char in range(len(__SCREAMING_SNAKE_CASE  )  )]
														for index in range(len(__SCREAMING_SNAKE_CASE  )  ):
																					num_transpositions[index].pop(__SCREAMING_SNAKE_CASE  )
														return max(
														    int("""""".join(list(__SCREAMING_SNAKE_CASE  )  )  ) for transposition in num_transpositions  )


if __name__ == "__main__":
			__import__('''doctest''').testmod()


