






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





				"""simple docstring"""







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




									"""simple docstring"""

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


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Any									=ids_tensor([self.batch_size, self.seq_length]				,    self.vocab_size       )

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

									__UpperCamelCase :   int									=None
									if self.use_token_type_ids:
														__UpperCamelCase :   Optional[Any]									=ids_tensor([self.batch_size, self.seq_length]				,    self.type_vocab_size       )

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

									__UpperCamelCase :   Any									=self.get_config()

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


				def  __lowercase				(					self       ):




									"""simple docstring"""

									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  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   Optional[int]									=DPRContextEncoder(config=lowerCamelCase__       )
									model.to(lowerCamelCase__       )
									model.eval()
									__UpperCamelCase :   Optional[Any]									=model(lowerCamelCase__				,    attention_mask=lowerCamelCase__				,    token_type_ids=lowerCamelCase__       )
									__UpperCamelCase :   Dict									=model(lowerCamelCase__				,    token_type_ids=lowerCamelCase__       )
									__UpperCamelCase :   Optional[Any]									=model(lowerCamelCase__       )
									self.parent.assertEqual(result.pooler_output.shape				,    (self.batch_size, self.projection_dim or self.hidden_size)       )


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   Optional[int]									=DPRQuestionEncoder(config=lowerCamelCase__       )
									model.to(lowerCamelCase__       )
									model.eval()
									__UpperCamelCase :   Any									=model(lowerCamelCase__				,    attention_mask=lowerCamelCase__				,    token_type_ids=lowerCamelCase__       )
									__UpperCamelCase :   Union[str, Any]									=model(lowerCamelCase__				,    token_type_ids=lowerCamelCase__       )
									__UpperCamelCase :   Dict									=model(lowerCamelCase__       )
									self.parent.assertEqual(result.pooler_output.shape				,    (self.batch_size, self.projection_dim or self.hidden_size)       )


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   Dict									=DPRReader(config=lowerCamelCase__       )
									model.to(lowerCamelCase__       )
									model.eval()
									__UpperCamelCase :   Any									=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  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   List[str]									=self.prepare_config_and_inputs()
									(
									    (
									    __UpperCamelCase
									)						,							(
									    __UpperCamelCase
									)						,							(
									    __UpperCamelCase
									)						,							(
									    __UpperCamelCase
									)						,							(
									    __UpperCamelCase
									)						,							(
									    __UpperCamelCase
									)						,							(
									    __UpperCamelCase
									)						,							
									) :   List[str]									=config_and_inputs
									__UpperCamelCase :   List[Any]									={'input_ids': input_ids}
									return config, inputs_dict




@require_torch
class 							__A	(     a ,	a ,	unittest.TestCase					):





				"""simple docstring"""







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

				UpperCamelCase__					: Optional[Any]     					=False
				UpperCamelCase__					: List[str]     					=False  # why?
				UpperCamelCase__					: Dict     					=False
				UpperCamelCase__					: str     					=False


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   int									=DPRModelTester(self       )
									__UpperCamelCase :   Optional[Any]									=ConfigTester(self				,    config_class=lowerCamelCase__				,    hidden_size=37       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									self.config_tester.run_common_tests()


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Any									=self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_context_encoder(*lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Optional[int]									=self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_question_encoder(*lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   List[Any]									=self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_reader(*lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   int									=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[str]									=DPRQuestionEncoder.from_pretrained(lowerCamelCase__				,    projection_dim=512       )

									self.assertIsNotNone(lowerCamelCase__       )







				@slow
				def  __lowercase				(					self       ):




									"""simple docstring"""

									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[int]									=DPRContextEncoder.from_pretrained(lowerCamelCase__       )
														self.assertIsNotNone(lowerCamelCase__       )

									for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														__UpperCamelCase :   List[Any]									=DPRQuestionEncoder.from_pretrained(lowerCamelCase__       )
														self.assertIsNotNone(lowerCamelCase__       )

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




@require_torch
class 							__A	(     unittest.TestCase					):





				"""simple docstring"""







				@slow
				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Tuple									=DPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'				,    return_dict=lowerCamelCase__       )
									model.to(lowerCamelCase__       )

									__UpperCamelCase :   Union[str, Any]									=torch.tensor(
									    [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]				,    dtype=torch.long				,    device=lowerCamelCase__       )  # [CLS] hello, is my dog cute? [SEP]
									__UpperCamelCase :   Tuple									=model(lowerCamelCase__       )[0]  # embedding shape = (1, 768)
									# compare the actual values for a slice.
									__UpperCamelCase :   Optional[Any]									=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[:, :10]				,    lowerCamelCase__				,    atol=1E-4       )       )







				@slow
				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   str									=DPRReaderTokenizer.from_pretrained('facebook/dpr-reader-single-nq-base'       )
									__UpperCamelCase :   List[str]									=DPRReader.from_pretrained('facebook/dpr-reader-single-nq-base'       )
									model.to(lowerCamelCase__       )

									__UpperCamelCase :   Union[str, Any]									=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 :   Any									=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 :   int									=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[:, :10]				,    lowerCamelCase__				,    atol=1E-4       )       )
									self.assertTrue(torch.allclose(outputs.end_logits[:, :10]				,    lowerCamelCase__				,    atol=1E-4       )       )









from __future__ import annotations






def 				A	(			a_				,a_   )	->		list[int]:
					__UpperCamelCase :   dict[int, int]									={}
					for index, val in enumerate(a_   ):
										__UpperCamelCase :   Union[str, Any]									=target - val
										if compl in chk_map:
															return [chk_map[compl], index]
										__UpperCamelCase :   List[Any]									=index
					return []


if __name__ == "__main__":
							import doctest

							doctest.testmod()
							print(f"{two_sum([2, 7, 11, 15], 9) = }")

