



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



 '''simple docstring'''







 def __init__(self					,  A					,  A=13					,  A=7					,  A=True					,  A=True					,  A=True					,  A=True					,  A=99					,  A=32					,  A=2					,  A=4					,  A=37					,  A="gelu"					,  A=0.1					,  A=0.1					,  A=512					,  A=16					,  A=2					,  A=0.02					,  A=3					,  A=4					,  A=None					,  A=0					,  )       ->	Union[str, Any]:


      """simple docstring"""



      _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			)       ->	int:


      """simple docstring"""



      _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=A					,  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					,  A					,  A					,  A					,  A					,  A					,  A					,  A			)       ->	int:


      """simple docstring"""



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



 def    a__    (self					,  A					,  A					,  A					,  A					,  A					,  A					,  A			)       ->	Tuple:


      """simple docstring"""



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



 def    a__    (self					,  A					,  A					,  A					,  A					,  A					,  A					,  A			)       ->	List[str]:


      """simple docstring"""



      _a           =    TFDPRReader(config=A			)
      _a           =    model(A					,  attention_mask=A			)

      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			)       ->	str:


      """simple docstring"""



      _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 __A     (    A  ,  A  ,  unittest.TestCase		):



 '''simple docstring'''







 __lowerCamelCase       :			Dict		=			(
     (
         TFDPRContextEncoder,
         TFDPRQuestionEncoder,
         TFDPRReader,
     )
     if is_tf_available()
     else ()
 )
 __lowerCamelCase       :			List[str]		=			{'feature-extraction': TFDPRQuestionEncoder} if is_tf_available() else {}

 __lowerCamelCase       :			int		=			False
 __lowerCamelCase       :			Any		=			False
 __lowerCamelCase       :			Any		=			False
 __lowerCamelCase       :			List[Any]		=			False
 __lowerCamelCase       :			List[Any]		=			False



 def    a__    (self			)       ->	List[str]:


      """simple docstring"""



      _a           =    TFDPRModelTester(self			)
      _a           =    ConfigTester(self					,  config_class=A					,  hidden_size=37			)



 def    a__    (self			)       ->	Tuple:


      """simple docstring"""



      self.config_tester.run_common_tests()



 def    a__    (self			)       ->	Union[str, Any]:


      """simple docstring"""



      _a           =    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_dpr_context_encoder(*A			)



 def    a__    (self			)       ->	int:


      """simple docstring"""



      _a           =    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_dpr_question_encoder(*A			)



 def    a__    (self			)       ->	Dict:


      """simple docstring"""



      _a           =    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_dpr_reader(*A			)



 @slow
 def    a__    (self			)       ->	List[Any]:


      """simple docstring"""



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

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

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

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






@require_tf
class __A     (    unittest.TestCase		):



 '''simple docstring'''







 @slow
 def    a__    (self			)       ->	Any:


      """simple docstring"""



      _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(A			)[0]  # embedding shape = (1, 768)
      # compare the actual values for a slice.
      _a           =    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			)			)








'''simple docstring'''





from __future__ import annotations




def lowerCAmelCase    (__A):



     """simple docstring"""



     return len(set(__A)) == len(__A)


if __name__ == "__main__":
  import doctest

  doctest.testmod()



