Repository navigation
[graph_trainer] Group SPMD gradient accumulation settings into one config - #5005
Merged
Merged
Conversation
…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.
SherlockNoMad
requested review from
IvanKobzarev,
aditvenk,
sanketpurandare and
tianyu-l
as code owners
October 1, 2026 21:40
aditvenk
approved these changes
Oct 1, 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.
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.
Replaces #4997, which was merged into its stacked base branch (
bahuang/graph_trainer_single_config) instead ofmain. Rebased ontomainnow that #4983 has landed; same single commit.Why
fsdp_param_unshard_mode,fsdp_gradient_sync_modeandgradient_accum_in_wgrad_fusiononly make sense for users running SPMD (PP=1) with gradient accumulation.This simplifies the mental model for SPMD with GA and for PP>1:
UNSHARD/REDUCE_GRADschedule actions, so there is nothing to configure.Before this PR, every user saw these settings at the top level of
GraphTrainerCompileConfig, withautovalues and per-mode caveats like "PP>1: error".What
New
compile.spmd_gradient_accumulation: SPMDGradientAccumulationConfigwith three fields:compile.*)compile.spmd_gradient_accumulation.*)fsdp_param_unshard_modefsdp_param_unshard_modeevery_microbatch,first_microbatch(default),schedulefsdp_gradient_sync_modefsdp_grad_reduce_modeevery_microbatch,last_microbatch(default),schedulegradient_accum_in_wgrad_fusionfuse_wgrad_accumulationauto(default),disabled,enabledautois gone from the FSDP modes.only_in_first_microbatch/only_in_last_microbatchbecomefirst_microbatch/last_microbatch, andextracted_in_schedule_stage/deferred_as_schedule_stagebecomeschedule.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_microbatchandscheduleunsharding reuse unsharded parameters across microbatches, so they now implyfsdp_reshard_after_forward=neverfor the compiled graphs. Before, the defaults raised an error with FSDP unless the user set that flag manually.GraphExecutionPlan.reuse_unsharded_parametersbecomes a derived property.RESHARDat the end of the step.fsdp_reshard_after_forward="always"together with these modes logs a warning.The FSDP wgrad-fusion error now says
fsdp_grad_reduce_modemust be'last_microbatch' or 'schedule'. Before, it named only'schedule', though the check accepts both.Behavior changes
compile.*fields need to move tocompile.spmd_gradient_accumulation.*. No in-repo recipes used them other than the integration tests, which are updated here.fuse_wgrad_accumulation="enabled"or non-default FSDP modes now log a warning instead of raising.schedule) unsharding now reuse unsharded parameters across microbatches, instead of runningUNSHARD/RESHARDper microbatch.Test plan
test_graph_pp_runner.pypasses (58 tests). New and updated tests cover:reshard_after_forward=neverdisable_global_flags, missingautoparallel, and an Inductor config key.test_autoparallel_integration_matrixwas excluded becausetorchvisionisn't installed.first_microbatch,last_microbatch) FSDP path has no GPU integration coverage yet.pre-commitclean (pyrefly skipped locally).main:test_graph_pp_runner.pyandtest_graph_pp_passes.pypass (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).