




"""simple docstring"""




import unittest

from transformers import MPNetConfig, 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 (
       MPNetForMaskedLM,
       MPNetForMultipleChoice,
       MPNetForQuestionAnswering,
       MPNetForSequenceClassification,
       MPNetForTokenClassification,
       MPNetModel,
   )




class       a__	:







     def __init__(     self  ,		A  ,		A=13  ,		A=7  ,		A=True  ,		A=True  ,		A=False  ,		A=True  ,		A=99  ,		A=64  ,		A=5  ,		A=4  ,		A=64  ,		A="gelu"  ,		A=0.1  ,		A=0.1  ,		A=512  ,		A=16  ,		A=2  ,		A=0.0_2  ,		A=3  ,		A=4  ,		A=None  ,		)  ->   Optional[Any]:



          '''simple docstring'''



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







     def       lowerCAmelCase_    (     self			)  ->   str:



          '''simple docstring'''



          return MPNetConfig.from_pretrained("microsoft/mpnet-base"			)







     def       lowerCAmelCase_    (     self			)  ->   List[Any]:



          '''simple docstring'''



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

          a     =				None
          if self.use_input_mask:
               a     =				random_attention_mask([self.batch_size, self.seq_length]			)

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

          a     =				self.get_config()
          return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels







     def       lowerCAmelCase_    (     self			)  ->   List[Any]:



          '''simple docstring'''



          return 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  ,		)







     def       lowerCAmelCase_    (     self  ,		A  ,		A  ,		A  ,		A  ,		A  ,		A			)  ->   str:



          '''simple docstring'''



          a     =				MPNetModel(config=a_			)
          model.to(a_			)
          model.eval()
          a     =				model(a_  ,		a_			)
          a     =				model(a_			)
          self.parent.assertEqual(result.last_hidden_state.shape  ,		(self.batch_size, self.seq_length, self.hidden_size)			)
          self.parent.assertEqual(result.pooler_output.shape  ,		(self.batch_size, self.hidden_size)			)







     def       lowerCAmelCase_    (     self  ,		A  ,		A  ,		A  ,		A  ,		A  ,		A			)  ->   List[str]:



          '''simple docstring'''



          a     =				MPNetForQuestionAnswering(config=a_			)
          model.to(a_			)
          model.eval()
          a     =				model(
              a_  ,		attention_mask=a_  ,		start_positions=a_  ,		end_positions=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)			)







     def       lowerCAmelCase_    (     self  ,		A  ,		A  ,		A  ,		A  ,		A  ,		A			)  ->   Tuple:



          '''simple docstring'''



          a     =				self.num_labels
          a     =				MPNetForSequenceClassification(a_			)
          model.to(a_			)
          model.eval()
          a     =				model(a_  ,		attention_mask=a_  ,		labels=a_			)
          self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.num_labels)			)







     def       lowerCAmelCase_    (     self  ,		A  ,		A  ,		A  ,		A  ,		A  ,		A			)  ->   Optional[Any]:



          '''simple docstring'''



          a     =				self.num_choices
          a     =				MPNetForMultipleChoice(config=a_			)
          model.to(a_			)
          model.eval()
          a     =				input_ids.unsqueeze(1			).expand(-1  ,		self.num_choices  ,		-1			).contiguous()
          a     =				input_mask.unsqueeze(1			).expand(-1  ,		self.num_choices  ,		-1			).contiguous()
          a     =				model(
              a_  ,		attention_mask=a_  ,		labels=a_  ,		)
          self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.num_choices)			)







     def       lowerCAmelCase_    (     self  ,		A  ,		A  ,		A  ,		A  ,		A  ,		A			)  ->   List[Any]:



          '''simple docstring'''



          a     =				self.num_labels
          a     =				MPNetForTokenClassification(config=a_			)
          model.to(a_			)
          model.eval()
          a     =				model(a_  ,		attention_mask=a_  ,		labels=a_			)
          self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.seq_length, self.num_labels)			)







     def       lowerCAmelCase_    (     self			)  ->   Dict:



          '''simple docstring'''



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







@require_torch
class       a__	( SCREAMING_SNAKE_CASE__       , SCREAMING_SNAKE_CASE__       , unittest.TestCase				):
     a		:      int		 =     (
         (
             MPNetForMaskedLM,
             MPNetForMultipleChoice,
             MPNetForQuestionAnswering,
             MPNetForSequenceClassification,
             MPNetForTokenClassification,
             MPNetModel,
         )
         if is_torch_available()
         else ()
     )
     a		:      str		 =     (
         {
             'feature-extraction': MPNetModel,
             'fill-mask': MPNetForMaskedLM,
             'question-answering': MPNetForQuestionAnswering,
             'text-classification': MPNetForSequenceClassification,
             'token-classification': MPNetForTokenClassification,
             'zero-shot': MPNetForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     a		:      Any		 =     False
     a		:      Any		 =     True







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



          '''simple docstring'''



          a     =				MPNetModelTester(self			)
          a     =				ConfigTester(self  ,		config_class=a_  ,		hidden_size=37			)







     def       lowerCAmelCase_    (     self			)  ->   List[Any]:



          '''simple docstring'''



          self.config_tester.run_common_tests()







     def       lowerCAmelCase_    (     self			)  ->   str:



          '''simple docstring'''



          a     =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mpnet_model(*a_			)







     def       lowerCAmelCase_    (     self			)  ->   Any:



          '''simple docstring'''



          a     =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mpnet_for_sequence_classification(*a_			)







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



          '''simple docstring'''



          a     =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mpnet_for_multiple_choice(*a_			)







     def       lowerCAmelCase_    (     self			)  ->   Any:



          '''simple docstring'''



          a     =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mpnet_for_token_classification(*a_			)







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



          '''simple docstring'''



          a     =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mpnet_for_question_answering(*a_			)







@require_torch
class       a__	( unittest.TestCase				):







     @slow
     def       lowerCAmelCase_    (     self			)  ->   Optional[int]:



          '''simple docstring'''



          a     =				MPNetModel.from_pretrained("microsoft/mpnet-base"			)
          a     =				torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]			)
          a     =				model(a_			)[0]
          a     =				torch.Size((1, 11, 768)			)
          self.assertEqual(output.shape  ,		a_			)
          a     =				torch.tensor(
              [[[-0.0_5_5_0, 0.1_9_4_3, -0.0_7_4_0], [-0.0_5_6_2, 0.2_2_1_1, -0.0_5_7_9], [-0.0_4_3_7, 0.3_3_3_7, -0.0_6_4_1]]]			)
          # compare the actual values for a slice.
          self.assertTrue(torch.allclose(output[:, :3, :3]  ,		a_  ,		atol=1e-4			)			)







import numpy as np
from nltk.translate import meteor_score

import datasets
from datasets.config import importlib_metadata, version


lowercase__				:      Optional[int]	   = version.parse(importlib_metadata.version("nltk"))
if NLTK_VERSION >= version.Version("3.6.4"):
   from nltk import word_tokenize


lowercase__				:      int	   = "\\n@inproceedings{banarjee2005,\n  title     = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},\n  author    = {Banerjee, Satanjeev  and Lavie, Alon},\n  booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},\n  month     = jun,\n  year      = {2005},\n  address   = {Ann Arbor, Michigan},\n  publisher = {Association for Computational Linguistics},\n  url       = {https://www.aclweb.org/anthology/W05-0909},\n  pages     = {65--72},\n}\n"

lowercase__				:      str	   = "\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n"

lowercase__				:      Tuple	   = "\nComputes METEOR score of translated segments against one or more references.\nArgs:\n    predictions: list of predictions to score. Each prediction\n        should be a string with tokens separated by spaces.\n    references: list of reference for each prediction. Each\n        reference should be a string with tokens separated by spaces.\n    alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n    beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n    gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n    'meteor': meteor score.\nExamples:\n\n    >>> meteor = datasets.load_metric('meteor')\n    >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]\n    >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]\n    >>> results = meteor.compute(predictions=predictions, references=references)\n    >>> print(round(results[\"meteor\"], 4))\n    0.6944\n"




@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION       , _KWARGS_DESCRIPTION				)
class       a__	( datasets.Metric				):







     def       lowerCAmelCase_    (     self			)  ->   List[Any]:



          '''simple docstring'''



          return datasets.MetricInfo(
              description=_DESCRIPTION  ,		citation=_CITATION  ,		inputs_description=_KWARGS_DESCRIPTION  ,		features=datasets.Features(
                  {
                      "predictions": datasets.Value("string"  ,		id="sequence"			),
                      "references": datasets.Value("string"  ,		id="sequence"			),
                  }			)  ,		codebase_urls=["https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"]  ,		reference_urls=[
                  "https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score",
                  "https://en.wikipedia.org/wiki/METEOR",
              ]  ,		)







     def       lowerCAmelCase_    (     self  ,		A			)  ->   List[str]:



          '''simple docstring'''



          import nltk

          nltk.download("wordnet"			)
          if NLTK_VERSION >= version.Version("3.6.5"			):
               nltk.download("punkt"			)
          if NLTK_VERSION >= version.Version("3.6.6"			):
               nltk.download("omw-1.4"			)







     def       lowerCAmelCase_    (     self  ,		A  ,		A  ,		A=0.9  ,		A=3  ,		A=0.5			)  ->   Tuple:



          '''simple docstring'''



          if NLTK_VERSION >= version.Version("3.6.5"			):
               a     =				[
                   meteor_score.single_meteor_score(
                       word_tokenize(A			)  ,		word_tokenize(A			)  ,		alpha=A  ,		beta=A  ,		gamma=A			)
                   for ref, pred in zip(A  ,		A			)
               ]
          else:
               a     =				[
                   meteor_score.single_meteor_score(A  ,		A  ,		alpha=A  ,		beta=A  ,		gamma=A			)
                   for ref, pred in zip(A  ,		A			)
               ]

          return {"meteor": np.mean(A			)}

