Repository navigation
[graph_trainer][aot_fx_trace][graph_pp] Add GraphPP runner integration - #3089
Conversation
08f80d7 to
8649738
Compare
66cdfee to
7d4629c
Compare
7d4629c to
2a062b6
Compare
2a062b6 to
361141c
Compare
361141c to
bfc1f6a
Compare
bfc1f6a to
f94c774
Compare
e6c5096 to
8a9c40a
Compare
stack-info: PR: #3089, branch: sanketpurandare/stack/8
f94c774 to
87b7453
Compare
87b7453 to
35d77c7
Compare
Add the GraphPP pipeline entry point, GraphPipelineStage wrapper, and GraphPPRunner runtime that executes prebuilt stage-local graph bundles for PyTorch runtime PP schedule actions. Graph construction uses eager PP metadata inference, GraphTrainer tracing, the metadata-preserving compile-time pass pipeline without terminal Inductor compilation, and the GraphPP partition/FSDP/dI-dW extraction passes. Reuse GraphTrainer tracer subclass wrapping for values crossing PP boundaries, keep internal GraphPP ABI values flat, preserve trace-time buffer state, and share explicit parameter-gradient accumulation helpers with the non-PP path. Add runner unit coverage for graph construction, forward/backward execution, FSDP unshard/reduce actions, metadata contracts, compile hooks, and supported runtime schedule validation. stack-info: PR: pytorch/torchtitan#3089, branch: sanketpurandare/stack/8
b06be82 to
01df919
Compare
| _validate_graph_pp_config, | ||
| ) | ||
|
|
||
| from torchtitan.experiments.graph_trainer.graph_pp.runner import ( |
There was a problem hiding this comment.
Can we have an agent to vet that this test is covering sufficient cases of the Graph PP runtime state machine?
For e.g., I don't see a unit test for the case dW falls back to full backward.
I suspect there may be more.
There was a problem hiding this comment.
This happens automatically for all the first stages since the input to the first stage do no require grads so in ZeroBubble and DualPipeV dW will fall back to fullBW, if it doesn't it won't work at all. But I agree with the general direction, will post a comment on the PR about test coverage generated using an agent.
There was a problem hiding this comment.
GraphPP Test Coverage Review Note
This stack has deliberately layered GraphPP coverage. The tests are not a large
synthetic matrix; they are split across the invariants that matter for review:
graph transformation correctness, runner calling conventions, distributed
numerics, and full-Inductor runtime coverage.
Summary
The current GraphPP stack is covered by:
| Layer | Test file | Coverage |
|---|---|---|
| Graph partitioning | torchtitan/experiments/graph_trainer/tests/test_graph_pp_passes.py |
Real DSV3 MoE decoder block with FlexAttention, plus partition invariant failures |
| dI/dW backward split | torchtitan/experiments/graph_trainer/tests/test_graph_pp_passes.py |
Real DSV3 MoE backward graph replayed through split dI and dW graphs |
| FSDP collective split | torchtitan/experiments/graph_trainer/tests/test_graph_pp_passes.py |
Forward unshard extraction, backward reduce-grad extraction, malformed collective chains, no-FSDP no-op paths |
| FSDP memory policy interaction | torchtitan/experiments/graph_trainer/tests/test_passes.py |
reshard_after_forward policy saves the helper-selected FSDP unshard boundary |
| GraphPP graph builder and runner contracts | torchtitan/experiments/graph_trainer/tests/test_graph_pp_runner.py |
26 focused tests for tracing, graph building, calling conventions, loss handling, block masks, overlap graphs, and runtime state |
| Distributed numerics | torchtitan/experiments/graph_trainer/tests/test_numerics.py |
Exact loss comparison for DSV3 MoE + FlexAttention + EP + DP-shard + PP across GraphPP schedules |
| Full-Inductor integration | torchtitan/experiments/graph_trainer/tests/integration_tests.py |
8-GPU DSV3 GraphPP full-Inductor runs for Interleaved1F1B, ZBVZeroBubble, and DualPipeV |
The highest-risk graph passes are not tested only with toy graphs. Partitioning and dI/dW splitting both exercise a real DeepSeek V3 MoE decoder block using FlexAttention and the real block-mask tracing path.
Commit-by-Commit Coverage
| Commit | Main tests added | What the tests prove |
|---|---|---|
[graph_pp] Add stage graph partitioning |
test_graph_pp_passes.py +386 lines |
Partitioning preserves the real joint graph semantics for a DSV3 MoE FlexAttention block; pass-through saved values are preserved; invalid backward-only inputs fail loudly. |
[graph_pp] Add dI/dW backward splitting |
test_graph_pp_passes.py +61 lines |
Splitting full backward into input-grad and weight-grad graphs reconstructs the original backward outputs on a real DSV3 MoE graph. |
[graph_pp] Add FSDP collective splitting |
test_graph_pp_passes.py +208 lines, test_passes.py +59 lines |
FSDP unshard and reduce-grad collectives are extracted into separate graphs; malformed chains are rejected; no-FSDP paths remain no-op; default memory policy handles reshard_after_forward correctly. |
[graph_pp] Add GraphPP runner integration |
test_graph_pp_runner.py +693 lines, integration/numerics additions |
GraphPP graph construction avoids real eager pre-trace forward/loss, preserves buffers, handles loss kwargs, builds callable graphs, and integrates with distributed DSV3 loss comparison. |
[graph_pp] Add multiplexed graph coverage and DualPipeV validation |
test_graph_pp_runner.py +312 lines, test_numerics.py +150 lines, integration updates |
Overlap FW/BW multiplex graphs are built, cached, metadata-correct, and validated through DSV3 numerics and full-Inductor integration for ZBV/DualPipeV. |
[graph_pp] Add DualPipeV scheduling placeholder |
minimal runner test update | Keeps placeholder scheduling validation in the stack without broad test churn. |
Pass-Level Coverage
Partition Pass
GraphPPPartitionTest has five tests:
test_real_dsv3_moe_block_partition_matches_joint_graphtest_partition_saves_backward_passthrough_placeholderstest_invalid_backward_only_input_indices_raisetest_backward_only_input_required_by_forward_raisestest_forward_mutation_of_backward_only_input_raises
The real DSV3 test traces a MoE decoder block with FlexAttention and then checks:
- The forward partition returns the same user forward output as the original
joint graph. - The backward partition consumes saved forward values plus output grads and
reconstructs the original joint backward outputs. - The metadata counts line up with the calling convention: user forward outputs,
saved values for backward, side-effect outputs, backward grad inputs, and
backward outputs.
The synthetic partition tests are intentionally small. They cover invariants
that would otherwise fail silently:
- duplicate or out-of-range backward-only input indices,
- backward-only inputs accidentally required by the forward graph,
- mutation of a backward-only input by the forward graph,
- pass-through values such as DTensor layout metadata that are needed by
backward even though they are not user-visible forward outputs.
dI/dW Split
GraphPPSplitDiDwTest has two tests:
test_real_dsv3_moe_block_split_reconstructs_backwardtest_real_dsv3_moe_block_without_input_grad_skips_split
The first test starts from the real partitioned DSV3 MoE backward graph, applies
the dI/dW split, and verifies:
- The dI graph produces the same input grads as the full backward graph.
- The dI graph forwards the live values needed by dW.
- The dW graph produces the same parameter grads as the full backward graph.
The second test covers the first-stage calling convention: if the stage does not
produce input grads, dI/dW splitting is correctly skipped.
FSDP Collective Split
GraphPPFSDPCollectiveSplitTest has five tests:
test_forward_split_extracts_unshard_and_replicated_paramstest_forward_split_no_fsdp_is_nooptest_forward_split_requires_wait_after_all_gathertest_backward_split_extracts_reduce_grad_epiloguestest_backward_split_no_fsdp_is_noop_and_validates_grad_count
These tests cover the pass contracts directly:
- Forward FSDP all-gather/wait/reconstruction chains are moved into an
explicit unshard graph. - Replicated params remain on the correct calling convention.
- Malformed all-gather chains fail loudly when the expected wait is missing.
- Backward reduce-grad epilogues are separated from the main backward graph.
- Both reduce-scatter and all-reduce collectives are handled in the reduce-grad
extraction path. - No-FSDP graphs remain no-op instead of being rewritten unnecessarily.
There is also targeted coverage in test_passes.py that the default memory
policy saves the FSDP unshard boundary when reshard_after_forward semantics
require it. This matters because GraphPP's FSDP splitting and graph trainer's
standard FSDP memory policy must agree on the same unshard boundary.
Runner and Graph Builder Coverage
GraphPPRunnerTraceTest has 26 tests. The important coverage groups are:
Tracing and Graph Construction
- Non-last and last stage graph builds do not run a real eager pre-trace
forward. - Last-stage graph build does not run a real eager pre-trace loss.
- The graph builder relies on fakeification from the minimal FX tracer instead
of doing an eager forward just to infer output structure. - Stage graph construction preserves module buffer mutations.
- GraphPP node metadata is annotated for TLParse and debugging.
Calling Convention and Loss Handling
- Non-last stage graphs match eager gradients.
- Last-stage graphs return scalar loss and input grads correctly.
- Chunked loss preserves the hidden-state grad accumulator and checks exact
tensor equality in the local test. - Loss kwargs, such as
global_valid_tokens, are passed through correctly. - Last-stage forward leaves loss ownership to upstream PP loss handling.
Runtime State
- Only trainable params get accumulated grads.
- Upstream PP owns microbatch splitting; GraphPP does not wrap or replace
upstream split inputs. eval()forwards to the schedule and clears GraphPP runtime state.- GraphPP validates unsupported schedule shape early.
- Runtime graph readiness errors are explicit.
BlockMask Handling
- Split BlockMask batch offsets remain dynamic for replay.
- Existing stage graphs normalize split BlockMasks in place before graph
construction.
This directly targets FlexAttention replay risk: GraphPP should not specialize
the graph to microbatch 0's batch-offset closure.
Overlap and Multiplexed Graphs
- Existing overlap graphs are reused instead of rebuilt.
- Full-Inductor overlap builds multiplexed FW/BW graphs.
- Unsupported overlap backward-input subactions fail explicitly.
- Shape environment metadata is transferred into the forward fake mode.
- Multiplexed graph outputs are ordered as backward outputs followed by forward
outputs. - Multiplexed overlap graphs are prebuilt for overlap actions.
These tests are focused on the highest-risk runner feature: DualPipeV and
overlapped FW/BW execution need the runner to call the multiplexed graph with a
precise convention and output split.
Distributed Numerics Coverage
test_graph_pp_moe_dsv3_aot_fx_trace_vs_eager runs exact loss comparison for:
- model:
graph_trainer.deepseek_v3, - config: DSV3 debug model,
- attention: FlexAttention,
- pipeline parallel degree: 2,
- data parallel shard degree: 4,
- expert parallel degree: 2,
- local batch size: 8,
- global batch size: 32,
- GraphPP schedules:
Interleaved1F1B,ZBVZeroBubble,DualPipeV.
The baseline is eager PP Interleaved1F1B. This is intentional:
- FlexAttention needs
torch.compile. - Eager PP cannot use
ZBVZeroBubbleorDualPipeVwithtorch.compile. - Therefore the meaningful comparison is GraphPP schedule execution against the
eager Interleaved1F1B PP baseline.
The test disables gradient clipping with --training.max_norm=inf. This is not
a GraphPP workaround. TorchTitan clipping is applied per local rank, so different
pipeline schedules can produce different local clipping coefficients even when
the pre-clip gradients are bitwise equal. Disabling clipping isolates the graph
execution numerics from that schedule-level clipping effect.
This is the strongest end-to-end correctness coverage in the stack because it
tests the actual distributed mode reviewers care about: DSV3 MoE with
FlexAttention, EP, DP shard, PP, graph tracing, and GraphPP schedule execution.
Full-Inductor Integration Coverage
The integration test matrix includes 8-GPU full-Inductor GraphPP runs for:
- DSV3 GraphPP
Interleaved1F1B, - DSV3 GraphPP
ZBVZeroBubble, - DSV3 GraphPP
DualPipeV.
Each uses:
--module graph_trainer.deepseek_v3,--config graph_trainer_deepseek_v3_debugmodel,--compile.mode aot_fx_trace,--compile.inductor_compilation full,--parallelism.pipeline_parallel_degree 2,--parallelism.data_parallel_shard_degree 4,--parallelism.expert_parallel_degree 2.
The distributed numerics test uses regional Inductor for exact loss comparison,
while the integration tests use full Inductor for runtime coverage. This split is
intentional: exact numerics catches semantic regressions; full-Inductor
integration catches backend/runtime integration failures.
Why This Coverage Is Meaningful
The stack does not rely on shallow smoke tests alone.
- The graph partition and dI/dW passes are replayed against the original joint
graph, including a real DSV3 MoE FlexAttention trace. - The runner tests assert the GraphPP calling convention directly, including
last-stage loss behavior, saved backward values, input grads, param grads,
chunked loss, and overlap graph output ordering. - Distributed numerics compares actual training losses for DSV3 MoE with EP,
DP-shard, PP, and FlexAttention across all GraphPP schedules in the stack. - Full-Inductor integration separately verifies the final compiled runtime path
for those schedules. - Error-path tests check that invalid graph-pass invariants fail loudly instead
of being silently skipped.
Known Bounded Gaps
There are still some gaps, but they are bounded and do not invalidate the current
coverage story:
- The runner unit tests use local/fake schedules for focused contract coverage.
End-to-end schedule execution is covered by distributed numerics and
integration tests rather than a small unit-level_PipelineScheduleRuntime
replay test. - CUDA graph capture itself is not directly tested in this stack. The current
stack makes GraphPP graph execution structured enough for future capture work;
capture/replay tests should be added with the actual CUDA graph enablement. - Exact numerics currently uses regional Inductor, while full Inductor is
covered by integration smoke tests. A full-Inductor exact numerics test could
be added later if test runtime is acceptable.
These gaps are appropriate for the current stack. The tests added here cover the
core semantic risk: graph transformations preserve the original graph behavior,
the runner uses the intended calling convention, and distributed DSV3 GraphPP
matches eager PP numerics for the supported schedules.
Reviewer-Facing Test Plan
The concise test plan for the stack is:
pytest torchtitan/experiments/graph_trainer/tests/test_graph_pp_passes.py -q
pytest torchtitan/experiments/graph_trainer/tests/test_graph_pp_runner.py -q
pytest torchtitan/experiments/graph_trainer/tests/test_numerics.py -k graph_pp_moe_dsv3_aot_fx_trace_vs_eager -qThe integration matrix additionally includes:
aot_fx_trace deepseek_v3 GraphPP Interleaved1F1B full_inductoraot_fx_trace deepseek_v3 GraphPP ZBVZeroBubble full_inductoraot_fx_trace deepseek_v3 GraphPP DualPipeV full_inductor
Bottom Line
The coverage is strong where this stack is risky:
- real DSV3 MoE FlexAttention graph pass coverage,
- exact distributed DSV3 GraphPP numerics,
- full-Inductor runtime integration,
- explicit runner calling-convention tests,
- explicit invariant/error-path tests.
35d8e14 to
e77a8ef
Compare
01df919 to
bdc5a07
Compare
4ff54c8 to
117b4b9
Compare
e77a8ef to
a6b658b
Compare
aditvenk
left a comment
There was a problem hiding this comment.
I have tried to be thorough in places where we are interfacing wiht larger graph trainer. The inner details of pipeline/runner -- I tried to mentally map with RFC and have not reviewed as thoroughly.
| from torch._subclasses.fake_tensor import FakeTensor | ||
|
|
||
| if serializable and boxed_codegen: | ||
| raise ValueError( |
There was a problem hiding this comment.
Few questions:
-
Is boxed_codegen expected to be a win even without PP?
-
Will this restriction be removed once Precompile support for PP is enabled?
cc: @bobrenjc93
There was a problem hiding this comment.
- I don't think so there is any with for boxed_codegen without PP since inputs of non-PP graphs are params and user inputs and outputs are loss and grads, all need to be retained.
- Precompile and save with boxed codegen disabled and can do boxed_codegen at runtime, I guess the boxed_codegen is a very lightweight pass?
Stacked PRs:
[graph_trainer][aot_fx_trace][graph_pp] Add GraphPP runner integration
Add the GraphPP pipeline entry point, GraphPipelineStage wrapper, and GraphPipelineRuntime schedule wrapper that executes prebuilt stage-local graph executors for PyTorch runtime PP schedule actions. Graph construction uses upstream PP metadata initialization, GraphTrainer tracing, the metadata-preserving compile-time pass pipeline without terminal Inductor compilation, and the GraphPP partition/FSDP/dI-dW extraction passes.
Reuse GraphTrainer tracer subclass wrapping for values crossing PP boundaries, keep internal GraphPP calling-convention values flat, avoid eager pre-trace forwards, normalize PP-split BlockMask inputs, and share boxed graph execution plus explicit parameter-gradient accumulation helpers with the non-PP path. OVERLAP_F_B is registered but intentionally unsupported until the multiplex/DualPipeV follow-up commit.
Add runner unit coverage for graph construction, forward/backward execution, FSDP unshard/reduce actions, BlockMask handling, metadata contracts, compile hooks, and supported runtime schedule validation. Add DeepSeek V3 GraphPP numerics and integration coverage for Interleaved1F1B and ZBVZeroBubble.