trl icon indicating copy to clipboard operation
trl copied to clipboard

The `<think>`-format examples score with `accuracy_reward`, which credits a `\boxed{}` answer inside the reasoning block

Open jimmy-holdout opened this issue 1 month ago • 4 comments

Reproduction

Six examples prompt for a <think></think> block (the prompt's format example puts the answer after </think>) and score with reward_funcs=[think_format_reward, accuracy_reward]:

accuracy_reward parses the whole completion, including the <think> block. So a \boxed{} answer written inside the reasoning gets the accuracy reward, even when nothing after </think> answers the question, or when the completion stops before </think>:

from trl.rewards import accuracy_reward, reasoning_accuracy_reward, think_format_reward


completions = [
    [{"content": "<think>\nMaybe \\boxed{7}? Not sure.\n</think>\nI don't know."}],  # answer only inside <think>
    [{"content": "<think>\nMaybe \\boxed{7}? Let me check this again"}],  # answer only inside <think>, never closed
    [{"content": "<think>\nLet me work it out.\n</think>\nThe answer is \\boxed{7}."}],  # answer only after </think>
    [{"content": "<think>\nMaybe \\boxed{7}.\n</think>\nThe answer is \\boxed{7}."}],  # answer in both
    [{"content": "<think>\nMaybe \\boxed{7}.\n</think>\nThe answer is $5$."}],  # wrong final answer, not boxed
]
solution = ["$7$"] * 5

print(think_format_reward(completions))
print(accuracy_reward(completions, solution))
print(reasoning_accuracy_reward(completions, solution))

outputs:

[1.0, 0.0, 1.0, 1.0, 1.0]
[1.0, 1.0, 1.0, 1.0, 1.0]
[0.0, 0.0, 1.0, 1.0, 0.0]

So with the examples' reward pair, the first completion (no answer after the reasoning) and the last one (a wrong final answer written without \boxed{}, which the examples' prompt never asks for) both get the maximum total of 2.0, the same as a correct final answer.

reasoning_accuracy_reward scores only the text after </think>. The README recommends it for reasoning models, and the grpo_qlora notebook already uses [think_format_reward, reasoning_accuracy_reward]. The six scripts above don't. This is the case reasoning_accuracy_reward was added for in #4563 ("reasoning content ... can bias the grader").

The same pattern holds on the examples' datasets. For each task I took the gold answer G (the last \boxed{} in the solution for NuminaMath-TIR, the <answer> tag for the visual dataset), built the five completions below, scored them against the task's original solution with both reward pairs, and kept the tasks where the first (correct) completion gets the full 2.0 under both:

completion accuracy_reward = 1 reasoning_accuracy_reward = 1
\boxed{G} only after </think> all all
\boxed{G} inside <think> and after it all all
\boxed{G} only inside <think>, then "I don't know." all 0
\boxed{G} only inside <think>, stops before </think> all 0
wrong final answer in \boxed{-987654321} after </think> 0 0

"all" is 3,580 of the 3,622 tasks in NuminaMath-TIR train[:5%] (rloo_math, gspo_math) and 2,227 of the 7,689 rows of lmms-lab/multimodal-open-r1-8k-verified (the four visual-math examples; full source dataset, before the examples' train/test split and image filter). On the visual dataset, 16 rows have no extractable non-empty <answer> value, 1,703 have a gold solution math_verify can't parse, and 3,743 fail the reference check; 1,690 of those have a single-letter (multiple-choice) gold answer, related to the answer-format mismatch described in #3903. That's separate from this issue and affects both rewards the same way. These are fixed, constructed completions, not model samples.

Script for the dataset counts (python table.py numina / python table.py visual)
import re
import sys

from datasets import load_dataset

from trl.rewards import accuracy_reward, reasoning_accuracy_reward, think_format_reward


def last_boxed(s):
    i = s.rfind("\\boxed{")
    if i < 0:
        return None
    j = k = i + len("\\boxed{")
    depth = 1
    while k < len(s) and depth:
        depth += {"{": 1, "}": -1}.get(s[k], 0)
        k += 1
    return s[j : k - 1] if depth == 0 else None


if sys.argv[1] == "numina":
    ds = load_dataset("AI-MO/NuminaMath-TIR", split="train[:5%]").select_columns(["solution"])
    get_gold = last_boxed
else:
    ds = load_dataset("lmms-lab/multimodal-open-r1-8k-verified", split="train").select_columns(["solution"])
    get_gold = lambda s: (re.findall(r"<answer>(.*?)</answer>", s, re.S) or [""])[-1].strip() or None

templates = [
    "<think>\nLet me work it out.\n</think>\nThe answer is \\boxed{G}.",
    "<think>\nMaybe \\boxed{G}.\n</think>\nThe answer is \\boxed{G}.",
    "<think>\nMaybe \\boxed{G}? Not sure.\n</think>\nI don't know.",
    "<think>\nMaybe \\boxed{G}? Let me check this again",
    "<think>\nLet me work it out.\n</think>\nThe answer is \\boxed{-987654321}.",
]
kept, no_gold, unparseable, ref_fail, acc, reasoning_acc = 0, 0, 0, 0, [0] * 5, [0] * 5
for sol in ds["solution"]:
    gold = get_gold(sol)
    if not gold:
        no_gold += 1
        continue
    completions = [[{"content": t.replace("\\boxed{G}", "\\boxed{" + gold + "}")}] for t in templates]
    f = think_format_reward(completions)
    a = accuracy_reward(completions, [sol] * 5)
    r = reasoning_accuracy_reward(completions, [sol] * 5)
    if a[0] is None:  # math_verify can't parse the gold solution
        unparseable += 1
        continue
    if f[0] + a[0] != 2.0 or f[0] + r[0] != 2.0:  # keep tasks where the correct completion gets 2.0
        ref_fail += 1
        continue
    kept += 1
    acc = [n + (x == 1.0) for n, x in zip(acc, a)]
    reasoning_acc = [n + (x == 1.0) for n, x in zip(reasoning_acc, r)]
print(f"{len(ds)} rows: {no_gold} no gold extracted, {unparseable} gold unparseable,", end=" ")
print(f"{ref_fail} fail the check, {kept} kept")
print("accuracy_reward = 1:          ", acc)
print("reasoning_accuracy_reward = 1:", reasoning_acc)

Output:

3622 rows: 0 no gold extracted, 0 gold unparseable, 42 fail the check, 3580 kept
accuracy_reward = 1:           [3580, 3580, 3580, 3580, 0]
reasoning_accuracy_reward = 1: [3580, 3580, 0, 0, 0]
7689 rows: 16 no gold extracted, 1703 gold unparseable, 3743 fail the check, 2227 kept
accuracy_reward = 1:           [2227, 2227, 2227, 2227, 0]
reasoning_accuracy_reward = 1: [2227, 2227, 0, 0, 0]

Suggested fix, in each of the six files:

  1. Switch to reasoning_accuracy_reward (the import and the reward_funcs line).
  2. Reword the system prompt. It says "The reasoning process and answer are enclosed within <think></think> tags", while its example (<think>\nThis is my reasoning.\n</think>\nThis is my answer.) puts the answer after </think> (also pointed out in #5298). With reasoning_accuracy_reward an answer inside the tags isn't scored, so the prompt should say the answer comes after </think>.

How did you hit this?

Not in a training run. I found it by reading the reward functions with AI assistance; the reproduction and dataset counts above and the Qwen3 sampling below were run with AI help.

What would go wrong for a user: someone who runs one of these examples as written trains with a reward that gives the full 2.0 to a closed <think> block that contains the correct \boxed{} answer followed by no final answer (or by "I don't know."), the same as a committed final answer. A completion cut off before </think> loses the format point but still gets the accuracy point for an answer inside the block. In the GRPO and RLOO examples, rewards/accuracy_reward/mean counts either completion as correct. The same pair also gives the full 2.0 to a wrong final answer written without \boxed{} (which the prompt never asks for) when a correct \boxed{} guess appears inside <think>, as in the last case of the reproduction.

To see whether this shows up with a real model, I sampled one completion each for 64 random NuminaMath-TIR train[:5%] problems from the off-the-shelf Qwen/Qwen3-0.6B checkpoint (before any training with these examples), with the examples' current system prompt, temperature 1.0, top-p 1.0, top-k disabled (the GRPOConfig / RLOOConfig defaults) and 1,024 new tokens (transformers on CPU, not vLLM). 47 of the 64 hit the length limit before </think>. Of the 17 that closed </think>, 2 wrote their \boxed{} final answer inside the <think> block and nothing after </think>; accuracy_reward gave them 1 and reasoning_accuracy_reward 0. Both answers were correct, so these two samples show that the answer can land inside <think> under the original prompt, not a wrong guess being rewarded. I haven't run training, so I can't say how often it would happen after RL.

#5298 is related: a contributor suggested this exact switch for the GSPO example. The reporter said that switching alone did not fix their low accuracy (a larger model did), so I'm not claiming the change improves training results, only that the examples' prompt and reward would then agree on where the answer goes, using the reward the README recommends for reasoning models.

I can open a PR for this. Could you assign the issue to me?

System Info

- Platform: Linux-6.8.0-146-generic-x86_64-with-glibc2.39
- Python version: 3.12.15
- TRL version: 1.17.0.dev0+9824938
- PyTorch version: 2.14.1+cpu
- accelerator(s): cpu
- Transformers version: 5.21.0
- Accelerate version: 1.15.0
- Accelerate config: not found
- Datasets version: 5.1.0
- HF Hub version: 2.3.0
- bitsandbytes version: not installed
- DeepSpeed version: not installed
- Liger-Kernel version: not installed
- PEFT version: not installed
- vLLM version: not installed

Also: math-verify 0.9.0, latex2sympy2_extended 1.11.0.

Checklist

  • [x] I have checked that my issue isn't already filed (see open issues)
  • [x] I have included my system information
  • [x] Any code provided is minimal, complete, and reproducible (more on MREs)
  • [x] Any code provided is properly formatted in code blocks, (no screenshot, more on code blocks)
  • [x] Any traceback provided is complete

jimmy-holdout avatar Oct 27 '26 17:10 jimmy-holdout

Hi! I'd like to work on this issue as my first contribution to TRL.

I investigated the issue and believe the proposed fix is to use reasoning_accuracy_reward instead of accuracy_reward in the six affected examples, and clarify the prompts so that the final answer is placed after .

Would it be possible to assign this issue to me? I'd be happy to prepare a fix and the relevant tests.

Thanks!

MarouaZoubir avatar Oct 28 '26 20:10 MarouaZoubir

Thanks for the interest! I've already got the fix ready (the six example scripts switched to reasoning_accuracy_reward, plus the prompt wording), with tests passing, and asked to be assigned above. I'll open the PR as soon as a maintainer gives the go-ahead.

jimmy-holdout avatar Oct 29 '26 07:10 jimmy-holdout

Thanks for the update! I'm also looking to make my first contribution to TRL and was wondering if you know of any open issues that would be suitable for a first-time contributor. I'd really appreciate any pointers!

MarouaZoubir avatar Oct 29 '26 09:10 MarouaZoubir

Closing. Opening issues in bulk from a scan of the code is not the kind of contribution we welcome: a bug report should come from a real run that hit the bug. Please stop, here and on any other Hugging Face repo, otherwise I'll temporarily ban you. More on why: https://huggingface.co/blog/qgallouedec/open-source-good-intentions

qgallouedec avatar Nov 18 '26 16:11 qgallouedec