Skip to content

[graph_trainer] Standardize on SPMD with/without gradient accumulation and PP - #4999

Merged
SherlockNoMad merged 1 commit into
mainfrom
bahuang/graph_trainer_spmd_path_terms
Oct 2, 2026
Merged

SherlockNoMad merged 1 commit into
mainfrom
bahuang/graph_trainer_spmd_path_terms

Conversation

@SherlockNoMad

@SherlockNoMad SherlockNoMad commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Rebased onto main after #5024 (dropping the SPMD "schedule" FSDP modes) landed.

Why

GraphTrainer comments, docstrings, and error messages referred to the execution paths inconsistently (PP=1, PP>1, "SPMD", "scheduled SPMD", "direct SPMD"), and several "SPMD" descriptions only applied to one of the two SPMD paths. This makes the code harder to follow now that SPMD with and without gradient accumulation use different executors.

What

Standardize on three named paths, defined once in make_graph_runtime:

  1. SPMD without gradient accumulation: no PP, one microbatch per step (GraphTrainerJointStageGraphs, single FULL_FORWARD_BACKWARD)
  2. SPMD with gradient accumulation: no PP, more than one microbatch (GraphTrainerScheduledFwdBwdStageGraphs)
  3. PP: pipeline parallelism

Fixes to comments that were inaccurate:

  • stage.py / runner.py claimed SPMD without gradient accumulation uses runtime-owned gradient slots; its reduced gradients go directly to param.grad. Only PP uses the slots.
  • FORWARD_BACKWARD_NOGRADACCUM was described as the SPMD graph "without gradient accumulation"; it is the first graph of SPMD with gradient accumulation.

Error/warning text now uses the same names (e.g. "SPMD with gradient accumulation does not support ..."). The PP=1 test helper is renamed to _make_test_spmd_plan.

No behavior change beyond message text.

Tests

  • test_graph_pp_runner.py, test_graph_pp_passes.py, test_trace_module.py: 162 passed, 3 skipped.
  • pre-commit clean.

@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 1, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 1, 2026
@SherlockNoMad
SherlockNoMad added this pull request to stack #5002 October 1, 2026 19:52
@SherlockNoMad
SherlockNoMad force-pushed the bahuang/graph_trainer_spmd_path_terms branch from b73a4e9 to 1b04cb9 Compare October 1, 2026 21:31
@SherlockNoMad
SherlockNoMad force-pushed the bahuang/graph_trainer_drop_spmd_schedule_mode branch from 78beb65 to 60decf6 Compare October 2, 2026 17:16
@SherlockNoMad
SherlockNoMad removed this pull request from stack #5002 October 2, 2026 19:47
…n and PP

GraphTrainer comments, docstrings, and error messages referred to the
execution paths inconsistently (PP=1, PP>1, SPMD, "scheduled SPMD",
"direct SPMD"), and several "SPMD" descriptions only applied to one of the
two SPMD paths. Standardize on three named paths:

1. SPMD without gradient accumulation: no PP, one microbatch per step
2. SPMD with gradient accumulation: no PP, more than one microbatch
3. PP: pipeline parallelism

Define them once in make_graph_runtime and use the same names everywhere.

Also fix comments that became inaccurate:
- stage.py / runner.py claimed SPMD without gradient accumulation uses
  runtime-owned gradient slots; its reduced gradients go directly to
  param.grad. Only PP uses the slots.
- FORWARD_BACKWARD_NOGRADACCUM was described as the SPMD graph "without
  gradient accumulation"; it is the first graph of SPMD with gradient
  accumulation.

Rename the PP=1 test helper to _make_test_spmd_plan. No behavior change
beyond error/warning message text.
@SherlockNoMad
SherlockNoMad force-pushed the bahuang/graph_trainer_spmd_path_terms branch from 1b04cb9 to b7b2710 Compare October 2, 2026 19:51
@SherlockNoMad
SherlockNoMad changed the base branch from bahuang/graph_trainer_drop_spmd_schedule_mode to main October 2, 2026 19:51
@SherlockNoMad
SherlockNoMad merged commit 73c78f4 into main Oct 2, 2026
SherlockNoMad added a commit that referenced this pull request Oct 5, 2026
Regenerated on current `main` (the earlier version predated #4999,
#5001, #5003, #5035, #5038 and the Dist-MoE adapter #4541, which all
touched `graph_builder.py`).

## Why
`graph_builder.py` had grown to ~2650 lines mixing the stage-graph
provider, shared helpers, SPMD graph construction (with and without
gradient accumulation), and PP stage graph construction. Splitting it
along the execution paths (SPMD / PP) makes each path readable on its
own.

## What
| File | Contents |
|---|---|
| `graph_builder.py` (~290 lines) | `GraphExecutionPlan`, placement
types, `GraphTrainerStageGraphProvider`, the provider's Dist-MoE slot
lookup |
| `graph_builder_utils.py` (~310) | `GraphTrainerConfigView`, FSDP
bucketing helpers, graph execute/pack/compile helpers shared by both
paths |
| `spmd_graph_builder.py` (~360) | joint-graph tracing shared by both
SPMD paths; SPMD without gradient accumulation executor and builder |
| `spmd_gradient_accumulation_graph_builder.py` (~810) | SPMD with
gradient accumulation: first / middle / last graphs, their executor,
accumulation, edge-graph bucketing |
| `graph_pp/pp_graph_builder.py` (~1020) | PP stage graphs,
partitioning, overlap graphs, the Dist-MoE activation-slot rewrite used
by PP stages |

**Pure code move.** All 45 top-level definitions are AST-identical to
`main` (checked by script), except:
- `spmd_graph_builder.py` imports `GraphExecutionPlan` under
`TYPE_CHECKING` and quotes its annotations, to avoid an import cycle
with `graph_builder.py`.
- Each file has only the imports it uses, its own `logger`, and keeps
`graph_builder.py`'s `from __future__ import annotations` (needed for
the `TYPE_CHECKING`-only Dist-MoE annotations).

Callers (`pipeline.py`, `precompile_main.py`, tests) and `mock.patch`
targets are updated to the new modules.

**Second commit** moves the SPMD gradient-accumulation code out of
`spmd_graph_builder.py` (it was ~70% of that file), so the simple SPMD
path reads on its own. The only non-move change is the dispatch:
`_build_fwd_bwd_graphs` used to trace and then branch on
`plan.has_gradient_accumulation`; its tracing becomes
`_trace_spmd_stage_graph` (shared), `_build_fwd_bwd_graphs` builds only
SPMD without gradient accumulation, the new
`_build_gradient_accumulation_fwd_bwd_graphs` builds the GA path, and
`GraphTrainerStageGraphProvider` picks between them as it already does
for PP. The new module imports from `spmd_graph_builder.py`, not the
reverse.

## Tests
- `test_graph_pp_runner.py`, `test_graph_pp_passes.py`,
`test_trace_module.py`: 169 passed, 4 skipped, 2 failed. Both failures
reproduce on unmodified `main`:
- `test_dist_moe_slot_is_an_explicit_stage_graph_input`: `dist_moe`
package not installed locally.
- `test_initializes_one_stage_pipeline_runtime`: its `SimpleNamespace`
config lacks the new `dist_moe` field read at `trainer.py:108`.
- Bitwise-identical loss and grad_norm vs. `main`, 10 steps, `seed=42`,
`deterministic=True` (`scripts/loss_compare.py --assert-equal`): SPMD GA
first/last (FSDP=4), SPMD GA without FSDP (1 GPU), SPMD without GA
(FSDP=4).
- `precompile_main` / `trainer` import check passes.
- pre-commit clean.
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.

2 participants