
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				,       UpperCamelCase__				,       UpperCamelCase__=13				,       UpperCamelCase__=30				,       UpperCamelCase__=2				,       UpperCamelCase__=3				,       UpperCamelCase__=True				,       UpperCamelCase__=True				,       UpperCamelCase__=32				,       UpperCamelCase__=5				,       UpperCamelCase__=4				,       UpperCamelCase__=37				,       UpperCamelCase__="gelu"				,       UpperCamelCase__=0.1				,       UpperCamelCase__=0.1				,       UpperCamelCase__=10				,       UpperCamelCase__=0.0_2				,       UpperCamelCase__=None				,       UpperCamelCase__=2				,       ):
					A__  :	int         =							parent
					A__  :	Union[str, Any]         =							batch_size
					A__  :	Dict         =							image_size
					A__  :	int         =							patch_size
					A__  :	Any         =							num_channels
					A__  :	List[Any]         =							is_training
					A__  :	Optional[Any]         =							use_labels
					A__  :	str         =							hidden_size
					A__  :	int         =							num_hidden_layers
					A__  :	Tuple         =							num_attention_heads
					A__  :	Tuple         =							intermediate_size
					A__  :	str         =							hidden_act
					A__  :	Optional[int]         =							hidden_dropout_prob
					A__  :	Tuple         =							attention_probs_dropout_prob
					A__  :	str         =							type_sequence_label_size
					A__  :	Optional[int]         =							initializer_range
					A__  :	Optional[int]         =							scope
					A__  :	str         =							encoder_stride

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





	def   __snake_case				(			self		):
					A__  :	str         =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

					A__  :	str         =							None
					if self.use_labels:
									A__  :	Dict         =							ids_tensor([self.batch_size]				,       self.type_sequence_label_size		)

					A__  :	Tuple         =							self.get_config()

					return config, pixel_values, labels





	def   __snake_case				(			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   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__  :	Optional[Any]         =							ViTModel(config=UpperCamelCase__		)
					model.to(UpperCamelCase__		)
					model.eval()
					A__  :	int         =							model(UpperCamelCase__		)
					self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, self.seq_length, self.hidden_size)		)





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__  :	List[Any]         =							ViTForMaskedImageModeling(config=UpperCamelCase__		)
					model.to(UpperCamelCase__		)
					model.eval()
					A__  :	Any         =							model(UpperCamelCase__		)
					self.parent.assertEqual(
					    result.reconstruction.shape				,       (self.batch_size, self.num_channels, self.image_size, self.image_size)		)

					# test greyscale images
					A__  :	Optional[Any]         =							1
					A__  :	str         =							ViTForMaskedImageModeling(UpperCamelCase__		)
					model.to(UpperCamelCase__		)
					model.eval()

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





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__  :	str         =							self.type_sequence_label_size
					A__  :	str         =							ViTForImageClassification(UpperCamelCase__		)
					model.to(UpperCamelCase__		)
					model.eval()
					A__  :	Any         =							model(UpperCamelCase__				,       labels=UpperCamelCase__		)
					self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.type_sequence_label_size)		)

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

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





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







@require_torch
class  UpperCamelCase__		(    SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			unittest.TestCase   ):







	'''simple docstring'''
	_lowerCAmelCase							       =				(
	    (
	        ViTModel,
	        ViTForImageClassification,
	        ViTForMaskedImageModeling,
	    )
	    if is_torch_available()
	    else ()
	)
	_lowerCAmelCase							       =				(
	    {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification}
	    if is_torch_available()
	    else {}
	)
	_lowerCAmelCase							       =				True

	_lowerCAmelCase							       =				False
	_lowerCAmelCase							       =				False
	_lowerCAmelCase							       =				False





	def   __snake_case				(			self		):
					A__  :	Union[str, Any]         =							ViTModelTester(self		)
					A__  :	Optional[Any]         =							ConfigTester(self				,       config_class=UpperCamelCase__				,       has_text_modality=UpperCamelCase__				,       hidden_size=37		)





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





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





	def   __snake_case				(			self		):
					A__       ,							A__  :	Any         =							self.model_tester.prepare_config_and_inputs_for_common()

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





	def   __snake_case				(			self		):
					A__       ,							A__  :	int         =							self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
									A__  :	Optional[int]         =							model_class(UpperCamelCase__		)
									A__  :	Union[str, Any]         =							inspect.signature(model.forward		)
									# signature.parameters is an OrderedDict => so arg_names order is deterministic
									A__  :	Any         =							[*signature.parameters.keys()]

									A__  :	Optional[int]         =							['''pixel_values''']
									self.assertListEqual(arg_names[:1]				,       UpperCamelCase__		)





	def   __snake_case				(			self		):
					A__  :	int         =							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*UpperCamelCase__		)





	def   __snake_case				(			self		):
					A__  :	List[Any]         =							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase__		)





	def   __snake_case				(			self		):
					A__  :	Any         =							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__		)





	@slow
	def   __snake_case				(			self		):
					for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									A__  :	Tuple         =							ViTModel.from_pretrained(UpperCamelCase__		)
									self.assertIsNotNone(UpperCamelCase__		)




def 				SCREAMING_SNAKE_CASE     (      ) ->       Optional[int]:

				"""simple docstring"""







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







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







	'''simple docstring'''





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





	@slow
	def   __snake_case				(			self		):
					A__  :	Any         =							ViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''		).to(UpperCamelCase__		)

					A__  :	List[str]         =							self.default_image_processor
					A__  :	Dict         =							prepare_img()
					A__  :	Dict         =							image_processor(images=UpperCamelCase__				,       return_tensors='''pt'''		).to(UpperCamelCase__		)

					# forward pass
					with torch.no_grad():
									A__  :	Optional[int]         =							model(**UpperCamelCase__		)

					# verify the logits
					A__  :	Any         =							torch.Size((1, 1000)		)
					self.assertEqual(outputs.logits.shape				,       UpperCamelCase__		)

					A__  :	Optional[int]         =							torch.tensor([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6]		).to(UpperCamelCase__		)

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





	@slow
	def   __snake_case				(			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__  :	Optional[Any]         =							ViTModel.from_pretrained('''facebook/dino-vits8'''		).to(UpperCamelCase__		)

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

					# forward pass
					with torch.no_grad():
									A__  :	int         =							model(UpperCamelCase__				,       interpolate_pos_encoding=UpperCamelCase__		)

					# verify the logits
					A__  :	Dict         =							torch.Size((1, 3601, 384)		)
					self.assertEqual(outputs.last_hidden_state.shape				,       UpperCamelCase__		)

					A__  :	int         =							torch.tensor(
					    [[4.2_3_4_0, 4.3_9_0_6, -6.6_6_9_2], [4.5_4_6_3, 1.8_9_2_8, -6.7_2_5_7], [4.4_4_2_9, 0.8_4_9_6, -5.8_5_8_5]]		).to(UpperCamelCase__		)

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





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

					A__  :	Optional[Any]         =							prepare_img()
					A__  :	Tuple         =							image_processor(images=UpperCamelCase__				,       return_tensors='''pt'''		)
					A__  :	Dict         =							inputs.pixel_values.to(UpperCamelCase__		)

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

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tensorflow_text_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_SCREAMING_SNAKE_CASE    :      int          =			{
    'configuration_bert': ['BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BertConfig', 'BertOnnxConfig'],
    'tokenization_bert': ['BasicTokenizer', 'BertTokenizer', 'WordpieceTokenizer'],
}

try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Optional[Any]          =			['BertTokenizerFast']

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Union[str, Any]          =			[
					    'BERT_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'BertForMaskedLM',
					    'BertForMultipleChoice',
					    'BertForNextSentencePrediction',
					    'BertForPreTraining',
					    'BertForQuestionAnswering',
					    'BertForSequenceClassification',
					    'BertForTokenClassification',
					    'BertLayer',
					    'BertLMHeadModel',
					    'BertModel',
					    'BertPreTrainedModel',
					    'load_tf_weights_in_bert',
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Tuple          =			[
					    'TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'TFBertEmbeddings',
					    'TFBertForMaskedLM',
					    'TFBertForMultipleChoice',
					    'TFBertForNextSentencePrediction',
					    'TFBertForPreTraining',
					    'TFBertForQuestionAnswering',
					    'TFBertForSequenceClassification',
					    'TFBertForTokenClassification',
					    'TFBertLMHeadModel',
					    'TFBertMainLayer',
					    'TFBertModel',
					    'TFBertPreTrainedModel',
					]
try:
					if not is_tensorflow_text_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Dict          =			['TFBertTokenizer']

try:
					if not is_flax_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Optional[int]          =			[
					    'FlaxBertForCausalLM',
					    'FlaxBertForMaskedLM',
					    'FlaxBertForMultipleChoice',
					    'FlaxBertForNextSentencePrediction',
					    'FlaxBertForPreTraining',
					    'FlaxBertForQuestionAnswering',
					    'FlaxBertForSequenceClassification',
					    'FlaxBertForTokenClassification',
					    'FlaxBertModel',
					    'FlaxBertPreTrainedModel',
					]

if TYPE_CHECKING:
					from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig, BertOnnxConfig
					from .tokenization_bert import BasicTokenizer, BertTokenizer, WordpieceTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_bert_fast import BertTokenizerFast

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_bert import (
										    BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
										    BertForMaskedLM,
										    BertForMultipleChoice,
										    BertForNextSentencePrediction,
										    BertForPreTraining,
										    BertForQuestionAnswering,
										    BertForSequenceClassification,
										    BertForTokenClassification,
										    BertLayer,
										    BertLMHeadModel,
										    BertModel,
										    BertPreTrainedModel,
										    load_tf_weights_in_bert,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_bert import (
										    TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFBertEmbeddings,
										    TFBertForMaskedLM,
										    TFBertForMultipleChoice,
										    TFBertForNextSentencePrediction,
										    TFBertForPreTraining,
										    TFBertForQuestionAnswering,
										    TFBertForSequenceClassification,
										    TFBertForTokenClassification,
										    TFBertLMHeadModel,
										    TFBertMainLayer,
										    TFBertModel,
										    TFBertPreTrainedModel,
										)

					try:
										if not is_tensorflow_text_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_bert_tf import TFBertTokenizer

					try:
										if not is_flax_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_flax_bert import (
										    FlaxBertForCausalLM,
										    FlaxBertForMaskedLM,
										    FlaxBertForMultipleChoice,
										    FlaxBertForNextSentencePrediction,
										    FlaxBertForPreTraining,
										    FlaxBertForQuestionAnswering,
										    FlaxBertForSequenceClassification,
										    FlaxBertForTokenClassification,
										    FlaxBertModel,
										    FlaxBertPreTrainedModel,
										)

else:
					import sys

					_SCREAMING_SNAKE_CASE    :      Union[str, Any]          =			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)