






"""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    _UpperCAmelCase			:




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

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




					def     A    (							self	:		Tuple  )    ->    Tuple:
							lowercase_					:					Any							=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

							lowercase_					:					Optional[int]							=  None
							if self.use_labels:
									lowercase_					:					List[str]							=  ids_tensor([self.batch_size]       ,		self.type_sequence_label_size  )

							lowercase_					:					Tuple							=  self.get_config()

							return config, pixel_values, labels




					def     A    (							self	:		Union[str, Any]  )    ->    Union[str, Any]:
							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     A    (							self	:		Optional[int]       ,		A	:		List[str]       ,		A	:		Optional[int]       ,		A	:		List[Any]  )    ->    Any:
							lowercase_					:					int							=  ViTModel(config=UpperCamelCase_  )
							model.to(UpperCamelCase_  )
							model.eval()
							lowercase_					:					Optional[Any]							=  model(UpperCamelCase_  )
							self.parent.assertEqual(result.last_hidden_state.shape       ,		(self.batch_size, self.seq_length, self.hidden_size)  )




					def     A    (							self	:		Optional[Any]       ,		A	:		Optional[Any]       ,		A	:		int       ,		A	:		int  )    ->    List[str]:
							lowercase_					:					Any							=  ViTForMaskedImageModeling(config=UpperCamelCase_  )
							model.to(UpperCamelCase_  )
							model.eval()
							lowercase_					:					List[Any]							=  model(UpperCamelCase_  )
							self.parent.assertEqual(
							    result.reconstruction.shape       ,		(self.batch_size, self.num_channels, self.image_size, self.image_size)  )

							# test greyscale images
							lowercase_					:					str							=  1
							lowercase_					:					Optional[int]							=  ViTForMaskedImageModeling(UpperCamelCase_  )
							model.to(UpperCamelCase_  )
							model.eval()

							lowercase_					:					int							=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
							lowercase_					:					Tuple							=  model(UpperCamelCase_  )
							self.parent.assertEqual(result.reconstruction.shape       ,		(self.batch_size, 1, self.image_size, self.image_size)  )




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

							# test greyscale images
							lowercase_					:					Any							=  1
							lowercase_					:					List[Any]							=  ViTForImageClassification(UpperCamelCase_  )
							model.to(UpperCamelCase_  )
							model.eval()

							lowercase_					:					Optional[Any]							=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
							lowercase_					:					str							=  model(UpperCamelCase_  )
							self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.type_sequence_label_size)  )






					def     A    (							self	:		Optional[int]  )    ->    Union[str, Any]:
							lowercase_					:					List[Any]							=  self.prepare_config_and_inputs()
							(
							    (
							    lowercase_  
							)			,   (
							    lowercase_  
							)			,   (
							    lowercase_  
							)			,     
							)					:					int							=  config_and_inputs
							lowercase_					:					Optional[int]							=  {'''pixel_values''': pixel_values}
							return config, inputs_dict





@require_torch
class    _UpperCAmelCase			(					_A  ,		_A  ,		unittest.TestCase				):
					SCREAMING_SNAKE_CASE_						:							int				       =  (
					    (
					        ViTModel,
					        ViTForImageClassification,
					        ViTForMaskedImageModeling,
					    )
					    if is_torch_available()
					    else ()
					)
					SCREAMING_SNAKE_CASE_						:							Optional[Any]				       =  (
					    {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification}
					    if is_torch_available()
					    else {}
					)
					SCREAMING_SNAKE_CASE_						:							str				       =  True

					SCREAMING_SNAKE_CASE_						:							List[str]				       =  False
					SCREAMING_SNAKE_CASE_						:							Dict				       =  False
					SCREAMING_SNAKE_CASE_						:							str				       =  False




					def     A    (							self	:		Union[str, Any]  )    ->    List[str]:
							lowercase_					:					str							=  ViTModelTester(self  )
							lowercase_					:					int							=  ConfigTester(self       ,		config_class=UpperCamelCase_       ,		has_text_modality=UpperCamelCase_       ,		hidden_size=37  )




					def     A    (							self	:		int  )    ->    Optional[int]:
							self.config_tester.run_common_tests()




					@unittest.skip(reason='''ViT does not use inputs_embeds'''  )
					def     A    (							self	:		Any  )    ->    Optional[int]:
							pass




					def     A    (							self	:		str  )    ->    Union[str, Any]:
							lowercase_			,   lowercase_					:					List[Any]							=  self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									lowercase_					:					Tuple							=  model_class(UpperCamelCase_  )
									self.assertIsInstance(model.get_input_embeddings()       ,		(nn.Module)  )
									lowercase_					:					List[Any]							=  model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(UpperCamelCase_       ,		nn.Linear  )  )




					def     A    (							self	:		List[str]  )    ->    Optional[Any]:
							lowercase_			,   lowercase_					:					Optional[Any]							=  self.model_tester.prepare_config_and_inputs_for_common()

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

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




					def     A    (							self	:		List[Any]  )    ->    str:
							lowercase_					:					Any							=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*UpperCamelCase_  )




					def     A    (							self	:		List[str]  )    ->    List[Any]:
							lowercase_					:					str							=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase_  )




					def     A    (							self	:		Tuple  )    ->    Optional[int]:
							lowercase_					:					List[Any]							=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_  )






					@slow
					def     A    (							self	:		Optional[Any]  )    ->    Union[str, Any]:
							for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowercase_					:					List[Any]							=  ViTModel.from_pretrained(UpperCamelCase_  )
									self.assertIsNotNone(UpperCamelCase_  )



def 		lowercase       (							):
		lowercase_					:					str							=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
		return image






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




					@cached_property
					def     A    (							self	:		Dict  )    ->    Tuple:
							return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224'''  ) if is_vision_available() else None




					@slow
					def     A    (							self	:		List[Any]  )    ->    Union[str, Any]:
							lowercase_					:					Tuple							=  ViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''  ).to(UpperCamelCase_  )

							lowercase_					:					str							=  self.default_image_processor
							lowercase_					:					List[str]							=  prepare_img()
							lowercase_					:					int							=  image_processor(images=UpperCamelCase_       ,		return_tensors='''pt'''  ).to(UpperCamelCase_  )

							# forward pass
							with torch.no_grad():
									lowercase_					:					Dict							=  model(**UpperCamelCase_  )

							# verify the logits
							lowercase_					:					Tuple							=  torch.Size((1, 10_00)  )
							self.assertEqual(outputs.logits.shape       ,		UpperCamelCase_  )

							lowercase_					:					List[str]							=  torch.tensor([-0.2744, 0.8215, -0.0836]  ).to(UpperCamelCase_  )

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




					@slow
					def     A    (							self	:		List[Any]  )    ->    List[str]:
							# 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.
							lowercase_					:					Dict							=  ViTModel.from_pretrained('''facebook/dino-vits8'''  ).to(UpperCamelCase_  )

							lowercase_					:					Optional[int]							=  ViTImageProcessor.from_pretrained('''facebook/dino-vits8'''       ,		size=4_80  )
							lowercase_					:					Dict							=  prepare_img()
							lowercase_					:					Dict							=  image_processor(images=UpperCamelCase_       ,		return_tensors='''pt'''  )
							lowercase_					:					Optional[int]							=  inputs.pixel_values.to(UpperCamelCase_  )

							# forward pass
							with torch.no_grad():
									lowercase_					:					List[str]							=  model(UpperCamelCase_       ,		interpolate_pos_encoding=UpperCamelCase_  )

							# verify the logits
							lowercase_					:					List[str]							=  torch.Size((1, 36_01, 3_84)  )
							self.assertEqual(outputs.last_hidden_state.shape       ,		UpperCamelCase_  )

							lowercase_					:					Optional[Any]							=  torch.tensor(
							    [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]  ).to(UpperCamelCase_  )

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






					@slow
					@require_accelerate
					@require_torch_gpu
					def     A    (							self	:		List[str]  )    ->    int:
							lowercase_					:					Tuple							=  ViTModel.from_pretrained('''facebook/dino-vits8'''       ,		torch_dtype=torch.floataa       ,		device_map='''auto'''  )
							lowercase_					:					str							=  self.default_image_processor

							lowercase_					:					List[str]							=  prepare_img()
							lowercase_					:					int							=  image_processor(images=UpperCamelCase_       ,		return_tensors='''pt'''  )
							lowercase_					:					List[str]							=  inputs.pixel_values.to(UpperCamelCase_  )

							# forward pass to make sure inference works in fp16
							with torch.no_grad():
									lowercase_					:					List[Any]							=  model(UpperCamelCase_  )










'''simple docstring'''




import warnings

from ...utils import logging
from .image_processing_perceiver import PerceiverImageProcessor


a__					:			int          =     logging.get_logger(__name__)






class 				__snake_case     (					__magic_name__							):





	def __init__(		self      ,       *UpperCamelCase_      ,       **UpperCamelCase_		)      ->		None:
		warnings.warn(
		    'The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'
		    ' Please use PerceiverImageProcessor instead.'      ,       UpperCamelCase_      ,       )
		super().__init__(*UpperCamelCase_      ,       **UpperCamelCase_		)



