Skip to content

[graph_trainer] Group SPMD gradient accumulation settings into one config - #5005

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

SherlockNoMad merged 1 commit into
mainfrom
bahuang/graph_trainer_spmd_ga_config

Conversation

@SherlockNoMad

Copy link
Copy Markdown
Contributor

Replaces #4997, which was merged into its stacked base branch (bahuang/graph_trainer_single_config) instead of main. Rebased onto main now that #4983 has landed; same single commit.

Why

  • fsdp_param_unshard_mode, fsdp_gradient_sync_mode and gradient_accum_in_wgrad_fusion only make sense for users running SPMD (PP=1) with gradient accumulation.

  • This simplifies the mental model for SPMD with GA and for PP>1:

    • SPMD without GA: FSDP collectives always stay in the graph, so there is nothing to configure.
    • PP>1: FSDP collectives are always extracted as UNSHARD / REDUCE_GRAD schedule actions, so there is nothing to configure.

    Before this PR, every user saw these settings at the top level of GraphTrainerCompileConfig, with auto values and per-mode caveats like "PP>1: error".

What

  • New compile.spmd_gradient_accumulation: SPMDGradientAccumulationConfig with three fields:

    Old (compile.*) New (compile.spmd_gradient_accumulation.*) Values
    fsdp_param_unshard_mode fsdp_param_unshard_mode every_microbatch, first_microbatch (default), schedule
    fsdp_gradient_sync_mode fsdp_grad_reduce_mode every_microbatch, last_microbatch (default), schedule
    gradient_accum_in_wgrad_fusion fuse_wgrad_accumulation auto (default), disabled, enabled

    auto is gone from the FSDP modes. only_in_first_microbatch / only_in_last_microbatch become first_microbatch / last_microbatch, and extracted_in_schedule_stage / deferred_as_schedule_stage become schedule.

  • Ignored instead of raising: outside SPMD with GA (PP>1, or a single microbatch), non-default settings are ignored with a warning. The FSDP fields are also ignored when FSDP is disabled. Wgrad fusion still applies without FSDP.

  • Parameter reuse implies no resharding: first_microbatch and schedule unsharding reuse unsharded parameters across microbatches, so they now imply fsdp_reshard_after_forward=never for the compiled graphs. Before, the defaults raised an error with FSDP unless the user set that flag manually.

    • GraphExecutionPlan.reuse_unsharded_parameters becomes a derived property.
    • The runtime schedule adds one RESHARD at the end of the step.
    • Explicitly setting fsdp_reshard_after_forward="always" together with these modes logs a warning.
  • The FSDP wgrad-fusion error now says fsdp_grad_reduce_mode must be 'last_microbatch' or 'schedule'. Before, it named only 'schedule', though the check accepts both.

Behavior changes

  • Old field names: recipes that set the old compile.* fields need to move to compile.spmd_gradient_accumulation.*. No in-repo recipes used them other than the integration tests, which are updated here.
  • Errors become warnings: with PP>1 or a single microbatch, fuse_wgrad_accumulation="enabled" or non-default FSDP modes now log a warning instead of raising.
  • Integration jobs take a new code path: the CI integration jobs that use extracted (schedule) unsharding now reuse unsharded parameters across microbatches, instead of running UNSHARD/RESHARD per microbatch.

Test plan

  • test_graph_pp_runner.py passes (58 tests). New and updated tests cover:
    • parameter reuse forcing reshard_after_forward=never
    • the ignored-config warnings for PP>1, GA=1 and no FSDP
    • wgrad fusion without FSDP
    • the FSDP split-reduction error
  • Full graph_trainer unit suite: 444 passed, 24 failed. The same 24 fail on the base commit locally because of environment issues: cudnn disable_global_flags, missing autoparallel, and an Inductor config key. test_autoparallel_integration_matrix was excluded because torchvision isn't installed.
  • Not yet run: GPU integration tests and a loss comparison.
  • Gap: the default (first_microbatch, last_microbatch) FSDP path has no GPU integration coverage yet.
  • pre-commit clean (pyrefly skipped locally).
  • After rebasing onto main: test_graph_pp_runner.py and test_graph_pp_passes.py pass (105), pre-commit clean, and a 10-step 1-GPU SPMD GA run matches the pre-rebase loss (step 1 8.11272, step 10 4.03365).

…nfig

fsdp_param_unshard_mode, fsdp_gradient_sync_mode, and
gradient_accum_in_wgrad_fusion only make sense for SPMD (PP=1) runs with
gradient accumulation. They lived at the top level of
GraphTrainerCompileConfig with "auto" values and per-mode PP=1/PP>1
caveats, so every user had to reason about them.

Move them into compile.spmd_gradient_accumulation
(SPMDGradientAccumulationConfig):
- fsdp_param_unshard_mode: every_microbatch | first_microbatch | schedule
- fsdp_grad_reduce_mode: every_microbatch | last_microbatch | schedule
- fuse_wgrad_accumulation: auto | disabled | enabled

Outside SPMD with gradient accumulation the settings are ignored with a
warning instead of raising: a single PP=1 microbatch keeps FSDP
collectives in the joint graph, and PP>1 always extracts them as schedule
actions without WGrad fusion. The FSDP fields are also ignored without
FSDP. first_microbatch and schedule unsharding now imply
fsdp_reshard_after_forward=never for the compiled graphs, which makes the
default (first_microbatch, last_microbatch) work with FSDP out of the box.
@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 merged commit db050eb into main Oct 2, 2026
SherlockNoMad added a commit that referenced this pull request Oct 2, 2026
…tion (#5024)

Replaces #4998, whose stacked base branch was merged and can no longer
be retargeted. Rebased onto `main` now that #5005 (the SPMD gradient
accumulation config) has landed; same single commit.

## Why
`fsdp_param_unshard_mode="schedule"` /
`fsdp_grad_reduce_mode="schedule"` extracted FSDP all-gather /
reduce-scatter out of the PP=1 joint graph into standalone `UNSHARD` /
`REDUCE_GRAD` schedule actions. With PP=1 there is a single stage, so
tracking these collectives as separate schedule actions gives no
scheduling benefit over keeping them inside the first/last (or every)
microbatch graph -- but it required a lot of extra machinery (mixed
REPEAT computation types, extra extracted graphs, merge/compile paths,
runtime branches).

PP>1 is unchanged and still always uses scheduled `UNSHARD` /
`REDUCE_GRAD`.

## What
Remaining PP=1 + GA combinations:
- `(every_microbatch, every_microbatch)`: `FULL_FORWARD_BACKWARD` per
microbatch
- `(first_microbatch, last_microbatch)` (default): `FIRST_WITH_UNSHARD`,
`FORWARD_BACKWARD`, `LAST_WITH_REDUCE_GRAD`, then `RESHARD`
- Mixed combinations are rejected with a `ValueError`.

Removed:
- `FORWARD_BACKWARD_REPEAT_WITH_UNSHARD` /
`FORWARD_BACKWARD_REPEAT_WITH_REDUCE_GRAD`,
`joint_forward_backward_computation_type` and its lookup table
- PP=1 extraction of standalone UNSHARD / REDUCE_GRAD graphs, their
merge passes, annotation/compilation, `_compile_optional_graph`
- `GraphExecutionPlan.requires_graph_extraction` (replaced by
`has_gradient_accumulation`) and a duplicated precompile check
- PP=1 `UNSHARD` / `REDUCE_GRAD` schedule actions (schedule is now
per-microbatch actions + `RESHARD`)
- Runtime branch deciding whether grad reduction ran inside the joint
graph

Net: +99 / -419.

## Tests
- `test_graph_pp_runner.py`, `test_graph_pp_passes.py`,
`test_trace_module.py`: 162 passed, 3 skipped.
- Replaced the three `"schedule"` integration jobs with
`aot_fx_trace_llama3_ga_first_unshard_last_reduce_fsdp` (default FSDP GA
path had no coverage).
- Ran both remaining FSDP=4 + GA configs for 10 steps on 4 GPUs with
`seed=42`, `deterministic=True`; both modes produce identical
loss/grad_norm (stdout precision):

| mode | step 1 loss | step 10 loss | step 10 grad_norm |
|---|---|---|---|
| first/last | 8.14194 | 3.99857 | 1.9983 |
| every/every | 8.14194 | 3.99857 | 1.9983 |

**After rebasing onto `main`:** one import conflict in
`test_graph_pp_runner.py` (kept `WAIT_REDUCE_GRAD` from #4662, dropped
the now-unused `UNSHARD`). pre-commit and pyflakes are clean. The unit
tests above were run before the rebase; they could not be re-run locally
because current `main` imports `WAIT_REDUCE_GRAD` (#4662), which needs a
newer PyTorch than the local build. Relying on CI for the post-rebase
run.
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