





'''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 				__snake_case     :





	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      ,       )      ->		List[Any]:
		snake_case__	       =   parent
		snake_case__	       =   batch_size
		snake_case__	       =   image_size
		snake_case__	       =   patch_size
		snake_case__	       =   num_channels
		snake_case__	       =   is_training
		snake_case__	       =   use_labels
		snake_case__	       =   hidden_size
		snake_case__	       =   num_hidden_layers
		snake_case__	       =   num_attention_heads
		snake_case__	       =   intermediate_size
		snake_case__	       =   hidden_act
		snake_case__	       =   hidden_dropout_prob
		snake_case__	       =   attention_probs_dropout_prob
		snake_case__	       =   type_sequence_label_size
		snake_case__	       =   initializer_range
		snake_case__	       =   scope
		snake_case__	       =   encoder_stride

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





	def   _snake_case   (		self		)      ->		Tuple:
		snake_case__	       =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

		snake_case__	       =   self.get_config()

		return config, pixel_values, labels





	def   _snake_case   (		self		)      ->		Union[str, 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=UpperCamelCase_      ,       initializer_range=self.initializer_range      ,       encoder_stride=self.encoder_stride      ,       )





	def   _snake_case   (		self      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_		)      ->		Any:
		snake_case__	       =   ViTModel(config=UpperCamelCase_		)
		model.to(UpperCamelCase_		)
		model.eval()
		snake_case__	       =   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_		)      ->		List[str]:
		snake_case__	       =   ViTForMaskedImageModeling(config=UpperCamelCase_		)
		model.to(UpperCamelCase_		)
		model.eval()
		snake_case__	       =   model(UpperCamelCase_		)
		self.parent.assertEqual(
		    result.reconstruction.shape      ,       (self.batch_size, self.num_channels, self.image_size, self.image_size)		)

		# test greyscale images
		snake_case__	       =   1
		snake_case__	       =   ViTForMaskedImageModeling(UpperCamelCase_		)
		model.to(UpperCamelCase_		)
		model.eval()

		snake_case__	       =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
		snake_case__	       =   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_		)      ->		int:
		snake_case__	       =   self.type_sequence_label_size
		snake_case__	       =   ViTForImageClassification(UpperCamelCase_		)
		model.to(UpperCamelCase_		)
		model.eval()
		snake_case__	       =   model(UpperCamelCase_      ,       labels=UpperCamelCase_		)
		self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.type_sequence_label_size)		)

		# test greyscale images
		snake_case__	       =   1
		snake_case__	       =   ViTForImageClassification(UpperCamelCase_		)
		model.to(UpperCamelCase_		)
		model.eval()

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





	def   _snake_case   (		self		)      ->		Union[str, Any]:
		snake_case__	       =   self.prepare_config_and_inputs()
		(
		    (
		    snake_case__
		)       ,  (
		    snake_case__
		)       ,  (
		    snake_case__
		)       ,  
		)	       =   config_and_inputs
		snake_case__	       =   {'pixel_values': pixel_values}
		return config, inputs_dict






@require_torch
class 				__snake_case     (					__magic_name__       ,       __magic_name__       ,       unittest.TestCase							):
	__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		)      ->		List[str]:
		snake_case__	       =   ViTModelTester(self		)
		snake_case__	       =   ConfigTester(self      ,       config_class=UpperCamelCase_      ,       has_text_modality=UpperCamelCase_      ,       hidden_size=37		)





	def   _snake_case   (		self		)      ->		Optional[int]:
		self.config_tester.run_common_tests()





	@unittest.skip(reason='ViT does not use inputs_embeds'		)
	def   _snake_case   (		self		)      ->		Optional[int]:
		pass





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

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





	def   _snake_case   (		self		)      ->		Optional[Any]:
		snake_case__       ,  snake_case__	       =   self.model_tester.prepare_config_and_inputs_for_common()

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

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





	def   _snake_case   (		self		)      ->		str:
		snake_case__	       =   self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_model(*UpperCamelCase_		)





	def   _snake_case   (		self		)      ->		List[Any]:
		snake_case__	       =   self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase_		)





	def   _snake_case   (		self		)      ->		Optional[int]:
		snake_case__	       =   self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_		)





	@slow
	def   _snake_case   (		self		)      ->		Union[str, Any]:
		for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			snake_case__	       =   ViTModel.from_pretrained(UpperCamelCase_		)
			self.assertIsNotNone(UpperCamelCase_		)





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







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





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





	@slow
	def   _snake_case   (		self		)      ->		Union[str, Any]:
		snake_case__	       =   ViTForImageClassification.from_pretrained('google/vit-base-patch16-224'		).to(UpperCamelCase_		)

		snake_case__	       =   self.default_image_processor
		snake_case__	       =   prepare_img()
		snake_case__	       =   image_processor(images=UpperCamelCase_      ,       return_tensors='pt'		).to(UpperCamelCase_		)

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

		# verify the logits
		snake_case__	       =   torch.Size((1, 1000)		)
		self.assertEqual(outputs.logits.shape      ,       UpperCamelCase_		)

		snake_case__	       =   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		)      ->		List[str]:
		# 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__	       =   ViTModel.from_pretrained('facebook/dino-vits8'		).to(UpperCamelCase_		)

		snake_case__	       =   ViTImageProcessor.from_pretrained('facebook/dino-vits8'      ,       size=480		)
		snake_case__	       =   prepare_img()
		snake_case__	       =   image_processor(images=UpperCamelCase_      ,       return_tensors='pt'		)
		snake_case__	       =   inputs.pixel_values.to(UpperCamelCase_		)

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

		# verify the logits
		snake_case__	       =   torch.Size((1, 3601, 384)		)
		self.assertEqual(outputs.last_hidden_state.shape      ,       UpperCamelCase_		)

		snake_case__	       =   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		)      ->		int:
		snake_case__	       =   ViTModel.from_pretrained('facebook/dino-vits8'      ,       torch_dtype=torch.floataa      ,       device_map='auto'		)
		snake_case__	       =   self.default_image_processor

		snake_case__	       =   prepare_img()
		snake_case__	       =   image_processor(images=UpperCamelCase_      ,       return_tensors='pt'		)
		snake_case__	       =   inputs.pixel_values.to(UpperCamelCase_		)

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










'''simple docstring'''




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


a__					:			str          =     logging.get_logger(__name__)

a__					:			Any          =     {
    '''xlm-mlm-en-2048''': '''https://huggingface.co/xlm-mlm-en-2048/resolve/main/config.json''',
    '''xlm-mlm-ende-1024''': '''https://huggingface.co/xlm-mlm-ende-1024/resolve/main/config.json''',
    '''xlm-mlm-enfr-1024''': '''https://huggingface.co/xlm-mlm-enfr-1024/resolve/main/config.json''',
    '''xlm-mlm-enro-1024''': '''https://huggingface.co/xlm-mlm-enro-1024/resolve/main/config.json''',
    '''xlm-mlm-tlm-xnli15-1024''': '''https://huggingface.co/xlm-mlm-tlm-xnli15-1024/resolve/main/config.json''',
    '''xlm-mlm-xnli15-1024''': '''https://huggingface.co/xlm-mlm-xnli15-1024/resolve/main/config.json''',
    '''xlm-clm-enfr-1024''': '''https://huggingface.co/xlm-clm-enfr-1024/resolve/main/config.json''',
    '''xlm-clm-ende-1024''': '''https://huggingface.co/xlm-clm-ende-1024/resolve/main/config.json''',
    '''xlm-mlm-17-1280''': '''https://huggingface.co/xlm-mlm-17-1280/resolve/main/config.json''',
    '''xlm-mlm-100-1280''': '''https://huggingface.co/xlm-mlm-100-1280/resolve/main/config.json''',
}






class 				__snake_case     (					__magic_name__							):
	__lowerCAmelCase       						=  '''xlm'''
	__lowerCAmelCase       						=  {
	    '''hidden_size''': '''emb_dim''',
	    '''num_attention_heads''': '''n_heads''',
	    '''num_hidden_layers''': '''n_layers''',
	    '''n_words''': '''vocab_size''',  # For backward compatibility
	}





	def __init__(		self      ,       UpperCamelCase_=3_0145      ,       UpperCamelCase_=2048      ,       UpperCamelCase_=12      ,       UpperCamelCase_=16      ,       UpperCamelCase_=0.1      ,       UpperCamelCase_=0.1      ,       UpperCamelCase_=True      ,       UpperCamelCase_=False      ,       UpperCamelCase_=False      ,       UpperCamelCase_=False      ,       UpperCamelCase_=1      ,       UpperCamelCase_=True      ,       UpperCamelCase_=512      ,       UpperCamelCase_=2048**-0.5      ,       UpperCamelCase_=1E-1_2      ,       UpperCamelCase_=0.0_2      ,       UpperCamelCase_=0      ,       UpperCamelCase_=1      ,       UpperCamelCase_=2      ,       UpperCamelCase_=3      ,       UpperCamelCase_=5      ,       UpperCamelCase_=True      ,       UpperCamelCase_="first"      ,       UpperCamelCase_=True      ,       UpperCamelCase_=None      ,       UpperCamelCase_=True      ,       UpperCamelCase_=0.1      ,       UpperCamelCase_=5      ,       UpperCamelCase_=5      ,       UpperCamelCase_=0      ,       UpperCamelCase_=0      ,       UpperCamelCase_=2      ,       UpperCamelCase_=0      ,       **UpperCamelCase_      ,       )      ->		List[str]:
		snake_case__	       =   vocab_size
		snake_case__	       =   emb_dim
		snake_case__	       =   n_layers
		snake_case__	       =   n_heads
		snake_case__	       =   dropout
		snake_case__	       =   attention_dropout
		snake_case__	       =   gelu_activation
		snake_case__	       =   sinusoidal_embeddings
		snake_case__	       =   causal
		snake_case__	       =   asm
		snake_case__	       =   n_langs
		snake_case__	       =   use_lang_emb
		snake_case__	       =   layer_norm_eps
		snake_case__	       =   bos_index
		snake_case__	       =   eos_index
		snake_case__	       =   pad_index
		snake_case__	       =   unk_index
		snake_case__	       =   mask_index
		snake_case__	       =   is_encoder
		snake_case__	       =   max_position_embeddings
		snake_case__	       =   embed_init_std
		snake_case__	       =   init_std
		snake_case__	       =   summary_type
		snake_case__	       =   summary_use_proj
		snake_case__	       =   summary_activation
		snake_case__	       =   summary_proj_to_labels
		snake_case__	       =   summary_first_dropout
		snake_case__	       =   start_n_top
		snake_case__	       =   end_n_top
		snake_case__	       =   mask_token_id
		snake_case__	       =   lang_id

		if "n_words" in kwargs:
			snake_case__	       =   kwargs['n_words']

		super().__init__(pad_token_id=UpperCamelCase_      ,       bos_token_id=UpperCamelCase_      ,       **UpperCamelCase_		)






class 				__snake_case     (					__magic_name__							):





	@property
	def   _snake_case   (		self		)      ->		Mapping[str, Mapping[int, str]]:
		if self.task == "multiple-choice":
			snake_case__	       =   {0: 'batch', 1: 'choice', 2: 'sequence'}
		else:
			snake_case__	       =   {0: 'batch', 1: 'sequence'}
		return OrderedDict(
		    [
		        ('input_ids', dynamic_axis),
		        ('attention_mask', dynamic_axis),
		        ('token_type_ids', dynamic_axis),
		    ]		)



