



import tempfile
import unittest

from transformers import DPRConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import DPRContextEncoder, DPRQuestionEncoder, DPRReader, DPRReaderTokenizer
			from transformers.models.dpr.modeling_dpr import (
			    DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
			    DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
			    DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
			)

class 			_a     :



			def __init__( self       :							Optional[Any]      ,  _lowercase       :							Dict      ,  _lowercase       :							int=13      ,  _lowercase       :							Tuple=7      ,  _lowercase       :							str=False      ,  _lowercase       :							str=True      ,  _lowercase       :							Optional[Any]=True      ,  _lowercase       :							List[Any]=True      ,  _lowercase       :							int=99      ,  _lowercase       :							Optional[Any]=32      ,  _lowercase       :							Optional[int]=5      ,  _lowercase       :							int=4      ,  _lowercase       :							str=37      ,  _lowercase       :							Dict="gelu"      ,  _lowercase       :							str=0.1      ,  _lowercase       :							List[Any]=0.1      ,  _lowercase       :							Dict=512      ,  _lowercase       :							str=16      ,  _lowercase       :							str=2      ,  _lowercase       :							Union[str, Any]=0.02      ,  _lowercase       :							str=3      ,  _lowercase       :							str=4      ,  _lowercase       :							Optional[int]=None      ,  _lowercase       :							int=0      ,  )       ->				int:
									snake_case      : str							=   parent
									snake_case      : int							=   batch_size
									snake_case      : List[Any]							=   seq_length
									snake_case      : List[Any]							=   is_training
									snake_case      : Any							=   use_input_mask
									snake_case      : int							=   use_token_type_ids
									snake_case      : Tuple							=   use_labels
									snake_case      : List[Any]							=   vocab_size
									snake_case      : List[Any]							=   hidden_size
									snake_case      : Any							=   num_hidden_layers
									snake_case      : List[Any]							=   num_attention_heads
									snake_case      : Tuple							=   intermediate_size
									snake_case      : Optional[int]							=   hidden_act
									snake_case      : Tuple							=   hidden_dropout_prob
									snake_case      : Dict							=   attention_probs_dropout_prob
									snake_case      : Optional[Any]							=   max_position_embeddings
									snake_case      : List[Any]							=   type_vocab_size
									snake_case      : int							=   type_sequence_label_size
									snake_case      : Optional[int]							=   initializer_range
									snake_case      : str							=   num_labels
									snake_case      : Union[str, Any]							=   num_choices
									snake_case      : Optional[Any]							=   scope
									snake_case      : Any							=   projection_dim



			def 							__lowercase				( self       :							List[str]		)       ->				Union[str, Any]:
									snake_case      : Optional[Any]							=   ids_tensor([self.batch_size, self.seq_length]      ,  self.vocab_size		)

									snake_case      : List[Any]							=   None
									if self.use_input_mask:
															snake_case      : Optional[Any]							=   random_attention_mask([self.batch_size, self.seq_length]		)

									snake_case      : Tuple							=   None
									if self.use_token_type_ids:
															snake_case      : List[Any]							=   ids_tensor([self.batch_size, self.seq_length]      ,  self.type_vocab_size		)

									snake_case      : Union[str, Any]							=   None
									snake_case      : Optional[Any]							=   None
									snake_case      : Tuple							=   None
									if self.use_labels:
															snake_case      : str							=   ids_tensor([self.batch_size]      ,  self.type_sequence_label_size		)
															snake_case      : List[Any]							=   ids_tensor([self.batch_size, self.seq_length]      ,  self.num_labels		)
															snake_case      : List[Any]							=   ids_tensor([self.batch_size]      ,  self.num_choices		)

									snake_case      : int							=   self.get_config()

									return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



			def 							__lowercase				( self       :							str		)       ->				str:
									return DPRConfig(
									    projection_dim=self.projection_dim      ,  vocab_size=self.vocab_size      ,  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      ,  max_position_embeddings=self.max_position_embeddings      ,  type_vocab_size=self.type_vocab_size      ,  initializer_range=self.initializer_range      ,  )



			def 							__lowercase				( self       :							Optional[Any]      ,  _lowercase       :							Tuple      ,  _lowercase       :							Optional[Any]      ,  _lowercase       :							Optional[int]      ,  _lowercase       :							Any      ,  _lowercase       :							Optional[int]      ,  _lowercase       :							Dict      ,  _lowercase       :							List[str]		)       ->				Dict:
									snake_case      : Dict							=   DPRContextEncoder(config=_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case      : List[str]							=   model(_lowercase      ,  attention_mask=_lowercase      ,  token_type_ids=_lowercase		)
									snake_case      : Any							=   model(_lowercase      ,  token_type_ids=_lowercase		)
									snake_case      : Union[str, Any]							=   model(_lowercase		)
									self.parent.assertEqual(result.pooler_output.shape      ,  (self.batch_size, self.projection_dim or self.hidden_size)		)



			def 							__lowercase				( self       :							int      ,  _lowercase       :							int      ,  _lowercase       :							Union[str, Any]      ,  _lowercase       :							Any      ,  _lowercase       :							Dict      ,  _lowercase       :							Tuple      ,  _lowercase       :							str      ,  _lowercase       :							Tuple		)       ->				str:
									snake_case      : Optional[int]							=   DPRQuestionEncoder(config=_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case      : Optional[Any]							=   model(_lowercase      ,  attention_mask=_lowercase      ,  token_type_ids=_lowercase		)
									snake_case      : int							=   model(_lowercase      ,  token_type_ids=_lowercase		)
									snake_case      : Optional[Any]							=   model(_lowercase		)
									self.parent.assertEqual(result.pooler_output.shape      ,  (self.batch_size, self.projection_dim or self.hidden_size)		)



			def 							__lowercase				( self       :							int      ,  _lowercase       :							Optional[Any]      ,  _lowercase       :							int      ,  _lowercase       :							Optional[Any]      ,  _lowercase       :							List[Any]      ,  _lowercase       :							Tuple      ,  _lowercase       :							List[str]      ,  _lowercase       :							Tuple		)       ->				Union[str, Any]:
									snake_case      : str							=   DPRReader(config=_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case      : str							=   model(
									    _lowercase      ,  attention_mask=_lowercase      ,  )

									self.parent.assertEqual(result.start_logits.shape      ,  (self.batch_size, self.seq_length)		)
									self.parent.assertEqual(result.end_logits.shape      ,  (self.batch_size, self.seq_length)		)
									self.parent.assertEqual(result.relevance_logits.shape      ,  (self.batch_size,)		)



			def 							__lowercase				( self       :							List[str]		)       ->				Any:
									snake_case      : int							=   self.prepare_config_and_inputs()
									(
									    (
									    snake_case       
									)						, (
									    snake_case       
									)						, (
									    snake_case       
									)						, (
									    snake_case       
									)						, (
									    snake_case       
									)						, (
									    snake_case       
									)						, (
									    snake_case       
									)						,        
									)      : List[Any]							=   config_and_inputs
									snake_case      : Tuple							=   {"input_ids": input_ids}
									return config, inputs_dict



@require_torch
class 			_a     (   __lowerCamelCase    ,  __lowerCamelCase    ,  unittest.TestCase):
			__magic_name__    				=    (
			    (
			        DPRContextEncoder,
			        DPRQuestionEncoder,
			        DPRReader,
			    )
			    if is_torch_available()
			    else ()
			)
			__magic_name__    				=    {'feature-extraction': DPRQuestionEncoder} if is_torch_available() else {}

			__magic_name__    				=    False
			__magic_name__    				=    False  # why?
			__magic_name__    				=    False
			__magic_name__    				=    False



			def 							__lowercase				( self       :							List[str]		)       ->				Any:
									snake_case      : Tuple							=   DPRModelTester(self		)
									snake_case      : int							=   ConfigTester(self      ,  config_class=_lowercase      ,  hidden_size=37		)



			def 							__lowercase				( self       :							int		)       ->				str:
									self.config_tester.run_common_tests()



			def 							__lowercase				( self       :							Optional[int]		)       ->				str:
									snake_case      : Optional[int]							=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_context_encoder(*_lowercase		)



			def 							__lowercase				( self       :							int		)       ->				Union[str, Any]:
									snake_case      : List[str]							=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_question_encoder(*_lowercase		)



			def 							__lowercase				( self       :							Optional[Any]		)       ->				Optional[int]:
									snake_case      : Tuple							=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_reader(*_lowercase		)



			def 							__lowercase				( self       :							Optional[Any]		)       ->				List[str]:
									snake_case      : str							=   self.model_tester.prepare_config_and_inputs()[0]

									snake_case      : int							=   DPRQuestionEncoder(config=_lowercase		)
									model.to(_lowercase		)
									model.eval()

									with tempfile.TemporaryDirectory() as tmp_dirname:
															model.save_pretrained(_lowercase		)
															snake_case      : List[Any]							=   DPRQuestionEncoder.from_pretrained(_lowercase      ,  projection_dim=512		)

									self.assertIsNotNone(_lowercase		)



			@slow
			def 							__lowercase				( self       :							Tuple		)       ->				Tuple:






									for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case      : List[Any]							=   DPRContextEncoder.from_pretrained(_lowercase		)
															self.assertIsNotNone(_lowercase		)

									for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case      : Optional[int]							=   DPRContextEncoder.from_pretrained(_lowercase		)
															self.assertIsNotNone(_lowercase		)

									for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case      : Dict							=   DPRQuestionEncoder.from_pretrained(_lowercase		)
															self.assertIsNotNone(_lowercase		)

									for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case      : Union[str, Any]							=   DPRReader.from_pretrained(_lowercase		)
															self.assertIsNotNone(_lowercase		)



@require_torch
class 			_a     (   unittest.TestCase):



			@slow
			def 							__lowercase				( self       :							Optional[Any]		)       ->				Optional[Any]:
									snake_case      : Optional[int]							=   DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"      ,  return_dict=_lowercase		)
									model.to(_lowercase		)

									snake_case      : Optional[int]							=   torch.tensor(
									    [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]      ,  dtype=torch.long      ,  device=_lowercase		)  # [CLS] hello, is my dog cute? [SEP]
									snake_case      : int							=   model(_lowercase		)[0]  # embedding shape = (1, 768)
									# compare the actual values for a slice.
									snake_case      : Optional[int]							=   torch.tensor(
									    [
									        [
									            0.03236253,
									            0.12753335,
									            0.16818509,
									            0.00279786,
									            0.3896933,
									            0.24264945,
									            0.2178971,
									            -0.02335227,
									            -0.08481959,
									            -0.14324117,
									        ]
									    ]      ,  dtype=torch.float      ,  device=_lowercase      ,  )
									self.assertTrue(torch.allclose(output[:, :10]      ,  _lowercase      ,  atol=1E-4		)		)



			@slow
			def 							__lowercase				( self       :							Dict		)       ->				Union[str, Any]:
									snake_case      : List[str]							=   DPRReaderTokenizer.from_pretrained("facebook/dpr-reader-single-nq-base"		)
									snake_case      : List[str]							=   DPRReader.from_pretrained("facebook/dpr-reader-single-nq-base"		)
									model.to(_lowercase		)

									snake_case      : List[Any]							=   tokenizer(
									    questions="What is love ?"      ,  titles="Haddaway"      ,  texts="What Is Love is a song recorded by the artist Haddaway"      ,  padding=_lowercase      ,  return_tensors="pt"      ,  )
									encoded_inputs.to(_lowercase		)

									snake_case      : Any							=   model(**_lowercase		)

									# compare the actual values for a slice.
									snake_case      : List[str]							=   torch.tensor(
									    [[-10.3005, -10.7765, -11.4872, -11.6841, -11.9312, -10.3002, -9.8544, -11.7378, -12.0821, -10.2975]]      ,  dtype=torch.float      ,  device=_lowercase      ,  )

									snake_case      : int							=   torch.tensor(
									    [[-11.0684, -11.7041, -11.5397, -10.3465, -10.8791, -6.8443, -11.9959, -11.0364, -10.0096, -6.8405]]      ,  dtype=torch.float      ,  device=_lowercase      ,  )
									self.assertTrue(torch.allclose(outputs.start_logits[:, :10]      ,  _lowercase      ,  atol=1E-4		)		)
									self.assertTrue(torch.allclose(outputs.end_logits[:, :10]      ,  _lowercase      ,  atol=1E-4		)		)








"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
    is_vision_available,
)


A    =	{
    'configuration_mobilevitv2': [
        'MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'MobileViTV2Config',
        'MobileViTV2OnnxConfig',
    ],
}


try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			A    =	[
			    'MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST',
			    'MobileViTV2ForImageClassification',
			    'MobileViTV2ForSemanticSegmentation',
			    'MobileViTV2Model',
			    'MobileViTV2PreTrainedModel',
			]

if TYPE_CHECKING:
			from .configuration_mobilevitva import (
			    MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP,
			    MobileViTVaConfig,
			    MobileViTVaOnnxConfig,
			)

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_mobilevitva import (
						    MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST,
						    MobileViTVaForImageClassification,
						    MobileViTVaForSemanticSegmentation,
						    MobileViTVaModel,
						    MobileViTVaPreTrainedModel,
						)

else:
			import sys

			A    =	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



