Upload math_verify/metric.py with huggingface_hub
Browse files- math_verify/metric.py +102 -0
math_verify/metric.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## Parser definition
|
| 2 |
+
import logging
|
| 3 |
+
from math_verify.errors import TimeoutException
|
| 4 |
+
from typing import Callable, Optional, Sequence
|
| 5 |
+
from math_verify.grader import verify
|
| 6 |
+
from math_verify.parser import ExprExtractionConfig, ExtractionTarget, parse
|
| 7 |
+
from math_verify.utils import timeout
|
| 8 |
+
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def math_metric(
|
| 13 |
+
gold_extraction_target: Sequence[ExtractionTarget] = (ExprExtractionConfig(),),
|
| 14 |
+
pred_extraction_target: Sequence[ExtractionTarget] = (ExprExtractionConfig(),),
|
| 15 |
+
aggregation_function: Callable[[list[float]], float] = max,
|
| 16 |
+
precision: int = 6,
|
| 17 |
+
) -> Callable[
|
| 18 |
+
[list[str], list[str]], tuple[float, Optional[tuple[list[str], list[str]]]]
|
| 19 |
+
]:
|
| 20 |
+
"""Creates a language-aware extractive match metric that extracts answers from the model's output.
|
| 21 |
+
|
| 22 |
+
Known issues:
|
| 23 |
+
- If the task is to simplify an expression, the metric might overestimate the accuracy. This is because if the model doesn't output any anchor for the extraction (e.g final answer is..),
|
| 24 |
+
it's possible that the the extracted prediction will be the expression to simplify. Because we do simplifications ourselves, it can thus happen that sympy will correctly simplify the expression,
|
| 25 |
+
thus it will match gold, despite model not doing anything. PRs to fix this are welcome.
|
| 26 |
+
|
| 27 |
+
Args:
|
| 28 |
+
language: Language
|
| 29 |
+
The language of the samples.
|
| 30 |
+
gold_extraction_target: Sequence[ExtractionTarget]
|
| 31 |
+
Extraction targets to use for gold answers. Defaults to extracting simple math expressions.
|
| 32 |
+
pred_extraction_target: Sequence[ExtractionTarget]
|
| 33 |
+
Extraction targets to use for predictions. Defaults to extracting simple math expressions.
|
| 34 |
+
aggregation_function: Callable[[list[float]], float]
|
| 35 |
+
Function to aggregate scores when multiple golds/predictions are present. Defaults to max.
|
| 36 |
+
fallback_mode: Literal["no_fallback", "first_match"]
|
| 37 |
+
How to perform extraction. Defaults to "first_match".
|
| 38 |
+
- "no_fallback": Only use first successfully parsed matches
|
| 39 |
+
- "first_match": Use the first successfully parsed match + first match irregardless the parsing success
|
| 40 |
+
precision: int
|
| 41 |
+
Number of decimal places to use when comparing numerical values. Defaults to 6.
|
| 42 |
+
|
| 43 |
+
Returns:
|
| 44 |
+
A sample level metric that extracts and compares mathematical expressions.
|
| 45 |
+
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
@timeout(2)
|
| 49 |
+
def get_str_preds_with_timeout(
|
| 50 |
+
extracted_predictions: list[list[str]], extracted_golds: list[list[str]]
|
| 51 |
+
) -> tuple[list[str], list[str]]:
|
| 52 |
+
golds = [str(gold) for golds in extracted_golds for gold in golds]
|
| 53 |
+
predictions = [str(pred) for preds in extracted_predictions for pred in preds]
|
| 54 |
+
return (golds, predictions)
|
| 55 |
+
|
| 56 |
+
def sample_level_fn(
|
| 57 |
+
golds: list[str], predictions: list[str]
|
| 58 |
+
) -> tuple[float, Optional[tuple[list[str], list[str]]]]:
|
| 59 |
+
extracted_predictions = [
|
| 60 |
+
parse(pred, pred_extraction_target) for pred in predictions
|
| 61 |
+
]
|
| 62 |
+
extracted_golds = [parse(gold, gold_extraction_target) for gold in golds]
|
| 63 |
+
|
| 64 |
+
# Assert on empty gold and warn on empty pred
|
| 65 |
+
if any(len(g) == 0 for g in extracted_golds):
|
| 66 |
+
raise ValueError(
|
| 67 |
+
f"No gold targets found for at least one gold. Gold: {golds}, Pred: {predictions}"
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
if all(len(p) == 0 for p in extracted_predictions):
|
| 71 |
+
logger.warning(
|
| 72 |
+
f"We did not manage to extract a prediction in the correct format. Gold: {golds}, Pred: {predictions}"
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# We have to use timeout because the sypmy to str conversion can be very slow
|
| 76 |
+
str_preds = None
|
| 77 |
+
try:
|
| 78 |
+
str_preds = get_str_preds_with_timeout(
|
| 79 |
+
extracted_predictions, extracted_golds
|
| 80 |
+
)
|
| 81 |
+
except TimeoutException:
|
| 82 |
+
logger.warning(
|
| 83 |
+
"Timeout when adding extracted predictions and golds to specific"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
return (
|
| 87 |
+
aggregation_function(
|
| 88 |
+
[
|
| 89 |
+
(
|
| 90 |
+
1.0
|
| 91 |
+
if any(
|
| 92 |
+
verify(gold, pred, precision) for gold in extracted_golds
|
| 93 |
+
)
|
| 94 |
+
else 0.0
|
| 95 |
+
)
|
| 96 |
+
for pred in extracted_predictions
|
| 97 |
+
]
|
| 98 |
+
),
|
| 99 |
+
str_preds,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
return sample_level_fn
|