Repository navigation
Conversation
With hot_swap=False the router took every generator out of rotation at once while it drained, so the first generator back took every waiting turn and the sticky strategy re-pinned those sessions there for good. The generator's engine loop already decides between admitting requests and applying a pull, and rank 0 already tracks every outstanding generation across DP ranks. With VLLMGenerator.Config.drain_on_weight_sync, a pending pull keeps stepping the running requests, holds new ones, and applies once nothing is running; the held requests then run under the new weights. The router no longer changes its candidate set: InterGeneratorRouter.Config .hot_swap, the SYNCING state and the idle/serving events are removed, and a weight sync is prefetch then pull.
Contributor
|
reply from claude: this is the same fix as #5503 (in the HTTP generation stack). Suggest closing this in favor of #5503:
|
pzhan9
added a commit
to pzhan9/torchtitan
that referenced
this pull request
Nov 6, 2026
Resolves the TODO in `InterGeneratorRouter._route` (pytorch#5253). With hot_swap=False, the generator now drains itself (previous commit), so the router no longer clears a `serving` event, waits for the generator to go idle, or makes `_route` wait on the pinned generator's and the global `serving` events. It keeps a plain `draining` flag, set around each load call, that only steers routing: - a turn pinned to a draining generator still goes to it, bypassing the strategy, and waits there: no re-pin while draining, as before. Unlike before, such a turn also skips the repin_load_ratio check after the drain, since it was already sent to the generator. - every other call goes to a generator that isn't draining. A draining generator holds new work back until its load is applied, which its reserved load doesn't reflect. When all generators drain, calls spread over them by load, instead of all waiting for, then piling onto, the first one to finish its load. hot_swap=True is unchanged: the flag is never set. Observability: the router's `router_drain_wait` trace span is gone. In drain mode, the drain wait now falls inside the generator's `load_model_state_dict` span, and held-back requests' time to first token includes it.
tianyu-l
pushed a commit
that referenced
this pull request
Nov 7, 2026
… generators (#5905) ## Summary (human-written) Address this TODO: https://github.com/pytorch/torchtitan/blob/c2579f7da7539986b2d5d02593957656890ebaf8/torchtitan/rl/routing/inter_generator.py#L171 Briefly, we move the draining logic to generator, so the router logic stays simpler. But router still needs to maintain the generator draining state (a bool flag), so it can use that flag to decide routing: 1. A request pinned to a draining generator goes to it and waits there. * this is the current behavior. It is avoid pileup on the first undrained generator (see #5218). we just maintain it. 2. every other request goes to a non-draining generator, or, when all drain, to the least-loaded one. ## Why Resolves the TODO in `InterGeneratorRouter._route` (#5253). With `hot_swap=False`, the router drains each generator itself (clears a `serving` event, waits for router-side `idle`, then calls `load_model_state_dict`), and every `_route` call waits on the pinned generator's `serving` event and a global `_serving` event. All generators drain in the same pull, so new sessions block at the router until the first generator finishes its load, then see it as the only candidate and are pinned there by sticky routing. **Example** (real router, 4 generators, `hot_swap=False`, 20 new sessions arriving mid-drain): | Situation | `main`: where the 20 new sessions go | This PR | | --- | --- | --- | | All 4 draining, gen0 comes back first | gen0: 20 | spread by load, each waits in its generator's queue | | gen3 already back, gen0-2 draining | gen3: 20 | gen3: 20 (unchanged) | ``` main: route() --await serving--> choose(serving generators) --> generate this PR: route() --> choose(...) --> generate --(generator holds it back until its load is applied) ``` ## What changed - **Generator (logic):** `Generator.load_model_state_dict(version, *, drain)`. With `drain=True`, rank 0 of `EngineLoop._decide_next_action` stops admitting requests, keeps stepping the admitted ones until none is left, applies the load, then admits the held-back requests under the new weights. Admitted = outstanding generations minus `pending_engine_requests` (requests register with the dispatcher when taken off the queue, so counting held-back ones would deadlock). While a load is pending, the loop decides right away instead of waiting on the queue. `drain=False` is unchanged. - **Router (logic):** the `serving`/`idle` events, `_serving`, `_candidates`, `_set_serving` and `_refresh_serving_status` become one `_GeneratorHandle.draining: bool`, set around the load call in drain mode. `_route` never waits: - a turn pinned to a draining generator goes to it, bypassing the strategy (no re-pin while draining, as on `main`); - every other call goes to a generator that isn't draining, or to any of them when all drain. - **Mechanical, skim:** Monarch endpoint passthrough of `drain`; `RequestDispatcher.rank0_has_outstanding_generations()` -> `rank0_num_outstanding_generations()`; `drain=False` added to existing engine-loop tests; router tests rewritten for the removed events. - `strategies.py` and `hot_swap` (still on `InterGeneratorRouter.Config`) are unchanged. Two commits: the first adds the generator-side drain (the router still drains too, so no behavior change); the second removes the router-side drain. ## Alternatives considered - Let pinned turns spill over to other generators while theirs drains: rejected, it re-prefills sessions for no reason; this keeps `main`'s no-re-pin behavior. - Treat draining as extra load in `reserved_load`: rejected, needs a magic number and trips `repin_load_ratio`, re-pinning every session on the draining generator. - Move `hot_swap` to `Generator.Config`: rejected, the router owns the drain-or-not policy and the generator only implements it. ## User-facing changes Only with `hot_swap=False`; `hot_swap=True` (the default, and every recipe) is unchanged. | | Before | After | | --- | --- | --- | | Pinned turn, its generator draining | waits at the router; `repin_load_ratio` check when the drain ends | waits in the generator's queue; no `repin_load_ratio` check | | New session, some generators serving | a serving generator | same | | New session, all generators draining | waits, then all go to the first one back | least-loaded over all; waits in that generator's queue | | `router_drain_wait` trace span | present | removed; the drain wait is inside the generator's `load_model_state_dict` span | | `generator/time_to_first_token_ms`, `generator/inflight_requests_at_completion` | exclude held-back requests | include them (they wait in the generator now) | API: `Generator.load_model_state_dict(version)` -> `load_model_state_dict(version, *, drain)` (required). ## Numerics / performance No computation change. `hot_swap=True`: routing and load timing are logically identical (the flag is never set; a `drain=False` load is applied at the next decision as before). `hot_swap=False`: which generator a session lands on can differ, so runs are not bitwise comparable to `main`. ## Testing - `tests/rl/unit_tests/cpu/test_engine_loop.py`, `test_inter_generator_router.py`, `test_generator.py`, `test_intra_generator_router.py` pass on each commit. The full `tests/rl/unit_tests/cpu` suite matches `main` file by file; the remaining failures (`test_dapo_math`, `test_model_defaults`, `test_policy_loss_configs`, `test_search_r1`, `test_rdma_microbatch`) also fail on `main` in this environment (stale `renderers`/package versions). - New and changed tests (each verified by mutating the code; failures time out rather than hang): | Test | Regression it catches | | --- | --- | | `test_draining_load_waits_for_admitted_requests` | load applied while an admitted request runs; loop hanging once the last admitted request finishes and no message arrives | | `test_hot_swap_load_does_not_wait_for_admitted_requests` | `drain=False` loads starting to wait | | `test_draining_load_applies_after_admitted_requests_and_before_held_back_ones` (real `EngineLoop` thread) | counting held-back requests (deadlock); admitting a held-back request before the load | | `test_sticky_session_stays_on_its_draining_generator` | re-pin while draining, even when the generator looks >1.5x loaded | | `test_new_sessions_spread_over_generators_that_all_drain` | missing all-drain fallback | | `test_drain_marks_generator_draining_for_its_load` | router waiting on in-flight routes; pulls not concurrent; unpinned calls not avoiding a draining generator | | `test_drain_clears_draining_on_pull_failure`, `test_round_robin_skips_draining_generators` | flag left set after a failed load; exclusion under round robin | Deleted: `test_drain_excludes_syncing_generator_from_routes` and `test_drain_pulls_idle_generators_while_busy_generator_drains` (merged into `test_drain_marks_generator_draining_for_its_load`); `test_single_generator_blocks_routes_while_draining` (the router no longer blocks; holding back is covered by the engine-loop tests). - A scratch harness (not committed) wiring the real router to real `EngineLoop`s with fake engines, random multi-turn sticky sessions and 15 back-to-back pulls passed with `hot_swap` False/True; in drain mode 20-28 of 45 loads per run arrived with requests running, and none was applied with an admitted request. - GPU smoke on 8 GPUs: `torchtitan_recipes.tests.rl.alphabet_sort.rl_grpo_fsdp2_gen_tp2_no_compile` (3 generators x TP2 + FSDP2 trainer), 6-8 steps, with `hot_swap` False and True on this branch and False on `main`: all complete. Caveat: the controller pulls when generators are idle in these recipes, so only one load across all runs had a `generate` in flight (it finished before the weight copy); drain under load is mainly covered by the CPU tests above. ## Known issues / follow-ups - A turn pinned to a draining generator skips `StickySessionRoutingStrategy`'s LRU refresh for that turn (negligible at `max_sessions=262144`). - No in-repo recipe sets `hot_swap=False`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem: with
hot_swap=False,InterGeneratorRoutermarks every generator SYNCING at once while it drains. Every turn sent during the sync goes to the first generator back, and the sticky strategy re-pins those sessions there for good. #5218 fixes this inside the router (a pinned turn waits for its own generator). This PR is a simpler alternative: take draining out of the router.Solution: drain inside the generator. The engine loop already chooses between admitting requests and applying a pull, and rank 0 already tracks every outstanding generation across DP ranks. With the new
VLLMGenerator.Config.drain_on_weight_sync, a pending pull:The router never changes its candidate set anymore, so a session stays on its generator and the re-pinning problem can't happen. Removed from the router:
Config.hot_swap,_GeneratorState, the per-generatoridleevent,_serving,_candidates(),_set_state(). A weight sync is now prefetch, then pull.Behavior changes
drain_on_weight_sync=False, i.e. the oldhot_swap=True): unchanged. With hot swap on, the removed router code never ran.InterGeneratorRouter.Config.hot_swap=FalsebecomesVLLMGenerator.Config.drain_on_weight_sync=True. No recipe setshot_swap=Falsetoday.router_drain_waitspan is gone. The generator'spull_model_state_dictspan now covers drain plus apply, andpull_model_state_dict_copyis the apply alone.Test plan
pytest tests/rl/unit_tests/cpu: 511 passed, 8 skipped.test_drain_holds_requests_until_running_ones_finish: decision-level test. The pull stays pending while a request runs, then is applied, then the held request is admitted.test_drain_on_weight_sync_finishes_running_requests_before_the_pull: engine-thread test, run with drain on and off. With drain on, the running request finishes under the old version and the held one runs under the new version. With drain off, the pull lands mid-request.test_sticky_session_stays_on_its_generator_during_a_pullandtest_pull_failure_propagates.pre-commit runon the changed files, withpyreflyskipped (torchtitan/rland tests are in pyrefly'sproject-excludes).