


'''simple docstring'''
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 		UpperCamelCase_		:





						"""simple docstring"""




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

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

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

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

										__magic_name__	  = self.get_config()

										return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
						def 					__A					( self				:					int  )							->					Tuple:
										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 					__A					( self				:					Optional[int]	,				_lowerCamelCase				:					Optional[Any]	,				_lowerCamelCase				:					Dict	,				_lowerCamelCase				:					Optional[Any]	,				_lowerCamelCase				:					Dict	,				_lowerCamelCase				:					int	,				_lowerCamelCase				:					Tuple	,				_lowerCamelCase				:					Optional[int]  )							->					Optional[Any]:
										__magic_name__	  = DPRContextEncoder(config=_lowerCamelCase  )
										model.to(_lowerCamelCase  )
										model.eval()
										__magic_name__	  = model(_lowerCamelCase	,				attention_mask=_lowerCamelCase	,				token_type_ids=_lowerCamelCase  )
										__magic_name__	  = model(_lowerCamelCase	,				token_type_ids=_lowerCamelCase  )
										__magic_name__	  = model(_lowerCamelCase  )
										self.parent.assertEqual(result.pooler_output.shape	,				(self.batch_size, self.projection_dim or self.hidden_size)  )
						def 					__A					( self				:					Dict	,				_lowerCamelCase				:					Any	,				_lowerCamelCase				:					Any	,				_lowerCamelCase				:					Optional[int]	,				_lowerCamelCase				:					str	,				_lowerCamelCase				:					int	,				_lowerCamelCase				:					Optional[Any]	,				_lowerCamelCase				:					List[str]  )							->					List[Any]:
										__magic_name__	  = DPRQuestionEncoder(config=_lowerCamelCase  )
										model.to(_lowerCamelCase  )
										model.eval()
										__magic_name__	  = model(_lowerCamelCase	,				attention_mask=_lowerCamelCase	,				token_type_ids=_lowerCamelCase  )
										__magic_name__	  = model(_lowerCamelCase	,				token_type_ids=_lowerCamelCase  )
										__magic_name__	  = model(_lowerCamelCase  )
										self.parent.assertEqual(result.pooler_output.shape	,				(self.batch_size, self.projection_dim or self.hidden_size)  )
						def 					__A					( self				:					Tuple	,				_lowerCamelCase				:					Optional[Any]	,				_lowerCamelCase				:					int	,				_lowerCamelCase				:					Union[str, Any]	,				_lowerCamelCase				:					str	,				_lowerCamelCase				:					int	,				_lowerCamelCase				:					Optional[Any]	,				_lowerCamelCase				:					Optional[Any]  )							->					Optional[Any]:
										__magic_name__	  = DPRReader(config=_lowerCamelCase  )
										model.to(_lowerCamelCase  )
										model.eval()
										__magic_name__	  = 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 					__A					( self				:					Tuple  )							->					int:
										__magic_name__	  = self.prepare_config_and_inputs()
										(
										    (
										    __magic_name__      
										)  ,      (
										    __magic_name__      
										)  ,      (
										    __magic_name__      
										)  ,      (
										    __magic_name__      
										)  ,      (
										    __magic_name__      
										)  ,      (
										    __magic_name__      
										)  ,      (
										    __magic_name__      
										)  ,            
										)	  = config_and_inputs
										__magic_name__	  = {"input_ids": input_ids}
										return config, inputs_dict






@require_torch
class 		UpperCamelCase_		(					A   ,		A   ,		unittest.TestCase   ):





						"""simple docstring"""




						UpperCAmelCase__	:       int         =     (
						    (
						        DPRContextEncoder,
						        DPRQuestionEncoder,
						        DPRReader,
						    )
						    if is_torch_available()
						    else ()
						)
						UpperCAmelCase__	:       Optional[int]         =     {'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

						UpperCAmelCase__	:       Union[str, Any]         =     False
						UpperCAmelCase__	:       List[str]         =     False  # why?
						UpperCAmelCase__	:       Any         =     False
						UpperCAmelCase__	:       List[Any]         =     False
						def 					__A					( self				:					Optional[int]  )							->					Optional[int]:
										__magic_name__	  = DPRModelTester(self  )
										__magic_name__	  = ConfigTester(self	,				config_class=_lowerCamelCase	,				hidden_size=37  )
						def 					__A					( self				:					int  )							->					Any:
										self.config_tester.run_common_tests()
						def 					__A					( self				:					Any  )							->					Any:
										__magic_name__	  = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_context_encoder(*_lowerCamelCase  )
						def 					__A					( self				:					Any  )							->					str:
										__magic_name__	  = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_question_encoder(*_lowerCamelCase  )
						def 					__A					( self				:					List[str]  )							->					str:
										__magic_name__	  = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_reader(*_lowerCamelCase  )
						def 					__A					( self				:					List[str]  )							->					List[str]:
										__magic_name__	  = self.model_tester.prepare_config_and_inputs()[0]

										__magic_name__	  = DPRQuestionEncoder(config=_lowerCamelCase  )
										model.to(_lowerCamelCase  )
										model.eval()

										with tempfile.TemporaryDirectory() as tmp_dirname:
														model.save_pretrained(_lowerCamelCase  )
														__magic_name__	  = DPRQuestionEncoder.from_pretrained(_lowerCamelCase	,				projection_dim=5_12  )

										self.assertIsNotNone(_lowerCamelCase  )

						@slow
						def 					__A					( self				:					Dict  )							->					Any:
										for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														__magic_name__	  = DPRContextEncoder.from_pretrained(_lowerCamelCase  )
														self.assertIsNotNone(_lowerCamelCase  )

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

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

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






@require_torch
class 		UpperCamelCase_		(					unittest.TestCase   ):





						"""simple docstring"""




						@slow
						def 					__A					( self				:					Tuple  )							->					Tuple:
										__magic_name__	  = DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"	,				return_dict=_lowerCamelCase  )
										model.to(_lowerCamelCase  )

										__magic_name__	  = torch.tensor(
										    [[1_01, 75_92, 10_10, 20_03, 20_26, 38_99, 1_01_40, 10_29, 1_02]]	,				dtype=torch.long	,				device=_lowerCamelCase  )  # [CLS] hello, is my dog cute? [SEP]
										__magic_name__	  = model(_lowerCamelCase  )[0]  # embedding shape = (1, 768)
										# compare the actual values for a slice.
										__magic_name__	  = torch.tensor(
										    [
										        [
										            0.03_236_253,
										            0.12_753_335,
										            0.16_818_509,
										            0.00_279_786,
										            0.3_896_933,
										            0.24_264_945,
										            0.2_178_971,
										            -0.02_335_227,
										            -0.08_481_959,
										            -0.14_324_117,
										        ]
										    ]	,				dtype=torch.float	,				device=_lowerCamelCase	,				)
										self.assertTrue(torch.allclose(output[:, :10]	,				_lowerCamelCase	,				atol=1e-4  )  )

						@slow
						def 					__A					( self				:					Dict  )							->					List[Any]:
										__magic_name__	  = DPRReaderTokenizer.from_pretrained("facebook/dpr-reader-single-nq-base"  )
										__magic_name__	  = DPRReader.from_pretrained("facebook/dpr-reader-single-nq-base"  )
										model.to(_lowerCamelCase  )

										__magic_name__	  = 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  )

										__magic_name__	  = model(**_lowerCamelCase  )

										# compare the actual values for a slice.
										__magic_name__	  = torch.tensor(
										    [[-10.3_005, -10.7_765, -11.4_872, -11.6_841, -11.9_312, -10.3_002, -9.8_544, -11.7_378, -12.0_821, -10.2_975]]	,				dtype=torch.float	,				device=_lowerCamelCase	,				)

										__magic_name__	  = torch.tensor(
										    [[-11.0_684, -11.7_041, -11.5_397, -10.3_465, -10.8_791, -6.8_443, -11.9_959, -11.0_364, -10.0_096, -6.8_405]]	,				dtype=torch.float	,				device=_lowerCamelCase	,				)
										self.assertTrue(torch.allclose(outputs.start_logits[:, :10]	,				_lowerCamelCase	,				atol=1e-4  )  )
										self.assertTrue(torch.allclose(outputs.end_logits[:, :10]	,				_lowerCamelCase	,				atol=1e-4  )  )







'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


__magic_name__   :   List[Any]  							={'configuration_yoso': ['YOSO_PRETRAINED_CONFIG_ARCHIVE_MAP', 'YosoConfig']}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__magic_name__   :   Union[str, Any]  							=[
						    'YOSO_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'YosoForMaskedLM',
						    'YosoForMultipleChoice',
						    'YosoForQuestionAnswering',
						    'YosoForSequenceClassification',
						    'YosoForTokenClassification',
						    'YosoLayer',
						    'YosoModel',
						    'YosoPreTrainedModel',
						]


if TYPE_CHECKING:
						from .configuration_yoso import YOSO_PRETRAINED_CONFIG_ARCHIVE_MAP, YosoConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_yoso import (
												    YOSO_PRETRAINED_MODEL_ARCHIVE_LIST,
												    YosoForMaskedLM,
												    YosoForMultipleChoice,
												    YosoForQuestionAnswering,
												    YosoForSequenceClassification,
												    YosoForTokenClassification,
												    YosoLayer,
												    YosoModel,
												    YosoPreTrainedModel,
												)


else:
						import sys

						__magic_name__   :   Optional[int]  							=_LazyModule(__name__, globals()['__file__'], _import_structure)



