


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

		"""simple docstring"""





		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.0_2					,				SCREAMING_SNAKE_CASE__=None					,				SCREAMING_SNAKE_CASE__=2					,				)      ->	List[str]:

					"""simple docstring"""



					_UpperCamelCase					:Dict					     =	parent
					_UpperCamelCase					:Any					     =	batch_size
					_UpperCamelCase					:str					     =	image_size
					_UpperCamelCase					:int					     =	patch_size
					_UpperCamelCase					:int					     =	num_channels
					_UpperCamelCase					:int					     =	is_training
					_UpperCamelCase					:List[str]					     =	use_labels
					_UpperCamelCase					:int					     =	hidden_size
					_UpperCamelCase					:Tuple					     =	num_hidden_layers
					_UpperCamelCase					:Optional[int]					     =	num_attention_heads
					_UpperCamelCase					:Optional[int]					     =	intermediate_size
					_UpperCamelCase					:Tuple					     =	hidden_act
					_UpperCamelCase					:int					     =	hidden_dropout_prob
					_UpperCamelCase					:int					     =	attention_probs_dropout_prob
					_UpperCamelCase					:Any					     =	type_sequence_label_size
					_UpperCamelCase					:List[str]					     =	initializer_range
					_UpperCamelCase					:Optional[Any]					     =	scope
					_UpperCamelCase					:Union[str, Any]					     =	encoder_stride

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

					"""simple docstring"""



					_UpperCamelCase					:List[Any]					     =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

					_UpperCamelCase					:List[Any]					     =	self.get_config()

					return config, pixel_values, labels
		def   _UpperCamelCase( self   )      ->	List[str]:

					"""simple docstring"""



					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=SCREAMING_SNAKE_CASE__					,				initializer_range=self.initializer_range					,				encoder_stride=self.encoder_stride					,				)
		def   _UpperCamelCase( self					,				SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__   )      ->	List[Any]:

					"""simple docstring"""



					_UpperCamelCase					:Any					     =	ViTModel(config=SCREAMING_SNAKE_CASE__   )
					model.to(SCREAMING_SNAKE_CASE__   )
					model.eval()
					_UpperCamelCase					:int					     =	model(SCREAMING_SNAKE_CASE__   )
					self.parent.assertEqual(result.last_hidden_state.shape					,				(self.batch_size, self.seq_length, self.hidden_size)   )
		def   _UpperCamelCase( self					,				SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__   )      ->	str:

					"""simple docstring"""



					_UpperCamelCase					:Union[str, Any]					     =	ViTForMaskedImageModeling(config=SCREAMING_SNAKE_CASE__   )
					model.to(SCREAMING_SNAKE_CASE__   )
					model.eval()
					_UpperCamelCase					:Optional[int]					     =	model(SCREAMING_SNAKE_CASE__   )
					self.parent.assertEqual(
					    result.reconstruction.shape					,				(self.batch_size, self.num_channels, self.image_size, self.image_size)   )

					# test greyscale images
					_UpperCamelCase					:Any					     =	1
					_UpperCamelCase					:List[Any]					     =	ViTForMaskedImageModeling(SCREAMING_SNAKE_CASE__   )
					model.to(SCREAMING_SNAKE_CASE__   )
					model.eval()

					_UpperCamelCase					:Any					     =	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
					_UpperCamelCase					:str					     =	model(SCREAMING_SNAKE_CASE__   )
					self.parent.assertEqual(result.reconstruction.shape					,				(self.batch_size, 1, self.image_size, self.image_size)   )
		def   _UpperCamelCase( self					,				SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__   )      ->	Dict:

					"""simple docstring"""



					_UpperCamelCase					:str					     =	self.type_sequence_label_size
					_UpperCamelCase					:str					     =	ViTForImageClassification(SCREAMING_SNAKE_CASE__   )
					model.to(SCREAMING_SNAKE_CASE__   )
					model.eval()
					_UpperCamelCase					:Optional[int]					     =	model(SCREAMING_SNAKE_CASE__					,				labels=SCREAMING_SNAKE_CASE__   )
					self.parent.assertEqual(result.logits.shape					,				(self.batch_size, self.type_sequence_label_size)   )

					# test greyscale images
					_UpperCamelCase					:str					     =	1
					_UpperCamelCase					:List[str]					     =	ViTForImageClassification(SCREAMING_SNAKE_CASE__   )
					model.to(SCREAMING_SNAKE_CASE__   )
					model.eval()

					_UpperCamelCase					:List[str]					     =	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
					_UpperCamelCase					:str					     =	model(SCREAMING_SNAKE_CASE__   )
					self.parent.assertEqual(result.logits.shape					,				(self.batch_size, self.type_sequence_label_size)   )





		def   _UpperCamelCase( self   )      ->	Union[str, Any]:

					"""simple docstring"""



					_UpperCamelCase					:Any					     =	self.prepare_config_and_inputs()
					(
					    (
					    _UpperCamelCase
					)     ,      (
					    _UpperCamelCase
					)     ,      (
					    _UpperCamelCase
					)     ,      
					)					:Dict					     =	config_and_inputs
					_UpperCamelCase					:Optional[Any]					     =	{'''pixel_values''': pixel_values}
					return config, inputs_dict






@require_torch
class      A(  lowerCamelCase__				,							lowerCamelCase__				,							unittest.TestCase		):

		"""simple docstring"""





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

		A    =							False
		A    =							False
		A    =							False
		def   _UpperCamelCase( self   )      ->	str:

					"""simple docstring"""



					_UpperCamelCase					:int					     =	ViTModelTester(self   )
					_UpperCamelCase					:Tuple					     =	ConfigTester(self					,				config_class=SCREAMING_SNAKE_CASE__					,				has_text_modality=SCREAMING_SNAKE_CASE__					,				hidden_size=37   )
		def   _UpperCamelCase( self   )      ->	List[Any]:

					"""simple docstring"""



					self.config_tester.run_common_tests()
		@unittest.skip(reason='''ViT does not use inputs_embeds'''   )
		def   _UpperCamelCase( self   )      ->	Any:

					"""simple docstring"""



					pass
		def   _UpperCamelCase( self   )      ->	int:

					"""simple docstring"""



					_UpperCamelCase     ,      _UpperCamelCase					:str					     =	self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
								_UpperCamelCase					:Optional[int]					     =	model_class(SCREAMING_SNAKE_CASE__   )
								self.assertIsInstance(model.get_input_embeddings()					,				(nn.Module)   )
								_UpperCamelCase					:Union[str, Any]					     =	model.get_output_embeddings()
								self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE__					,				nn.Linear   )   )
		def   _UpperCamelCase( self   )      ->	Any:

					"""simple docstring"""



					_UpperCamelCase     ,      _UpperCamelCase					:List[str]					     =	self.model_tester.prepare_config_and_inputs_for_common()

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

								_UpperCamelCase					:Any					     =	['''pixel_values''']
								self.assertListEqual(arg_names[:1]					,				SCREAMING_SNAKE_CASE__   )
		def   _UpperCamelCase( self   )      ->	List[str]:

					"""simple docstring"""



					_UpperCamelCase					:Dict					     =	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__   )
		def   _UpperCamelCase( self   )      ->	str:

					"""simple docstring"""



					_UpperCamelCase					:str					     =	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE__   )
		def   _UpperCamelCase( self   )      ->	str:

					"""simple docstring"""



					_UpperCamelCase					:int					     =	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE__   )





		@slow
		def   _UpperCamelCase( self   )      ->	Optional[int]:

					"""simple docstring"""



					for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								_UpperCamelCase					:List[Any]					     =	ViTModel.from_pretrained(SCREAMING_SNAKE_CASE__   )
								self.assertIsNotNone(SCREAMING_SNAKE_CASE__   )





def 				A_       ( )					->  Optional[int]:
			_UpperCamelCase					:int					     =	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
			return image






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

		"""simple docstring"""





		@cached_property
		def   _UpperCamelCase( self   )      ->	str:

					"""simple docstring"""



					return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224'''   ) if is_vision_available() else None
		@slow
		def   _UpperCamelCase( self   )      ->	Any:

					"""simple docstring"""



					_UpperCamelCase					:List[str]					     =	ViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''   ).to(SCREAMING_SNAKE_CASE__   )

					_UpperCamelCase					:Optional[Any]					     =	self.default_image_processor
					_UpperCamelCase					:str					     =	prepare_img()
					_UpperCamelCase					:List[Any]					     =	image_processor(images=SCREAMING_SNAKE_CASE__					,				return_tensors='''pt'''   ).to(SCREAMING_SNAKE_CASE__   )

					# forward pass
					with torch.no_grad():
								_UpperCamelCase					:str					     =	model(**SCREAMING_SNAKE_CASE__   )

					# verify the logits
					_UpperCamelCase					:Dict					     =	torch.Size((1, 10_00)   )
					self.assertEqual(outputs.logits.shape					,				SCREAMING_SNAKE_CASE__   )

					_UpperCamelCase					:Dict					     =	torch.tensor([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6]   ).to(SCREAMING_SNAKE_CASE__   )

					self.assertTrue(torch.allclose(outputs.logits[0, :3]					,				SCREAMING_SNAKE_CASE__					,				atol=1E-4   )   )
		@slow
		def   _UpperCamelCase( self   )      ->	str:

					"""simple docstring"""



					_UpperCamelCase					:Tuple					     =	ViTModel.from_pretrained('''facebook/dino-vits8'''   ).to(SCREAMING_SNAKE_CASE__   )

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

					# forward pass
					with torch.no_grad():
								_UpperCamelCase					:Tuple					     =	model(SCREAMING_SNAKE_CASE__					,				interpolate_pos_encoding=SCREAMING_SNAKE_CASE__   )

					# verify the logits
					_UpperCamelCase					:Optional[Any]					     =	torch.Size((1, 36_01, 3_84)   )
					self.assertEqual(outputs.last_hidden_state.shape					,				SCREAMING_SNAKE_CASE__   )

					_UpperCamelCase					:List[Any]					     =	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(SCREAMING_SNAKE_CASE__   )

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





		@slow
		@require_accelerate
		@require_torch_gpu
		def   _UpperCamelCase( self   )      ->	Dict:

					"""simple docstring"""



					_UpperCamelCase					:List[Any]					     =	ViTModel.from_pretrained('''facebook/dino-vits8'''					,				torch_dtype=torch.floataa					,				device_map='''auto'''   )
					_UpperCamelCase					:Tuple					     =	self.default_image_processor

					_UpperCamelCase					:Union[str, Any]					     =	prepare_img()
					_UpperCamelCase					:Tuple					     =	image_processor(images=SCREAMING_SNAKE_CASE__					,				return_tensors='''pt'''   )
					_UpperCamelCase					:List[Any]					     =	inputs.pixel_values.to(SCREAMING_SNAKE_CASE__   )

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





"""simple docstring"""






import os
import sys

UpperCamelCase__				:Union[str, Any]			   =	os.path.join(os.path.dirname(__file__), """src""")
sys.path.append(SRC_DIR)


from transformers import (
    AutoConfig,
    AutoModel,
    AutoModelForCausalLM,
    AutoModelForMaskedLM,
    AutoModelForQuestionAnswering,
    AutoModelForSequenceClassification,
    AutoTokenizer,
    add_start_docstrings,
)


UpperCamelCase__				:List[Any]			   =	[
    """torch""",
    """numpy""",
    """tokenizers""",
    """filelock""",
    """requests""",
    """tqdm""",
    """regex""",
    """sentencepiece""",
    """sacremoses""",
    """importlib_metadata""",
    """huggingface_hub""",
]





@add_start_docstrings(AutoConfig.__doc__		)
def 				A_       ( *snake_case__       ,					**snake_case__		)					->  int:
			return AutoConfig.from_pretrained(*snake_case__       ,					**snake_case__		)





@add_start_docstrings(AutoTokenizer.__doc__		)
def 				A_       ( *snake_case__       ,					**snake_case__		)					->  int:
			return AutoTokenizer.from_pretrained(*snake_case__       ,					**snake_case__		)





@add_start_docstrings(AutoModel.__doc__		)
def 				A_       ( *snake_case__       ,					**snake_case__		)					->  Dict:
			return AutoModel.from_pretrained(*snake_case__       ,					**snake_case__		)





@add_start_docstrings(AutoModelForCausalLM.__doc__		)
def 				A_       ( *snake_case__       ,					**snake_case__		)					->  Dict:
			return AutoModelForCausalLM.from_pretrained(*snake_case__       ,					**snake_case__		)





@add_start_docstrings(AutoModelForMaskedLM.__doc__		)
def 				A_       ( *snake_case__       ,					**snake_case__		)					->  Dict:
			return AutoModelForMaskedLM.from_pretrained(*snake_case__       ,					**snake_case__		)





@add_start_docstrings(AutoModelForSequenceClassification.__doc__		)
def 				A_       ( *snake_case__       ,					**snake_case__		)					->  Dict:
			return AutoModelForSequenceClassification.from_pretrained(*snake_case__       ,					**snake_case__		)





@add_start_docstrings(AutoModelForQuestionAnswering.__doc__		)
def 				A_       ( *snake_case__       ,					**snake_case__		)					->  Union[str, Any]:
			return AutoModelForQuestionAnswering.from_pretrained(*snake_case__       ,					**snake_case__		)

