


'''simple docstring'''







from __future__ import annotations

import unittest

from transformers import MPNetConfig, 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 tensorflow as tf

   from transformers.models.mpnet.modeling_tf_mpnet import (
       TFMPNetForMaskedLM,
       TFMPNetForMultipleChoice,
       TFMPNetForQuestionAnswering,
       TFMPNetForSequenceClassification,
       TFMPNetForTokenClassification,
       TFMPNetModel,
   )







class A__	:






      def __init__(			self      :Optional[Any]					,						SCREAMING_SNAKE_CASE      :List[str]					,						SCREAMING_SNAKE_CASE      :str=1_3					,						SCREAMING_SNAKE_CASE      :str=7					,						SCREAMING_SNAKE_CASE      :List[str]=True					,						SCREAMING_SNAKE_CASE      :List[str]=True					,						SCREAMING_SNAKE_CASE      :str=False					,						SCREAMING_SNAKE_CASE      :Dict=True					,						SCREAMING_SNAKE_CASE      :Dict=9_9					,						SCREAMING_SNAKE_CASE      :Optional[Any]=6_4					,						SCREAMING_SNAKE_CASE      :int=2					,						SCREAMING_SNAKE_CASE      :List[str]=4					,						SCREAMING_SNAKE_CASE      :List[Any]=6_4					,						SCREAMING_SNAKE_CASE      :Optional[int]="gelu"					,						SCREAMING_SNAKE_CASE      :int=0.1					,						SCREAMING_SNAKE_CASE      :Dict=0.1					,						SCREAMING_SNAKE_CASE      :Tuple=5_1_2					,						SCREAMING_SNAKE_CASE      :Any=1_6					,						SCREAMING_SNAKE_CASE      :Optional[Any]=2					,						SCREAMING_SNAKE_CASE      :Union[str, Any]=0.02					,						SCREAMING_SNAKE_CASE      :List[Any]=3					,						SCREAMING_SNAKE_CASE      :Dict=4					,						SCREAMING_SNAKE_CASE      :str=None					,						)	-> Dict:




             '''simple docstring'''
             _a   :			Tuple					 =parent
             _a   :			Dict					 =batch_size
             _a   :			Optional[int]					 =seq_length
             _a   :			Optional[Any]					 =is_training
             _a   :			Tuple					 =use_input_mask
             _a   :			str					 =use_token_type_ids
             _a   :			Tuple					 =use_labels
             _a   :			Dict					 =vocab_size
             _a   :			Optional[int]					 =hidden_size
             _a   :			Optional[Any]					 =num_hidden_layers
             _a   :			Union[str, Any]					 =num_attention_heads
             _a   :			Union[str, Any]					 =intermediate_size
             _a   :			List[str]					 =hidden_act
             _a   :			int					 =hidden_dropout_prob
             _a   :			Tuple					 =attention_probs_dropout_prob
             _a   :			Tuple					 =max_position_embeddings
             _a   :			Dict					 =type_vocab_size
             _a   :			Any					 =type_sequence_label_size
             _a   :			List[Any]					 =initializer_range
             _a   :			Optional[Any]					 =num_labels
             _a   :			Dict					 =num_choices
             _a   :			List[str]					 =scope






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




             '''simple docstring'''
             _a   :			Union[str, Any]					 =ids_tensor([self.batch_size, self.seq_length]					,						self.vocab_size     )

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

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

             _a   :			Optional[Any]					 =MPNetConfig(
                 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					,						initializer_range=self.initializer_range					,						)
             return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels






      def __UpperCAmelCase	(			self      :Tuple					,						SCREAMING_SNAKE_CASE      :str					,						SCREAMING_SNAKE_CASE      :List[Any]					,						SCREAMING_SNAKE_CASE      :Optional[Any]					,						SCREAMING_SNAKE_CASE      :int					,						SCREAMING_SNAKE_CASE      :Union[str, Any]					,						SCREAMING_SNAKE_CASE      :int     )	-> Tuple:




             '''simple docstring'''
             _a   :			List[Any]					 =TFMPNetModel(config=SCREAMING_SNAKE_CASE     )
             _a   :			Optional[Any]					 ={"""input_ids""": input_ids, """attention_mask""": input_mask}
             _a   :			Union[str, Any]					 =model(SCREAMING_SNAKE_CASE     )
             _a   :			Any					 =[input_ids, input_mask]
             _a   :			int					 =model(SCREAMING_SNAKE_CASE     )
             self.parent.assertEqual(result.last_hidden_state.shape					,						(self.batch_size, self.seq_length, self.hidden_size)     )






      def __UpperCAmelCase	(			self      :Union[str, Any]					,						SCREAMING_SNAKE_CASE      :Dict					,						SCREAMING_SNAKE_CASE      :int					,						SCREAMING_SNAKE_CASE      :Any					,						SCREAMING_SNAKE_CASE      :Optional[int]					,						SCREAMING_SNAKE_CASE      :List[str]					,						SCREAMING_SNAKE_CASE      :int     )	-> Any:




             '''simple docstring'''
             _a   :			Tuple					 =TFMPNetForMaskedLM(config=SCREAMING_SNAKE_CASE     )
             _a   :			List[Any]					 ={"""input_ids""": input_ids, """attention_mask""": input_mask}
             _a   :			List[str]					 =model(SCREAMING_SNAKE_CASE     )
             self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.vocab_size)     )






      def __UpperCAmelCase	(			self      :Any					,						SCREAMING_SNAKE_CASE      :List[str]					,						SCREAMING_SNAKE_CASE      :str					,						SCREAMING_SNAKE_CASE      :List[Any]					,						SCREAMING_SNAKE_CASE      :str					,						SCREAMING_SNAKE_CASE      :Optional[int]					,						SCREAMING_SNAKE_CASE      :Dict     )	-> List[Any]:




             '''simple docstring'''
             _a   :			Optional[Any]					 =TFMPNetForQuestionAnswering(config=SCREAMING_SNAKE_CASE     )
             _a   :			int					 ={
                 """input_ids""": input_ids,
                 """attention_mask""": input_mask,
             }
             _a   :			List[Any]					 =model(SCREAMING_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)     )






      def __UpperCAmelCase	(			self      :int					,						SCREAMING_SNAKE_CASE      :int					,						SCREAMING_SNAKE_CASE      :Optional[Any]					,						SCREAMING_SNAKE_CASE      :Dict					,						SCREAMING_SNAKE_CASE      :Any					,						SCREAMING_SNAKE_CASE      :List[Any]					,						SCREAMING_SNAKE_CASE      :Optional[int]     )	-> Dict:




             '''simple docstring'''
             _a   :			Dict					 =self.num_labels
             _a   :			int					 =TFMPNetForSequenceClassification(SCREAMING_SNAKE_CASE     )
             _a   :			Dict					 ={"""input_ids""": input_ids, """attention_mask""": input_mask}
             _a   :			int					 =model(SCREAMING_SNAKE_CASE     )
             self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.num_labels)     )






      def __UpperCAmelCase	(			self      :Optional[int]					,						SCREAMING_SNAKE_CASE      :Tuple					,						SCREAMING_SNAKE_CASE      :List[str]					,						SCREAMING_SNAKE_CASE      :Tuple					,						SCREAMING_SNAKE_CASE      :List[Any]					,						SCREAMING_SNAKE_CASE      :Tuple					,						SCREAMING_SNAKE_CASE      :Union[str, Any]     )	-> List[str]:




             '''simple docstring'''
             _a   :			str					 =self.num_choices
             _a   :			Any					 =TFMPNetForMultipleChoice(SCREAMING_SNAKE_CASE     )
             _a   :			int					 =tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE					,						1     )					,						(1, self.num_choices, 1)     )
             _a   :			int					 =tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE					,						1     )					,						(1, self.num_choices, 1)     )
             _a   :			Dict					 ={
                 """input_ids""": multiple_choice_inputs_ids,
                 """attention_mask""": multiple_choice_input_mask,
             }
             _a   :			Dict					 =model(SCREAMING_SNAKE_CASE     )
             self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.num_choices)     )






      def __UpperCAmelCase	(			self      :Optional[Any]					,						SCREAMING_SNAKE_CASE      :Optional[Any]					,						SCREAMING_SNAKE_CASE      :List[str]					,						SCREAMING_SNAKE_CASE      :str					,						SCREAMING_SNAKE_CASE      :List[str]					,						SCREAMING_SNAKE_CASE      :Tuple					,						SCREAMING_SNAKE_CASE      :Dict     )	-> str:




             '''simple docstring'''
             _a   :			str					 =self.num_labels
             _a   :			Any					 =TFMPNetForTokenClassification(SCREAMING_SNAKE_CASE     )
             _a   :			Optional[Any]					 ={"""input_ids""": input_ids, """attention_mask""": input_mask}
             _a   :			Optional[int]					 =model(SCREAMING_SNAKE_CASE     )
             self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.num_labels)     )






      def __UpperCAmelCase	(			self      :Tuple     )	-> str:




             '''simple docstring'''
             _a   :			Union[str, Any]					 =self.prepare_config_and_inputs()
             ((_a) ,   (_a) ,   (_a) ,   (_a) ,   (_a) ,   (_a))   :			Optional[int]					 =config_and_inputs
             _a   :			List[str]					 ={"""input_ids""": input_ids, """attention_mask""": input_mask}
             return config, inputs_dict







@require_tf
class A__	(						UpperCAmelCase__			,					UpperCAmelCase__			,					unittest.TestCase				):
      __UpperCamelCase	:  Tuple     	=							(
          (
              TFMPNetForMaskedLM,
              TFMPNetForMultipleChoice,
              TFMPNetForQuestionAnswering,
              TFMPNetForSequenceClassification,
              TFMPNetForTokenClassification,
              TFMPNetModel,
          )
          if is_tf_available()
          else ()
      )
      __UpperCamelCase	:  Optional[int]     	=							(
          {
              "feature-extraction": TFMPNetModel,
              "fill-mask": TFMPNetForMaskedLM,
              "question-answering": TFMPNetForQuestionAnswering,
              "text-classification": TFMPNetForSequenceClassification,
              "token-classification": TFMPNetForTokenClassification,
              "zero-shot": TFMPNetForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      __UpperCamelCase	:  Optional[int]     	=							False
      __UpperCamelCase	:  Any     	=							False






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




             '''simple docstring'''
             _a   :			Optional[Any]					 =TFMPNetModelTester(self     )
             _a   :			Optional[int]					 =ConfigTester(self					,						config_class=SCREAMING_SNAKE_CASE					,						hidden_size=3_7     )






      def __UpperCAmelCase	(			self      :Optional[Any]     )	-> Optional[Any]:




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






      def __UpperCAmelCase	(			self      :List[Any]     )	-> Optional[int]:




             '''simple docstring'''
             _a   :			Tuple					 =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mpnet_model(*SCREAMING_SNAKE_CASE     )






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




             '''simple docstring'''
             _a   :			Any					 =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mpnet_for_masked_lm(*SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :Any     )	-> int:




             '''simple docstring'''
             _a   :			int					 =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mpnet_for_question_answering(*SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :Optional[int]     )	-> str:




             '''simple docstring'''
             _a   :			Optional[int]					 =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mpnet_for_sequence_classification(*SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :Optional[int]     )	-> int:




             '''simple docstring'''
             _a   :			Optional[int]					 =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mpnet_for_multiple_choice(*SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :str     )	-> str:




             '''simple docstring'''
             _a   :			str					 =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mpnet_for_token_classification(*SCREAMING_SNAKE_CASE     )






      @slow
      def __UpperCAmelCase	(			self      :Union[str, Any]     )	-> Optional[Any]:




             '''simple docstring'''
             for model_name in ["microsoft/mpnet-base"]:
                    _a   :			Tuple					 =TFMPNetModel.from_pretrained(SCREAMING_SNAKE_CASE     )
                    self.assertIsNotNone(SCREAMING_SNAKE_CASE     )







@require_tf
class A__	(						unittest.TestCase				):






      @slow
      def __UpperCAmelCase	(			self      :Dict     )	-> Union[str, Any]:




             '''simple docstring'''
             _a   :			Union[str, Any]					 =TFMPNetModel.from_pretrained("""microsoft/mpnet-base"""     )
             _a   :			Union[str, Any]					 =tf.constant([[0, 1, 2, 3, 4, 5]]     )
             _a   :			Optional[Any]					 =model(SCREAMING_SNAKE_CASE     )[0]

             _a   :			Optional[Any]					 =[1, 6, 7_6_8]
             self.assertEqual(output.shape					,						SCREAMING_SNAKE_CASE     )

             _a   :			List[str]					 =tf.constant(
                 [
                     [
                         [-0.1_067_172, 0.08_216_473, 0.0_024_543],
                         [-0.03_465_879, 0.8_354_118, -0.03_252_288],
                         [-0.06_569_476, -0.12_424_111, -0.0_494_436],
                     ]
                 ]     )
             tf.debugging.assert_near(output[:, :3, :3]					,						SCREAMING_SNAKE_CASE					,						atol=1e-4     )






'''simple docstring'''







from sklearn.metrics import mean_absolute_error

import datasets


A__:   Dict    =	'''\
@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}
}
'''

A__:   Union[str, Any]    =	'''\
Mean Absolute Error (MAE) is the mean of the magnitude of difference between the predicted and actual
values.
'''


A__:   str    =	'''
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 A__	(						datasets.Metric				):






      def __UpperCAmelCase	(			self      :List[Any]     )	-> Dict:




             '''simple docstring'''
             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 __UpperCAmelCase	(			self      :Dict     )	-> List[str]:




             '''simple docstring'''
             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 __UpperCAmelCase	(			self      :Any					,						SCREAMING_SNAKE_CASE      :Union[str, Any]					,						SCREAMING_SNAKE_CASE      :str					,						SCREAMING_SNAKE_CASE      :Union[str, Any]=None					,						SCREAMING_SNAKE_CASE      :Any="uniform_average"     )	-> List[Any]:




             '''simple docstring'''
             _a   :			Tuple					 =mean_absolute_error(SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE					,						sample_weight=SCREAMING_SNAKE_CASE					,						multioutput=SCREAMING_SNAKE_CASE     )

             return {"mae": mae_score}


