


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





      '''simple docstring'''






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

             '''simple docstring'''







             lowercase__      =					parent
             lowercase__      =					batch_size
             lowercase__      =					seq_length
             lowercase__      =					is_training
             lowercase__      =					use_input_mask
             lowercase__      =					use_token_type_ids
             lowercase__      =					use_labels
             lowercase__      =					vocab_size
             lowercase__      =					hidden_size
             lowercase__      =					num_hidden_layers
             lowercase__      =					num_attention_heads
             lowercase__      =					intermediate_size
             lowercase__      =					hidden_act
             lowercase__      =					hidden_dropout_prob
             lowercase__      =					attention_probs_dropout_prob
             lowercase__      =					max_position_embeddings
             lowercase__      =					type_vocab_size
             lowercase__      =					type_sequence_label_size
             lowercase__      =					initializer_range
             lowercase__      =					num_labels
             lowercase__      =					num_choices
             lowercase__      =					scope
             lowercase__      =					projection_dim



      def  UpperCamelCase__						(self       :       Tuple					):

             '''simple docstring'''







             lowercase__      =					ids_tensor([self.batch_size, self.seq_length]  ,		self.vocab_size					)

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

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

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

             lowercase__      =					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=UpperCamelCase  ,		initializer_range=self.initializer_range  ,		)
             lowercase__      =					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  UpperCamelCase__						(self       :       Any  ,		UpperCamelCase       :       Optional[Any]  ,		UpperCamelCase       :       Any  ,		UpperCamelCase       :       str  ,		UpperCamelCase       :       Any  ,		UpperCamelCase       :       Dict  ,		UpperCamelCase       :       str  ,		UpperCamelCase       :       Optional[Any]					):

             '''simple docstring'''







             lowercase__      =					TFDPRContextEncoder(config=UpperCamelCase					)
             lowercase__      =					model(UpperCamelCase  ,		attention_mask=UpperCamelCase  ,		token_type_ids=UpperCamelCase					)
             lowercase__      =					model(UpperCamelCase  ,		token_type_ids=UpperCamelCase					)
             lowercase__      =					model(UpperCamelCase					)
             self.parent.assertEqual(result.pooler_output.shape  ,		(self.batch_size, self.projection_dim or self.hidden_size)					)



      def  UpperCamelCase__						(self       :       int  ,		UpperCamelCase       :       int  ,		UpperCamelCase       :       Union[str, Any]  ,		UpperCamelCase       :       Union[str, Any]  ,		UpperCamelCase       :       Dict  ,		UpperCamelCase       :       List[Any]  ,		UpperCamelCase       :       int  ,		UpperCamelCase       :       Optional[int]					):

             '''simple docstring'''







             lowercase__      =					TFDPRQuestionEncoder(config=UpperCamelCase					)
             lowercase__      =					model(UpperCamelCase  ,		attention_mask=UpperCamelCase  ,		token_type_ids=UpperCamelCase					)
             lowercase__      =					model(UpperCamelCase  ,		token_type_ids=UpperCamelCase					)
             lowercase__      =					model(UpperCamelCase					)
             self.parent.assertEqual(result.pooler_output.shape  ,		(self.batch_size, self.projection_dim or self.hidden_size)					)



      def  UpperCamelCase__						(self       :       int  ,		UpperCamelCase       :       Any  ,		UpperCamelCase       :       Optional[int]  ,		UpperCamelCase       :       Dict  ,		UpperCamelCase       :       List[Any]  ,		UpperCamelCase       :       List[Any]  ,		UpperCamelCase       :       List[str]  ,		UpperCamelCase       :       Union[str, Any]					):

             '''simple docstring'''







             lowercase__      =					TFDPRReader(config=UpperCamelCase					)
             lowercase__      =					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  UpperCamelCase__						(self       :       List[str]					):

             '''simple docstring'''







             lowercase__      =					self.prepare_config_and_inputs()
             (
                 (
                 lowercase__
             )     ,(
                 lowercase__
             )     ,(
                 lowercase__
             )     ,(
                 lowercase__
             )     ,(
                 lowercase__
             )     ,(
                 lowercase__
             )     ,(
                 lowercase__
             )     ,
             )      =					config_and_inputs
             lowercase__      =					{'''input_ids''': input_ids}
             return config, inputs_dict






@require_tf
class     __lowerCAmelCase		(lowercase_    ,	lowercase_    ,	unittest.TestCase	):





      '''simple docstring'''






      lowerCAmelCase__   :	List[Any]       =				(
          (
              TFDPRContextEncoder,
              TFDPRQuestionEncoder,
              TFDPRReader,
          )
          if is_tf_available()
          else ()
      )
      lowerCAmelCase__   :	Optional[int]       =				{"""feature-extraction""": TFDPRQuestionEncoder} if is_tf_available() else {}

      lowerCAmelCase__   :	Any       =				False
      lowerCAmelCase__   :	int       =				False
      lowerCAmelCase__   :	Union[str, Any]       =				False
      lowerCAmelCase__   :	List[str]       =				False
      lowerCAmelCase__   :	List[Any]       =				False



      def  UpperCamelCase__						(self       :       Dict					):

             '''simple docstring'''







             lowercase__      =					TFDPRModelTester(self					)
             lowercase__      =					ConfigTester(self  ,		config_class=UpperCamelCase  ,		hidden_size=37					)



      def  UpperCamelCase__						(self       :       Dict					):

             '''simple docstring'''







             self.config_tester.run_common_tests()



      def  UpperCamelCase__						(self       :       List[Any]					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_dpr_context_encoder(*UpperCamelCase					)



      def  UpperCamelCase__						(self       :       Dict					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_dpr_question_encoder(*UpperCamelCase					)



      def  UpperCamelCase__						(self       :       Dict					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_dpr_reader(*UpperCamelCase					)






      @slow
      def  UpperCamelCase__						(self       :       Tuple					):

             '''simple docstring'''







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

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

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

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






@require_tf
class     __lowerCAmelCase		(unittest.TestCase	):





      '''simple docstring'''






      @slow
      def  UpperCamelCase__						(self       :       Any					):

             '''simple docstring'''







             lowercase__      =					TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''					)

             lowercase__      =					tf.constant(
                 [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]					)  # [CLS] hello, is my dog cute? [SEP]
             lowercase__      =					model(UpperCamelCase					)[0]  # embedding shape = (1, 768)
             # compare the actual values for a slice.
             lowercase__      =					tf.constant(
                 [
                     [
                         0.03_23_62_53,
                         0.12_75_33_35,
                         0.16_81_85_09,
                         0.00_27_97_86,
                         0.3_89_69_33,
                         0.24_26_49_45,
                         0.2_17_89_71,
                         -0.02_33_52_27,
                         -0.08_48_19_59,
                         -0.14_32_41_17,
                     ]
                 ]					)
             self.assertTrue(numpy.allclose(output[:, :10].numpy()  ,		expected_slice.numpy()  ,		atol=1E-4					)					)








'''simple docstring'''

def _SCREAMING_SNAKE_CASE (A    ,					A    ,					A			)			->			list:






       """simple docstring"""







       lowercase__      =					len(A			)
       lowercase__      =					[[0] * n for i in range(A			)]
       for i in range(A			):
              lowercase__      =					y_points[i]

       for i in range(2    ,					A			):
              for j in range(A    ,					A			):
                     lowercase__      =					(
                         (xa - x_points[j - i + 1]) * q[j][i - 1]
                         - (xa - x_points[j]) * q[j - 1][i - 1]
                     ) / (x_points[j] - x_points[j - i + 1])

       return [q[n - 1][n - 1], q]


if __name__ == "__main__":
 import doctest

 doctest.testmod()




