Skip to content

[graph_trainer] Don't pick dead unshard chain as canonical in FSDP dedup - #4966

Closed
SherlockNoMad wants to merge 1 commit into
mainfrom
graph_trainer_fix_dead_unshard_dedup
Closed

SherlockNoMad wants to merge 1 commit into
mainfrom
graph_trainer_fix_dead_unshard_dedup

Conversation

@SherlockNoMad

Copy link
Copy Markdown
Contributor

Fixes the qwen3 MoE loss-compare failure in the GraphTrainer 8 GPU H100 Integration Tests on main.

Why

#4867 moved SimpleFSDP stacked expert weights (w13, shape [E, 2, F, D]) from Shard(0) to Shard(2). Their traced unshard chain is now all_gather -> wait -> split -> cat(dim 2) -> view, not just all_gather -> wait.

GroupedLinear.forward reads self.weight twice, and one of those reads is only for its shape. That chain's output is unused, so DCE cuts it back to the impure all_gather/wait. deduplicate_fsdp_unshard_chains_pass could then pick the cut-down chain as canonical. It redirected the live chain's users to the raw wait output, which has the wrong layout for Shard(dim > 0). The expert matmul silently used a scrambled weight, and the losses diverged from eager.

Under Shard(0) the raw wait output happened to be the correct unsharded weight, which kept the bug hidden until #4867.

What

  • Only chains whose output has users can be picked as canonical.
  • Dead chains are removed together with their redundant all-gather.

Test plan

  • New unit test test_dead_unshard_chain_is_not_canonical (fails without the fix, passes with it)
  • test_passes.py: 231 passed, 1 skipped
  • test_graph_pp_passes.py: 46 passed
  • test_bitwise_deterministic.py: 15 passed, 10 skipped
  • 20-step qwen3 MoE scripts/loss_compare.py (eager vs graph_trainer, same config as CI): all losses equal

Follow-ups (not in this PR)

  • SimpleFSDP now ignores the shard_dim=0 it is passed for routed experts, so its expert placement no longer matches core FSDP. This may be unintended in [ci][graph_trainer] Shard stacked linears on matrix rows #4867.
  • GroupedLinear.forward could read self.weight once, which would avoid the redundant unshard chain entirely.

#4867 moved SimpleFSDP stacked expert weights (w13, [E, 2, F, D]) to
Shard(2), so their unshard chain is all_gather -> wait -> split -> cat(dim 2)
-> view instead of just all_gather -> wait.

GroupedLinear.forward reads self.weight twice, once only for its shape. That
chain's output is unused, so DCE truncates it to the impure all_gather/wait.
deduplicate_fsdp_unshard_chains_pass could then pick the truncated chain as
canonical and redirect the live chain's users to the raw wait output, which
has the wrong layout for Shard(dim > 0). The expert matmul silently consumed
a scrambled weight and the qwen3 MoE eager-vs-graph_trainer loss compare in
the H100 GraphTrainer CI diverged. With Shard(0) the raw wait happened to be
the correct unsharded weight, which is why this was latent.

Fix: only chains whose output has users are eligible as canonical; dead
chains are removed along with their redundant all-gather.

Test plan:
- New unit test test_dead_unshard_chain_is_not_canonical (fails without fix)
- test_passes.py, test_graph_pp_passes.py, test_bitwise_deterministic.py pass
- 20-step qwen3 MoE loss_compare (eager vs graph_trainer): all losses equal
@SherlockNoMad SherlockNoMad added the ciflow/h100.8 Trigger H100.8 CI label Sep 30, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 30, 2026
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 30, 2026
@SherlockNoMad

Copy link
Copy Markdown
Contributor Author

abort, in favor of #4949

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/h100.8 Trigger H100.8 CI 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.

2 participants