





'''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     __a					:

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







													"""simple docstring"""
													UpperCAmelCase_	:					Tuple     =     parent
													UpperCAmelCase_	:					Optional[Any]     =     batch_size
													UpperCAmelCase_	:					Optional[Any]     =     image_size
													UpperCAmelCase_	:					int     =     patch_size
													UpperCAmelCase_	:					List[str]     =     num_channels
													UpperCAmelCase_	:					List[Any]     =     is_training
													UpperCAmelCase_	:					Union[str, Any]     =     use_labels
													UpperCAmelCase_	:					Union[str, Any]     =     hidden_size
													UpperCAmelCase_	:					Dict     =     num_hidden_layers
													UpperCAmelCase_	:					Tuple     =     num_attention_heads
													UpperCAmelCase_	:					Optional[Any]     =     intermediate_size
													UpperCAmelCase_	:					Dict     =     hidden_act
													UpperCAmelCase_	:					str     =     hidden_dropout_prob
													UpperCAmelCase_	:					List[str]     =     attention_probs_dropout_prob
													UpperCAmelCase_	:					Optional[int]     =     type_sequence_label_size
													UpperCAmelCase_	:					int     =     initializer_range
													UpperCAmelCase_	:					List[Any]     =     scope
													UpperCAmelCase_	:					Optional[int]     =     encoder_stride

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

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







													"""simple docstring"""
													UpperCAmelCase_	:					List[str]     =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

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

													return config, pixel_values, labels

							def 			UpperCAmelCase__		(							self							:   int      )    ->				int:







													"""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 			UpperCAmelCase__		(							self							:   Dict				,   __magic_name__							:   Union[str, Any]				,   __magic_name__							:   List[str]				,   __magic_name__							:   Optional[Any]      )    ->				Dict:







													"""simple docstring"""
													UpperCAmelCase_	:					Tuple     =     TFDeiTModel(config=a_      )
													UpperCAmelCase_	:					str     =     model(a_      )
													self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)      )

							def 			UpperCAmelCase__		(							self							:   Optional[int]				,   __magic_name__							:   Tuple				,   __magic_name__							:   Any				,   __magic_name__							:   List[str]      )    ->				Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_	:					Dict     =     TFDeiTForMaskedImageModeling(config=a_      )
													UpperCAmelCase_	:					Any     =     model(a_      )
													self.parent.assertEqual(
													    result.reconstruction.shape				,   (self.batch_size, self.num_channels, self.image_size, self.image_size)      )

													# test greyscale images
													UpperCAmelCase_	:					Optional[Any]     =     1
													UpperCAmelCase_	:					Any     =     TFDeiTForMaskedImageModeling(a_      )

													UpperCAmelCase_	:					str     =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
													UpperCAmelCase_	:					List[str]     =     model(a_      )
													self.parent.assertEqual(result.reconstruction.shape				,   (self.batch_size, 1, self.image_size, self.image_size)      )

							def 			UpperCAmelCase__		(							self							:   Tuple				,   __magic_name__							:   List[Any]				,   __magic_name__							:   Tuple				,   __magic_name__							:   int      )    ->				Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_	:					List[str]     =     self.type_sequence_label_size
													UpperCAmelCase_	:					Union[str, Any]     =     TFDeiTForImageClassification(a_      )
													UpperCAmelCase_	:					Union[str, Any]     =     model(a_				,   labels=a_      )
													self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.type_sequence_label_size)      )

													# test greyscale images
													UpperCAmelCase_	:					str     =     1
													UpperCAmelCase_	:					str     =     TFDeiTForImageClassification(a_      )

													UpperCAmelCase_	:					Tuple     =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
													UpperCAmelCase_	:					str     =     model(a_				,   labels=a_      )
													self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.type_sequence_label_size)      )



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







													"""simple docstring"""
													UpperCAmelCase_	:					Dict     =     self.prepare_config_and_inputs()
													UpperCAmelCase_	:					Optional[Any]     =     config_and_inputs
													UpperCAmelCase_	:					Union[str, Any]     =     {'''pixel_values''': pixel_values}
													return config, inputs_dict





@require_tf
class     __a					(__snake_case				,						__snake_case				,						unittest.TestCase   ):
							__a    :      Optional[int]  	=				(
							    (
							        TFDeiTModel,
							        TFDeiTForImageClassification,
							        TFDeiTForImageClassificationWithTeacher,
							        TFDeiTForMaskedImageModeling,
							    )
							    if is_tf_available()
							    else ()
							)
							__a    :      Dict  	=				(
							    {
							        "feature-extraction": TFDeiTModel,
							        "image-classification": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
							    }
							    if is_tf_available()
							    else {}
							)

							__a    :      Dict  	=				False
							__a    :      List[Any]  	=				False
							__a    :      Union[str, Any]  	=				False
							__a    :      Optional[Any]  	=				False

							def 			UpperCAmelCase__		(							self							:   List[str]      )    ->				str:







													"""simple docstring"""
													UpperCAmelCase_	:					Dict     =     TFDeiTModelTester(self      )
													UpperCAmelCase_	:					int     =     ConfigTester(self				,   config_class=a_				,   has_text_modality=a_				,   hidden_size=37      )

							def 			UpperCAmelCase__		(							self							:   Optional[int]      )    ->				Optional[int]:







													"""simple docstring"""
													self.config_tester.run_common_tests()

							@unittest.skip(reason='''DeiT does not use inputs_embeds'''      )
							def 			UpperCAmelCase__		(							self							:   Any      )    ->				Union[str, Any]:







													"""simple docstring"""
													pass

							def 			UpperCAmelCase__		(							self							:   int      )    ->				Tuple:







													"""simple docstring"""
													UpperCAmelCase_	:					Any     =     self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																			UpperCAmelCase_	:					Optional[Any]     =     model_class(a_      )
																			self.assertIsInstance(model.get_input_embeddings()				,   (tf.keras.layers.Layer)      )
																			UpperCAmelCase_	:					int     =     model.get_output_embeddings()
																			self.assertTrue(x is None or isinstance(a_				,   tf.keras.layers.Dense      )      )

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







													"""simple docstring"""
													UpperCAmelCase_	:					List[str]     =     self.model_tester.prepare_config_and_inputs_for_common()

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

																			UpperCAmelCase_	:					Dict     =     ['''pixel_values''']
																			self.assertListEqual(arg_names[:1]				,   a_      )

							def 			UpperCAmelCase__		(							self							:   Tuple      )    ->				str:







													"""simple docstring"""
													UpperCAmelCase_	:					Optional[int]     =     self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*a_      )

							def 			UpperCAmelCase__		(							self							:   Tuple      )    ->				List[str]:







													"""simple docstring"""
													UpperCAmelCase_	:					Tuple     =     self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_image_modeling(*a_      )

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







													"""simple docstring"""
													UpperCAmelCase_	:					Optional[int]     =     self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*a_      )

							def 			UpperCAmelCase__		(							self							:   Optional[int]				,   __magic_name__							:   Any				,   __magic_name__							:   Optional[int]				,   __magic_name__							:   Any=False      )    ->				str:







													"""simple docstring"""
													UpperCAmelCase_	:					List[str]     =     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 			UpperCAmelCase__		(							self							:   Union[str, Any]      )    ->				List[Any]:







													"""simple docstring"""

													for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			UpperCAmelCase_	:					Tuple     =     TFDeiTModel.from_pretrained(a_      )
																			self.assertIsNotNone(a_      )





def 				lowerCamelCase_					(     )       ->		Union[str, Any]:
						UpperCAmelCase_	:					List[str]     =     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
						return image


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

							@cached_property
							def 			UpperCAmelCase__		(							self							:   List[Any]      )    ->				Optional[int]:







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



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







													"""simple docstring"""
													UpperCAmelCase_	:					Tuple     =     TFDeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224'''      )

													UpperCAmelCase_	:					List[str]     =     self.default_image_processor
													UpperCAmelCase_	:					Optional[Any]     =     prepare_img()
													UpperCAmelCase_	:					Dict     =     image_processor(images=a_				,   return_tensors='''tf'''      )

													# forward pass
													UpperCAmelCase_	:					Union[str, Any]     =     model(**a_      )

													# verify the logits
													UpperCAmelCase_	:					Optional[int]     =     tf.TensorShape((1, 10_00)      )
													self.assertEqual(outputs.logits.shape				,   a_      )

													UpperCAmelCase_	:					Optional[int]     =     tf.constant([-1.0_2_6_6, 0.1_9_1_2, -1.2_8_6_1]      )

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









"""simple docstring"""
def 		lowercase							(     _snake_case						:      int     ,   _snake_case						:      int     )     ->str:
	"""simple docstring"""





	if a < 0 or b < 0:
		raise ValueError('''the value of both inputs must be positive'''     )
	__snake_case	:						Tuple						=				str(bin(_snake_case     )     )[2:]  # remove the leading "0b"
	__snake_case	:						List[Any]						=				str(bin(_snake_case     )     )[2:]
	__snake_case	:						Any						=				max(len(_snake_case     )     ,   len(_snake_case     )     )
	return "0b" + "".join(
	    str(int('''1''' in (char_a, char_b)     )     )
	    for char_a, char_b in zip(a_binary.zfill(_snake_case     )     ,   b_binary.zfill(_snake_case     )     )     )


if __name__ == "__main__":
			import doctest

			doctest.testmod()





