Skip to content

[rl] Take a training sample's min policy version over all its turns - #5954

Merged
felipemello1 merged 2 commits into
mainfrom
felipemello1/sample-min-version-over-turns
Nov 10, 2026
Merged

felipemello1 merged 2 commits into
mainfrom
felipemello1/sample-min-version-over-turns

Conversation

@felipemello1

@felipemello1 felipemello1 commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

small nit to cover edge case if a sample is moved to a generator with an older policy version

Agent details

TLDR: A training sample's min_policy_version now takes the oldest version over all its turns, not only its
first turn's. Its docstring already promised that ("Oldest policy version among this branch's trained turns",
training_sample_builder.py:47). The value feeds the policy-age metrics and their max_offpolicy_steps check, which
this PR can't newly trip (Effect 3 below), so this is a metrics fix: 3 lines, the same widening the max already gets.

Two generators. Sibling A's turn 0 pins group 7 to v3 on gen 1. Gen 0 loads v4, and B's turn 0 pins group 7 to v4 there (min 4). B's session moves to gen 1, whose pin is v3, so B's turn 1 has min 3. Main reports 4 for B's sample; this PR reports 3.

How a later turn can carry an older version

Two different keys are involved:

  • Each generator pins each group to the version it has when it first serves that group (keyed by group_id,
    engine_loop.py:524-526). That pin is the turn's min_policy_version and its prefix-cache salt
    (vllm/engine.py:359).
  • Routing pins each session (one per rollout) to a generator (keyed by session_id, routing/strategies.py:212),
    so siblings can sit on different generators. A session also moves when its generator is busy (repin_load_ratio,
    routing/strategies.py:149-152).

The bug needs both: a session moves to a generator where its group was pinned earlier.

2 generators. Group 7 is released at v3.
sibling A, turn 0 -> generator 1 (at v3)  -> generator 1 pins group 7 to v3
generator 0 loads v4
sibling B, turn 0 -> generator 0 (at v4), its first request of group 7 -> generator 0 pins group 7 to v4
                                          -> B turn 0: min 4
generator 0 gets busy, so B's session moves to generator 1
sibling B, turn 1 -> generator 1          -> group 7 is pinned to v3 there -> B turn 1: min 3
                                             (it reuses the prompt KV that sibling A computed under v3)
B's training sample: main reports min 4. The oldest version in it is 3.

B's first request reaches generator 0 after the load when its environment starts slowly (a sandbox that takes a
while to boot), or when the earlier siblings all went to generator 1.

Effect

  1. Per-sample metrics read the true age for samples like B's: train_batch/policy_age (mean) and
    train_batch/pct_samples_over_target_age (controller/metrics.py:203-232).
  2. Batch-level readers usually don't move: the max_offpolicy_steps check, the over-target warning and
    rollout/min_policy_version use the batch max age or min version, and a sibling like A already carries min 3.
  3. It can't newly trip max_offpolicy_steps: generator versions only grow, so no turn's pin is older than the
    oldest generator's version when its group was admitted. On main, turn 0 alone could already carry that pin.
  4. examples/verifiers/generation_server.py:246-252 already takes the min over turns.

Test plan

pytest -q tests/rl/unit_tests/cpu/test_rollout_utils.py   # the new (4, 3) case fails before the fix, 17 passed after
pytest -q tests/rl/unit_tests/cpu                         # 569 passed, 7 skipped (main: 568)

A training sample kept its opening turn's min_policy_version and widened only the max. A later
turn can be older: the router moves a session off an overloaded generator (repin_load_ratio),
and the new generator may not have finished the same model state dict load. The sample's age
then read too low. Widen the min the same way as the max, as the verifiers generation server
already does.
@pytorch-bot pytorch-bot Bot added the ciflow/rl label Nov 8, 2026
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 8, 2026
@claude

claude Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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


Approve -- please address the items below before merging (the human-written summary is empty): 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 bug is real on main, the fix is the smallest one possible (it mirrors the existing max-widening and what examples/verifiers/generation_server.py:248 already does), and it can't newly trip the max_offpolicy_steps invariant.

What this PR does. rollout_to_training_samples took a packed sample's min_policy_version from its first turn only, but TrainingSample.min_policy_version is documented as "Oldest policy version among this branch's trained turns" (training_sample_builder.py:47). A later turn can be older: PrefixAwareRouting moves an overloaded session to another generator (routing/strategies.py:202, repin_load_ratio), and generators load model state dicts independently. The PR now widens the min over every turn, the same way the max is widened.

Example.

turn 0 -> generator 0 (on v4): stamp 4
turn 1 -> repinned to generator 1 (still on v3): stamp 3
trainer at v6
  main: sample min 4 -> policy_age 2   (understated)
  PR:   sample min 3 -> policy_age 3

Affected consumers: rollout/min_policy_version (training_sample_builder.py:242), train_batch/policy_age* and the over-target warning (controller/metrics.py:203-227). Metric keys stay the same; only the values get more accurate.

Must fix

  1. PR description -- the "Summary (human-written)" section is empty. The author needs to fill it in before merge (per CONTRIBUTING.md, agents don't write it).
Details

max_offpolicy_steps safety. compute_policy_age_metrics raises RuntimeError when trainer_version - min_policy_version > max_offpolicy_steps (metrics.py:208), so making the min smaller could in principle make it fire. It can't fire because of this PR. A turn on generator 1 is stamped either with generator 1's current version (KV reset on load) or with generator 1's _group_min_policy_versions.setdefault(group_id, ...) pin (engine_loop.py:519-528). Both are >= generator 1's version when the group was admitted, because versions only increase. On main, the group's turn 0 could already have been routed to generator 1 at admission and stamped with that version, so the PR adds no new worst case.

Same-generator multi-turn (no repin): every rollout in the group shares the per-group pin, so turn k's stamp can never be below turn 0's. The fix only matters across generators, as the inline comment says.

Test. The change parametrizes the existing test, (3,4) plus the new (4,3); no new test function. (4,3) fails on main, so it guards the regression.

Overlap. #5953 (re-prefill on next turn, drop the KV reset on load) is in the same area but changes stamping, not packing. The two are independent.

A maintainer still needs to review (touches torchtitan/rl/trainer/).

@felipemello1
felipemello1 marked this pull request as ready for review November 8, 2026 23:50
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 8, 2026
@github-actions
github-actions Bot requested a review from pzhan9 November 8, 2026 23:50

@tianyu-l tianyu-l 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.

Each generator pins a group to the version it has when it first serves that group

I found this description to be less accurate

I think here you meant session, not group

@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @tianyu-l thanks. You're right that routing is per session. The version pin is per group, though, and the bug needs both:

  • Pin: each generator keys it by group_id (engine_loop.py:524-526, self._group_min_policy_versions.setdefault(request.group_id, self.policy_version)). The salt is that pinned version (vllm/engine.py:359, prompt["cache_salt"] = str(request.min_policy_version)).
  • Routing: keyed by session_id (routing/strategies.py:212), one session per rollout.

So B's session moves to gen 1, where group 7 was already pinned to v3 by sibling A, and B's turn 1 gets min 3. I reworded that paragraph to name both keys and added a picture of the example under the TLDR.

@felipemello1
felipemello1 merged commit afb8427 into main Nov 10, 2026
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