




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







								'''simple docstring'''







								a									=				parent
								a									=				batch_size
								a									=				seq_length
								a									=				is_training
								a									=				use_input_mask
								a									=				use_token_type_ids
								a									=				use_labels
								a									=				vocab_size
								a									=				hidden_size
								a									=				num_hidden_layers
								a									=				num_attention_heads
								a									=				intermediate_size
								a									=				hidden_act
								a									=				hidden_dropout_prob
								a									=				attention_probs_dropout_prob
								a									=				max_position_embeddings
								a									=				type_vocab_size
								a									=				type_sequence_label_size
								a									=				initializer_range
								a									=				num_labels
								a									=				num_choices
								a									=				scope
								a									=				projection_dim
							def  lowerCamelCase__				(		self		:List[str]							):







								'''simple docstring'''







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

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

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

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

								a									=				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						, )
								a									=				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		:int						, __magic_name__		:Optional[Any]						, __magic_name__		:Dict						, __magic_name__		:Any						, __magic_name__		:Optional[Any]						, __magic_name__		:Any						, __magic_name__		:Tuple						, __magic_name__		:Tuple							):







								'''simple docstring'''







								a									=				TFDPRContextEncoder(config=_lowercase							)
								a									=				model(_lowercase						, attention_mask=_lowercase						, token_type_ids=_lowercase							)
								a									=				model(_lowercase						, token_type_ids=_lowercase							)
								a									=				model(_lowercase							)
								self.parent.assertEqual(result.pooler_output.shape						, (self.batch_size, self.projection_dim or self.hidden_size)							)
							def  lowerCamelCase__				(		self		:Optional[int]						, __magic_name__		:str						, __magic_name__		:Union[str, Any]						, __magic_name__		:Optional[Any]						, __magic_name__		:Optional[int]						, __magic_name__		:Optional[Any]						, __magic_name__		:List[str]						, __magic_name__		:int							):







								'''simple docstring'''







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







								'''simple docstring'''







								a									=				TFDPRReader(config=_lowercase							)
								a									=				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		:int							):







								'''simple docstring'''







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







@require_tf
class 						__lowerCAmelCase      (      SCREAMING_SNAKE_CASE__ ,			SCREAMING_SNAKE_CASE__ ,			unittest.TestCase					):
							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  lowerCamelCase__				(		self		:Any							):







								'''simple docstring'''







								a									=				TFDPRModelTester(self							)
								a									=				ConfigTester(self						, config_class=_lowercase						, hidden_size=37							)
							def  lowerCamelCase__				(		self		:List[str]							):







								'''simple docstring'''







								self.config_tester.run_common_tests()
							def  lowerCamelCase__				(		self		:int							):







								'''simple docstring'''







								a									=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_context_encoder(*_lowercase							)
							def  lowerCamelCase__				(		self		:str							):







								'''simple docstring'''







								a									=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_question_encoder(*_lowercase							)
							def  lowerCamelCase__				(		self		:Optional[int]							):







								'''simple docstring'''







								a									=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_dpr_reader(*_lowercase							)





							@slow
							def  lowerCamelCase__				(		self		:List[str]							):







								'''simple docstring'''







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

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

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

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







@require_tf
class 						__lowerCAmelCase      (      unittest.TestCase					):
							@slow
							def  lowerCamelCase__				(		self		:List[Any]							):







								'''simple docstring'''







								a									=				TFDPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""							)

								a									=				tf.constant(
								    [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]							)  # [CLS] hello, is my dog cute? [SEP]
								a									=				model(_lowercase							)[0]  # embedding shape = (1, 768)
								# compare the actual values for a slice.
								a									=				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[:, :10].numpy()						, expected_slice.numpy()						, atol=1E-4							)							)






"""simple docstring"""







import math
import sys

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							int ):
						"""simple docstring"""






						if number != int(lowerCamelCase_ ):
												raise ValueError("the value of input must be a natural number" )
						if number < 0:
												raise ValueError("the value of input must not be a negative number" )
						if number == 0:
												return 1
						snake_case   :			List[str]						    = [-1] * (number + 1)
						snake_case   :			str						    = 0
						for i in range(1     ,		number + 1 ):
												snake_case   :			Tuple						    = sys.maxsize
												snake_case   :			Optional[Any]						    = int(math.sqrt(lowerCamelCase_ ) )
												for j in range(1     ,		root + 1 ):
																		snake_case   :			List[str]						    = 1 + answers[i - (j**2)]
																		snake_case   :			List[str]						    = min(lowerCamelCase_     ,		lowerCamelCase_ )
												snake_case   :			Dict						    = answer
						return answers[number]


if __name__ == "__main__":
			import doctest

			doctest.testmod()



