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

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

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

		def 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    :		Tuple		    =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

			__lowerCAmelCase    :		Union[str, Any]		    =   self.get_config()

			return config, pixel_values, labels

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

		def 							__lowerCamelCase							(   self ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				):
			__lowerCAmelCase    :		str		    =   ViTModel(config=UpperCAmelCase_				)
			model.to(UpperCAmelCase_				)
			model.eval()
			__lowerCAmelCase    :		str		    =   model(UpperCAmelCase_				)
			self.parent.assertEqual(result.last_hidden_state.shape ,	(self.batch_size, self.seq_length, self.hidden_size)				)

		def 							__lowerCamelCase							(   self ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				):
			__lowerCAmelCase    :		Union[str, Any]		    =   ViTForMaskedImageModeling(config=UpperCAmelCase_				)
			model.to(UpperCAmelCase_				)
			model.eval()
			__lowerCAmelCase    :		Any		    =   model(UpperCAmelCase_				)
			self.parent.assertEqual(
			    result.reconstruction.shape ,	(self.batch_size, self.num_channels, self.image_size, self.image_size)				)

			# test greyscale images
			__lowerCAmelCase    :		Tuple		    =   1
			__lowerCAmelCase    :		Tuple		    =   ViTForMaskedImageModeling(UpperCAmelCase_				)
			model.to(UpperCAmelCase_				)
			model.eval()

			__lowerCAmelCase    :		Dict		    =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
			__lowerCAmelCase    :		List[Any]		    =   model(UpperCAmelCase_				)
			self.parent.assertEqual(result.reconstruction.shape ,	(self.batch_size, 1, self.image_size, self.image_size)				)

		def 							__lowerCamelCase							(   self ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				):
			__lowerCAmelCase    :		Optional[Any]		    =   self.type_sequence_label_size
			__lowerCAmelCase    :		List[Any]		    =   ViTForImageClassification(UpperCAmelCase_				)
			model.to(UpperCAmelCase_				)
			model.eval()
			__lowerCAmelCase    :		Any		    =   model(UpperCAmelCase_ ,	labels=UpperCAmelCase_				)
			self.parent.assertEqual(result.logits.shape ,	(self.batch_size, self.type_sequence_label_size)				)

			# test greyscale images
			__lowerCAmelCase    :		Dict		    =   1
			__lowerCAmelCase    :		List[str]		    =   ViTForImageClassification(UpperCAmelCase_				)
			model.to(UpperCAmelCase_				)
			model.eval()

			__lowerCAmelCase    :		Tuple		    =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
			__lowerCAmelCase    :		int		    =   model(UpperCAmelCase_				)
			self.parent.assertEqual(result.logits.shape ,	(self.batch_size, self.type_sequence_label_size)				)







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




@require_torch
class 				A__ (			__UpperCAmelCase ,  __UpperCAmelCase ,  unittest.TestCase):
		A_		: List[str]            =			(
		    (
		        ViTModel,
		        ViTForImageClassification,
		        ViTForMaskedImageModeling,
		    )
		    if is_torch_available()
		    else ()
		)
		A_		: int            =			(
		    {"""feature-extraction""": ViTModel, """image-classification""": ViTForImageClassification}
		    if is_torch_available()
		    else {}
		)
		A_		: List[Any]            =			True

		A_		: List[Any]            =			False
		A_		: List[Any]            =			False
		A_		: Any            =			False

		def 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    :		Any		    =   ViTModelTester(self				)
			__lowerCAmelCase    :		str		    =   ConfigTester(self ,	config_class=UpperCAmelCase_ ,	has_text_modality=UpperCAmelCase_ ,	hidden_size=37				)

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

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

		def 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    ,     __lowerCAmelCase    :		Optional[Any]		    =   self.model_tester.prepare_config_and_inputs_for_common()

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

		def 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    ,     __lowerCAmelCase    :		List[Any]		    =   self.model_tester.prepare_config_and_inputs_for_common()

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

				__lowerCAmelCase    :		Dict		    =   ['pixel_values']
				self.assertListEqual(arg_names[:1] ,	UpperCAmelCase_				)

		def 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    :		Tuple		    =   self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*UpperCAmelCase_				)

		def 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    :		str		    =   self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_masked_image_modeling(*UpperCAmelCase_				)

		def 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    :		Any		    =   self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_				)







		@slow
		def 							__lowerCamelCase							(   self				):
			for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__lowerCAmelCase    :		Any		    =   ViTModel.from_pretrained(UpperCAmelCase_				)
				self.assertIsNotNone(UpperCAmelCase_				)



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




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

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

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

			__lowerCAmelCase    :		Any		    =   self.default_image_processor
			__lowerCAmelCase    :		Union[str, Any]		    =   prepare_img()
			__lowerCAmelCase    :		Dict		    =   image_processor(images=UpperCAmelCase_ ,	return_tensors='pt'				).to(UpperCAmelCase_				)

			# forward pass
			with torch.no_grad():
				__lowerCAmelCase    :		Any		    =   model(**UpperCAmelCase_				)

			# verify the logits
			__lowerCAmelCase    :		List[Any]		    =   torch.Size((1, 10_00)				)
			self.assertEqual(outputs.logits.shape ,	UpperCAmelCase_				)

			__lowerCAmelCase    :		Tuple		    =   torch.tensor([-0.2744, 0.8215, -0.0836]				).to(UpperCAmelCase_				)

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

		@slow
		def 							__lowerCamelCase							(   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.
			__lowerCAmelCase    :		Optional[int]		    =   ViTModel.from_pretrained('facebook/dino-vits8'				).to(UpperCAmelCase_				)

			__lowerCAmelCase    :		List[Any]		    =   ViTImageProcessor.from_pretrained('facebook/dino-vits8' ,	size=4_80				)
			__lowerCAmelCase    :		List[str]		    =   prepare_img()
			__lowerCAmelCase    :		int		    =   image_processor(images=UpperCAmelCase_ ,	return_tensors='pt'				)
			__lowerCAmelCase    :		Tuple		    =   inputs.pixel_values.to(UpperCAmelCase_				)

			# forward pass
			with torch.no_grad():
				__lowerCAmelCase    :		Tuple		    =   model(UpperCAmelCase_ ,	interpolate_pos_encoding=UpperCAmelCase_				)

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

			__lowerCAmelCase    :		List[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 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    :		int		    =   ViTModel.from_pretrained('facebook/dino-vits8' ,	torch_dtype=torch.floataa ,	device_map='auto'				)
			__lowerCAmelCase    :		Union[str, Any]		    =   self.default_image_processor

			__lowerCAmelCase    :		Any		    =   prepare_img()
			__lowerCAmelCase    :		str		    =   image_processor(images=UpperCAmelCase_ ,	return_tensors='pt'				)
			__lowerCAmelCase    :		Dict		    =   inputs.pixel_values.to(UpperCAmelCase_				)

			# forward pass to make sure inference works in fp16
			with torch.no_grad():
				__lowerCAmelCase    :		Optional[Any]		    =   model(UpperCAmelCase_				)

"""simple docstring"""







from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType




class 				A__ (			_lowerCamelCase):
		A_		: Any            =			['image_processor', 'tokenizer']
		A_		: Optional[Any]            =			'BlipImageProcessor'
		A_		: int            =			'AutoTokenizer'

		def __init__(   self ,	_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				):
			__lowerCAmelCase    :		Union[str, Any]		    =   False
			super().__init__(_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				)
			__lowerCAmelCase    :		List[Any]		    =   self.image_processor

		def __call__(   self ,	_SCREAMING_SNAKE_CASE = None ,	_SCREAMING_SNAKE_CASE = None ,	_SCREAMING_SNAKE_CASE = True ,	_SCREAMING_SNAKE_CASE = False ,	_SCREAMING_SNAKE_CASE = None ,	_SCREAMING_SNAKE_CASE = None ,	_SCREAMING_SNAKE_CASE = 0 ,	_SCREAMING_SNAKE_CASE = None ,	_SCREAMING_SNAKE_CASE = None ,	_SCREAMING_SNAKE_CASE = False ,	_SCREAMING_SNAKE_CASE = False ,	_SCREAMING_SNAKE_CASE = False ,	_SCREAMING_SNAKE_CASE = False ,	_SCREAMING_SNAKE_CASE = False ,	_SCREAMING_SNAKE_CASE = True ,	_SCREAMING_SNAKE_CASE = None ,	**_SCREAMING_SNAKE_CASE ,	):
			if images is None and text is None:
				raise ValueError('You have to specify either images or text.'				)

			# Get only text
			if images is None:
				__lowerCAmelCase    :		Optional[int]		    =   self.tokenizer
				__lowerCAmelCase    :		List[str]		    =   self.tokenizer(
				    text=_SCREAMING_SNAKE_CASE ,	add_special_tokens=_SCREAMING_SNAKE_CASE ,	padding=_SCREAMING_SNAKE_CASE ,	truncation=_SCREAMING_SNAKE_CASE ,	max_length=_SCREAMING_SNAKE_CASE ,	stride=_SCREAMING_SNAKE_CASE ,	pad_to_multiple_of=_SCREAMING_SNAKE_CASE ,	return_attention_mask=_SCREAMING_SNAKE_CASE ,	return_overflowing_tokens=_SCREAMING_SNAKE_CASE ,	return_special_tokens_mask=_SCREAMING_SNAKE_CASE ,	return_offsets_mapping=_SCREAMING_SNAKE_CASE ,	return_token_type_ids=_SCREAMING_SNAKE_CASE ,	return_length=_SCREAMING_SNAKE_CASE ,	verbose=_SCREAMING_SNAKE_CASE ,	return_tensors=_SCREAMING_SNAKE_CASE ,	**_SCREAMING_SNAKE_CASE ,	)
				return text_encoding

			# add pixel_values
			__lowerCAmelCase    :		List[Any]		    =   self.image_processor(_SCREAMING_SNAKE_CASE ,	return_tensors=_SCREAMING_SNAKE_CASE				)

			if text is not None:
				__lowerCAmelCase    :		Optional[int]		    =   self.tokenizer(
				    text=_SCREAMING_SNAKE_CASE ,	add_special_tokens=_SCREAMING_SNAKE_CASE ,	padding=_SCREAMING_SNAKE_CASE ,	truncation=_SCREAMING_SNAKE_CASE ,	max_length=_SCREAMING_SNAKE_CASE ,	stride=_SCREAMING_SNAKE_CASE ,	pad_to_multiple_of=_SCREAMING_SNAKE_CASE ,	return_attention_mask=_SCREAMING_SNAKE_CASE ,	return_overflowing_tokens=_SCREAMING_SNAKE_CASE ,	return_special_tokens_mask=_SCREAMING_SNAKE_CASE ,	return_offsets_mapping=_SCREAMING_SNAKE_CASE ,	return_token_type_ids=_SCREAMING_SNAKE_CASE ,	return_length=_SCREAMING_SNAKE_CASE ,	verbose=_SCREAMING_SNAKE_CASE ,	return_tensors=_SCREAMING_SNAKE_CASE ,	**_SCREAMING_SNAKE_CASE ,	)
			else:
				__lowerCAmelCase    :		int		    =   None

			if text_encoding is not None:
				encoding_image_processor.update(_SCREAMING_SNAKE_CASE				)

			return encoding_image_processor

		def 							__lowerCamelCase							(   self ,	*_SCREAMING_SNAKE_CASE ,	**_SCREAMING_SNAKE_CASE				):
			return self.tokenizer.batch_decode(*_SCREAMING_SNAKE_CASE ,	**_SCREAMING_SNAKE_CASE				)

		def 							__lowerCamelCase							(   self ,	*_SCREAMING_SNAKE_CASE ,	**_SCREAMING_SNAKE_CASE				):
			return self.tokenizer.decode(*_SCREAMING_SNAKE_CASE ,	**_SCREAMING_SNAKE_CASE				)







		@property
		# Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
		def 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    :		int		    =   self.tokenizer.model_input_names
			__lowerCAmelCase    :		Union[str, Any]		    =   self.image_processor.model_input_names
			return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names				)				)