





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





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






		def  A_     ( self     :					Optional[int]						)				->			str:





									"""simple docstring"""
									SCREAMING_SNAKE_CASE__       :      int     =						ids_tensor([self.batch_size, self.seq_length],			self.vocab_size						)

									SCREAMING_SNAKE_CASE__       :      List[Any]     =						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__       :      Tuple     =						None
									if self.use_token_type_ids:
																SCREAMING_SNAKE_CASE__       :      List[Any]     =						ids_tensor([self.batch_size, self.seq_length],			self.type_vocab_size						)

									SCREAMING_SNAKE_CASE__       :      Tuple     =						None
									SCREAMING_SNAKE_CASE__       :      Any     =						None
									SCREAMING_SNAKE_CASE__       :      Dict     =						None
									if self.use_labels:
																SCREAMING_SNAKE_CASE__       :      Dict     =						ids_tensor([self.batch_size],			self.type_sequence_label_size						)
																SCREAMING_SNAKE_CASE__       :      List[Any]     =						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__       :      Optional[int]     =						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=_UpperCAmelCase,			initializer_range=self.initializer_range,			)
									SCREAMING_SNAKE_CASE__       :      Tuple     =						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     :					List[str],			_UpperCAmelCase     :					int,			_UpperCAmelCase     :					Tuple,			_UpperCAmelCase     :					Tuple,			_UpperCAmelCase     :					Dict,			_UpperCAmelCase     :					str,			_UpperCAmelCase     :					List[Any],			_UpperCAmelCase     :					Optional[int]						)				->			Union[str, Any]:





									"""simple docstring"""
									SCREAMING_SNAKE_CASE__       :      Tuple     =						TFDPRContextEncoder(config=_UpperCAmelCase						)
									SCREAMING_SNAKE_CASE__       :      Optional[int]     =						model(_UpperCAmelCase,			attention_mask=_UpperCAmelCase,			token_type_ids=_UpperCAmelCase						)
									SCREAMING_SNAKE_CASE__       :      Tuple     =						model(_UpperCAmelCase,			token_type_ids=_UpperCAmelCase						)
									SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						model(_UpperCAmelCase						)
									self.parent.assertEqual(result.pooler_output.shape,			(self.batch_size, self.projection_dim or self.hidden_size)						)






		def  A_     ( self     :					str,			_UpperCAmelCase     :					str,			_UpperCAmelCase     :					List[Any],			_UpperCAmelCase     :					Union[str, Any],			_UpperCAmelCase     :					Any,			_UpperCAmelCase     :					Union[str, Any],			_UpperCAmelCase     :					Tuple,			_UpperCAmelCase     :					int						)				->			Any:





									"""simple docstring"""
									SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						TFDPRQuestionEncoder(config=_UpperCAmelCase						)
									SCREAMING_SNAKE_CASE__       :      str     =						model(_UpperCAmelCase,			attention_mask=_UpperCAmelCase,			token_type_ids=_UpperCAmelCase						)
									SCREAMING_SNAKE_CASE__       :      str     =						model(_UpperCAmelCase,			token_type_ids=_UpperCAmelCase						)
									SCREAMING_SNAKE_CASE__       :      str     =						model(_UpperCAmelCase						)
									self.parent.assertEqual(result.pooler_output.shape,			(self.batch_size, self.projection_dim or self.hidden_size)						)






		def  A_     ( self     :					Optional[int],			_UpperCAmelCase     :					Optional[Any],			_UpperCAmelCase     :					int,			_UpperCAmelCase     :					List[Any],			_UpperCAmelCase     :					List[Any],			_UpperCAmelCase     :					Any,			_UpperCAmelCase     :					Union[str, Any],			_UpperCAmelCase     :					str						)				->			Any:





									"""simple docstring"""
									SCREAMING_SNAKE_CASE__       :      Tuple     =						TFDPRReader(config=_UpperCAmelCase						)
									SCREAMING_SNAKE_CASE__       :      str     =						model(_UpperCAmelCase,			attention_mask=_UpperCAmelCase						)

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





									"""simple docstring"""
									SCREAMING_SNAKE_CASE__       :      List[Any]     =						self.prepare_config_and_inputs()
									(
									    SCREAMING_SNAKE_CASE__
									)       :      Optional[int]     =						config_and_inputs
									SCREAMING_SNAKE_CASE__       :      Any     =						{"input_ids": input_ids}
									return config, inputs_dict


@require_tf
class 						lowerCamelCase       (__lowerCamelCase							,     __lowerCamelCase							,     unittest.TestCase      ):







		"""simple docstring"""




		UpperCAmelCase_      						=   (
		    (
		        TFDPRContextEncoder,
		        TFDPRQuestionEncoder,
		        TFDPRReader,
		    )
		    if is_tf_available()
		    else ()
		)
		UpperCAmelCase_      						=   {"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

		UpperCAmelCase_      						=   False
		UpperCAmelCase_      						=   False
		UpperCAmelCase_      						=   False
		UpperCAmelCase_      						=   False
		UpperCAmelCase_      						=   False






		def  A_     ( self     :					Optional[int]						)				->			Dict:





									"""simple docstring"""
									SCREAMING_SNAKE_CASE__       :      List[str]     =						TFDPRModelTester(self						)
									SCREAMING_SNAKE_CASE__       :      List[str]     =						ConfigTester(self,			config_class=_UpperCAmelCase,			hidden_size=3_7						)






		def  A_     ( self     :					str						)				->			Tuple:





									"""simple docstring"""
									self.config_tester.run_common_tests()






		def  A_     ( self     :					Tuple						)				->			Any:





									"""simple docstring"""
									SCREAMING_SNAKE_CASE__       :      List[str]     =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_dpr_context_encoder(*_UpperCAmelCase						)






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





									"""simple docstring"""
									SCREAMING_SNAKE_CASE__       :      str     =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_dpr_question_encoder(*_UpperCAmelCase						)






		def  A_     ( self     :					Union[str, Any]						)				->			Optional[int]:





									"""simple docstring"""
									SCREAMING_SNAKE_CASE__       :      List[str]     =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_dpr_reader(*_UpperCAmelCase						)






		@slow
		def  A_     ( self     :					Union[str, Any]						)				->			Any:





									"""simple docstring"""
									for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																SCREAMING_SNAKE_CASE__       :      List[str]     =						TFDPRContextEncoder.from_pretrained(_UpperCAmelCase						)
																self.assertIsNotNone(_UpperCAmelCase						)

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

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

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


@require_tf
class 						lowerCamelCase       (unittest.TestCase      ):







		"""simple docstring"""




		@slow
		def  A_     ( self     :					Any						)				->			Union[str, Any]:





									"""simple docstring"""
									SCREAMING_SNAKE_CASE__       :      Dict     =						TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"						)

									SCREAMING_SNAKE_CASE__       :      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]
									SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						model(_UpperCAmelCase						)[0]  # embedding shape = (1, 768)
									# compare the actual values for a slice.
									SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						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[:, :1_0].numpy(),			expected_slice.numpy(),			atol=1E-4						)						)













def _a (  SCREAMING_SNAKE_CASE__      :  int       )	->    str:

							'''simple docstring'''






							if isinstance(SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__       ):
														raise TypeError("'float' object cannot be interpreted as an integer"       )
							if isinstance(SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__       ):
														raise TypeError("'str' object cannot be interpreted as an integer"       )

							if num == 0:
														return "0b0"

							SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						False

							if num < 0:
														SCREAMING_SNAKE_CASE__       :      Any     =						True
														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						-num

							SCREAMING_SNAKE_CASE__       :      list[int]     =						[]
							while num > 0:
														binary.insert(0			,				num % 2       )
														num >>= 1

							if negative:
														return "-0b" + "".join(str(SCREAMING_SNAKE_CASE__       ) for e in binary       )

							return "0b" + "".join(str(SCREAMING_SNAKE_CASE__       ) for e in binary       )


if __name__ == "__main__":
		import doctest

		doctest.testmod()






