





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







					"""simple docstring"""






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







							"""simple docstring"""
							__UpperCAmelCase	:							int     =     parent
							__UpperCAmelCase	:							Union[str, Any]     =     batch_size
							__UpperCAmelCase	:							int     =     seq_length
							__UpperCAmelCase	:							Optional[int]     =     is_training
							__UpperCAmelCase	:							Optional[Any]     =     use_input_mask
							__UpperCAmelCase	:							Dict     =     use_token_type_ids
							__UpperCAmelCase	:							str     =     use_labels
							__UpperCAmelCase	:							Dict     =     vocab_size
							__UpperCAmelCase	:							List[str]     =     hidden_size
							__UpperCAmelCase	:							Optional[Any]     =     num_hidden_layers
							__UpperCAmelCase	:							Optional[int]     =     num_attention_heads
							__UpperCAmelCase	:							str     =     intermediate_size
							__UpperCAmelCase	:							List[Any]     =     hidden_act
							__UpperCAmelCase	:							Optional[int]     =     hidden_dropout_prob
							__UpperCAmelCase	:							List[Any]     =     attention_probs_dropout_prob
							__UpperCAmelCase	:							List[Any]     =     max_position_embeddings
							__UpperCAmelCase	:							int     =     type_vocab_size
							__UpperCAmelCase	:							Optional[int]     =     type_sequence_label_size
							__UpperCAmelCase	:							Union[str, Any]     =     initializer_range
							__UpperCAmelCase	:							Union[str, Any]     =     num_labels
							__UpperCAmelCase	:							Any     =     num_choices
							__UpperCAmelCase	:							Union[str, Any]     =     scope
							__UpperCAmelCase	:							Optional[int]     =     projection_dim
					def 		lowerCamelCase_   (   self       :      Dict		):







							"""simple docstring"""
							__UpperCAmelCase	:							Dict     =     ids_tensor([self.batch_size, self.seq_length]		,	self.vocab_size		)

							__UpperCAmelCase	:							Any     =     None
							if self.use_input_mask:
									__UpperCAmelCase	:							Optional[int]     =     random_attention_mask([self.batch_size, self.seq_length]		)

							__UpperCAmelCase	:							Tuple     =     None
							if self.use_token_type_ids:
									__UpperCAmelCase	:							List[Any]     =     ids_tensor([self.batch_size, self.seq_length]		,	self.type_vocab_size		)

							__UpperCAmelCase	:							Tuple     =     None
							__UpperCAmelCase	:							Union[str, Any]     =     None
							__UpperCAmelCase	:							int     =     None
							if self.use_labels:
									__UpperCAmelCase	:							Optional[int]     =     ids_tensor([self.batch_size]		,	self.type_sequence_label_size		)
									__UpperCAmelCase	:							Optional[int]     =     ids_tensor([self.batch_size, self.seq_length]		,	self.num_labels		)
									__UpperCAmelCase	:							Any     =     ids_tensor([self.batch_size]		,	self.num_choices		)

							__UpperCAmelCase	:							Optional[int]     =     self.get_config()

							return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
					def 		lowerCamelCase_   (   self       :      Dict		):







							"""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 		lowerCamelCase_   (   self       :      Dict		,	UpperCAmelCase_       :      List[str]		,	UpperCAmelCase_       :      Union[str, Any]		,	UpperCAmelCase_       :      Union[str, Any]		,	UpperCAmelCase_       :      List[Any]		,	UpperCAmelCase_       :      str		,	UpperCAmelCase_       :      Dict		,	UpperCAmelCase_       :      Optional[int]		):







							"""simple docstring"""
							__UpperCAmelCase	:							List[Any]     =     DPRContextEncoder(config=lowerCamelCase__		)
							model.to(lowerCamelCase__		)
							model.eval()
							__UpperCAmelCase	:							Any     =     model(lowerCamelCase__		,	attention_mask=lowerCamelCase__		,	token_type_ids=lowerCamelCase__		)
							__UpperCAmelCase	:							Optional[Any]     =     model(lowerCamelCase__		,	token_type_ids=lowerCamelCase__		)
							__UpperCAmelCase	:							Optional[Any]     =     model(lowerCamelCase__		)
							self.parent.assertEqual(result.pooler_output.shape		,	(self.batch_size, self.projection_dim or self.hidden_size)		)
					def 		lowerCamelCase_   (   self       :      int		,	UpperCAmelCase_       :      Any		,	UpperCAmelCase_       :      Any		,	UpperCAmelCase_       :      str		,	UpperCAmelCase_       :      Any		,	UpperCAmelCase_       :      Tuple		,	UpperCAmelCase_       :      int		,	UpperCAmelCase_       :      List[Any]		):







							"""simple docstring"""
							__UpperCAmelCase	:							List[str]     =     DPRQuestionEncoder(config=lowerCamelCase__		)
							model.to(lowerCamelCase__		)
							model.eval()
							__UpperCAmelCase	:							Optional[int]     =     model(lowerCamelCase__		,	attention_mask=lowerCamelCase__		,	token_type_ids=lowerCamelCase__		)
							__UpperCAmelCase	:							int     =     model(lowerCamelCase__		,	token_type_ids=lowerCamelCase__		)
							__UpperCAmelCase	:							Optional[Any]     =     model(lowerCamelCase__		)
							self.parent.assertEqual(result.pooler_output.shape		,	(self.batch_size, self.projection_dim or self.hidden_size)		)
					def 		lowerCamelCase_   (   self       :      Optional[Any]		,	UpperCAmelCase_       :      int		,	UpperCAmelCase_       :      Any		,	UpperCAmelCase_       :      Optional[Any]		,	UpperCAmelCase_       :      Optional[Any]		,	UpperCAmelCase_       :      str		,	UpperCAmelCase_       :      Optional[int]		,	UpperCAmelCase_       :      List[Any]		):







							"""simple docstring"""
							__UpperCAmelCase	:							int     =     DPRReader(config=lowerCamelCase__		)
							model.to(lowerCamelCase__		)
							model.eval()
							__UpperCAmelCase	:							List[str]     =     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 		lowerCamelCase_   (   self       :      List[str]		):







							"""simple docstring"""
							__UpperCAmelCase	:							Tuple     =     self.prepare_config_and_inputs()
							(
							    __UpperCAmelCase					
							)	:							Tuple     =     config_and_inputs
							__UpperCAmelCase	:							List[str]     =     {'''input_ids''': input_ids}
							return config, inputs_dict






@require_torch
class      SCREAMING_SNAKE_CASE__      (   __lowerCAmelCase							,__lowerCAmelCase							,unittest.TestCase		):







					"""simple docstring"""






					SCREAMING_SNAKE_CASE							=	(
					    (
					        DPRContextEncoder,
					        DPRQuestionEncoder,
					        DPRReader,
					    )
					    if is_torch_available()
					    else ()
					)
					SCREAMING_SNAKE_CASE							=	{"feature-extraction": DPRQuestionEncoder} if is_torch_available() else {}

					SCREAMING_SNAKE_CASE							=	False
					SCREAMING_SNAKE_CASE							=	False  # why?
					SCREAMING_SNAKE_CASE							=	False
					SCREAMING_SNAKE_CASE							=	False
					def 		lowerCamelCase_   (   self       :      str		):







							"""simple docstring"""
							__UpperCAmelCase	:							int     =     DPRModelTester(self		)
							__UpperCAmelCase	:							Union[str, Any]     =     ConfigTester(self		,	config_class=lowerCamelCase__		,	hidden_size=37		)
					def 		lowerCamelCase_   (   self       :      List[Any]		):







							"""simple docstring"""
							self.config_tester.run_common_tests()
					def 		lowerCamelCase_   (   self       :      int		):







							"""simple docstring"""
							__UpperCAmelCase	:							int     =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_context_encoder(*lowerCamelCase__		)
					def 		lowerCamelCase_   (   self       :      List[str]		):







							"""simple docstring"""
							__UpperCAmelCase	:							Optional[Any]     =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_question_encoder(*lowerCamelCase__		)
					def 		lowerCamelCase_   (   self       :      Tuple		):







							"""simple docstring"""
							__UpperCAmelCase	:							Optional[Any]     =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_reader(*lowerCamelCase__		)
					def 		lowerCamelCase_   (   self       :      Dict		):







							"""simple docstring"""
							__UpperCAmelCase	:							List[str]     =     self.model_tester.prepare_config_and_inputs()[0]

							__UpperCAmelCase	:							str     =     DPRQuestionEncoder(config=lowerCamelCase__		)
							model.to(lowerCamelCase__		)
							model.eval()

							with tempfile.TemporaryDirectory() as tmp_dirname:
									model.save_pretrained(lowerCamelCase__		)
									__UpperCAmelCase	:							Optional[Any]     =     DPRQuestionEncoder.from_pretrained(lowerCamelCase__		,	projection_dim=512		)

							self.assertIsNotNone(lowerCamelCase__		)






					@slow
					def 		lowerCamelCase_   (   self       :      Tuple		):







							"""simple docstring"""






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

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

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

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






@require_torch
class      SCREAMING_SNAKE_CASE__      (   unittest.TestCase		):







					"""simple docstring"""






					@slow
					def 		lowerCamelCase_   (   self       :      List[Any]		):







							"""simple docstring"""
							__UpperCAmelCase	:							Tuple     =     DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"		,	return_dict=lowerCamelCase__		)
							model.to(lowerCamelCase__		)

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






					@slow
					def 		lowerCamelCase_   (   self       :      List[Any]		):







							"""simple docstring"""
							__UpperCAmelCase	:							Dict     =     DPRReaderTokenizer.from_pretrained("facebook/dpr-reader-single-nq-base"		)
							__UpperCAmelCase	:							Optional[Any]     =     DPRReader.from_pretrained("facebook/dpr-reader-single-nq-base"		)
							model.to(lowerCamelCase__		)

							__UpperCAmelCase	:							Optional[Any]     =     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__		)

							__UpperCAmelCase	:							int     =     model(**lowerCamelCase__		)

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

							__UpperCAmelCase	:							str     =     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=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		)		)








import numpy as np

def       _a	(					SCREAMING_SNAKE_CASE						:					Tuple  ):







 """simple docstring"""



 UpperCamelCase__       :     str 							= np.exp(SCREAMING_SNAKE_CASE  )

 # Add up the all the exponentials
 UpperCamelCase__       :     Optional[int] 							= np.sum(SCREAMING_SNAKE_CASE  )

 # Divide every exponent by the sum of all exponents
 UpperCamelCase__       :     Optional[Any] 							= exponent_vector / sum_of_exponents

 return softmax_vector


if __name__ == "__main__":
       print(softmax((0,)))


