


import inspect
import unittest

from transformers import ViTHybridConfig
from transformers.testing_utils import require_accelerate, require_torch, 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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch
		from torch import nn

		from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel
		from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image





class  _a							:




	def __init__( self						:		List[Any]		, _SCREAMING_SNAKE_CASE						:		List[Any]		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]=13		, _SCREAMING_SNAKE_CASE						:		List[str]=64		, _SCREAMING_SNAKE_CASE						:		int=2		, _SCREAMING_SNAKE_CASE						:		List[str]=3		, _SCREAMING_SNAKE_CASE						:		Optional[Any]=True		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]=True		, _SCREAMING_SNAKE_CASE						:		List[str]=32		, _SCREAMING_SNAKE_CASE						:		List[Any]=5		, _SCREAMING_SNAKE_CASE						:		Optional[Any]=4		, _SCREAMING_SNAKE_CASE						:		Optional[Any]=37		, _SCREAMING_SNAKE_CASE						:		Optional[int]="gelu"		, _SCREAMING_SNAKE_CASE						:		List[Any]=0.1		, _SCREAMING_SNAKE_CASE						:		Optional[int]=0.1		, _SCREAMING_SNAKE_CASE						:		str=10		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]=0.02		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]=[1, 16, 4, 4]		, _SCREAMING_SNAKE_CASE						:		Optional[Any]=None		, )->	Any:
			lowerCAmelCase__       :      int   					= parent
			lowerCAmelCase__       :      str   					= batch_size
			lowerCAmelCase__       :      List[str]   					= image_size
			lowerCAmelCase__       :      List[Any]   					= patch_size
			lowerCAmelCase__       :      str   					= num_channels
			lowerCAmelCase__       :      Any   					= is_training
			lowerCAmelCase__       :      Dict   					= use_labels
			lowerCAmelCase__       :      Dict   					= hidden_size
			lowerCAmelCase__       :      List[Any]   					= num_hidden_layers
			lowerCAmelCase__       :      Tuple   					= num_attention_heads
			lowerCAmelCase__       :      Union[str, Any]   					= intermediate_size
			lowerCAmelCase__       :      Union[str, Any]   					= hidden_act
			lowerCAmelCase__       :      str   					= hidden_dropout_prob
			lowerCAmelCase__       :      Optional[int]   					= attention_probs_dropout_prob
			lowerCAmelCase__       :      Any   					= type_sequence_label_size
			lowerCAmelCase__       :      Union[str, Any]   					= initializer_range
			lowerCAmelCase__       :      Dict   					= scope
			lowerCAmelCase__       :      int   					= backbone_featmap_shape

			# in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
			# the number of patches is based on the feature map of the backbone, which by default uses an output stride
			# of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size
			lowerCAmelCase__       :      Optional[int]   					= (self.image_size // 32) ** 2
			lowerCAmelCase__       :      Any   					= num_patches + 1




	def 							UpperCAmelCase__( self						:		Optional[Any]		)->	List[Any]:
			lowerCAmelCase__       :      Tuple   					= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

			lowerCAmelCase__       :      Tuple   					= self.get_config()

			return config, pixel_values, labels




	def 							UpperCAmelCase__( self						:		Tuple		)->	Union[str, Any]:
			lowerCAmelCase__       :      Any   					= {
			    '''global_padding''': '''same''',
			    '''layer_type''': '''bottleneck''',
			    '''depths''': [3, 4, 9],
			    '''out_features''': ['''stage1''', '''stage2''', '''stage3'''],
			    '''embedding_dynamic_padding''': True,
			    '''hidden_sizes''': [4, 8, 16, 32],
			    '''num_groups''': 2,
			}

			return ViTHybridConfig(
			    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		, backbone_featmap_shape=self.backbone_featmap_shape		, backbone_config=_SCREAMING_SNAKE_CASE		, )




	def 							UpperCAmelCase__( self						:		List[Any]		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]		, _SCREAMING_SNAKE_CASE						:		Tuple		, _SCREAMING_SNAKE_CASE						:		Optional[int]		)->	Optional[Any]:
			lowerCAmelCase__       :      Any   					= ViTHybridModel(config=_SCREAMING_SNAKE_CASE		)
			model.to(_SCREAMING_SNAKE_CASE		)
			model.eval()
			lowerCAmelCase__       :      List[str]   					= model(_SCREAMING_SNAKE_CASE		)
			self.parent.assertEqual(result.last_hidden_state.shape		, (self.batch_size, self.seq_length, self.hidden_size)		)




	def 							UpperCAmelCase__( self						:		int		, _SCREAMING_SNAKE_CASE						:		List[Any]		, _SCREAMING_SNAKE_CASE						:		Dict		, _SCREAMING_SNAKE_CASE						:		int		)->	Union[str, Any]:
			lowerCAmelCase__       :      Optional[Any]   					= self.type_sequence_label_size
			lowerCAmelCase__       :      Optional[Any]   					= ViTHybridForImageClassification(_SCREAMING_SNAKE_CASE		)
			model.to(_SCREAMING_SNAKE_CASE		)
			model.eval()
			lowerCAmelCase__       :      Optional[Any]   					= model(_SCREAMING_SNAKE_CASE		, labels=_SCREAMING_SNAKE_CASE		)
			self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.type_sequence_label_size)		)




	def 							UpperCAmelCase__( self						:		List[Any]		)->	Tuple:
			lowerCAmelCase__       :      Union[str, Any]   					= self.prepare_config_and_inputs()
			lowerCAmelCase__       :      Optional[Any]   					= config_and_inputs
			lowerCAmelCase__       :      Any   					= {'''pixel_values''': pixel_values}
			return config, inputs_dict





@require_torch
class  _a							(    _lowercase					,						_lowercase					,						unittest.TestCase):
	_a  :  Dict				   =							(ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
	_a  :  Tuple				   =							(
	    {'''feature-extraction''': ViTHybridModel, '''image-classification''': ViTHybridForImageClassification}
	    if is_torch_available()
	    else {}
	)
	_a  :  Dict				   =							False
	_a  :  Optional[int]				   =							False
	_a  :  Optional[int]				   =							False




	def 							UpperCAmelCase__( self						:		Dict		)->	Any:
			lowerCAmelCase__       :      Optional[Any]   					= ViTHybridModelTester(self		)
			lowerCAmelCase__       :      List[str]   					= ConfigTester(self		, config_class=_SCREAMING_SNAKE_CASE		, has_text_modality=_SCREAMING_SNAKE_CASE		, hidden_size=37		)




	def 							UpperCAmelCase__( self						:		Optional[int]		)->	List[Any]:
			self.config_tester.run_common_tests()




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




	def 							UpperCAmelCase__( self						:		Optional[int]		)->	int:
			lowerCAmelCase__       :      Tuple   					= self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
					lowerCAmelCase__       :      List[str]   					= model_class(_SCREAMING_SNAKE_CASE		)
					self.assertIsInstance(model.get_input_embeddings()		, (nn.Module)		)
					lowerCAmelCase__       :      Optional[int]   					= model.get_output_embeddings()
					self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE		, nn.Linear		)		)




	def 							UpperCAmelCase__( self						:		int		)->	Optional[int]:
			lowerCAmelCase__       :      Dict   					= self.model_tester.prepare_config_and_inputs_for_common()

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

					lowerCAmelCase__       :      List[Any]   					= ['''pixel_values''']
					self.assertListEqual(arg_names[:1]		, _SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		Optional[Any]		)->	Tuple:
			lowerCAmelCase__       :      Optional[Any]   					= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		Dict		)->	int:
			lowerCAmelCase__       :      List[str]   					= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		Tuple		)->	Optional[int]:
			lowerCAmelCase__       :      List[str]   					= self.model_tester.prepare_config_and_inputs_for_common()

			lowerCAmelCase__       :      Dict   					= _config_zero_init(_SCREAMING_SNAKE_CASE		)
			for model_class in self.all_model_classes:
					lowerCAmelCase__       :      Dict   					= model_class(config=_SCREAMING_SNAKE_CASE		)
					# Skip the check for the backbone
					for name, module in model.named_modules():
							if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
									lowerCAmelCase__       :      str   					= [F'{name}.{key}' for key in module.state_dict().keys()]
									break

					for name, param in model.named_parameters():
							if param.requires_grad:
									if name in backbone_params:
											continue
									self.assertIn(
									    ((param.data.mean() * 1E9).round() / 1E9).item()		, [0.0, 1.0]		, msg=F'Parameter {name} of model {model_class} seems not properly initialized'		, )




	@slow
	def 							UpperCAmelCase__( self						:		List[str]		)->	str:
			for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowerCAmelCase__       :      Tuple   					= ViTHybridModel.from_pretrained(_SCREAMING_SNAKE_CASE		)
					self.assertIsNotNone(_SCREAMING_SNAKE_CASE		)






def 					lowerCamelCase_   (	):




		"""simple docstring"""






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





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




	@cached_property
	def 							UpperCAmelCase__( self						:		Union[str, Any]		)->	Dict:
			return (
			    ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)
			    if is_vision_available()
			    else None
			)




	@slow
	def 							UpperCAmelCase__( self						:		List[str]		)->	Any:
			lowerCAmelCase__       :      List[Any]   					= ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]		).to(
			    _SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      Tuple   					= self.default_image_processor
			lowerCAmelCase__       :      Optional[int]   					= prepare_img()
			lowerCAmelCase__       :      Union[str, Any]   					= image_processor(images=_SCREAMING_SNAKE_CASE		, return_tensors='''pt'''		).to(_SCREAMING_SNAKE_CASE		)

			# forward pass
			with torch.no_grad():
					lowerCAmelCase__       :      int   					= model(**_SCREAMING_SNAKE_CASE		)

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

			lowerCAmelCase__       :      Tuple   					= torch.tensor([-1.9090, -0.4993, -0.2389]		).to(_SCREAMING_SNAKE_CASE		)

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




	@slow
	@require_accelerate
	def 							UpperCAmelCase__( self						:		str		)->	Tuple:
			lowerCAmelCase__       :      Optional[Any]   					= ViTHybridImageProcessor.from_pretrained('''google/vit-hybrid-base-bit-384'''		)
			lowerCAmelCase__       :      List[str]   					= ViTHybridForImageClassification.from_pretrained('''google/vit-hybrid-base-bit-384'''		, device_map='''auto'''		)

			lowerCAmelCase__       :      Union[str, Any]   					= prepare_img()

			lowerCAmelCase__       :      List[str]   					= image_processor(images=_SCREAMING_SNAKE_CASE		, return_tensors='''pt'''		)
			lowerCAmelCase__       :      str   					= model(**_SCREAMING_SNAKE_CASE		)
			lowerCAmelCase__       :      List[Any]   					= outputs.logits
			# model predicts one of the 1000 ImageNet classes
			lowerCAmelCase__       :      Union[str, Any]   					= logits.argmax(-1		).item()

			self.assertTrue(model.config.idalabel[predicted_class_idx]		, '''tabby, tabby cat'''		)




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


lowerCamelCase						=		logging.get_logger(__name__)

lowerCamelCase						=		{
    '''facebook/dpr-ctx_encoder-single-nq-base''': (
        '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-question_encoder-single-nq-base''': (
        '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-reader-single-nq-base''': (
        '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-ctx_encoder-multiset-base''': (
        '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-question_encoder-multiset-base''': (
        '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-reader-multiset-base''': (
        '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json'''
    ),
}





class  _a							(    _lowercase):
	_a  :  List[Any]				   =							'''dpr'''




	def __init__( self						:		List[str]		, _SCREAMING_SNAKE_CASE						:		List[str]=3_0522		, _SCREAMING_SNAKE_CASE						:		Optional[int]=768		, _SCREAMING_SNAKE_CASE						:		List[str]=12		, _SCREAMING_SNAKE_CASE						:		Tuple=12		, _SCREAMING_SNAKE_CASE						:		str=3072		, _SCREAMING_SNAKE_CASE						:		Optional[Any]="gelu"		, _SCREAMING_SNAKE_CASE						:		Dict=0.1		, _SCREAMING_SNAKE_CASE						:		Optional[int]=0.1		, _SCREAMING_SNAKE_CASE						:		List[str]=512		, _SCREAMING_SNAKE_CASE						:		int=2		, _SCREAMING_SNAKE_CASE						:		Optional[Any]=0.02		, _SCREAMING_SNAKE_CASE						:		Tuple=1E-12		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]=0		, _SCREAMING_SNAKE_CASE						:		List[str]="absolute"		, _SCREAMING_SNAKE_CASE						:		int = 0		, **_SCREAMING_SNAKE_CASE						:		Optional[Any]		, )->	Optional[int]:
			super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE		, **_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      Union[str, Any]   					= vocab_size
			lowerCAmelCase__       :      Tuple   					= hidden_size
			lowerCAmelCase__       :      Union[str, Any]   					= num_hidden_layers
			lowerCAmelCase__       :      List[Any]   					= num_attention_heads
			lowerCAmelCase__       :      Optional[int]   					= hidden_act
			lowerCAmelCase__       :      Optional[Any]   					= intermediate_size
			lowerCAmelCase__       :      Optional[Any]   					= hidden_dropout_prob
			lowerCAmelCase__       :      int   					= attention_probs_dropout_prob
			lowerCAmelCase__       :      str   					= max_position_embeddings
			lowerCAmelCase__       :      List[Any]   					= type_vocab_size
			lowerCAmelCase__       :      Optional[int]   					= initializer_range
			lowerCAmelCase__       :      List[str]   					= layer_norm_eps
			lowerCAmelCase__       :      Dict   					= projection_dim
			lowerCAmelCase__       :      int   					= position_embedding_type




