Repository navigation
[rl] Look-aside generator routing: the router reserves, the caller generates - #5990
Conversation
1ae3ade to
e3de0f9
Compare
felipemello1
left a comment
There was a problem hiding this comment.
thanks! I left a few comments.
Its neat that you only had to make changes to the router while keeping the generator/workers the same.
|
@claude review |
There was a problem hiding this comment.
Verdict: Approve -- please address the items below before merging. The look-aside design is right: the router only needs (request_id, session), and generator selection moves over unchanged. What's left is three small edits that @felipemello1 asked for and that aren't in HEAD (e3de0f9) yet.
What this PR does. InterGeneratorRouter.generate no longer proxies prompts and completions. reserve(request_id, routing_session_id=...) picks a generator, adds one unit of load and returns its rank-0 handle. The caller calls generate on that handle directly, then calls release(request_id) in a finally. This mirrors IntraGeneratorRouter's existing reserve/release. RoutingContext.estimated_cost is deleted because it was always 1 and had no other reader. Net -187 LoC.
Example.
before: rollout worker --prompt(16K ids)--> router --prompt--> gen1
rollout worker <--Completion------- router <--Completion-- gen1
after: rollout worker --reserve("step=3/group=0/sample=0/turn=0", "s0")--> router (gen1.load += 1)
rollout worker <-- gen1.rank0 handle ------------------------------ router
rollout worker --prompt / <--Completion--> gen1
rollout worker --release(request_id)--> router (gen1.load -= 1, also on error)
Must fix
torchtitan/rl/routing/inter_generator.py:144-- renamerelease(felipemello1), for example torelease_reservation, and renameIntraGeneratorRouter.releaseto match.torchtitan/rl/routing/inter_generator.py:140-- add the TODO felipemello1 asked for: load is one unit per call, and it ignores request size and the KV that sessions hold between turns.torchtitan/rl/routing/inter_generator.py:142-- add the 1-2 line comment felipemello1 asked for on returning the handle vs. an index.
Details
felipemello1's controller.py:603 question ("if return await generator.generate.call_one is called, nothing calls release"). This one is mistaken. A finally block runs when the try body returns, raises, or is cancelled. So release is awaited after every generate.call_one returns, before generate hands the completion back. The new test_generate_fn_forwards_the_session_and_releases_on_failure covers the raise path. The real gaps are a cancelled caller that releases early and a dead caller that never releases, and the TODO at controller.py:588 already records both.
Ordering checks.
- Both release paths are ordered correctly.
releaseis awaited insidegenerate, and the rollouter callsrelease_sessiononly after the session's lastgeneratereturns. So a session's load is always released before its pin is dropped. - A repin while a call is in flight still decrements the right generator, because
_reservationsmaps each request to the handle it reserved. - The
request_iduniqueness assert is not a new risk.IntraGeneratorRouter.reservealready asserts the same thing on the generator side.
hot_swap=False. A call reserved just before a drain can now reach the generator after the drain starts, and then it waits for the load. As the description says, this costs latency only. Pinned-but-draining sessions still stay on their generator, as before.
Not verified: the GPU smoke-test numbers in the description, and that Monarch actor-mesh handles returned from the router endpoint stay cheap to use from rollout-worker processes at larger scale.
A maintainer still needs to review.
e3de0f9 to
196428a
Compare
…nerates InterGeneratorRouter.generate proxied every generate call: the prompt and the completion were unpickled and repickled in the singleton router process and crossed the network twice, although routing only needs the request id and the routing session. Pickling plus unpickling a 16K-token prompt alone takes ~320 us of router CPU, before the completion's token ids and logprobs. The router now only routes, mirroring IntraGeneratorRouter's reserve/release: reserve(request_id, routing_session_id) picks a generator (same strategy and draining rules as before), adds one unit of load to it, and returns its rank-0 handle. The controller's GenerateFn sends the generate call to that handle and then awaits release(request_id), so load still counts in-flight generate calls. Trade-offs: - A turn now makes two small router round trips (reserve, release; ~0.4-0.6 ms each on one host) around the direct generate call, instead of routing the payload through the router. The returned handle is ~2.8 KB and takes ~46 us to pickle on the router. (Measured with Monarch 0.6.0 CPU actors on one host.) - release is awaited rather than broadcast, because Monarch's broadcast drops endpoint errors. - The router no longer sends both the generate call and load_model_state_dict to a generator, so they are no longer ordered: with hot_swap=False, a call reserved just before a load can reach the generator after the drain starts and wait for the load. Latency only. RoutingContext.estimated_cost loses its only writer (the proxy always passed 1) and its only reader, and is removed. Two sticky-session tests now set repin_load_ratio=None: with default repinning they passed even with eviction or session release broken.
… load-unit and handle trade-offs - Rename `release` to `release_reservation` on `InterGeneratorRouter` and its Monarch actor, so it isn't confused with `release_session` and `release_groups`, which free KV on a generator. - TODO where `reserve` adds a load unit: one unit per call ignores request size, KV held between turns and generated tokens. - Comment on returning the generator handle rather than an index.
The rollout worker no longer waits a router round trip after each generate call. A release that raises now fails the router actor (a supervision error) instead of being returned to the caller; for `@concurrent_endpoint` this needs meta-pytorch/monarch#5283.
ca15f66 to
49b5321
Compare
Reserve prompt tokens + max_tokens per call instead of one unit, and keep a session's last estimate on its generator between turns, so routing sees the KV that idle sessions hold. Follow-up to the TODO in pytorch#5990; part of pytorch#3755.

Summary (human-written)
It is more performant to skip the extra hop through router.
Why
InterGeneratorRouter.generateproxies every generate call through the singleton router actor. Each prompt and eachCompletion(token ids + logprobs) is unpickled and repickled in the router process and crosses the network twice. Routing only needs the request id and the routing session. Pickling plus unpickling a 16K-token prompt alone costs ~320 us of router CPU per call, before the completion.Example.
What changed
routing/inter_generator.py:generate/_route/_reserve/_releasereplaced byreserve(request_id, *, routing_session_id) -> rank-0 handleandrelease_reservation(request_id), likeIntraGeneratorRouter'sreserve/release(renamed so it isn't confused withrelease_sessionandrelease_groups, which free KV on a generator). Generator selection (strategy, draining, sticky pinning) is moved unchanged. The router keeps therequest_id -> generatormap. TODOs record that one load unit per call ignores request size and KV held between turns, and that returning an index instead of the handle is cheaper if the router becomes a bottleneck.actors/monarch/router.py: thegenerateendpoint is replaced byreserve/release_reservationendpoints.controller.py::_make_generate_fn: reserve, callgenerateon the returned handle, thenawait release_reservationin afinally.routing/types.py:RoutingContext.estimated_costremoved. Its only writer (the proxy, always 1) and only reader are gone.rl/README.md,actors/monarch/README.md, therollouter.pyclose docstring, the terminal_bench diagram.test_inter_generator_router.pyported fromrouter._route("generate", ...)toreserve/release_reservation. The tests are now synchronous; net -250 lines.Trade-offs and alternatives
load_model_state_dictno longer come from the same sender, so they are no longer ordered. Withhot_swap=False, a call reserved just before a load can reach the generator after the drain starts and wait for the load. This costs latency only; before, the router's own send order put it ahead of the load.release_reservationis an awaitedcall_one, notbroadcast: abroadcastto a@concurrent_endpointdrops its exceptions (@concurrent_endpoint silently swallows exceptions from .broadcast() calls meta-pytorch/monarch#5277), which would hide a broken release.router.release_reservationitself. Generators would need a router handle (circular at spawn) and would be coupled to routing._make_generate_fn): a cancelled caller releases early, and a caller that dies never releases. Nothing in-tree cancels aGenerateFncall, and a dead rollout worker fails the run (Monarch root-client supervision exits the controller).Numerics / performance
This is not a computation change. Least-loaded/sticky routing was already timing-dependent, so runs are not bitwise-comparable. GPU smoke test (Qwen3-0.6B, FSDP2 trainer + 3 TP2 generators, 8 GPUs, temperature 2 for long rollouts, 10 steps),
main(98a8ce0) vs this branch:hot_swap=False(drain)hot_swap=TrueAt this scale the router is not a bottleneck, so this run shows no regression, not the win. No large-scale router-CPU benchmark yet.
Testing
pytest tests/rl/unit_tests/cpu/{test_inter_generator_router,test_async_controller,test_shutdown,test_intra_generator_router,test_trainer_loop,test_generator,test_engine_loop}.py: all pass.test_generate_fn_forwards_the_session_and_releases_on_failure: catches a failed generate call that never releases its load unit (silently skewing routing), a release that runs before the generate call finishes, androuting_session_idnot reaching both routing layers.test_sticky_session_respects_max_sessionsand the mergedtest_release_session_unpins_it_and_frees_its_kv_on_its_generatornow setrepin_load_ratio=None. Before, default repinning moved the session anyway, so they passed with eviction/release disabled; now they fail (checked by mutation).