



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



		def __init__(     self     :Dict   ,__lowercase     :Dict   ,__lowercase     :Dict=1_3   ,__lowercase     :List[Any]=3_0   ,__lowercase     :int=2   ,__lowercase     :List[Any]=3   ,__lowercase     :Optional[int]=True   ,__lowercase     :str=True   ,__lowercase     :str=3_2   ,__lowercase     :Optional[int]=5   ,__lowercase     :Optional[Any]=4   ,__lowercase     :List[str]=3_7   ,__lowercase     :Union[str, Any]="gelu"   ,__lowercase     :Dict=0.1   ,__lowercase     :List[Any]=0.1   ,__lowercase     :Any=1_0   ,__lowercase     :List[str]=0.02   ,__lowercase     :List[str]=None   ,__lowercase     :Union[str, Any]=2   ,):
						snake_case__	:      Union[str, Any]     		=	parent
						snake_case__	:      List[Any]     		=	batch_size
						snake_case__	:      List[str]     		=	image_size
						snake_case__	:      Tuple     		=	patch_size
						snake_case__	:      str     		=	num_channels
						snake_case__	:      Optional[int]     		=	is_training
						snake_case__	:      Optional[Any]     		=	use_labels
						snake_case__	:      Tuple     		=	hidden_size
						snake_case__	:      Dict     		=	num_hidden_layers
						snake_case__	:      int     		=	num_attention_heads
						snake_case__	:      Optional[int]     		=	intermediate_size
						snake_case__	:      Optional[int]     		=	hidden_act
						snake_case__	:      Tuple     		=	hidden_dropout_prob
						snake_case__	:      Any     		=	attention_probs_dropout_prob
						snake_case__	:      Optional[int]     		=	type_sequence_label_size
						snake_case__	:      str     		=	initializer_range
						snake_case__	:      Any     		=	scope
						snake_case__	:      List[Any]     		=	encoder_stride

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



		def 	__lowerCamelCase			(     self     :int  ):
						snake_case__	:      str     		=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

						snake_case__	:      Union[str, Any]     		=	None
						if self.use_labels:
										snake_case__	:      List[Any]     		=	ids_tensor([self.batch_size]   ,self.type_sequence_label_size  )

						snake_case__	:      Dict     		=	self.get_config()

						return config, pixel_values, labels



		def 	__lowerCamelCase			(     self     :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=__lowercase   ,initializer_range=self.initializer_range   ,encoder_stride=self.encoder_stride   ,)



		def 	__lowerCamelCase			(     self     :str   ,__lowercase     :Dict   ,__lowercase     :str   ,__lowercase     :List[str]  ):
						snake_case__	:      Optional[int]     		=	ViTModel(config=__lowercase  )
						model.to(__lowercase  )
						model.eval()
						snake_case__	:      Optional[Any]     		=	model(__lowercase  )
						self.parent.assertEqual(result.last_hidden_state.shape   ,(self.batch_size, self.seq_length, self.hidden_size)  )



		def 	__lowerCamelCase			(     self     :Optional[Any]   ,__lowercase     :List[Any]   ,__lowercase     :List[Any]   ,__lowercase     :Union[str, Any]  ):
						snake_case__	:      Any     		=	ViTForMaskedImageModeling(config=__lowercase  )
						model.to(__lowercase  )
						model.eval()
						snake_case__	:      Optional[Any]     		=	model(__lowercase  )
						self.parent.assertEqual(
						    result.reconstruction.shape   ,(self.batch_size, self.num_channels, self.image_size, self.image_size)  )

						# test greyscale images
						snake_case__	:      List[str]     		=	1
						snake_case__	:      Optional[Any]     		=	ViTForMaskedImageModeling(__lowercase  )
						model.to(__lowercase  )
						model.eval()

						snake_case__	:      Union[str, Any]     		=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
						snake_case__	:      Union[str, Any]     		=	model(__lowercase  )
						self.parent.assertEqual(result.reconstruction.shape   ,(self.batch_size, 1, self.image_size, self.image_size)  )



		def 	__lowerCamelCase			(     self     :Optional[Any]   ,__lowercase     :Tuple   ,__lowercase     :Optional[int]   ,__lowercase     :Any  ):
						snake_case__	:      Tuple     		=	self.type_sequence_label_size
						snake_case__	:      str     		=	ViTForImageClassification(__lowercase  )
						model.to(__lowercase  )
						model.eval()
						snake_case__	:      Dict     		=	model(__lowercase   ,labels=__lowercase  )
						self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.type_sequence_label_size)  )

						# test greyscale images
						snake_case__	:      Optional[int]     		=	1
						snake_case__	:      Union[str, Any]     		=	ViTForImageClassification(__lowercase  )
						model.to(__lowercase  )
						model.eval()

						snake_case__	:      Optional[Any]     		=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
						snake_case__	:      List[Any]     		=	model(__lowercase  )
						self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.type_sequence_label_size)  )






		def 	__lowerCamelCase			(     self     :List[str]  ):
						snake_case__	:      str     		=	self.prepare_config_and_inputs()
						(
						    (
						    snake_case__
						)		, (
						    snake_case__
						)		, (
						    snake_case__
						)		, 
						)	:      Any     		=	config_and_inputs
						snake_case__	:      Dict     		=	{'''pixel_values''': pixel_values}
						return config, inputs_dict


@require_torch
class       a							(			__lowerCamelCase  ,							__lowerCamelCase  ,							unittest.TestCase				):
		__lowerCAmelCase	:    str											=					(
		    (
		        ViTModel,
		        ViTForImageClassification,
		        ViTForMaskedImageModeling,
		    )
		    if is_torch_available()
		    else ()
		)
		__lowerCAmelCase	:    str											=					(
		    {"""feature-extraction""": ViTModel, """image-classification""": ViTForImageClassification}
		    if is_torch_available()
		    else {}
		)
		__lowerCAmelCase	:    str											=					True

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



		def 	__lowerCamelCase			(     self     :Optional[int]  ):
						snake_case__	:      str     		=	ViTModelTester(self  )
						snake_case__	:      Optional[Any]     		=	ConfigTester(self   ,config_class=__lowercase   ,has_text_modality=__lowercase   ,hidden_size=3_7  )



		def 	__lowerCamelCase			(     self     :List[str]  ):
						self.config_tester.run_common_tests()



		@unittest.skip(reason='''ViT does not use inputs_embeds'''  )
		def 	__lowerCamelCase			(     self     :Dict  ):
						pass



		def 	__lowerCamelCase			(     self     :Dict  ):
						snake_case__		, snake_case__	:      int     		=	self.model_tester.prepare_config_and_inputs_for_common()

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



		def 	__lowerCamelCase			(     self     :Union[str, Any]  ):
						snake_case__		, snake_case__	:      str     		=	self.model_tester.prepare_config_and_inputs_for_common()

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

										snake_case__	:      str     		=	['''pixel_values''']
										self.assertListEqual(arg_names[:1]   ,__lowercase  )



		def 	__lowerCamelCase			(     self     :Dict  ):
						snake_case__	:      str     		=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__lowercase  )



		def 	__lowerCamelCase			(     self     :Any  ):
						snake_case__	:      Tuple     		=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_image_modeling(*__lowercase  )



		def 	__lowerCamelCase			(     self     :Dict  ):
						snake_case__	:      List[Any]     		=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__lowercase  )






		@slow
		def 	__lowerCamelCase			(     self     :Union[str, Any]  ):
						for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										snake_case__	:      int     		=	ViTModel.from_pretrained(__lowercase  )
										self.assertIsNotNone(__lowercase  )



def 			_lowerCAmelCase       (					)   ->					Any:


				"""simple docstring"""

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





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



		@cached_property
		def 	__lowerCamelCase			(     self     :List[Any]  ):
						return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224'''  ) if is_vision_available() else None



		@slow
		def 	__lowerCamelCase			(     self     :Tuple  ):
						snake_case__	:      str     		=	ViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''  ).to(__lowercase  )

						snake_case__	:      int     		=	self.default_image_processor
						snake_case__	:      Union[str, Any]     		=	prepare_img()
						snake_case__	:      List[Any]     		=	image_processor(images=__lowercase   ,return_tensors='''pt'''  ).to(__lowercase  )

						# forward pass
						with torch.no_grad():
										snake_case__	:      Tuple     		=	model(**__lowercase  )

						# verify the logits
						snake_case__	:      Union[str, Any]     		=	torch.Size((1, 1_0_0_0)  )
						self.assertEqual(outputs.logits.shape   ,__lowercase  )

						snake_case__	:      Optional[Any]     		=	torch.tensor([-0.2744, 0.8215, -0.0836]  ).to(__lowercase  )

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



		@slow
		def 	__lowerCamelCase			(     self     :Tuple  ):
						# 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.
						snake_case__	:      List[Any]     		=	ViTModel.from_pretrained('''facebook/dino-vits8'''  ).to(__lowercase  )

						snake_case__	:      Optional[Any]     		=	ViTImageProcessor.from_pretrained('''facebook/dino-vits8'''   ,size=4_8_0  )
						snake_case__	:      int     		=	prepare_img()
						snake_case__	:      List[str]     		=	image_processor(images=__lowercase   ,return_tensors='''pt'''  )
						snake_case__	:      List[str]     		=	inputs.pixel_values.to(__lowercase  )

						# forward pass
						with torch.no_grad():
										snake_case__	:      Any     		=	model(__lowercase   ,interpolate_pos_encoding=__lowercase  )

						# verify the logits
						snake_case__	:      Tuple     		=	torch.Size((1, 3_6_0_1, 3_8_4)  )
						self.assertEqual(outputs.last_hidden_state.shape   ,__lowercase  )

						snake_case__	:      int     		=	torch.tensor(
						    [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]  ).to(__lowercase  )

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






		@slow
		@require_accelerate
		@require_torch_gpu
		def 	__lowerCamelCase			(     self     :Any  ):
						snake_case__	:      str     		=	ViTModel.from_pretrained('''facebook/dino-vits8'''   ,torch_dtype=torch.floataa   ,device_map='''auto'''  )
						snake_case__	:      Dict     		=	self.default_image_processor

						snake_case__	:      Any     		=	prepare_img()
						snake_case__	:      int     		=	image_processor(images=__lowercase   ,return_tensors='''pt'''  )
						snake_case__	:      Tuple     		=	inputs.pixel_values.to(__lowercase  )

						# forward pass to make sure inference works in fp16
						with torch.no_grad():
										snake_case__	:      Any     		=	model(__lowercase  )






from torch import nn


class       a							(			nn.Module				):



		def __init__(     self     :Tuple   ,__lowercase     :Optional[int]   ,__lowercase     :int  ):
						super().__init__()
						snake_case__	:      Optional[Any]     		=	class_size
						snake_case__	:      Dict     		=	embed_size
						# self.mlp1 = nn.Linear(embed_size, embed_size)
						# self.mlp2 = (nn.Linear(embed_size, class_size))
						snake_case__	:      Dict     		=	nn.Linear(__lowercase   ,__lowercase  )






		def 	__lowerCamelCase			(     self     :str   ,__lowercase     :int  ):
						# hidden_state = nn.functional.relu(self.mlp1(hidden_state))
						# hidden_state = self.mlp2(hidden_state)
						snake_case__	:      Optional[Any]     		=	self.mlp(__lowercase  )
						return logits

