Skip to content

[graph_trainer][aot_fx_trace][graph_pp] Add GraphPP runner integration - #3089

Merged
sanketpurandare merged 1 commit into
mainfrom
sanketpurandare/stack/8
Jul 2, 2026
Merged

sanketpurandare merged 1 commit into
mainfrom
sanketpurandare/stack/8

Conversation

@sanketpurandare

@sanketpurandare sanketpurandare commented Apr 24, 2026 •

Copy link
Copy Markdown
Contributor

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.

@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/7 branch from 08f80d7 to 8649738 Compare April 24, 2026 19:53
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/8 branch from 66cdfee to 7d4629c Compare April 24, 2026 19:53
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Apr 24, 2026
@sanketpurandare
sanketpurandare changed the base branch from sanketpurandare/stack/7 to main April 30, 2026 18:15
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/8 branch from 7d4629c to 2a062b6 Compare April 30, 2026 18:15
@sanketpurandare
sanketpurandare changed the base branch from main to sanketpurandare/stack/7 April 30, 2026 18:16
@sanketpurandare
sanketpurandare changed the base branch from sanketpurandare/stack/7 to main April 30, 2026 18:41
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/8 branch from 2a062b6 to 361141c Compare April 30, 2026 18:41
@sanketpurandare
sanketpurandare changed the base branch from main to sanketpurandare/stack/7 April 30, 2026 18:41
@sanketpurandare
sanketpurandare changed the base branch from sanketpurandare/stack/7 to main May 4, 2026 03:05
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/8 branch from 361141c to bfc1f6a Compare May 4, 2026 03:05
@sanketpurandare sanketpurandare changed the title Wire graph PP as a unified path for both frontends [graph_trainer][aot_fx_trace][graph_pp] Add and Integrate GraphPP Runner May 4, 2026
@sanketpurandare
sanketpurandare changed the base branch from main to sanketpurandare/stack/7 May 4, 2026 03:06
@sanketpurandare
sanketpurandare changed the base branch from sanketpurandare/stack/7 to main May 4, 2026 04:38
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/8 branch from bfc1f6a to f94c774 Compare May 4, 2026 04:38
@sanketpurandare
sanketpurandare changed the base branch from main to sanketpurandare/stack/7 May 4, 2026 04:39
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/7 branch from e6c5096 to 8a9c40a Compare May 4, 2026 20:03
sanketpurandare added a commit that referenced this pull request May 4, 2026
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/8 branch from f94c774 to 87b7453 Compare May 4, 2026 20:03
@sanketpurandare
sanketpurandare changed the base branch from sanketpurandare/stack/7 to main May 8, 2026 00:37
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/8 branch from 87b7453 to 35d77c7 Compare May 8, 2026 00:37
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Jun 25, 2026
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
@sanketpurandare sanketpurandare linked an issue Jun 26, 2026 that may be closed by this pull request
@sanketpurandare
sanketpurandare marked this pull request as draft June 26, 2026 07:30
@sanketpurandare
sanketpurandare changed the base branch from sanketpurandare/stack/7 to main June 26, 2026 07:30
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/8 branch from b06be82 to 01df919 Compare June 26, 2026 07:30
@sanketpurandare
sanketpurandare changed the base branch from main to sanketpurandare/stack/7 June 26, 2026 07:31
@sanketpurandare
sanketpurandare marked this pull request as ready for review June 26, 2026 07:31
_validate_graph_pp_config,
)

from torchtitan.experiments.graph_trainer.graph_pp.runner import (

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.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@aditvenk

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_graph
  • test_partition_saves_backward_passthrough_placeholders
  • test_invalid_backward_only_input_indices_raise
  • test_backward_only_input_required_by_forward_raises
  • test_forward_mutation_of_backward_only_input_raises

The real DSV3 test traces a MoE decoder block with FlexAttention and then checks:

  1. The forward partition returns the same user forward output as the original
    joint graph.
  2. The backward partition consumes saved forward values plus output grads and
    reconstructs the original joint backward outputs.
  3. 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_backward
  • test_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:

  1. The dI graph produces the same input grads as the full backward graph.
  2. The dI graph forwards the live values needed by dW.
  3. 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_params
  • test_forward_split_no_fsdp_is_noop
  • test_forward_split_requires_wait_after_all_gather
  • test_backward_split_extracts_reduce_grad_epilogues
  • test_backward_split_no_fsdp_is_noop_and_validates_grad_count

These tests cover the pass contracts directly:

  1. Forward FSDP all-gather/wait/reconstruction chains are moved into an
    explicit unshard graph.
  2. Replicated params remain on the correct calling convention.
  3. Malformed all-gather chains fail loudly when the expected wait is missing.
  4. Backward reduce-grad epilogues are separated from the main backward graph.
  5. Both reduce-scatter and all-reduce collectives are handled in the reduce-grad
    extraction path.
  6. 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:

  1. FlexAttention needs torch.compile.
  2. Eager PP cannot use ZBVZeroBubble or DualPipeV with torch.compile.
  3. 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.

  1. The graph partition and dI/dW passes are replayed against the original joint
    graph, including a real DSV3 MoE FlexAttention trace.
  2. 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.
  3. Distributed numerics compares actual training losses for DSV3 MoE with EP,
    DP-shard, PP, and FlexAttention across all GraphPP schedules in the stack.
  4. Full-Inductor integration separately verifies the final compiled runtime path
    for those schedules.
  5. 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:

  1. 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.
  2. 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.
  3. 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 -q

The integration matrix additionally includes:

  • aot_fx_trace deepseek_v3 GraphPP Interleaved1F1B full_inductor
  • aot_fx_trace deepseek_v3 GraphPP ZBVZeroBubble full_inductor
  • aot_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.

Comment thread torchtitan/components/loss.py Resolved
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/7 branch from 35d8e14 to e77a8ef Compare June 30, 2026 15:40
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/8 branch from 01df919 to bdc5a07 Compare June 30, 2026 15:40
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/8 branch 2 times, most recently from 4ff54c8 to 117b4b9 Compare June 30, 2026 16:25
@sanketpurandare
sanketpurandare force-pushed the sanketpurandare/stack/7 branch from e77a8ef to a6b658b Compare June 30, 2026 16:25

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

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(

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.

Few questions:

  1. Is boxed_codegen expected to be a win even without PP?

  2. Will this restriction be removed once Precompile support for PP is enabled?

cc: @bobrenjc93

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  1. 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.
  2. Precompile and save with boxed codegen disabled and can do boxed_codegen at runtime, I guess the boxed_codegen is a very lightweight pass?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100.8 Trigger H100.8 CI ciflow/8gpu 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.

[graph_trainer] Graph-PP RFC

4 participants