





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







	'''simple docstring'''

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







				'''simple docstring'''





				__lowercase										=							parent
				__lowercase										=							batch_size
				__lowercase										=							seq_length
				__lowercase										=							is_training
				__lowercase										=							use_input_mask
				__lowercase										=							use_token_type_ids
				__lowercase										=							use_labels
				__lowercase										=							vocab_size
				__lowercase										=							hidden_size
				__lowercase										=							num_hidden_layers
				__lowercase										=							num_attention_heads
				__lowercase										=							intermediate_size
				__lowercase										=							hidden_act
				__lowercase										=							hidden_dropout_prob
				__lowercase										=							attention_probs_dropout_prob
				__lowercase										=							max_position_embeddings
				__lowercase										=							type_vocab_size
				__lowercase										=							type_sequence_label_size
				__lowercase										=							initializer_range
				__lowercase										=							num_labels
				__lowercase										=							num_choices
				__lowercase										=							scope
				__lowercase										=							projection_dim


	def 						_UpperCAmelCase     (self )							->							Tuple:







				'''simple docstring'''





				__lowercase										=							ids_tensor([self.batch_size, self.seq_length]     ,self.vocab_size )

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

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

				__lowercase										=							None
				__lowercase										=							None
				__lowercase										=							None
				if self.use_labels:
							__lowercase										=							ids_tensor([self.batch_size]     ,self.type_sequence_label_size )
							__lowercase										=							ids_tensor([self.batch_size, self.seq_length]     ,self.num_labels )
							__lowercase										=							ids_tensor([self.batch_size]     ,self.num_choices )

				__lowercase										=							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=lowerCamelCase__     ,initializer_range=self.initializer_range     ,)
				__lowercase										=							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     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase )							->							int:







				'''simple docstring'''





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


	def 						_UpperCAmelCase     (self     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase )							->							Any:







				'''simple docstring'''





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


	def 						_UpperCAmelCase     (self     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase )							->							str:







				'''simple docstring'''





				__lowercase										=							TFDPRReader(config=lowerCamelCase__ )
				__lowercase										=							model(lowerCamelCase__     ,attention_mask=lowerCamelCase__ )

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







				'''simple docstring'''





				__lowercase										=							self.prepare_config_and_inputs()
				(
				    __lowercase    
				)										=							config_and_inputs
				__lowercase										=							{"input_ids": input_ids}
				return config, inputs_dict






@require_tf
class 							__lowercase       (      UpperCAmelCase__ ,      UpperCAmelCase__ ,      unittest.TestCase						):







	'''simple docstring'''

	a    :				Optional[int]											=       (
	    (
	        TFDPRContextEncoder,
	        TFDPRQuestionEncoder,
	        TFDPRReader,
	    )
	    if is_tf_available()
	    else ()
	)
	a    :				List[str]											=       {"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

	a    :				int											=       False
	a    :				Optional[Any]											=       False
	a    :				Union[str, Any]											=       False
	a    :				Tuple											=       False
	a    :				int											=       False


	def 						_UpperCAmelCase     (self )							->							str:







				'''simple docstring'''





				__lowercase										=							TFDPRModelTester(self )
				__lowercase										=							ConfigTester(self     ,config_class=lowerCamelCase__     ,hidden_size=37 )


	def 						_UpperCAmelCase     (self )							->							List[Any]:







				'''simple docstring'''





				self.config_tester.run_common_tests()


	def 						_UpperCAmelCase     (self )							->							Dict:







				'''simple docstring'''





				__lowercase										=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_context_encoder(*lowerCamelCase__ )


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







				'''simple docstring'''





				__lowercase										=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_question_encoder(*lowerCamelCase__ )


	def 						_UpperCAmelCase     (self )							->							Dict:







				'''simple docstring'''





				__lowercase										=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_reader(*lowerCamelCase__ )


	@slow
	def 						_UpperCAmelCase     (self )							->							Dict:







				'''simple docstring'''





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

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

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

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






@require_tf
class 							__lowercase       (      unittest.TestCase						):







	'''simple docstring'''

	@slow
	def 						_UpperCAmelCase     (self )							->							str:







				'''simple docstring'''





				__lowercase										=							TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base''' )

				__lowercase										=							tf.constant(
				    [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]] )  # [CLS] hello, is my dog cute? [SEP]
				__lowercase										=							model(lowerCamelCase__ )[0]  # embedding shape = (1, 768)
				# compare the actual values for a slice.
				__lowercase										=							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'''



# flake8: noqa
# Lint as: python3
_SCREAMING_SNAKE_CASE					=    [
    '''VerificationMode''',
    '''Version''',
    '''disable_progress_bar''',
    '''enable_progress_bar''',
    '''is_progress_bar_enabled''',
    '''experimental''',
]

from .info_utils import VerificationMode
from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled
from .version import Version
from .experimental import experimental





