




"""simple docstring"""





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


				'''simple docstring'''


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

				def 					_lowerCamelCase    (      self      :   Any					):
										snake_case__				:    Union[str, Any]								=    ids_tensor([self.batch_size, self.seq_length]					,self.vocab_size					)

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

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

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

										snake_case__				:    List[Any]								=    self.get_config()

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

				def 					_lowerCamelCase    (      self      :   Union[str, 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      :   List[Any]					,SCREAMING_SNAKE_CASE_      :   Optional[Any]					,SCREAMING_SNAKE_CASE_      :   str					,SCREAMING_SNAKE_CASE_      :   Optional[Any]					,SCREAMING_SNAKE_CASE_      :   Any					,SCREAMING_SNAKE_CASE_      :   Optional[Any]					,SCREAMING_SNAKE_CASE_      :   Any					,SCREAMING_SNAKE_CASE_      :   Optional[int]					):
										snake_case__				:    Optional[Any]								=    DPRContextEncoder(config=SCREAMING_SNAKE_CASE_					)
										model.to(SCREAMING_SNAKE_CASE_					)
										model.eval()
										snake_case__				:    Dict								=    model(SCREAMING_SNAKE_CASE_					,attention_mask=SCREAMING_SNAKE_CASE_					,token_type_ids=SCREAMING_SNAKE_CASE_					)
										snake_case__				:    Tuple								=    model(SCREAMING_SNAKE_CASE_					,token_type_ids=SCREAMING_SNAKE_CASE_					)
										snake_case__				:    Tuple								=    model(SCREAMING_SNAKE_CASE_					)
										self.parent.assertEqual(result.pooler_output.shape					,(self.batch_size, self.projection_dim or self.hidden_size)					)

				def 					_lowerCamelCase    (      self      :   List[str]					,SCREAMING_SNAKE_CASE_      :   int					,SCREAMING_SNAKE_CASE_      :   Dict					,SCREAMING_SNAKE_CASE_      :   Optional[Any]					,SCREAMING_SNAKE_CASE_      :   Optional[int]					,SCREAMING_SNAKE_CASE_      :   Any					,SCREAMING_SNAKE_CASE_      :   Union[str, Any]					,SCREAMING_SNAKE_CASE_      :   str					):
										snake_case__				:    Optional[Any]								=    DPRQuestionEncoder(config=SCREAMING_SNAKE_CASE_					)
										model.to(SCREAMING_SNAKE_CASE_					)
										model.eval()
										snake_case__				:    Tuple								=    model(SCREAMING_SNAKE_CASE_					,attention_mask=SCREAMING_SNAKE_CASE_					,token_type_ids=SCREAMING_SNAKE_CASE_					)
										snake_case__				:    Optional[Any]								=    model(SCREAMING_SNAKE_CASE_					,token_type_ids=SCREAMING_SNAKE_CASE_					)
										snake_case__				:    List[Any]								=    model(SCREAMING_SNAKE_CASE_					)
										self.parent.assertEqual(result.pooler_output.shape					,(self.batch_size, self.projection_dim or self.hidden_size)					)

				def 					_lowerCamelCase    (      self      :   Any					,SCREAMING_SNAKE_CASE_      :   List[str]					,SCREAMING_SNAKE_CASE_      :   Union[str, Any]					,SCREAMING_SNAKE_CASE_      :   Optional[int]					,SCREAMING_SNAKE_CASE_      :   Optional[Any]					,SCREAMING_SNAKE_CASE_      :   int					,SCREAMING_SNAKE_CASE_      :   Any					,SCREAMING_SNAKE_CASE_      :   int					):
										snake_case__				:    Optional[Any]								=    DPRReader(config=SCREAMING_SNAKE_CASE_					)
										model.to(SCREAMING_SNAKE_CASE_					)
										model.eval()
										snake_case__				:    Dict								=    model(
										    SCREAMING_SNAKE_CASE_					,attention_mask=SCREAMING_SNAKE_CASE_					,)

										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      :   List[str]					):
										snake_case__				:    Optional[Any]								=    self.prepare_config_and_inputs()
										(
										    (
										    snake_case__      
										)	,		(
										    snake_case__      
										)	,		(
										    snake_case__      
										)	,		(
										    snake_case__      
										)	,		(
										    snake_case__      
										)	,		(
										    snake_case__      
										)	,		(
										    snake_case__      
										)	,		      
										)				:    Tuple								=    config_and_inputs
										snake_case__				:    List[str]								=    {'''input_ids''': input_ids}
										return config, inputs_dict







@require_torch
class 					__lowercase				(	__snake_case  ,      __snake_case  ,      unittest.TestCase		):


				'''simple docstring'''


				_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[Any]					):
										snake_case__				:    Any								=    DPRModelTester(self					)
										snake_case__				:    Tuple								=    ConfigTester(self					,config_class=SCREAMING_SNAKE_CASE_					,hidden_size=3_7					)

				def 					_lowerCamelCase    (      self      :   Optional[int]					):
										self.config_tester.run_common_tests()

				def 					_lowerCamelCase    (      self      :   Tuple					):
										snake_case__				:    int								=    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_context_encoder(*SCREAMING_SNAKE_CASE_					)

				def 					_lowerCamelCase    (      self      :   Union[str, Any]					):
										snake_case__				:    Dict								=    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_question_encoder(*SCREAMING_SNAKE_CASE_					)

				def 					_lowerCamelCase    (      self      :   List[Any]					):
										snake_case__				:    List[str]								=    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_reader(*SCREAMING_SNAKE_CASE_					)

				def 					_lowerCamelCase    (      self      :   List[Any]					):
										snake_case__				:    List[Any]								=    self.model_tester.prepare_config_and_inputs()[0]

										snake_case__				:    int								=    DPRQuestionEncoder(config=SCREAMING_SNAKE_CASE_					)
										model.to(SCREAMING_SNAKE_CASE_					)
										model.eval()

										with tempfile.TemporaryDirectory() as tmp_dirname:
																model.save_pretrained(SCREAMING_SNAKE_CASE_					)
																snake_case__				:    Optional[Any]								=    DPRQuestionEncoder.from_pretrained(SCREAMING_SNAKE_CASE_					,projection_dim=5_1_2					)

										self.assertIsNotNone(SCREAMING_SNAKE_CASE_					)





				@slow
				def 					_lowerCamelCase    (      self      :   Optional[Any]					):
										for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																snake_case__				:    Optional[int]								=    DPRContextEncoder.from_pretrained(SCREAMING_SNAKE_CASE_					)
																self.assertIsNotNone(SCREAMING_SNAKE_CASE_					)

										for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																snake_case__				:    List[Any]								=    DPRContextEncoder.from_pretrained(SCREAMING_SNAKE_CASE_					)
																self.assertIsNotNone(SCREAMING_SNAKE_CASE_					)

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

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







@require_torch
class 					__lowercase				(	unittest.TestCase		):


				'''simple docstring'''


				@slow
				def 					_lowerCamelCase    (      self      :   Optional[int]					):
										snake_case__				:    str								=    DPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''					,return_dict=SCREAMING_SNAKE_CASE_					)
										model.to(SCREAMING_SNAKE_CASE_					)

										snake_case__				:    List[str]								=    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=SCREAMING_SNAKE_CASE_					)  # [CLS] hello, is my dog cute? [SEP]
										snake_case__				:    Any								=    model(SCREAMING_SNAKE_CASE_					)[0]  # embedding shape = (1, 768)
										# compare the actual values for a slice.
										snake_case__				:    int								=    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=SCREAMING_SNAKE_CASE_					,)
										self.assertTrue(torch.allclose(output[:, :1_0]					,SCREAMING_SNAKE_CASE_					,atol=1E-4					)					)





				@slow
				def 					_lowerCamelCase    (      self      :   Optional[int]					):
										snake_case__				:    Tuple								=    DPRReaderTokenizer.from_pretrained('''facebook/dpr-reader-single-nq-base'''					)
										snake_case__				:    List[str]								=    DPRReader.from_pretrained('''facebook/dpr-reader-single-nq-base'''					)
										model.to(SCREAMING_SNAKE_CASE_					)

										snake_case__				:    List[str]								=    tokenizer(
										    questions='''What is love ?'''					,titles='''Haddaway'''					,texts='''What Is Love is a song recorded by the artist Haddaway'''					,padding=SCREAMING_SNAKE_CASE_					,return_tensors='''pt'''					,)
										encoded_inputs.to(SCREAMING_SNAKE_CASE_					)

										snake_case__				:    Union[str, Any]								=    model(**SCREAMING_SNAKE_CASE_					)

										# compare the actual values for a slice.
										snake_case__				:    str								=    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=SCREAMING_SNAKE_CASE_					,)

										snake_case__				:    Optional[Any]								=    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=SCREAMING_SNAKE_CASE_					,)
										self.assertTrue(torch.allclose(outputs.start_logits[:, :1_0]					,SCREAMING_SNAKE_CASE_					,atol=1E-4					)					)
										self.assertTrue(torch.allclose(outputs.end_logits[:, :1_0]					,SCREAMING_SNAKE_CASE_					,atol=1E-4					)					)



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCamelCase_				:		int       						=       {
    """configuration_fsmt""": ["""FSMT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FSMTConfig"""],
    """tokenization_fsmt""": ["""FSMTTokenizer"""],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	lowerCamelCase_				:		Dict       						=       ["""FSMTForConditionalGeneration""", """FSMTModel""", """PretrainedFSMTModel"""]


if TYPE_CHECKING:
	from .configuration_fsmt import FSMT_PRETRAINED_CONFIG_ARCHIVE_MAP, FSMTConfig
	from .tokenization_fsmt import FSMTTokenizer

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_fsmt import FSMTForConditionalGeneration, FSMTModel, PretrainedFSMTModel

else:
	import sys

	lowerCamelCase_				:		Dict       						=       _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




