Repository navigation
[rl] Send each rollout group to the worker with the fewest rollouts in flight - #6114
felipemello1 wants to merge 1 commit into
Conversation
|
Reply from claude: @claude please review |
|
Claude finished @felipemello1's task in 1m 42s —— View job Approve -- please address the items before merging (the human-written summary is empty, and one should-consider about a fast-failing worker attracting most groups) -- review · |
There was a problem hiding this comment.
Verdict: Approve -- please address the items below before merging. The change is small and correct (+29/-5 logic) and replaces random placement with a least-loaded pick, but the human-written summary is empty.
What this PR does. Rollouter keeps _num_rollouts_in_flight, one count per entry of by_worker. run_group_rollouts sends each group to the worker with the lowest count, and the first index wins a tie. The count goes up by group_size only after call_one returns, so a send that fails to pickle never counts. It goes down in the done callback, which also runs when the caller was cancelled, because the call is shielded.
Example. 3 workers, all idle.
dispatch g0(size 4), g1(2), g2(2), g3(1)
before: each group goes to random.choice(by_worker); several can land on one worker
after: g0->w0, g1->w1, g2->w2, g3->w1 (w1 and w2 tie at 2; first wins) counts [4, 3, 2]
g0 returns -> counts [0, 3, 2] -> the next group goes to w0
Must fix
- The PR description's "Summary (human-written)" section is empty. Please fill it in (CONTRIBUTING.md, "AI-assisted contributions").
Should consider
torchtitan/rl/rollout/rollouter.py:279-- a worker whoserun_groupfails fast attracts nearly every group under least-loaded, where random placement sent it about 1/N. At minimum, write the behavior down (see the inline comment).
Details
- Necessity: placement used to ignore load, and a group holds its worker until its slowest rollout ends. That makes a least-loaded pick a reasonable fix at the right layer, the Rollouter. The evidence is a simulation, which the PR says plainly. The change is small enough that no end-to-end measurement is needed to land it.
- Atomicity: nothing is awaited between
minand+=, and all callers share one event loop. Concurrentrun_group_rolloutscalls, such as the validationgatherincontroller.py:903and the training_rollout_loop, therefore can't both read a stale minimum. - Ordering:
add_done_callbackon a pending task schedules its callback withcall_soon, so the decrement can never run before the increment. The same holds ifcall_onereturns a future that is already done. - Callsites:
setup_asyncinitializes the counts, so theobject.__new__helper intest_verifiers.py:300, which skips__init__but callssetup_async, keeps working.test_shutdown.pycallsby_worker[i].run_group.call_onedirectly and is unaffected. - Tests: the send-failure test guards the ordering invariant, which would otherwise fail silently (a leaked count would steer every later group away from that worker). It is a cheap CPU test that extends the existing fakes.
- Overlap: #6099 changes the
run_group_rolloutsarguments in this same function. Expect a rebase conflict, but no design conflict.
Not verified: the simulation numbers in the description.
| # The worker with the fewest rollouts in flight; the first one on a tie. | ||
| # TODO: lower a worker's count as each rollout ends, not when its whole group returns | ||
| # (simulated busiest worker: 2.6x -> 1.8x the mean). It needs the worker to report each end. | ||
| worker_index = self._num_rollouts_in_flight.index( | ||
| min(self._num_rollouts_in_flight) | ||
| ) |
There was a problem hiding this comment.
Least-loaded makes a worker that fails fast attract most groups. Picture a worker whose run_group raises within milliseconds, for example because its host has no Docker daemon, or a 127.0.0.1 service is missing after workers_on_controller_host=False from the base PR. Its count drops back to 0 almost at once. The healthy workers stay above 0, so this worker wins nearly every pick. _rollout_loop then logs "rollout group N failed; dropping" for nearly every group. With random placement only about 1/N of groups would fail.
The failure is loud (rollout/group_failures), so this doesn't block the PR. Still, it is worth a NOTE/TODO here and a line in the description. A cheap fix is to skip the refund in _on_run_group_call_done when call.exception() is not None, so a failing worker keeps its load. The catch is that a worker with occasional transient failures then drifts upward forever. Either way, please write the chosen behavior down.
There was a problem hiding this comment.
Reply from claude: Fixed in 6eb7975: the refund stays. A 2-line # TODO: at the pick names the fast-failing worker and the fix (keep a failed group's count) with its cost (transient failures pile up). The description's NOTE says it too.
9a478ef to
6eb7975
Compare
|
Claude finished @felipemello1's task in 36s —— View job Approve -- please address the item before merging (the fast-failing-worker TODO is resolved; the human-written summary is still empty) -- review · |
There was a problem hiding this comment.
Verdict: Approve -- please address the item below before merging. The fast-failing-worker concern is now written down in code and in the description. The only open item is the empty human-written summary.
Re-review of 6eb7975
- Resolved: the should-consider at
torchtitan/rl/rollout/rollouter.py:282. A# TODOat the pick now names the fast-failing worker, the fix (keep a failed group's count), and its cost (transient failures pile up). The description's NOTE and follow-up list say the same. - Still open: the "Summary (human-written)" section of the PR description is still just the template placeholder.
Must fix
- The PR description's "Summary (human-written)" section is empty. The author has to write it (CONTRIBUTING.md, "AI-assisted contributions").
Details
- No new problems in the new commit. The diff only differs from the last review by the added TODO.
- The earlier analysis still holds at HEAD. Nothing is awaited between
min(...)and+= group_size, so concurrentrun_group_rolloutscalls on the one event loop can't read a stale minimum. The done callback is scheduled withcall_soon, so the decrement always runs after the increment. The increment comes aftercall_one, so a send that fails to pickle never leaks a count, andtest_a_run_group_that_fails_to_send_leaves_its_worker_count_unchangedguards that.
Not verified: the simulation numbers in the description. This touches only RL rollout code, but a maintainer still needs to review.
6eb7975 to
dfc5649
Compare
|
Claude finished @felipemello1's task in 59s —— View job Approve -- please address the item before merging (rebase leaves the patch unchanged with no new problems; the human-written summary is still empty) -- review |
There was a problem hiding this comment.
Verdict: Approve -- please address the item below before merging. The rebase onto the base PR's new default (workers_on_controller_host=False) leaves the patch unchanged and adds no new problems. The only open item is still the empty human-written summary.
Re-review of dfc5649
- Unchanged: the diff against the new base matches 6eb7975. That covers the least-loaded pick, the increment after
call_one, the decrement in the done callback, both TODOs, and the three tests. - Still open: the "Summary (human-written)" section of the description is still the template placeholder.
Must fix
- The PR description's "Summary (human-written)" section is empty. The author has to write it (CONTRIBUTING.md, "AI-assisted contributions").
Details
- The new default makes the fast-failing-worker TODO (
torchtitan/rl/rollout/rollouter.py:284) more likely to apply. An env that needs a 127.0.0.1 service now runs off the controller host by default. If that service is missing,run_groupfails fast on those workers. That case is already written down in the TODO and the description's NOTE, and the failures still show up loudly inrollout/group_failures, so no new action is needed. - Tie-break and host order:
by_workeris host-major ([A0, A1, B0, B1, C0],torchtitan/rl/actors/monarch/system.py:221). Ties go to the first index, so when fewer groups are in flight than there are workers, host A's workers fill first. This only happens under light load: under normal sizing (rollouts_per_worker), there are at least as many groups in flight as workers, and the first round already puts one group on every worker. Not a finding. If per-host load ever matters (say, Docker containers per host), rotating the tie-break would be a one-line change. - The analysis from the earlier reviews still holds at HEAD. Nothing is awaited between
min(...)and+= group_size. The done callback is scheduled withcall_soon, so it always runs after the increment. A send that fails to pickle never leaks a count, andtest_a_run_group_that_fails_to_send_leaves_its_worker_count_unchangedguards that.
Not verified: the simulation numbers in the description. This touches only RL rollout code, but a maintainer still needs to review.
felipemello1
left a comment
There was a problem hiding this comment.
this looks ok and i would approve it as is, but i wonder what the lift is to actually do this by rollout
| # TODO: a worker whose `run_group` fails fast drops back to 0 and draws most groups. Keeping a | ||
| # failed group's count on its worker would stop that, but transient failures would then pile up. |
There was a problem hiding this comment.
i dont understand this todo. Please clarify.
There was a problem hiding this comment.
Reply from claude: rewritten with an example. Today a worker's count drops only when its whole group returns, so a
group of 8 with 7 rollouts done still counts 8, and new groups avoid that worker. The second TODO: a worker that fails
every group at once (e.g. its env can't start) is back at 0 right away, so it keeps drawing new groups.
| ) | ||
| self._inflight_run_group_calls.add(call) | ||
| call.add_done_callback(self._on_run_group_call_done) | ||
| call.add_done_callback( |
There was a problem hiding this comment.
lets maybe add a 1 or 2 liner comment here with a hint, e.g. "update inflight count". Maybe we need to rename _on_run_group_call_done? not sure if we should keep generic. Maybe we should, if the function is doing multiple things.
There was a problem hiding this comment.
Reply from claude: kept the name. It does three things: lowers the count, forgets the call, reads the error. The new
comment above add_done_callback says so: "When run_group ends: lower this worker's count, forget the call and read
its error."
| on_controller_host=self._config.workers_on_controller_host, | ||
| **(worker_kwargs or {}), | ||
| ) | ||
| self._num_rollouts_in_flight = [0] * len(self._workers.by_worker) |
There was a problem hiding this comment.
lets also add a one liner hint here on why we need this, e.g. "Keep count to help with load balancing"
There was a problem hiding this comment.
Reply from claude: done: "Every worker on every host, with its rollouts in flight: each new group goes to the least
loaded."
| # (simulated busiest worker: 2.6x -> 1.8x the mean). It needs the worker to report each end. | ||
| # TODO: a worker whose `run_group` fails fast drops back to 0 and draws most groups. Keeping a | ||
| # failed group's count on its worker would stop that, but transient failures would then pile up. | ||
| worker_index = self._num_rollouts_in_flight.index( |
There was a problem hiding this comment.
I need you to refresh my mind: do we have 1 rollouter in the controller, on multiple?
In other words: is this selecting across ALL workers in ALL nodes?
There was a problem hiding this comment.
Reply from claude: one. The controller builds one Rollouter (controller.py:635), and it runs in the controller's
process. Its handle lists every worker on every host (by_worker), so each pick is across all workers on all nodes.
The comment above the counts says so now.
dfc5649 to
f4e05c0
Compare
|
Reply from claude: on your review,
two readings.
I'd do the second after the Verifiers-env-in-worker stack lands, since it rewrites the Verifiers worker. The new |
|
Reply from claude: @claude please review. The code at this head answers the maintainer review (replies are in each thread). Follow-up work, not in this push:
|
|
Claude finished @felipemello1's task in 1m 29s —— View job Approve -- please address the item before merging (all maintainer comments are addressed and the move to |
There was a problem hiding this comment.
Verdict: Approve -- please address the item below before merging. All four of the maintainer's inline comments are handled at f4e05c0, and moving the pick to LeastLoadedRoutingStrategy introduces no new problems. The only open item is still the empty human-written summary.
Re-review of f4e05c0
- Changed since the last review: the hand-rolled
_num_rollouts_in_flight.index(min(...))is gone. In its place is one_RolloutWorkerLoad(RoutingCandidate)per worker, picked byLeastLoadedRoutingStrategy(torchtitan/rl/rollout/rollouter.py:305). A tie now goes to the least recently chosen worker, not the first index, which fixes the host-A-fills-first point from the last review. The PR also addsRollouter.metrics()(rollout_worker/rollouts_in_flight_max/_mean), wired into_trainer_loop(controller.py:1492). - Maintainer comments (felipemello1), all addressed in code:
- "i dont understand this todo": both TODOs now give an example (a group of 8 with 7 rollouts done still counts 8; a worker whose env can't start is back at 0 right away).
- Hint at the done callback: there is now a one-line comment above
add_done_callback, and the name stays. - Hint at the counts: added at
rollouter.py:216. - "1 rollouter or multiple?": answered. There is one, in the controller, and it picks across every worker on every host.
- The top-level question about the lift of per-rollout counting is answered in the thread and tracked as a TODO.
- Still open: the human-written summary.
Example. 3 workers, all idle:
send g0(4), g1(2), g2(2), g3(1) -> [[g0], [g1, g3], [g2]] loads [4, 3, 2]
(g3: w1 and w2 tie at 2; w1 was chosen earlier, so it wins)
g0 returns -> loads [0, 3, 2]
send g4(2), g5(1) -> g4->w0; w0 and w2 tie at 2, w2 chosen longer ago -> g5->w2
first-index tie-break would send g5 to w0; the test catches that
Must fix
- The PR description's "Summary (human-written)" section is still the template placeholder. The author has to write it (CONTRIBUTING.md, "AI-assisted contributions").
Details
- Reuse:
_RolloutWorkerLoadfollows the same pattern as_DPRankHandleand_GeneratorHandle(akw_only, slotsdataclass whosereserved_load: int = 0field overrides the abstract property).LeastLoadedRoutingStrategykeys its recency map onid(candidate). That works here because the candidates are built once insetup_asyncand live as long as the Rollouter, which the updated comment instrategies.py:102states. - Ordering holds at HEAD. Nothing is awaited between
chooseandreserved_load += group_size, so concurrentrun_group_rolloutscalls (the validationgatherand_rollout_loop) can't both pick a stale minimum. The done callback is scheduled withcall_soon, so it runs after the increment, also whencall_onereturns a future that is already done. The increment comes aftercall_one, so a send that fails to pickle leaves the count unchanged, and the new test guards that. - Cancel paths:
close()cancels pending calls after the drain timeout. Cancelling still fires the done callback, so the counts return to 0. The cancelled-caller test checks 2 -> 0. metrics():max(loads)would raise on an empty list, butnum_rollout_workersis a ceil of a positive value, and_trainer_loopruns aftersetup_async.test_verifiers.py:291builds the rollouter withobject.__new__and skips__init__, so it has no_worker_strategy. That is safe because the test only callssetup_async/closeand neverrun_group_rollouts.- Gauges: they are snapshots taken once per training step, not peaks or time averages. That is enough for the stated purpose (deciding whether per-rollout counting is worth it) over many steps, but one step's value can be noisy.
Not verified: the simulation numbers in the description. This touches only RL rollout code, but a maintainer still needs to review.
f4e05c0 to
a46408c
Compare
|
Reply from claude: @claude please review. Since your last review: |
|
Claude finished @felipemello1's task in 1m 8s —— View job Approve -- please address the item before merging (no new problems at a46408c; the human-written summary is still empty) -- review |
There was a problem hiding this comment.
Verdict: Approve -- please address the item below before merging. The changes since f4e05c0 (the empty-workers guard in metrics(), plus rewritten names and TODOs) add no correctness problems. The only open item is still the empty human-written summary.
Re-review of a46408c
- Changed:
metrics()returns[]when_worker_loadsis empty (torchtitan/rl/rollout/rollouter.py:358). The test checks this beforesetup_async. - Changed: both TODOs at the pick (
rollouter.py:300-310) are rewritten. Each now gives an example, its blocker or cost, and a link to #6118 (open; it rewrites the Verifiers worker). Their numbers (1.9x -> 1.4x) match the description. - Unchanged and still correct: the least-loaded pick through
LeastLoadedRoutingStrategy, the increment aftercall_one, the decrement in the done callback, and the three tests. - Still open: the "Summary (human-written)" section. The author says it is empty on purpose, but it still has to be filled in before merge.
Must fix
- The PR description's "Summary (human-written)" section is still the template placeholder. The author has to write it (CONTRIBUTING.md, "AI-assisted contributions").
Should consider
torchtitan/rl/rollout/rollouter.py:357-- no in-repo caller reaches the empty-workers guard (see the inline comment).
Details
VerifiersRollouteris the onlyRolloutersubclass, and itssetup_asynccallssuper().setup_async(...)(torchtitan/rl/examples/verifiers/rollouter.py:172). So_worker_loadsis never empty when_trainer_loop(controller.py:1495) callsmetrics().test_verifiers.py:291builds aVerifiersRollouterwithobject.__new__, which skips__init__, so_worker_loadsand_strategyare never set. The test only callssetup_asyncandclose, and both work without those fields. If a future test there callsmetrics()orrun_group_rollouts, it will hit anAttributeError.- The earlier ordering analysis still holds. Nothing is awaited between
chooseandreserved_load += group_size. The done callback runs on a later loop step, so the decrement always follows the increment. A send that fails to pickle never leaks a count.
Not verified: the simulation numbers in the description. This touches only RL rollout code, but a maintainer still needs to review.
| # A Rollouter subclass that overrides `setup_async` may spawn no workers. | ||
| if not self._worker_loads: | ||
| return [] |
There was a problem hiding this comment.
Nothing in the repo reaches this branch. The only subclass, VerifiersRollouter.setup_async (torchtitan/rl/examples/verifiers/rollouter.py:172), calls super().setup_async, so it always spawns workers. num_rollout_workers is a ceil of a positive value, and _trainer_loop runs only after setup_async succeeds. The only consumer is the new assert rollouter.metrics() == [] in the test. Per design.md ("every new symbol/branch needs a current consumer"), consider dropping the guard and that assert. A future subclass that spawns no workers would then fail loudly at max([]), and it would fail on choose([]) in run_group_rollouts anyway. This is small, so keeping it is also fine.
There was a problem hiding this comment.
Reply from claude: keeping it. The class docstring (rollouter.py:99-101, from main) tells a subclass that runs its own orchestration to override run_group_rollouts and also setup_async, so no pool spawns. That subclass never reaches choose([]), but the controller still calls metrics() at every step log, and max([]) would crash it there.
4dd856a to
b1e99fc
Compare
b1e99fc to
1bfa4eb
Compare
1bfa4eb to
fdbc666
Compare
|
Reply from claude: @claude please review |
|
Claude finished @felipemello1's task in 54s —— View job Approve -- please address the item before merging (no new problems at fdbc666; the human-written summary is still empty) -- review |
There was a problem hiding this comment.
Verdict: Approve -- please address the item below before merging. At fdbc666 the patch's logic is the same as at e287806, and I found no new problems. The only open item is still the empty human-written summary.
Re-review of fdbc666 (since e287806)
- Unchanged:
- the pick through
LeastLoadedRoutingStrategy(torchtitan/rl/rollout/rollouter.py:400) - the increment right after
call_one(:413), then the decrement in the done callback (:433) - the empty-workers guard in
metrics(), and both TODOs - the README paragraph, which still names the TODO "retire a worker whose groups keep failing" with the exact wording used in the code
- the docstring updates in
routing/strategies.pyandrouting/types.py, and the three tests
- the pick through
- Callsites:
torchtitan/rl/examples/verifiers/rollouter.pydefines the Verifiers rollout worker, not aRolloutersubclass. It never touches_worker_loads. The only bare-constructedRollouterthat reachesrun_group_rollouts/metrics()is_rollouter_without_datasetsintest_rollouter_pool.py, and it sets both new fields. - Still open: the "Summary (human-written)" section.
Example. 3 workers, all idle:
send g0(4), g1(2), g2(2), g3(1) -> [[g0], [g1, g3], [g2]] loads [4, 3, 2]
(g3: w1 and w2 tie at 2; w1 was chosen longer ago)
g0 returns -> loads [0, 3, 2]; the next group goes to w0
Must fix
- The "Summary (human-written)" section of the PR description is still the template placeholder. The author has to write it (CONTRIBUTING.md, "AI-assisted contributions").
Not verified: the simulation numbers in the description. I also couldn't fetch e287806 in this shallow checkout, so "unchanged" means I compared HEAD against the earlier reviews' descriptions of the patch, not against an actual diff of the two commits. This touches only RL rollout code, but a maintainer still needs to review.
fdbc666 to
5936533
Compare
40d13b7 to
4e43438
Compare
24e9435 to
c64fc36
Compare
…n flight The Rollouter keeps one _RolloutWorkerLoad (a RoutingCandidate) per worker and picks with LeastLoadedRoutingStrategy: the lowest count of rollouts in flight, the one chosen longest ago on a tie. The count rises by the group size once the call is sent and drops when the call ends. Rollouter.metrics() adds rollout_worker/num_rollouts_in_flight_max and _mean to each step's metrics. The RL README's "Rollout workers on more than one host" says why a host whose groups fail at once gets most new groups.
c64fc36 to
388e05b
Compare
Summary (human-written)
Agent details
TLDR: Each rollout group now goes to the worker with the fewest rollouts in flight, picked by
LeastLoadedRoutingStrategy, the class both generator routers use to place new sessions. In a placement simulation at Terminal-Bench 9B's shape (48 groups of 32 on 12 workers), the busiest worker runs 1.9x the mean load instead of 2.4x with random placement, and 2.8x instead of 4.0x at p99. The simulation is not part of this PR. On real runs, two gauges read the busiest worker's count and the mean count once per step.Problem
Solution
_RolloutWorkerLoadper worker: aRoutingCandidatewhosereserved_loadis the worker's rollouts in flight.LeastLoadedRoutingStrategy. A tie goes to the worker chosen longest ago.Rollouter.metrics()addsrollout_worker/num_rollouts_in_flight_maxandrollout_worker/num_rollouts_in_flight_meanto each training step's metrics, next to the rollout buffer's gauges. They count whole groups until they return, so they read higher than the table's running rollouts below. A Rollouter subclass that overridessetup_asyncmay spawn no workers: thenmetrics()returns nothing.Rollouter.metrics()'s docstring says how to read them: a max well above the mean means the load is uneven.Results
The table comes from a placement simulation outside this PR, not from runs. Rollout times: lognormal, a per-task factor times per-rollout noise, median 6 minutes, 3,000 simulated minutes. The simulation copies
LeastLoadedRoutingStrategy.choose; a first-index tie-break gives the same numbers, because ties happen only at the start.You may ask:
LeastLoadedRoutingStrategy? It already picks the least loaded target, with a tie-break that spreads idle starts: the generator router and the DP-rank router place new sessions with it (the sticky strategy's fallback). One way to do it in titan.run_group, the Verifiers worker's included. A TODO marks it.call_oneraises before sending when an argument can't be pickled, e.g. a dataset sample that holds a lock. The count must not rise for a group that never left.rollouts_per_worker(R)? Counted the way the gauges count (whole groups until they return), and with no validation group in flight, a worker holds at most ceil(R / S) x S training rollouts, where S is the group size. That is R when S divides R, as in every recipe. A validation burst can push a worker past it.NOTE:
tests/) is back at 0 at once and gets most new groups, where random placement sent it about 1 in N. The RL README says so too. A rollout that fails ends as ERROR_INFRA with 0 turns, sorollout/num_turns(mean) drops; only arun_groupthat raises counts inrollout/group_failures. A TODO at the pick says to retire such a worker.run_group_rolloutsedits the same lines. Whichever lands second rebases, and a retry must count the group on the worker it retries on.Follow up / TODO (each is a
# TODOin the code):Test plan
New or changed in
test_rollouter_pool.py:test_each_group_goes_to_the_worker_with_the_fewest_rollouts_in_flight(with a tie that the least recently chosen worker wins, which a first-index tie-break fails; andmetrics()with no workers returns nothing, whichmax([])fails),test_a_run_group_that_fails_to_send_leaves_its_worker_count_unchanged, andtest_close_waits_for_run_group_of_cancelled_caller(the count stays 2 after the cancel, and is 0 once the group returns).pre-commitandpyrefly checkpass.