





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
     import torch

     from transformers.generation import DisjunctiveConstraint



@require_torch
class 				_UpperCAmelCase    (    unittest.TestCase    ):

  """simple docstring"""


  def 				lowercase		(				self :		Dict     )							->						List[str]:
     # For consistency across different places the DisjunctiveConstraint is called,
     # dc.token_ids is a list of integers. It is also initialized only by integers.

     __lowerCAmelCase								=	[[1, 2, 4], [1, 2, 3, 4]]
     __lowerCAmelCase								=	DisjunctiveConstraint(lowerCAmelCase_     )
     self.assertTrue(isinstance(dc.token_ids					,  lowerCAmelCase_     )     )

     with self.assertRaises(lowerCAmelCase_     ):
        DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]]     )     )

     with self.assertRaises(lowerCAmelCase_     ):
        DisjunctiveConstraint([torch.LongTensor([1, 2, 4]     ), torch.LongTensor([1, 2, 3, 4, 5]     )]     )






  def 				lowercase		(				self :		List[str]     )							->						Optional[int]:
     # We can't have constraints that are complete subsets of another. This leads to a preverse
     # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint?
     # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially
     # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm
     # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it).
     __lowerCAmelCase								=	[[1, 2], [1, 2, 3, 4]]

     with self.assertRaises(lowerCAmelCase_     ):
        DisjunctiveConstraint(lowerCAmelCase_     )  # fails here






  def 				lowercase		(				self :		List[str]     )							->						Dict:
     __lowerCAmelCase								=	[[1, 2, 3], [1, 2, 4]]

     __lowerCAmelCase								=	DisjunctiveConstraint(lowerCAmelCase_     )

     __lowerCAmelCase	,	__lowerCAmelCase	,	__lowerCAmelCase								=	dc.update(1     )
     __lowerCAmelCase								=	stepped is True and completed is False and reset is False
     self.assertTrue(lowerCAmelCase_     )
     self.assertTrue(not dc.completed     )
     self.assertTrue(dc.current_seq == [1]     )

     __lowerCAmelCase	,	__lowerCAmelCase	,	__lowerCAmelCase								=	dc.update(2     )
     __lowerCAmelCase								=	stepped is True and completed is False and reset is False
     self.assertTrue(lowerCAmelCase_     )
     self.assertTrue(not dc.completed     )
     self.assertTrue(dc.current_seq == [1, 2]     )

     __lowerCAmelCase	,	__lowerCAmelCase	,	__lowerCAmelCase								=	dc.update(3     )
     __lowerCAmelCase								=	stepped is True and completed is True and reset is False
     self.assertTrue(lowerCAmelCase_     )
     self.assertTrue(dc.completed     )  # Completed!
     self.assertTrue(dc.current_seq == [1, 2, 3]     )






  def 				lowercase		(				self :		Tuple     )							->						Dict:
     __lowerCAmelCase								=	[[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

     __lowerCAmelCase								=	DisjunctiveConstraint(lowerCAmelCase_     )

     __lowerCAmelCase	,	__lowerCAmelCase	,	__lowerCAmelCase								=	dc.update(1     )
     self.assertTrue(not dc.completed     )
     self.assertTrue(dc.current_seq == [1]     )

     __lowerCAmelCase	,	__lowerCAmelCase	,	__lowerCAmelCase								=	dc.update(2     )
     self.assertTrue(not dc.completed     )
     self.assertTrue(dc.current_seq == [1, 2]     )

     __lowerCAmelCase	,	__lowerCAmelCase	,	__lowerCAmelCase								=	dc.update(4     )
     self.assertTrue(not dc.completed     )
     self.assertTrue(dc.current_seq == [1, 2, 4]     )

     __lowerCAmelCase	,	__lowerCAmelCase	,	__lowerCAmelCase								=	dc.update(5     )
     self.assertTrue(dc.completed     )  # Completed!
     self.assertTrue(dc.current_seq == [1, 2, 4, 5]     )

     dc.reset()

     __lowerCAmelCase	,	__lowerCAmelCase	,	__lowerCAmelCase								=	dc.update(1     )
     self.assertTrue(not dc.completed     )
     self.assertTrue(dc.remaining() == 3     )
     self.assertTrue(dc.current_seq == [1]     )

     __lowerCAmelCase	,	__lowerCAmelCase	,	__lowerCAmelCase								=	dc.update(2     )
     self.assertTrue(not dc.completed     )
     self.assertTrue(dc.remaining() == 2     )
     self.assertTrue(dc.current_seq == [1, 2]     )

     __lowerCAmelCase	,	__lowerCAmelCase	,	__lowerCAmelCase								=	dc.update(5     )
     self.assertTrue(dc.completed     )  # Completed!
     self.assertTrue(dc.remaining() == 0     )
     self.assertTrue(dc.current_seq == [1, 2, 5]     )








import math_equivalence  # From: git+https://github.com/hendrycks/math.git

import datasets


_snake_case  :    Tuple						    =  '\\n@article{hendrycksmath2021,\n  title={Measuring Mathematical Problem Solving With the MATH Dataset},\n  author={Dan Hendrycks\n    and Collin Burns\n    and Saurav Kadavath\n    and Akul Arora\n    and Steven Basart\n    and Eric Tang\n    and Dawn Song\n    and Jacob Steinhardt},\n  journal={arXiv preprint arXiv:2103.03874},\n  year={2021}\n}\n'


_snake_case  :    str						    =  '\\nThis metric is used to assess performance on the Mathematics Aptitude Test of Heuristics (MATH) dataset.\nIt first canonicalizes the inputs (e.g., converting "1/2" to "\\frac{1}{2}") and then computes accuracy.\n'


_snake_case  :    List[str]						    =  R'\nCalculates accuracy after canonicalizing inputs.\n\nArgs:\n    predictions: list of predictions to score. Each prediction\n        is a string that contains natural language and LaTex.\n    references: list of reference for each prediction. Each\n        reference is a string that contains natural language\n        and LaTex.\nReturns:\n    accuracy: accuracy after canonicalizing inputs\n        (e.g., converting "1/2" to "\\frac{1}{2}")\n\nExamples:\n    >>> metric = datasets.load_metric("competition_math")\n    >>> results = metric.compute(references=["\\frac{1}{2}"], predictions=["1/2"])\n    >>> print(results)\n    {\'accuracy\': 1.0}\n'



@datasets.utils.file_utils.add_end_docstrings(_DESCRIPTION		,    _KWARGS_DESCRIPTION    )
class 				_UpperCAmelCase    (    datasets.Metric    ):

  """simple docstring"""


  def 				lowercase		(				self :		str     )							->						int:
     return datasets.MetricInfo(
         description=_DESCRIPTION					,  citation=_CITATION					,  inputs_description=_KWARGS_DESCRIPTION					,  features=datasets.Features(
             {
                 'predictions': datasets.Value('string'     ),
                 'references': datasets.Value('string'     ),
             }     )					,  homepage='https://github.com/hendrycks/math'					,  codebase_urls=['https://github.com/hendrycks/math']					,  )






  def 				lowercase		(				self :		str					,  lowerCAmelCase_ :		Any					,  lowerCAmelCase_ :		Optional[int]     )							->						List[Any]:
     __lowerCAmelCase								=	0.0
     for i, j in zip(lowerCAmelCase_					,  lowerCAmelCase_     ):
        n_correct += 1.0 if math_equivalence.is_equiv(lowerCAmelCase_					,  lowerCAmelCase_     ) else 0.0
     __lowerCAmelCase								=	n_correct / len(lowerCAmelCase_     )
     return {
         "accuracy": accuracy,
     }

