Skip to content

[rl] Verifiers: release each finished rollout's routing session (2/14) - #5510

Merged
felipemello1 merged 4 commits into
mainfrom
felipemello1/verifiers-release-sessions
Nov 5, 2026
Merged

felipemello1 merged 4 commits into
mainfrom
felipemello1/verifiers-release-sessions

Conversation

@felipemello1

@felipemello1 felipemello1 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

TLDR: A Verifiers rollout now releases its routing sessions when Verifiers' run returns, as native rollouts already do. It also fixes a bug: one failing Verifiers rollout made the controller release its group while its siblings still generated.

Stack (14 PRs, merge bottom-up; this PR is in bold)

PRs 2-7 don't need the HTTP design and can be reviewed now. PRs 8-14 wait on the design discussion on PRs 9 and 10.

# PR Independent? Needs (stack positions)
1 Verifiers 0.4.0 (merged) yes —
2 Verifiers: release each finished rollout's routing session yes — (resolves its test overlap with 1)
3 Verifiers generation server: no per-request access log yes —
4 Verifiers generation server: faster request path, required sampling params yes — (resolves its test overlap with 1-3)
5 Rollout workers write structured logs yes —
6 Drain in-flight rollouts during shutdown yes —
7 Rollouts that fail outside the policy are recorded, not trained no 2 (resolves its overlap with 4-6)
8 Generator drains itself before a hot_swap=False weight load yes —
9 Move GenerationServer to torchtitan/rl/generator yes, in code — (it moves the file 3 and 4 edit)
10 Every generate call goes over HTTP to its generator no 1, 2, 3, 4, 7, 8, 9
11 Generator aborts a request whose caller left no 10
12 Cancel a rollout group on its worker no 5, 6, 7, 10
13 Run Verifiers groups in rollout workers no 7, 10, 12
14 Optional native rollout timeout no 7, 13

Problem

  1. Native rollouts release. RolloutWorker calls release_session_fn after a rollout's last turn: the router drops the session's pin and, with session_kv_holder, the generator frees its held KV.
  2. Verifiers didn't. VerifiersRollouter.run_group_rollouts dropped release_session_fn (a TODO), so a pin stayed until the max_sessions LRU (262,144 entries) dropped it, and held KV stayed until the group ended.
  3. Group failure. The group gathered without return_exceptions: one failing rollout raised at once, and the controller released the group while its siblings kept generating.
  4. What that leaks. The siblings' later calls pin the group's cache salt and hold KV again, and nothing releases them.
  5. Cost today: small. No recipe turns on session_kv_holder, and a stale pin is ~150 B. It matters once a recipe holds session KV.

Solution

  1. GenerationServer already routes each call by Verifiers' X-Session-ID, which is the trace id. This PR doesn't change the server.
  2. When VerifiersEnvClient.run returns, _run_single_rollout releases every trace of the episode. It also takes each trace's generation metadata from the server, so another agent's trace (a judge, a simulated user) leaves nothing behind.
  3. The group gathers with return_exceptions=True and raises the first failure after every sibling finished.
  4. Unchanged: native rollouts.
Verifiers path                          the rollout's sessions               the group
run returns, ok or not                  released, every trace (new)          released after every sibling (new)
a sibling's run raises                  the others released; it is not       released after every sibling (new)
an attempt Verifiers retried            not released (not in the traces)     released
cancelled mid-rollout (shutdown)        not released                         not released

NOTE:

  • Releasing an id the router never saw does nothing; a failed release is logged.
  • A call Verifiers gave up on can still run after run returns and pin its session again. The group's release frees its held KV; the LRU drops its pin.

Test plan

  • test_verifiers.py::test_rollout_releases_every_trace_session_even_if_one_release_fails: two traces released in order and both traces' metadata taken; the first release's error is logged.
  • test_verifiers.py::test_a_failing_rollout_fails_its_group_only_after_every_sibling_finished: fails without return_exceptions.
  • pytest tests/rl/unit_tests/cpu (verifiers 0.4.0, on PR 1, main d0ab95e): 587 passed, 4 skipped. The fake traces carry is_timeout, which 0.4.0 reads.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 18, 2026
@felipemello1
felipemello1 added this pull request to stack #5513 October 18, 2026 23:32
@felipemello1
felipemello1 force-pushed the felipemello1/verifiers-release-sessions branch from b9a8e8a to cf9cb8a Compare October 19, 2026 01:15
Comment on lines +272 to +277
# Raise only after every sibling finished: the caller releases the group next, and a
# sibling still generating would pin the group again, with nothing left to release it.
for result in results:
if isinstance(result, BaseException):
raise result
rollouts = results

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

we shouldnt throw away the entire group if one rollout fails. Instead, just mark this one as failed, and make sure its not accounted for during advantage. Add a TODO to check if there are techniques to weight a 16-rollout-group vs a 10-rollout-group

Comment on lines +324 to +334
# `run` returned, so this rollout starts no new calls. Release each trace's routing
# session: Verifiers sends the trace id as X-Session-ID.
# TODO: an attempt Verifiers retried has no trace here, so its held KV waits for the
# group's release and its routing pin for the router's `max_sessions` LRU. The generation
# server could record each rollout's session ids and release those too.
for trace in verifiers_episode.traces:
try:
await release_session_fn(routing_session_id=trace.id)
except Exception:
# Not fatal: `max_sessions` evicts an unreleased session.
logger.exception("releasing routing session %s failed", trace.id)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it sounds like we somewhere may define if we should stop a rollout of not, but we check it only in between turns. This is wasteful. We should have the ability to stop it mid rollout. Do we?

Also, all changes we are doing here for verifiers should also exist on the non-verifiers path, e.g. alphabet sort example

@felipemello1 felipemello1 changed the title [rl] Verifiers: release each finished rollout's routing session [rl] Verifiers: release each finished rollout's routing session (2/14) Oct 19, 2026
Base automatically changed from felipemello1/verifiers-0.4.0 to main October 22, 2026 23:31
@felipemello1
felipemello1 force-pushed the felipemello1/verifiers-release-sessions branch from cf9cb8a to 12b762c Compare October 22, 2026 23:31
@felipemello1

Copy link
Copy Markdown
Contributor Author

reply from claude:

  • One failed rollout drops the group: PR 14 at the top of this stack fixes that. A rollout that raises or fails outside the policy becomes ERROR_SYSTEM. It's kept for logs and skipped by scoring and advantage, while its siblings train. This PR only waits for every sibling before raising, so their sessions are released first. I added the TODO about weighting a group that lost rollouts in PR 14, where the group drops them.
  • Still dropped, as on main: a rollout with no completion tokens drops its whole group in TrainingSampleBuilder. That's usually a first prompt past the context, which its siblings share, so the group had nothing to train anyway. Follow-up.
  • Stopping mid-rollout: yes, nothing waits for a turn boundary.
    • PR 10 aborts a generate call whose caller left and frees its KV mid-generation.
    • PR 11 cancels a group on its worker mid-turn.
    • PR 13 adds an optional native rollout timeout.
    • Verifiers ends a rollout with its own timeout.rollout.
  • Native path: yes, it gets the same.
    • It already releases each rollout's session after its last turn (release_session_fn).
    • PR 11 moves that release into a finally.
    • PR 11 also makes the native group wait for every sibling before raising, like this PR.

@felipemello1
felipemello1 removed this pull request from stack #5513 October 27, 2026 14:27
@felipemello1
felipemello1 added this pull request to stack #5639 October 27, 2026 14:33
@felipemello1
felipemello1 force-pushed the felipemello1/verifiers-release-sessions branch from 12b762c to 9fa5486 Compare November 5, 2026 00:34
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude review

@claude

claude Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

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


Verdict: Approve (fix one sentence in the description 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. This closes a real session leak on main with a small change. The only must-fix is a sentence in the description that claims a change this PR doesn't make.

What this PR does. When VerifiersEnvClient.run returns, _run_single_rollout now calls release_session_fn for every trace id. Verifiers sends the trace id as X-Session-ID, and the generation server already uses that header as routing_session_id. run_group_rollouts now gathers its siblings with return_exceptions=True and re-raises the first failure only after every sibling has finished. This matters because _rollout_loop calls release_groups as soon as the group raises (controller.py:1026-1034).

Example. A group of 4 where rollout 0's run raises at t=1s and rollouts 1-3 run until t=30s:

main:     gather raises at t=1 -> controller release_groups([g]) at t=1
          rollouts 1-3 keep generating until t=30 -> they pin g's salt and hold KV again, and nothing releases it
          every trace's router pin stays until the max_sessions LRU (262,144) evicts it
this PR:  gather waits until t=30 -> rollouts 1-3 each release their trace sessions
          -> then it raises -> release_groups([g]) runs with no sibling still in flight

Must fix

  1. PR description, Solution item 1 ("GenerationServer now routes each call by Verifiers' X-Session-ID"): main already does this (generation_server.py:171-213 passes routing_session_id=session_id), and this PR doesn't touch generation_server.py. Rephrase it as background ("GenerationServer already routes by...") so a reviewer doesn't go looking for a server change.

Should consider

  • torchtitan/rl/examples/verifiers/rollouter.py:329 -- for non-trainable traces, the generation server's request_counts and generation_metadata entries are never popped. That is the same kind of leak on the server side; it was already there on main (see inline).
  • tests/rl/unit_tests/cpu/test_verifiers.py:891 -- third copy of the fake trace and fake rollouter; a shared helper would make each future change one edit (see inline).
Details
  • Maintainer comments. Both of @felipemello1's comments got replies. Per-rollout failure handling (instead of failing the whole group) is deferred to PR 14, with the weighting TODO added there. The question about stopping mid-rollout is answered by PRs 10, 11 and 13. Nothing there needs to change in this PR.
  • Parity with the native path. Native RolloutWorker releases even when the rollout raised, because rollout/rollouter.py:478-497 catches the exception and then releases. Verifiers can't do that: when run raises there are no trace ids to release. The PR's table documents this, and the TODO at line 326 names the fix (record session ids in the generation server).
  • Cancellation. With return_exceptions=True, cancelling the outer task still cancels the children and raises CancelledError. A child's own CancelledError comes back as a result and is re-raised by the BaseException check. Shutdown behavior is unchanged.
  • Tests. test_a_failing_rollout_fails_its_group_only_after_every_sibling_finished guards an invariant that would otherwise break silently: without return_exceptions it fails. The release test checks that every trace is released, not only the trainable one, and that a failed release doesn't stop the others.

Not verified: that Verifiers leaves a retried attempt out of episode.traces (comment at line 326).

Comment on lines +891 to +912
span = SimpleNamespace(duration=0.0)
traces = [
SimpleNamespace(
id=trace_id,
task=SimpleNamespace(key="task"),
agent=SimpleNamespace(trainable=trainable),
ok=True,
is_timeout=False,
is_truncated=False,
stop_condition=None,
errors=[],
timing=SimpleNamespace(
setup=span,
agent=SimpleNamespace(duration=0.0, model=span, harness=span),
scoring=span,
),
calls=[],
nodes=[],
branches=[],
)
for trace_id, trainable in [("opponent", False), ("policy", True)]
]

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.

Should consider: this is now the third copy of the same ~20-line fake trace and fake rollouter setup. The other two are test_rollout_whose_first_prompt_does_not_fit_is_flagged (~line 750) and test_timed_out_rollout_is_logged_and_keeps_no_reward (~line 830). PRs later in the stack edit these fakes again, for example when the next Verifiers field gets read. A module-level _fake_trace(trace_id="t", *, trainable=True, **overrides) and _fake_rollouter(traces) would make each change one edit and shorten this test to its assertions. Optional; it is test-only.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reply from claude: Agreed. PRs 7, 10 and 13 edit these three fakes, so a shared helper here would conflict in each of them. Added a TODO that names the three tests (524e6b4).

# server could record each rollout's session ids and release those too.
for trace in verifiers_episode.traces:
try:
await release_session_fn(routing_session_id=trace.id)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

why await sequentially instead of gather? But most importantly: why not call this as soon as a rollout is finished, instead of the entire group?

Say i am doing 128 rollouts per group. It makes no sense that i have to wait for ALL 128 to finish before i release the other 127 that finished earlier.

the only issue with this is that all 128 share the prompt, for example, and I don't know if in the current state, when releasing the cache of the 1st, we would damage the cache of the other 127.

If thats the case, we should first see if we can easily fix this first.

@felipemello1 felipemello1 Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reply from claude:

  • It's already per rollout. This loop is in _run_single_rollout and runs right after that rollout's run returns. It goes over the traces of one episode (the policy's, plus e.g. a judge's), not over the group. With 128 rollouts, each one releases as soon as it finishes.
  • The shared prompt is safe. Siblings share the same physical KV blocks, and vLLM counts the users of each block: a sibling's prefix-cache hit adds 1 (touch), and a finished request or a released hold subtracts 1 (free_blocks). Releasing this session subtracts only its own 1, so a block stays while any sibling still runs or holds it. At 0 it goes to the end of vLLM's free queue and is still a prefix-cache hit until the memory is needed. The group's cache salt is released only at group end.
  • Concurrent now: switched to gather in 5084cee. Each release still catches and logs its own error, so one failure doesn't stop the others. PR 10 replaces this loop with one batched call (release_rollout).

Comment on lines +326 to +328
# TODO: an attempt Verifiers retried has no trace here, so its held KV waits for the
# group's release and its routing pin for the router's `max_sessions` LRU. The generation
# server could record each rollout's session ids and release those too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

how difficult is this todo? can we just fix this in this pr?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reply from claude: Keeping the TODO for now. The fix: tag each request with its rollout id (like the group id), have the generation server record every X-Session-ID per rollout, and release them all when run returns or raises. That's ~25 lines here plus tests, but it conflicts with PRs 4, 7 and 10. Today it buys little: Verifiers retries are opt-in (RetryConfig.max_retries defaults to 0) and no recipe turns them on. From PR 10 on, a Verifiers rollout holds one generator lease with no router session, and release_groups drops the group's leftover server metadata and held KV.

@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude review again

@claude

claude Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 41s —— View job


Verdict: Approve: the earlier must-fix and both should-consider items are resolved at 524e6b4, and the new commits add no new problems. 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 -- the earlier must-fix and both should-consider items are resolved at HEAD (524e6b4), and the two new commits add no new problems.

Since the last review (5eea7f0, 524e6b4)

  • Must fix (description, Solution item 1): resolved. It now reads "GenerationServer already routes each call by Verifiers' X-Session-ID", so it is background, not a claimed change.
  • Should consider (server metadata for non-trainable traces): resolved. rollouter.py:330-333 now pops every trace's metadata, which clears both request_counts and generation_metadata (generation_server.py:147-148). The trainable trace's metadata is then looked up from that dict (rollouter.py:349). pop_generation_metadata returns None for an id the server never saw, so a judge that doesn't route through this server is fine. The test now checks that both traces were popped.
  • Should consider (duplicated test fakes): answered. The author added a TODO that names the three tests (test_verifiers.py:889-891). The reason given is that PRs 7, 10 and 13 edit those fakes. That's reasonable for a test-only cleanup.

Maintainer comments. @felipemello1 asked about sequential vs. gather, per-rollout vs. per-group release, and whether the retried-attempt TODO can be fixed now. The replies answer all three, and the code agrees. The release loop is in _run_single_rollout, so each rollout releases its sessions as soon as its own run returns, not after the whole group. The retry TODO stays because Verifiers retries are opt-in (max_retries defaults to 0) and the fix would conflict with PRs 4, 7 and 10. The reply about vLLM ref_cnt (a release drops only this session's reference on blocks shared with siblings) is unverified here, but the PR doesn't depend on it: native rollouts already release a session after its last turn while siblings still share the prompt.

Not verified: that Verifiers leaves a retried attempt out of episode.traces.

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

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude review again

@claude

claude Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 41s —— View job


Verdict: Approve: the new commit 5084cee releases a rollout's trace sessions concurrently and adds no new problems, and nothing from the earlier reviews is 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 -- the one new commit (5084cee) does what @felipemello1 asked and adds no new problems. Nothing from the earlier reviews is still open.

Since the last review (5084cee)

  • Release runs concurrently: resolved. rollouter.py:335-342 wraps each release in its own try/except and gathers them, so a failed release is still logged and doesn't stop the others. This is still per rollout, not per group: the loop is in _run_single_rollout, right after that rollout's run returns.
  • Test still deterministic. test_rollout_releases_every_trace_session_even_if_one_release_fails checks the order of release_session_fn calls. gather starts its tasks in argument order, and AsyncMock records a call when its task first runs, so the order stays opponent, policy. The side_effect list still gives the error to the first call.
  • Cancellation unchanged. The inner gather has no return_exceptions, but release catches only Exception. A CancelledError still propagates, as it did with the sequential loop.

Maintainer comments. All of @felipemello1's comments have answers at HEAD:

  • Sequential vs. gather: fixed in code.
  • Per-rollout vs. per-group release: it was already per rollout.
  • Retried-attempt TODO: kept, with a reason. Retries are opt-in, and the fix would conflict with PRs 4, 7 and 10.
  • Failing one rollout instead of the group: deferred to PR 14.

Not verified: I couldn't run the unit tests in this job; CI covers them.

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

unblock; I didn't review carefully

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