
"""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 	UpperCamelCase      :

				"""simple docstring"""



				def __init__(							self						,      lowercase__						,      lowercase__=13						,      lowercase__=7						,      lowercase__=False						,      lowercase__=True						,      lowercase__=True						,      lowercase__=True						,      lowercase__=99						,      lowercase__=32						,      lowercase__=5						,      lowercase__=4						,      lowercase__=37						,      lowercase__="gelu"						,      lowercase__=0.1						,      lowercase__=0.1						,      lowercase__=512						,      lowercase__=16						,      lowercase__=2						,      lowercase__=0.02						,      lowercase__=3						,      lowercase__=4						,      lowercase__=None						,      lowercase__=0						,      )       ->       str:
									"""simple docstring"""







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


				def 				A   (							self     )       ->       List[str]:
									"""simple docstring"""







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

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

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

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

									SCREAMING_SNAKE_CASE           =      self.get_config()

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


				def 				A   (							self     )       ->       Any:
									"""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 				A   (							self						,      lowercase__						,      lowercase__						,      lowercase__						,      lowercase__						,      lowercase__						,      lowercase__						,      lowercase__     )       ->       Dict:
									"""simple docstring"""







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


				def 				A   (							self						,      lowercase__						,      lowercase__						,      lowercase__						,      lowercase__						,      lowercase__						,      lowercase__						,      lowercase__     )       ->       str:
									"""simple docstring"""







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


				def 				A   (							self						,      lowercase__						,      lowercase__						,      lowercase__						,      lowercase__						,      lowercase__						,      lowercase__						,      lowercase__     )       ->       Optional[Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =      DPRReader(config=lowercase__     )
									model.to(lowercase__     )
									model.eval()
									SCREAMING_SNAKE_CASE           =      model(
									    lowercase__						,      attention_mask=lowercase__						,      )

									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 				A   (							self     )       ->       Dict:
									"""simple docstring"""







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






@require_torch
class 	UpperCamelCase      (			__magic_name__    , __magic_name__    , unittest.TestCase ):

				"""simple docstring"""



				UpperCAmelCase_							: Dict		    =							(
				    (
				        DPRContextEncoder,
				        DPRQuestionEncoder,
				        DPRReader,
				    )
				    if is_torch_available()
				    else ()
				)
				UpperCAmelCase_							: List[Any]		    =							{"feature-extraction": DPRQuestionEncoder} if is_torch_available() else {}

				UpperCAmelCase_							: List[Any]		    =							False
				UpperCAmelCase_							: Union[str, Any]		    =							False  # why?
				UpperCAmelCase_							: Tuple		    =							False
				UpperCAmelCase_							: Tuple		    =							False


				def 				A   (							self     )       ->       List[str]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =      DPRModelTester(self     )
									SCREAMING_SNAKE_CASE           =      ConfigTester(self						,      config_class=lowercase__						,      hidden_size=37     )


				def 				A   (							self     )       ->       Tuple:
									"""simple docstring"""







									self.config_tester.run_common_tests()


				def 				A   (							self     )       ->       Dict:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_context_encoder(*lowercase__     )


				def 				A   (							self     )       ->       int:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_question_encoder(*lowercase__     )


				def 				A   (							self     )       ->       List[Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_reader(*lowercase__     )


				def 				A   (							self     )       ->       List[Any]:
									"""simple docstring"""







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

									SCREAMING_SNAKE_CASE           =      DPRQuestionEncoder(config=lowercase__     )
									model.to(lowercase__     )
									model.eval()

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

									self.assertIsNotNone(lowercase__     )




				@slow
				def 				A   (							self     )       ->       Dict:
									"""simple docstring"""







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

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

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

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






@require_torch
class 	UpperCamelCase      (			unittest.TestCase ):

				"""simple docstring"""



				@slow
				def 				A   (							self     )       ->       Dict:
									"""simple docstring"""







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

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




				@slow
				def 				A   (							self     )       ->       List[Any]:
									"""simple docstring"""







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

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

									SCREAMING_SNAKE_CASE           =      model(**lowercase__     )

									# compare the actual values for a slice.
									SCREAMING_SNAKE_CASE           =      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=lowercase__						,      )

									SCREAMING_SNAKE_CASE           =      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=lowercase__						,      )
									self.assertTrue(torch.allclose(outputs.start_logits[:, :10]						,      lowercase__						,      atol=1E-4     )     )
									self.assertTrue(torch.allclose(outputs.end_logits[:, :10]						,      lowercase__						,      atol=1E-4     )     )


"""simple docstring"""




from typing import Dict, List, Optional, Tuple

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


snake_case					  =						logging.get_logger(__name__)




class 	UpperCamelCase      (			__magic_name__ ):

				"""simple docstring"""



				UpperCAmelCase_							: Dict		    =							["input_ids", "attention_mask"]


				def __init__(							self						,      lowercase__="[PAD]"						,      lowercase__="[BOS]"						,      lowercase__="[EOS]"						,      lowercase__="[MASK]"						,      lowercase__="[CLS]"						,      lowercase__="[SEP]"						,      lowercase__=2048						,      **lowercase__						,      )       ->       None:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =      AddedToken(lowercase__						,      lstrip=lowercase__						,      rstrip=lowercase__     ) if isinstance(lowercase__						,      lowercase__     ) else pad_token
									SCREAMING_SNAKE_CASE           =      AddedToken(lowercase__						,      lstrip=lowercase__						,      rstrip=lowercase__     ) if isinstance(lowercase__						,      lowercase__     ) else bos_token
									SCREAMING_SNAKE_CASE           =      AddedToken(lowercase__						,      lstrip=lowercase__						,      rstrip=lowercase__     ) if isinstance(lowercase__						,      lowercase__     ) else eos_token
									SCREAMING_SNAKE_CASE           =      AddedToken(lowercase__						,      lstrip=lowercase__						,      rstrip=lowercase__     ) if isinstance(lowercase__						,      lowercase__     ) else mask_token
									SCREAMING_SNAKE_CASE           =      AddedToken(lowercase__						,      lstrip=lowercase__						,      rstrip=lowercase__     ) if isinstance(lowercase__						,      lowercase__     ) else cls_token
									SCREAMING_SNAKE_CASE           =      AddedToken(lowercase__						,      lstrip=lowercase__						,      rstrip=lowercase__     ) if isinstance(lowercase__						,      lowercase__     ) else sep_token

									super().__init__(
									    pad_token=lowercase__						,      bos_token=lowercase__						,      eos_token=lowercase__						,      mask_token=lowercase__						,      cls_token=lowercase__						,      sep_token=lowercase__						,      model_max_length=lowercase__						,      **lowercase__						,      )

									SCREAMING_SNAKE_CASE           =      2**8  # utf is 8 bits

									# define special tokens dict
									SCREAMING_SNAKE_CASE           =      {
									    self.pad_token: 0,
									    self.bos_token: 1,
									    self.eos_token: 2,
									    self.mask_token: 3,
									    self.cls_token: 4,
									    self.sep_token: 5,
									}
									SCREAMING_SNAKE_CASE           =      len(self.special_tokens_encoder     )
									SCREAMING_SNAKE_CASE           =      {v: k for k, v in self.special_tokens_encoder.items()}


				def 				A   (							self     )       ->       Dict[str, int]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =      self.special_tokens_encoder.copy()
									vocab.update(self.added_tokens_encoder     )
									for i in range(self._utf_vocab_size     ):
														SCREAMING_SNAKE_CASE           =      chr(lowercase__     )
														SCREAMING_SNAKE_CASE           =      i + len(self.special_tokens_encoder     )
									return vocab


				@property
				def 				A   (							self     )       ->       List[Any]:
									"""simple docstring"""







									return self._utf_vocab_size + self._num_special_tokens


				def 				A   (							self						,      lowercase__						,      lowercase__ = None						,      lowercase__ = False     )       ->       List[int]:
									"""simple docstring"""







									if already_has_special_tokens:
														return super().get_special_tokens_mask(
														    token_ids_a=lowercase__						,      token_ids_a=lowercase__						,      already_has_special_tokens=lowercase__     )

									# normal case: some special tokens
									if token_ids_a is None:
														return [1] + [0] * len(lowercase__     ) + [1]
									return [1] + ([0] * len(lowercase__     )) + [1] + ([0] * len(lowercase__     )) + [1]


				def 				A   (							self						,      lowercase__						,      lowercase__ = None     )       ->       List[int]:
									"""simple docstring"""







									if token_ids_a is None:
														return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
									else:
														return [self.cls_token_id] + token_ids_a + [self.sep_token_id] + token_ids_a + [self.sep_token_id]


				def 				A   (							self						,      lowercase__     )       ->       List[str]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =      [chr(lowercase__     ) for i in text.encode('utf-8'     )]
									return tokens


				def 				A   (							self						,      lowercase__     )       ->       str:
									"""simple docstring"""







									if token in self.special_tokens_encoder:
														SCREAMING_SNAKE_CASE           =      self.special_tokens_encoder[token]
									elif token in self.added_tokens_encoder:
														SCREAMING_SNAKE_CASE           =      self.added_tokens_encoder[token]
									elif len(lowercase__     ) != 1:
														SCREAMING_SNAKE_CASE           =      self.unk_token_id
									else:
														SCREAMING_SNAKE_CASE           =      ord(lowercase__     ) + self._num_special_tokens
									return token_id


				def 				A   (							self						,      lowercase__     )       ->       int:
									"""simple docstring"""







									if index in self.special_tokens_decoder:
														SCREAMING_SNAKE_CASE           =      self.special_tokens_decoder[index]
									elif index in self.added_tokens_decoder:
														SCREAMING_SNAKE_CASE           =      self.added_tokens_decoder[index]
									else:
														SCREAMING_SNAKE_CASE           =      chr(index - self._num_special_tokens     )
									return token


				def 				A   (							self						,      lowercase__     )       ->       Dict:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE           =      b''
									for token in tokens:
														if token in self.special_tokens_decoder:
																			SCREAMING_SNAKE_CASE           =      self.special_tokens_decoder[token].encode('utf-8'     )
														elif token in self.added_tokens_decoder:
																			SCREAMING_SNAKE_CASE           =      self.special_tokens_decoder[token].encode('utf-8'     )
														elif token in self.special_tokens_encoder:
																			SCREAMING_SNAKE_CASE           =      token.encode('utf-8'     )
														elif token in self.added_tokens_encoder:
																			SCREAMING_SNAKE_CASE           =      token.encode('utf-8'     )
														else:
																			SCREAMING_SNAKE_CASE           =      bytes([ord(lowercase__     )]     )
														bstring += tok_string
									SCREAMING_SNAKE_CASE           =      bstring.decode('utf-8'						,      errors='replace'     )
									return string




				def 				A   (							self						,      lowercase__						,      lowercase__ = None     )       ->       Tuple[str]:
									"""simple docstring"""







									return ()
