Skip to content

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

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

SherlockNoMad merged 1 commit into
bahuang/graph_trainer_single_configfrom
bahuang/graph_trainer_spmd_ga_config

Conversation

@SherlockNoMad

Copy link
Copy Markdown
Contributor

Stacked on #4983.

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).

…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.
@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 marked this pull request as ready for review October 1, 2026 18:21
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 1, 2026
@SherlockNoMad
SherlockNoMad added this pull request to stack #5002 October 1, 2026 19:52

@aditvenk aditvenk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@SherlockNoMad
SherlockNoMad merged commit 059b8e3 into bahuang/graph_trainer_single_config Oct 1, 2026
8 checks passed
SherlockNoMad added a commit that referenced this pull request Oct 2, 2026
…nfig (#5005)

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).
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