

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:			Optional[Any]	,			__UpperCamelCase:			int	,			__UpperCamelCase:			Dict=13	,			__UpperCamelCase:			List[str]=7	,			__UpperCamelCase:			Optional[int]=False	,			__UpperCamelCase:			str=True	,			__UpperCamelCase:			Union[str, Any]=True	,			__UpperCamelCase:			Dict=True	,			__UpperCamelCase:			Optional[int]=99	,			__UpperCamelCase:			Any=32	,			__UpperCamelCase:			Tuple=5	,			__UpperCamelCase:			Any=4	,			__UpperCamelCase:			int=37	,			__UpperCamelCase:			Optional[Any]="gelu"	,			__UpperCamelCase:			Any=0.1	,			__UpperCamelCase:			Dict=0.1	,			__UpperCamelCase:			Dict=5_12	,			__UpperCamelCase:			Tuple=16	,			__UpperCamelCase:			Optional[Any]=2	,			__UpperCamelCase:			Dict=0.02	,			__UpperCamelCase:			str=3	,			__UpperCamelCase:			Optional[Any]=4	,			__UpperCamelCase:			List[str]=None	,			__UpperCamelCase:			Optional[Any]=0	,			):

						'''simple docstring'''





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







	def 						_SCREAMING_SNAKE_CASE				( self:			List[str]   ):

						'''simple docstring'''





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

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

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

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

						__magic_name__				=     self.get_config()

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







	def 						_SCREAMING_SNAKE_CASE				( self:			int   ):

						'''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 						_SCREAMING_SNAKE_CASE				( self:			List[str]	,			__UpperCamelCase:			Any	,			__UpperCamelCase:			Union[str, Any]	,			__UpperCamelCase:			Tuple	,			__UpperCamelCase:			Dict	,			__UpperCamelCase:			Any	,			__UpperCamelCase:			List[str]	,			__UpperCamelCase:			Optional[int]   ):

						'''simple docstring'''





						__magic_name__				=     DPRContextEncoder(config=__UpperCamelCase   )
						model.to(__UpperCamelCase   )
						model.eval()
						__magic_name__				=     model(__UpperCamelCase	,			attention_mask=__UpperCamelCase	,			token_type_ids=__UpperCamelCase   )
						__magic_name__				=     model(__UpperCamelCase	,			token_type_ids=__UpperCamelCase   )
						__magic_name__				=     model(__UpperCamelCase   )
						self.parent.assertEqual(result.pooler_output.shape	,			(self.batch_size, self.projection_dim or self.hidden_size)   )







	def 						_SCREAMING_SNAKE_CASE				( self:			Optional[int]	,			__UpperCamelCase:			List[str]	,			__UpperCamelCase:			Dict	,			__UpperCamelCase:			Tuple	,			__UpperCamelCase:			Any	,			__UpperCamelCase:			Any	,			__UpperCamelCase:			Optional[Any]	,			__UpperCamelCase:			List[str]   ):

						'''simple docstring'''





						__magic_name__				=     DPRQuestionEncoder(config=__UpperCamelCase   )
						model.to(__UpperCamelCase   )
						model.eval()
						__magic_name__				=     model(__UpperCamelCase	,			attention_mask=__UpperCamelCase	,			token_type_ids=__UpperCamelCase   )
						__magic_name__				=     model(__UpperCamelCase	,			token_type_ids=__UpperCamelCase   )
						__magic_name__				=     model(__UpperCamelCase   )
						self.parent.assertEqual(result.pooler_output.shape	,			(self.batch_size, self.projection_dim or self.hidden_size)   )







	def 						_SCREAMING_SNAKE_CASE				( self:			Optional[int]	,			__UpperCamelCase:			Any	,			__UpperCamelCase:			Optional[int]	,			__UpperCamelCase:			Union[str, Any]	,			__UpperCamelCase:			Optional[Any]	,			__UpperCamelCase:			Any	,			__UpperCamelCase:			Tuple	,			__UpperCamelCase:			Dict   ):

						'''simple docstring'''





						__magic_name__				=     DPRReader(config=__UpperCamelCase   )
						model.to(__UpperCamelCase   )
						model.eval()
						__magic_name__				=     model(
						    __UpperCamelCase	,			attention_mask=__UpperCamelCase	,			)

						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 						_SCREAMING_SNAKE_CASE				( self:			List[Any]   ):

						'''simple docstring'''





						__magic_name__				=     self.prepare_config_and_inputs()
						(
						    (
						    __magic_name__
						),						(
						    __magic_name__
						),						(
						    __magic_name__
						),						(
						    __magic_name__
						),						(
						    __magic_name__
						),						(
						    __magic_name__
						),						(
						    __magic_name__
						),						
						)				=     config_and_inputs
						__magic_name__				=     {'input_ids': input_ids}
						return config, inputs_dict




@require_torch
class 					__UpperCamelCase					(       SCREAMING_SNAKE_CASE	,    SCREAMING_SNAKE_CASE	,    unittest.TestCase      ):
	_lowercase	: Dict												=	(
	    (
	        DPRContextEncoder,
	        DPRQuestionEncoder,
	        DPRReader,
	    )
	    if is_torch_available()
	    else ()
	)
	_lowercase	: Tuple												=	{"feature-extraction": DPRQuestionEncoder} if is_torch_available() else {}

	_lowercase	: Any												=	False
	_lowercase	: str												=	False  # why?
	_lowercase	: Union[str, Any]												=	False
	_lowercase	: Optional[Any]												=	False







	def 						_SCREAMING_SNAKE_CASE				( self:			Dict   ):

						'''simple docstring'''





						__magic_name__				=     DPRModelTester(self   )
						__magic_name__				=     ConfigTester(self	,			config_class=__UpperCamelCase	,			hidden_size=37   )







	def 						_SCREAMING_SNAKE_CASE				( self:			List[Any]   ):

						'''simple docstring'''





						self.config_tester.run_common_tests()







	def 						_SCREAMING_SNAKE_CASE				( self:			Any   ):

						'''simple docstring'''





						__magic_name__				=     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_context_encoder(*__UpperCamelCase   )







	def 						_SCREAMING_SNAKE_CASE				( self:			List[str]   ):

						'''simple docstring'''





						__magic_name__				=     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_question_encoder(*__UpperCamelCase   )







	def 						_SCREAMING_SNAKE_CASE				( self:			str   ):

						'''simple docstring'''





						__magic_name__				=     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_reader(*__UpperCamelCase   )







	def 						_SCREAMING_SNAKE_CASE				( self:			Tuple   ):

						'''simple docstring'''





						__magic_name__				=     self.model_tester.prepare_config_and_inputs()[0]

						__magic_name__				=     DPRQuestionEncoder(config=__UpperCamelCase   )
						model.to(__UpperCamelCase   )
						model.eval()

						with tempfile.TemporaryDirectory() as tmp_dirname:
											model.save_pretrained(__UpperCamelCase   )
											__magic_name__				=     DPRQuestionEncoder.from_pretrained(__UpperCamelCase	,			projection_dim=5_12   )

						self.assertIsNotNone(__UpperCamelCase   )







	@slow
	def 						_SCREAMING_SNAKE_CASE				( self:			Dict   ):

						'''simple docstring'''





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

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

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

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




@require_torch
class 					__UpperCamelCase					(       unittest.TestCase      ):







	@slow
	def 						_SCREAMING_SNAKE_CASE				( self:			str   ):

						'''simple docstring'''





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

						__magic_name__				=     torch.tensor(
						    [[1_01, 75_92, 10_10, 20_03, 20_26, 38_99, 1_01_40, 10_29, 1_02]]	,			dtype=torch.long	,			device=__UpperCamelCase   )  # [CLS] hello, is my dog cute? [SEP]
						__magic_name__				=     model(__UpperCamelCase   )[0]  # embedding shape = (1, 768)
						# compare the actual values for a slice.
						__magic_name__				=     torch.tensor(
						    [
						        [
						            0.03236253,
						            0.12753335,
						            0.16818509,
						            0.00279786,
						            0.3896933,
						            0.24264945,
						            0.2178971,
						            -0.02335227,
						            -0.08481959,
						            -0.14324117,
						        ]
						    ]	,			dtype=torch.float	,			device=__UpperCamelCase	,			)
						self.assertTrue(torch.allclose(output[:, :10]	,			__UpperCamelCase	,			atol=1E-4   )   )







	@slow
	def 						_SCREAMING_SNAKE_CASE				( self:			Dict   ):

						'''simple docstring'''





						__magic_name__				=     DPRReaderTokenizer.from_pretrained('facebook/dpr-reader-single-nq-base'   )
						__magic_name__				=     DPRReader.from_pretrained('facebook/dpr-reader-single-nq-base'   )
						model.to(__UpperCamelCase   )

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

						__magic_name__				=     model(**__UpperCamelCase   )

						# compare the actual values for a slice.
						__magic_name__				=     torch.tensor(
						    [[-10.3005, -10.7765, -11.4872, -11.6841, -11.9312, -10.3002, -9.8544, -11.7378, -12.0821, -10.2975]]	,			dtype=torch.float	,			device=__UpperCamelCase	,			)

						__magic_name__				=     torch.tensor(
						    [[-11.0684, -11.7041, -11.5397, -10.3465, -10.8791, -6.8443, -11.9959, -11.0364, -10.0096, -6.8405]]	,			dtype=torch.float	,			device=__UpperCamelCase	,			)
						self.assertTrue(torch.allclose(outputs.start_logits[:, :10]	,			__UpperCamelCase	,			atol=1E-4   )   )
						self.assertTrue(torch.allclose(outputs.end_logits[:, :10]	,			__UpperCamelCase	,			atol=1E-4   )   )







from ...configuration_utils import PretrainedConfig
from ...utils import logging


A__							=				logging.get_logger(__name__)

A__							=				{
    "tau/splinter-base": "https://huggingface.co/tau/splinter-base/resolve/main/config.json",
    "tau/splinter-base-qass": "https://huggingface.co/tau/splinter-base-qass/resolve/main/config.json",
    "tau/splinter-large": "https://huggingface.co/tau/splinter-large/resolve/main/config.json",
    "tau/splinter-large-qass": "https://huggingface.co/tau/splinter-large-qass/resolve/main/config.json",
    # See all Splinter models at https://huggingface.co/models?filter=splinter
}

class 					__UpperCamelCase					(       SCREAMING_SNAKE_CASE      ):
	_lowercase	: int												=	"splinter"







	def __init__( self:			str	,			__UpperCamelCase:			Any=3_05_22	,			__UpperCamelCase:			List[Any]=7_68	,			__UpperCamelCase:			str=12	,			__UpperCamelCase:			Optional[Any]=12	,			__UpperCamelCase:			Optional[int]=30_72	,			__UpperCamelCase:			List[str]="gelu"	,			__UpperCamelCase:			Union[str, Any]=0.1	,			__UpperCamelCase:			Dict=0.1	,			__UpperCamelCase:			str=5_12	,			__UpperCamelCase:			Dict=2	,			__UpperCamelCase:			str=0.02	,			__UpperCamelCase:			Tuple=1E-12	,			__UpperCamelCase:			Optional[int]=True	,			__UpperCamelCase:			Union[str, Any]=0	,			__UpperCamelCase:			Optional[int]=1_04	,			**__UpperCamelCase:			Tuple	,			):

						'''simple docstring'''





						super().__init__(pad_token_id=__UpperCamelCase	,			**__UpperCamelCase   )

						__magic_name__				=     vocab_size
						__magic_name__				=     max_position_embeddings
						__magic_name__				=     hidden_size
						__magic_name__				=     num_hidden_layers
						__magic_name__				=     num_attention_heads
						__magic_name__				=     intermediate_size
						__magic_name__				=     hidden_act
						__magic_name__				=     hidden_dropout_prob
						__magic_name__				=     attention_probs_dropout_prob
						__magic_name__				=     initializer_range
						__magic_name__				=     type_vocab_size
						__magic_name__				=     layer_norm_eps
						__magic_name__				=     use_cache
						__magic_name__				=     question_token_id




