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

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

			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							UpperCamelCase_:							str		=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

							UpperCamelCase_:							Optional[int]		=  self.get_config()

							return config, pixel_values, labels

			def  lowerCAmelCase__		(    self	:    List[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=snake_case_  ,		initializer_range=self.initializer_range  ,		encoder_stride=self.encoder_stride  ,		)

			def  lowerCAmelCase__		(    self	:    str  ,		snake_case_	:    Optional[int]  ,		snake_case_	:    List[str]  ,		snake_case_	:    Any ):
							UpperCamelCase_:							int		=  ViTModel(config=snake_case_ )
							model.to(snake_case_ )
							model.eval()
							UpperCamelCase_:							List[str]		=  model(snake_case_ )
							self.parent.assertEqual(result.last_hidden_state.shape  ,		(self.batch_size, self.seq_length, self.hidden_size) )

			def  lowerCAmelCase__		(    self	:    Optional[Any]  ,		snake_case_	:    List[str]  ,		snake_case_	:    str  ,		snake_case_	:    Tuple ):
							UpperCamelCase_:							Any		=  ViTForMaskedImageModeling(config=snake_case_ )
							model.to(snake_case_ )
							model.eval()
							UpperCamelCase_:							List[Any]		=  model(snake_case_ )
							self.parent.assertEqual(
							    result.reconstruction.shape  ,		(self.batch_size, self.num_channels, self.image_size, self.image_size) )

							# test greyscale images
							UpperCamelCase_:							int		=  1
							UpperCamelCase_:							List[str]		=  ViTForMaskedImageModeling(snake_case_ )
							model.to(snake_case_ )
							model.eval()

							UpperCamelCase_:							Union[str, Any]		=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
							UpperCamelCase_:							Dict		=  model(snake_case_ )
							self.parent.assertEqual(result.reconstruction.shape  ,		(self.batch_size, 1, self.image_size, self.image_size) )

			def  lowerCAmelCase__		(    self	:    Optional[Any]  ,		snake_case_	:    Dict  ,		snake_case_	:    Optional[Any]  ,		snake_case_	:    List[str] ):
							UpperCamelCase_:							int		=  self.type_sequence_label_size
							UpperCamelCase_:							str		=  ViTForImageClassification(snake_case_ )
							model.to(snake_case_ )
							model.eval()
							UpperCamelCase_:							int		=  model(snake_case_  ,		labels=snake_case_ )
							self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.type_sequence_label_size) )

							# test greyscale images
							UpperCamelCase_:							Any		=  1
							UpperCamelCase_:							List[Any]		=  ViTForImageClassification(snake_case_ )
							model.to(snake_case_ )
							model.eval()

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




			def  lowerCAmelCase__		(    self	:    Any ):
							UpperCamelCase_:							Union[str, Any]		=  self.prepare_config_and_inputs()
							(
							    (
							    UpperCamelCase_
							),				(
							    UpperCamelCase_
							),				(
							    UpperCamelCase_
							),				
							):							List[str]		=  config_and_inputs
							UpperCamelCase_:							Tuple		=  {"""pixel_values""": pixel_values}
							return config, inputs_dict




@require_torch
class     _UpperCamelCase			(   _A       ,    _A       ,    unittest.TestCase					):






			'''simple docstring'''

			__UpperCamelCase :  List[str]    =	(
			    (
			        ViTModel,
			        ViTForImageClassification,
			        ViTForMaskedImageModeling,
			    )
			    if is_torch_available()
			    else ()
			)
			__UpperCamelCase :  str    =	(
			    {"""feature-extraction""": ViTModel, """image-classification""": ViTForImageClassification}
			    if is_torch_available()
			    else {}
			)
			__UpperCamelCase :  List[Any]    =	True

			__UpperCamelCase :  Union[str, Any]    =	False
			__UpperCamelCase :  Optional[int]    =	False
			__UpperCamelCase :  List[str]    =	False

			def  lowerCAmelCase__		(    self	:    Tuple ):
							UpperCamelCase_:							int		=  ViTModelTester(self )
							UpperCamelCase_:							Optional[int]		=  ConfigTester(self  ,		config_class=snake_case_  ,		has_text_modality=snake_case_  ,		hidden_size=37 )

			def  lowerCAmelCase__		(    self	:    str ):
							self.config_tester.run_common_tests()

			@unittest.skip(reason="""ViT does not use inputs_embeds""" )
			def  lowerCAmelCase__		(    self	:    Dict ):
							pass

			def  lowerCAmelCase__		(    self	:    Union[str, Any] ):
							UpperCamelCase_,				UpperCamelCase_:							Optional[Any]		=  self.model_tester.prepare_config_and_inputs_for_common()

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

			def  lowerCAmelCase__		(    self	:    Dict ):
							UpperCamelCase_,				UpperCamelCase_:							Union[str, Any]		=  self.model_tester.prepare_config_and_inputs_for_common()

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

											UpperCamelCase_:							int		=  ["""pixel_values"""]
											self.assertListEqual(arg_names[:1]  ,		snake_case_ )

			def  lowerCAmelCase__		(    self	:    int ):
							UpperCamelCase_:							Optional[int]		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							UpperCamelCase_:							Dict		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    List[Any] ):
							UpperCamelCase_:							Tuple		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*snake_case_ )




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







def     A__   (  )					->		List[Any]:
				UpperCamelCase_:							Any		=  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
				return image





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






			'''simple docstring'''

			@cached_property
			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224""" ) if is_vision_available() else None

			@slow
			def  lowerCAmelCase__		(    self	:    Optional[int] ):
							UpperCamelCase_:							Any		=  ViTForImageClassification.from_pretrained("""google/vit-base-patch16-224""" ).to(snake_case_ )

							UpperCamelCase_:							Optional[int]		=  self.default_image_processor
							UpperCamelCase_:							Tuple		=  prepare_img()
							UpperCamelCase_:							Dict		=  image_processor(images=snake_case_  ,		return_tensors="""pt""" ).to(snake_case_ )

							# forward pass
							with torch.no_grad():
											UpperCamelCase_:							Union[str, Any]		=  model(**snake_case_ )

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

							UpperCamelCase_:							int		=  torch.tensor([-0.2744, 0.8215, -0.0836] ).to(snake_case_ )

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

			@slow
			def  lowerCAmelCase__		(    self	:    Optional[int] ):
							# 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.
							UpperCamelCase_:							List[Any]		=  ViTModel.from_pretrained("""facebook/dino-vits8""" ).to(snake_case_ )

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

							# forward pass
							with torch.no_grad():
											UpperCamelCase_:							List[Any]		=  model(snake_case_  ,		interpolate_pos_encoding=snake_case_ )

							# verify the logits
							UpperCamelCase_:							Optional[Any]		=  torch.Size((1, 3601, 384) )
							self.assertEqual(outputs.last_hidden_state.shape  ,		snake_case_ )

							UpperCamelCase_:							Dict		=  torch.tensor(
							    [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]] ).to(snake_case_ )

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




			@slow
			@require_accelerate
			@require_torch_gpu
			def  lowerCAmelCase__		(    self	:    Optional[int] ):
							UpperCamelCase_:							Dict		=  ViTModel.from_pretrained("""facebook/dino-vits8"""  ,		torch_dtype=torch.floataa  ,		device_map="""auto""" )
							UpperCamelCase_:							str		=  self.default_image_processor

							UpperCamelCase_:							List[Any]		=  prepare_img()
							UpperCamelCase_:							List[str]		=  image_processor(images=snake_case_  ,		return_tensors="""pt""" )
							UpperCamelCase_:							Tuple		=  inputs.pixel_values.to(snake_case_ )

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








from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase_  :	Optional[int] 				=					logging.get_logger(__name__)

lowerCamelCase_  :	Dict 				=					{
    """studio-ousia/luke-base""": """https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json""",
    """studio-ousia/luke-large""": """https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json""",
}

class     _UpperCamelCase			(   _A					):






			'''simple docstring'''

			__UpperCamelCase :  Optional[int]    =	"""luke"""

			def __init__(    self	:    Tuple  ,		snake_case_	:    List[Any]=5_0267  ,		snake_case_	:    Any=50_0000  ,		snake_case_	:    str=768  ,		snake_case_	:    int=256  ,		snake_case_	:    str=12  ,		snake_case_	:    int=12  ,		snake_case_	:    Dict=3072  ,		snake_case_	:    Optional[Any]="gelu"  ,		snake_case_	:    Dict=0.1  ,		snake_case_	:    List[str]=0.1  ,		snake_case_	:    int=512  ,		snake_case_	:    Dict=2  ,		snake_case_	:    List[Any]=0.02  ,		snake_case_	:    int=1e-12  ,		snake_case_	:    Union[str, Any]=True  ,		snake_case_	:    Union[str, Any]=None  ,		snake_case_	:    Dict=1  ,		snake_case_	:    Optional[int]=0  ,		snake_case_	:    List[str]=2  ,		**snake_case_	:    Union[str, Any]  ,		):
							super().__init__(pad_token_id=snake_case_  ,		bos_token_id=snake_case_  ,		eos_token_id=snake_case_  ,		**snake_case_ )

							UpperCamelCase_:							Dict		=  vocab_size
							UpperCamelCase_:							Tuple		=  entity_vocab_size
							UpperCamelCase_:							Optional[int]		=  hidden_size
							UpperCamelCase_:							Any		=  entity_emb_size
							UpperCamelCase_:							str		=  num_hidden_layers
							UpperCamelCase_:							Union[str, Any]		=  num_attention_heads
							UpperCamelCase_:							Dict		=  hidden_act
							UpperCamelCase_:							Dict		=  intermediate_size
							UpperCamelCase_:							str		=  hidden_dropout_prob
							UpperCamelCase_:							List[str]		=  attention_probs_dropout_prob
							UpperCamelCase_:							int		=  max_position_embeddings
							UpperCamelCase_:							int		=  type_vocab_size
							UpperCamelCase_:							List[Any]		=  initializer_range
							UpperCamelCase_:							Union[str, Any]		=  layer_norm_eps
							UpperCamelCase_:							Tuple		=  use_entity_aware_attention
							UpperCamelCase_:							int		=  classifier_dropout






