






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

						"""simple docstring"""



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




											'''simple docstring'''



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

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



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




											'''simple docstring'''



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

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

											__a					:						Union[str, Any]      =					self.get_config()

											return config, pixel_values, labels



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




											'''simple docstring'''



											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=__UpperCAmelCase ,					initializer_range=self.initializer_range ,					encoder_stride=self.encoder_stride ,					)



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




											'''simple docstring'''



											__a					:						List[Any]      =					ViTModel(config=__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						Dict      =					model(__UpperCAmelCase   )
											self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)   )



						def        UpperCAmelCase__					(self:					Dict ,					__UpperCAmelCase:					Optional[Any] ,					__UpperCAmelCase:					str ,					__UpperCAmelCase:					List[str]   )    ->     int:




											'''simple docstring'''



											__a					:						List[Any]      =					ViTForMaskedImageModeling(config=__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						Tuple      =					model(__UpperCAmelCase   )
											self.parent.assertEqual(
											    result.reconstruction.shape ,					(self.batch_size, self.num_channels, self.image_size, self.image_size)   )

											# test greyscale images
											__a					:						Any      =					1
											__a					:						Optional[Any]      =					ViTForMaskedImageModeling(__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()

											__a					:						Any      =					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
											__a					:						Any      =					model(__UpperCAmelCase   )
											self.parent.assertEqual(result.reconstruction.shape ,					(self.batch_size, 1, self.image_size, self.image_size)   )



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




											'''simple docstring'''



											__a					:						List[str]      =					self.type_sequence_label_size
											__a					:						Tuple      =					ViTForImageClassification(__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						Optional[int]      =					model(__UpperCAmelCase ,					labels=__UpperCAmelCase   )
											self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.type_sequence_label_size)   )

											# test greyscale images
											__a					:						Any      =					1
											__a					:						Any      =					ViTForImageClassification(__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()

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



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




											'''simple docstring'''



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




@require_torch
class 							snake_case_       (    __UpperCamelCase		,							__UpperCamelCase		,							unittest.TestCase      ):

						"""simple docstring"""



						snake_case__        =      (
						    (
						        ViTModel,
						        ViTForImageClassification,
						        ViTForMaskedImageModeling,
						    )
						    if is_torch_available()
						    else ()
						)
						snake_case__        =      (
						    {"""feature-extraction""": ViTModel, """image-classification""": ViTForImageClassification}
						    if is_torch_available()
						    else {}
						)
						snake_case__        =      True

						snake_case__        =      False
						snake_case__        =      False
						snake_case__        =      False



						def        UpperCAmelCase__					(self:					str   )    ->     Tuple:




											'''simple docstring'''



											__a					:						Optional[int]      =					ViTModelTester(self   )
											__a					:						List[Any]      =					ConfigTester(self ,					config_class=__UpperCAmelCase ,					has_text_modality=__UpperCAmelCase ,					hidden_size=37   )



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




											'''simple docstring'''



											self.config_tester.run_common_tests()



						@unittest.skip(reason="ViT does not use inputs_embeds"   )
						def        UpperCAmelCase__					(self:					int   )    ->     Optional[int]:




											'''simple docstring'''



											pass



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




											'''simple docstring'''



											__a      ,			__a					:						Optional[int]      =					self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																__a					:						List[str]      =					model_class(__UpperCAmelCase   )
																self.assertIsInstance(model.get_input_embeddings() ,					(nn.Module)   )
																__a					:						Optional[int]      =					model.get_output_embeddings()
																self.assertTrue(x is None or isinstance(__UpperCAmelCase ,					nn.Linear   )   )



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




											'''simple docstring'''



											__a      ,			__a					:						Optional[Any]      =					self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																__a					:						Optional[int]      =					model_class(__UpperCAmelCase   )
																__a					:						List[str]      =					inspect.signature(model.forward   )
																# signature.parameters is an OrderedDict => so arg_names order is deterministic
																__a					:						Optional[int]      =					[*signature.parameters.keys()]

																__a					:						Dict      =					["pixel_values"]
																self.assertListEqual(arg_names[:1] ,					__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					int   )    ->     Any:




											'''simple docstring'''



											__a					:						int      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__UpperCAmelCase   )



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




											'''simple docstring'''



											__a					:						int      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					int   )    ->     str:




											'''simple docstring'''



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



						@slow
						def        UpperCAmelCase__					(self:					Tuple   )    ->     Dict:




											'''simple docstring'''



											for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__a					:						Union[str, Any]      =					ViTModel.from_pretrained(__UpperCAmelCase   )
																self.assertIsNotNone(__UpperCAmelCase   )







def     a_   ()				->						str:



					"""simple docstring"""
					__a					:						List[Any]      =					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
					return image




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

						"""simple docstring"""



						@cached_property
						def        UpperCAmelCase__					(self:					Dict   )    ->     Any:




											'''simple docstring'''



											return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"   ) if is_vision_available() else None



						@slow
						def        UpperCAmelCase__					(self:					str   )    ->     Dict:




											'''simple docstring'''



											__a					:						Any      =					ViTForImageClassification.from_pretrained("google/vit-base-patch16-224"   ).to(__UpperCAmelCase   )

											__a					:						Optional[int]      =					self.default_image_processor
											__a					:						List[Any]      =					prepare_img()
											__a					:						Tuple      =					image_processor(images=__UpperCAmelCase ,					return_tensors="pt"   ).to(__UpperCAmelCase   )

											# forward pass
											with torch.no_grad():
																__a					:						List[str]      =					model(**__UpperCAmelCase   )

											# verify the logits
											__a					:						Dict      =					torch.Size((1, 1000)   )
											self.assertEqual(outputs.logits.shape ,					__UpperCAmelCase   )

											__a					:						List[Any]      =					torch.tensor([-0.27_44, 0.82_15, -0.08_36]   ).to(__UpperCAmelCase   )

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



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




											'''simple docstring'''



											__a					:						List[str]      =					ViTModel.from_pretrained("facebook/dino-vits8"   ).to(__UpperCAmelCase   )

											__a					:						Optional[int]      =					ViTImageProcessor.from_pretrained("facebook/dino-vits8" ,					size=480   )
											__a					:						Dict      =					prepare_img()
											__a					:						Tuple      =					image_processor(images=__UpperCAmelCase ,					return_tensors="pt"   )
											__a					:						str      =					inputs.pixel_values.to(__UpperCAmelCase   )

											# forward pass
											with torch.no_grad():
																__a					:						List[Any]      =					model(__UpperCAmelCase ,					interpolate_pos_encoding=__UpperCAmelCase   )

											# verify the logits
											__a					:						List[str]      =					torch.Size((1, 3601, 384)   )
											self.assertEqual(outputs.last_hidden_state.shape ,					__UpperCAmelCase   )

											__a					:						List[str]      =					torch.tensor(
											    [[4.23_40, 4.39_06, -6.66_92], [4.54_63, 1.89_28, -6.72_57], [4.44_29, 0.84_96, -5.85_85]]   ).to(__UpperCAmelCase   )

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



						@slow
						@require_accelerate
						@require_torch_gpu
						def        UpperCAmelCase__					(self:					int   )    ->     int:




											'''simple docstring'''



											__a					:						List[Any]      =					ViTModel.from_pretrained("facebook/dino-vits8" ,					torch_dtype=torch.floataa ,					device_map="auto"   )
											__a					:						Tuple      =					self.default_image_processor

											__a					:						Union[str, Any]      =					prepare_img()
											__a					:						Optional[int]      =					image_processor(images=__UpperCAmelCase ,					return_tensors="pt"   )
											__a					:						Any      =					inputs.pixel_values.to(__UpperCAmelCase   )

											# forward pass to make sure inference works in fp16
											with torch.no_grad():
																__a					:						List[str]      =					model(__UpperCAmelCase   )













from abc import ABC, abstractmethod
from argparse import ArgumentParser




class 							snake_case_       (    __UpperCamelCase      ):

						"""simple docstring"""



						@staticmethod
						@abstractmethod
						def        UpperCAmelCase__					(__UpperCAmelCase:					ArgumentParser   )    ->     Tuple:




											'''simple docstring'''



											raise NotImplementedError()



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




											'''simple docstring'''



											raise NotImplementedError()





