






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					:	Tuple  ,  lowerCAmelCase__					:	Union[str, Any]  ,  lowerCAmelCase__					:	int=1_3  ,  lowerCAmelCase__					:	Optional[int]=7  ,  lowerCAmelCase__					:	Union[str, Any]=False  ,  lowerCAmelCase__					:	int=True  ,  lowerCAmelCase__					:	Dict=True  ,  lowerCAmelCase__					:	Dict=True  ,  lowerCAmelCase__					:	Any=9_9  ,  lowerCAmelCase__					:	Optional[Any]=3_2  ,  lowerCAmelCase__					:	str=5  ,  lowerCAmelCase__					:	Tuple=4  ,  lowerCAmelCase__					:	List[str]=3_7  ,  lowerCAmelCase__					:	Optional[int]="gelu"  ,  lowerCAmelCase__					:	Optional[Any]=0.1  ,  lowerCAmelCase__					:	List[str]=0.1  ,  lowerCAmelCase__					:	List[str]=5_1_2  ,  lowerCAmelCase__					:	Optional[int]=1_6  ,  lowerCAmelCase__					:	Any=2  ,  lowerCAmelCase__					:	List[str]=0.02  ,  lowerCAmelCase__					:	int=3  ,  lowerCAmelCase__					:	str=4  ,  lowerCAmelCase__					:	Any=None  ,  lowerCAmelCase__					:	Dict=0  ,  )							->	List[Any]:

      """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     __UpperCamelCase						(		self					:	str)							->	Union[str, Any]:

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

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





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

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





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

      """simple docstring"""
      SCREAMING_SNAKE_CASE				    =		DPRReader(config=lowerCAmelCase__)
      model.to(lowerCAmelCase__)
      model.eval()
      SCREAMING_SNAKE_CASE				    =		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					:	Any)							->	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 	a_(		lowercase__			,							lowercase__			,							unittest.TestCase	):



     """simple docstring"""



     __snake_case		:				Union[str, Any]  					=(
         (
             DPRContextEncoder,
             DPRQuestionEncoder,
             DPRReader,
         )
         if is_torch_available()
         else ()
     )
     __snake_case		:				Union[str, Any]  					={'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

     __snake_case		:				List[str]  					=False
     __snake_case		:				Tuple  					=False  # why?
     __snake_case		:				Tuple  					=False
     __snake_case		:				Optional[Any]  					=False





     def     __UpperCamelCase						(		self					:	Optional[int])							->	Tuple:

      """simple docstring"""
      SCREAMING_SNAKE_CASE				    =		DPRModelTester(self)
      SCREAMING_SNAKE_CASE				    =		ConfigTester(self  ,  config_class=lowerCAmelCase__  ,  hidden_size=3_7)





     def     __UpperCamelCase						(		self					:	Tuple)							->	List[Any]:

      """simple docstring"""
      self.config_tester.run_common_tests()





     def     __UpperCamelCase						(		self					:	Union[str, Any])							->	Optional[int]:

      """simple docstring"""
      SCREAMING_SNAKE_CASE				    =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_context_encoder(*lowerCAmelCase__)





     def     __UpperCamelCase						(		self					:	List[str])							->	int:

      """simple docstring"""
      SCREAMING_SNAKE_CASE				    =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_question_encoder(*lowerCAmelCase__)





     def     __UpperCamelCase						(		self					:	str)							->	Optional[Any]:

      """simple docstring"""
      SCREAMING_SNAKE_CASE				    =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_reader(*lowerCAmelCase__)





     def     __UpperCamelCase						(		self					:	Dict)							->	List[str]:

      """simple docstring"""
      SCREAMING_SNAKE_CASE				    =		self.model_tester.prepare_config_and_inputs()[0]

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

      with tempfile.TemporaryDirectory() as tmp_dirname:
       model.save_pretrained(lowerCAmelCase__)
       SCREAMING_SNAKE_CASE				    =		DPRQuestionEncoder.from_pretrained(lowerCAmelCase__  ,  projection_dim=5_1_2)

      self.assertIsNotNone(lowerCAmelCase__)





     @slow
     def     __UpperCamelCase						(		self					:	int)							->	Dict:

      """simple docstring"""
      for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       SCREAMING_SNAKE_CASE				    =		DPRContextEncoder.from_pretrained(lowerCAmelCase__)
       self.assertIsNotNone(lowerCAmelCase__)

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

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

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

@require_torch
class 	a_(		unittest.TestCase	):



     """simple docstring"""



     @slow
     def     __UpperCamelCase						(		self					:	List[Any])							->	Dict:

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

      SCREAMING_SNAKE_CASE				    =		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]
      SCREAMING_SNAKE_CASE				    =		model(lowerCAmelCase__)[0]  # embedding shape = (1, 768)
      # compare the actual values for a slice.
      SCREAMING_SNAKE_CASE				    =		torch.tensor(
          [
              [
                  0.03_23_62_53,
                  0.12_75_33_35,
                  0.16_81_85_09,
                  0.00_27_97_86,
                  0.3_89_69_33,
                  0.24_26_49_45,
                  0.2_17_89_71,
                  -0.02_33_52_27,
                  -0.08_48_19_59,
                  -0.14_32_41_17,
              ]
          ]  ,  dtype=torch.float  ,  device=lowerCAmelCase__  ,  )
      self.assertTrue(torch.allclose(output[:, :1_0]  ,  lowerCAmelCase__  ,  atol=1e-4))





     @slow
     def     __UpperCamelCase						(		self					:	int)							->	Optional[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(lowerCAmelCase__)

      SCREAMING_SNAKE_CASE				    =		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__)

      SCREAMING_SNAKE_CASE				    =		model(**lowerCAmelCase__)

      # compare the actual values for a slice.
      SCREAMING_SNAKE_CASE				    =		torch.tensor(
          [[-10.30_05, -10.77_65, -11.48_72, -11.68_41, -11.93_12, -10.30_02, -9.85_44, -11.73_78, -12.08_21, -10.29_75]]  ,  dtype=torch.float  ,  device=lowerCAmelCase__  ,  )

      SCREAMING_SNAKE_CASE				    =		torch.tensor(
          [[-11.06_84, -11.70_41, -11.53_97, -10.34_65, -10.87_91, -6.84_43, -11.99_59, -11.03_64, -10.00_96, -6.84_05]]  ,  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))








'''simple docstring'''




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


_lowerCAmelCase	:		List[str]		  =						logging.get_logger(__name__)

_lowerCAmelCase	:		int		  =						{
    'facebook/opt-125m': 'https://huggingface.co/facebook/opt-125m/blob/main/config.json',
    'facebook/opt-350m': 'https://huggingface.co/facebook/opt-350m/blob/main/config.json',
    'facebook/opt-1.3b': 'https://huggingface.co/facebook/opt-1.3b/blob/main/config.json',
    'facebook/opt-2.7b': 'https://huggingface.co/facebook/opt-2.7b/blob/main/config.json',
    'facebook/opt-6.7b': 'https://huggingface.co/facebook/opt-6.7b/blob/main/config.json',
    'facebook/opt-13b': 'https://huggingface.co/facebook/opt-13b/blob/main/config.json',
}



class 			lowerCAmelCase					(					a				):
	_lowerCamelCase :       Dict         =			"""opt"""
	_lowerCamelCase :       Optional[Any]         =			["""past_key_values"""]



	def __init__(       self			,       snake_case__=5_0272			,       snake_case__=768			,       snake_case__=12			,       snake_case__=3072			,       snake_case__=2048			,       snake_case__=True			,       snake_case__=False			,       snake_case__=None			,       snake_case__=0.1			,       snake_case__=0.0			,       snake_case__=12			,       snake_case__="relu"			,       snake_case__=0.0			,       snake_case__=0.0_2			,       snake_case__=True			,       snake_case__=1			,       snake_case__=2			,       snake_case__=2			,       snake_case__=True			,       snake_case__=True			,       **snake_case__			,       ):
		super().__init__(
		    pad_token_id=snake_case__			,       bos_token_id=snake_case__			,       eos_token_id=snake_case__			,       **snake_case__			,       )
		lowerCAmelCase       :     Union[str, Any]   				=			vocab_size
		lowerCAmelCase       :     List[Any]   				=			max_position_embeddings
		lowerCAmelCase       :     List[str]   				=			num_attention_heads
		lowerCAmelCase       :     Dict   				=			word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
		lowerCAmelCase       :     Optional[Any]   				=			ffn_dim
		lowerCAmelCase       :     Optional[int]   				=			hidden_size
		lowerCAmelCase       :     List[str]   				=			num_hidden_layers
		lowerCAmelCase       :     Union[str, Any]   				=			dropout
		lowerCAmelCase       :     List[str]   				=			attention_dropout
		lowerCAmelCase       :     Union[str, Any]   				=			activation_function
		lowerCAmelCase       :     Optional[Any]   				=			init_std
		lowerCAmelCase       :     List[str]   				=			layerdrop
		lowerCAmelCase       :     Dict   				=			use_cache
		lowerCAmelCase       :     List[Any]   				=			do_layer_norm_before
		# We keep these variables at `True` for backward compatibility.
		lowerCAmelCase       :     List[str]   				=			enable_bias
		lowerCAmelCase       :     Dict   				=			layer_norm_elementwise_affine

		# Note that the only purpose of `_remove_final_layer_norm` is to keep backward compatibility
		# with checkpoints that have been fine-tuned before transformers v4.20.1
		# see https://github.com/facebookresearch/metaseq/pull/164
		lowerCAmelCase       :     Tuple   				=			_remove_final_layer_norm
