Skip to content

[rl] Send each trainer DP replica only its column of the grid - #5301

Closed
felipemello1 wants to merge 1 commit into
felipemello1/trainer-pull/1-microbatch-refsfrom
felipemello1/trainer-pull/4-scatter
Closed

felipemello1 wants to merge 1 commit into
felipemello1/trainer-pull/1-microbatch-refsfrom
felipemello1/trainer-pull/4-scatter

Conversation

@felipemello1

Copy link
Copy Markdown
Contributor

TLDR: Every trainer rank received the whole step's [num_microbatches][dp_degree] microbatch grid and kept only its column. Now each DP replica receives only its column, so each rank receives 1/dp_degree of the bytes. CPU Monarch, 4 ranks, a 2.6 GB step: 5.3 -> 2.1 s at dp 4 and 5.0 -> 3.0 s at dp 2 x tp 2, and peak memory per rank 7.6 -> 1.9 GB / 3.8 GB. Stacked on the PR that adds the opt-in RDMA fast path; works with it on or off.

Problem

  1. The batcher packs each step as a [num_microbatches][dp_degree] grid of TrainingMicrobatch.
  2. The controller calls trainer.forward_backward on the whole trainer mesh with the whole grid.
  3. Every trainer rank receives the grid and keeps only its column, 1/dp_degree of it, and needs ~3x the message in fresh memory while it unpickles.
  4. With the RDMA fast path off (the default), that is the whole by-value grid. In one router-replay run (614 B/token, DP 2 x TP 2), step 3 sent 17.8 GB per rank and the job died on Monarch's 60 s proc-state timeout.
before: trainer[0..3].forward_backward(grid [M][2])   -> each of the 4 ranks receives M x 2 microbatches

Solution

  1. The trainer lays its ranks out as (dp, cp, tp), and RL rejects PP. So each DP replica is cp * tp consecutive ranks.
  2. Controller.setup_async uses Monarch's split to cut the trainer mesh into one sub-mesh per DP replica, once.
  3. It then checks that layout once. Each rank reports its DP rank through a new get_dp_rank endpoint, and setup raises if a replica's ranks don't all match.
  4. Controller._trainer_loop sends one call per replica, each with its column, then awaits them together. It reads rank 0's metrics as before.
  5. Trainer.forward_backward takes that column, microbatches: [num_microbatches], instead of the grid.
  6. Unchanged: the batcher, the grid, the global loss and routing token counts, the RDMA fast path, and every other trainer call.
after:  trainer[0, 1].forward_backward([row[0] for row in grid])  -> M microbatches each
        trainer[2, 3].forward_backward([row[1] for row in grid])  -> M microbatches each

Results

CPU Monarch, one host, 4 rank processes, real TrainingMicrobatches, 131,072-token microbatches at 614 B/token (routed expert ids and top-32 logprobs in model_kwargs), RDMA fast path off. Time from the call to every rank holding its column:

2.6 GB per step              whole grid to every rank   each replica its column   peak memory per rank
dp 2 x tp 2                  5.0 s                      3.0 s                     7.6 -> 3.8 GB
dp 4 x tp 1                  5.3 s                      2.1 s                     7.6 -> 1.9 GB

GPU, Qwen3-0.6B, H100, batch invariant, lockstep, trainer DP 2 (3 GPUs), one TP 1 generator, Monarch 0.7, 5 steps: loss/mean is bitwise equal to main's DP 2 run at every step with the RDMA fast path on, at this PR's head. On top of the whole stack (the same final code), it is also bitwise equal with the fast path off and on.

CPU mesh check (the controller's actual setup and _trainer_loop code against real Monarch actor meshes, a fake trainer with the real DP-rank lines and a collective that spans replicas; run when this PR sat on top of the batcher-process PR):

  • dp 2 x tp 2, dp 4, dp 1 x tp 4, dp 2 x cp 2, and HSDP 2 x 2 x 2 on a 2-level mesh: each rank got exactly its column every step, and rank 0's metrics came from global rank 0.
  • With DP made the innermost dimension, the startup check raised.
  • When one replica raised, the step raised within 20 ms and the batcher release ran once.

You may ask:

  • Why keep the RDMA fast path? They're orthogonal. Scattering still sends each rank its column by value; at heavy payloads that is still past Monarch's 10 GiB frame cap (~14.7 GB per rank in the replay run at dp 2). The fast path removes the big message, and a later PR in this stack overlaps its reads with compute. With both, the fast path's refs message also shrinks by 1/dp_degree.
  • Why not one call per rank? Each column would be pickled cp * tp times on the controller's event loop.
  • Why slice by layout instead of asking each rank where it belongs? A Monarch slice needs consecutive ranks anyway. The startup check turns a layout drift into an error instead of silently wrong gradients.

NOTE:

  • The cp * tp ranks of a replica still each receive the same column (the "read once per replica" TODO).
  • The GPU run is DP 2 x TP 1, so each replica is one rank; the CPU mesh check covers replicas of several ranks.
  • Not tested on more than one host.

Test plan

  • pytest tests/rl/unit_tests/cpu (from the repo root, Monarch 0.7): 435 passed, 5 skipped; the 2 failures (test_verifiers_terminal_bench.py, a config mismatch) fail on main too.
  • test_trainer.py: forward_backward trains a column by value, and reads a column of RDMAMicrobatches before training it.
  • GPU parity and the CPU mesh check above.

The controller called forward_backward on the whole trainer mesh with the
step's [num_microbatches][dp_degree] grid, so every rank received every DP
column and kept one. It now splits the trainer mesh into DP replicas (the
trainer orders ranks as (dp, cp, tp)) and calls each replica with only its
column; a startup check confirms each replica's ranks report its DP rank.
Trainer.forward_backward takes that column.

This cuts the by-value message and unpickle per rank to 1/dp, and works
with or without the RDMA fast path.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 12, 2026
@felipemello1
felipemello1 added this pull request to stack #5302 October 12, 2026 04:10
@felipemello1
felipemello1 removed this pull request from stack #5302 October 12, 2026 04:43
@felipemello1
felipemello1 deleted the felipemello1/trainer-pull/4-scatter branch October 12, 2026 04:44
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