Why a long multi-turn answer escaped the overlong penalty, and how this PR counts every turn

NVIDIA-NeMo/RL #4487. All permalinks pin the PR head c169600.

Before
The overlong penalty measured only the first assistant turn of a sample. Later turns were never counted.
This PR
Adds up the tokens of all assistant turns, on both places that measure length.
Status
Fixed by this PR. Single-turn runs get the same numbers as before. The review asks for one more change (see the end).
Background. DAPO's overlong reward shaping lowers the reward of answers that run too long. With 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):

So an answer split into many short turns was never penalized, however long it was in total.

One sample, measured two ways

One sample: max_response_length = 32, overlong_buffer_length = 8, so the penalty starts after 32 โˆ’ 8 = 24 tokens turns user 4 assistant 15 env 6 assistant 12 before first assistant turn only = 15 old loop: break at first assistant turn this PR 15 + 12 = 27 sum(assistant_lengths) response_lengths[i] += length penalty 0 24 32 no penalty penalty grows before: 15 โ‰ค 24 โ†’ reward stays 1.0 this PR: 27 is 3 past 24 โ†’ 1.0 โˆ’ 3/8 = 0.625 assistant turn counted before counted by this PR penalty zone

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).

The fix

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).

Which training paths are affected

PathWhere shaping runsLength fromAffected?
GRPO, sync (grpo_train)grpo.py:3505message log scanYes, fixed here
PPOppo.py:1591message log scanYes, fixed here
GRPO, data-plane (grpo_sync)grpo_sync.py:790response_token_lengths from decomposeYes, fixed here
Async GRPOnonen/aNo: the launcher refuses shaping (run_grpo.py:193)
Single controllernonen/aNo: config check refuses shaping (config.py:1123)

The rows marked "No" never run reward shaping, so this PR does not change them.

Who notices. Only runs with reward shaping on and more than one assistant turn per sample. They will see more overlong penalties and may need to retune 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