

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



	"""simple docstring"""





	def __init__(    self				,			lowerCamelCase				,			lowerCamelCase=13				,			lowerCamelCase=30				,			lowerCamelCase=2				,			lowerCamelCase=3				,			lowerCamelCase=True				,			lowerCamelCase=True				,			lowerCamelCase=32				,			lowerCamelCase=5				,			lowerCamelCase=4				,			lowerCamelCase=37				,			lowerCamelCase="gelu"				,			lowerCamelCase=0.1				,			lowerCamelCase=0.1				,			lowerCamelCase=10				,			lowerCamelCase=0.02				,			lowerCamelCase=None				,			lowerCamelCase=2				,			)							->     Optional[int]:




				'''simple docstring'''






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

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

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     int:




				'''simple docstring'''






				UpperCamelCase				:	str    						=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

				UpperCamelCase				:	str    						=					self.get_config()

				return config, pixel_values, labels

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Optional[Any]:




				'''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=a__				,			initializer_range=self.initializer_range				,			encoder_stride=self.encoder_stride				,			)

	def 	SCREAMING_SNAKE_CASE__							(    self				,			lowerCamelCase				,			lowerCamelCase				,			lowerCamelCase   )							->     Any:




				'''simple docstring'''






				UpperCamelCase				:	List[Any]    						=					ViTModel(config=a__   )
				model.to(a__   )
				model.eval()
				UpperCamelCase				:	List[str]    						=					model(a__   )
				self.parent.assertEqual(result.last_hidden_state.shape				,			(self.batch_size, self.seq_length, self.hidden_size)   )

	def 	SCREAMING_SNAKE_CASE__							(    self				,			lowerCamelCase				,			lowerCamelCase				,			lowerCamelCase   )							->     str:




				'''simple docstring'''






				UpperCamelCase				:	Dict    						=					ViTForMaskedImageModeling(config=a__   )
				model.to(a__   )
				model.eval()
				UpperCamelCase				:	Optional[int]    						=					model(a__   )
				self.parent.assertEqual(
				    result.reconstruction.shape				,			(self.batch_size, self.num_channels, self.image_size, self.image_size)   )

				# test greyscale images
				UpperCamelCase				:	List[str]    						=					1
				UpperCamelCase				:	Any    						=					ViTForMaskedImageModeling(a__   )
				model.to(a__   )
				model.eval()

				UpperCamelCase				:	Optional[int]    						=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
				UpperCamelCase				:	str    						=					model(a__   )
				self.parent.assertEqual(result.reconstruction.shape				,			(self.batch_size, 1, self.image_size, self.image_size)   )

	def 	SCREAMING_SNAKE_CASE__							(    self				,			lowerCamelCase				,			lowerCamelCase				,			lowerCamelCase   )							->     Optional[int]:




				'''simple docstring'''






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

				# test greyscale images
				UpperCamelCase				:	int    						=					1
				UpperCamelCase				:	List[str]    						=					ViTForImageClassification(a__   )
				model.to(a__   )
				model.eval()

				UpperCamelCase				:	int    						=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
				UpperCamelCase				:	str    						=					model(a__   )
				self.parent.assertEqual(result.logits.shape				,			(self.batch_size, self.type_sequence_label_size)   )



	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Dict:




				'''simple docstring'''






				UpperCamelCase				:	Optional[Any]    						=					self.prepare_config_and_inputs()
				(
				    (
				    UpperCamelCase						
				)						,   (
				    UpperCamelCase						
				)						,   (
				    UpperCamelCase						
				)						,   						
				)				:	Union[str, Any]    						=					config_and_inputs
				UpperCamelCase				:	Union[str, Any]    						=					{"pixel_values": pixel_values}
				return config, inputs_dict

@require_torch
class     UpperCAmelCase_ (	A__				,	A__				,	unittest.TestCase    ):



	"""simple docstring"""





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

	__SCREAMING_SNAKE_CASE					=       False
	__SCREAMING_SNAKE_CASE					=       False
	__SCREAMING_SNAKE_CASE					=       False

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Optional[Any]:




				'''simple docstring'''






				UpperCamelCase				:	Dict    						=					ViTModelTester(self   )
				UpperCamelCase				:	Dict    						=					ConfigTester(self				,			config_class=a__				,			has_text_modality=a__				,			hidden_size=37   )

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Optional[Any]:




				'''simple docstring'''






				self.config_tester.run_common_tests()

	@unittest.skip(reason="ViT does not use inputs_embeds"   )
	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Any:




				'''simple docstring'''






				pass

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Optional[int]:




				'''simple docstring'''






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

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

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     int:




				'''simple docstring'''






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

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

							UpperCamelCase				:	List[Any]    						=					["pixel_values"]
							self.assertListEqual(arg_names[:1]				,			a__   )

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Any:




				'''simple docstring'''






				UpperCamelCase				:	Optional[int]    						=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*a__   )

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     List[Any]:




				'''simple docstring'''






				UpperCamelCase				:	int    						=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_image_modeling(*a__   )

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     List[Any]:




				'''simple docstring'''






				UpperCamelCase				:	Tuple    						=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*a__   )



	@slow
	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Optional[Any]:




				'''simple docstring'''






				for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCamelCase				:	Any    						=					ViTModel.from_pretrained(a__   )
							self.assertIsNotNone(a__   )

def       A__	(							):


			'''simple docstring'''


			UpperCamelCase				:	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 	SCREAMING_SNAKE_CASE__							(    self   )							->     Optional[int]:




				'''simple docstring'''






				return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"   ) if is_vision_available() else None

	@slow
	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     int:




				'''simple docstring'''






				UpperCamelCase				:	Optional[Any]    						=					ViTForImageClassification.from_pretrained("google/vit-base-patch16-224"   ).to(a__   )

				UpperCamelCase				:	Any    						=					self.default_image_processor
				UpperCamelCase				:	Tuple    						=					prepare_img()
				UpperCamelCase				:	int    						=					image_processor(images=a__				,			return_tensors="pt"   ).to(a__   )

				# forward pass
				with torch.no_grad():
							UpperCamelCase				:	List[str]    						=					model(**a__   )

				# verify the logits
				UpperCamelCase				:	Any    						=					torch.Size((1, 10_00)   )
				self.assertEqual(outputs.logits.shape				,			a__   )

				UpperCamelCase				:	List[Any]    						=					torch.tensor([-0.2744, 0.8215, -0.0836]   ).to(a__   )

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

	@slow
	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Optional[Any]:




				'''simple docstring'''






				UpperCamelCase				:	Union[str, Any]    						=					ViTModel.from_pretrained("facebook/dino-vits8"   ).to(a__   )

				UpperCamelCase				:	List[str]    						=					ViTImageProcessor.from_pretrained("facebook/dino-vits8"				,			size=4_80   )
				UpperCamelCase				:	Optional[Any]    						=					prepare_img()
				UpperCamelCase				:	Optional[Any]    						=					image_processor(images=a__				,			return_tensors="pt"   )
				UpperCamelCase				:	Union[str, Any]    						=					inputs.pixel_values.to(a__   )

				# forward pass
				with torch.no_grad():
							UpperCamelCase				:	str    						=					model(a__				,			interpolate_pos_encoding=a__   )

				# verify the logits
				UpperCamelCase				:	str    						=					torch.Size((1, 36_01, 3_84)   )
				self.assertEqual(outputs.last_hidden_state.shape				,			a__   )

				UpperCamelCase				:	Optional[Any]    						=					torch.tensor(
				    [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]   ).to(a__   )

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



	@slow
	@require_accelerate
	@require_torch_gpu
	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Optional[int]:




				'''simple docstring'''






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

				UpperCamelCase				:	List[str]    						=					prepare_img()
				UpperCamelCase				:	Union[str, Any]    						=					image_processor(images=a__				,			return_tensors="pt"   )
				UpperCamelCase				:	Optional[int]    						=					inputs.pixel_values.to(a__   )

				# forward pass to make sure inference works in fp16
				with torch.no_grad():
							UpperCamelCase				:	List[str]    						=					model(a__   )





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_lowercase    	=		{
    "configuration_upernet": ["UperNetConfig"],
}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _lowercase    	=		[
       "UperNetForSemanticSegmentation",
       "UperNetPreTrainedModel",
   ]


if TYPE_CHECKING:
   from .configuration_upernet import UperNetConfig

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_upernet import UperNetForSemanticSegmentation, UperNetPreTrainedModel


else:
   import sys

   _lowercase    	=		_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
