What the think-tag penalty checks, and how this PR runs it on token capture

NVIDIA-NeMo/RL #4776 ยท commits 97e23867f, c3977d53d ยท all links pinned to head 96ec8a36

Before
The think-tag penalty only worked on the echo path. With token capture on, setup refused it.
This PR
Token capture runs the same per-turn check as echo.
Status
Added by this PR, and the check matches echo. One small gap exists on both paths; it goes in a follow-up issue (last section).

Three pages on this PR, one question each. This one: the think-tag penalty.

Background. A reasoning model first thinks, then answers: <think> plan </think> answer. When thinking is on, the chat template already puts <think> at the end of the prompt, so the model should write exactly one </think> and no <think>. When thinking is off, the template puts an empty <think></think> in the prompt, and the model should write no tags at all. The think-tag penalty sets a rollout's reward to 0 when the model breaks this, so training stops rewarding broken output. A rollout comes back from Gym in one of two formats, shown below. All of the penalty runs in RL, on both paths. Gym only returns the task reward from its /run call; RL checks the tokens and sets that reward to 0. Dotted words have a short explanation: hover or tap them.

Here is one coding-agent rollout with two turns. In each turn, some new prompt tokens arrive (the task, or a tool result), and then the model writes some tokens. The echo path gets this as chat messages: a user message with the new prompt tokens, then an assistant message with what the model wrote. The token-capture path gets the exact token list vLLM saw and sampled, with no messages. Instead, Gym adds a list of spans: one pair of lengths per turn, (new prompt tokens, model tokens). The new code walks the list with those lengths and cuts out the same turns.

fix bug<think>plan</think>lstool:a.pyasst:<think>read</think>cata.py<end>Turn 1new promptmodel wroteTurn 2new promptmodel wroteEcho path: Gym sends 4 messagesuser2 tokensassistant3 tokensuser4 tokensassistant5 tokensToken-capture path: Gym sends one flat token list + a list of lengthstoken_ids: all 14 tokens above, in order, no message boundariesspans: [(2, 3), (4, 5)] = (new prompt, model wrote) token counts per turnBoth paths cut out the same turns, then run the same check on eachprompt ends in <think> โ†’ thinking onneed: one </think>, no <think>got: one </think> โ†’ okprompt ends in <think> โ†’ thinking onneed: one </think>, no <think>got: one </think> โ†’ okResult: no turn broke the rule, so the reward stays 1.0. One bad turn would set the whole rollout's reward to 0.code:echo builds messagescapture cuts turns by spanthe check (capture)the check (echo)token from the prompttoken the model wrote<think>= one special tag token. Example rollout; 1 box = 1 token.

The rule, with inputs and outputs

For each turn: look at the new prompt to decide if thinking is on, then count the tags the model wrote. It counts twice. First it counts the special tag tokens. Then it turns the model's tokens back into text and counts the tag strings, because a model can also spell </think> out of ordinary pieces like </ think >, which the token count cannot see. Reward starts at 1.0 in every row:

New prompt ends withModel wroteTag tokensTag textReward
<think> (thinking on)plan </think> ls0 open, 1 close: okok1.0
<think>plan ls (never closed)0 close, need 10
<think><think> plan </think> ls1 open, need 00
<think>plan </think> ls, but <think> spelled from plain pieceslooks ok1 <think> string0
<think></think> (thinking off)lsnone: okok1.0
<think></think></think> ls1 close, need 00
<think></think></think> ls, spelled from plain pieceslooks ok1 close string, allowed up to 11.0 (missed)

On capture, RL's finalizer runs this after rebuilding the token list, and the trainer only sees the final reward. Code: has_malformed_think_tag (capture, new) and the echo check use the same counts.

What still differs, and the one gap

EchoToken capture (this PR)
Where the tag text comes fromthe text Gym sends with each replyRL turns the tokens back into text with the tokenizer, so it now loads one, only when this penalty is on
Extra check on the answer textRL also checks each reply's final answer text and flags any tag in itNot done: capture has only tokens, no separate answer text. The docs say so.
Resume from a checkpointnothing savedThe tag settings are saved, and a resume with different tags is refused

The gap is the last table row above: thinking off, and the model spells </think> from plain pieces. The token count expects 0 and sees 0. The text count allows up to 1 on every turn, even though a thinking-off turn should allow 0. So nothing fires. Echo has the same rule, so this PR does not make anything worse. The fix for both paths: when thinking is off, allow 0 close strings, not 1.

โ†ช C3 ยท review comment โ€” reward_penalties.py:495

So what. Token-capture runs can now use the think-tag penalty, and it judges each turn exactly like echo does. The only real difference is echo's extra check on the answer text, which capture has no text for. The shared gap is small and goes into the follow-up issue the author already agreed to file.