RegalInvader's picture
Release: Marco-Nano-Instruct 1-, 2- and 3-bit GGUF for phones
e5235fb verified
Raw History Blame Contribute Delete
1.29 kB
"""GSM8K answer extraction and matching, shared by the eval harness (RS-fast/RS-full) and the T1.4 FP8 check."""
from __future__ import annotations
import math
import re
MATH_SUFFIX = "\nPlease reason step by step, and put your final answer within \\boxed{}."
def extract_answer(text: str) -> str | None:
"""The last \\boxed{...} answer, else the last number in the text, normalised for comparison."""
boxed = re.findall(r"\\boxed\{((?:[^{}]|\{[^{}]*\})*)\}", text)
cand = boxed[-1] if boxed else None
if cand is None:
nums = re.findall(r"-?\d[\d,]*(?:\.\d+)?", text)
cand = nums[-1] if nums else None
if cand is None:
return None
cand = cand.replace("\\$", "").replace("\\!", "").replace(",", "").replace("$", "").replace("\\%", "").replace("%", "")
cand = cand.strip().rstrip(".")
cand = re.sub(r"\\text\{[^}]*\}", "", cand).strip()
m = re.fullmatch(r"\\[dt]?frac\{(-?\d+)\}\{(\d+)\}", cand)
if m:
return str(int(m.group(1)) / int(m.group(2)))
return cand
def same_number(pred: str | None, gold: str) -> bool:
if pred is None:
return False
try:
return math.isclose(float(pred), float(gold), rel_tol=0, abs_tol=1e-6)
except ValueError:
return pred.strip() == gold.strip()