





"""simple docstring"""




from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import numpy
		import tensorflow as tf

		from transformers import (
		    TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
		    BertConfig,
		    DPRConfig,
		    TFDPRContextEncoder,
		    TFDPRQuestionEncoder,
		    TFDPRReader,
		)


class 			__UpperCamelCase							:






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






							def        __a  (  self )      ->					Optional[int]:
								a :						List[Any]      =							ids_tensor([self.batch_size, self.seq_length] ,   self.vocab_size )

								a :						Optional[int]      =							None
								if self.use_input_mask:
									# follow test_modeling_tf_ctrl.py
									a :						Tuple      =							random_attention_mask([self.batch_size, self.seq_length] )

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

								a :						int      =							None
								a :						Tuple      =							None
								a :						str      =							None
								if self.use_labels:
									a :						List[str]      =							ids_tensor([self.batch_size] ,   self.type_sequence_label_size )
									a :						Dict      =							ids_tensor([self.batch_size, self.seq_length] ,   self.num_labels )
									a :						Any      =							ids_tensor([self.batch_size] ,   self.num_choices )

								a :						int      =							BertConfig(
								    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 ,   is_decoder=lowerCAmelCase__ ,   initializer_range=self.initializer_range ,   )
								a :						Optional[Any]      =							DPRConfig(projection_dim=self.projection_dim ,   **config.to_dict() )

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






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					Dict:
								a :						List[str]      =							TFDPRContextEncoder(config=lowerCAmelCase__ )
								a :						Any      =							model(lowerCAmelCase__ ,   attention_mask=lowerCAmelCase__ ,   token_type_ids=lowerCAmelCase__ )
								a :						Optional[Any]      =							model(lowerCAmelCase__ ,   token_type_ids=lowerCAmelCase__ )
								a :						Tuple      =							model(lowerCAmelCase__ )
								self.parent.assertEqual(result.pooler_output.shape ,   (self.batch_size, self.projection_dim or self.hidden_size) )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					Optional[int]:
								a :						Tuple      =							TFDPRQuestionEncoder(config=lowerCAmelCase__ )
								a :						List[str]      =							model(lowerCAmelCase__ ,   attention_mask=lowerCAmelCase__ ,   token_type_ids=lowerCAmelCase__ )
								a :						Optional[int]      =							model(lowerCAmelCase__ ,   token_type_ids=lowerCAmelCase__ )
								a :						List[Any]      =							model(lowerCAmelCase__ )
								self.parent.assertEqual(result.pooler_output.shape ,   (self.batch_size, self.projection_dim or self.hidden_size) )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					str:
								a :						Optional[int]      =							TFDPRReader(config=lowerCAmelCase__ )
								a :						Optional[int]      =							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        __a  (  self )      ->					Tuple:
								a :						Optional[Any]      =							self.prepare_config_and_inputs()
								(
								    (
								    a
								),			(
								    a
								),			(
								    a
								),			(
								    a
								),			(
								    a
								),			(
								    a
								),			(
								    a
								),			
								) :						Union[str, Any]      =							config_and_inputs
								a :						Dict      =							{"input_ids": input_ids}
								return config, inputs_dict


@require_tf
class 			__UpperCamelCase							(		a__						,			a__						,			unittest.TestCase		):
							lowerCamelCase  :   Optional[Any]    =(
							    (
							        TFDPRContextEncoder,
							        TFDPRQuestionEncoder,
							        TFDPRReader,
							    )
							    if is_tf_available()
							    else ()
							)
							lowerCamelCase  :   Any    ={"""feature-extraction""": TFDPRQuestionEncoder} if is_tf_available() else {}

							lowerCamelCase  :   int    =False
							lowerCamelCase  :   Optional[int]    =False
							lowerCamelCase  :   str    =False
							lowerCamelCase  :   Any    =False
							lowerCamelCase  :   Optional[int]    =False






							def        __a  (  self )      ->					Union[str, Any]:
								a :						Tuple      =							TFDPRModelTester(self )
								a :						Optional[int]      =							ConfigTester(self ,   config_class=lowerCAmelCase__ ,   hidden_size=37 )






							def        __a  (  self )      ->					str:
								self.config_tester.run_common_tests()






							def        __a  (  self )      ->					List[Any]:
								a :						str      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_context_encoder(*lowerCAmelCase__ )






							def        __a  (  self )      ->					Optional[Any]:
								a :						Dict      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_question_encoder(*lowerCAmelCase__ )






							def        __a  (  self )      ->					List[Any]:
								a :						int      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_reader(*lowerCAmelCase__ )






							@slow
							def        __a  (  self )      ->					Any:
								for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									a :						Union[str, Any]      =							TFDPRContextEncoder.from_pretrained(lowerCAmelCase__ )
									self.assertIsNotNone(lowerCAmelCase__ )

								for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									a :						Optional[int]      =							TFDPRContextEncoder.from_pretrained(lowerCAmelCase__ )
									self.assertIsNotNone(lowerCAmelCase__ )

								for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									a :						str      =							TFDPRQuestionEncoder.from_pretrained(lowerCAmelCase__ )
									self.assertIsNotNone(lowerCAmelCase__ )

								for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									a :						Tuple      =							TFDPRReader.from_pretrained(lowerCAmelCase__ )
									self.assertIsNotNone(lowerCAmelCase__ )


@require_tf
class 			__UpperCamelCase							(		unittest.TestCase		):






							@slow
							def        __a  (  self )      ->					List[Any]:
								a :						List[str]      =							TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base" )

								a :						Optional[int]      =							tf.constant(
								    [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]] )  # [CLS] hello, is my dog cute? [SEP]
								a :						Tuple      =							model(lowerCAmelCase__ )[0]  # embedding shape = (1, 768)
								# compare the actual values for a slice.
								a :						List[str]      =							tf.constant(
								    [
								        [
								            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,
								        ]
								    ] )
								self.assertTrue(numpy.allclose(output[:, :10].numpy() ,   expected_slice.numpy() ,   atol=1E-4 ) )









"""simple docstring"""




from numpy import exp, pi, sqrt






def  _SCREAMING_SNAKE_CASE						(      _lowercase		:       Tuple       ,			_lowercase		:       float = 0.0       ,			_lowercase		:       float = 1.0   )					->int:






	'''simple docstring'''






	return 1 / sqrt(2 * pi * sigma**2   ) * exp(-((x - mu) ** 2) / (2 * sigma**2)   )


if __name__ == "__main__":
		import doctest

		doctest.testmod()


