




"""simple docstring"""

def    snake_case__						(							lowerCAmelCase :		int		):
					if not isinstance(lowerCAmelCase					,       lowerCAmelCase		):
										__snake_case        =						F'''Input value of [number={number}] must be an integer'''
										raise TypeError(lowerCAmelCase		)
					if number < 0 or number % 2 == 1:
										return False
					__snake_case        =						int(number**0.5		)
					return number == number_sqrt * (number_sqrt + 1)


if __name__ == "__main__":
		import doctest

		doctest.testmod()








"""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      A__   :




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




	def 				a_		( self	:						str						)       ->			Optional[Any]:
						__snake_case        =						ids_tensor([self.batch_size, self.seq_length]   ,     self.vocab_size						)

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

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

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

						__snake_case        =						self.get_config()

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




	def 				a_		( 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 				a_		( self	:						Optional[Any]   ,     lowercase	:						str   ,     lowercase	:						Optional[Any]   ,     lowercase	:						str   ,     lowercase	:						Dict   ,     lowercase	:						Union[str, Any]   ,     lowercase	:						Any   ,     lowercase	:						List[str]						)       ->			Optional[Any]:
						__snake_case        =						DPRContextEncoder(config=lowercase						)
						model.to(lowercase						)
						model.eval()
						__snake_case        =						model(lowercase   ,     attention_mask=lowercase   ,     token_type_ids=lowercase						)
						__snake_case        =						model(lowercase   ,     token_type_ids=lowercase						)
						__snake_case        =						model(lowercase						)
						self.parent.assertEqual(result.pooler_output.shape   ,     (self.batch_size, self.projection_dim or self.hidden_size)						)




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




	def 				a_		( self	:						str   ,     lowercase	:						List[Any]   ,     lowercase	:						int   ,     lowercase	:						Optional[Any]   ,     lowercase	:						Any   ,     lowercase	:						Tuple   ,     lowercase	:						Optional[Any]   ,     lowercase	:						int						)       ->			str:
						__snake_case        =						DPRReader(config=lowercase						)
						model.to(lowercase						)
						model.eval()
						__snake_case        =						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 				a_		( self	:						str						)       ->			Optional[Any]:
						__snake_case        =						self.prepare_config_and_inputs()
						(
						    (
						    __snake_case
						)   ,   (
						    __snake_case
						)   ,   (
						    __snake_case
						)   ,   (
						    __snake_case
						)   ,   (
						    __snake_case
						)   ,   (
						    __snake_case
						)   ,   (
						    __snake_case
						)   ,   
						)        =						config_and_inputs
						__snake_case        =						{"""input_ids""": input_ids}
						return config, inputs_dict

@require_torch
class      A__   (   lowercase_  ,						lowercase_  ,						unittest.TestCase  ):
	A_				:					Dict      =   (
	    (
	        DPRContextEncoder,
	        DPRQuestionEncoder,
	        DPRReader,
	    )
	    if is_torch_available()
	    else ()
	)
	A_				:					Any      =   {"""feature-extraction""": DPRQuestionEncoder} if is_torch_available() else {}

	A_				:					List[Any]      =   False
	A_				:					Union[str, Any]      =   False  # why?
	A_				:					Any      =   False
	A_				:					List[str]      =   False




	def 				a_		( self	:						Union[str, Any]						)       ->			Dict:
						__snake_case        =						DPRModelTester(self						)
						__snake_case        =						ConfigTester(self   ,     config_class=lowercase   ,     hidden_size=37						)




	def 				a_		( self	:						Optional[int]						)       ->			Optional[int]:
						self.config_tester.run_common_tests()




	def 				a_		( self	:						Dict						)       ->			List[str]:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_context_encoder(*lowercase						)




	def 				a_		( self	:						Any						)       ->			Tuple:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_question_encoder(*lowercase						)




	def 				a_		( self	:						Any						)       ->			Optional[int]:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_reader(*lowercase						)




	def 				a_		( self	:						Tuple						)       ->			str:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()[0]

						__snake_case        =						DPRQuestionEncoder(config=lowercase						)
						model.to(lowercase						)
						model.eval()

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

						self.assertIsNotNone(lowercase						)




	@slow
	def 				a_		( self	:						Any						)       ->			Optional[Any]:
						for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__snake_case        =						DPRContextEncoder.from_pretrained(lowercase						)
											self.assertIsNotNone(lowercase						)

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

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

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

@require_torch
class      A__   (   unittest.TestCase  ):




	@slow
	def 				a_		( self	:						List[str]						)       ->			Union[str, Any]:
						__snake_case        =						DPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""   ,     return_dict=lowercase						)
						model.to(lowercase						)

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




	@slow
	def 				a_		( self	:						Tuple						)       ->			int:
						__snake_case        =						DPRReaderTokenizer.from_pretrained("""facebook/dpr-reader-single-nq-base"""						)
						__snake_case        =						DPRReader.from_pretrained("""facebook/dpr-reader-single-nq-base"""						)
						model.to(lowercase						)

						__snake_case        =						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        =						model(**lowercase						)

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

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


