Skip to content

[rl] TrainingSampleBuilder drops only the rollout it can't train, and counts it by reason (4/4) - #6204

Merged
felipemello1 merged 1 commit into
mainfrom
felipemello1/infra-errors/4-rollout-filter
Nov 23, 2026
Merged

felipemello1 merged 1 commit into
mainfrom
felipemello1/infra-errors/4-rollout-filter

Conversation

@felipemello1

@felipemello1 felipemello1 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

Agent details

TLDR: The builder drops a rollout it can't train on its own, never its siblings, and counts each drop by reason in training_sample_builder/num_rollouts_dropped/<reason>. A trainable rollout with no advantage is dropped and counted instead of stopping the run. A new metric, training_sample_builder/zero_advantage_frac, logs the share of trained rollouts whose advantage is 0. This PR changes no loss numbers: the zero-std group drop is as before. Fourth of 4, on top of the Verifiers PR (3/4).

For a group of 3 where rollout 0 has an untrainable status, no completion token, an empty turn or no advantage, the builder used to skip it uncounted, drop the whole group or stop the run, while in this PR only rollout 0 is dropped and counted by reason; with rewards 1, 0 and 0.5 all 3 train and zero_advantage_frac is 1/3.

Problem

  1. After (1/4)-(3/4), both stock rollout workers mark a rollout that fails outside the policy ERROR_INFRA, and the builder skips it.
  2. Every other row of the table is a rollouter bug, and the builder handles each one differently: it drops the group, drops the rollout, or raises and stops the run.
  3. Its metrics don't say how many rollouts were dropped, or why. num_groups_dropped_untrainable counts groups, and num_rollouts_dropped_empty_completion counts only empty later turns.
  4. No metric shows how many trained rollouts have advantage 0. Such a rollout adds nothing to the gradient, only tokens to the loss's token count.

Solution

  1. _select_rollouts_to_train runs first. It drops a rollout for the first reason that matches: untrainable (a status that never trains), empty_turn (no turn, or a turn with no completion tokens), no_advantage (a trainable rollout with no advantage). It logs a warning for the last two.
  2. Each drop counts in training_sample_builder/num_rollouts_dropped/<reason>. It replaces training_sample_builder/num_groups_dropped_untrainable and training_sample_builder/num_rollouts_dropped_empty_completion.
  3. training_sample_builder/zero_advantage_frac: the share of trained rollouts whose advantage is 0 (within 1e-9).
  4. rollout_to_training_samples no longer checks for empty turns: the builder drops those rollouts first.
  5. rollout/min_policy_version and rollout/max_policy_version read only the training samples that are kept.
  6. Unchanged: the zero-std group drop keeps its flag (drop_zero_std_reward_groups), its metrics (num_groups_dropped_zero_std, rollout_reward/group_zero_std_frac) and its rule from (1/4). It reads the rewards of the rollouts that _select_rollouts_to_train keeps. A group with no empty turn and no missing advantage trains the same samples, with the same loss.

You may ask: why drop a rollout with no advantage, not raise? A rollouter bug in one rollout shouldn't stop the run. The drop is logged and counted in num_rollouts_dropped/no_advantage.
You may ask: why not drop the zero-advantage rollouts too? With partial rewards, e.g. [1, 0, 0.5], the 0.5 rollout has advantage 0. Dropping it leaves the same gradient divided by fewer loss tokens, a larger step. zero_advantage_frac measures how often that happens first.

New W&B keys: training_sample_builder/num_rollouts_dropped/untrainable, .../empty_turn, .../no_advantage, training_sample_builder/zero_advantage_frac.
Removed W&B keys: training_sample_builder/num_groups_dropped_untrainable, training_sample_builder/num_rollouts_dropped_empty_completion.

Test plan

pytest -rs tests/rl/unit_tests/cpu tests/unit_tests/cpu/components/test_renderer.py tests/unit_tests/cpu/config
# Verifiers 0.4.0: 1047 passed, 1 skipped (test_train_loop.py, skipped on the base too).
# Without Verifiers: 963 passed, 7 skipped (the Verifiers test files and recipes).

pre-commit passes. pyrefly skips torchtitan/rl and tests (project-excludes in pyproject.toml), so it checks none of this PR's files.
Each of these tests fails on the builder of (3/4):

  • test_build_from_group_drops_a_rollout_with_an_empty_completion: an empty later turn, an empty turn 0, no turn.
  • test_build_from_group_trains_a_zero_advantage_rollout_and_counts_it: rewards [1, 0, 0.5]; all 3 train, zero_advantage_frac = 1/3.
  • test_build_from_group_skips_an_unscored_rollout_but_trains_its_siblings: gains a no_advantage case, and checks the count for each reason.

Changed: test_batcher_actor_returns_the_in_process_batch triggers its builder error with a bad prompt_prefix_len ("reuses 99 tokens"), since a missing advantage no longer raises.

Deleted: test_empty_completion_on_a_later_turn_raises and test_empty_completion_on_first_turn_is_skipped (the checks they pinned are gone), test_build_from_group_drops_a_rollout_with_an_empty_later_completion (now a case of the first test).

@felipemello1
felipemello1 added this pull request to stack #6205 November 19, 2026 01:49
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 19, 2026
@felipemello1
felipemello1 force-pushed the felipemello1/infra-errors/4-rollout-filter branch from 5659b20 to d33b84d Compare November 19, 2026 02:00
@felipemello1
felipemello1 force-pushed the felipemello1/infra-errors/4-rollout-filter branch from d33b84d to 249f6c9 Compare November 19, 2026 02:10
@felipemello1
felipemello1 marked this pull request as ready for review November 19, 2026 19:25
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 19, 2026
@felipemello1

Copy link
Copy Markdown
Contributor Author

@claude review

@github-actions
github-actions Bot requested a review from pzhan9 November 19, 2026 19:25
@claude

claude Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 2m 29s —— View job


Approve (please address the items before merging): review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Approve -- please address the items below before merging. The builder now has one place that decides which rollouts train, and it counts every drop by reason. Stock recipes train exactly the same samples. A maintainer still needs to review.

What this PR does. _select_rollouts_to_train runs once per group, before the zero-std check. It drops each rollout separately, as untrainable, empty_turn or no_advantage, and the drops are counted in training_sample_builder/num_rollouts_dropped/<reason>. Before, the builder handled these cases three ways: drop the whole group, drop the rollout inside the loop, or raise inside rollout_to_training_samples. That last check is now removed. The PR also adds zero_advantage_frac, and the policy-version metrics now read only the samples that are kept.

Example. One group, sibling 0 broken, the others fine:

sibling 0 turns         base (3/4)             this PR
[]  (no turn)           whole group dropped    drop 0, count empty_turn
[[], [4]]               ValueError, run stops  drop 0, count empty_turn
[[4], []]               drop 0, count          drop 0, count empty_turn
COMPLETED, adv=None     ValueError, run stops  drop 0, count no_advantage

Both stock workers already turn an empty completion or an empty turn list into ERROR_INFRA (token_env.py:251, verifiers/rollouter.py:375). So these rows only happen with custom rollouters, and the "no loss change" claim holds for in-repo recipes.

Must fix

  1. PR description: "Summary (human-written)" is empty. The author needs to fill it in.

Should consider

  • training_sample_builder.py:106-116: an empty_turn rollout was already scored, so its reward went into its siblings' advantages. Dropping only that rollout trains the siblings on biased advantages. It's cleaner to mark such a rollout untrainable in make_scored_group before scoring. See the inline comment.
  • training_sample_builder.py:223-225: the reward is not None filter can quietly change the zero-std decision. Remove it or count it as a drop reason.
  • training_sample_builder.py:285-291: the comment says "trained rollouts", but the metric also covers selected rollouts that end up with no sample. 1e-9 needs a reason.

Questions

  • Turning no_advantage and empty_turn from a raise into warn-and-drop goes against the repo's "errors over warnings" rule (CLAUDE.md principles 8 and "assert for programmer error"). The PR explains its reason: one buggy rollout shouldn't stop the run. The batcher's consecutive-untrainable limit (batcher.py:355) still stops a run where every group is broken. Can a maintainer confirm that counting plus a warning per rollout is the policy they want for rollouter bugs?
Details
  • rollout_to_training_samples is documented as the extension point to override. It no longer guards against empty turns, so callers depend on the builder's pre-filter. If it is called directly with an empty turn, it builds a prompt-only sample, which _has_valid_loss_token then drops. Nothing crashes.
  • Its advantage is None raise can no longer be hit from build_from_group. It still narrows the type for direct callers, so keeping it is fine.
  • Empty-list metrics (Mean, Min, Max, SummaryStats.from_list([])) handle 0 items, so a group where every rollout is dropped and drop_zero_std_reward_groups=False still logs safely.
  • Removed metric keys (num_groups_dropped_untrainable, num_rollouts_dropped_empty_completion) have no other in-repo readers.

Not verified: I did not run the unit tests.

Comment on lines +106 to +116
# Sanity check: filter a rollout with no turn, or a turn with no completion.
if not rollout.turns or any(
len(turn.completion_token_ids) == 0 for turn in rollout.turns
):
logger.warning(
"rollout %s/rollout=%d has no turn, or a turn with no completion; dropping it",
rollout.group_id,
rollout.rollout_id,
)
num_rollouts_dropped["empty_turn"] += 1
continue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An empty_turn rollout has a trainable status, so make_scored_group (rollouter.py:412-422) already scored it and fed its reward into the advantage estimator. Dropping it here keeps its siblings, but their advantages are still centered on a reward that never trains. For example, rewards [1 (empty turn), 1, 0, 0] have mean 0.5. The siblings train with advantages [+0.5, -0.5, -0.5], which sum to -0.5, where the kept rollouts alone would have mean 1/3. The zero-std check below then runs on the kept rewards, which doesn't match how the advantages were computed.

The old code had the same problem for an empty later turn. For an empty turn 0 or no turn, it dropped the whole group, which kept the advantages clean. This PR's "drop only that rollout" rule makes that case biased too.

Both stock workers go through make_scored_group, so the clean fix is in that layer. Mark an empty-turn or no-turn rollout untrainable before scoring, so it never enters the advantage, the same way TokenEnv and the Verifiers rollouter already make it ERROR_INFRA. Then this branch is only a sanity check for rollouters that skip make_scored_group. If you'd rather keep this PR small, at least drop the group for empty_turn here, since only no_advantage rollouts are known to be outside the estimator.

Comment on lines +223 to +225
rewards = [
rollout.reward for rollout in rollouts_to_train if rollout.reward is not None
]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every rollout here is trainable and has an advantage, and make_scored_group sets reward on the same rollouts it gives an advantage. So reward is None can only come from a rollouter bug. When that happens, this filter quietly changes the zero-std decision: it uses fewer rewards, and a group can fall below 2 rewards and be dropped. Nothing gets counted. That's the kind of just-in-case check CLAUDE.md principle 8 asks us not to add.

Either remove the filter, or check reward is None together with advantage is None in _select_rollouts_to_train (e.g. no_score) so it is counted like the other drops.

Comment on lines +285 to +291
# Share of trained rollouts whose advantage is 0: they add nothing to the gradient, only
# tokens to the loss's token count.
m.Metric(
"training_sample_builder/zero_advantage_frac",
m.Mean.from_list(
[
float(math.isclose(advantage, 0.0, abs_tol=1e-9))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two small points.

  1. "Share of trained rollouts" isn't quite right. advantages covers every rollout in rollouts_to_train, including ones whose samples were all dropped by _has_valid_loss_token above, which therefore train nothing. Either say "rollouts selected to train" or compute the fraction over rollouts that kept at least one sample.
  2. abs_tol=1e-9 needs a one-line reason, e.g. that it absorbs float rounding in the mean/std estimator. The rules ask for a comment on magic constants.

@felipemello1
felipemello1 force-pushed the felipemello1/infra-errors/4-rollout-filter branch from 249f6c9 to be71e9f Compare November 19, 2026 23:24
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review

@claude

claude Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 1m 1s —— View job


Approve (the diff is the same as at my last review, and its items are still open): review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Approve -- please address the items below before merging. The force-push to be71e9f only rebases this PR onto the updated (3/4). The diff in training_sample_builder.py and in both test files is the same as at 249f6c9, so every item from the earlier review is still open. A maintainer still needs to review.

What this PR does. _select_rollouts_to_train runs once per group. It drops each rollout on its own, as untrainable, empty_turn or no_advantage, and counts each drop in training_sample_builder/num_rollouts_dropped/<reason>. It also adds zero_advantage_frac.

Example. One group, sibling 0 broken, the others fine:

sibling 0               base (3/4)             this PR
turns=[]                whole group dropped    drop 0, count empty_turn
turns=[[], [4]]         ValueError             drop 0, count empty_turn
COMPLETED, adv=None     ValueError             drop 0, count no_advantage

I checked this against the rebased base. TokenEnv.step (token_env.py:251) still turns an empty completion into ERROR_INFRA. TRUNCATED_PROMPT_TOO_LONG (token_env.py:383-391) is trainable, but it adds no empty-completion turn. So stock recipes never reach empty_turn, and the claim that no loss changes still holds for in-repo recipes.

Must fix

  1. PR description: "Summary (human-written)" is still empty.

Should consider (still open, see the earlier inline comments)

  • training_sample_builder.py:106-116: make_scored_group (rollouter.py:412-435) scores an empty_turn rollout and includes its reward in its siblings' advantages. Dropping only that rollout then trains its siblings on biased advantages. The fix: mark such a rollout untrainable before scoring, or drop the whole group for empty_turn.
  • training_sample_builder.py:223-225: the reward is not None filter changes the zero-std decision without counting anything. Remove it, or make it a counted drop reason.
  • training_sample_builder.py:285-294: the comment says "trained rollouts", but advantages covers every selected rollout, including ones whose samples _has_valid_loss_token removes. Also, abs_tol=1e-9 needs a one-line reason.

Questions

  • The PR changes rollouter bugs (no_advantage, empty_turn) from raising to warning and dropping. Can a maintainer confirm that this is the policy they want? The batcher's consecutive-untrainable limit still stops a run where every group is broken.

Not verified: I did not run the unit tests.

@felipemello1
felipemello1 force-pushed the felipemello1/infra-errors/4-rollout-filter branch from be71e9f to 4f0548a Compare November 22, 2026 09:03
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review

@claude

claude Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 1m 2s —— View job


Approve (the rebase onto the updated (3/4) left this PR's changes the same, so the earlier items are still open): review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Approve -- please address the items below before merging. The force-push to 4f0548a rebases this PR again, this time onto the updated (3/4) at 2784265. This PR's own changes are the same as at be71e9f: same hunks, same +163/-104. So the items from the earlier reviews are still open. tianyu-l has approved. Their review has no comments, so no maintainer comment is waiting on a reply.

What this PR does. _select_rollouts_to_train drops each rollout on its own and gives a reason: untrainable, empty_turn or no_advantage. Each drop is counted in training_sample_builder/num_rollouts_dropped/<reason>. The PR also adds zero_advantage_frac and removes the empty-turn checks from rollout_to_training_samples.

Example. One group where sibling 0 is broken and the others are fine:

sibling 0               base (3/4)             this PR
turns=[]                whole group dropped    drop 0, count empty_turn
turns=[[], [4]]         ValueError             drop 0, count empty_turn
COMPLETED, adv=None     ValueError             drop 0, count no_advantage

make_scored_group (rollouter.py:402-449) still skips only rollouts that aren't is_trainable(). The Verifiers rollouter still marks a trainable rollout with no turns as ERROR_INFRA (verifiers/rollouter.py:373). So stock recipes never reach empty_turn, and the "no loss change" claim still holds.

Must fix

  1. PR description: "Summary (human-written)" is still empty.

Should consider (still open, see the earlier inline comments)

  • training_sample_builder.py:105-115: an empty_turn rollout was already scored by make_scored_group, so its reward went into its siblings' advantages. Dropping only that rollout trains the siblings on biased advantages. Either mark it untrainable before scoring, or drop the whole group for empty_turn.
  • training_sample_builder.py:220-222: the reward is not None filter changes the zero-std decision and counts nothing. Remove it, or make it a counted drop reason.
  • training_sample_builder.py:282-291: the comment says "trained rollouts", but advantages covers every selected rollout, including ones whose samples _has_valid_loss_token removes. Also, abs_tol=1e-9 needs a one-line reason.
Details
  • The rebased (3/4) removed the TODO above the rollout pick: "a group left with 10 of 16 rollouts gives its prompt about 10/16 of a full group's weight". This PR doesn't touch that line. Still, empty_turn and no_advantage now drop one rollout where the base dropped the group or raised, so partial groups are more common. If the caveat still applies, it may be worth putting the TODO back.
  • tianyu-l's approval settles the earlier question of whether to warn and drop or to raise on rollouter bugs.

Not verified: I did not run the unit tests.

@felipemello1
felipemello1 force-pushed the felipemello1/infra-errors/4-rollout-filter branch 3 times, most recently from 1dbfecb to 22d2d2c Compare November 23, 2026 19:35
Base automatically changed from felipemello1/infra-errors/3-verifiers-status to main November 23, 2026 19:56
… counts it by reason

_select_rollouts_to_train drops a rollout that can't train and counts it in
training_sample_builder/num_rollouts_dropped/<reason>: untrainable (a status such as ERROR_INFRA),
empty_turn (no turn, or a turn with no completion tokens) or no_advantage (a trainable rollout with
no advantage, which used to raise). A rollout with no completion token no longer drops its whole
group. training_sample_builder/zero_advantage_frac logs the share of trained rollouts whose
advantage is 0. The zero-std group drop is unchanged: a group with no empty turn and no missing
advantage trains the same samples, with the same loss, as before.
@felipemello1
felipemello1 force-pushed the felipemello1/infra-errors/4-rollout-filter branch from 22d2d2c to cd9b3d0 Compare November 23, 2026 19:57
@felipemello1
felipemello1 merged commit a7887db into main Nov 23, 2026
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants