


'''simple docstring'''

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



		'''simple docstring'''







		def __init__( self		:						Dict  ,					_A		:						Tuple  ,					_A		:						Tuple=13  ,					_A		:						Optional[Any]=30  ,					_A		:						str=2  ,					_A		:						Any=3  ,					_A		:						int=True  ,					_A		:						List[str]=True  ,					_A		:						str=32  ,					_A		:						Any=5  ,					_A		:						int=4  ,					_A		:						str=37  ,					_A		:						Optional[int]="gelu"  ,					_A		:						Optional[Any]=0.1  ,					_A		:						Union[str, Any]=0.1  ,					_A		:						Optional[int]=10  ,					_A		:						str=0.02  ,					_A		:						List[str]=None  ,					_A		:						Any=2  ,					)  ->			Dict:
			__magic_name__	:   Dict					     =					parent
			__magic_name__	:   int					     =					batch_size
			__magic_name__	:   int					     =					image_size
			__magic_name__	:   Optional[Any]					     =					patch_size
			__magic_name__	:   Optional[int]					     =					num_channels
			__magic_name__	:   List[Any]					     =					is_training
			__magic_name__	:   Dict					     =					use_labels
			__magic_name__	:   Optional[Any]					     =					hidden_size
			__magic_name__	:   List[str]					     =					num_hidden_layers
			__magic_name__	:   List[Any]					     =					num_attention_heads
			__magic_name__	:   Optional[Any]					     =					intermediate_size
			__magic_name__	:   Any					     =					hidden_act
			__magic_name__	:   Union[str, Any]					     =					hidden_dropout_prob
			__magic_name__	:   Tuple					     =					attention_probs_dropout_prob
			__magic_name__	:   List[Any]					     =					type_sequence_label_size
			__magic_name__	:   Optional[Any]					     =					initializer_range
			__magic_name__	:   List[str]					     =					scope
			__magic_name__	:   int					     =					encoder_stride

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







		def        __lowerCAmelCase		( self		:						Optional[Any]      )  ->			List[Any]:
			__magic_name__	:   List[str]					     =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

			__magic_name__	:   Optional[Any]					     =					None
			if self.use_labels:
				__magic_name__	:   Optional[Any]					     =					ids_tensor([self.batch_size]  ,					self.type_sequence_label_size      )

			__magic_name__	:   Optional[int]					     =					self.get_config()

			return config, pixel_values, labels







		def        __lowerCAmelCase		( self		:						Dict      )  ->			str:
			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        __lowerCAmelCase		( self		:						Optional[Any]  ,					_A		:						Tuple  ,					_A		:						Tuple  ,					_A		:						Optional[Any]      )  ->			Dict:
			__magic_name__	:   Union[str, Any]					     =					ViTModel(config=_lowerCamelCase      )
			model.to(_lowerCamelCase      )
			model.eval()
			__magic_name__	:   Optional[Any]					     =					model(_lowerCamelCase      )
			self.parent.assertEqual(result.last_hidden_state.shape  ,					(self.batch_size, self.seq_length, self.hidden_size)      )







		def        __lowerCAmelCase		( self		:						int  ,					_A		:						Dict  ,					_A		:						Dict  ,					_A		:						Tuple      )  ->			str:
			__magic_name__	:   Tuple					     =					ViTForMaskedImageModeling(config=_lowerCamelCase      )
			model.to(_lowerCamelCase      )
			model.eval()
			__magic_name__	:   Any					     =					model(_lowerCamelCase      )
			self.parent.assertEqual(
			    result.reconstruction.shape  ,					(self.batch_size, self.num_channels, self.image_size, self.image_size)      )

			# test greyscale images
			__magic_name__	:   List[Any]					     =					1
			__magic_name__	:   int					     =					ViTForMaskedImageModeling(_lowerCamelCase      )
			model.to(_lowerCamelCase      )
			model.eval()

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







		def        __lowerCAmelCase		( self		:						List[str]  ,					_A		:						Any  ,					_A		:						Dict  ,					_A		:						Union[str, Any]      )  ->			Dict:
			__magic_name__	:   Optional[int]					     =					self.type_sequence_label_size
			__magic_name__	:   Optional[Any]					     =					ViTForImageClassification(_lowerCamelCase      )
			model.to(_lowerCamelCase      )
			model.eval()
			__magic_name__	:   List[Any]					     =					model(_lowerCamelCase  ,					labels=_lowerCamelCase      )
			self.parent.assertEqual(result.logits.shape  ,					(self.batch_size, self.type_sequence_label_size)      )

			# test greyscale images
			__magic_name__	:   str					     =					1
			__magic_name__	:   Optional[Any]					     =					ViTForImageClassification(_lowerCamelCase      )
			model.to(_lowerCamelCase      )
			model.eval()

			__magic_name__	:   str					     =					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
			__magic_name__	:   List[Any]					     =					model(_lowerCamelCase      )
			self.parent.assertEqual(result.logits.shape  ,					(self.batch_size, self.type_sequence_label_size)      )







		def        __lowerCAmelCase		( self		:						Optional[Any]      )  ->			Tuple:
			__magic_name__	:   Any					     =					self.prepare_config_and_inputs()
			(
			    __magic_name__
			)	:   Dict					     =					config_and_inputs
			__magic_name__	:   List[Any]					     =					{'''pixel_values''': pixel_values}
			return config, inputs_dict







@require_torch
class 					_lowerCamelCase	(						a__		,					a__		,					unittest.TestCase							):



		'''simple docstring'''







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

		A_     :      Union[str, Any]				   =    False
		A_     :      Union[str, Any]				   =    False
		A_     :      Tuple				   =    False







		def        __lowerCAmelCase		( self		:						Optional[int]      )  ->			Optional[Any]:
			__magic_name__	:   Union[str, Any]					     =					ViTModelTester(self      )
			__magic_name__	:   str					     =					ConfigTester(self  ,					config_class=_lowerCamelCase  ,					has_text_modality=_lowerCamelCase  ,					hidden_size=37      )







		def        __lowerCAmelCase		( self		:						Optional[Any]      )  ->			Union[str, Any]:
			self.config_tester.run_common_tests()







		@unittest.skip(reason='ViT does not use inputs_embeds'      )
		def        __lowerCAmelCase		( self		:						str      )  ->			Dict:
			pass







		def        __lowerCAmelCase		( self		:						Tuple      )  ->			str:
			__magic_name__	:   List[str]					     =					self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
				__magic_name__	:   Any					     =					model_class(_lowerCamelCase      )
				self.assertIsInstance(model.get_input_embeddings()  ,					(nn.Module)      )
				__magic_name__	:   Union[str, Any]					     =					model.get_output_embeddings()
				self.assertTrue(x is None or isinstance(_lowerCamelCase  ,					nn.Linear      )      )







		def        __lowerCAmelCase		( self		:						int      )  ->			Union[str, Any]:
			__magic_name__	:   Any					     =					self.model_tester.prepare_config_and_inputs_for_common()

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

				__magic_name__	:   List[str]					     =					['''pixel_values''']
				self.assertListEqual(arg_names[:1]  ,					_lowerCamelCase      )







		def        __lowerCAmelCase		( self		:						Dict      )  ->			List[Any]:
			__magic_name__	:   Optional[int]					     =					self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*_lowerCamelCase      )







		def        __lowerCAmelCase		( self		:						List[str]      )  ->			List[Any]:
			__magic_name__	:   str					     =					self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_masked_image_modeling(*_lowerCamelCase      )







		def        __lowerCAmelCase		( self		:						List[str]      )  ->			List[str]:
			__magic_name__	:   Union[str, Any]					     =					self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase      )







		@slow
		def        __lowerCAmelCase		( self		:						Dict      )  ->			List[Any]:







			for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__magic_name__	:   Optional[int]					     =					ViTModel.from_pretrained(_lowerCamelCase      )
				self.assertIsNotNone(_lowerCamelCase      )







def lowerCamelCase      (      ):





	"""simple docstring"""







	__magic_name__	:   Dict					     =					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'		)
	return image





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



		'''simple docstring'''







		@cached_property
		def        __lowerCAmelCase		( self		:						Optional[int]      )  ->			str:
			return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224'      ) if is_vision_available() else None







		@slow
		def        __lowerCAmelCase		( self		:						List[str]      )  ->			Any:
			__magic_name__	:   List[str]					     =					ViTForImageClassification.from_pretrained('google/vit-base-patch16-224'      ).to(_lowerCamelCase      )

			__magic_name__	:   int					     =					self.default_image_processor
			__magic_name__	:   Optional[Any]					     =					prepare_img()
			__magic_name__	:   Optional[Any]					     =					image_processor(images=_lowerCamelCase  ,					return_tensors='pt'      ).to(_lowerCamelCase      )

			# forward pass
			with torch.no_grad():
				__magic_name__	:   List[Any]					     =					model(**_lowerCamelCase      )

			# verify the logits
			__magic_name__	:   Optional[int]					     =					torch.Size((1, 1000)      )
			self.assertEqual(outputs.logits.shape  ,					_lowerCamelCase      )

			__magic_name__	:   Any					     =					torch.tensor([-0.2744, 0.8215, -0.0836]      ).to(_lowerCamelCase      )

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







		@slow
		def        __lowerCAmelCase		( self		:						int      )  ->			List[str]:
			__magic_name__	:   str					     =					ViTModel.from_pretrained('facebook/dino-vits8'      ).to(_lowerCamelCase      )

			__magic_name__	:   List[Any]					     =					ViTImageProcessor.from_pretrained('facebook/dino-vits8'  ,					size=480      )
			__magic_name__	:   Optional[Any]					     =					prepare_img()
			__magic_name__	:   List[Any]					     =					image_processor(images=_lowerCamelCase  ,					return_tensors='pt'      )
			__magic_name__	:   Optional[int]					     =					inputs.pixel_values.to(_lowerCamelCase      )

			# forward pass
			with torch.no_grad():
				__magic_name__	:   Union[str, Any]					     =					model(_lowerCamelCase  ,					interpolate_pos_encoding=_lowerCamelCase      )

			# verify the logits
			__magic_name__	:   str					     =					torch.Size((1, 3601, 384)      )
			self.assertEqual(outputs.last_hidden_state.shape  ,					_lowerCamelCase      )

			__magic_name__	:   List[Any]					     =					torch.tensor(
			    [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]      ).to(_lowerCamelCase      )

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







		@slow
		@require_accelerate
		@require_torch_gpu
		def        __lowerCAmelCase		( self		:						Tuple      )  ->			Optional[Any]:
			__magic_name__	:   Tuple					     =					ViTModel.from_pretrained('facebook/dino-vits8'  ,					torch_dtype=torch.floataa  ,					device_map='auto'      )
			__magic_name__	:   Dict					     =					self.default_image_processor

			__magic_name__	:   str					     =					prepare_img()
			__magic_name__	:   Any					     =					image_processor(images=_lowerCamelCase  ,					return_tensors='pt'      )
			__magic_name__	:   int					     =					inputs.pixel_values.to(_lowerCamelCase      )

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



'''simple docstring'''

import warnings

from ...utils import logging
from .image_processing_beit import BeitImageProcessor


lowerCAmelCase				:Tuple        =      logging.get_logger(__name__)
class _lowerCamelCase	(						lowercase__							):



		'''simple docstring'''







		def __init__( self		:						Any  ,					*_A		:						List[Any]  ,					**_A		:						str      )  ->			None:
			warnings.warn(
			    'The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
			    ' use BeitImageProcessor instead.'  ,					_A  ,					)
			super().__init__(*_A  ,					**_A      )