Skip to content

[graph_trainer] Trace compact DeepEP and overlap the shared experts with the dispatch wait - #5315

Open
yuweih205 wants to merge 3 commits into
pytorch:mainfrom
yuweih205:feat/graph-trainer-deepep-overlap
Open

yuweih205 wants to merge 3 commits into
pytorch:mainfrom
yuweih205:feat/graph-trainer-deepep-overlap

Conversation

@yuweih205

@yuweih205 yuweih205 commented Oct 12, 2026 •

Copy link
Copy Markdown

Summary

Following the review of #5263: express the shared-expert / DeepEP-dispatch overlap as a GraphTrainer
graph pass instead of an eager callback. That needs a prerequisite, which is most of this diff: the compact
DeepEP training path has to be traceable by GraphTrainer at all. Today memory_policy.py already lists
deepep.dispatch, but the ops have no fake kernels, the backward runs DeepEP directly, and the combine
relies on an eager-only stream wait.

What changes

torchtitan/distributed/deepep/deepep.py (eager behavior preserved):

  • every deepep:: op gets a fake kernel; the received row count is an unbacked size (new_dynamic_size()),
    the same way the all-to-all path exposes data-dependent counts;
  • the backward communications become ops (dispatch_bwd, combine_bwd) with fake kernels, so a traced joint
    graph contains them instead of running DeepEP on fake tensors;
  • combine waits for its own communication (the dispatcher synchronized right after it anyway), which a
    traced graph needs because sync_combine() was never part of the graph;
  • the handle cache is only touched inside op implementations; setup_context keeps the handle_id tensor;
  • internal split of the compact dispatch into launch_dispatch + wait_dispatch (DeepEP v2.5
    defer_epilogue). GraphTrainer configures it automatically before tracing when
    compile.deepep_dispatch_overlap=True; the dispatcher has no public split toggle and eager keeps the
    original unsplit dispatch. _permute_tokens uses shape[0] instead of len() so it traces with an
    unbacked size;
  • EPBuffer / ElasticBuffer import compatibility (DeepEP v2.5 renamed the buffer); the DeepEP GPU test
    patches whichever class the installed DeepEP has.

torchtitan/experiments/graph_trainer:

  • compile.deepep_dispatch_overlap and deepep_overlap_pass: for each forward wait_dispatch, every node in
    the window up to the combine's consumer that does not depend on the wait, has no side effect and is
    computable from pre-launch values (in the common MoE layout: the shared-expert FFN) is scheduled between
    the launch and the wait. Forward only, stable topological sort, no node added or changed. Independent of
    ep_overlap: DeepEP has one communication buffer, so the second in-flight exchange that chunking needs is
    not available, which is why this only sinks waits.
  • memory policy lists the new ops as communication ops, annotate_moe_ep_regions covers the DeepEP
    dispatcher, test recipe graph_trainer_deepseek_v3_debugmodel_deepep, CPU unit tests for the pass, README.

Validation (2026-11-14, head 396c047b)

Revalidated on one 8×H200 141GB node with native DeepSeek-V3 16B (64 routed experts, top-6, two shared experts), EP8/FSDP8, BF16 compute/FP32 reductions, Flex attention, CUDA graphs off, random initialization, seed 42, deterministic mode and one synthetic 8192-token document per rank. Runtime: PyTorch 2.16.0.dev20261009+cu132, DeepEP 2.5.0 (35 checked source files match 93eb6eb), PyTorch-reported NCCL 2.30.7.

All arms use this PR's source. A disables the feature; B enables split dispatch but disables only deepep_dispatch_overlap_pass; C enables both. Three balanced blocks (ABCCBA / BCAACB / CABBAC), 18 fresh-process runs sharing compilation caches, 30 steps each. Timing: median of steps 6–30, taking the maximum synchronized wall time across eight ranks separately for each step.

Arm Dispatch / scheduling Step time (ms)
A Original compact dispatch 879.949
B Split launch/wait, pass off 882.116
C Split launch/wait, pass on 869.368

Table values are geometric means of six run medians per arm.

  • Complete switch A→C: 1.20% less step time; individual blocks: 1.064%, 1.250%, 1.293%.
  • Scheduling B→C: 1.45% less step time. Splitting alone A→B adds 0.246%.
  • All 18 runs passed same-rank byte-hash checks for every input, loss and grad norm, plus canonical initial/final model and optimizer state. This establishes bitwise agreement for the recorded values in this experiment.
  • PyTorch allocated peak: 39.352 GiB in all arms. Reserved peak: 41.156 GiB (A) → 41.723 GiB (C), +0.566 GiB. These allocator peaks include compilation/warmup windows and exclude external allocations.
  • CPU dispatcher test: 1 passed; graph-pass tests: 3 passed; GPU activation-checkpoint tests: 2 passed; zero skips. The three-arm 8-rank correctness probe and independent raw-data audit also passed.

Scope: synchronized native training-step latency, including fetch, forward/backward, optimizer and native metrics/logging; outer barriers and evidence hashing are excluded. Three blocks on one allocation support a small gain for this workload, not broad performance claims. A retains this PR's shared traceability/backward/combine changes, so A→C is a feature ablation, not an unmodified-upstream comparison. Graph evidence confirms the selected path; no new physical-overlap profiler trace was collected.

Not validated: multi-node, TP > 1 with EP, FP8 dispatch, CUDA graphs (the compact path has a host sync), ep_overlap chunking composed with this pass, or convergence on real data. Upstream GPU CI pins an older DeepEP; the v2.5-only split path was exercised by this experiment.

Rebase validation (2026-10-19)

Rebased onto main at 48bbce4434df. Followed the upstream MoE module move to
torchtitan.models.common.moe.token_dispatcher; the DeepEP debug recipe now explicitly
selects attn_backend="flex" in both builders, matching the upstream GraphTrainer recipes.

  • Formatting, whitespace, Python AST, merge-conflict, license, flake8, ufmt, pydoclint,
    and codespell hooks passed across the full checkout.
  • The three existing CPU scheduling tests passed on PyTorch 2.16.0.dev20261018
    using source-defined DeepEP schemas and the real FX scheduling pass. This isolates
    graph reordering; no native DeepEP communication or CUDA code was exercised.
  • Local Pyrefly diagnostics were identical to the same main checkout (no new
    diagnostics). A full type-check pass could not be established with the local
    dependency set.
  • GPU training and communication tests were not rerun for the October 19 rebase itself.
    The current-head GPU validation is recorded in the November 14 section above.

Single-switch configuration (2026-10-20)

compile.deepep_dispatch_overlap is the only feature switch and defaults to False.
GraphTrainer automatically selects split dispatch before tracing when it is enabled.
The DeepEP debug recipe uses Flex attention, matching the existing GraphTrainer recipes.

For tuning, hold the model and inputs fixed and compare:

Configuration Dispatch Scheduling pass
deepep_dispatch_overlap=False Original compact op Off
deepep_dispatch_overlap=True, disable_passes=["deepep_dispatch_overlap_pass"] Split launch/wait Off
deepep_dispatch_overlap=True Split launch/wait On

Append to an existing disable_passes list when other passes are already disabled.
The original October 12 GPU ON/OFF result (1.16%) isolated the scheduling pass: OFF retained split
dispatch, corresponding to the second row. The November 14 validation above measures all three cases.

  • Six CPU checks passed on PyTorch 2.16.0.dev20261018: three dispatch-configuration cases and the
    three existing graph-scheduling tests. This local harness runs the source-defined dispatcher classes,
    compile configuration and setup, and real FX pass, with unrelated training infrastructure and native
    DeepEP replaced for import/execution. It exercises no CUDA kernels or native communication.
  • All three ablation cases were also checked through the source-defined pass filtering code and the
    real FX scheduler: disabling only deepep_dispatch_overlap_pass retains split dispatch and the
    original node order.
  • Formatting and non-type pre-commit hooks passed across the checkout; the new CPU test also passed
    its file hooks. Local Pyrefly produced the same 190 diagnostics as the upstream base, with no new
    diagnostics. The dependency-limited local type-check does not establish a full type-check pass.
  • GPU training, communication and performance were not rerun for this configuration refactor.

Open questions

  • Is the compact (data-dependent shape) DeepEP path acceptable in GraphTrainer graphs, or should GraphTrainer
    stay with HybridEP? The traceability part stands on its own even without the pass.
  • Should the pass live in ep_overlap_pass.py as a single-chunk mode instead of its own module?

Supersedes the eager approach of #5263 if accepted.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 12, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/rl
  • ciflow/fake-pg

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@yuweih205
yuweih205 marked this pull request as ready for review October 12, 2026 14:36
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 12, 2026
@yuweih205
yuweih205 force-pushed the feat/graph-trainer-deepep-overlap branch 2 times, most recently from 7288480 to 49e4695 Compare October 19, 2026 09:41
Resolve dispatch API conflicts by retaining per-row router scores in metadata. Preserve the main combine SAVE region and the PR-specific overlap/tracing behavior.
Update the compact DeepEP recipe for current model and accelerator-graph configuration fields. Use the selected DeepEP buffer API in activation-rematerialization tests, and correct overlap scheduling validation and FX producer typing.

Assisted-by: Codex
Signed-off-by: HuangYuwei <[email protected]>
@yuweih205

Copy link
Copy Markdown
Author

Revalidated head 396c047b on 8×H200 with DeepSeek-V3 16B, EP8/FSDP8 and 8192 synthetic tokens/rank: 18 runs in three balanced blocks. The complete feature switch reduces synchronized training-step time 1.20% (879.949 → 869.368 ms); scheduling on split dispatch reduces it 1.45%, while splitting alone adds 0.246%.

Input/loss/grad-norm byte hashes and canonical initial/final model/optimizer state hashes match across all runs. Allocated peak is unchanged; reserved peak increases 0.566 GiB. This is a same-PR feature ablation on one allocation. Updated the description with the protocol, results and scope.

@tianyu-l

Copy link
Copy Markdown
Contributor

This branch has not been deployed

No deployments
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 ciflow/rl 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.

3 participants