

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    :






					'''simple docstring'''



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





					def       _UpperCAmelCase						(							self	):
						UpperCAmelCase__:  int		       =   ids_tensor([self.batch_size, self.seq_length]							,			self.vocab_size	)

						UpperCAmelCase__:  Any		       =   None
						if self.use_input_mask:
							UpperCAmelCase__:  Optional[int]		       =   random_attention_mask([self.batch_size, self.seq_length]	)

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

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

						UpperCAmelCase__:  Tuple		       =   self.get_config()

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





					def       _UpperCAmelCase						(							self	):
						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       _UpperCAmelCase						(							self							,			lowerCamelCase__							,			lowerCamelCase__							,			lowerCamelCase__							,			lowerCamelCase__							,			lowerCamelCase__							,			lowerCamelCase__							,			lowerCamelCase__	):
						UpperCAmelCase__:  List[str]		       =   DPRContextEncoder(config=lowerCamelCase__	)
						model.to(lowerCamelCase__	)
						model.eval()
						UpperCAmelCase__:  Union[str, Any]		       =   model(lowerCamelCase__							,			attention_mask=lowerCamelCase__							,			token_type_ids=lowerCamelCase__	)
						UpperCAmelCase__:  List[str]		       =   model(lowerCamelCase__							,			token_type_ids=lowerCamelCase__	)
						UpperCAmelCase__:  Tuple		       =   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__	):
						UpperCAmelCase__:  Dict		       =   DPRQuestionEncoder(config=lowerCamelCase__	)
						model.to(lowerCamelCase__	)
						model.eval()
						UpperCAmelCase__:  List[Any]		       =   model(lowerCamelCase__							,			attention_mask=lowerCamelCase__							,			token_type_ids=lowerCamelCase__	)
						UpperCAmelCase__:  Optional[int]		       =   model(lowerCamelCase__							,			token_type_ids=lowerCamelCase__	)
						UpperCAmelCase__:  Optional[int]		       =   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__	):
						UpperCAmelCase__:  Tuple		       =   DPRReader(config=lowerCamelCase__	)
						model.to(lowerCamelCase__	)
						model.eval()
						UpperCAmelCase__:  int		       =   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	):
						UpperCAmelCase__:  Optional[Any]		       =   self.prepare_config_and_inputs()
						(
						    (
						    UpperCAmelCase__
						)       ,	(
						    UpperCAmelCase__
						)       ,	(
						    UpperCAmelCase__
						)       ,	(
						    UpperCAmelCase__
						)       ,	(
						    UpperCAmelCase__
						)       ,	(
						    UpperCAmelCase__
						)       ,	(
						    UpperCAmelCase__
						)       ,	
						):  Any		       =   config_and_inputs
						UpperCAmelCase__:  Optional[int]		       =   {"input_ids": input_ids}
						return config, inputs_dict

@require_torch
class __UpperCamelCase    (							_a				,_a				,unittest.TestCase		):






					'''simple docstring'''



					__magic_name__     			=      (
					    (
					        DPRContextEncoder,
					        DPRQuestionEncoder,
					        DPRReader,
					    )
					    if is_torch_available()
					    else ()
					)
					__magic_name__     			=      {"feature-extraction": DPRQuestionEncoder} if is_torch_available() else {}

					__magic_name__     			=      False
					__magic_name__     			=      False  # why?
					__magic_name__     			=      False
					__magic_name__     			=      False





					def       _UpperCAmelCase						(							self	):
						UpperCAmelCase__:  Dict		       =   DPRModelTester(self	)
						UpperCAmelCase__:  List[Any]		       =   ConfigTester(self							,			config_class=lowerCamelCase__							,			hidden_size=3_7	)





					def       _UpperCAmelCase						(							self	):
						self.config_tester.run_common_tests()





					def       _UpperCAmelCase						(							self	):
						UpperCAmelCase__:  Tuple		       =   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_context_encoder(*lowerCamelCase__	)





					def       _UpperCAmelCase						(							self	):
						UpperCAmelCase__:  Any		       =   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_question_encoder(*lowerCamelCase__	)





					def       _UpperCAmelCase						(							self	):
						UpperCAmelCase__:  Dict		       =   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_reader(*lowerCamelCase__	)





					def       _UpperCAmelCase						(							self	):
						UpperCAmelCase__:  Union[str, Any]		       =   self.model_tester.prepare_config_and_inputs()[0]

						UpperCAmelCase__:  Union[str, Any]		       =   DPRQuestionEncoder(config=lowerCamelCase__	)
						model.to(lowerCamelCase__	)
						model.eval()

						with tempfile.TemporaryDirectory() as tmp_dirname:
							model.save_pretrained(lowerCamelCase__	)
							UpperCAmelCase__:  List[Any]		       =   DPRQuestionEncoder.from_pretrained(lowerCamelCase__							,			projection_dim=5_1_2	)

						self.assertIsNotNone(lowerCamelCase__	)





					@slow
					def       _UpperCAmelCase						(							self	):
						for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase__:  Any		       =   DPRContextEncoder.from_pretrained(lowerCamelCase__	)
							self.assertIsNotNone(lowerCamelCase__	)

						for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase__:  Optional[Any]		       =   DPRContextEncoder.from_pretrained(lowerCamelCase__	)
							self.assertIsNotNone(lowerCamelCase__	)

						for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase__:  int		       =   DPRQuestionEncoder.from_pretrained(lowerCamelCase__	)
							self.assertIsNotNone(lowerCamelCase__	)

						for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase__:  Optional[Any]		       =   DPRReader.from_pretrained(lowerCamelCase__	)
							self.assertIsNotNone(lowerCamelCase__	)

@require_torch
class __UpperCamelCase    (							unittest.TestCase		):






					'''simple docstring'''



					@slow
					def       _UpperCAmelCase						(							self	):
						UpperCAmelCase__:  Any		       =   DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"							,			return_dict=lowerCamelCase__	)
						model.to(lowerCamelCase__	)

						UpperCAmelCase__:  List[Any]		       =   torch.tensor(
						    [[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]]							,			dtype=torch.long							,			device=lowerCamelCase__	)  # [CLS] hello, is my dog cute? [SEP]
						UpperCAmelCase__:  Optional[int]		       =   model(lowerCamelCase__	)[0]  # embedding shape = (1, 768)
						# compare the actual values for a slice.
						UpperCAmelCase__:  Dict		       =   torch.tensor(
						    [
						        [
						            0.03_236_253,
						            0.12_753_335,
						            0.16_818_509,
						            0.00_279_786,
						            0.3_896_933,
						            0.24_264_945,
						            0.2_178_971,
						            -0.02_335_227,
						            -0.08_481_959,
						            -0.14_324_117,
						        ]
						    ]							,			dtype=torch.float							,			device=lowerCamelCase__							,			)
						self.assertTrue(torch.allclose(output[:, :1_0]							,			lowerCamelCase__							,			atol=1e-4	)	)





					@slow
					def       _UpperCAmelCase						(							self	):
						UpperCAmelCase__:  List[str]		       =   DPRReaderTokenizer.from_pretrained("facebook/dpr-reader-single-nq-base"	)
						UpperCAmelCase__:  Dict		       =   DPRReader.from_pretrained("facebook/dpr-reader-single-nq-base"	)
						model.to(lowerCamelCase__	)

						UpperCAmelCase__:  str		       =   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__	)

						UpperCAmelCase__:  List[str]		       =   model(**lowerCamelCase__	)

						# compare the actual values for a slice.
						UpperCAmelCase__:  int		       =   torch.tensor(
						    [[-10.3_005, -10.7_765, -11.4_872, -11.6_841, -11.9_312, -10.3_002, -9.8_544, -11.7_378, -12.0_821, -10.2_975]]							,			dtype=torch.float							,			device=lowerCamelCase__							,			)

						UpperCAmelCase__:  List[Any]		       =   torch.tensor(
						    [[-11.0_684, -11.7_041, -11.5_397, -10.3_465, -10.8_791, -6.8_443, -11.9_959, -11.0_364, -10.0_096, -6.8_405]]							,			dtype=torch.float							,			device=lowerCamelCase__							,			)
						self.assertTrue(torch.allclose(outputs.start_logits[:, :1_0]							,			lowerCamelCase__							,			atol=1e-4	)	)
						self.assertTrue(torch.allclose(outputs.end_logits[:, :1_0]							,			lowerCamelCase__							,			atol=1e-4	)	)


from __future__ import annotations



def  _A  (			SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE			):
	assert n > 1 and a > 0 and greatest_common_divisor(SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE			) == 1
	((UpperCAmelCase__)       ,	(UpperCAmelCase__)       ,	(UpperCAmelCase__)):  Optional[Any]		       =   extended_gcd(SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE			)  # Implemented below
	UpperCAmelCase__:  List[str]		       =   (b * s) % n
	return x



def  _A  (			SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE			):
	((UpperCAmelCase__)       ,	(UpperCAmelCase__)):  int		       =   extended_euclid(SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE			)  # Implemented below
	if b < 0:
		UpperCAmelCase__:  List[str]		       =   (b % n + n) % n
	return b



def  _A  (			SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE			):
	UpperCAmelCase__:  Union[str, Any]		       =   invert_modulo(SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE			)
	UpperCAmelCase__:  Any		       =   (b * s) % n
	return x



def  _A  (			SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE			):
	assert a >= 0 and b >= 0

	if b == 0:
		UpperCAmelCase__       ,	UpperCAmelCase__       ,	UpperCAmelCase__:  List[Any]		       =   a, 1, 0
	else:
		((UpperCAmelCase__)       ,	(UpperCAmelCase__)       ,	(UpperCAmelCase__)):  Tuple		       =   extended_gcd(SCREAMING_SNAKE_CASE  ,a % b			)
		UpperCAmelCase__:  Union[str, Any]		       =   q
		UpperCAmelCase__:  Union[str, Any]		       =   p - q * (a // b)

	assert a % d == 0 and b % d == 0
	assert d == a * x + b * y

	return (d, x, y)



def  _A  (			SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE			):
	if b == 0:
		return (1, 0)
	((UpperCAmelCase__)       ,	(UpperCAmelCase__)):  List[str]		       =   extended_euclid(SCREAMING_SNAKE_CASE  ,a % b			)
	UpperCAmelCase__:  Optional[Any]		       =   a // b
	return (y, x - k * y)



def  _A  (			SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE			):
	if a < b:
		UpperCAmelCase__       ,	UpperCAmelCase__:  str		       =   b, a

	while a % b != 0:
		UpperCAmelCase__       ,	UpperCAmelCase__:  Union[str, Any]		       =   b, a % b

	return b


if __name__ == "__main__":
					from doctest import testmod

					testmod(name="""modular_division""", verbose=True)
					testmod(name="""modular_division2""", verbose=True)
					testmod(name="""invert_modulo""", verbose=True)
					testmod(name="""extended_gcd""", verbose=True)
					testmod(name="""extended_euclid""", verbose=True)
					testmod(name="""greatest_common_divisor""", verbose=True)