





'''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 		snake_case_      :







	'''simple docstring'''




	def __init__(	self       :    List[str]				,			__lowerCamelCase       :    Tuple				,			__lowerCamelCase       :    List[Any]=13				,			__lowerCamelCase       :    Union[str, Any]=7				,			__lowerCamelCase       :    Tuple=False				,			__lowerCamelCase       :    Dict=True				,			__lowerCamelCase       :    Optional[int]=True				,			__lowerCamelCase       :    Optional[Any]=True				,			__lowerCamelCase       :    Optional[int]=99				,			__lowerCamelCase       :    Optional[Any]=32				,			__lowerCamelCase       :    Tuple=5				,			__lowerCamelCase       :    List[Any]=4				,			__lowerCamelCase       :    int=37				,			__lowerCamelCase       :    Dict="gelu"				,			__lowerCamelCase       :    Tuple=0.1				,			__lowerCamelCase       :    Tuple=0.1				,			__lowerCamelCase       :    Tuple=512				,			__lowerCamelCase       :    Optional[int]=16				,			__lowerCamelCase       :    int=2				,			__lowerCamelCase       :    Tuple=0.02				,			__lowerCamelCase       :    Optional[int]=3				,			__lowerCamelCase       :    str=4				,			__lowerCamelCase       :    List[Any]=None				,			__lowerCamelCase       :    Optional[Any]=0				,			)					->   Optional[int]:

			'''simple docstring'''




			__lowercase					=    parent
			__lowercase					=    batch_size
			__lowercase					=    seq_length
			__lowercase					=    is_training
			__lowercase					=    use_input_mask
			__lowercase					=    use_token_type_ids
			__lowercase					=    use_labels
			__lowercase					=    vocab_size
			__lowercase					=    hidden_size
			__lowercase					=    num_hidden_layers
			__lowercase					=    num_attention_heads
			__lowercase					=    intermediate_size
			__lowercase					=    hidden_act
			__lowercase					=    hidden_dropout_prob
			__lowercase					=    attention_probs_dropout_prob
			__lowercase					=    max_position_embeddings
			__lowercase					=    type_vocab_size
			__lowercase					=    type_sequence_label_size
			__lowercase					=    initializer_range
			__lowercase					=    num_labels
			__lowercase					=    num_choices
			__lowercase					=    scope
			__lowercase					=    projection_dim




	def       UpperCAmelCase (	self       :    List[Any]      )					->   int:

			'''simple docstring'''




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

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

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

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

			__lowercase					=    self.get_config()

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




	def       UpperCAmelCase (	self       :    List[str]      )					->   Any:

			'''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       UpperCAmelCase (	self       :    Optional[Any]				,			__lowerCamelCase       :    Any				,			__lowerCamelCase       :    int				,			__lowerCamelCase       :    Optional[int]				,			__lowerCamelCase       :    Optional[int]				,			__lowerCamelCase       :    List[str]				,			__lowerCamelCase       :    Tuple				,			__lowerCamelCase       :    int      )					->   Optional[Any]:

			'''simple docstring'''




			__lowercase					=    DPRContextEncoder(config=_A      )
			model.to(_A      )
			model.eval()
			__lowercase					=    model(_A				,			attention_mask=_A				,			token_type_ids=_A      )
			__lowercase					=    model(_A				,			token_type_ids=_A      )
			__lowercase					=    model(_A      )
			self.parent.assertEqual(result.pooler_output.shape				,			(self.batch_size, self.projection_dim or self.hidden_size)      )




	def       UpperCAmelCase (	self       :    Optional[int]				,			__lowerCamelCase       :    Any				,			__lowerCamelCase       :    Union[str, Any]				,			__lowerCamelCase       :    List[Any]				,			__lowerCamelCase       :    str				,			__lowerCamelCase       :    Tuple				,			__lowerCamelCase       :    Optional[Any]				,			__lowerCamelCase       :    Tuple      )					->   List[Any]:

			'''simple docstring'''




			__lowercase					=    DPRQuestionEncoder(config=_A      )
			model.to(_A      )
			model.eval()
			__lowercase					=    model(_A				,			attention_mask=_A				,			token_type_ids=_A      )
			__lowercase					=    model(_A				,			token_type_ids=_A      )
			__lowercase					=    model(_A      )
			self.parent.assertEqual(result.pooler_output.shape				,			(self.batch_size, self.projection_dim or self.hidden_size)      )




	def       UpperCAmelCase (	self       :    Tuple				,			__lowerCamelCase       :    List[str]				,			__lowerCamelCase       :    Tuple				,			__lowerCamelCase       :    Optional[Any]				,			__lowerCamelCase       :    Union[str, Any]				,			__lowerCamelCase       :    Any				,			__lowerCamelCase       :    Dict				,			__lowerCamelCase       :    Union[str, Any]      )					->   List[str]:

			'''simple docstring'''




			__lowercase					=    DPRReader(config=_A      )
			model.to(_A      )
			model.eval()
			__lowercase					=    model(
			    _A				,			attention_mask=_A				,			)

			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       UpperCAmelCase (	self       :    int      )					->   List[str]:

			'''simple docstring'''




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





@require_torch
class 		snake_case_      (       lowercase__    ,    lowercase__    ,    unittest.TestCase	):







	'''simple docstring'''
	__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       UpperCAmelCase (	self       :    Dict      )					->   Optional[int]:

			'''simple docstring'''




			__lowercase					=    DPRModelTester(self      )
			__lowercase					=    ConfigTester(self				,			config_class=_A				,			hidden_size=37      )




	def       UpperCAmelCase (	self       :    List[Any]      )					->   Any:

			'''simple docstring'''




			self.config_tester.run_common_tests()




	def       UpperCAmelCase (	self       :    Tuple      )					->   Dict:

			'''simple docstring'''




			__lowercase					=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_context_encoder(*_A      )




	def       UpperCAmelCase (	self       :    List[Any]      )					->   Union[str, Any]:

			'''simple docstring'''




			__lowercase					=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_question_encoder(*_A      )




	def       UpperCAmelCase (	self       :    Tuple      )					->   Any:

			'''simple docstring'''




			__lowercase					=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_reader(*_A      )




	def       UpperCAmelCase (	self       :    Union[str, Any]      )					->   Dict:

			'''simple docstring'''




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

			__lowercase					=    DPRQuestionEncoder(config=_A      )
			model.to(_A      )
			model.eval()

			with tempfile.TemporaryDirectory() as tmp_dirname:
					model.save_pretrained(_A      )
					__lowercase					=    DPRQuestionEncoder.from_pretrained(_A				,			projection_dim=512      )

			self.assertIsNotNone(_A      )






	@slow
	def       UpperCAmelCase (	self       :    Dict      )					->   List[str]:

			'''simple docstring'''




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

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

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

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





@require_torch
class 		snake_case_      (       unittest.TestCase	):







	'''simple docstring'''




	@slow
	def       UpperCAmelCase (	self       :    Tuple      )					->   List[Any]:

			'''simple docstring'''




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

			__lowercase					=    torch.tensor(
			    [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]]				,			dtype=torch.long				,			device=_A      )  # [CLS] hello, is my dog cute? [SEP]
			__lowercase					=    model(_A      )[0]  # embedding shape = (1, 768)
			# compare the actual values for a slice.
			__lowercase					=    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=_A				,			)
			self.assertTrue(torch.allclose(output[:, :10]				,			_A				,			atol=1E-4      )      )






	@slow
	def       UpperCAmelCase (	self       :    List[Any]      )					->   Dict:

			'''simple docstring'''




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

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

			__lowercase					=    model(**_A      )

			# compare the actual values for a slice.
			__lowercase					=    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=_A				,			)

			__lowercase					=    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=_A				,			)
			self.assertTrue(torch.allclose(outputs.start_logits[:, :10]				,			_A				,			atol=1E-4      )      )
			self.assertTrue(torch.allclose(outputs.end_logits[:, :10]				,			_A				,			atol=1E-4      )      )











def    SCREAMING_SNAKE_CASE							(			snake_case       )		->						int:
		__lowercase					=    str(snake_case       ).strip()
		if not bin_string:
				raise ValueError('Empty string was passed to the function'       )
		__lowercase					=    bin_string[0] == '-'
		if is_negative:
				__lowercase					=    bin_string[1:]
		if not all(char in '01' for char in bin_string       ):
				raise ValueError('Non-binary value was passed to the function'       )
		__lowercase					=    0
		for char in bin_string:
				__lowercase					=    2 * decimal_number + int(snake_case       )
		return -decimal_number if is_negative else decimal_number


if __name__ == "__main__":
		from doctest import testmod

		testmod()




