




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   :	List[str]   ,     __UpperCAmelCase   :	Any   ,     __UpperCAmelCase   :	List[Any]=13   ,     __UpperCAmelCase   :	Union[str, Any]=7   ,     __UpperCAmelCase   :	int=False   ,     __UpperCAmelCase   :	int=True   ,     __UpperCAmelCase   :	Optional[Any]=True   ,     __UpperCAmelCase   :	Optional[Any]=True   ,     __UpperCAmelCase   :	Optional[Any]=99   ,     __UpperCAmelCase   :	int=32   ,     __UpperCAmelCase   :	int=5   ,     __UpperCAmelCase   :	List[Any]=4   ,     __UpperCAmelCase   :	Tuple=37   ,     __UpperCAmelCase   :	List[str]="gelu"   ,     __UpperCAmelCase   :	int=0.1   ,     __UpperCAmelCase   :	Tuple=0.1   ,     __UpperCAmelCase   :	Dict=512   ,     __UpperCAmelCase   :	Tuple=16   ,     __UpperCAmelCase   :	Any=2   ,     __UpperCAmelCase   :	List[Any]=0.02   ,     __UpperCAmelCase   :	int=3   ,     __UpperCAmelCase   :	Dict=4   ,     __UpperCAmelCase   :	List[str]=None   ,     __UpperCAmelCase   :	Optional[int]=0   ,     )		->					Union[str, Any]:

										"""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   :	Dict				)		->					Tuple:

										"""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   :	Tuple				)		->					Optional[int]:

										"""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   :	str   ,     __UpperCAmelCase   :	List[str]   ,     __UpperCAmelCase   :	Union[str, Any]   ,     __UpperCAmelCase   :	Union[str, Any]   ,     __UpperCAmelCase   :	Tuple   ,     __UpperCAmelCase   :	Optional[int]   ,     __UpperCAmelCase   :	str   ,     __UpperCAmelCase   :	Any				)		->					List[Any]:

										"""simple docstring"""



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







							def        lowercase__							(    self   :	Optional[int]   ,     __UpperCAmelCase   :	Optional[Any]   ,     __UpperCAmelCase   :	Tuple   ,     __UpperCAmelCase   :	List[str]   ,     __UpperCAmelCase   :	Tuple   ,     __UpperCAmelCase   :	List[str]   ,     __UpperCAmelCase   :	Optional[Any]   ,     __UpperCAmelCase   :	Dict				)		->					Union[str, Any]:

										"""simple docstring"""



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







							def        lowercase__							(    self   :	Dict   ,     __UpperCAmelCase   :	Union[str, Any]   ,     __UpperCAmelCase   :	List[Any]   ,     __UpperCAmelCase   :	Optional[int]   ,     __UpperCAmelCase   :	Any   ,     __UpperCAmelCase   :	Tuple   ,     __UpperCAmelCase   :	Dict   ,     __UpperCAmelCase   :	List[str]				)		->					List[str]:

										"""simple docstring"""



										UpperCamelCase_     				= DPRReader(config=__UpperCAmelCase				)
										model.to(__UpperCAmelCase				)
										model.eval()
										UpperCamelCase_     				= model(
										    __UpperCAmelCase   ,     attention_mask=__UpperCAmelCase   ,     )

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

										"""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					(				lowerCamelCase_  ,						lowerCamelCase_  ,						unittest.TestCase       ):
							_SCREAMING_SNAKE_CASE  :		Optional[Any]			  =       (
							    (
							        DPRContextEncoder,
							        DPRQuestionEncoder,
							        DPRReader,
							    )
							    if is_torch_available()
							    else ()
							)
							_SCREAMING_SNAKE_CASE  :		Dict			  =       {'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

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







							def        lowercase__							(    self   :	Dict				)		->					List[Any]:

										"""simple docstring"""



										UpperCamelCase_     				= DPRModelTester(self				)
										UpperCamelCase_     				= ConfigTester(self   ,     config_class=__UpperCAmelCase   ,     hidden_size=37				)







							def        lowercase__							(    self   :	int				)		->					Dict:

										"""simple docstring"""



										self.config_tester.run_common_tests()







							def        lowercase__							(    self   :	str				)		->					Any:

										"""simple docstring"""



										UpperCamelCase_     				= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_context_encoder(*__UpperCAmelCase				)







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

										"""simple docstring"""



										UpperCamelCase_     				= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_question_encoder(*__UpperCAmelCase				)







							def        lowercase__							(    self   :	Dict				)		->					List[str]:

										"""simple docstring"""



										UpperCamelCase_     				= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_reader(*__UpperCAmelCase				)







							def        lowercase__							(    self   :	int				)		->					str:

										"""simple docstring"""



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

										UpperCamelCase_     				= DPRQuestionEncoder(config=__UpperCAmelCase				)
										model.to(__UpperCAmelCase				)
										model.eval()

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

										self.assertIsNotNone(__UpperCAmelCase				)







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

										"""simple docstring"""



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

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

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

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







@require_torch
class     A					(				unittest.TestCase       ):







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

										"""simple docstring"""



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

										UpperCamelCase_     				= torch.tensor(
										    [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]   ,     dtype=torch.long   ,     device=__UpperCAmelCase				)  # [CLS] hello, is my dog cute? [SEP]
										UpperCamelCase_     				= model(__UpperCAmelCase				)[0]  # embedding shape = (1, 768)
										# compare the actual values for a slice.
										UpperCamelCase_     				= 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=__UpperCAmelCase   ,     )
										self.assertTrue(torch.allclose(output[:, :10]   ,     __UpperCAmelCase   ,     atol=1E-4				)				)







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

										"""simple docstring"""



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

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

										UpperCamelCase_     				= model(**__UpperCAmelCase				)

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

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






from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types




def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case					)	->			torch.Tensor:

			'''simple docstring'''


			if not inplace:
						UpperCamelCase_     				= ma + ma
			else:
						ma += ma

			return ma




def  a_		(							__snake_case     ,		__snake_case					)	->			torch.Tensor:

			'''simple docstring'''


			UpperCamelCase_     				= -1 * len(__snake_case					)
			UpperCamelCase_     				= list(range(len(tensor.shape[:zero_index]					)					)					)
			return tensor.permute(first_inds + [zero_index + i for i in inds]					)




def  a_		(							__snake_case     ,		__snake_case					)	->			torch.Tensor:

			'''simple docstring'''


			return t.reshape(t.shape[:-no_dims] + (-1,)					)




def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case     ,		__snake_case = 1E-4					)	->			torch.Tensor:

			'''simple docstring'''


			UpperCamelCase_     				= mask.expand(*value.shape					)
			return torch.sum(mask * value     ,		dim=__snake_case					) / (eps + torch.sum(__snake_case     ,		dim=__snake_case					))




def  a_		(							__snake_case     ,		__snake_case = 2.31_25     ,		__snake_case = 21.68_75     ,		__snake_case = 6_4					)	->			torch.Tensor:

			'''simple docstring'''


			UpperCamelCase_     				= torch.linspace(__snake_case     ,		__snake_case     ,		no_bins - 1     ,		device=pts.device					)
			UpperCamelCase_     				= torch.sqrt(torch.sum((pts.unsqueeze(-2					) - pts.unsqueeze(-3					)) ** 2     ,		dim=-1					)					)
			return torch.bucketize(__snake_case     ,		__snake_case					)




def  a_		(							__snake_case     ,		__snake_case					)	->			dict:

			'''simple docstring'''


			UpperCamelCase_     				= dicts[0]
			UpperCamelCase_     				= {}
			for k, v in first.items():
						UpperCamelCase_     				= [d[k] for d in dicts]
						if isinstance(__snake_case     ,		__snake_case					):
									UpperCamelCase_     				= dict_multimap(__snake_case     ,		__snake_case					)
						else:
									UpperCamelCase_     				= fn(__snake_case					)

			return new_dict




def  a_		(							__snake_case     ,		__snake_case					)	->			torch.Tensor:

			'''simple docstring'''


			UpperCamelCase_     				= v_bins.view(((1,) * len(x.shape					)) + (len(__snake_case					),)					)
			UpperCamelCase_     				= x[..., None] - reshaped_bins
			UpperCamelCase_     				= torch.argmin(torch.abs(__snake_case					)     ,		dim=-1					)
			return nn.functional.one_hot(__snake_case     ,		num_classes=len(__snake_case					)					).float()




def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case = 0     ,		__snake_case = 0					)	->			torch.Tensor:

			'''simple docstring'''


			UpperCamelCase_     				= []
			for i, s in enumerate(data.shape[:no_batch_dims]					):
						UpperCamelCase_     				= torch.arange(__snake_case					)
						UpperCamelCase_     				= r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape					) - i - 1)))					)
						ranges.append(__snake_case					)

			UpperCamelCase_     				= [slice(__snake_case					) for _ in range(len(data.shape					) - no_batch_dims					)]
			UpperCamelCase_     				= inds
			ranges.extend(__snake_case					)
			# Matt note: Editing this to get around the behaviour of using a list as an array index changing
			# in recent Numpy versions
			return data[tuple(__snake_case					)]


__a			:	List[Any]							     =		TypeVar("""T""")




def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case					)	->			Dict[Any, Union[dict, list, tuple, Any]]:

			'''simple docstring'''


			UpperCamelCase_     				= {}
			for k, v in dic.items():
						if isinstance(__snake_case     ,		__snake_case					):
									UpperCamelCase_     				= dict_map(__snake_case     ,		__snake_case     ,		__snake_case					)
						else:
									UpperCamelCase_     				= tree_map(__snake_case     ,		__snake_case     ,		__snake_case					)

			return new_dict




@overload
def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case					)	->			Any:

			'''simple docstring'''


			...




@overload
def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case					)	->			dict:

			'''simple docstring'''


			...




@overload
def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case					)	->			list:

			'''simple docstring'''


			...




@overload
def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case					)	->			tuple:

			'''simple docstring'''


			...




def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case					)	->			Any:

			'''simple docstring'''


			if isinstance(__snake_case     ,		__snake_case					):
						return dict_map(__snake_case     ,		__snake_case     ,		__snake_case					)
			elif isinstance(__snake_case     ,		__snake_case					):
						return [tree_map(__snake_case     ,		__snake_case     ,		__snake_case					) for x in tree]
			elif isinstance(__snake_case     ,		__snake_case					):
						return tuple(tree_map(__snake_case     ,		__snake_case     ,		__snake_case					) for x in tree					)
			elif isinstance(__snake_case     ,		__snake_case					):
						return fn(__snake_case					)
			else:
						print(type(__snake_case					)					)
						raise ValueError('Not supported'					)





__a			:	Tuple							     =		partial(tree_map, leaf_type=torch.Tensor)
