





import tempfile
import unittest

from transformers import DPRConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import DPRContextEncoder, DPRQuestionEncoder, DPRReader, DPRReaderTokenizer
					from transformers.models.dpr.modeling_dpr import (
					    DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
					    DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
					    DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
					)
class       UpperCamelCase_		:




					def __init__(		self					,    lowerCAmelCase_					,    lowerCAmelCase_=13					,    lowerCAmelCase_=7					,    lowerCAmelCase_=False					,    lowerCAmelCase_=True					,    lowerCAmelCase_=True					,    lowerCAmelCase_=True					,    lowerCAmelCase_=99					,    lowerCAmelCase_=32					,    lowerCAmelCase_=5					,    lowerCAmelCase_=4					,    lowerCAmelCase_=37					,    lowerCAmelCase_="gelu"					,    lowerCAmelCase_=0.1					,    lowerCAmelCase_=0.1					,    lowerCAmelCase_=512					,    lowerCAmelCase_=16					,    lowerCAmelCase_=2					,    lowerCAmelCase_=0.02					,    lowerCAmelCase_=3					,    lowerCAmelCase_=4					,    lowerCAmelCase_=None					,    lowerCAmelCase_=0					,    )		->				Any:
										_snake_case						=					parent
										_snake_case						=					batch_size
										_snake_case						=					seq_length
										_snake_case						=					is_training
										_snake_case						=					use_input_mask
										_snake_case						=					use_token_type_ids
										_snake_case						=					use_labels
										_snake_case						=					vocab_size
										_snake_case						=					hidden_size
										_snake_case						=					num_hidden_layers
										_snake_case						=					num_attention_heads
										_snake_case						=					intermediate_size
										_snake_case						=					hidden_act
										_snake_case						=					hidden_dropout_prob
										_snake_case						=					attention_probs_dropout_prob
										_snake_case						=					max_position_embeddings
										_snake_case						=					type_vocab_size
										_snake_case						=					type_sequence_label_size
										_snake_case						=					initializer_range
										_snake_case						=					num_labels
										_snake_case						=					num_choices
										_snake_case						=					scope
										_snake_case						=					projection_dim




					def 						lowerCAmelCase      (		self	)		->				Dict:
										_snake_case						=					ids_tensor([self.batch_size, self.seq_length]					,    self.vocab_size	)

										_snake_case						=					None
										if self.use_input_mask:
															_snake_case						=					random_attention_mask([self.batch_size, self.seq_length]	)

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

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

										_snake_case						=					self.get_config()

										return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels




					def 						lowerCAmelCase      (		self	)		->				List[Any]:
										return DPRConfig(
										    projection_dim=self.projection_dim					,    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					,    initializer_range=self.initializer_range					,    )




					def 						lowerCAmelCase      (		self					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_	)		->				Optional[Any]:
										_snake_case						=					DPRContextEncoder(config=lowerCAmelCase_	)
										model.to(lowerCAmelCase_	)
										model.eval()
										_snake_case						=					model(lowerCAmelCase_					,    attention_mask=lowerCAmelCase_					,    token_type_ids=lowerCAmelCase_	)
										_snake_case						=					model(lowerCAmelCase_					,    token_type_ids=lowerCAmelCase_	)
										_snake_case						=					model(lowerCAmelCase_	)
										self.parent.assertEqual(result.pooler_output.shape					,    (self.batch_size, self.projection_dim or self.hidden_size)	)




					def 						lowerCAmelCase      (		self					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_	)		->				Tuple:
										_snake_case						=					DPRQuestionEncoder(config=lowerCAmelCase_	)
										model.to(lowerCAmelCase_	)
										model.eval()
										_snake_case						=					model(lowerCAmelCase_					,    attention_mask=lowerCAmelCase_					,    token_type_ids=lowerCAmelCase_	)
										_snake_case						=					model(lowerCAmelCase_					,    token_type_ids=lowerCAmelCase_	)
										_snake_case						=					model(lowerCAmelCase_	)
										self.parent.assertEqual(result.pooler_output.shape					,    (self.batch_size, self.projection_dim or self.hidden_size)	)




					def 						lowerCAmelCase      (		self					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_					,    lowerCAmelCase_	)		->				List[Any]:
										_snake_case						=					DPRReader(config=lowerCAmelCase_	)
										model.to(lowerCAmelCase_	)
										model.eval()
										_snake_case						=					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 						lowerCAmelCase      (		self	)		->				Any:
										_snake_case						=					self.prepare_config_and_inputs()
										(
										    (
										    _snake_case
										)				,  (
										    _snake_case
										)				,  (
										    _snake_case
										)				,  (
										    _snake_case
										)				,  (
										    _snake_case
										)				,  (
										    _snake_case
										)				,  (
										    _snake_case
										)				,  
										)						=					config_and_inputs
										_snake_case						=					{'input_ids': input_ids}
										return config, inputs_dict





@require_torch
class       UpperCamelCase_		(	_lowerCamelCase				,	_lowerCamelCase				,	unittest.TestCase       ):
					lowerCAmelCase_				=						(
					    (
					        DPRContextEncoder,
					        DPRQuestionEncoder,
					        DPRReader,
					    )
					    if is_torch_available()
					    else ()
					)
					lowerCAmelCase_				=						{'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

					lowerCAmelCase_				=						False
					lowerCAmelCase_				=						False  # why?
					lowerCAmelCase_				=						False
					lowerCAmelCase_				=						False




					def 						lowerCAmelCase      (		self	)		->				Optional[int]:
										_snake_case						=					DPRModelTester(self	)
										_snake_case						=					ConfigTester(self					,    config_class=lowerCAmelCase_					,    hidden_size=37	)




					def 						lowerCAmelCase      (		self	)		->				Tuple:
										self.config_tester.run_common_tests()




					def 						lowerCAmelCase      (		self	)		->				List[Any]:
										_snake_case						=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_context_encoder(*lowerCAmelCase_	)




					def 						lowerCAmelCase      (		self	)		->				List[Any]:
										_snake_case						=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_question_encoder(*lowerCAmelCase_	)




					def 						lowerCAmelCase      (		self	)		->				str:
										_snake_case						=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_reader(*lowerCAmelCase_	)




					def 						lowerCAmelCase      (		self	)		->				List[Any]:
										_snake_case						=					self.model_tester.prepare_config_and_inputs()[0]

										_snake_case						=					DPRQuestionEncoder(config=lowerCAmelCase_	)
										model.to(lowerCAmelCase_	)
										model.eval()

										with tempfile.TemporaryDirectory() as tmp_dirname:
															model.save_pretrained(lowerCAmelCase_	)
															_snake_case						=					DPRQuestionEncoder.from_pretrained(lowerCAmelCase_					,    projection_dim=512	)

										self.assertIsNotNone(lowerCAmelCase_	)






					@slow
					def 						lowerCAmelCase      (		self	)		->				Optional[Any]:
										for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_snake_case						=					DPRContextEncoder.from_pretrained(lowerCAmelCase_	)
															self.assertIsNotNone(lowerCAmelCase_	)

										for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_snake_case						=					DPRContextEncoder.from_pretrained(lowerCAmelCase_	)
															self.assertIsNotNone(lowerCAmelCase_	)

										for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_snake_case						=					DPRQuestionEncoder.from_pretrained(lowerCAmelCase_	)
															self.assertIsNotNone(lowerCAmelCase_	)

										for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_snake_case						=					DPRReader.from_pretrained(lowerCAmelCase_	)
															self.assertIsNotNone(lowerCAmelCase_	)





@require_torch
class       UpperCamelCase_		(	unittest.TestCase       ):




					@slow
					def 						lowerCAmelCase      (		self	)		->				Optional[Any]:
										_snake_case						=					DPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'					,    return_dict=lowerCAmelCase_	)
										model.to(lowerCAmelCase_	)

										_snake_case						=					torch.tensor(
										    [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]					,    dtype=torch.long					,    device=lowerCAmelCase_	)  # [CLS] hello, is my dog cute? [SEP]
										_snake_case						=					model(lowerCAmelCase_	)[0]  # embedding shape = (1, 768)
										# compare the actual values for a slice.
										_snake_case						=					torch.tensor(
										    [
										        [
										            0.03_23_62_53,
										            0.12_75_33_35,
										            0.16_81_85_09,
										            0.00_27_97_86,
										            0.3_89_69_33,
										            0.24_26_49_45,
										            0.2_17_89_71,
										            -0.02_33_52_27,
										            -0.08_48_19_59,
										            -0.14_32_41_17,
										        ]
										    ]					,    dtype=torch.float					,    device=lowerCAmelCase_					,    )
										self.assertTrue(torch.allclose(output[:, :10]					,    lowerCAmelCase_					,    atol=1E-4	)	)






					@slow
					def 						lowerCAmelCase      (		self	)		->				str:
										_snake_case						=					DPRReaderTokenizer.from_pretrained('facebook/dpr-reader-single-nq-base'	)
										_snake_case						=					DPRReader.from_pretrained('facebook/dpr-reader-single-nq-base'	)
										model.to(lowerCAmelCase_	)

										_snake_case						=					tokenizer(
										    questions='What is love ?'					,    titles='Haddaway'					,    texts='What Is Love is a song recorded by the artist Haddaway'					,    padding=lowerCAmelCase_					,    return_tensors='pt'					,    )
										encoded_inputs.to(lowerCAmelCase_	)

										_snake_case						=					model(**lowerCAmelCase_	)

										# compare the actual values for a slice.
										_snake_case						=					torch.tensor(
										    [[-10.30_05, -10.77_65, -11.48_72, -11.68_41, -11.93_12, -10.30_02, -9.85_44, -11.73_78, -12.08_21, -10.29_75]]					,    dtype=torch.float					,    device=lowerCAmelCase_					,    )

										_snake_case						=					torch.tensor(
										    [[-11.06_84, -11.70_41, -11.53_97, -10.34_65, -10.87_91, -6.84_43, -11.99_59, -11.03_64, -10.00_96, -6.84_05]]					,    dtype=torch.float					,    device=lowerCAmelCase_					,    )
										self.assertTrue(torch.allclose(outputs.start_logits[:, :10]					,    lowerCAmelCase_					,    atol=1E-4	)	)
										self.assertTrue(torch.allclose(outputs.end_logits[:, :10]					,    lowerCAmelCase_					,    atol=1E-4	)	)










import math
from timeit import timeit





def     lowerCamelCase__  (  UpperCamelCase__      :							int    )      ->			int:



					'''simple docstring'''




					_snake_case						=					0
					_snake_case						=					abs(UpperCamelCase__    )
					while True:
										_snake_case						=					n // 10
										digits += 1
										if n == 0:
															break
					return digits





def     lowerCamelCase__  (  UpperCamelCase__      :							int    )      ->			int:



					'''simple docstring'''




					return 1 if n == 0 else math.floor(math.log(abs(UpperCamelCase__    )							,     10    ) + 1    )





def     lowerCamelCase__  (  UpperCamelCase__      :							int    )      ->			int:



					'''simple docstring'''




					return len(str(abs(UpperCamelCase__    )    )    )





def     lowerCamelCase__  (  )      ->			None:



					'''simple docstring'''




					from collections.abc import Callable

					def benchmark_a_function(UpperCamelCase__      :							Callable							,     UpperCamelCase__      :							int    ) -> None:
										_snake_case						=					F'''{func.__name__}({value})'''
										_snake_case						=					timeit(F'''__main__.{call}'''							,     setup='import __main__'    )
										print(F'''{call}: {func(UpperCamelCase__    )} -- {timing} seconds'''    )

					for value in (262_144, 1_125_899_906_842_624, 1_267_650_600_228_229_401_496_703_205_376):
										for func in (num_digits, num_digits_fast, num_digits_faster):
															benchmark_a_function(UpperCamelCase__							,     UpperCamelCase__    )
										print()


if __name__ == "__main__":
					import doctest

					doctest.testmod()
					benchmark()



