cosine_scaled_reward returns 1.0 for every completion when the gold answer cannot be parsed (should be None, as accuracy_reward does since #566)
src/open_r1/rewards.py:238-241 (HEAD 1416fa0)
gold_parsed = parse(
sol,
extraction_mode="first_match",
extraction_config=[LatexExtractionConfig()],
)
if len(gold_parsed) == 0:
rewards.append(1.0) # Skip unparseable examples
print("Failed to parse gold solution: ", sol)
continueThe comment says the row is skipped, but the code appends the literal 1.0, so the row is scored as a perfect answer for whatever the completion is. accuracy_reward in the same file handles the same case with reward = None (rewards.py:76-79). That change was made in #566 for the reason quoted there: "avoid getting spurious reward curves where the base accuracy is high simply because the gold answers could not be parsed". #566 only touched accuracy_reward; get_cosine_scaled_reward still has the pre-#566 pattern (same for the gold-parse config, see below).
The gold is parsed with LatexExtractionConfig() only, whereas accuracy_reward now uses the default extraction (#566 dropped extraction_config=[LatexExtractionConfig()] so bare answers like 6 parse). So a bare-answer solution such as 42 or 1/2 also lands in this branch in cosine_scaled_reward while accuracy_reward scores it normally.
Measured
Environment: math-verify==0.5.2, latex2sympy2_extended==1.0.6 (the versions setup.py resolves to), Python 3.12. The open_r1 package was not imported; the source segments of get_cosine_scaled_reward and accuracy_reward were extracted from rewards.py at 1416fa0 with ast.get_source_segment and exec'd unmodified, with the real parse/verify/NormalizationConfig/LatexExtractionConfig. Sanity check: the four cases in tests/test_rewards.py::test_cosine_scaled_reward reproduce (0.9426, 0.5477, -0.9426, -0.5477). get_cosine_scaled_reward() with defaults.
| gold | completion | cosine_scaled_reward |
accuracy_reward |
|---|---|---|---|
\frac{63}{400} (control) |
\boxed{\frac{63}{400}} |
0.9994 | 1.0 |
\frac{63}{400} (control) |
\boxed{\frac{64}{400}} |
-0.9994 | 0.0 |
\frac{63}{400} (control) |
empty | -1.0000 | 0.0 |
\frac{63}{400} (control) |
I cannot help with that. |
-0.9993 | 0.0 |
unparseable_latex |
empty | 1.0000 | None |
unparseable_latex |
" \n" |
1.0000 | None |
unparseable_latex |
..... |
1.0000 | None |
unparseable_latex |
I cannot help with that. |
1.0000 | None |
unparseable_latex |
\boxed{9} |
1.0000 | None |
unparseable_latex |
5000 x x |
1.0000 | None |
42 |
empty | 1.0000 | 0.0 |
42 |
\boxed{7} |
1.0000 | 0.0 |
1/2 |
I cannot help with that. |
1.0000 | 0.0 |
12 apples |
..... |
1.0000 | 0.0 |
With get_cosine_scaled_reward(min_value_correct=0.2, max_value_correct=0.3) a correct answer scores 0.2999 and an empty reply on an unparseable gold scores 1.0, i.e. the literal is outside the configured correct band.
Gold strings that accuracy_reward parses but the cosine/length gold parse returns [] for: 42, -3, 1/2, 0.5, x=3, 12 apples, 3,4, 10%, 1e5. Of 18 hand-picked typical gold strings, 6 are unparseable under the accuracy_reward config and 15 under the cosine config (small hand-picked list, not a dataset statistic).
Batch of 8 prompts x 1 rollout where every rollout is wrong or empty, 4 golds latex (\frac{63}{400}, \frac{1}{2}, $\boxed{7}$, \frac{3}{4}) and 4 bare (42, 1/2, 12 apples, -3): the mean that GRPOTrainer logs as rewards/cosine_scaled_reward/mean is +0.0001 with the current code and -0.9998 when the unparseable rows return None.
Consequence
- The function gives the top score to any reply (including an empty one) for a row it cannot score, and
accuracy_rewardon the same row returnsNone, so the two disagree. trl==0.18.0(pinned insetup.py) turnsNoneinto NaN, sums withnansum, and logs the per-function mean over non-NaN rows only (grpo_trainer.py).Noneis the supported skip channel; the literal1.0counts every unscorable row as a perfect score in the logged mean and in therewardcurve.- On a dataset whose
solutioncolumn holds bare answers, the cosine term is disabled for every row (length shaping never applied) while the metric shows the maximum value. - Scope:
cosineis opt-in viareward_funcsand no shipped recipe enables it. Within a single prompt the constant is identical for all rollouts, so the group-relative advantage from this term is 0 either way (checked:[1.0]*4and[None]*4both give advantages[0, 0, 0, 0]); the damage is the wrong per-sample verdict, the inflated logged reward, and the silently inert length term. - There is no test for this branch:
tests/test_rewards.py::test_cosine_scaled_rewardonly uses parseable golds.
Suggested fix
Follow #566 (merge commit 4f5b21e), which did this for accuracy_reward:
if len(gold_parsed) == 0:
rewards.append(None) # Skip unparseable examples
print("Failed to parse gold solution: ", sol)
continueplus the -> list[Optional[float]] annotation and a test asserting get_cosine_scaled_reward()(completions, ["unparseable_latex"]) == [None]. Optionally (separate commit, same precedent) drop extraction_config=[LatexExtractionConfig()] on the gold parse so bare answers stay scorable, and use verify(gold_parsed, answer_parsed) as accuracy_reward does after #557.
len_reward has the same shape (correctness.append(True), rewards.py:158), but tests/test_rewards.py::test_unparseable_solution pins the current 0.5, so I would leave it for a separate decision unless you want both changed together.
Happy to open the PR.
Source: huggingface/open-r1