


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







      def __init__(   self			:				Union[str, Any]		,	_lowercase			:				Any		,	_lowercase			:				List[str]=1_3		,	_lowercase			:				Tuple=7		,	_lowercase			:				Any=True		,	_lowercase			:				int=True		,	_lowercase			:				List[str]=True		,	_lowercase			:				List[str]=True		,	_lowercase			:				Tuple=9_9		,	_lowercase			:				Tuple=3_2		,	_lowercase			:				Any=2		,	_lowercase			:				Optional[int]=4		,	_lowercase			:				str=3_7		,	_lowercase			:				str="gelu"		,	_lowercase			:				int=0.1		,	_lowercase			:				List[Any]=0.1		,	_lowercase			:				List[str]=5_1_2		,	_lowercase			:				int=1_6		,	_lowercase			:				Optional[Any]=2		,	_lowercase			:				int=0.02		,	_lowercase			:				Optional[Any]=3		,	_lowercase			:				Union[str, Any]=4		,	_lowercase			:				List[str]=None		,	_lowercase			:				int=0		,	):
        lowerCAmelCase__     :     int										=       parent
        lowerCAmelCase__     :     List[Any]										=       batch_size
        lowerCAmelCase__     :     Union[str, Any]										=       seq_length
        lowerCAmelCase__     :     Union[str, Any]										=       is_training
        lowerCAmelCase__     :     List[str]										=       use_input_mask
        lowerCAmelCase__     :     int										=       use_token_type_ids
        lowerCAmelCase__     :     Any										=       use_labels
        lowerCAmelCase__     :     Union[str, Any]										=       vocab_size
        lowerCAmelCase__     :     Optional[int]										=       hidden_size
        lowerCAmelCase__     :     Tuple										=       num_hidden_layers
        lowerCAmelCase__     :     Optional[Any]										=       num_attention_heads
        lowerCAmelCase__     :     Optional[Any]										=       intermediate_size
        lowerCAmelCase__     :     Optional[int]										=       hidden_act
        lowerCAmelCase__     :     Optional[Any]										=       hidden_dropout_prob
        lowerCAmelCase__     :     str										=       attention_probs_dropout_prob
        lowerCAmelCase__     :     List[str]										=       max_position_embeddings
        lowerCAmelCase__     :     Any										=       type_vocab_size
        lowerCAmelCase__     :     int										=       type_sequence_label_size
        lowerCAmelCase__     :     str										=       initializer_range
        lowerCAmelCase__     :     Optional[int]										=       num_labels
        lowerCAmelCase__     :     Tuple										=       num_choices
        lowerCAmelCase__     :     List[str]										=       scope
        lowerCAmelCase__     :     Dict										=       projection_dim







      def 					_lowerCAmelCase							(   self			:				Any     ):
        lowerCAmelCase__     :     int										=       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__     :     Optional[int]										=       random_attention_mask([self.batch_size, self.seq_length]     )

        lowerCAmelCase__     :     Optional[int]										=       None
        if self.use_token_type_ids:
          lowerCAmelCase__     :     List[str]										=       ids_tensor([self.batch_size, self.seq_length]		,	self.type_vocab_size     )

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

        lowerCAmelCase__     :     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=_lowercase		,	initializer_range=self.initializer_range		,	)
        lowerCAmelCase__     :     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 					_lowerCAmelCase							(   self			:				List[Any]		,	_lowercase			:				List[Any]		,	_lowercase			:				Tuple		,	_lowercase			:				Union[str, Any]		,	_lowercase			:				Optional[Any]		,	_lowercase			:				Union[str, Any]		,	_lowercase			:				str		,	_lowercase			:				str     ):
        lowerCAmelCase__     :     Dict										=       TFDPRContextEncoder(config=_lowercase     )
        lowerCAmelCase__     :     Optional[int]										=       model(_lowercase		,	attention_mask=_lowercase		,	token_type_ids=_lowercase     )
        lowerCAmelCase__     :     str										=       model(_lowercase		,	token_type_ids=_lowercase     )
        lowerCAmelCase__     :     Any										=       model(_lowercase     )
        self.parent.assertEqual(result.pooler_output.shape		,	(self.batch_size, self.projection_dim or self.hidden_size)     )







      def 					_lowerCAmelCase							(   self			:				int		,	_lowercase			:				Any		,	_lowercase			:				List[Any]		,	_lowercase			:				Optional[Any]		,	_lowercase			:				Union[str, Any]		,	_lowercase			:				Optional[int]		,	_lowercase			:				Dict		,	_lowercase			:				str     ):
        lowerCAmelCase__     :     Any										=       TFDPRQuestionEncoder(config=_lowercase     )
        lowerCAmelCase__     :     Tuple										=       model(_lowercase		,	attention_mask=_lowercase		,	token_type_ids=_lowercase     )
        lowerCAmelCase__     :     List[str]										=       model(_lowercase		,	token_type_ids=_lowercase     )
        lowerCAmelCase__     :     Dict										=       model(_lowercase     )
        self.parent.assertEqual(result.pooler_output.shape		,	(self.batch_size, self.projection_dim or self.hidden_size)     )







      def 					_lowerCAmelCase							(   self			:				List[Any]		,	_lowercase			:				Optional[int]		,	_lowercase			:				Optional[int]		,	_lowercase			:				Union[str, Any]		,	_lowercase			:				List[Any]		,	_lowercase			:				int		,	_lowercase			:				Tuple		,	_lowercase			:				Tuple     ):
        lowerCAmelCase__     :     int										=       TFDPRReader(config=_lowercase     )
        lowerCAmelCase__     :     Dict										=       model(_lowercase		,	attention_mask=_lowercase     )

        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 					_lowerCAmelCase							(   self			:				Union[str, Any]     ):
        lowerCAmelCase__     :     str										=       self.prepare_config_and_inputs()
        (
            (
            lowerCAmelCase__ 
        )					,		(
            lowerCAmelCase__ 
        )					,		(
            lowerCAmelCase__ 
        )					,		(
            lowerCAmelCase__ 
        )					,		(
            lowerCAmelCase__ 
        )					,		(
            lowerCAmelCase__ 
        )					,		(
            lowerCAmelCase__ 
        )					,		 
        )     :     List[Any]										=       config_and_inputs
        lowerCAmelCase__     :     Optional[Any]										=       {"input_ids": input_ids}
        return config, inputs_dict

@require_tf
class 			lowercase_	(			a_				,				a_				,				unittest.TestCase    ):
      __magic_name__  :				Any 							=					(
          (
              TFDPRContextEncoder,
              TFDPRQuestionEncoder,
              TFDPRReader,
          )
          if is_tf_available()
          else ()
      )
      __magic_name__  :				Optional[int] 							=					{'feature-extraction': TFDPRQuestionEncoder} if is_tf_available() else {}

      __magic_name__  :				str 							=					False
      __magic_name__  :				Any 							=					False
      __magic_name__  :				int 							=					False
      __magic_name__  :				Dict 							=					False
      __magic_name__  :				Any 							=					False







      def 					_lowerCAmelCase							(   self			:				List[str]     ):
        lowerCAmelCase__     :     str										=       TFDPRModelTester(self     )
        lowerCAmelCase__     :     Optional[int]										=       ConfigTester(self		,	config_class=_lowercase		,	hidden_size=3_7     )







      def 					_lowerCAmelCase							(   self			:				Optional[Any]     ):
        self.config_tester.run_common_tests()







      def 					_lowerCAmelCase							(   self			:				int     ):
        lowerCAmelCase__     :     List[Any]										=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_dpr_context_encoder(*_lowercase     )







      def 					_lowerCAmelCase							(   self			:				Tuple     ):
        lowerCAmelCase__     :     Union[str, Any]										=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_dpr_question_encoder(*_lowercase     )







      def 					_lowerCAmelCase							(   self			:				Any     ):
        lowerCAmelCase__     :     Optional[Any]										=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_dpr_reader(*_lowercase     )







      @slow
      def 					_lowerCAmelCase							(   self			:				str     ):
        for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase__     :     str										=       TFDPRContextEncoder.from_pretrained(_lowercase     )
          self.assertIsNotNone(_lowercase     )

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

        for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase__     :     str										=       TFDPRQuestionEncoder.from_pretrained(_lowercase     )
          self.assertIsNotNone(_lowercase     )

        for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase__     :     Tuple										=       TFDPRReader.from_pretrained(_lowercase     )
          self.assertIsNotNone(_lowercase     )

@require_tf
class 			lowercase_	(			unittest.TestCase    ):







      @slow
      def 					_lowerCAmelCase							(   self			:				Tuple     ):
        lowerCAmelCase__     :     Any										=       TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"     )

        lowerCAmelCase__     :     Optional[int]										=       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]
        lowerCAmelCase__     :     List[Any]										=       model(_lowercase     )[0]  # embedding shape = (1, 768)
        # compare the actual values for a slice.
        lowerCAmelCase__     :     Union[str, Any]										=       tf.constant(
            [
                [
                    0.0323_6253,
                    0.1275_3335,
                    0.1681_8509,
                    0.0027_9786,
                    0.389_6933,
                    0.2426_4945,
                    0.217_8971,
                    -0.0233_5227,
                    -0.0848_1959,
                    -0.1432_4117,
                ]
            ]     )
        self.assertTrue(numpy.allclose(output[:, :1_0].numpy()		,	expected_slice.numpy()		,	atol=1e-4     )     )


"""simple docstring"""


def  lowercase						(	lowerCAmelCase__						):
			return " ".join(input_str.split()[::-1]						)


if __name__ == "__main__":
						import doctest

						doctest.testmod()



