Skip to content

[rl] Generator drains itself before a hot_swap=False weight load (8/14) - #5503

Closed
felipemello1 wants to merge 53 commits into
felipemello1/rollout-error-systemfrom
felipemello1/http/1-generator-drain
Closed

felipemello1 wants to merge 53 commits into
felipemello1/rollout-error-systemfrom
felipemello1/http/1-generator-drain

Conversation

@felipemello1

@felipemello1 felipemello1 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

<WONT REVIEW -- PR IS NOT A PRIORITY>

TLDR: With hot_swap=False, the generator now drains itself before a weight load, instead of the router holding back its calls. A caller can then send generate straight to a generator (PR 10) without losing the drain.

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. With hot_swap=False, InterGeneratorRouter.pull_model_state_dict stops routing to a generator, waits until no call it routed there is in flight, then loads.
  2. That works only because the router wraps every generate call and sees it start and end.
  3. A caller that sends generate straight to the generator (over HTTP, PR 10) bypasses the router, so the router can't drain.

Solution

  1. Generator.load_model_state_dict(version, drain=True) puts a draining load on the engine loop's queue.
  2. The engine loop stops admitting new requests, keeps stepping the admitted ones, and loads once none is left.
  3. Requests that arrive meanwhile are admitted right after the load, so they run on the new weights.
  4. The router passes drain=not hot_swap and drops its per-generator serving/idle state. drain is a required keyword, so a caller can't get a hot-swap load by leaving it out.
  5. Unchanged: hot_swap=True loads, routing, and when a load is requested.

Results
Numerics: bitwise vs main. With hot_swap=True (the default, and every recipe) nothing changes; with hot_swap=False a request still waits for the load and runs on the new weights, only where it waits moves.

hot_swap=False, Qwen3-4B-Base native DAPO-Math (8 x 16, 8k tokens), 8 x H100, 5 steps, measured with PR 10 on top
                                   main           this PR
step period, mean / median (s)     57.0 / 53.9    57.4 / 55.3
pull span seen by the caller (s)   46.0 median    45.7 median   (drain + load; the drain moved into the generator)

NOTE:

  • No recipe sets hot_swap=False.
  • With hot_swap=False, time_to_first_token_ms now includes the drain wait.
  • A client that leaves while its request is parked still gets it generated after the load (PR 11 fixes this).
  • The router's router_drain_wait span is gone; the generator's load_model_state_dict span covers drain + load.
  • StickySessionRoutingStrategy no longer checks that a pinned candidate is still in candidates: both routers now pass a fixed list.
  • All generators still drain in one gather, so a parked request waits about as long as one held in the router did. A new session can now land on a generator still draining while another has finished.

Test plan

pytest tests/rl/unit_tests/cpu/test_engine_loop.py tests/rl/unit_tests/cpu/test_inter_generator_router.py
  • test_session_releases_during_a_drain_ride_the_step_decision
  • test_draining_load_at_an_idle_engine_applies_at_once
  • test_draining_load_finishes_admitted_requests_then_admits_held_ones (real engine-loop thread, fake engine)
  • test_close_during_a_drain_fails_the_load_and_every_request
  • test_pull_model_state_dict_pulls_every_generator runs with and without hot swap.
  • RL CPU suite at this position (local, 11-05): 614 passed, 4 skipped (Verifiers 0.4.0).

@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 force-pushed the felipemello1/http/1-generator-drain branch from fc8a101 to ba0dc0b Compare October 18, 2026 23:31
@felipemello1
felipemello1 changed the base branch from main to felipemello1/shutdown/1-drain-in-flight October 18, 2026 23:31
@felipemello1
felipemello1 added this pull request to stack #5513 October 18, 2026 23:32
@felipemello1
felipemello1 force-pushed the felipemello1/http/1-generator-drain branch from ba0dc0b to 3a10979 Compare October 19, 2026 00:37
@felipemello1
felipemello1 force-pushed the felipemello1/shutdown/1-drain-in-flight branch from ba8820a to 9bf2abd Compare October 19, 2026 01:16
@felipemello1
felipemello1 force-pushed the felipemello1/http/1-generator-drain branch from 3a10979 to 94f44ea Compare October 19, 2026 01:16
@felipemello1

Copy link
Copy Markdown
Contributor Author

<WONT REVIEW -- PR IS NOT A PRIORITY>

@felipemello1 felipemello1 changed the title [rl] Generator holds requests while it drains for a weight load [rl] Generator drains itself before a hot_swap=False weight load (7/14) Oct 19, 2026
@felipemello1
felipemello1 force-pushed the felipemello1/http/1-generator-drain branch from 94f44ea to 052cb87 Compare October 22, 2026 23:31
@felipemello1

Copy link
Copy Markdown
Contributor Author

reply from claude: this is the same fix as #5253: the generator drains itself before a hot_swap=False weight load. Suggest landing this one and closing #5253:

@felipemello1
felipemello1 force-pushed the felipemello1/shutdown/1-drain-in-flight branch from 36b807f to 1bd02b5 Compare October 23, 2026 07:34
@felipemello1
felipemello1 force-pushed the felipemello1/http/1-generator-drain branch from 052cb87 to ca5281f Compare October 23, 2026 07:34
@felipemello1
felipemello1 removed this pull request from stack #5513 October 27, 2026 14:27
@felipemello1
felipemello1 changed the base branch from felipemello1/shutdown/1-drain-in-flight to felipemello1/rollout-error-system October 27, 2026 14:30
@felipemello1
felipemello1 force-pushed the felipemello1/http/1-generator-drain branch from ca5281f to 8da6b9d Compare October 27, 2026 14:30
@felipemello1 felipemello1 changed the title [rl] Generator drains itself before a hot_swap=False weight load (7/14) [rl] Generator drains itself before a hot_swap=False weight load (8/14) Oct 27, 2026
@felipemello1
felipemello1 added this pull request to stack #5639 October 27, 2026 14:33
Felipe Mello and others added 10 commits November 4, 2026 15:30
…YSTEM

A HarnessError counts as the policy's only once the policy has sampled a turn
(trace.num_turns > 0). Verifiers also raises it when the harness fails to start,
e.g. "opening harness session timed out" when the sandbox tunnel isn't ready in
30 s. The policy never ran, so the rollout is now ERROR_SYSTEM: left out of the
reward, the advantage and training, while its siblings train.

As ERROR it got reward 0 with no completion tokens, so TrainingSampleBuilder
dropped its whole group: a 53-step SWE run lost 293 groups of 16 this way
(training_sample_builder/num_groups_dropped_untrainable).

A HarnessError after a turn (the agent's own exit) and a ProviderError 4xx
before one (first prompt past the context) stay ERROR.
@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 1m 1s —— View job


Verdict: Approve -- one thing is still open: the PR description has no "Summary (human-written)" section. A maintainer still needs to review. See 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 item below before merging. The new commits fix four of the five items from the last review. The new code has no problems. The one item left is in the PR description, not the code. A maintainer still needs to review, because this touches the RL generator core.

What changed since the last review. drain is now a required keyword in all three load_model_state_dict signatures and on ModelStateDictLoadMessage, and every test passes it explicitly. The two outdated docstrings and the dead sticky-strategy check in strategies.py are fixed or removed. The duplicate decision-level drain test is gone. The three merges from the base branch don't change this PR's diff.

Must fix

  1. PR description: the "Summary (human-written)" section is still missing. The author has to write it; agents don't fill it in. If the TLDR is meant as the summary, put it under that heading.

Earlier items, resolved

  • strategies.py:118-121 LeastLoadedRoutingStrategy.choose docstring: now says the tie-break is about which candidates tie, not about candidates leaving the set. Resolved.
  • strategies.py:186-205 sticky "pinned candidate gone" check: deleted, along with its docstring bullet and comments. pinned_candidate is still used by release_session (inter_generator.py:250), so keeping that method is right. Resolved.
  • drain default: required in generator.py:454, engine_loop.py:208, actors/monarch/generator.py:86 and on the message (engine_loop.py:600). The only production caller (inter_generator.py:198) passes drain=not hot_swap. No other caller in the repo leaves it out. Resolved.
  • Duplicate drain test: test_draining_load_waits_for_admitted_work_and_holds_new_requests is gone. The threaded test_draining_load_finishes_admitted_requests_then_admits_held_ones covers the same sequence and also checks the policy version at admission. Resolved.
Details

I re-checked these at HEAD (9dd0742):

  • No leftover references to serving, idle, _set_serving, _candidates or router_drain_wait.
  • asyncio and field are still used in inter_generator.py.
  • The test_engine_loop.py:153 helper _load(..., drain=False) keeps a default. That is fine for a test-only helper, and none of the new tests rely on it by accident: the drain tests pass drain=True.

The engine-loop decision logic is the same as at the last review, and the analysis there still applies.

Not verified: I didn't run the unit tests here. The GPU timings come from the PR description.

Felipe Mello added 19 commits November 4, 2026 21:29
…/genserver-request-path

# Conflicts:
#	torchtitan/rl/examples/verifiers/generation_server.py
@felipemello1
felipemello1 removed this pull request from stack #5639 November 5, 2026 07:53
@felipemello1
felipemello1 force-pushed the felipemello1/rollout-error-system branch 4 times, most recently from 0ecfb1f to 53b2361 Compare November 6, 2026 06:15
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: superseded by #5905, which drains inside the generator with the same algorithm and also keeps routing away from draining generators. This PR's 4 engine-loop tests pass on #5905's code. Closing; the branch stays because #5505 is based on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

1 participant