
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 :




	'''simple docstring'''





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

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






	def 							UpperCamelCase_    (			self   :					Optional[int]					):
					__A       =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

					__A       =					self.get_config()

					return config, pixel_values, labels






	def 							UpperCamelCase_    (			self   :					Tuple					):
					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=A				,initializer_range=self.initializer_range				,encoder_stride=self.encoder_stride				,)






	def 							UpperCamelCase_    (			self   :					List[Any]				,A   :					Optional[int]				,A   :					Any				,A   :					Tuple					):
					__A       =					ViTModel(config=A					)
					model.to(A					)
					model.eval()
					__A       =					model(A					)
					self.parent.assertEqual(result.last_hidden_state.shape				,(self.batch_size, self.seq_length, self.hidden_size)					)






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

					# test greyscale images
					__A       =					1
					__A       =					ViTForMaskedImageModeling(A					)
					model.to(A					)
					model.eval()

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






	def 							UpperCamelCase_    (			self   :					Dict				,A   :					str				,A   :					Optional[int]				,A   :					Tuple					):
					__A       =					self.type_sequence_label_size
					__A       =					ViTForImageClassification(A					)
					model.to(A					)
					model.eval()
					__A       =					model(A				,labels=A					)
					self.parent.assertEqual(result.logits.shape				,(self.batch_size, self.type_sequence_label_size)					)

					# test greyscale images
					__A       =					1
					__A       =					ViTForImageClassification(A					)
					model.to(A					)
					model.eval()

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







	def 							UpperCamelCase_    (			self   :					Optional[int]					):
					__A       =					self.prepare_config_and_inputs()
					(
					    (
					    __A
					)     ,				(
					    __A
					)     ,				(
					    __A
					)     ,				
					)       =					config_and_inputs
					__A       =					{"pixel_values": pixel_values}
					return config, inputs_dict





@require_torch
class 				UpperCAmelCase (    __SCREAMING_SNAKE_CASE     ,     __SCREAMING_SNAKE_CASE     ,     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   :					Optional[Any]					):
					__A       =					ViTModelTester(self					)
					__A       =					ConfigTester(self				,config_class=A				,has_text_modality=A				,hidden_size=37					)






	def 							UpperCamelCase_    (			self   :					Any					):
					self.config_tester.run_common_tests()






	@unittest.skip(reason="ViT does not use inputs_embeds"					)
	def 							UpperCamelCase_    (			self   :					List[Any]					):
					pass






	def 							UpperCamelCase_    (			self   :					Optional[Any]					):
					__A     ,				__A       =					self.model_tester.prepare_config_and_inputs_for_common()

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






	def 							UpperCamelCase_    (			self   :					int					):
					__A     ,				__A       =					self.model_tester.prepare_config_and_inputs_for_common()

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

									__A       =					["pixel_values"]
									self.assertListEqual(arg_names[:1]				,A					)






	def 							UpperCamelCase_    (			self   :					Tuple					):
					__A       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*A					)






	def 							UpperCamelCase_    (			self   :					List[Any]					):
					__A       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_image_modeling(*A					)






	def 							UpperCamelCase_    (			self   :					Optional[int]					):
					__A       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*A					)







	@slow
	def 							UpperCamelCase_    (			self   :					Optional[Any]					):
					for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A       =					ViTModel.from_pretrained(A					)
									self.assertIsNotNone(A					)





def 		UpperCAmelCase     (      )						->      Any:







				"""simple docstring"""



				__A       =					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
				return image





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




	'''simple docstring'''





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






	@slow
	def 							UpperCamelCase_    (			self   :					int					):
					__A       =					ViTForImageClassification.from_pretrained("google/vit-base-patch16-224"					).to(A					)

					__A       =					self.default_image_processor
					__A       =					prepare_img()
					__A       =					image_processor(images=A				,return_tensors="pt"					).to(A					)

					# forward pass
					with torch.no_grad():
									__A       =					model(**A					)

					# verify the logits
					__A       =					torch.Size((1, 10_00)					)
					self.assertEqual(outputs.logits.shape				,A					)

					__A       =					torch.tensor([-0.27_44, 0.82_15, -0.08_36]					).to(A					)

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






	@slow
	def 							UpperCamelCase_    (			self   :					Dict					):
					# 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.
					__A       =					ViTModel.from_pretrained("facebook/dino-vits8"					).to(A					)

					__A       =					ViTImageProcessor.from_pretrained("facebook/dino-vits8"				,size=4_80					)
					__A       =					prepare_img()
					__A       =					image_processor(images=A				,return_tensors="pt"					)
					__A       =					inputs.pixel_values.to(A					)

					# forward pass
					with torch.no_grad():
									__A       =					model(A				,interpolate_pos_encoding=A					)

					# verify the logits
					__A       =					torch.Size((1, 36_01, 3_84)					)
					self.assertEqual(outputs.last_hidden_state.shape				,A					)

					__A       =					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(A					)

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







	@slow
	@require_accelerate
	@require_torch_gpu
	def 							UpperCamelCase_    (			self   :					List[str]					):
					__A       =					ViTModel.from_pretrained("facebook/dino-vits8"				,torch_dtype=torch.floataa				,device_map="auto"					)
					__A       =					self.default_image_processor

					__A       =					prepare_img()
					__A       =					image_processor(images=A				,return_tensors="pt"					)
					__A       =					inputs.pixel_values.to(A					)

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








def     a_   (__A				,       __A				,       __A				,       __A      )				->						int:



					"""simple docstring"""
					__a      ,			__a					:						Any      =					len(__A      ), len(grid[0]      )
					if (
					    min(__A				,       __A      ) < 0
					    or row == row_length
					    or col == col_length
					    or (row, col) in visit
					    or grid[row][col] == 1
					):
										return 0
					if row == row_length - 1 and col == col_length - 1:
										return 1

					visit.add((row, col)      )

					__a					:						Dict      =					0
					count += depth_first_search(__A				,       row + 1				,       __A				,       __A      )
					count += depth_first_search(__A				,       row - 1				,       __A				,       __A      )
					count += depth_first_search(__A				,       __A				,       col + 1				,       __A      )
					count += depth_first_search(__A				,       __A				,       col - 1				,       __A      )

					visit.remove((row, col)      )
					return count


if __name__ == "__main__":
			import doctest

			doctest.testmod()





