

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							, _A							, _A=1_3							, _A=7							, _A=False							, _A=True							, _A=True							, _A=True							, _A=9_9							, _A=3_2							, _A=5							, _A=4							, _A=3_7							, _A="gelu"							, _A=0.1							, _A=0.1							, _A=5_1_2							, _A=1_6							, _A=2							, _A=0.02							, _A=3							, _A=4							, _A=None							, _A=0							, ):






				'''simple docstring'''







				UpperCAmelCase       		=    parent
				UpperCAmelCase       		=    batch_size
				UpperCAmelCase       		=    seq_length
				UpperCAmelCase       		=    is_training
				UpperCAmelCase       		=    use_input_mask
				UpperCAmelCase       		=    use_token_type_ids
				UpperCAmelCase       		=    use_labels
				UpperCAmelCase       		=    vocab_size
				UpperCAmelCase       		=    hidden_size
				UpperCAmelCase       		=    num_hidden_layers
				UpperCAmelCase       		=    num_attention_heads
				UpperCAmelCase       		=    intermediate_size
				UpperCAmelCase       		=    hidden_act
				UpperCAmelCase       		=    hidden_dropout_prob
				UpperCAmelCase       		=    attention_probs_dropout_prob
				UpperCAmelCase       		=    max_position_embeddings
				UpperCAmelCase       		=    type_vocab_size
				UpperCAmelCase       		=    type_sequence_label_size
				UpperCAmelCase       		=    initializer_range
				UpperCAmelCase       		=    num_labels
				UpperCAmelCase       		=    num_choices
				UpperCAmelCase       		=    scope
				UpperCAmelCase       		=    projection_dim
	def 					_lowercase    (   self		):






				'''simple docstring'''







				UpperCAmelCase       		=    ids_tensor([self.batch_size, self.seq_length]							, self.vocab_size		)

				UpperCAmelCase       		=    None
				if self.use_input_mask:
							UpperCAmelCase       		=    random_attention_mask([self.batch_size, self.seq_length]		)

				UpperCAmelCase       		=    None
				if self.use_token_type_ids:
							UpperCAmelCase       		=    ids_tensor([self.batch_size, self.seq_length]							, self.type_vocab_size		)

				UpperCAmelCase       		=    None
				UpperCAmelCase       		=    None
				UpperCAmelCase       		=    None
				if self.use_labels:
							UpperCAmelCase       		=    ids_tensor([self.batch_size]							, self.type_sequence_label_size		)
							UpperCAmelCase       		=    ids_tensor([self.batch_size, self.seq_length]							, self.num_labels		)
							UpperCAmelCase       		=    ids_tensor([self.batch_size]							, self.num_choices		)

				UpperCAmelCase       		=    self.get_config()

				return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
	def 					_lowercase    (   self		):






				'''simple docstring'''







				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							, _A							, _A							, _A							, _A							, _A							, _A							, _A		):






				'''simple docstring'''







				UpperCAmelCase       		=    DPRContextEncoder(config=lowerCAmelCase__		)
				model.to(lowerCAmelCase__		)
				model.eval()
				UpperCAmelCase       		=    model(lowerCAmelCase__							, attention_mask=lowerCAmelCase__							, token_type_ids=lowerCAmelCase__		)
				UpperCAmelCase       		=    model(lowerCAmelCase__							, token_type_ids=lowerCAmelCase__		)
				UpperCAmelCase       		=    model(lowerCAmelCase__		)
				self.parent.assertEqual(result.pooler_output.shape							, (self.batch_size, self.projection_dim or self.hidden_size)		)
	def 					_lowercase    (   self							, _A							, _A							, _A							, _A							, _A							, _A							, _A		):






				'''simple docstring'''







				UpperCAmelCase       		=    DPRQuestionEncoder(config=lowerCAmelCase__		)
				model.to(lowerCAmelCase__		)
				model.eval()
				UpperCAmelCase       		=    model(lowerCAmelCase__							, attention_mask=lowerCAmelCase__							, token_type_ids=lowerCAmelCase__		)
				UpperCAmelCase       		=    model(lowerCAmelCase__							, token_type_ids=lowerCAmelCase__		)
				UpperCAmelCase       		=    model(lowerCAmelCase__		)
				self.parent.assertEqual(result.pooler_output.shape							, (self.batch_size, self.projection_dim or self.hidden_size)		)
	def 					_lowercase    (   self							, _A							, _A							, _A							, _A							, _A							, _A							, _A		):






				'''simple docstring'''







				UpperCAmelCase       		=    DPRReader(config=lowerCAmelCase__		)
				model.to(lowerCAmelCase__		)
				model.eval()
				UpperCAmelCase       		=    model(
				    lowerCAmelCase__							, attention_mask=lowerCAmelCase__							, )

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






				'''simple docstring'''







				UpperCAmelCase       		=    self.prepare_config_and_inputs()
				(
				    (
				    UpperCAmelCase 
				)		,      (
				    UpperCAmelCase 
				)		,      (
				    UpperCAmelCase 
				)		,      (
				    UpperCAmelCase 
				)		,      (
				    UpperCAmelCase 
				)		,      (
				    UpperCAmelCase 
				)		,      (
				    UpperCAmelCase 
				)		,       
				)       		=    config_and_inputs
				UpperCAmelCase       		=    {'''input_ids''': input_ids}
				return config, inputs_dict

@require_torch
class      A_	(lowercase__					,							lowercase__					,							unittest.TestCase      ):
	UpperCAmelCase__ =      (
	    (
	        DPRContextEncoder,
	        DPRQuestionEncoder,
	        DPRReader,
	    )
	    if is_torch_available()
	    else ()
	)
	UpperCAmelCase__ =      {'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

	UpperCAmelCase__ =      False
	UpperCAmelCase__ =      False  # why?
	UpperCAmelCase__ =      False
	UpperCAmelCase__ =      False
	def 					_lowercase    (   self		):






				'''simple docstring'''







				UpperCAmelCase       		=    DPRModelTester(self		)
				UpperCAmelCase       		=    ConfigTester(self							, config_class=lowerCAmelCase__							, hidden_size=3_7		)
	def 					_lowercase    (   self		):






				'''simple docstring'''







				self.config_tester.run_common_tests()
	def 					_lowercase    (   self		):






				'''simple docstring'''







				UpperCAmelCase       		=    self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_context_encoder(*lowerCAmelCase__		)
	def 					_lowercase    (   self		):






				'''simple docstring'''







				UpperCAmelCase       		=    self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_question_encoder(*lowerCAmelCase__		)
	def 					_lowercase    (   self		):






				'''simple docstring'''







				UpperCAmelCase       		=    self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_reader(*lowerCAmelCase__		)
	def 					_lowercase    (   self		):






				'''simple docstring'''







				UpperCAmelCase       		=    self.model_tester.prepare_config_and_inputs()[0]

				UpperCAmelCase       		=    DPRQuestionEncoder(config=lowerCAmelCase__		)
				model.to(lowerCAmelCase__		)
				model.eval()

				with tempfile.TemporaryDirectory() as tmp_dirname:
							model.save_pretrained(lowerCAmelCase__		)
							UpperCAmelCase       		=    DPRQuestionEncoder.from_pretrained(lowerCAmelCase__							, projection_dim=5_1_2		)

				self.assertIsNotNone(lowerCAmelCase__		)







	@slow
	def 					_lowercase    (   self		):






				'''simple docstring'''







				for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase       		=    DPRContextEncoder.from_pretrained(lowerCAmelCase__		)
							self.assertIsNotNone(lowerCAmelCase__		)

				for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase       		=    DPRContextEncoder.from_pretrained(lowerCAmelCase__		)
							self.assertIsNotNone(lowerCAmelCase__		)

				for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase       		=    DPRQuestionEncoder.from_pretrained(lowerCAmelCase__		)
							self.assertIsNotNone(lowerCAmelCase__		)

				for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase       		=    DPRReader.from_pretrained(lowerCAmelCase__		)
							self.assertIsNotNone(lowerCAmelCase__		)

@require_torch
class      A_	(unittest.TestCase      ):
	@slow
	def 					_lowercase    (   self		):






				'''simple docstring'''







				UpperCAmelCase       		=    DPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''							, return_dict=lowerCAmelCase__		)
				model.to(lowerCAmelCase__		)

				UpperCAmelCase       		=    torch.tensor(
				    [[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_0_3, 2_0_2_6, 3_8_9_9, 1_0_1_4_0, 1_0_2_9, 1_0_2]]							, dtype=torch.long							, device=lowerCAmelCase__		)  # [CLS] hello, is my dog cute? [SEP]
				UpperCAmelCase       		=    model(lowerCAmelCase__		)[0]  # embedding shape = (1, 768)
				# compare the actual values for a slice.
				UpperCAmelCase       		=    torch.tensor(
				    [
				        [
				            0.03_23_62_53,
				            0.12_75_33_35,
				            0.16_81_85_09,
				            0.00_27_97_86,
				            0.3_89_69_33,
				            0.24_26_49_45,
				            0.2_17_89_71,
				            -0.02_33_52_27,
				            -0.08_48_19_59,
				            -0.14_32_41_17,
				        ]
				    ]							, dtype=torch.float							, device=lowerCAmelCase__							, )
				self.assertTrue(torch.allclose(output[:, :1_0]							, lowerCAmelCase__							, atol=1E-4		)		)







	@slow
	def 					_lowercase    (   self		):






				'''simple docstring'''







				UpperCAmelCase       		=    DPRReaderTokenizer.from_pretrained('''facebook/dpr-reader-single-nq-base'''		)
				UpperCAmelCase       		=    DPRReader.from_pretrained('''facebook/dpr-reader-single-nq-base'''		)
				model.to(lowerCAmelCase__		)

				UpperCAmelCase       		=    tokenizer(
				    questions='''What is love ?'''							, titles='''Haddaway'''							, texts='''What Is Love is a song recorded by the artist Haddaway'''							, padding=lowerCAmelCase__							, return_tensors='''pt'''							, )
				encoded_inputs.to(lowerCAmelCase__		)

				UpperCAmelCase       		=    model(**lowerCAmelCase__		)

				# compare the actual values for a slice.
				UpperCAmelCase       		=    torch.tensor(
				    [[-10.30_05, -10.77_65, -11.48_72, -11.68_41, -11.93_12, -10.30_02, -9.85_44, -11.73_78, -12.08_21, -10.29_75]]							, dtype=torch.float							, device=lowerCAmelCase__							, )

				UpperCAmelCase       		=    torch.tensor(
				    [[-11.06_84, -11.70_41, -11.53_97, -10.34_65, -10.87_91, -6.84_43, -11.99_59, -11.03_64, -10.00_96, -6.84_05]]							, dtype=torch.float							, device=lowerCAmelCase__							, )
				self.assertTrue(torch.allclose(outputs.start_logits[:, :1_0]							, lowerCAmelCase__							, atol=1E-4		)		)
				self.assertTrue(torch.allclose(outputs.end_logits[:, :1_0]							, lowerCAmelCase__							, atol=1E-4		)		)










from typing import TYPE_CHECKING

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


__A    :   Tuple          =       {
    "configuration_mobilevitv2": [
        "MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "MobileViTV2Config",
        "MobileViTV2OnnxConfig",
    ],
}


try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A    :   List[str]          =       [
				    "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    :   Tuple          =       _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



