




from __future__ import annotations




def 		lowerCAmelCase_   (	lowerCamelCase   ):
				if not nums:
								raise ValueError("""List is empty"""   )
				return sum(lowerCamelCase   ) / len(lowerCamelCase   )


if __name__ == "__main__":
							import doctest

							doctest.testmod()










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

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

										__magic_name__	:	Optional[int]								=parent
										__magic_name__	:	int								=batch_size
										__magic_name__	:	Any								=seq_length
										__magic_name__	:	List[str]								=is_training
										__magic_name__	:	Any								=use_input_mask
										__magic_name__	:	Union[str, Any]								=use_token_type_ids
										__magic_name__	:	Union[str, Any]								=use_labels
										__magic_name__	:	Optional[Any]								=vocab_size
										__magic_name__	:	Optional[Any]								=hidden_size
										__magic_name__	:	Optional[Any]								=num_hidden_layers
										__magic_name__	:	Optional[Any]								=num_attention_heads
										__magic_name__	:	Tuple								=intermediate_size
										__magic_name__	:	Tuple								=hidden_act
										__magic_name__	:	Tuple								=hidden_dropout_prob
										__magic_name__	:	Any								=attention_probs_dropout_prob
										__magic_name__	:	Union[str, Any]								=max_position_embeddings
										__magic_name__	:	int								=type_vocab_size
										__magic_name__	:	Optional[Any]								=type_sequence_label_size
										__magic_name__	:	Union[str, Any]								=initializer_range
										__magic_name__	:	Optional[Any]								=num_labels
										__magic_name__	:	Any								=num_choices
										__magic_name__	:	Optional[Any]								=scope
										__magic_name__	:	str								=projection_dim

						def     A__						(		self   :Dict ):
										'''simple docstring'''

										__magic_name__	:	Optional[int]								=ids_tensor([self.batch_size, self.seq_length]    , self.vocab_size )

										__magic_name__	:	Union[str, Any]								=None
										if self.use_input_mask:
														# follow test_modeling_tf_ctrl.py
														__magic_name__	:	List[Any]								=random_attention_mask([self.batch_size, self.seq_length] )

										__magic_name__	:	Optional[int]								=None
										if self.use_token_type_ids:
														__magic_name__	:	str								=ids_tensor([self.batch_size, self.seq_length]    , self.type_vocab_size )

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

										__magic_name__	:	Optional[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=__snake_case    , initializer_range=self.initializer_range    , )
										__magic_name__	:	Dict								=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   :List[Any]    , __snake_case   :List[Any]    , __snake_case   :List[str]    , __snake_case   :Any    , __snake_case   :Any    , __snake_case   :Optional[Any]    , __snake_case   :Optional[Any]    , __snake_case   :List[str] ):
										'''simple docstring'''

										__magic_name__	:	List[Any]								=TFDPRContextEncoder(config=__snake_case )
										__magic_name__	:	Union[str, Any]								=model(__snake_case    , attention_mask=__snake_case    , token_type_ids=__snake_case )
										__magic_name__	:	str								=model(__snake_case    , token_type_ids=__snake_case )
										__magic_name__	:	Optional[int]								=model(__snake_case )
										self.parent.assertEqual(result.pooler_output.shape    , (self.batch_size, self.projection_dim or self.hidden_size) )

						def     A__						(		self   :int    , __snake_case   :str    , __snake_case   :Tuple    , __snake_case   :Optional[Any]    , __snake_case   :Optional[Any]    , __snake_case   :Tuple    , __snake_case   :List[Any]    , __snake_case   :int ):
										'''simple docstring'''

										__magic_name__	:	int								=TFDPRQuestionEncoder(config=__snake_case )
										__magic_name__	:	Union[str, Any]								=model(__snake_case    , attention_mask=__snake_case    , token_type_ids=__snake_case )
										__magic_name__	:	List[str]								=model(__snake_case    , token_type_ids=__snake_case )
										__magic_name__	:	Union[str, Any]								=model(__snake_case )
										self.parent.assertEqual(result.pooler_output.shape    , (self.batch_size, self.projection_dim or self.hidden_size) )

						def     A__						(		self   :List[Any]    , __snake_case   :List[str]    , __snake_case   :Optional[int]    , __snake_case   :str    , __snake_case   :Any    , __snake_case   :str    , __snake_case   :Optional[Any]    , __snake_case   :Dict ):
										'''simple docstring'''

										__magic_name__	:	Optional[int]								=TFDPRReader(config=__snake_case )
										__magic_name__	:	List[str]								=model(__snake_case    , attention_mask=__snake_case )

										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[Any] ):
										'''simple docstring'''

										__magic_name__	:	str								=self.prepare_config_and_inputs()
										(
										    (
										    __magic_name__
										)				, (
										    __magic_name__
										)				, (
										    __magic_name__
										)				, (
										    __magic_name__
										)				, (
										    __magic_name__
										)				, (
										    __magic_name__
										)				, (
										    __magic_name__
										)				, 
										)	:	Union[str, Any]								=config_and_inputs
										__magic_name__	:	List[str]								={"""input_ids""": input_ids}
										return config, inputs_dict




@require_tf
class 		__A      (			UpperCamelCase__					,						UpperCamelCase__					,						unittest.TestCase			):
						UpperCamelCase     						=					(
						    (
						        TFDPRContextEncoder,
						        TFDPRQuestionEncoder,
						        TFDPRReader,
						    )
						    if is_tf_available()
						    else ()
						)
						UpperCamelCase     						=					{"""feature-extraction""": TFDPRQuestionEncoder} if is_tf_available() else {}

						UpperCamelCase     						=					False
						UpperCamelCase     						=					False
						UpperCamelCase     						=					False
						UpperCamelCase     						=					False
						UpperCamelCase     						=					False

						def     A__						(		self   :Union[str, Any] ):
										'''simple docstring'''

										__magic_name__	:	Union[str, Any]								=TFDPRModelTester(self )
										__magic_name__	:	Optional[Any]								=ConfigTester(self    , config_class=__snake_case    , hidden_size=37 )

						def     A__						(		self   :Dict ):
										'''simple docstring'''

										self.config_tester.run_common_tests()

						def     A__						(		self   :Tuple ):
										'''simple docstring'''

										__magic_name__	:	str								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_dpr_context_encoder(*__snake_case )

						def     A__						(		self   :List[Any] ):
										'''simple docstring'''

										__magic_name__	:	int								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_dpr_question_encoder(*__snake_case )

						def     A__						(		self   :Any ):
										'''simple docstring'''

										__magic_name__	:	Optional[int]								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_dpr_reader(*__snake_case )


						@slow
						def     A__						(		self   :Tuple ):
										'''simple docstring'''

										for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														__magic_name__	:	Dict								=TFDPRContextEncoder.from_pretrained(__snake_case )
														self.assertIsNotNone(__snake_case )

										for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														__magic_name__	:	Dict								=TFDPRContextEncoder.from_pretrained(__snake_case )
														self.assertIsNotNone(__snake_case )

										for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														__magic_name__	:	Union[str, Any]								=TFDPRQuestionEncoder.from_pretrained(__snake_case )
														self.assertIsNotNone(__snake_case )

										for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														__magic_name__	:	List[str]								=TFDPRReader.from_pretrained(__snake_case )
														self.assertIsNotNone(__snake_case )




@require_tf
class 		__A      (			unittest.TestCase			):

						@slow
						def     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										__magic_name__	:	int								=TFDPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base""" )

										__magic_name__	:	int								=tf.constant(
										    [[1_01, 75_92, 10_10, 20_03, 20_26, 38_99, 1_01_40, 10_29, 1_02]] )  # [CLS] hello, is my dog cute? [SEP]
										__magic_name__	:	Optional[Any]								=model(__snake_case )[0]  # embedding shape = (1, 768)
										# compare the actual values for a slice.
										__magic_name__	:	Tuple								=tf.constant(
										    [
										        [
										            0.03236253,
										            0.12753335,
										            0.16818509,
										            0.00279786,
										            0.3896933,
										            0.24264945,
										            0.2178971,
										            -0.02335227,
										            -0.08481959,
										            -0.14324117,
										        ]
										    ] )
										self.assertTrue(numpy.allclose(output[:, :10].numpy()    , expected_slice.numpy()    , atol=1E-4 ) )




