


'''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 _lowerCamelCase :



		'''simple docstring'''







		def __init__(      self	:		List[str]						,  _A	:		Tuple						,  _A	:		List[Any]=13						,  _A	:		Union[str, Any]=7						,  _A	:		Tuple=False						,  _A	:		Dict=True						,  _A	:		Optional[int]=True						,  _A	:		Optional[Any]=True						,  _A	:		Optional[int]=99						,  _A	:		Optional[Any]=32						,  _A	:		Tuple=5						,  _A	:		List[Any]=4						,  _A	:		int=37						,  _A	:		Dict="gelu"						,  _A	:		Tuple=0.1						,  _A	:		Tuple=0.1						,  _A	:		Tuple=512						,  _A	:		Optional[int]=16						,  _A	:		int=2						,  _A	:		Tuple=0.02						,  _A	:		Optional[int]=3						,  _A	:		str=4						,  _A	:		List[Any]=None						,  _A	:		Optional[Any]=0						,  )						->				Optional[int]:
			__magic_name__ :					int  			=  parent
			__magic_name__ :					Any  			=  batch_size
			__magic_name__ :					Optional[Any]  			=  seq_length
			__magic_name__ :					str  			=  is_training
			__magic_name__ :					Optional[Any]  			=  use_input_mask
			__magic_name__ :					List[Any]  			=  use_token_type_ids
			__magic_name__ :					Any  			=  use_labels
			__magic_name__ :					Dict  			=  vocab_size
			__magic_name__ :					int  			=  hidden_size
			__magic_name__ :					Dict  			=  num_hidden_layers
			__magic_name__ :					Optional[Any]  			=  num_attention_heads
			__magic_name__ :					Dict  			=  intermediate_size
			__magic_name__ :					Optional[int]  			=  hidden_act
			__magic_name__ :					Optional[Any]  			=  hidden_dropout_prob
			__magic_name__ :					List[str]  			=  attention_probs_dropout_prob
			__magic_name__ :					Any  			=  max_position_embeddings
			__magic_name__ :					List[Any]  			=  type_vocab_size
			__magic_name__ :					Optional[Any]  			=  type_sequence_label_size
			__magic_name__ :					int  			=  initializer_range
			__magic_name__ :					Optional[Any]  			=  num_labels
			__magic_name__ :					Tuple  			=  num_choices
			__magic_name__ :					int  			=  scope
			__magic_name__ :					Optional[int]  			=  projection_dim







		def      __lowerCAmelCase	(      self	:		List[Any]    )						->				int:
			__magic_name__ :					Optional[int]  			=  ids_tensor([self.batch_size, self.seq_length]						,  self.vocab_size    )

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

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

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

			__magic_name__ :					Optional[Any]  			=  self.get_config()

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







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







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







		def      __lowerCAmelCase	(      self	:		Tuple						,  _A	:		List[str]						,  _A	:		Tuple						,  _A	:		Optional[Any]						,  _A	:		Union[str, Any]						,  _A	:		Any						,  _A	:		Dict						,  _A	:		Union[str, Any]    )						->				List[str]:
			__magic_name__ :					Dict  			=  DPRReader(config=_A    )
			model.to(_A    )
			model.eval()
			__magic_name__ :					Optional[Any]  			=  model(
			    _A						,  attention_mask=_A						,  )

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







@require_torch
class _lowerCamelCase (		lowercase__	,  lowercase__	,  unittest.TestCase							):



		'''simple docstring'''







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

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







		def      __lowerCAmelCase	(      self	:		Dict    )						->				Optional[int]:
			__magic_name__ :					str  			=  DPRModelTester(self    )
			__magic_name__ :					str  			=  ConfigTester(self						,  config_class=_A						,  hidden_size=37    )







		def      __lowerCAmelCase	(      self	:		List[Any]    )						->				Any:
			self.config_tester.run_common_tests()







		def      __lowerCAmelCase	(      self	:		Tuple    )						->				Dict:
			__magic_name__ :					List[Any]  			=  self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_context_encoder(*_A    )







		def      __lowerCAmelCase	(      self	:		List[Any]    )						->				Union[str, Any]:
			__magic_name__ :					Union[str, Any]  			=  self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_question_encoder(*_A    )







		def      __lowerCAmelCase	(      self	:		Tuple    )						->				Any:
			__magic_name__ :					Optional[Any]  			=  self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_reader(*_A    )







		def      __lowerCAmelCase	(      self	:		Union[str, Any]    )						->				Dict:
			__magic_name__ :					str  			=  self.model_tester.prepare_config_and_inputs()[0]

			__magic_name__ :					int  			=  DPRQuestionEncoder(config=_A    )
			model.to(_A    )
			model.eval()

			with tempfile.TemporaryDirectory() as tmp_dirname:
				model.save_pretrained(_A    )
				__magic_name__ :					Union[str, Any]  			=  DPRQuestionEncoder.from_pretrained(_A						,  projection_dim=512    )

			self.assertIsNotNone(_A    )







		@slow
		def      __lowerCAmelCase	(      self	:		Dict    )						->				List[str]:
			for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__magic_name__ :					Any  			=  DPRContextEncoder.from_pretrained(_A    )
				self.assertIsNotNone(_A    )

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

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

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







@require_torch
class _lowerCamelCase (		unittest.TestCase							):



		'''simple docstring'''







		@slow
		def      __lowerCAmelCase	(      self	:		Tuple    )						->				List[Any]:
			__magic_name__ :					Union[str, Any]  			=  DPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'						,  return_dict=_A    )
			model.to(_A    )

			__magic_name__ :					Tuple  			=  torch.tensor(
			    [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]						,  dtype=torch.long						,  device=_A    )  # [CLS] hello, is my dog cute? [SEP]
			__magic_name__ :					List[str]  			=  model(_A    )[0]  # embedding shape = (1, 768)
			# compare the actual values for a slice.
			__magic_name__ :					Dict  			=  torch.tensor(
			    [
			        [
			            0.0323_6253,
			            0.1275_3335,
			            0.1681_8509,
			            0.0027_9786,
			            0.389_6933,
			            0.2426_4945,
			            0.217_8971,
			            -0.0233_5227,
			            -0.0848_1959,
			            -0.1432_4117,
			        ]
			    ]						,  dtype=torch.float						,  device=_A						,  )
			self.assertTrue(torch.allclose(output[:, :10]						,  _A						,  atol=1E-4    )    )







		@slow
		def      __lowerCAmelCase	(      self	:		List[Any]    )						->				Dict:
			__magic_name__ :					Union[str, Any]  			=  DPRReaderTokenizer.from_pretrained('facebook/dpr-reader-single-nq-base'    )
			__magic_name__ :					Optional[Any]  			=  DPRReader.from_pretrained('facebook/dpr-reader-single-nq-base'    )
			model.to(_A    )

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

			__magic_name__ :					Dict  			=  model(**_A    )

			# compare the actual values for a slice.
			__magic_name__ :					Dict  			=  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=_A						,  )

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



'''simple docstring'''

from sklearn.metrics import mean_absolute_error

import datasets


lowerCAmelCase   :Union[str, Any]								=    '''\
@article{scikit-learn,
 title={Scikit-learn: Machine Learning in {P}ython},
 author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
 journal={Journal of Machine Learning Research},
 volume={12},
 pages={2825--2830},
 year={2011}
}
'''

lowerCAmelCase   :int								=    '''\
Mean Absolute Error (MAE) is the mean of the magnitude of difference between the predicted and actual
values.
'''


lowerCAmelCase   :Optional[int]								=    '''
Args:
    predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)
        Estimated target values.
    references: array-like of shape (n_samples,) or (n_samples, n_outputs)
        Ground truth (correct) target values.
    sample_weight: array-like of shape (n_samples,), default=None
        Sample weights.
    multioutput: {"raw_values", "uniform_average"} or array-like of shape (n_outputs,), default="uniform_average"
        Defines aggregating of multiple output values. Array-like value defines weights used to average errors.

                 "raw_values" : Returns a full set of errors in case of multioutput input.

                 "uniform_average" : Errors of all outputs are averaged with uniform weight.

Returns:
    mae : mean absolute error.
        If multioutput is "raw_values", then mean absolute error is returned for each output separately. If multioutput is "uniform_average" or an ndarray of weights, then the weighted average of all output errors is returned.
        MAE output is non-negative floating point. The best value is 0.0.
Examples:

    >>> mae_metric = datasets.load_metric("mae")
    >>> predictions = [2.5, 0.0, 2, 8]
    >>> references = [3, -0.5, 2, 7]
    >>> results = mae_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'mae\': 0.5}

    If you\'re using multi-dimensional lists, then set the config as follows :

    >>> mae_metric = datasets.load_metric("mae", "multilist")
    >>> predictions = [[0.5, 1], [-1, 1], [7, -6]]
    >>> references = [[0, 2], [-1, 2], [8, -5]]
    >>> results = mae_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'mae\': 0.75}
    >>> results = mae_metric.compute(predictions=predictions, references=references, multioutput=\'raw_values\')
    >>> print(results)
    {\'mae\': array([0.5, 1. ])}
'''
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION	,  _KWARGS_DESCRIPTION							)
class _lowerCamelCase (		datasets.Metric							):



		'''simple docstring'''







		def      __lowerCAmelCase	(      self	:		Union[str, Any]    )						->				int:
			return datasets.MetricInfo(
			    description=_DESCRIPTION						,  citation=_CITATION						,  inputs_description=_KWARGS_DESCRIPTION						,  features=datasets.Features(self._get_feature_types()    )						,  reference_urls=[
			        'https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_absolute_error.html'
			    ]						,  )







		def      __lowerCAmelCase	(      self	:		Tuple    )						->				List[str]:
			if self.config_name == "multilist":
				return {
				    "predictions": datasets.Sequence(datasets.Value('float'    )    ),
				    "references": datasets.Sequence(datasets.Value('float'    )    ),
				}
			else:
				return {
				    "predictions": datasets.Value('float'    ),
				    "references": datasets.Value('float'    ),
				}







		def      __lowerCAmelCase	(      self	:		Dict						,  _A	:		Optional[int]						,  _A	:		Optional[Any]						,  _A	:		Optional[Any]=None						,  _A	:		Tuple="uniform_average"    )						->				Any:
			__magic_name__ :					Optional[Any]  			=  mean_absolute_error(_A						,  _A						,  sample_weight=_A						,  multioutput=_A    )

			return {"mae": mae_score}