





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






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






   '''simple docstring'''

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






  def 				UpperCAmelCase__						(							self			:					List[Any]      )->int:






   '''simple docstring'''

   __lowerCAmelCase					:	str      =       ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size      )

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

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

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

   __lowerCAmelCase					:	Optional[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=_snake_case       ,  initializer_range=self.initializer_range       ,  )
   __lowerCAmelCase					:	Union[str, 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 				UpperCAmelCase__						(							self			:					Any       ,  _snake_case			:					List[Any]       ,  _snake_case			:					Union[str, Any]       ,  _snake_case			:					Optional[Any]       ,  _snake_case			:					Optional[Any]       ,  _snake_case			:					int       ,  _snake_case			:					str       ,  _snake_case			:					Dict      )->Union[str, Any]:






   '''simple docstring'''

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






  def 				UpperCAmelCase__						(							self			:					Optional[int]       ,  _snake_case			:					Optional[int]       ,  _snake_case			:					Tuple       ,  _snake_case			:					Union[str, Any]       ,  _snake_case			:					Optional[int]       ,  _snake_case			:					Union[str, Any]       ,  _snake_case			:					Any       ,  _snake_case			:					Union[str, Any]      )->List[Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	Optional[Any]      =       TFDPRQuestionEncoder(config=_snake_case      )
   __lowerCAmelCase					:	Tuple      =       model(_snake_case       ,  attention_mask=_snake_case       ,  token_type_ids=_snake_case      )
   __lowerCAmelCase					:	Optional[int]      =       model(_snake_case       ,  token_type_ids=_snake_case      )
   __lowerCAmelCase					:	Optional[Any]      =       model(_snake_case      )
   self.parent.assertEqual(result.pooler_output.shape       ,  (self.batch_size, self.projection_dim or self.hidden_size)      )






  def 				UpperCAmelCase__						(							self			:					List[str]       ,  _snake_case			:					List[Any]       ,  _snake_case			:					Union[str, Any]       ,  _snake_case			:					int       ,  _snake_case			:					Dict       ,  _snake_case			:					Any       ,  _snake_case			:					List[str]       ,  _snake_case			:					Any      )->Optional[Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	Any      =       TFDPRReader(config=_snake_case      )
   __lowerCAmelCase					:	Tuple      =       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 				UpperCAmelCase__						(							self			:					Union[str, Any]      )->Dict:






   '''simple docstring'''

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






@require_tf
class        snake_case_    (		__lowercase					,__lowercase					,unittest.TestCase						):
  A_      							=   (
      (
          TFDPRContextEncoder,
          TFDPRQuestionEncoder,
          TFDPRReader,
      )
      if is_tf_available()
      else ()
  )
  A_      							=   {'feature-extraction': TFDPRQuestionEncoder} if is_tf_available() else {}

  A_      							=   False
  A_      							=   False
  A_      							=   False
  A_      							=   False
  A_      							=   False






  def 				UpperCAmelCase__						(							self			:					int      )->int:






   '''simple docstring'''

   __lowerCAmelCase					:	int      =       TFDPRModelTester(self      )
   __lowerCAmelCase					:	List[Any]      =       ConfigTester(self       ,  config_class=_snake_case       ,  hidden_size=37      )






  def 				UpperCAmelCase__						(							self			:					Optional[Any]      )->Optional[int]:






   '''simple docstring'''

   self.config_tester.run_common_tests()






  def 				UpperCAmelCase__						(							self			:					Tuple      )->List[Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	List[str]      =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_dpr_context_encoder(*_snake_case      )






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






   '''simple docstring'''

   __lowerCAmelCase					:	List[Any]      =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_dpr_question_encoder(*_snake_case      )






  def 				UpperCAmelCase__						(							self			:					Dict      )->Optional[Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	Dict      =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_dpr_reader(*_snake_case      )






  @slow
  def 				UpperCAmelCase__						(							self			:					Optional[int]      )->Optional[Any]:






   '''simple docstring'''

   for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    __lowerCAmelCase					:	Optional[int]      =       TFDPRContextEncoder.from_pretrained(_snake_case      )
    self.assertIsNotNone(_snake_case      )

   for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    __lowerCAmelCase					:	List[Any]      =       TFDPRContextEncoder.from_pretrained(_snake_case      )
    self.assertIsNotNone(_snake_case      )

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

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






@require_tf
class        snake_case_    (		unittest.TestCase						):






  @slow
  def 				UpperCAmelCase__						(							self			:					Optional[int]      )->List[Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	List[str]      =       TFDPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""      )

   __lowerCAmelCase					:	List[Any]      =       tf.constant(
       [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]      )  # [CLS] hello, is my dog cute? [SEP]
   __lowerCAmelCase					:	Optional[Any]      =       model(_snake_case      )[0]  # embedding shape = (1, 768)
   # compare the actual values for a slice.
   __lowerCAmelCase					:	Any      =       tf.constant(
       [
           [
               0.03_236_253,
               0.12_753_335,
               0.16_818_509,
               0.00_279_786,
               0.3_896_933,
               0.24_264_945,
               0.2_178_971,
               -0.02_335_227,
               -0.08_481_959,
               -0.14_324_117,
           ]
       ]      )
   self.assertTrue(numpy.allclose(output[:, :10].numpy()       ,  expected_slice.numpy()       ,  atol=1E-4      )      )






def 	_SCREAMING_SNAKE_CASE		(      SCREAMING_SNAKE_CASE		:list[int]   )      ->   int:
 if not numbers:
  return 0

 if not isinstance(SCREAMING_SNAKE_CASE   ,   (list, tuple)   ) or not all(
     isinstance(SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   ) for number in numbers   ):
  raise ValueError("""numbers must be an iterable of integers"""   )

 __lowerCAmelCase					:	int      =       numbers[0]

 for i in range(1   ,   len(SCREAMING_SNAKE_CASE   )   ):
  # update the maximum and minimum subarray products
  __lowerCAmelCase					:	List[str]      =       numbers[i]
  if number < 0:
   __lowerCAmelCase ,					__lowerCAmelCase					:	Union[str, Any]      =       min_till_now, max_till_now
  __lowerCAmelCase					:	Optional[int]      =       max(SCREAMING_SNAKE_CASE   ,   max_till_now * number   )
  __lowerCAmelCase					:	List[Any]      =       min(SCREAMING_SNAKE_CASE   ,   min_till_now * number   )

  # update the maximum product found till now
  __lowerCAmelCase					:	List[str]      =       max(SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   )

 return max_prod