Skip to content

[graph_trainer] Prefer live SimpleFSDP unshard chains - #4934

Merged
sanketpurandare merged 9 commits into
mainfrom
gh/sanketpurandare/47/head
Oct 2, 2026
Merged

sanketpurandare merged 9 commits into
mainfrom
gh/sanketpurandare/47/head

Conversation

@sanketpurandare

@sanketpurandare sanketpurandare commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Summary:
SimpleFSDP intentionally materializes a parameter on each property access, so tracing may produce duplicate annotated all-gather and post-gather preparation chains for one parameter. GraphTrainer already canonicalizes those chains before partitioning and FSDP extraction.

Prefer an unshard endpoint that still has consumers when choosing the canonical chain. Dead-code elimination removes an unused preparation tail but retains its effectful collective; choosing that dead raw endpoint can otherwise replace a live prepared MXFP8 value with a BF16 gathered tensor. Remove dead duplicate chains without substituting their unused endpoints.

This keeps SimpleFSDP functional and compiler-safe. It adds no module hooks, Python cache state, MXFP8 execution-mode branches, or changes to eager FSDP behavior.

Test Plan:

  • The six focused FSDP unshard-deduplication and overlap tests pass on the current base.
  • The final changed-surface suite passes with 315 tests and 1 skip.

[ghstack-poisoned]
[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 29, 2026
@sanketpurandare sanketpurandare changed the title [mxfp8] Reuse materialized SimpleFSDP weights [HACKY DO NOT REVIEW][mxfp8] Reuse materialized SimpleFSDP weights Sep 29, 2026
[ghstack-poisoned]
Comment thread torchtitan/quantization/mxfp8/linear.py Resolved
…lized SimpleFSDP weights"


I reviewed this one-file fix (BAD HACK) and its existing MXFP8 and GraphPP validation; it preserves eager fused WGrad accumulation while removing duplicate SimpleFSDP materialization.

> The following technical description was prepared with Codex assistance and reviewed before posting.
>
> ### Summary
>
> `Linear.forward()` already materializes and flattens the weight before calling `MXFP8Linear._linear()`. Reading `self.weight` again is harmless for an ordinary module but reruns SimpleFSDP's dynamic parametrization, issuing a second all-gather and MXFP8 quantization for the same forward. Under GraphTrainer this also creates duplicate unshard chains whose extracted forward can receive an invalid scaled-mm operand.
>
> Reuse the supplied non-leaf `_UnshardedFSDPTensor` for SimpleFSDP and make_fx, where WGrad accumulation remains functional. Keep the module-attribute path for Dynamo and ordinary/FSDP2 eager execution, preserving leaf-owned fused WGrad accumulation and stacked-weight gradient shape.
>
> Fixes #4933.
>
> ### Test plan
>
> ```bash
> python -m pytest -q \
>   tests/unit_tests/gpu/test_mxfp8_fsdp.py \
>   tests/unit_tests/gpu/test_mxfp8_linear.py
> ```
>
> Result: `31 passed, 1 expected xfail`.
>
> ```bash
> python -m tests.integration_tests.run_tests \
>   /tmp/tt_graph_pp_distmoe_mxfp8_post_rebase_fix_v2_20260928 \
>   --test_suite b200 \
>   --test_name graph_trainer_dist_moe_mxfp8_fsdp_ep_pp_cudagraph \
>   --ngpu 4 --gpu_arch b200 --no-parallel
> ```
>
> Result: the four-rank PP2/FSDP2/EP2 run completed four steps with whole-step CUDA-graph capture and replay.

[ghstack-poisoned]
… weights"


I reviewed this one-file fix (BAD HACK) and its existing MXFP8 and GraphPP validation; it preserves eager fused WGrad accumulation while removing duplicate SimpleFSDP materialization.

> The following technical description was prepared with Codex assistance and reviewed before posting.
>
> ### Summary
>
> `Linear.forward()` already materializes and flattens the weight before calling `MXFP8Linear._linear()`. Reading `self.weight` again is harmless for an ordinary module but reruns SimpleFSDP's dynamic parametrization, issuing a second all-gather and MXFP8 quantization for the same forward. Under GraphTrainer this also creates duplicate unshard chains whose extracted forward can receive an invalid scaled-mm operand.
>
> Reuse the supplied non-leaf `_UnshardedFSDPTensor` for SimpleFSDP and make_fx, where WGrad accumulation remains functional. Keep the module-attribute path for Dynamo and ordinary/FSDP2 eager execution, preserving leaf-owned fused WGrad accumulation and stacked-weight gradient shape.
>
> Fixes #4933.
>
> ### Test plan
>
> ```bash
> python -m pytest -q \
>   tests/unit_tests/gpu/test_mxfp8_fsdp.py \
>   tests/unit_tests/gpu/test_mxfp8_linear.py
> ```
>
> Result: `31 passed, 1 expected xfail`.
>
> ```bash
> python -m tests.integration_tests.run_tests \
>   /tmp/tt_graph_pp_distmoe_mxfp8_post_rebase_fix_v2_20260928 \
>   --test_suite b200 \
>   --test_name graph_trainer_dist_moe_mxfp8_fsdp_ep_pp_cudagraph \
>   --ngpu 4 --gpu_arch b200 --no-parallel
> ```
>
> Result: the four-rank PP2/FSDP2/EP2 run completed four steps with whole-step CUDA-graph capture and replay.

[ghstack-poisoned]
@sanketpurandare sanketpurandare changed the title [HACKY DO NOT REVIEW][mxfp8] Reuse materialized SimpleFSDP weights [graph_trainer] Prefer live SimpleFSDP unshard chains Sep 30, 2026
@sanketpurandare
sanketpurandare marked this pull request as ready for review September 30, 2026 03:40
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 30, 2026
@aditvenk

Copy link
Copy Markdown
Contributor

This is now a dupe of #4966

@SherlockNoMad

Copy link
Copy Markdown
Contributor

#4949 is a better root fix.

IvanKobzarev pushed a commit that referenced this pull request Oct 1, 2026
Summary:
SimpleFSDP intentionally materializes a parameter on each property access, so tracing may produce duplicate annotated all-gather and post-gather preparation chains for one parameter. GraphTrainer already canonicalizes those chains before partitioning and FSDP extraction.

Prefer an unshard endpoint that still has consumers when choosing the canonical chain. Dead-code elimination removes an unused preparation tail but retains its effectful collective; choosing that dead raw endpoint can otherwise replace a live prepared MXFP8 value with a BF16 gathered tensor. Remove dead duplicate chains without substituting their unused endpoints.

This keeps SimpleFSDP functional and compiler-safe. It adds no module hooks, Python cache state, MXFP8 execution-mode branches, or changes to eager FSDP behavior.

Test Plan:
- `python -m pytest -q 'torchtitan/experiments/graph_trainer/tests/test_passes.py::TestFSDPUnshardDedupPass' 'torchtitan/experiments/graph_trainer/tests/test_passes.py::TestReassignCollectivePgsPass::test_overlap_preserves_total_ag_count' 'torchtitan/experiments/graph_trainer/tests/test_passes.py::TestReassignCollectivePgsPass::test_overlap_rewrites_ag_nodes' 'torchtitan/experiments/graph_trainer/tests/test_passes.py::TestReassignCollectivePgsPass::test_overlap_rewrites_multiple_pgs'` (`6 passed`).
- Four-rank MXFP8 GraphPP with PP2/FSDP2/EP2 and Interleaved1F1B completed four steps with CUDA-graph capture/replay; the pass reported 7 and 14 canonicalized duplicate chains for the local stages.

ghstack-source-id: 1ef78ef
Pull Request resolved: #4934
[ghstack-poisoned]
[ghstack-poisoned]
@sanketpurandare

Copy link
Copy Markdown
Contributor Author

#4949 is a better root fix.

@SherlockNoMad and @aditvenk #4949 doesn't entirely address the issue. I get the same issue in the dist-moe PR integration above

[ghstack-poisoned]
[ghstack-poisoned]
@sanketpurandare
sanketpurandare changed the base branch from gh/sanketpurandare/47/base to main October 2, 2026 17:25
@sanketpurandare
sanketpurandare merged commit 0a3e5e0 into main Oct 2, 2026
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Oct 2, 2026
Summary:
SimpleFSDP intentionally materializes a parameter on each property access, so tracing may produce duplicate annotated all-gather and post-gather preparation chains for one parameter. GraphTrainer already canonicalizes those chains before partitioning and FSDP extraction.

Prefer an unshard endpoint that still has consumers when choosing the canonical chain. Dead-code elimination removes an unused preparation tail but retains its effectful collective; choosing that dead raw endpoint can otherwise replace a live prepared MXFP8 value with a BF16 gathered tensor. Remove dead duplicate chains without substituting their unused endpoints.

This keeps SimpleFSDP functional and compiler-safe. It adds no module hooks, Python cache state, MXFP8 execution-mode branches, or changes to eager FSDP behavior.

Test Plan:
- The six focused FSDP unshard-deduplication and overlap tests pass on the current base.
- The final changed-surface suite passes with 315 tests and 1 skip.

ghstack-source-id: 1638b10
Pull Request resolved: pytorch/torchtitan#4934
AlbedoWang added a commit to AlbedoWang/torchtitan that referenced this pull request Oct 7, 2026
…torch#4934)

Backport of upstream torchtitan 0a3e5e0 (main) without chunk ownership
handling, which this base does not have (590624c not present).

deduplicate_fsdp_unshard_chains_pass picked unshard_outputs[0] as canonical.
For Shard(1) expert params (efsdp*ep > num_experts), the first chain's
post-gather chunk/cat tail is dead after DCE, so its raw wait_tensor was
substituted for the live reconstructed value and _grouped_mm saw a
(8, 352, 2048) gathered shard instead of (2, 1408, 2048).
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 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.

[mxfp8] SimpleFSDP materializes and quantizes weights twice per forward (in-place fused wgrad accumulation)

3 participants