Repository navigation
[rl] Verifiers: release each finished rollout's routing session (2/14) - #5510
Conversation
b9a8e8a to
cf9cb8a
Compare
| # 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 |
There was a problem hiding this comment.
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
| # `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) |
There was a problem hiding this comment.
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
cf9cb8a to
12b762c
Compare
|
reply from claude:
|
12b762c to
9fa5486
Compare
|
Reply from claude: @claude review |
|
Claude finished @felipemello1's task in 2m 5s —— View job Verdict: Approve (fix one sentence in the description before merging): review |
There was a problem hiding this comment.
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
- PR description, Solution item 1 ("
GenerationServernow routes each call by Verifiers'X-Session-ID"):mainalready does this (generation_server.py:171-213passesrouting_session_id=session_id), and this PR doesn't touchgeneration_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'srequest_countsandgeneration_metadataentries 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
RolloutWorkerreleases even when the rollout raised, becauserollout/rollouter.py:478-497catches the exception and then releases. Verifiers can't do that: whenrunraises 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 raisesCancelledError. A child's ownCancelledErrorcomes back as a result and is re-raised by theBaseExceptioncheck. Shutdown behavior is unchanged. - Tests.
test_a_failing_rollout_fails_its_group_only_after_every_sibling_finishedguards an invariant that would otherwise break silently: withoutreturn_exceptionsit 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).
| 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)] | ||
| ] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
… the trainable one's
| # 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Reply from claude:
- It's already per rollout. This loop is in
_run_single_rolloutand runs right after that rollout'srunreturns. 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
gatherin 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).
| # 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. |
There was a problem hiding this comment.
how difficult is this todo? can we just fix this in this pr?
There was a problem hiding this comment.
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.
|
Reply from claude: @claude review again |
|
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 |
There was a problem hiding this comment.
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 "
GenerationServeralready 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-333now pops every trace's metadata, which clears bothrequest_countsandgeneration_metadata(generation_server.py:147-148). The trainable trace's metadata is then looked up from that dict (rollouter.py:349).pop_generation_metadatareturnsNonefor 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.
|
Reply from claude: @claude review again |
|
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 |
There was a problem hiding this comment.
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-342wraps each release in its owntry/exceptand 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'srunreturns. - Test still deterministic.
test_rollout_releases_every_trace_session_even_if_one_release_failschecks the order ofrelease_session_fncalls.gatherstarts its tasks in argument order, andAsyncMockrecords a call when its task first runs, so the order staysopponent,policy. Theside_effectlist still gives the error to the first call. - Cancellation unchanged. The inner
gatherhas noreturn_exceptions, butreleasecatches onlyException. ACancelledErrorstill 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
left a comment
There was a problem hiding this comment.
unblock; I didn't review carefully
TLDR: A Verifiers rollout now releases its routing sessions when Verifiers'
runreturns, 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.
Problem
RolloutWorkercallsrelease_session_fnafter a rollout's last turn: the router drops the session's pin and, withsession_kv_holder, the generator frees its held KV.VerifiersRollouter.run_group_rolloutsdroppedrelease_session_fn(a TODO), so a pin stayed until themax_sessionsLRU (262,144 entries) dropped it, and held KV stayed until the group ended.return_exceptions: one failing rollout raised at once, and the controller released the group while its siblings kept generating.session_kv_holder, and a stale pin is ~150 B. It matters once a recipe holds session KV.Solution
GenerationServeralready routes each call by Verifiers'X-Session-ID, which is the trace id. This PR doesn't change the server.VerifiersEnvClient.runreturns,_run_single_rolloutreleases 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.return_exceptions=Trueand raises the first failure after every sibling finished.NOTE:
runreturns 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 withoutreturn_exceptions.pytest tests/rl/unit_tests/cpu(verifiers 0.4.0, on PR 1, main d0ab95e): 587 passed, 4 skipped. The fake traces carryis_timeout, which 0.4.0 reads.