



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  ,  a_  ,  a_=13  ,  a_=30  ,  a_=2  ,  a_=3  ,  a_=True  ,  a_=True  ,  a_=32  ,  a_=5  ,  a_=4  ,  a_=37  ,  a_="gelu"  ,  a_=0.1  ,  a_=0.1  ,  a_=10  ,  a_=0.02  ,  a_=None  ,  a_=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 _a      (							self		):
											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 _a      (							self		):
											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 _a      (							self  ,  a_  ,  a_  ,  a_		):
											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 _a      (							self  ,  a_  ,  a_  ,  a_		):
											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 _a      (							self  ,  a_  ,  a_  ,  a_		):
											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 _a      (							self		):
											a__				      =		self.prepare_config_and_inputs()
											(
											    (
											    a__
											)      ,			(
											    a__
											)      ,			(
											    a__
											)      ,			
											)				      =		config_and_inputs
											a__				      =		{"""pixel_values""": pixel_values}
											return config, inputs_dict




@require_torch
class  __snake_case      (   SCREAMING_SNAKE_CASE			,SCREAMING_SNAKE_CASE			,unittest.TestCase):







							'''simple docstring'''






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

							UpperCamelCase__				:      Tuple         =					False
							UpperCamelCase__				:      Any         =					False
							UpperCamelCase__				:      str         =					False


							def _a      (							self		):
											a__				      =		ViTModelTester(self		)
											a__				      =		ConfigTester(self  ,  config_class=a_  ,  has_text_modality=a_  ,  hidden_size=37		)


							def _a      (							self		):
											self.config_tester.run_common_tests()


							@unittest.skip(reason="""ViT does not use inputs_embeds"""		)
							def _a      (							self		):
											pass


							def _a      (							self		):
											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 _a      (							self		):
											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 _a      (							self		):
											a__				      =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*a_		)


							def _a      (							self		):
											a__				      =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_image_modeling(*a_		)


							def _a      (							self		):
											a__				      =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*a_		)







							@slow
							def _a      (							self		):
											for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															a__				      =		ViTModel.from_pretrained(a_		)
															self.assertIsNotNone(a_		)




def 			A_      (	):

				"""simple docstring"""
				a__				      =		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 _a      (							self		):
											return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224"""		) if is_vision_available() else None


							@slow
							def _a      (							self		):
											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, 1_000)		)
											self.assertEqual(outputs.logits.shape  ,  a_		)

											a__				      =		torch.tensor([-0.2_744, 0.8_215, -0.0_836]		).to(a_		)

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


							@slow
							def _a      (							self		):
											# 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=480		)
											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, 3_601, 384)		)
											self.assertEqual(outputs.last_hidden_state.shape  ,  a_		)

											a__				      =		torch.tensor(
											    [[4.2_340, 4.3_906, -6.6_692], [4.5_463, 1.8_928, -6.7_257], [4.4_429, 0.8_496, -5.8_585]]		).to(a_		)

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







							@slow
							@require_accelerate
							@require_torch_gpu
							def _a      (							self		):
											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_		)






from .configuration_bert_masked import MaskedBertConfig
from .modeling_bert_masked import (
    MaskedBertForMultipleChoice,
    MaskedBertForQuestionAnswering,
    MaskedBertForSequenceClassification,
    MaskedBertForTokenClassification,
    MaskedBertModel,
)
from .modules import *

