math_utils.grade_answer ignores units when both reference and prediction carry one, so the right number with the wrong unit (or wrong rate) is graded correct
The unit-stripping loop in _normalize (openrlhf/utils/math_utils.py:220-238, function defined at :205) runs on the reference and the prediction independently. It never checks that the two units match. Its output is compared at :377 in grade_answer_sympy (defined at :372). That function is reached from grade_answer (:411, return at :425) and from examples/python/math_reward_func.py:36-38.
for unit in [
"degree", "cm", "centimeter", "meter", "mile", "second", "minute",
"hour", "day", "week", "month", "year", "foot", "feet", "inch", "yard",
]:
expr = re.sub(f"{unit}(es)?(s)? *(\\^[0-9]+)?", "", expr) ground_truth_normalized = _normalize(ground_truth)
given_normalized = _normalize(given_answer)
...
if ground_truth_normalized == given_normalized:
return True
if not given_normalized:
return FalseThe loop exists so that 5 matches 5 hours. When both sides carry a unit, though, both units are erased and only what is left gets compared.
There are two paths:
- Bare-word units (
5 hours,10 miles):grade_answer_mathdreturns False for these, butgrade_answer_sympyreturns True through the string-equality check at:377. - MATH-style
\text{ unit}answers: the mathd path strips the unit too, through_remove_right_unitsin_strip_string. As a result,grade_answer('5\text{ minutes}', '5\text{ hours}')returns True through mathd.
This behaviour is inherited. The unit list and the re.sub loop are identical to openai/prm800k prm800k/grading/grader.py lines 126-144, and that file also runs its equality check before its empty-answer check (lines 257/260). OpenRLHF does not document the symmetric erasure as intended, and no test covers it: nothing under tests/ references grade_answer or math_utils.
Reachability: grade_answer is exported from openrlhf/utils/__init__.py. examples/python/math_reward_func.py is the REWARD_FUNC_PATH in three shipped example scripts, so the problem affects runs that use that reward function or call grade_answer directly.
Measured
At HEAD dc2a7ad, math_utils.py was loaded unmodified with the real pylatexenc 2.11. Substitutions used in the reproduction:
transformerswas not installed and its import was stubbed. These inputs never reach it.torchwas stubbed, only in the harness that runsexamples/python/math_reward_func.py.- The reward column comes from that real reward function, with
query = prompt + reply.
| gold | prediction | grade_answer / reward |
normalized gold / pred |
|---|---|---|---|
5 hours |
5 minutes |
True / 1.0 | 5 / 5 |
5 hours |
5 weeks |
True / 1.0 | 5 / 5 |
10 meters |
10 miles |
True / 1.0 | 10 / 10 |
10 meters |
10 centimeters |
True / 1.0 | 10 / 10 |
100 feet |
100 inches |
True | 100 / 100 |
5 days |
5 years |
True | 5 / 5 |
60 miles per hour |
60 feet per second |
True / 1.0 | 60per / 60per |
60 miles per hour |
60 miles per year |
True / 1.0 | 60per / 60per |
$5 per day |
5 per year |
True / 1.0 | 5per / 5per |
5\text{ hours} |
5\text{ minutes} |
True (via mathd) | |
12\text{ cm}^2 |
12\text{ inches}^2 |
True (via mathd) | |
12 \text{ cm}^2 |
12 \text{ m}^2 |
True (via mathd) | |
hours |
minutes |
True / 1.0 | / |
cm |
meter |
True | / |
hours |
`` (empty \boxed{}) |
True / 1.0 | / |
control: 5 hours |
6 hours |
False / 0.0 | |
control: 5 hours |
50 minutes |
False / 0.0 | |
control: 60 miles per hour |
70 miles per hour |
False / 0.0 | |
control: 5 hours |
`` / ... / I cannot answer that. / \boxed{} |
False / 0.0 | |
control: 5 hours |
5 hours, 5 |
True / 1.0 | |
control: 5 |
5 hours |
True | |
control: \frac{1}{2} |
0.5 |
True / 1.0 |
The last three non-control rows apply only when the gold label is nothing but a unit word. Such labels are unlikely in practice.
Consequence
When both the reference and the prediction include a unit, the reward is 1.0 for the correct magnitude with the wrong unit, and for the wrong rate. With that reward, the unit gets no training signal. This consequence is inferred from the grading behaviour above. It was not measured on a training run.
The shipped example datasets (AIME, geometry3k) mostly have unit-free numeric labels, so the practical impact falls mainly on user datasets whose labels carry units: rates and speeds, unit conversion, durations, and lengths or areas.
Suggested fix
Put the check in grade_answer, before both the mathd and the sympy branches, so that it also covers the \text{ unit} form:
- Extract the unit tokens from each raw string.
- Map them to canonical classes, such as {cm, centimeter}, {foot, feet} and singular/plural forms.
- Return False if both sides have units and the classes differ.
- Keep the current behaviour when only one side has a unit (
5vs5 hours,45vs45 degrees,90^\circvs90 degrees).
Comparing the raw unit words instead of canonical classes would break answers that are graded correct today. For example, 5 cm vs 5 centimeters and 3 feet vs 3 foot would both change from True to False.
Optionally, the if not given_normalized: return False guard at :379 could move above the equality check at :377. That change only affects gold labels made of a unit word alone.
A regression test through the public grade_answer API should pin these pairs:
- Must return False:
5 hours|5 minutes,5\text{ hours}|5\text{ minutes},10 meters|10 miles,60 miles per hour|60 feet per second. - Must stay True:
5 hours|5 hours,5 hours|5,5|5 hours,5 cm|5 centimeters,3 feet|3 foot,\frac{1}{2}|0.5.
The same function already has a precedent. In grade_answer_sympy, a tuple bracket/delimiter mismatch used to be computed and then overwritten. Commit 3c3be6234 ("Cleanup/simplicity tier1 2 (#1307)") fixed it with an early return False on the mismatch. The same defect had been proposed in the closed PR #1277. This issue has the same shape: a signal that tells the two answers apart is available but gets thrown away, and an early return False fixes it.
Happy to open the PR.
Source: OpenRLHF/OpenRLHF