


'''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					,				SCREAMING_SNAKE_CASE__  :				Optional[int]					,				SCREAMING_SNAKE_CASE__  :				List[Any]=1_3					,				SCREAMING_SNAKE_CASE__  :				Any=7					,				SCREAMING_SNAKE_CASE__  :				Tuple=True					,				SCREAMING_SNAKE_CASE__  :				Optional[int]=True					,				SCREAMING_SNAKE_CASE__  :				Tuple=True					,				SCREAMING_SNAKE_CASE__  :				Tuple=True					,				SCREAMING_SNAKE_CASE__  :				Optional[Any]=9_9					,				SCREAMING_SNAKE_CASE__  :				Any=3_2					,				SCREAMING_SNAKE_CASE__  :				str=2					,				SCREAMING_SNAKE_CASE__  :				Optional[int]=4					,				SCREAMING_SNAKE_CASE__  :				str=3_7					,				SCREAMING_SNAKE_CASE__  :				Optional[int]="gelu"					,				SCREAMING_SNAKE_CASE__  :				str=0.1					,				SCREAMING_SNAKE_CASE__  :				str=0.1					,				SCREAMING_SNAKE_CASE__  :				Any=5_1_2					,				SCREAMING_SNAKE_CASE__  :				int=1_6					,				SCREAMING_SNAKE_CASE__  :				str=2					,				SCREAMING_SNAKE_CASE__  :				Optional[Any]=0.0_2					,				SCREAMING_SNAKE_CASE__  :				Optional[int]=3					,				SCREAMING_SNAKE_CASE__  :				int=4					,				SCREAMING_SNAKE_CASE__  :				int=None					,				SCREAMING_SNAKE_CASE__  :				int=0					,				):





       '''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=SCREAMING_SNAKE_CASE__					,				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  :				Union[str, Any]					,				SCREAMING_SNAKE_CASE__  :				Any					,				SCREAMING_SNAKE_CASE__  :				Tuple					,				SCREAMING_SNAKE_CASE__  :				str					,				SCREAMING_SNAKE_CASE__  :				str					,				SCREAMING_SNAKE_CASE__  :				List[str]					,				SCREAMING_SNAKE_CASE__  :				int					,				SCREAMING_SNAKE_CASE__  :				Union[str, Any]    ):





       '''simple docstring'''

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

  def __a    (       self  :				int					,				SCREAMING_SNAKE_CASE__  :				Tuple					,				SCREAMING_SNAKE_CASE__  :				Union[str, Any]					,				SCREAMING_SNAKE_CASE__  :				Union[str, Any]					,				SCREAMING_SNAKE_CASE__  :				List[str]					,				SCREAMING_SNAKE_CASE__  :				Union[str, Any]					,				SCREAMING_SNAKE_CASE__  :				str					,				SCREAMING_SNAKE_CASE__  :				Dict    ):





       '''simple docstring'''

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

  def __a    (       self  :				Dict					,				SCREAMING_SNAKE_CASE__  :				Optional[Any]					,				SCREAMING_SNAKE_CASE__  :				Dict					,				SCREAMING_SNAKE_CASE__  :				Any					,				SCREAMING_SNAKE_CASE__  :				List[Any]					,				SCREAMING_SNAKE_CASE__  :				List[Any]					,				SCREAMING_SNAKE_CASE__  :				Optional[Any]					,				SCREAMING_SNAKE_CASE__  :				Union[str, Any]    ):





       '''simple docstring'''

       __a   			= TFDPRReader(config=SCREAMING_SNAKE_CASE__    )
       __a   			= 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 __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 			lowerCAmelCase_    (							snake_case__		,     snake_case__		,     unittest.TestCase   ):





  """simple docstring"""



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

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

  def __a    (       self  :				Optional[int]    ):





       '''simple docstring'''

       __a   			= TFDPRModelTester(self    )
       __a   			= ConfigTester(self					,				config_class=SCREAMING_SNAKE_CASE__					,				hidden_size=3_7    )

  def __a    (       self  :				Optional[int]    ):





       '''simple docstring'''

       self.config_tester.run_common_tests()

  def __a    (       self  :				str    ):





       '''simple docstring'''

       __a   			= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_context_encoder(*SCREAMING_SNAKE_CASE__    )

  def __a    (       self  :				List[str]    ):





       '''simple docstring'''

       __a   			= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_question_encoder(*SCREAMING_SNAKE_CASE__    )

  def __a    (       self  :				List[str]    ):





       '''simple docstring'''

       __a   			= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_reader(*SCREAMING_SNAKE_CASE__    )


  @slow
  def __a    (       self  :				str    ):





       '''simple docstring'''

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

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

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

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


@require_tf
class 			lowerCAmelCase_    (							unittest.TestCase   ):





  """simple docstring"""



  @slow
  def __a    (       self  :				Optional[int]    ):





       '''simple docstring'''

       __a   			= TFDPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""    )

       __a   			= tf.constant(
           [[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_0_3, 2_0_2_6, 3_8_9_9, 1_0_1_4_0, 1_0_2_9, 1_0_2]]    )  # [CLS] hello, is my dog cute? [SEP]
       __a   			= model(SCREAMING_SNAKE_CASE__    )[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[:, :1_0].numpy()					,				expected_slice.numpy()					,				atol=1E-4    )    )











'''simple docstring'''





class __A     :



 '''simple docstring'''







 def __init__(self					,  A			)       ->	None:


      """simple docstring"""



      _a           =    len(A			)
      _a           =    [0] * len_array

      if len_array > 0:
           _a           =    array[0]

      for i in range(1					,  A			):
           _a           =    self.prefix_sum[i - 1] + array[i]



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


      """simple docstring"""



      if start == 0:
           return self.prefix_sum[end]

      return self.prefix_sum[end] - self.prefix_sum[start - 1]



 def    a__    (self					,  A			)       ->	bool:


      """simple docstring"""



      _a           =    {0}
      for sum_item in self.prefix_sum:
           if sum_item - target_sum in sums:
                return True

           sums.add(A			)

      return False


if __name__ == "__main__":
  import doctest

  doctest.testmod()



