Repository navigation
[graph_trainer] Split graph_builder.py by execution path - #5000
Merged
Merged
Conversation
SherlockNoMad
requested review from
IvanKobzarev,
aditvenk,
sanketpurandare and
tianyu-l
as code owners
October 1, 2026 19:25
SherlockNoMad
force-pushed
the
bahuang/graph_trainer_split_graph_builder
branch
from
October 1, 2026 21:31
d39bd22 to
30b5835
Compare
aditvenk
approved these changes
Oct 1, 2026
SherlockNoMad
force-pushed
the
bahuang/graph_trainer_spmd_path_terms
branch
from
October 2, 2026 19:51
1b04cb9 to
b7b2710
Compare
SherlockNoMad
changed the base branch from
bahuang/graph_trainer_spmd_path_terms
to
main
October 2, 2026 19:56
SherlockNoMad
added a commit
that referenced
this pull request
Oct 2, 2026
Rebased directly onto `main`; no longer depends on the `graph_builder.py` split (#5000), so all changes are in `graph_builder.py`. ## Why `_annotate_graph_pp_graph` wrote `graph_pp_stage_index`, `graph_pp_callable`, `graph_pp_action`, and `graph_pp_slot` into `node.meta` of every GraphPP and SPMD gradient-accumulation graph. It was added in #3089 ("GraphPP node metadata is annotated for TLParse and debugging"), but nothing ever consumed it: - **No reader anywhere.** `git log --all -S` for each key finds only the writers and a unit test asserting them; no reader existed at any point. Upstream PyTorch has no references either. - **TLParse never showed them.** TLParse graph dumps use `gm.print_readable()`, which only prints extra `node.meta` keys listed in `additional_meta` (`torch/fx/graph.py`). `trace_graph_pp_graph` passes none, and `debug_utils` only lists `autograd_backward` / `seq_nr`. ## What - Remove `_annotate_graph_pp_graph` and `_annotate_graph_pp_modules`. - Drop the `annotate_graph` parameter from `stage_builder._build_graph_pp_overlap_graphs`. - Rename `_annotate_and_compile_scheduled_fwd_bwd_graphs` -> `_compile_scheduled_fwd_bwd_graphs`. Compiled graph names (visible in TLParse) are unchanged. - Remove `test_graph_pp_node_metadata_is_annotated`, the only consumer of the keys. Net: -84 / +3. No behavior change. ## Tests - Before rebasing onto `main` (same change applied to the unsplit `graph_builder.py`): `test_graph_pp_runner.py`, `test_graph_pp_passes.py`, `test_trace_module.py` -> 161 passed, 3 skipped (one GPU-contention flake in `test_peak_memory_identical_fsdp` passed on rerun). - After rebasing onto `main`: pre-commit and pyflakes clean. Unit tests not re-run locally (current `main` needs a newer PyTorch than the local build for `WAIT_REDUCE_GRAD`); relying on CI. - Merges cleanly with Sanket's Dist-MoE stack (#4541, #4542, #4543), none of which use the removed annotation API.
graph_builder.py had grown to ~2650 lines mixing the stage-graph provider, shared helpers, SPMD (with and without gradient accumulation) graph construction, and PP stage graph construction. Split it along the same execution paths used elsewhere: - graph_builder.py: GraphExecutionPlan, placement types, GraphTrainerStageGraphProvider, and the provider's Dist-MoE slot lookup - graph_builder_utils.py: GraphTrainerConfigView, FSDP bucketing helpers, and graph execute/pack/compile helpers shared by both paths - spmd_graph_builder.py: SPMD without and with gradient accumulation - graph_pp/pp_graph_builder.py: PP stage graphs, partitioning, overlap graphs, and the Dist-MoE activation-slot rewrite used by PP stages Pure code move: every top-level definition is AST-identical to before, except spmd_graph_builder quotes its GraphExecutionPlan annotations and imports it under TYPE_CHECKING to avoid an import cycle with graph_builder. Each new module keeps graph_builder.py's `from __future__ import annotations`. Callers and mock.patch targets are updated to the new modules.
SherlockNoMad
force-pushed
the
bahuang/graph_trainer_split_graph_builder
branch
from
October 5, 2026 16:59
30b5835 to
7f7675e
Compare
…ts own module spmd_graph_builder.py was ~70% gradient-accumulation code (call specs, the scheduled executor, variant extraction, accumulation, edge-graph bucketing), so a reader of the simple SPMD path had to read through all of it. Move that code into spmd_gradient_accumulation_graph_builder.py, which builds on spmd_graph_builder.py: - spmd_graph_builder.py (~360 lines): make_fwd_bwd_step, joint-graph tracing shared by both SPMD paths, and the SPMD without gradient accumulation executor and builder. - spmd_gradient_accumulation_graph_builder.py (~810 lines): the first / middle / last variants, their executor, and the builder. 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 part becomes _trace_spmd_stage_graph, shared by both builders; _build_fwd_bwd_graphs now builds only SPMD without gradient accumulation, and the new _build_gradient_accumulation_fwd_bwd_graphs builds the GA path. GraphTrainerStageGraphProvider picks between them, as it already does for PP. Keeping the branch in spmd_graph_builder.py would have made it import the new module that imports it.
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.
Regenerated on current
main(the earlier version predated #4999, #5001, #5003, #5035, #5038 and the Dist-MoE adapter #4541, which all touchedgraph_builder.py).Why
graph_builder.pyhad 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
graph_builder.py(~290 lines)GraphExecutionPlan, placement types,GraphTrainerStageGraphProvider, the provider's Dist-MoE slot lookupgraph_builder_utils.py(~310)GraphTrainerConfigView, FSDP bucketing helpers, graph execute/pack/compile helpers shared by both pathsspmd_graph_builder.py(~360)spmd_gradient_accumulation_graph_builder.py(~810)graph_pp/pp_graph_builder.py(~1020)Pure code move. All 45 top-level definitions are AST-identical to
main(checked by script), except:spmd_graph_builder.pyimportsGraphExecutionPlanunderTYPE_CHECKINGand quotes its annotations, to avoid an import cycle withgraph_builder.py.logger, and keepsgraph_builder.py'sfrom __future__ import annotations(needed for theTYPE_CHECKING-only Dist-MoE annotations).Callers (
pipeline.py,precompile_main.py, tests) andmock.patchtargets 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_graphsused to trace and then branch onplan.has_gradient_accumulation; its tracing becomes_trace_spmd_stage_graph(shared),_build_fwd_bwd_graphsbuilds only SPMD without gradient accumulation, the new_build_gradient_accumulation_fwd_bwd_graphsbuilds the GA path, andGraphTrainerStageGraphProviderpicks between them as it already does for PP. The new module imports fromspmd_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 unmodifiedmain:test_dist_moe_slot_is_an_explicit_stage_graph_input:dist_moepackage not installed locally.test_initializes_one_stage_pipeline_runtime: itsSimpleNamespaceconfig lacks the newdist_moefield read attrainer.py:108.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/trainerimport check passes.