File size: 6,315 Bytes
e99f306 4719635 e99f306 4719635 e99f306 4719635 e99f306 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | """Deterministic short-answer grader v2 — NO LLM judge.
Tiers (all offline/deterministic):
1. Exact (case-insensitive, whitespace-collapsed)
2. Numeric (abs/rel tolerance from ATOL/RTOL; gated to clean-number golds)
+ percent<->fraction bridge (e.g. gold 96.00 == pred 0.9621)
3. List (comma-separated): split, strip, order-insensitive; numeric-tolerant per element
(fixes 'a, b' vs 'a,b' spacing and reordering)
4. Math-Verify (symbolic/numeric equivalence)
"""
from __future__ import annotations
import os, re, sys
from dataclasses import dataclass
_NUMERIC_RE = re.compile(r"-?\d+(?:[.,]\d+)?(?:[eE][-+]?\d+)?")
@dataclass
class GradeResult:
reward: float
method: str
def _normalize(s: str) -> str:
return re.sub(r"\s+", " ", (s or "").strip().lower())
def _to_float(s: str):
if not s:
return None
m = _NUMERIC_RE.search(str(s).replace(",", ""))
if not m:
return None
try:
return float(m.group(0))
except ValueError:
return None
def _num_close(g, c, abs_tol, rel_tol) -> bool:
return abs(g - c) <= abs_tol or abs(g - c) / max(abs(g), 1e-9) <= rel_tol
def _is_clean_number(s: str) -> bool:
t = (s or "").strip().strip("%$").strip().replace(",", "")
return bool(_NUMERIC_RE.fullmatch(t))
def _elem_match(a, b, abs_tol, rel_tol) -> bool:
if _normalize(a) == _normalize(b):
return True
fa, fb = _to_float(a), _to_float(b)
if fa is not None and fb is not None:
return _num_close(fa, fb, abs_tol, rel_tol)
return False
def _list_match(gold: str, cand: str, abs_tol, rel_tol) -> bool:
gl = [x.strip() for x in gold.split(",") if x.strip()]
cl = [x.strip() for x in cand.split(",") if x.strip()]
if len(gl) < 2 or len(gl) != len(cl):
return False
for gs, cs in ((gl, cl), (sorted(gl, key=str.lower), sorted(cl, key=str.lower))):
if all(_elem_match(a, b, abs_tol, rel_tol) for a, b in zip(gs, cs)):
return True
return False
def _math_verify_match(gold: str, candidate: str) -> bool:
try:
from math_verify import parse, verify
return bool(verify(parse(gold), parse(candidate), timeout_seconds=5))
except Exception:
return False
def grade(gold: str, candidate: str, *, question: str = "", reward_mode: str = "",
judge: bool = True, judge_model=None, rel_tol: float = 1e-3, abs_tol: float = 1e-3) -> GradeResult:
if not gold or candidate is None:
return GradeResult(0.0, "miss")
# Tier 1: exact
if _normalize(gold) == _normalize(candidate):
return GradeResult(1.0, "exact")
# Tier 2: numeric (clean-number gold) + percent/fraction bridge
if reward_mode in ("numeric", "flexible") or _is_clean_number(gold):
g, c = _to_float(gold), _to_float(candidate)
if g is not None and c is not None:
if _num_close(g, c, abs_tol, rel_tol):
return GradeResult(1.0, "numeric")
# percent<->fraction: one side is a fraction (<1), the other a percent (>=1)
if (0 < abs(c) < 1 <= abs(g)) or (0 < abs(g) < 1 <= abs(c)):
if _num_close(g, c * 100, abs_tol, rel_tol) or _num_close(g, c / 100, abs_tol, rel_tol):
return GradeResult(1.0, "numeric_scaled")
# Tier 3: list (comma-separated), order-insensitive, per-element tolerant
if reward_mode in ("list", "list_csv") or ("," in gold and "," in candidate):
if _list_match(gold, candidate, abs_tol, rel_tol):
return GradeResult(1.0, "list")
# Tier 4: math-verify
if _math_verify_match(gold, candidate):
return GradeResult(1.0, "math_verify")
return GradeResult(0.0, "miss")
# TOOL EFFICIENCY IS NOT COMPUTED HERE, AND CANNOT BE.
#
# This grader used to emit a `tool_efficiency` key derived from a tool-call count it read from
# `/workdir/.n_tool_calls` or `$N_TOOL_CALLS`. Nothing writes that file and no task.toml sets that
# variable, so the count was always None, the key was always null, and Harbor's reward model
# (`dict[str, float | int]`) rejected the WHOLE dict -- taking `correctness` down with it. The only
# path that survived was the empty-submission branch below, so the suite graded failures correctly
# and crashed on every successful answer. That silently removed 86 of 250 tasks from scoring.
#
# The count is not the sandbox's to know: it lives in the capture proxy, which sees every model call.
# A reward belongs in the verifier only if the sandbox is what makes it computable. Correctness needs
# the data, the gold answer and the tolerances -- it belongs here. Tool efficiency needs a trace --
# it belongs to whoever holds the trace, and it is computed there as a shaping term, gated on
# correctness so that "made no tool calls" can never outscore solving the task.
def _tols():
def _f(name):
try:
return float(os.environ.get(name, "") or 1e-3)
except ValueError:
return 1e-3
return _f("ATOL"), _f("RTOL")
def main_cli() -> int:
gold = (os.environ.get("EXPECTED_ANSWER") or "").strip()
question = (os.environ.get("QUESTION") or "").strip()
candidate = sys.stdin.read().strip()
at, rt = _tols()
r = grade(gold, candidate, question=question, reward_mode=os.environ.get("REWARD_MODE", "") or "", abs_tol=at, rel_tol=rt)
print(f"{r.reward:.1f}")
print(f"[grader] gold={gold!r} pred={candidate[:80]!r} reward={r.reward} method={r.method}", file=sys.stderr)
return 0
def main_json() -> int:
import json
gold = (os.environ.get("EXPECTED_ANSWER") or "").strip()
question = (os.environ.get("QUESTION") or "").strip()
candidate = sys.stdin.read().strip()
if not candidate:
print(json.dumps({"correctness": 0.0, "submission": 0.0}))
return 0
at, rt = _tols()
r = grade(gold, candidate, question=question, reward_mode=os.environ.get("REWARD_MODE", "") or "", abs_tol=at, rel_tol=rt)
print(json.dumps({"correctness": float(r.reward), "submission": 1.0}))
print(f"[grader] gold={gold!r} pred={candidate[:80]!r} correctness={r.reward} method={r.method}", file=sys.stderr)
return 0
if __name__ == "__main__":
if "--json" in sys.argv[1:]:
raise SystemExit(main_json())
raise SystemExit(main_cli())
|