





import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch
						from torch import nn

						from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
						from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import ViTImageProcessor

class SCREAMING_SNAKE_CASE_ :





					"""simple docstring"""


					def __init__( self	,  _lowerCAmelCase	,  _lowerCAmelCase=13	,  _lowerCAmelCase=30	,  _lowerCAmelCase=2	,  _lowerCAmelCase=3	,  _lowerCAmelCase=True	,  _lowerCAmelCase=True	,  _lowerCAmelCase=32	,  _lowerCAmelCase=5	,  _lowerCAmelCase=4	,  _lowerCAmelCase=37	,  _lowerCAmelCase="gelu"	,  _lowerCAmelCase=0.1	,  _lowerCAmelCase=0.1	,  _lowerCAmelCase=10	,  _lowerCAmelCase=0.02	,  _lowerCAmelCase=None	,  _lowerCAmelCase=2	,  ):
										lowerCamelCase__       =       parent
										lowerCamelCase__       =       batch_size
										lowerCamelCase__       =       image_size
										lowerCamelCase__       =       patch_size
										lowerCamelCase__       =       num_channels
										lowerCamelCase__       =       is_training
										lowerCamelCase__       =       use_labels
										lowerCamelCase__       =       hidden_size
										lowerCamelCase__       =       num_hidden_layers
										lowerCamelCase__       =       num_attention_heads
										lowerCamelCase__       =       intermediate_size
										lowerCamelCase__       =       hidden_act
										lowerCamelCase__       =       hidden_dropout_prob
										lowerCamelCase__       =       attention_probs_dropout_prob
										lowerCamelCase__       =       type_sequence_label_size
										lowerCamelCase__       =       initializer_range
										lowerCamelCase__       =       scope
										lowerCamelCase__       =       encoder_stride

										# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
										lowerCamelCase__       =       (image_size // patch_size) ** 2
										lowerCamelCase__       =       num_patches + 1





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

										lowerCamelCase__       =       self.get_config()

										return config, pixel_values, labels





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





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										lowerCamelCase__       =       ViTModel(config=_lowerCAmelCase	)
										model.to(_lowerCAmelCase	)
										model.eval()
										lowerCamelCase__       =       model(_lowerCAmelCase	)
										self.parent.assertEqual(result.last_hidden_state.shape	,  (self.batch_size, self.seq_length, self.hidden_size)	)





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										lowerCamelCase__       =       ViTForMaskedImageModeling(config=_lowerCAmelCase	)
										model.to(_lowerCAmelCase	)
										model.eval()
										lowerCamelCase__       =       model(_lowerCAmelCase	)
										self.parent.assertEqual(
										    result.reconstruction.shape	,  (self.batch_size, self.num_channels, self.image_size, self.image_size)	)

										# test greyscale images
										lowerCamelCase__       =       1
										lowerCamelCase__       =       ViTForMaskedImageModeling(_lowerCAmelCase	)
										model.to(_lowerCAmelCase	)
										model.eval()

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





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										lowerCamelCase__       =       self.type_sequence_label_size
										lowerCamelCase__       =       ViTForImageClassification(_lowerCAmelCase	)
										model.to(_lowerCAmelCase	)
										model.eval()
										lowerCamelCase__       =       model(_lowerCAmelCase	,  labels=_lowerCAmelCase	)
										self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.type_sequence_label_size)	)

										# test greyscale images
										lowerCamelCase__       =       1
										lowerCamelCase__       =       ViTForImageClassification(_lowerCAmelCase	)
										model.to(_lowerCAmelCase	)
										model.eval()

										lowerCamelCase__       =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
										lowerCamelCase__       =       model(_lowerCAmelCase	)
										self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.type_sequence_label_size)	)






					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.prepare_config_and_inputs()
										(
										    (
										    lowerCamelCase__		
										)    ,		(
										    lowerCamelCase__		
										)    ,		(
										    lowerCamelCase__		
										)    ,				
										)       =       config_and_inputs
										lowerCamelCase__       =       {"pixel_values": pixel_values}
										return config, inputs_dict

@require_torch
class SCREAMING_SNAKE_CASE_ (					lowercase_							,       lowercase_							,       unittest.TestCase   ):





					"""simple docstring"""


					A__     :   str				 =    (
					    (
					        ViTModel,
					        ViTForImageClassification,
					        ViTForMaskedImageModeling,
					    )
					    if is_torch_available()
					    else ()
					)
					A__     :   Optional[int]				 =    (
					    {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification}
					    if is_torch_available()
					    else {}
					)
					A__     :   List[Any]				 =    True

					A__     :   int				 =    False
					A__     :   Union[str, Any]				 =    False
					A__     :   Optional[int]				 =    False





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       ViTModelTester(self	)
										lowerCamelCase__       =       ConfigTester(self	,  config_class=_lowerCAmelCase	,  has_text_modality=_lowerCAmelCase	,  hidden_size=37	)





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





					@unittest.skip(reason="ViT does not use inputs_embeds"	)
					def 					__magic_name__ ( self	):
										pass





					def 					__magic_name__ ( self	):
										lowerCamelCase__    ,		lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
															lowerCamelCase__       =       model_class(_lowerCAmelCase	)
															self.assertIsInstance(model.get_input_embeddings()	,  (nn.Module)	)
															lowerCamelCase__       =       model.get_output_embeddings()
															self.assertTrue(x is None or isinstance(_lowerCAmelCase	,  nn.Linear	)	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__    ,		lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs_for_common()

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

															lowerCamelCase__       =       ["pixel_values"]
															self.assertListEqual(arg_names[:1]	,  _lowerCAmelCase	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_lowerCAmelCase	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_image_modeling(*_lowerCAmelCase	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase	)






					@slow
					def 					__magic_name__ ( self	):
										for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowerCamelCase__       =       ViTModel.from_pretrained(_lowerCAmelCase	)
															self.assertIsNotNone(_lowerCAmelCase	)





def    __UpperCamelCase      (    ) ->Tuple:
					lowerCamelCase__       =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
					return image







@require_torch
@require_vision
class SCREAMING_SNAKE_CASE_ (					unittest.TestCase   ):





					"""simple docstring"""


					@cached_property
					def 					__magic_name__ ( self	):
										return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"	) if is_vision_available() else None





					@slow
					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       ViTForImageClassification.from_pretrained("google/vit-base-patch16-224"	).to(_lowerCAmelCase	)

										lowerCamelCase__       =       self.default_image_processor
										lowerCamelCase__       =       prepare_img()
										lowerCamelCase__       =       image_processor(images=_lowerCAmelCase	,  return_tensors="pt"	).to(_lowerCAmelCase	)

										# forward pass
										with torch.no_grad():
															lowerCamelCase__       =       model(**_lowerCAmelCase	)

										# verify the logits
										lowerCamelCase__       =       torch.Size((1, 1000)	)
										self.assertEqual(outputs.logits.shape	,  _lowerCAmelCase	)

										lowerCamelCase__       =       torch.tensor([-0.2_744, 0.8_215, -0.0_836]	).to(_lowerCAmelCase	)

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





					@slow
					def 					__magic_name__ ( self	):
										# ViT models have an `interpolate_pos_encoding` argument in their forward method,
										# allowing to interpolate the pre-trained position embeddings in order to use
										# the model on higher resolutions. The DINO model by Facebook AI leverages this
										# to visualize self-attention on higher resolution images.
										lowerCamelCase__       =       ViTModel.from_pretrained("facebook/dino-vits8"	).to(_lowerCAmelCase	)

										lowerCamelCase__       =       ViTImageProcessor.from_pretrained("facebook/dino-vits8"	,  size=480	)
										lowerCamelCase__       =       prepare_img()
										lowerCamelCase__       =       image_processor(images=_lowerCAmelCase	,  return_tensors="pt"	)
										lowerCamelCase__       =       inputs.pixel_values.to(_lowerCAmelCase	)

										# forward pass
										with torch.no_grad():
															lowerCamelCase__       =       model(_lowerCAmelCase	,  interpolate_pos_encoding=_lowerCAmelCase	)

										# verify the logits
										lowerCamelCase__       =       torch.Size((1, 3601, 384)	)
										self.assertEqual(outputs.last_hidden_state.shape	,  _lowerCAmelCase	)

										lowerCamelCase__       =       torch.tensor(
										    [[4.2_340, 4.3_906, -6.6_692], [4.5_463, 1.8_928, -6.7_257], [4.4_429, 0.8_496, -5.8_585]]	).to(_lowerCAmelCase	)

										self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]	,  _lowerCAmelCase	,  atol=1E-4	)	)






					@slow
					@require_accelerate
					@require_torch_gpu
					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       ViTModel.from_pretrained("facebook/dino-vits8"	,  torch_dtype=torch.floataa	,  device_map="auto"	)
										lowerCamelCase__       =       self.default_image_processor

										lowerCamelCase__       =       prepare_img()
										lowerCamelCase__       =       image_processor(images=_lowerCAmelCase	,  return_tensors="pt"	)
										lowerCamelCase__       =       inputs.pixel_values.to(_lowerCAmelCase	)

										# forward pass to make sure inference works in fp16
										with torch.no_grad():
															lowerCamelCase__       =       model(_lowerCAmelCase	)








A_												=     {str(digit): digit**5 for digit in range(1_0)}





def    __UpperCamelCase      (    a) ->int:
					return sum(DIGITS_FIFTH_POWER[digit] for digit in str(a))





def    __UpperCamelCase      (    ) ->int:
					return sum(
					    number
					    for number in range(1000,     1000000)
					    if number == digits_fifth_powers_sum(a))


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

