
from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
			import numpy
			import tensorflow as tf

			from transformers import (
			    TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
			    TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
			    TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
			    BertConfig,
			    DPRConfig,
			    TFDPRContextEncoder,
			    TFDPRQuestionEncoder,
			    TFDPRReader,
			)




class   SCREAMING_SNAKE_CASE__	:





	'''simple docstring'''



	def __init__(    self      :       List[str]     ,    lowercase      :       Optional[Any]     ,    lowercase      :       int=13     ,    lowercase      :       Any=7     ,    lowercase      :       int=True     ,    lowercase      :       Optional[Any]=True     ,    lowercase      :       Optional[int]=True     ,    lowercase      :       Tuple=True     ,    lowercase      :       int=99     ,    lowercase      :       Optional[int]=32     ,    lowercase      :       str=2     ,    lowercase      :       Optional[Any]=4     ,    lowercase      :       int=37     ,    lowercase      :       List[str]="gelu"     ,    lowercase      :       str=0.1     ,    lowercase      :       str=0.1     ,    lowercase      :       Dict=512     ,    lowercase      :       Union[str, Any]=16     ,    lowercase      :       Dict=2     ,    lowercase      :       Dict=0.02     ,    lowercase      :       Optional[Any]=3     ,    lowercase      :       Tuple=4     ,    lowercase      :       Optional[int]=None     ,    lowercase      :       int=0     ,    ):


				'''simple docstring'''


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



	def 					A				(    self      :       int      ):


				'''simple docstring'''


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

				_snake_case            =     None
				if self.use_input_mask:
							# follow test_modeling_tf_ctrl.py
							_snake_case            =     random_attention_mask([self.batch_size, self.seq_length]      )

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

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

				_snake_case            =     BertConfig(
				    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     ,    is_decoder=a_     ,    initializer_range=self.initializer_range     ,    )
				_snake_case            =     DPRConfig(projection_dim=self.projection_dim     ,    **config.to_dict()      )

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



	def 					A				(    self      :       Dict     ,    lowercase      :       Dict     ,    lowercase      :       List[Any]     ,    lowercase      :       int     ,    lowercase      :       List[str]     ,    lowercase      :       List[Any]     ,    lowercase      :       Tuple     ,    lowercase      :       Union[str, Any]      ):


				'''simple docstring'''


				_snake_case            =     TFDPRContextEncoder(config=a_      )
				_snake_case            =     model(a_     ,    attention_mask=a_     ,    token_type_ids=a_      )
				_snake_case            =     model(a_     ,    token_type_ids=a_      )
				_snake_case            =     model(a_      )
				self.parent.assertEqual(result.pooler_output.shape     ,    (self.batch_size, self.projection_dim or self.hidden_size)      )



	def 					A				(    self      :       Dict     ,    lowercase      :       Union[str, Any]     ,    lowercase      :       Optional[int]     ,    lowercase      :       Any     ,    lowercase      :       List[Any]     ,    lowercase      :       Tuple     ,    lowercase      :       Any     ,    lowercase      :       Dict      ):


				'''simple docstring'''


				_snake_case            =     TFDPRQuestionEncoder(config=a_      )
				_snake_case            =     model(a_     ,    attention_mask=a_     ,    token_type_ids=a_      )
				_snake_case            =     model(a_     ,    token_type_ids=a_      )
				_snake_case            =     model(a_      )
				self.parent.assertEqual(result.pooler_output.shape     ,    (self.batch_size, self.projection_dim or self.hidden_size)      )



	def 					A				(    self      :       List[Any]     ,    lowercase      :       Union[str, Any]     ,    lowercase      :       Dict     ,    lowercase      :       Optional[int]     ,    lowercase      :       str     ,    lowercase      :       List[Any]     ,    lowercase      :       List[str]     ,    lowercase      :       List[Any]      ):


				'''simple docstring'''


				_snake_case            =     TFDPRReader(config=a_      )
				_snake_case            =     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 					A				(    self      :       Dict      ):


				'''simple docstring'''


				_snake_case            =     self.prepare_config_and_inputs()
				(
				    _snake_case	
				)            =     config_and_inputs
				_snake_case            =     {"input_ids": input_ids}
				return config, inputs_dict




@require_tf
class   SCREAMING_SNAKE_CASE__	(      _UpperCamelCase				,_UpperCamelCase				,unittest.TestCase   ):





	'''simple docstring'''



	_UpperCAmelCase			:						Optional[Any]         =   (
	    (
	        TFDPRContextEncoder,
	        TFDPRQuestionEncoder,
	        TFDPRReader,
	    )
	    if is_tf_available()
	    else ()
	)
	_UpperCAmelCase			:						Dict         =   {"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

	_UpperCAmelCase			:						Tuple         =   False
	_UpperCAmelCase			:						Optional[int]         =   False
	_UpperCAmelCase			:						List[str]         =   False
	_UpperCAmelCase			:						Union[str, Any]         =   False
	_UpperCAmelCase			:						List[str]         =   False



	def 					A				(    self      :       Tuple      ):


				'''simple docstring'''


				_snake_case            =     TFDPRModelTester(self      )
				_snake_case            =     ConfigTester(self     ,    config_class=a_     ,    hidden_size=37      )



	def 					A				(    self      :       List[str]      ):


				'''simple docstring'''


				self.config_tester.run_common_tests()



	def 					A				(    self      :       Tuple      ):


				'''simple docstring'''


				_snake_case            =     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_context_encoder(*a_      )



	def 					A				(    self      :       Dict      ):


				'''simple docstring'''


				_snake_case            =     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_question_encoder(*a_      )



	def 					A				(    self      :       Union[str, Any]      ):


				'''simple docstring'''


				_snake_case            =     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_reader(*a_      )





	@slow
	def 					A				(    self      :       int      ):


				'''simple docstring'''


				for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							_snake_case            =     TFDPRContextEncoder.from_pretrained(a_      )
							self.assertIsNotNone(a_      )

				for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							_snake_case            =     TFDPRContextEncoder.from_pretrained(a_      )
							self.assertIsNotNone(a_      )

				for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							_snake_case            =     TFDPRQuestionEncoder.from_pretrained(a_      )
							self.assertIsNotNone(a_      )

				for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							_snake_case            =     TFDPRReader.from_pretrained(a_      )
							self.assertIsNotNone(a_      )




@require_tf
class   SCREAMING_SNAKE_CASE__	(      unittest.TestCase   ):





	'''simple docstring'''



	@slow
	def 					A				(    self      :       Dict      ):


				'''simple docstring'''


				_snake_case            =     TFDPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'      )

				_snake_case            =     tf.constant(
				    [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]]      )  # [CLS] hello, is my dog cute? [SEP]
				_snake_case            =     model(a_      )[0]  # embedding shape = (1, 768)
				# compare the actual values for a slice.
				_snake_case            =     tf.constant(
				    [
				        [
				            0.03236253,
				            0.12753335,
				            0.16818509,
				            0.00279786,
				            0.3896933,
				            0.24264945,
				            0.2178971,
				            -0.02335227,
				            -0.08481959,
				            -0.14324117,
				        ]
				    ]      )
				self.assertTrue(numpy.allclose(output[:, :10].numpy()     ,    expected_slice.numpy()     ,    atol=1E-4      )      )






"""simple docstring"""






from sklearn.metrics import matthews_corrcoef

import datasets


__UpperCAmelCase						=			'''
Compute the Matthews correlation coefficient (MCC)

The Matthews correlation coefficient is used in machine learning as a
measure of the quality of binary and multiclass classifications. It takes
into account true and false positives and negatives and is generally
regarded as a balanced measure which can be used even if the classes are of
very different sizes. The MCC is in essence a correlation coefficient value
between -1 and +1. A coefficient of +1 represents a perfect prediction, 0
an average random prediction and -1 an inverse prediction.  The statistic
is also known as the phi coefficient. [source: Wikipedia]
'''

__UpperCAmelCase						=			'''
Args:
    predictions (list of int): Predicted labels, as returned by a model.
    references (list of int): Ground truth labels.
    sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.
Returns:
    matthews_correlation (dict containing float): Matthews correlation.
Examples:
    Example 1, a basic example with only predictions and references as inputs:
        >>> matthews_metric = datasets.load_metric("matthews_correlation")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3])
        >>> print(round(results[\'matthews_correlation\'], 2))
        0.54

    Example 2, the same example as above, but also including sample weights:
        >>> matthews_metric = datasets.load_metric("matthews_correlation")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3],
        ...                                     sample_weight=[0.5, 3, 1, 1, 1, 2])
        >>> print(round(results[\'matthews_correlation\'], 2))
        0.1

    Example 3, the same example as above, but with sample weights that cause a negative correlation:
        >>> matthews_metric = datasets.load_metric("matthews_correlation")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3],
        ...                                     sample_weight=[0.5, 1, 0, 0, 0, 1])
        >>> print(round(results[\'matthews_correlation\'], 2))
        -0.25
'''

__UpperCAmelCase						=			'''\
@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}
}
'''



@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION						,  _KWARGS_DESCRIPTION  )
class 					__UpperCAmelCase							(						datasets.Metric  ):
				def       UpperCAmelCase      (		self    :			Optional[int]    )	->	str:



							'''simple docstring'''



							return datasets.MetricInfo(
							    description=_DESCRIPTION	, citation=_CITATION	, inputs_description=_KWARGS_DESCRIPTION	, features=datasets.Features(
							        {
							            "predictions": datasets.Value("int32"    ),
							            "references": datasets.Value("int32"    ),
							        }    )	, reference_urls=[
							        "https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html"
							    ]	, )






				def       UpperCAmelCase      (		self    :			Tuple	, a_    :			Optional[Any]	, a_    :			Optional[Any]	, a_    :			Any=None    )	->	Union[str, Any]:



							'''simple docstring'''



							return {
							    "matthews_correlation": float(matthews_corrcoef(a_	, a_	, sample_weight=a_    )    ),
							}