





"""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 					_SCREAMING_SNAKE_CASE:




       def __init__(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=13		,SCREAMING_SNAKE_CASE__=7		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=99		,SCREAMING_SNAKE_CASE__=32		,SCREAMING_SNAKE_CASE__=2		,SCREAMING_SNAKE_CASE__=4		,SCREAMING_SNAKE_CASE__=37		,SCREAMING_SNAKE_CASE__="gelu"		,SCREAMING_SNAKE_CASE__=0.1		,SCREAMING_SNAKE_CASE__=0.1		,SCREAMING_SNAKE_CASE__=5_12		,SCREAMING_SNAKE_CASE__=16		,SCREAMING_SNAKE_CASE__=2		,SCREAMING_SNAKE_CASE__=0.0_2		,SCREAMING_SNAKE_CASE__=3		,SCREAMING_SNAKE_CASE__=4		,SCREAMING_SNAKE_CASE__=None		,SCREAMING_SNAKE_CASE__=0		,)			->      Union[str, Any]:





             """simple docstring"""






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




       def     _UpperCamelCase     (						self							)			->      Tuple:





             """simple docstring"""






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

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

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

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

             __SCREAMING_SNAKE_CASE   :str						=  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		,)
             __SCREAMING_SNAKE_CASE   :List[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		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  TFDPRContextEncoder(config=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :List[Any]						=  model(SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :int						=  model(SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :Tuple						=  model(SCREAMING_SNAKE_CASE__							)
             self.parent.assertEqual(result.pooler_output.shape		,(self.batch_size, self.projection_dim or self.hidden_size)							)




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





             """simple docstring"""






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




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      Optional[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Any						=  TFDPRReader(config=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :Dict						=  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     _UpperCamelCase     (						self							)			->      List[str]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :int						=  self.prepare_config_and_inputs()
             (
                 (
                 __SCREAMING_SNAKE_CASE
             )				,(
                 __SCREAMING_SNAKE_CASE
             )				,(
                 __SCREAMING_SNAKE_CASE
             )				,(
                 __SCREAMING_SNAKE_CASE
             )				,(
                 __SCREAMING_SNAKE_CASE
             )				,(
                 __SCREAMING_SNAKE_CASE
             )				,(
                 __SCREAMING_SNAKE_CASE
             )				,
             )   :List[Any]						=  config_and_inputs
             __SCREAMING_SNAKE_CASE   :Any						=  {'''input_ids''': input_ids}
             return config, inputs_dict






@require_tf
class 					_SCREAMING_SNAKE_CASE(   A    ,	A    ,	unittest.TestCase  ):
       SCREAMING_SNAKE_CASE_   :      Any							=  (
           (
               TFDPRContextEncoder,
               TFDPRQuestionEncoder,
               TFDPRReader,
           )
           if is_tf_available()
           else ()
       )
       SCREAMING_SNAKE_CASE_   :      List[Any]							=  {'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

       SCREAMING_SNAKE_CASE_   :      Optional[Any]							=  False
       SCREAMING_SNAKE_CASE_   :      Dict							=  False
       SCREAMING_SNAKE_CASE_   :      Any							=  False
       SCREAMING_SNAKE_CASE_   :      List[Any]							=  False
       SCREAMING_SNAKE_CASE_   :      Any							=  False




       def     _UpperCamelCase     (						self							)			->      Optional[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Dict						=  TFDPRModelTester(self							)
             __SCREAMING_SNAKE_CASE   :List[Any]						=  ConfigTester(self		,config_class=SCREAMING_SNAKE_CASE__		,hidden_size=37							)




       def     _UpperCamelCase     (						self							)			->      Any:





             """simple docstring"""






             self.config_tester.run_common_tests()




       def     _UpperCamelCase     (						self							)			->      Optional[int]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :List[Any]						=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_dpr_context_encoder(*SCREAMING_SNAKE_CASE__							)




       def     _UpperCamelCase     (						self							)			->      str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Dict						=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_dpr_question_encoder(*SCREAMING_SNAKE_CASE__							)




       def     _UpperCamelCase     (						self							)			->      int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Any						=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_dpr_reader(*SCREAMING_SNAKE_CASE__							)




       @slow
       def     _UpperCamelCase     (						self							)			->      List[str]:





             """simple docstring"""






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

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

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

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






@require_tf
class 					_SCREAMING_SNAKE_CASE(   unittest.TestCase  ):




       @slow
       def     _UpperCamelCase     (						self							)			->      Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Optional[int]						=  TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''							)

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






"""simple docstring"""
def      __lowerCamelCase   (							a_     :		int							)			->      bool:
      if not isinstance(a_ ,   a_							):
            __SCREAMING_SNAKE_CASE   :int						=  f'''Input value of [number={number}] must be an integer'''
            raise TypeError(a_							)
      if number < 0:
            return False
      __SCREAMING_SNAKE_CASE   :Any						=  number * number
      while number > 0:
            if number % 10 != number_square % 10:
                  return False
            number //= 10
            number_square //= 10
      return True


if __name__ == "__main__":
  import doctest

  doctest.testmod()