NVIDIA-NeMo/RL #4487.
All permalinks pin the PR head c169600.
max_response_length = M and
overlong_buffer_length = B, an answer up to M โ B tokens keeps its reward. Each token past that costs
1/B of the penalty weight (reward_functions.py:132,
L165-168).
Dotted words have a short explanation: hover or tap them.In a multi-turn rollout (tool use, agents, max_rollout_turns > 1), one sample holds several
assistant turns with tool or environment output between them. Before this PR, the length came from two places,
and both kept only the first assistant turn (links below point at the code before the PR):
apply_reward_shaping
scans the message log
and hits break at the first assistant turn.decompose_message_log
walks every turn with no early exit, but it only sets the length while it is still 0:
if role == "assistant" and response_lengths[i] == 0: response_lengths[i] = length.
After the first assistant turn sets it, every later turn is skipped. It feeds
response_token_lengths to the
data-plane path.So an answer split into many short turns was never penalized, however long it was in total.
The numbers come from running the real apply_reward_shaping on this sample with a reward of 1.0.
The old code returns 1.0 and the new code returns 0.625. The data-plane path gives the same two answers
(response_token_lengths 15 before, 27 after).
Where the fix lands: RL only, two functions.
apply_reward_shaping (scans the message log) and
decompose_message_log (data-plane path). No config change.
@@ reward_functions.py L144-L156 (head) ยท apply_reward_shaping + # The response length is the total over all assistant turns, so that + # multi-turn rollouts are measured by everything the policy generated. response_lengths = [] for message_log in batch["message_log"]: - length = None - for message in message_log: - if message["role"] == "assistant": - length = message["token_ids"].shape[0] - break - assert length is not None, ( + assistant_lengths = [ + message["token_ids"].shape[0] + for message in message_log + if message["role"] == "assistant" + ] + assert assistant_lengths, ( "Assistant response not found during reward shaping" ) - response_lengths.append(length) + response_lengths.append(sum(assistant_lengths)) @@ llm_message_utils.py L816-L817 (head) ยท decompose_message_log lengths_i[t] = length - if role == "assistant" and response_lengths[i] == 0: - response_lengths[i] = length + if role == "assistant": + response_lengths[i] += length
The first hunk replaces "stop at the first assistant turn" with "collect every assistant turn and add them up". The second does the same for the precomputed field, so both paths agree. A new test runs the same two-turn sample through both paths and checks the same penalty (test_reward_shaping_counts_all_assistant_turns).
| Path | Where shaping runs | Length from | Affected? |
|---|---|---|---|
GRPO, sync (grpo_train) | grpo.py:3505 | message log scan | Yes, fixed here |
| PPO | ppo.py:1591 | message log scan | Yes, fixed here |
GRPO, data-plane (grpo_sync) | grpo_sync.py:790 | response_token_lengths from decompose | Yes, fixed here |
| Async GRPO | none | n/a | No: the launcher refuses shaping (run_grpo.py:193) |
| Single controller | none | n/a | No: config check refuses shaping (config.py:1123) |
The rows marked "No" never run reward shaping, so this PR does not change them.
max_response_length.
No shipped example config has both, so no shipped recipe changes.
Open in review: the sum counts every role: "assistant" message, but the GRPO
loss mask
trains only on turns with generation_logprobs. The review asks to use that rule for both length paths
and for the PPO and distillation loss masks. Gym, including PivotRL, is already safe: earlier turns arrive inside a user message
(nemo_gym.py).
๐ review comment โ reward_functions.py:152 review comment โ llm_message_utils.py:817