


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







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







						def __a						(							self   :    List[str]		):
								A					       = ids_tensor([self.batch_size, self.seq_length]			,			self.vocab_size		)

								A					       = None
								if self.use_input_mask:
										# follow test_modeling_tf_ctrl.py
										A					       = random_attention_mask([self.batch_size, self.seq_length]		)

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

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

								A					       = 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=_lowercase			,			initializer_range=self.initializer_range			,			)
								A					       = 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   :    int			,			_lowercase   :    Tuple			,			_lowercase   :    Any			,			_lowercase   :    List[str]			,			_lowercase   :    Tuple			,			_lowercase   :    str			,			_lowercase   :    Dict			,			_lowercase   :    Tuple		):
								A					       = TFDPRContextEncoder(config=_lowercase		)
								A					       = model(_lowercase			,			attention_mask=_lowercase			,			token_type_ids=_lowercase		)
								A					       = model(_lowercase			,			token_type_ids=_lowercase		)
								A					       = model(_lowercase		)
								self.parent.assertEqual(result.pooler_output.shape			,			(self.batch_size, self.projection_dim or self.hidden_size)		)







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







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



@require_tf
class 			lowerCamelCase__						(   UpperCAmelCase_							,    UpperCAmelCase_							,    unittest.TestCase			):
						lowerCAmelCase       =     (
						    (
						        TFDPRContextEncoder,
						        TFDPRQuestionEncoder,
						        TFDPRReader,
						    )
						    if is_tf_available()
						    else ()
						)
						lowerCAmelCase       =     {"""feature-extraction""": TFDPRQuestionEncoder} if is_tf_available() else {}

						lowerCAmelCase       =     False
						lowerCAmelCase       =     False
						lowerCAmelCase       =     False
						lowerCAmelCase       =     False
						lowerCAmelCase       =     False







						def __a						(							self   :    int		):
								A					       = TFDPRModelTester(self		)
								A					       = ConfigTester(self			,			config_class=_lowercase			,			hidden_size=37		)







						def __a						(							self   :    int		):
								self.config_tester.run_common_tests()







						def __a						(							self   :    List[str]		):
								A					       = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_context_encoder(*_lowercase		)







						def __a						(							self   :    int		):
								A					       = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_question_encoder(*_lowercase		)







						def __a						(							self   :    List[str]		):
								A					       = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_reader(*_lowercase		)







						@slow
						def __a						(							self   :    str		):
								for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										A					       = TFDPRContextEncoder.from_pretrained(_lowercase		)
										self.assertIsNotNone(_lowercase		)

								for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										A					       = TFDPRContextEncoder.from_pretrained(_lowercase		)
										self.assertIsNotNone(_lowercase		)

								for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										A					       = TFDPRQuestionEncoder.from_pretrained(_lowercase		)
										self.assertIsNotNone(_lowercase		)

								for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										A					       = TFDPRReader.from_pretrained(_lowercase		)
										self.assertIsNotNone(_lowercase		)



@require_tf
class 			lowerCamelCase__						(   unittest.TestCase			):







						@slow
						def __a						(							self   :    Union[str, Any]		):
								A					       = TFDPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'		)

								A					       = tf.constant(
								    [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]]		)  # [CLS] hello, is my dog cute? [SEP]
								A					       = model(_lowercase		)[0]  # embedding shape = (1, 768)
								# compare the actual values for a slice.
								A					       = tf.constant(
								    [
								        [
								            0.0_3_2_3_6_2_5_3,
								            0.1_2_7_5_3_3_3_5,
								            0.1_6_8_1_8_5_0_9,
								            0.0_0_2_7_9_7_8_6,
								            0.3_8_9_6_9_3_3,
								            0.2_4_2_6_4_9_4_5,
								            0.2_1_7_8_9_7_1,
								            -0.0_2_3_3_5_2_2_7,
								            -0.0_8_4_8_1_9_5_9,
								            -0.1_4_3_2_4_1_1_7,
								        ]
								    ]		)
								self.assertTrue(numpy.allclose(output[:, :10].numpy()			,			expected_slice.numpy()			,			atol=1e-4		)		)









"""simple docstring"""


from statistics import mean, stdev







def  __snake_case       (   UpperCamelCase__ ,			UpperCamelCase__ = 3	)      ->		list:







		"""simple docstring"""





		A					       = min(UpperCamelCase__	)
		A					       = max(UpperCamelCase__	)
		# normalize data
		return [round((x - x_min) / (x_max - x_min) ,			UpperCamelCase__	) for x in data]







def  __snake_case       (   UpperCamelCase__ ,			UpperCamelCase__ = 3	)      ->		list:







		"""simple docstring"""





		A					       = mean(UpperCamelCase__	)
		A					       = stdev(UpperCamelCase__	)
		# standardize data
		return [round((x - mu) / (sigma) ,			UpperCamelCase__	) for x in data]





