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







	def __init__(    self			:Optional[Any]      ,    lowercase			:Dict      ,    lowercase			:int=1_3      ,    lowercase			:Tuple=7      ,    lowercase			:str=False      ,    lowercase			:str=True      ,    lowercase			:Optional[Any]=True      ,    lowercase			:List[Any]=True      ,    lowercase			:int=9_9      ,    lowercase			:Optional[Any]=3_2      ,    lowercase			:Optional[int]=5      ,    lowercase			:int=4      ,    lowercase			:str=3_7      ,    lowercase			:Dict="gelu"      ,    lowercase			:str=0.1      ,    lowercase			:List[Any]=0.1      ,    lowercase			:Dict=5_1_2      ,    lowercase			:str=1_6      ,    lowercase			:str=2      ,    lowercase			:Union[str, Any]=0.02      ,    lowercase			:str=3      ,    lowercase			:str=4      ,    lowercase			:Optional[int]=None      ,    lowercase			:int=0      ,    )		->			int:







				"""simple docstring"""






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







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







				"""simple docstring"""






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

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

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

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

				SCREAMING_SNAKE_CASE    	=   self.get_config()

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







	def    snake_case__    (    self			:str )		->			str:







				"""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    snake_case__    (    self			:Optional[Any]      ,    lowercase			:Tuple      ,    lowercase			:Optional[Any]      ,    lowercase			:Optional[int]      ,    lowercase			:Any      ,    lowercase			:Optional[int]      ,    lowercase			:Dict      ,    lowercase			:List[str] )		->			Dict:







				"""simple docstring"""






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







	def    snake_case__    (    self			:int      ,    lowercase			:int      ,    lowercase			:Union[str, Any]      ,    lowercase			:Any      ,    lowercase			:Dict      ,    lowercase			:Tuple      ,    lowercase			:str      ,    lowercase			:Tuple )		->			str:







				"""simple docstring"""






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







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







				"""simple docstring"""






				SCREAMING_SNAKE_CASE    	=   DPRReader(config=lowercase )
				model.to(lowercase )
				model.eval()
				SCREAMING_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    snake_case__    (    self			:List[str] )		->			Any:







				"""simple docstring"""






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





@require_torch
class 						lowerCamelCase			(					__lowerCamelCase   ,		__lowerCamelCase   ,		unittest.TestCase				):
	UpperCamelCase_     :				int								=  (
	    (
	        DPRContextEncoder,
	        DPRQuestionEncoder,
	        DPRReader,
	    )
	    if is_torch_available()
	    else ()
	)
	UpperCamelCase_     :				Optional[int]								=  {'feature-extraction': DPRQuestionEncoder} if is_torch_available() else {}

	UpperCamelCase_     :				List[str]								=  False
	UpperCamelCase_     :				Dict								=  False  # why?
	UpperCamelCase_     :				List[str]								=  False
	UpperCamelCase_     :				Dict								=  False







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







				"""simple docstring"""






				SCREAMING_SNAKE_CASE    	=   DPRModelTester(self )
				SCREAMING_SNAKE_CASE    	=   ConfigTester(self      ,    config_class=lowercase      ,    hidden_size=3_7 )







	def    snake_case__    (    self			:int )		->			str:







				"""simple docstring"""






				self.config_tester.run_common_tests()







	def    snake_case__    (    self			:Optional[int] )		->			str:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE    	=   self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_context_encoder(*lowercase )







	def    snake_case__    (    self			:int )		->			Union[str, Any]:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE    	=   self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_question_encoder(*lowercase )







	def    snake_case__    (    self			:Optional[Any] )		->			Optional[int]:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE    	=   self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_reader(*lowercase )







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







				"""simple docstring"""






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

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

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

				self.assertIsNotNone(lowercase )







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







				"""simple docstring"""






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

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

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

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





@require_torch
class 						lowerCamelCase			(					unittest.TestCase				):







	@slow
	def    snake_case__    (    self			:Optional[Any] )		->			Optional[Any]:







				"""simple docstring"""






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

				SCREAMING_SNAKE_CASE    	=   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=lowercase )  # [CLS] hello, is my dog cute? [SEP]
				SCREAMING_SNAKE_CASE    	=   model(lowercase )[0]  # embedding shape = (1, 768)
				# compare the actual values for a slice.
				SCREAMING_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[:, :1_0]      ,    lowercase      ,    atol=1e-4 ) )







	@slow
	def    snake_case__    (    self			:Dict )		->			Union[str, Any]:







				"""simple docstring"""






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

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

				SCREAMING_SNAKE_CASE    	=   model(**lowercase )

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

				SCREAMING_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[:, :1_0]      ,    lowercase      ,    atol=1e-4 ) )
				self.assertTrue(torch.allclose(outputs.end_logits[:, :1_0]      ,    lowercase      ,    atol=1e-4 ) )

import numpy as np





class 						lowerCamelCase			:







	def __init__(    self			:Any      ,    lowercase			:List[Any]=5      ,    lowercase			:Optional[Any]=5 )		->			Tuple:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE    	=   depth
				SCREAMING_SNAKE_CASE    	=   0
				SCREAMING_SNAKE_CASE    	=   None
				SCREAMING_SNAKE_CASE    	=   None
				SCREAMING_SNAKE_CASE    	=   min_leaf_size
				SCREAMING_SNAKE_CASE    	=   None







	def    snake_case__    (    self			:str      ,    lowercase			:int      ,    lowercase			:int )		->			str:







				"""simple docstring"""






				if labels.ndim != 1:
							print('''Error: Input labels must be one dimensional''' )

				return np.mean((labels - prediction) ** 2 )







	def    snake_case__    (    self			:Union[str, Any]      ,    lowercase			:int      ,    lowercase			:Dict )		->			Tuple:







				"""simple docstring"""






				if x.ndim != 1:
							print('''Error: Input data set must be one dimensional''' )
							return
				if len(lowercase ) != len(lowercase ):
							print('''Error: X and y have different lengths''' )
							return
				if y.ndim != 1:
							print('''Error: Data set labels must be one dimensional''' )
							return

				if len(lowercase ) < 2 * self.min_leaf_size:
							SCREAMING_SNAKE_CASE    	=   np.mean(lowercase )
							return

				if self.depth == 1:
							SCREAMING_SNAKE_CASE    	=   np.mean(lowercase )
							return

				SCREAMING_SNAKE_CASE    	=   0
				SCREAMING_SNAKE_CASE    	=   self.mean_squared_error(lowercase      ,    np.mean(lowercase ) ) * 2
				for i in range(len(lowercase ) ):
							if len(x[:i] ) < self.min_leaf_size:
										continue
							elif len(x[i:] ) < self.min_leaf_size:
										continue
							else:
										SCREAMING_SNAKE_CASE    	=   self.mean_squared_error(x[:i]      ,    np.mean(y[:i] ) )
										SCREAMING_SNAKE_CASE    	=   self.mean_squared_error(x[i:]      ,    np.mean(y[i:] ) )
										SCREAMING_SNAKE_CASE    	=   error_left + error_right
										if error < min_error:
													SCREAMING_SNAKE_CASE    	=   i
													SCREAMING_SNAKE_CASE    	=   error

				if best_split != 0:
							SCREAMING_SNAKE_CASE    	=   x[:best_split]
							SCREAMING_SNAKE_CASE    	=   y[:best_split]
							SCREAMING_SNAKE_CASE    	=   x[best_split:]
							SCREAMING_SNAKE_CASE    	=   y[best_split:]

							SCREAMING_SNAKE_CASE    	=   x[best_split]
							SCREAMING_SNAKE_CASE    	=   DecisionTree(
							    depth=self.depth - 1      ,    min_leaf_size=self.min_leaf_size )
							SCREAMING_SNAKE_CASE    	=   DecisionTree(
							    depth=self.depth - 1      ,    min_leaf_size=self.min_leaf_size )
							self.left.train(lowercase      ,    lowercase )
							self.right.train(lowercase      ,    lowercase )
				else:
							SCREAMING_SNAKE_CASE    	=   np.mean(lowercase )

				return







	def    snake_case__    (    self			:Optional[int]      ,    lowercase			:List[Any] )		->			str:







				"""simple docstring"""






				if self.prediction is not None:
							return self.prediction
				elif self.left or self.right is not None:
							if x >= self.decision_boundary:
										return self.right.predict(lowercase )
							else:
										return self.left.predict(lowercase )
				else:
							print('''Error: Decision tree not yet trained''' )
							return None





class 						lowerCamelCase			:







	@staticmethod
	def    snake_case__    (    lowercase			:Optional[int]      ,    lowercase			:Optional[Any] )		->			Dict:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE    	=   float(0 )
				for label in labels:
							squared_error_sum += (label - prediction) ** 2

				return float(squared_error_sum / labels.size )



def       a			(   )    ->Any:




			'''simple docstring'''


			SCREAMING_SNAKE_CASE    	=   np.arange(-1.0		,	1.0		,	0.0_05  )
			SCREAMING_SNAKE_CASE    	=   np.sin(a  )

			SCREAMING_SNAKE_CASE    	=   DecisionTree(depth=10		,	min_leaf_size=10  )
			tree.train(a		,	a  )

			SCREAMING_SNAKE_CASE    	=   (np.random.rand(10  ) * 2) - 1
			SCREAMING_SNAKE_CASE    	=   np.array([tree.predict(a  ) for x in test_cases]  )
			SCREAMING_SNAKE_CASE    	=   np.mean((predictions - test_cases) ** 2  )

			print('''Test values: ''' + str(a  )  )
			print('''Predictions: ''' + str(a  )  )
			print('''Average error: ''' + str(a  )  )


if __name__ == "__main__":
							main()
							import doctest

							doctest.testmod(name='mean_squarred_error', verbose=True)