Repository navigation
[spmd_types] TorchFT and Forge trainer compat - #4416
Merged
Merged
Conversation
[ghstack-poisoned]
This was referenced Sep 2, 2026
tianyu-l
approved these changes
Sep 2, 2026
[ghstack-poisoned]
pianpwk
added a commit
that referenced
this pull request
Sep 2, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0) (oldest at bottom): * #4419 * #4416 * __->__ #4325 ## human-written section Enables graph trainer for spmd_types backend - simple FSDP is kept in DTensor, we just unwrap to plain tensor params (spmd_types) for fwd/bwd. DTensor-less FSDP is doable and prototyped, but needs more orthogonal support (padding uneven params, DCP, grad norm clipping, etc.) - precompile in DTensor backend would (tensor subclass) unflatten DTensor inputs/params, accessing PGs for collectives from their DeviceMesh attributes. In spmd_types the PGs are from ambient state, and precompile graph patterns are different when the owning meshes aren't graph inputs (PGs become opaque objects). To match patterns & make bucketing happy, I preregister the meshes as graph inputs - generally tested through perf/numerics, not sure if any features missed - typechecking is WIP ## Changes - Reuse GraphTrainer's DTensor-backed SimpleFSDP implementation with an `spmd_types` model-parallel backend. - Preserve each parameter's `SpmdType` through FSDP wrapping and materialize replicated non-DP axes with `spmd.redistribute`. - Pass concrete mesh inputs through tracing and precompile so functional collectives resolve runtime process groups. ## Test environment | Item | Value | | --- | --- | | Test commit | This temporary commit | | Baseline | `main` (`partial_dtensor`) | | GPUs | 8x NVIDIA H100 80 GB | | Numerics | `--debug.seed=42 --debug.deterministic` | | Performance | 8-GPU sweep: 20 steps, discard 1-5, 3 repeats; Qwen control: 100 steps, discard 1-20 | ## Backend parity Both backends loaded the same seed checkpoint and ran with `--debug.seed=42 --debug.deterministic`. Full-precision TensorBoard values were compared rather than rounded stdout. | Model | Parallelism | Steps | Max loss diff | Max grad-norm diff | Result | | --- | --- | ---: | ---: | ---: | --- | | Llama3 | FSDP4 + TP2 | 10 | 0 | 0 | Bitwise equal | | Qwen3 MoE | FSDP4 + TP2 + EP4 | 10 | 0 | 0 | Bitwise equal | | DeepSeek V3 | FSDP4 + TP2 + EP4 | 10 | `6.198883e-05` | `1.537800e-04` | Small EP all-to-all variation | DeepSeek parity disabled the bucketing and CUDA-graph passes on both backends. The focused deterministic suite also completed with 16 passed and 9 skipped. ## Activation checkpointing Configuration: Llama3 debug model, 8 GPUs, FSDP4 + TP2, `aot_fx_trace`, regional Inductor, CUDA graphs disabled, 20 steps, and one run per backend. TPS is the rank-0 median over steps 6-20, and memory is the maximum reported peak across ranks. Only `compile.memory_policy` changed between rows. The `partial_dtensor` and `spmd_types` graphs made identical save/recompute decisions for every tested policy and every one of the six Llama layers. | Policy | Forced boundary saves | Non-layer save/recompute | Per-layer save/recompute | Partial TPS | SPMD TPS | Delta | Partial peak | SPMD peak | | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | | Default SAC | 6 | 2 / 21 | 13 / 79 | 130,058 | 132,557 | +1.92% | 0.45 GiB | 0.45 GiB | | Eager-equivalent SAC | 6 | 2 / 21 | 11 / 81 | 126,280 | 134,145 | +6.23% | 0.39 GiB | 0.39 GiB | | Full recompute | 7 | 1 / 22 | 1 / 91 | 129,144 | 127,320 | -1.41% | 0.24 GiB | 0.24 GiB | | SAC + CPU offload | 6 | 2 / 21 | 13 / 79 | 122,871 | 121,882 | -0.80% | 0.33 GiB | 0.33 GiB | CPU offload matched exactly as well: both backends tagged 35 activations (282.5 MB), offloaded 20 tensors (142.5 MB), replayed five views, and moved 20 reloads one layer earlier. ## Precompile Artifacts were generated independently for each backend, then loaded by a fresh 8-GPU process for a 20-step run. CUDA graphs were disabled so these numbers isolate the precompiled regional-Inductor path. TPS is the rank-0 median over steps 6-20; memory is the maximum reported peak across ranks. | Model | Parallelism | Partial TPS | Partial peak | SPMD TPS | SPMD peak | Delta | | --- | --- | ---: | ---: | ---: | ---: | ---: | | Llama3 SDPA | FSDP4 + TP2 | 110,019 | 0.45 GiB | 110,993 | 0.45 GiB | +0.89% | | Llama3 FlexAttention | FSDP4 + TP2 | 110,891 | 0.45 GiB | 108,441 | 0.45 GiB | -2.21% | | DeepSeek V3 | FSDP4 + TP2 + EP4 | Artifact generation fails | -- | 67,709 | 1.59 GiB | -- | DeepSeek used `--training.disable_cuda_graphs` because its default token dispatcher is rejected by CUDA-graph configuration validation. Its `partial_dtensor` artifact generation reaches the same `stable topological sort of region failed` limitation as the direct-trace default-bucketing path; `spmd_types` generates, loads, and runs the artifact. ## CUDA graphs CUDA graphs are not blocked for the `spmd_types` backend as a whole. They work on the precompiled Llama FlexAttention path: the artifact loaded, the pass logged `Applied cudagraph pass`, and training completed for 10 steps. Over steps 6-10, median throughput was 246,007 TPS with CUDA graphs versus 116,855 TPS from the same precompiled artifact with the pass disabled; peak memory was 0.46 versus 0.45 GiB. This is a single short run. The direct-trace Llama FSDP4 + TP2 path remains blocked from CUDA graph capture on both backends. The pass is invoked but logs `Skipping cudagraph: graph is not compatible`, so direct-trace runs with the pass enabled are not CUDA graph performance measurements. ## Inductor | Mode | Partial TPS | SPMD TPS | Delta | Partial memory | SPMD memory | | --- | ---: | ---: | ---: | ---: | ---: | | Regional | 130,058 | 132,557 | +1.92% | 0.45 GiB | 0.45 GiB | | Full | 181,628 | 187,040 | +2.98% | 0.48 GiB | 0.48 GiB | Both modes completed 20 steps. Full Inductor was the fastest tested direct trace configuration for both backends. ## FSDP SimpleFSDP unit coverage passed (`2 passed`). Llama FSDP4 + TP2 completed under both backends, and the optional AG/RS overlap pass found the same 22 overlap dependencies in each graph. | Configuration | Partial TPS | SPMD TPS | Delta | Partial peak | SPMD peak | | --- | ---: | ---: | ---: | ---: | ---: | | Regional Inductor, no CUDA graph | 130,058 | 132,557 | +1.92% | 0.45 GiB | 0.45 GiB | | Regional Inductor + FSDP AG/RS overlap | 131,768 | 136,092 | +3.28% | 0.45 GiB | 0.45 GiB | For DeepSeek FSDP4 + TP2 + EP4 with the default bucketing pass, `partial_dtensor` fails with `stable topological sort of region failed`, while `spmd_types` completes. This is a baseline limitation rather than an `spmd_types` blocker. ## DCP Llama FSDP4 + TP2 with `spmd_types` trained for 10 steps, saved a full DCP checkpoint at step 10, then loaded it in a fresh process and resumed at step 11 through step 12. Model, optimizer, scheduler, dataloader, and trainer state were restored through the normal checkpoint path. ## GraphPP Configuration: DeepSeek V3 debug model, 8 GPUs, FSDP4 + PP2 + EP2, TP1, eight pipeline microbatches, 2,048 tokens per microbatch per DP rank, full Inductor, CUDA graphs disabled, and 20 training steps. All three schedules completed under both backends. TPS is the rank-0 median over steps 6-20; memory is the maximum reported peak across ranks. | Schedule | Partial TPS | SPMD TPS | Delta | Partial peak | SPMD peak | | --- | ---: | ---: | ---: | ---: | ---: | | Interleaved1F1B | 40,897 | 42,180 | +3.14% | 0.69 GiB | 0.69 GiB | | ZBVZeroBubble | 42,952 | 40,724 | -5.19% | 0.81 GiB | 0.81 GiB | | DualPipeV | 42,752 | 42,255 | -1.16% | 0.87 GiB | 0.87 GiB | ## Other coverage | Test | Result | | --- | --- | | MuseGlimmer FSDP4 + TP2 integration | Pass, 10 steps, 0.65 GiB peak | | Supported MoE-level graph EP chunking | Pass | | Supported MoE-level eager EP chunking | Pass | | `test_bitwise_deterministic.py` | 16 passed, 9 skipped | | `test_simple_fsdp.py` | 2 passed | | Formatter, pydoclint, codespell, CI Pyrefly | Pass | | Standard DeepSeek FSDP + TP + EP | Both 10-step parity runs passed; the integration test remains disabled for a recorded intermittent all-to-all failure | The formatter-only changes requested by lint are included in `simple_fsdp.py`. A local all-files Pyrefly invocation is not representative of CI in this dirty workspace because it scans unrelated untracked scripts and generated `torch_compile_debug` files; the clean CI result supplied for this commit passes. ## Unsupported GraphTrainer configurations | Area | `partial_dtensor` | `spmd_types` | Scope | | --- | --- | --- | --- | | CP + FlexAttention | Rejected by GraphTrainer configuration validation | Rejected by GraphTrainer configuration validation | Unsupported by GraphTrainer | | Runtime SPMD typechecking | Flag rejected as unsupported | Flag rejected as unsupported | Unsupported by GraphTrainer | | Direct-trace CUDA graphs, Llama FSDP4 + TP2 | Compatibility gate skips capture | Compatibility gate skips capture | Shared | | MoE EP overlap + dense-region FSDP scheduling | Fails on alias users of reduce-scatter wait sinks | Same failure | Shared (`#4052`) | | Transformer-level EP chunking | Graph mode hits a CUDA device assertion | Same failure; eager mode also produces an invalid attention mask | Shared, obsolete configuration (`#4342`) | ## Reproduction commands Backend parity used `scripts/loss_compare.py` with 10 steps, the same seed checkpoint, and backend-specific `--parallelism.spmd_backend` values. The common Llama options were FSDP4 + TP2; Qwen and DeepSeek additionally used EP4. DeepSeek disabled `joint_transformer_block_bucketing_reordering_pass`. Feature performance used: ```bash NGPU=8 MODULE=graph_trainer.llama3 CONFIG=graph_trainer_llama3_debugmodel \ ./run_train.sh --training.steps 20 --metrics.log_freq 1 \ --compile.mode aot_fx_trace --training.disable_cuda_graphs \ --compile.disable_passes cudagraph_pass \ --parallelism.spmd_backend <partial_dtensor|spmd_types> \ --parallelism.data_parallel_shard_degree 4 \ --parallelism.tensor_parallel_degree 2 \ <feature options> ``` `<feature options>` was one of `--compile.memory_policy eager`, `--compile.memory_policy full`, `--compile.memory_policy sac_and_offload`, `--compile.inductor_compilation full`, or `--compile.enable_fsdp_ag_rs_overlap`. The paired GraphPP and precompile performance runs used the same options as their checked-in integration cases, with 20 steps and a separate launch or artifact for each backend.
[ghstack-poisoned]
[ghstack-poisoned]
pianpwk
marked this pull request as ready for review
September 3, 2026 17:12
submartingales
pushed a commit
to botcanlearn/torchtitan-upstream
that referenced
this pull request
Sep 3, 2026
ghstack-source-id: aafb88a Pull Request resolved: pytorch/torchtitan#4416
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack from ghstack (oldest at bottom):