






"""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       ,  snake_case__       ,  snake_case__=13       ,  snake_case__=7       ,  snake_case__=False       ,  snake_case__=True       ,  snake_case__=True       ,  snake_case__=True       ,  snake_case__=99       ,  snake_case__=32       ,  snake_case__=5       ,  snake_case__=4       ,  snake_case__=37       ,  snake_case__="gelu"       ,  snake_case__=0.1       ,  snake_case__=0.1       ,  snake_case__=512       ,  snake_case__=16       ,  snake_case__=2       ,  snake_case__=0.02       ,  snake_case__=3       ,  snake_case__=4       ,  snake_case__=None       ,  snake_case__=0       ,  ):
												'''simple docstring'''
												lowercase__				:      Dict=       parent
												lowercase__				:      Optional[int]=       batch_size
												lowercase__				:      Dict=       seq_length
												lowercase__				:      List[str]=       is_training
												lowercase__				:      str=       use_input_mask
												lowercase__				:      int=       use_token_type_ids
												lowercase__				:      Any=       use_labels
												lowercase__				:      str=       vocab_size
												lowercase__				:      List[Any]=       hidden_size
												lowercase__				:      int=       num_hidden_layers
												lowercase__				:      List[str]=       num_attention_heads
												lowercase__				:      List[Any]=       intermediate_size
												lowercase__				:      Any=       hidden_act
												lowercase__				:      Dict=       hidden_dropout_prob
												lowercase__				:      List[str]=       attention_probs_dropout_prob
												lowercase__				:      List[Any]=       max_position_embeddings
												lowercase__				:      List[Any]=       type_vocab_size
												lowercase__				:      List[Any]=       type_sequence_label_size
												lowercase__				:      str=       initializer_range
												lowercase__				:      List[str]=       num_labels
												lowercase__				:      int=       num_choices
												lowercase__				:      List[str]=       scope
												lowercase__				:      Union[str, Any]=       projection_dim




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Optional[Any]=       ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size			)

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

												lowercase__				:      List[str]=       None
												if self.use_token_type_ids:
																		lowercase__				:      List[Any]=       ids_tensor([self.batch_size, self.seq_length]       ,  self.type_vocab_size			)

												lowercase__				:      int=       None
												lowercase__				:      Any=       None
												lowercase__				:      Any=       None
												if self.use_labels:
																		lowercase__				:      Optional[Any]=       ids_tensor([self.batch_size]       ,  self.type_sequence_label_size			)
																		lowercase__				:      Optional[Any]=       ids_tensor([self.batch_size, self.seq_length]       ,  self.num_labels			)
																		lowercase__				:      Optional[Any]=       ids_tensor([self.batch_size]       ,  self.num_choices			)

												lowercase__				:      Dict=       self.get_config()

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




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




						def 		UpperCAmelCase_       (   self       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__			):
												'''simple docstring'''
												lowercase__				:      List[str]=       DPRQuestionEncoder(config=snake_case__			)
												model.to(snake_case__			)
												model.eval()
												lowercase__				:      Optional[int]=       model(snake_case__       ,  attention_mask=snake_case__       ,  token_type_ids=snake_case__			)
												lowercase__				:      str=       model(snake_case__       ,  token_type_ids=snake_case__			)
												lowercase__				:      Any=       model(snake_case__			)
												self.parent.assertEqual(result.pooler_output.shape       ,  (self.batch_size, self.projection_dim or self.hidden_size)			)




						def 		UpperCAmelCase_       (   self       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__			):
												'''simple docstring'''
												lowercase__				:      List[Any]=       DPRReader(config=snake_case__			)
												model.to(snake_case__			)
												model.eval()
												lowercase__				:      Union[str, Any]=       model(
												    snake_case__       ,  attention_mask=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 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      int=       self.prepare_config_and_inputs()
												(
												    (
												    lowercase__
												),	(
												    lowercase__
												),	(
												    lowercase__
												),	(
												    lowercase__
												),	(
												    lowercase__
												),	(
												    lowercase__
												),	(
												    lowercase__
												),	
												)				:      str=       config_and_inputs
												lowercase__				:      Dict=       {"input_ids": input_ids}
												return config, inputs_dict


@require_torch
class     __UpperCAmelCase(   SCREAMING_SNAKE_CASE__  ,  SCREAMING_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 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      List[str]=       DPRModelTester(self			)
												lowercase__				:      int=       ConfigTester(self       ,  config_class=snake_case__       ,  hidden_size=37			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												self.config_tester.run_common_tests()




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Optional[int]=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_context_encoder(*snake_case__			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Union[str, Any]=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_question_encoder(*snake_case__			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Optional[Any]=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_reader(*snake_case__			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      int=       self.model_tester.prepare_config_and_inputs()[0]

												lowercase__				:      Tuple=       DPRQuestionEncoder(config=snake_case__			)
												model.to(snake_case__			)
												model.eval()

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

												self.assertIsNotNone(snake_case__			)




						@slow
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		lowercase__				:      Union[str, Any]=       DPRContextEncoder.from_pretrained(snake_case__			)
																		self.assertIsNotNone(snake_case__			)

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

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

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


@require_torch
class     __UpperCAmelCase(   unittest.TestCase							):




						"""simple docstring"""




						@slow
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      str=       DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"       ,  return_dict=snake_case__			)
												model.to(snake_case__			)

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




						@slow
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Dict=       DPRReaderTokenizer.from_pretrained("facebook/dpr-reader-single-nq-base"			)
												lowercase__				:      List[Any]=       DPRReader.from_pretrained("facebook/dpr-reader-single-nq-base"			)
												model.to(snake_case__			)

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

												lowercase__				:      List[Any]=       model(**snake_case__			)

												# compare the actual values for a slice.
												lowercase__				:      Optional[Any]=       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=snake_case__       ,  )

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











"""simple docstring"""







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


UpperCAmelCase:   Dict       			=							logging.get_logger(__name__)

UpperCAmelCase:   Optional[Any]       			=							{
    """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    UpperCamelCase     (			snake_case    ):




	"""simple docstring"""
	SCREAMING_SNAKE_CASE_				:    Dict		    =   "opt"
	SCREAMING_SNAKE_CASE_				:    List[str]		    =   ["past_key_values"]






	def __init__(			self							,UpperCAmelCase_=5_02_72							,UpperCAmelCase_=7_68							,UpperCAmelCase_=12							,UpperCAmelCase_=30_72							,UpperCAmelCase_=20_48							,UpperCAmelCase_=True							,UpperCAmelCase_=False							,UpperCAmelCase_=None							,UpperCAmelCase_=0.1							,UpperCAmelCase_=0.0							,UpperCAmelCase_=12							,UpperCAmelCase_="relu"							,UpperCAmelCase_=0.0							,UpperCAmelCase_=0.02							,UpperCAmelCase_=True							,UpperCAmelCase_=1							,UpperCAmelCase_=2							,UpperCAmelCase_=2							,UpperCAmelCase_=True							,UpperCAmelCase_=True							,**UpperCAmelCase_							,):
			super().__init__(
			    pad_token_id=UpperCAmelCase_							,bos_token_id=UpperCAmelCase_							,eos_token_id=UpperCAmelCase_							,**UpperCAmelCase_							,)
			_lowercase							:      Optional[Any]			=					vocab_size
			_lowercase							:      Tuple			=					max_position_embeddings
			_lowercase							:      Union[str, Any]			=					num_attention_heads
			_lowercase							:      List[str]			=					word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
			_lowercase							:      List[str]			=					ffn_dim
			_lowercase							:      List[str]			=					hidden_size
			_lowercase							:      List[Any]			=					num_hidden_layers
			_lowercase							:      List[Any]			=					dropout
			_lowercase							:      str			=					attention_dropout
			_lowercase							:      List[Any]			=					activation_function
			_lowercase							:      int			=					init_std
			_lowercase							:      List[Any]			=					layerdrop
			_lowercase							:      Tuple			=					use_cache
			_lowercase							:      Any			=					do_layer_norm_before
			# We keep these variables at `True` for backward compatibility.
			_lowercase							:      int			=					enable_bias
			_lowercase							:      str			=					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
			_lowercase							:      Optional[Any]			=					_remove_final_layer_norm



