Repository navigation
Use the fake backend for a size-1 edp_shard mesh axis - #5837
Conversation
[ghstack-poisoned]
When expert parallelism spans the whole sparse region (ep = dp_shard * cp * tp), edp_shard has size 1. The axis stays active so FSDP still wraps the routed experts, and unflatten_mesh gave only inactive axes the fake backend, so DeviceMesh created a one-rank NCCL communicator for it. FSDP2 and DTensor skip collectives on a one-rank axis, and the fake backend copies inputs to outputs as a one-rank collective would, so unflatten_mesh now gives a size-1 edp_shard the fake backend. On GB200 this saves about 390 MiB of GPU memory per rank. build_alternative_fsdp_mesh no longer warns that max_ctas is not modeled by the fake backend when the fake group has one rank, since there is nothing to cap. Otherwise DistMuon with max_nccl_ctas_for_comms would print that warning in real runs. Co-authored-by: Wei Feng <[email protected]> ghstack-source-id: 991aac1 Pull-Request: #5837
|
@claude review this pr |
|
Claude finished @anshul-si's task in 2m 27s —— View job Approve -- please address the items below before merging (needs a bitwise loss/grad_norm check with |
There was a problem hiding this comment.
Verdict: Approve -- please address the items below before merging. The fix is small and justified by a measured ~390 MiB per rank. The one thing missing is a numerics check: the fake PG now backs an axis that FSDP uses in real training. A maintainer still needs to review, since this touches core distributed code.
What this PR does. When ep == dp_shard * cp * tp, edp_shard has size 1. It stays "active" (_axis_is_active returns ep > 1) so FSDP can still wrap routed experts with mixed precision. Before this PR, unflatten_mesh only gave fake groups to inactive axes, so DeviceMesh built a one-rank NCCL communicator for it. Now a size-1 edp_shard gets the fake backend. build_alternative_fsdp_mesh also stops warning about max_ctas for one-rank fake groups.
Example.
dp_shard=4, tp=2, ep=8, world=8 -> edp_shard = 4*1*2/8 = 1
before: sparse mesh (edp_shard=1 [nccl, 1 rank], ep=8 [nccl])
+ a CTA-capped 1-rank NCCL child when max_ctas is set
after: sparse mesh (edp_shard=1 [fake], ep=8 [nccl])
get_optional_mesh("edp_shard") still returns the mesh, so FSDP still wraps experts
Must fix
- No numerics proof in the description. Before, the fake backend only backed axes that never carry collectives. Now it backs an FSDP shard axis on the real training path. Correctness depends on FSDP2/DTensor skipping collectives at size 1, or on
FakeProcessGroupcopying input to output for every collective FSDP issues (all-gather, reduce-scatter, all-reduce). Please add a bitwise loss + grad_norm comparison (seed 42, deterministic) before vs. after for an MoE config withedp_shard == 1. Ideally use HSDP (dp_replicate > 1), so the sparse mesh mixes a fakeedp_shardwith a realdp_replicate. If a silent mismatch shows up, it would be in the routed-expert gradients.
Should consider
torchtitan/distributed/parallelism_context.py:291-- the same reasoning applies to a size-1dp_shard. Keying ondegree == 1removes theedp_shardspecial case (see inline).torchtitan/distributed/parallelism_context.py:578-582(get_all_one_dimensional_meshesdocstring) -- it says fake-backed axes are excluded "because their process groups cannot carry collectives". A size-1edp_shardis now fake but still used by FSDP. It is excluded only by thesize() > 1filter, so the stated reason no longer holds for it. Consider rewording.
Not verified: that FSDP2 skips, or the fake PG faithfully reproduces, every collective issued on a one-rank edp_shard group (needs the GPU run above).
| if not self._axis_is_active(axis, degree) or ( | ||
| axis == MeshAxisName.EDP_SHARD and degree == 1 | ||
| ): |
There was a problem hiding this comment.
The reasoning here (FSDP2/DTensor skip collectives on a one-rank axis, and the fake PG behaves like a one-rank group) isn't specific to edp_shard. dp_shard is the only other axis _axis_is_active keeps alive at size 1. It hits the same case under TP-only, PP-only, and DDP-only runs (e.g. dp_shard=1, tp=8), and still gets a one-rank NCCL communicator there, plus a CTA-capped child from build_alternative_fsdp_mesh. Since every other axis is already fake at size 1, keying on the degree drops the axis-name special case:
| if not self._axis_is_active(axis, degree) or ( | |
| axis == MeshAxisName.EDP_SHARD and degree == 1 | |
| ): | |
| if degree == 1 or not self._axis_is_active(axis, degree): |
(and change the docstring above to "for inactive axes and size-1 axes"). This touches the dense main path, so it needs the same loss/grad_norm check as the sparse case. If there's a reason dp_shard has to keep a real group at size 1, please put it in a comment here.
| if not self._axis_is_active(axis, degree) or ( | ||
| axis == MeshAxisName.EDP_SHARD and degree == 1 |
There was a problem hiding this comment.
why don't we update _axis_is_active function itself so that EDP_SHARD and DP_SHARD are treated similarly.
https://github.com/pytorch/torchtitan/blob/main/torchtitan/distributed/parallelism_context.py#L238
|
@claude review my review |
|
Claude finished @tianyu-l's task in 1m 14s —— View job Request changes: @tianyu-l is right that the two axes should be treated the same way, but the change belongs in the |
There was a problem hiding this comment.
Verdict: Request changes -- @tianyu-l is right that edp_shard and dp_shard should be handled the same way, but the change belongs in the backend choice inside unflatten_mesh. Putting it in _axis_is_active would remove edp_shard from FSDP.
What this PR does. When ep == dp_shard * cp * tp, edp_shard has size 1. This PR gives that axis the fake backend, so no one-rank NCCL communicator is built for it. It also stops build_alternative_fsdp_mesh from warning about max_ctas on one-rank fake groups.
On @tianyu-l's comment (parallelism_context.py:292)
Partly correct. The goal of treating both axes alike is right. The mechanism isn't.
_axis_is_active does two separate jobs:
- It picks the backend in
unflatten_mesh(parallelism_context.py:291). - It decides whether an axis is exposed at all, through
get_optional_mesh(:446-450),get_activated_mesh(:546-551) andget_all_one_dimensional_meshes(:606).
The PR wants a size-1 edp_shard to be fake-backed and still exposed. If _axis_is_active("edp_shard", 1) returned False, then get_activated_mesh(_SPARSE_STORAGE_AXES) in resolve_sparse_fsdp_mesh (fsdp.py:169) would drop edp_shard from the sparse mesh. But DataParallelMeshDims(shard="edp_shard") (fsdp.py:172) and edp_mesh["edp_shard"] (fsdp.py:431) still expect that axis. The result would be a failure, or routed experts losing their FSDP mixed-precision wrapping. That wrapping is the reason the comment at :238-241 keeps the axis alive at size 1.
dp_shard has the same two-job problem: it must stay exposed at size 1 (comment at :233-236).
The symmetric fix is to key the backend on the degree and leave _axis_is_active as it is:
for axis, degree in zip(axis_names, axis_degrees, strict=True):
if degree == 1 or not self._axis_is_active(axis, degree):
backend_override[axis] = "fake"For every axis except dp_shard/edp_shard, inactive already means degree == 1. So this line just says "one rank or inactive -> fake" and drops the edp_shard name check. not active is still needed for edp_shard with ep == 1, where its degree is dp_shard * cp * tp > 1. Update the docstring at :286-287 to match. This is the same change the earlier automated inline suggestion proposed.
Must fix
- Scope of the extra validation. With the change above, a size-1
dp_shardalso gets the fake backend. That happens on the dense main path in TP-only, PP-only and DDP-only runs (e.g.dp_shard=1, tp=8). The description's bitwise checks all havedp_shard=2, so they don't cover it. Please add one bitwise loss + grad_norm run (seed 42, deterministic) withdp_shard == 1, preferably HSDP-styledp_replicate > 1, dp_shard = 1so FSDP mixes a fake shard group with a real replicate group. If some consumer needs a real one-rankdp_shardgroup, say why in a comment at:291and keep the special case. get_all_one_dimensional_meshesdocstring (:577-582) says fake-backed axes are excluded "because their process groups cannot carry collectives". A size-1 fakedp_shard/edp_shardis still used by FSDP and is excluded only bysize() > 1. Reword it to: excluded because they are inactive or have one rank.
Not verified: that FSDP2 issues no collectives, or only ones the fake PG reproduces faithfully, on a size-1 dp_shard group in DDP/HSDP mode (needs a GPU run). A maintainer still needs to review this core distributed change.
dp_shard and edp_shard stay active at size 1 so FSDP can still wrap parameters on them: edp_shard when expert parallelism spans the whole sparse region (ep = dp_shard * cp * tp), and dp_shard in TP-, CP-, PP- or DDP-only runs. unflatten_mesh gave only inactive axes the fake backend, so DeviceMesh created a one-rank NCCL communicator for each. FSDP2 and DTensor skip collectives on a one-rank axis, and the fake backend copies inputs to outputs as a one-rank collective would, so unflatten_mesh now gives every size-1 axis the fake backend, as other axes already had; the two shard axes stay active. Groups flattened from a size-1 dp_shard, such as FSDP's (dp_shard, cp) group under CP, are still real. On GB200 each one-rank communicator costs about 390 MiB of GPU memory per rank. build_alternative_fsdp_mesh no longer warns that max_ctas is not modeled by the fake backend when the fake group has one rank, since there is nothing to cap. Otherwise DistMuon with max_nccl_ctas_for_comms would print that warning in real runs. Co-authored-by: Wei Feng <[email protected]> ghstack-source-id: 0ce05fd Pull-Request: #5837
|
@claude review the pr and make sure all feedback has been addressed |
|
Claude finished @anshul-si's task in 1m 38s —— View job Approve -- please address the items below before merging: all the code feedback is fixed, but the PR description is out of date (it covers only |
There was a problem hiding this comment.
Verdict: Approve -- please address the items below before merging. All code feedback is fixed at HEAD. Two things are still open: the PR description (it still covers only edp_shard and gives an old test name), and a numerics run for the dp_shard == 1 path that the PR now also changes. @tianyu-l's change request is still open and needs their sign-off. A maintainer still needs to review this core distributed change.
What this PR does. unflatten_mesh now gives the fake backend to any size-1 axis, not just inactive ones (degree == 1 or not self._axis_is_active(...)). As a result, a size-1 dp_shard or edp_shard no longer gets a one-rank NCCL communicator, and both axes are still exposed to FSDP. build_alternative_fsdp_mesh stops warning about max_ctas when a fake group has one rank.
Example.
dp_shard=1, tp=8, ep=8, world=8 -> edp_shard = 1*1*8/8 = 1
before: dp_shard=1 [nccl, 1 rank], edp_shard=1 [nccl, 1 rank]
after: dp_shard=1 [fake], edp_shard=1 [fake]
get_optional_mesh("dp_shard"/"edp_shard") still returns the mesh
Status of earlier feedback
- @tianyu-l (
parallelism_context.py:291, treatdp_shardandedp_shardthe same): addressed, by keying on the degree instead of changing_axis_is_active, which would have droppededp_shardfromresolve_sparse_fsdp_mesh. Theedp_shardname check is gone. @tianyu-l, please confirm this resolves your request. - Bot:
unflatten_meshdocstring (:286-287): addressed. - Bot:
get_all_one_dimensional_meshesdocstring (:576-586): addressed. - Bot: bitwise check with
edp_shard == 1: addressed in the description (HSDP 4x GB300, DSV3 and Kimi K3 on 2x GB200). - Bot: bitwise check with
dp_shard == 1: still open (item 2 below).
Must fix
- The PR description doesn't match the code. The title and summary still say only a size-1
edp_shardgoes fake. The code now also coversdp_shard, which affects the dense path for TP-only, PP-only and DDP-only runs. The test command uses-k test_size_one_edp_shard_is_fake_but_available, but that test is nowtest_size_one_shard_axes_are_fake_but_available, so the command as written selects no tests. Please update the title, summary and test command, and say the dense path also saves its one-rank communicator in these configs. - No numerics proof for
dp_shard == 1. Every listed bitwise run hasdp_shard=2. Please add one bitwise loss and grad_norm run (seed 42, deterministic, 10 steps) before vs. after withdp_shard == 1. HSDP-styledp_replicate=2, dp_shard=1is best, because thenfully_shardmixes a fake shard group with a real replicate group on the dense parameters. A TP-only run (dp_shard=1, tp>1) is a cheaper second choice.
Details
- Other users of a size-1 fake
dp_shard/edp_shardgroup:set_pg_timeouts(utils.py:605) andspmd_mesh_group(spmd_types.py:126) never see a one-rank group, because of thesize() > 1filters. graph_trainer's HSDP reorder pass (experiments/graph_trainer/passes.py:308-319) still appends a pair for a size-1edp_shardand now uses the fake group's name. That pair was already one rank before this PR, so only the backend changed. It's worth checking in thedp_shard == 1run if graph_trainer is in scope. - The new CPU test does guard the invariant that matters: both axes stay reachable through
get_optional_meshand are fake-backed. A regression to_axis_is_activewould break it.
Not verified: that FSDP2 issues no collectives, or only ones the fake PG reproduces faithfully, on a size-1 dp_shard group in the DDP/HSDP dense path (needs the GPU run in item 2).
…er override; agent self-review; rl/transport (#6407) ## Summary (human-written) <!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why this approach, and what you checked yourself. --> ## Why Since #5667, `@claude review` has reviewed 459 PRs. Of the 380 PRs it approved, 30 then got a formal "request changes" from a maintainer, and about 20 more got substantive inline asks. In nearly all of them the bot either saw the problem and approved anyway, or never asked the questions maintainers ask most ("does the repo already do this?", "who should own this?"). The opposite failure, the bot blocking something a maintainer was fine with, is rare: in the PRs we sampled, 2 of its 81 must-fix items were wrong and about a third were minor (the empty human-written summary was the biggest source). **Example** (#6112, controller sizes the rollout worker pool): ``` before: bot approved 10 times; tianyu-l then blocked: "the controller reads rollouter.rollouts_per_worker to compute a count it hands back to the Rollouter -- violates encapsulation" after: same commit, new skill -> Request changes, must-fix #1: "controller.py:589 -- wrong owner for the worker count ..." ``` There was also no way for a code owner to overrule the bot except dismissing its review in the GitHub UI, and a later `@claude review again` would block again. ## What changed **Review skill** (`.claude/skills/pr-review/`, `review-checklist.md`): - Verdict rules: Approve only when every remaining item is an edit the bot could write itself. Anything the author still owes (a design change, an unexplained golden change, evidence the PR rests on) is Request changes; anything only a maintainer can decide is Needs discussion. A hedge like "a maintainer should decide" can't come with Approve. - Always must-fix: a violated written rule, a silent wrong result, a second definition of an existing concept, a gate finding. - Design gate: new questions for the existing counterpart, in-repo ownership, and the design behind a bug; "ban only what doesn't work on main"; read the numbers behind a claim. The overlapping-PR search is mandatory and reported. - Re-review covers every commit since the last full review, including code taken from the bot's own suggestions. A scoped question gets a `COMMENT`, not a whole-PR verdict. - The bot fact-checks its own proposed fixes, and cites a source for claims about external tools or marks them unverified (never must-fix). - An empty "Summary (human-written)" is a one-line "Before merge" note that never affects the verdict. - Checklist: config ownership direction, general shared wrappers, dead code the PR creates, model variants as flavors, a mechanical pass over every new or changed function. **Code owner override** (`SKILL.md`, `claude-code.yml`, `CONTRIBUTING.md`): a code owner comments `@claude override` (optionally `@claude override 2,3: <reason>`); the bot approves if nothing else is open and doesn't re-raise the overridden items. A code owner's GitHub approval or "LGTM" is not an override, so a teammate can approve without the bot dropping its findings. Dismissing the bot's review still unblocks the PR, but isn't an override: the bot can't tell who dismissed it. **Agent self-review** (`CLAUDE.md`, `pr-description`): an agent preparing a PR runs `pr-review` in branch mode first and records unaddressed items in an optional "Self-review" section. PRs written without an agent are unaffected; a missing section is never a finding. Where each new rule comes from (maintainer asks the bot missed): | Change | PRs | | --- | --- | | No Approve with open design/evidence items or a "maintainer should decide" hedge | #6007, #5837, #5997, #5712, #5511, #5996 | | Author waiver can't clear a must-fix | #6201, #6202, #6203 | | Existing counterpart; one source of truth | #5228, #6169, #5881, #5823, #5824, #6059, #5812 | | Responsibility owner; config ownership direction | #6112, #6332, #5802, #5803, #6325 | | Fix the design behind the bug; a TODO doesn't justify a workaround | #5572, #5689, #5657 | | Fact-check the bot's own suggested fixes | #5740, #5802, #5511, #4819, #5543 | | Re-review covers all new commits; scoped asks don't approve | #5824, #5842 | | Mandatory overlap search | #5885, #6248 | | Ban only what doesn't work on main | #5543 | | Summary placeholder no longer a must-fix | #6119, #6174, #6112, #5868 | **Rules**: `design.md` gets the one-general-wrapper rule (moved from `models.md`, whose glob skipped `torchtitan/rl`), thin adapters, and the consumer owning its values; `config.md` gets ownership direction and "a Config doesn't read runtime state"; `direction.md` calls all-to-all the MoE reference implementation, so a new vendor-specific dispatch path (e.g. #6242) needs a reason the existing backends can't serve. **Area reviewers**: `.ci/` -> huydhn, wwwjn; `torchtitan/rl/transport/` -> d4l3k. **Mechanical, skim**: `torchtitan/rl/transport/` holds data movement between RL actors. - `rl/model_state_dict_transport.py` -> `rl/transport/model_state_dict.py` - `rl/MODEL_STATE_DICT_TRANSPORT.md` -> `rl/transport/MODEL_STATE_DICT_TRANSPORT.md` - `rl/trainer/rdma_microbatch.py` -> `rl/transport/rdma_microbatch.py` - tests: `tests/rl/unit_tests/cpu/test_{model_state_dict_transport,rdma_microbatch}.py` -> `tests/rl/unit_tests/cpu/transport/test_{model_state_dict,rdma_microbatch}.py` - imports, READMEs and `scripts/install_rl_env.sh` updated; no code changes. ## User-facing changes | Before | After | | --- | --- | | Overrule the bot: dismiss its review in the UI, and it blocks again on the next run | `@claude override [items]: <reason>`; the bot approves and doesn't re-raise them | | Empty human-written summary: bot must-fix | One "Before merge" line, no effect on the verdict | | `from torchtitan.rl.model_state_dict_transport import ...` | `from torchtitan.rl.transport.model_state_dict import ...` | | `from torchtitan.rl.trainer.rdma_microbatch import ...` | `from torchtitan.rl.transport.rdma_microbatch import ...` | ## Testing Backtest: the new skill run locally (nothing posted) on each PR at the commit and thread state the bot originally saw. | PR | Bot then | New skill | What the maintainer asked | | --- | --- | --- | --- | | #6112 | Approve | Request changes | worker-count ownership (caught) | | #5837 | Approve | Request changes | apply to `dp_shard` too (caught) | | #5572 | Approve | Request changes | delete the injection design (caught) | | #6201 | Approve after author waived pins | Request changes, waiver ignored | pins broke GPU tests after merge (caught) | | #6007 | Approve | Request changes | fold K3.1 into K3 (caught) | | #5543 | Request changes: "ban it" | Request changes: keep main's path | don't break gpt-oss + DeepEP (fixed) | | #6144, #5905 (controls) | Approve | Approve | -- | | #6137 (control) | Approve | Needs discussion | -- (merge gate on an upstream API) | Code owner override, simulated on #6201: override by a code owner lifts the named item; `@claude override` from the author changes nothing; a code owner's "LGTM" approval followed by "review again" keeps Request changes. Transport move: 83 RL CPU unit tests pass locally (`transport/test_model_state_dict.py`, `test_trainer.py`, `test_engine_loop.py`). `transport/test_rdma_microbatch.py`, `test_generator.py` and `test_batcher_actor.py` fail to collect in my env on `origin/main` too (stale attention-gym / Monarch), so CI is the check for those. ## Known issues / follow-ups - Open PRs that edit the moved files rebase cleanly (git follows the renames), but new imports of the old module paths will break: #6309 (adds a delta transport that should live in `rl/transport/`), #6237, #6176, #6177, #5862, and drafts #6127, #6393, #6163, #5764. - The Actions bot can't read dependency source (e.g. SGLang); follow-up for the workflow. ## Self-review pr-review (branch mode) at ad905a5: Needs discussion. Fixed: branch mode diffs against `origin/main` and skips description findings before a description exists; the "Config doesn't read runtime state" rule no longer contradicts hardware validation; dismissal dropped as an override form (the bot can't tell who dismissed); `test_rdma_microbatch.py` added to the transport area. Not addressed: - One concern per PR -- the five changes ship together at the maintainer's request. - Open PRs touching the moved files -- listed above; the maintainer will coordinate.
Summary:
When expert parallelism spans the whole sparse region (ep = dp_shard * cp *
tp), edp_shard has size 1. The axis stays active so FSDP still wraps the
routed experts, and unflatten_mesh gave only inactive axes the fake backend,
so DeviceMesh created a one-rank NCCL communicator for it. FSDP2 and DTensor
skip collectives on a one-rank axis, and the fake backend copies inputs to
outputs as a one-rank collective would, so unflatten_mesh now gives a size-1
edp_shard the fake backend. On GB200 this saves about 390 MiB of GPU memory
per rank. Based on weifengpy#20, which did numeric testing on a larger scale, and also didbitwise check of base d0ab95e vs this PR (seed 42, deterministic, 10 steps), all with edp_shard == 1: HSDP dp_replicate=2, dp_shard=2, ep=2 on 4x GB300, plus DeepSeek V3 and Kimi K3 (DistMuon) with dp_shard=2, ep=2 on 2x GB200. Loss, grad_norm and the MoE load-balance loss match exactly, which also covers the routed-expert gradients, and per-rank NCCL communicators drop from 6 to 5 because the 1-rank edp_shard group is gone.
Test Cases
Stack from ghstack (oldest at bottom):
Co-authored-by: Wei Feng [email protected]