






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[Any]				,  SCREAMING_SNAKE_CASE   :  List[str]				,  SCREAMING_SNAKE_CASE   :  List[str]=13				,  SCREAMING_SNAKE_CASE   :  str=7				,  SCREAMING_SNAKE_CASE   :  List[Any]=True				,  SCREAMING_SNAKE_CASE   :  List[str]=True				,  SCREAMING_SNAKE_CASE   :  Optional[int]=True				,  SCREAMING_SNAKE_CASE   :  int=True				,  SCREAMING_SNAKE_CASE   :  Dict=99				,  SCREAMING_SNAKE_CASE   :  List[str]=32				,  SCREAMING_SNAKE_CASE   :  Any=2				,  SCREAMING_SNAKE_CASE   :  Union[str, Any]=4				,  SCREAMING_SNAKE_CASE   :  int=37				,  SCREAMING_SNAKE_CASE   :  Tuple="gelu"				,  SCREAMING_SNAKE_CASE   :  List[Any]=0.1				,  SCREAMING_SNAKE_CASE   :  int=0.1				,  SCREAMING_SNAKE_CASE   :  str=5_12				,  SCREAMING_SNAKE_CASE   :  int=16				,  SCREAMING_SNAKE_CASE   :  List[str]=2				,  SCREAMING_SNAKE_CASE   :  Optional[int]=0.0_2				,  SCREAMING_SNAKE_CASE   :  Optional[int]=3				,  SCREAMING_SNAKE_CASE   :  Tuple=4				,  SCREAMING_SNAKE_CASE   :  List[Any]=None				,  SCREAMING_SNAKE_CASE   :  Any=0				,  ):


      '''simple docstring'''






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







 def    __lowercase   (						self   :  int		):


      '''simple docstring'''






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

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

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

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

      UpperCamelCase__     :  List[Any]										=    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=SCREAMING_SNAKE_CASE				,  initializer_range=self.initializer_range				,  )
      UpperCamelCase__     :  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    __lowercase   (						self   :  Dict				,  SCREAMING_SNAKE_CASE   :  Tuple				,  SCREAMING_SNAKE_CASE   :  List[str]				,  SCREAMING_SNAKE_CASE   :  List[str]				,  SCREAMING_SNAKE_CASE   :  Any				,  SCREAMING_SNAKE_CASE   :  int				,  SCREAMING_SNAKE_CASE   :  List[str]				,  SCREAMING_SNAKE_CASE   :  Union[str, Any]		):


      '''simple docstring'''






      UpperCamelCase__     :  str										=    TFDPRContextEncoder(config=SCREAMING_SNAKE_CASE		)
      UpperCamelCase__     :  Optional[Any]										=    model(SCREAMING_SNAKE_CASE				,  attention_mask=SCREAMING_SNAKE_CASE				,  token_type_ids=SCREAMING_SNAKE_CASE		)
      UpperCamelCase__     :  Optional[Any]										=    model(SCREAMING_SNAKE_CASE				,  token_type_ids=SCREAMING_SNAKE_CASE		)
      UpperCamelCase__     :  Any										=    model(SCREAMING_SNAKE_CASE		)
      self.parent.assertEqual(result.pooler_output.shape				,  (self.batch_size, self.projection_dim or self.hidden_size)		)







 def    __lowercase   (						self   :  int				,  SCREAMING_SNAKE_CASE   :  Any				,  SCREAMING_SNAKE_CASE   :  Tuple				,  SCREAMING_SNAKE_CASE   :  List[Any]				,  SCREAMING_SNAKE_CASE   :  Optional[Any]				,  SCREAMING_SNAKE_CASE   :  List[Any]				,  SCREAMING_SNAKE_CASE   :  List[Any]				,  SCREAMING_SNAKE_CASE   :  Any		):


      '''simple docstring'''






      UpperCamelCase__     :  int										=    TFDPRQuestionEncoder(config=SCREAMING_SNAKE_CASE		)
      UpperCamelCase__     :  Optional[int]										=    model(SCREAMING_SNAKE_CASE				,  attention_mask=SCREAMING_SNAKE_CASE				,  token_type_ids=SCREAMING_SNAKE_CASE		)
      UpperCamelCase__     :  str										=    model(SCREAMING_SNAKE_CASE				,  token_type_ids=SCREAMING_SNAKE_CASE		)
      UpperCamelCase__     :  List[str]										=    model(SCREAMING_SNAKE_CASE		)
      self.parent.assertEqual(result.pooler_output.shape				,  (self.batch_size, self.projection_dim or self.hidden_size)		)







 def    __lowercase   (						self   :  Optional[int]				,  SCREAMING_SNAKE_CASE   :  List[Any]				,  SCREAMING_SNAKE_CASE   :  Optional[Any]				,  SCREAMING_SNAKE_CASE   :  Any				,  SCREAMING_SNAKE_CASE   :  Any				,  SCREAMING_SNAKE_CASE   :  Union[str, Any]				,  SCREAMING_SNAKE_CASE   :  Tuple				,  SCREAMING_SNAKE_CASE   :  int		):


      '''simple docstring'''






      UpperCamelCase__     :  List[Any]										=    TFDPRReader(config=SCREAMING_SNAKE_CASE		)
      UpperCamelCase__     :  int										=    model(SCREAMING_SNAKE_CASE				,  attention_mask=SCREAMING_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    __lowercase   (						self   :  Tuple		):


      '''simple docstring'''






      UpperCamelCase__     :  Any										=    self.prepare_config_and_inputs()
      (
          (
          UpperCamelCase__						
      )							,	(
          UpperCamelCase__						
      )							,	(
          UpperCamelCase__						
      )							,	(
          UpperCamelCase__						
      )							,	(
          UpperCamelCase__						
      )							,	(
          UpperCamelCase__						
      )							,	(
          UpperCamelCase__						
      )							,							
      )     :  Optional[Any]										=    config_and_inputs
      UpperCamelCase__     :  Optional[Any]										=    {"input_ids": input_ids}
      return config, inputs_dict







@require_tf
class      __a    (       snake_case_			,  snake_case_			,  unittest.TestCase			):
 _lowerCAmelCase			:			List[Any]								=					(
     (
         TFDPRContextEncoder,
         TFDPRQuestionEncoder,
         TFDPRReader,
     )
     if is_tf_available()
     else ()
 )
 _lowerCAmelCase			:			Tuple								=					{'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

 _lowerCAmelCase			:			Optional[int]								=					False
 _lowerCAmelCase			:			List[Any]								=					False
 _lowerCAmelCase			:			Optional[Any]								=					False
 _lowerCAmelCase			:			List[str]								=					False
 _lowerCAmelCase			:			str								=					False







 def    __lowercase   (						self   :  Optional[Any]		):


      '''simple docstring'''






      UpperCamelCase__     :  Any										=    TFDPRModelTester(self		)
      UpperCamelCase__     :  Any										=    ConfigTester(self				,  config_class=SCREAMING_SNAKE_CASE				,  hidden_size=37		)







 def    __lowercase   (						self   :  str		):


      '''simple docstring'''






      self.config_tester.run_common_tests()







 def    __lowercase   (						self   :  Optional[int]		):


      '''simple docstring'''






      UpperCamelCase__     :  Optional[int]										=    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_dpr_context_encoder(*SCREAMING_SNAKE_CASE		)







 def    __lowercase   (						self   :  Optional[Any]		):


      '''simple docstring'''






      UpperCamelCase__     :  str										=    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_dpr_question_encoder(*SCREAMING_SNAKE_CASE		)







 def    __lowercase   (						self   :  Tuple		):


      '''simple docstring'''






      UpperCamelCase__     :  int										=    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_dpr_reader(*SCREAMING_SNAKE_CASE		)







 @slow
 def    __lowercase   (						self   :  Tuple		):


      '''simple docstring'''






      for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__     :  Any										=    TFDPRContextEncoder.from_pretrained(SCREAMING_SNAKE_CASE		)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE		)

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

      for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__     :  Optional[Any]										=    TFDPRQuestionEncoder.from_pretrained(SCREAMING_SNAKE_CASE		)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE		)

      for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__     :  Union[str, Any]										=    TFDPRReader.from_pretrained(SCREAMING_SNAKE_CASE		)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE		)







@require_tf
class      __a    (       unittest.TestCase			):







 @slow
 def    __lowercase   (						self   :  str		):


      '''simple docstring'''






      UpperCamelCase__     :  Union[str, Any]										=    TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"		)

      UpperCamelCase__     :  Optional[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]
      UpperCamelCase__     :  Dict										=    model(SCREAMING_SNAKE_CASE		)[0]  # embedding shape = (1, 768)
      # compare the actual values for a slice.
      UpperCamelCase__     :  Tuple										=    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		)		)







import numpy as np





def 		__UpperCAmelCase     (	a_):
							return 1 / (1 + np.exp(-vector))


if __name__ == "__main__":
	import doctest

	doctest.testmod()





