


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



	def __init__(					self ,					snake_case__ ,					snake_case__=13 ,					snake_case__=30 ,					snake_case__=2 ,					snake_case__=3 ,					snake_case__=True ,					snake_case__=True ,					snake_case__=32 ,					snake_case__=2 ,					snake_case__=4 ,					snake_case__=37 ,					snake_case__="gelu" ,					snake_case__=0.1 ,					snake_case__=0.1 ,					snake_case__=10 ,					snake_case__=0.0_2 ,					snake_case__=3 ,					snake_case__=None ,					snake_case__=2 ,					):
		lowerCAmelCase					:    List[Any]      							= parent
		lowerCAmelCase					:    str      							= batch_size
		lowerCAmelCase					:    str      							= image_size
		lowerCAmelCase					:    str      							= patch_size
		lowerCAmelCase					:    Dict      							= num_channels
		lowerCAmelCase					:    List[Any]      							= is_training
		lowerCAmelCase					:    Any      							= use_labels
		lowerCAmelCase					:    List[str]      							= hidden_size
		lowerCAmelCase					:    Any      							= num_hidden_layers
		lowerCAmelCase					:    List[str]      							= num_attention_heads
		lowerCAmelCase					:    Union[str, Any]      							= intermediate_size
		lowerCAmelCase					:    int      							= hidden_act
		lowerCAmelCase					:    Union[str, Any]      							= hidden_dropout_prob
		lowerCAmelCase					:    Dict      							= attention_probs_dropout_prob
		lowerCAmelCase					:    str      							= type_sequence_label_size
		lowerCAmelCase					:    int      							= initializer_range
		lowerCAmelCase					:    int      							= scope
		lowerCAmelCase					:    str      							= encoder_stride

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



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    int      							= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

		lowerCAmelCase					:    Tuple      							= None
		if self.use_labels:
			lowerCAmelCase					:    Union[str, Any]      							= ids_tensor([self.batch_size] ,					self.type_sequence_label_size    )

		lowerCAmelCase					:    Any      							= self.get_config()

		return config, pixel_values, labels



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



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__    ):
		lowerCAmelCase					:    Tuple      							= TFDeiTModel(config=snake_case__    )
		lowerCAmelCase					:    str      							= model(snake_case__    )
		self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)    )



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__    ):
		lowerCAmelCase					:    List[str]      							= TFDeiTForMaskedImageModeling(config=snake_case__    )
		lowerCAmelCase					:    int      							= model(snake_case__    )
		self.parent.assertEqual(
		    result.reconstruction.shape ,					(self.batch_size, self.num_channels, self.image_size, self.image_size)    )

		# test greyscale images
		lowerCAmelCase					:    int      							= 1
		lowerCAmelCase					:    Optional[int]      							= TFDeiTForMaskedImageModeling(snake_case__    )

		lowerCAmelCase					:    Optional[int]      							= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
		lowerCAmelCase					:    Any      							= model(snake_case__    )
		self.parent.assertEqual(result.reconstruction.shape ,					(self.batch_size, 1, self.image_size, self.image_size)    )



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__    ):
		lowerCAmelCase					:    Tuple      							= self.type_sequence_label_size
		lowerCAmelCase					:    str      							= TFDeiTForImageClassification(snake_case__    )
		lowerCAmelCase					:    List[Any]      							= model(snake_case__ ,					labels=snake_case__    )
		self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.type_sequence_label_size)    )

		# test greyscale images
		lowerCAmelCase					:    Any      							= 1
		lowerCAmelCase					:    Any      							= TFDeiTForImageClassification(snake_case__    )

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



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Union[str, Any]      							= self.prepare_config_and_inputs()
		lowerCAmelCase					:    Dict      							= config_and_inputs
		lowerCAmelCase					:    List[Any]      							= {'pixel_values': pixel_values}
		return config, inputs_dict



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

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



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    List[str]      							= TFDeiTModelTester(self    )
		lowerCAmelCase					:    List[str]      							= ConfigTester(self ,					config_class=snake_case__ ,					has_text_modality=snake_case__ ,					hidden_size=37    )



	def 				lowercase     (					self    ):
		self.config_tester.run_common_tests()



	@unittest.skip(reason='DeiT does not use inputs_embeds'    )
	def 				lowercase     (					self    ):
		pass



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Optional[Any]      							= self.model_tester.prepare_config_and_inputs_for_common()

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



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Optional[int]      							= self.model_tester.prepare_config_and_inputs_for_common()

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

			lowerCAmelCase					:    Optional[Any]      							= ['pixel_values']
			self.assertListEqual(arg_names[:1] ,					snake_case__    )



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    str      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_model(*snake_case__    )



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Optional[int]      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_masked_image_modeling(*snake_case__    )



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    List[str]      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_image_classification(*snake_case__    )



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__=False    ):
		lowerCAmelCase					:    List[str]      							= super()._prepare_for_class(snake_case__ ,					snake_case__ ,					return_labels=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 				lowercase     (					self    ):
		for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			lowerCAmelCase					:    Tuple      							= TFDeiTModel.from_pretrained(snake_case__    )
			self.assertIsNotNone(snake_case__    )


def 			__UpperCamelCase    (    )    ->      Optional[int]:




	"""simple docstring"""





	lowerCAmelCase					:    Union[str, Any]      							= Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
	return image






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



	@cached_property
	def 				lowercase     (					self    ):
		return (
		    DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224'    )
		    if is_vision_available()
		    else None
		)



	@slow
	def 				lowercase     (					self    ):
		lowerCAmelCase					:    str      							= TFDeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224'    )

		lowerCAmelCase					:    Tuple      							= self.default_image_processor
		lowerCAmelCase					:    List[str]      							= prepare_img()
		lowerCAmelCase					:    Tuple      							= image_processor(images=snake_case__ ,					return_tensors='tf'    )

		# forward pass
		lowerCAmelCase					:    int      							= model(**snake_case__    )

		# verify the logits
		lowerCAmelCase					:    Optional[int]      							= tf.TensorShape((1, 1000)    )
		self.assertEqual(outputs.logits.shape ,					snake_case__    )

		lowerCAmelCase					:    Tuple      							= 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] ,					snake_case__ ,					atol=1e-4    )    )





'''simple docstring'''




_lowerCAmelCase  :		List[str]		     =	{str(digit): digit**5 for digit in range(10)}

def 			__UpperCamelCase    (    _A    :				int				)    ->      int:




	"""simple docstring"""





	return sum(DIGITS_FIFTH_POWER[digit] for digit in str(_A				)				)

def 			__UpperCamelCase    (    )    ->      int:




	"""simple docstring"""





	return sum(
	    number
	    for number in range(10_00					,   1_00_00_00				)
	    if number == digits_fifth_powers_sum(_A				)				)


if __name__ == "__main__":
				print(solution())
