Skip to content

[Common][PyTorch] Add FusedMoE with EP dispatch/combine and GEMM overlap - #3906

Open
phu0ngng wants to merge 67 commits into
NVIDIA:mainfrom
phu0ngng:feat/moe-overlap-ops-main
Open

phu0ngng wants to merge 67 commits into
NVIDIA:mainfrom
phu0ngng:feat/moe-overlap-ops-main

Conversation

@phu0ngng

@phu0ngng phu0ngng commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Adds FusedMoE, a fused MoeDispatch -> GroupedLinear -> ScaledSwiGLU -> GroupedLinear -> MoeCombine op. The NCCL EP dispatch overlaps FC1 and the combine overlaps FC2, in forward and backward. Weight gradients are computed inside the backward calls. Supports BF16 and MXFP8 (E4M3) on Blackwell (SM 10.0, 10.3) and Rubin (SM 10.7), with fixed-capacity CUDA graphs and eager receive sizing.

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

  • Common: new C API ep_gemm.h (nvte_ep_dispatch_gemm_fwd/bwd, nvte_ep_combine_gemm_fwd/bwd, workspace query). Readiness flags are one per alignment rows for the dispatch and one per 1024 hidden columns (or the whole row) for the combine, wired to the NCCL EP signal API (needs NCCL EP >= 0.3.0). A single reset kernel prepares each call's scratch.
  • CuTeDSL adapters: common/CuTeDSL/ep_gemm loads the grouped GEMMs from the directory in NVTE_MOE_OVERLAP, an external kernel tree meant to move into cuDNN. Requires NVTE_ENABLE_CUTEDSL_BACKEND=1 and NVTE_WITH_CUTEDSL=1 at build time.
  • PyTorch: tex.ep_*_gemm_* bindings, which check that all operands are CUDA tensors on the current device, and ops/fused/moe_overlap.py, registered with the fuser. One forward serves both recipes through small per-recipe operand classes. A frozen FC1 or FC2 weight skips its weight-gradient preparation and saved inputs. With the overlap enabled, a window that cannot be fused raises with the reason instead of silently running unfused.
  • Docs and example: NVTE_MOE_OVERLAP in docs/envvars.rst, and examples/pytorch/ep/bench/bench_moe_overlap.py for fwd/bwd timing.

Dependencies:

  • NCCL EP submodule needs to be bumped
  • cuDNN version needs to be added.

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@greptile-apps

greptile-apps Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The latest changes appear safe to merge, with no new blocking defect established.

Summary

Adds FusedMoE to overlap expert communication with grouped GEMMs in BF16 and MXFP8.

  • Common EP calls can now overlap grouped GEMMs with token movement.
  • PyTorch can fuse expert routing, grouped MLP layers, and combine into one operation.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  D["EP dispatch"] --> F1["FC1 + SwiGLU"]
  F1 --> F2["FC2"]
  F2 --> C["EP combine"]
  C --> B2["Backward dispatch + FC2 backward"]
  B2 --> B1["FC1 input gradient"]
  B1 --> W["Optional FC1 weight gradient"]
  W --> BC["Backward combine"]
  B1 --> BC
Loading

Reviews (27) · Last reviewed commit: "[Common] Run the EP dispatch backward un..." · Reviewed by Greptile

Comment thread tests/pytorch/distributed/run_ep.py Resolved
@phu0ngng
phu0ngng force-pushed the feat/moe-overlap-ops-main branch from 82d9201 to 7c92f4a Compare November 20, 2026 17:15
@phu0ngng phu0ngng added the 3.0 label Nov 20, 2026
@phu0ngng
phu0ngng force-pushed the feat/moe-overlap-ops-main branch from 7c92f4a to 6e27c25 Compare November 21, 2026 00:20
@phu0ngng
phu0ngng marked this pull request as draft November 21, 2026 03:38
@phu0ngng
phu0ngng force-pushed the feat/moe-overlap-ops-main branch from ff21bae to be1df76 Compare November 23, 2026 18:43
@phu0ngng
phu0ngng force-pushed the feat/moe-overlap-ops-main branch from be1df76 to a279351 Compare November 24, 2026 05:01
@phu0ngng phu0ngng changed the title [Common][PyTorch] EP Gemm Overlap [Common][PyTorch] Add FusedMoE with EP dispatch/combine and GEMM overlap Nov 24, 2026
@phu0ngng
phu0ngng marked this pull request as ready for review November 24, 2026 05:05
dinodeep added a commit to dinodeep/TransformerEngine that referenced this pull request Nov 25, 2026
The overlap path no longer rejects TE checkpoint names. The forward tags the
residuals of the fused dispatch+FC1 call (routing state, received tokens and
their MXFP8 scales, received routing weights, per-expert counts, the FC1
output and, for BF16, the SwiGLU output) with checkpoint_name; the wo name
tags nothing because the overlap backward does not read FC2's output. Under a
names-based remat policy JAX rebuilds unsaved residuals by re-running only the
fused dispatch+FC1 call; FC2+combine is never re-run (no separate recompute
path).

A re-run dispatch can place the received rows in a different order, so mixing
saved and rebuilt residuals corrupts the gate and input gradients (measured:
relative error ~1). Every fused-call residual therefore carries all given
names (dispatch, wi_0, wi_1), nested, and is saved when the policy saves any
of them: a mixed policy saves all of them, otherwise all are rebuilt.

_MoEBlock gains wi_0/wi_1/wo_checkpoint_name (forwarded to moe()).

Tests (tests/jax/test_te_ep_moe.py, 4x GB300, EP2xFSDP2, unfused mode):
test_ep_remat_policy over BF16/MXFP8 x policies {wo saved (MaxText custom
remat), nothing saved, dispatch+wo, wi+wo}: overlap loss and gradients match
the un-rematerialized flag-off step; compiled HLO has 2x
te_ep_dispatch_gemm_fwd_ffi when no fused-call name is saved (1x otherwise)
and 1x each other overlap call. The flag-off path is checked only with
everything rematerialized: with saved residuals and a re-run dispatch it shows
the same row-order mismatch (pre-existing, not changed here).
`-k "overlap or remat or checkpoint"`: 63 passed, 29 skipped.

DeepSeek-V3 MaxText proxy (4x GB300, EP4, 1 dense + 4 MoE layers, FP8 dense
+ MXFP8 MoE, pdb 6 x seq 4096, GA 2, custom remat: dispatch and moe_mlpwi
remat, moe_mlpwo saved): the compiled train step has
te_ep_dispatch_gemm_fwd_ffi x16, te_ep_combine_gemm_fwd_ffi x8 and each
backward call x8 (4 layers x 2 microbatches: dispatch+FC1 re-run once per
layer, FC2+combine not re-run). With the overlap on (+ native layouts) vs
off: step 0.991 vs 1.090 s (-9.0%), LHS estimate 98.2 vs 107.7 GiB, peak
159.5 vs 169.7 GiB, losses within 0.004 over 24 steps.

Depends on upstream PR NVIDIA#3906.

Signed-off-by: Deep Patel <[email protected]>
dinodeep added a commit to dinodeep/TransformerEngine that referenced this pull request Nov 25, 2026
NCCL EP allocates a group's handle scan scratch lazily, on the first handle
init or import. In a MaxText train step (XLA command buffers with
CUSTOM_CALL) the first overlap call that imports a handle ran inside a CUDA
graph capture, and the allocation failed: "EP combine_gemm_fwd failed: CUDA
error nccl_ep.cc:4370 'operation not permitted when stream is capturing'".

nvte_ep_overlap_compile, which already runs at trace time once per layer
configuration, now opens and closes a handle for the layer configuration
(EPBackend::warm_handle_scratch) so the scratch exists before any captured
call. No behaviour change otherwise.

Measured (4x GB300, DeepSeek-V3 proxy, 1 dense + 4 MoE layers, EP4, FP8 +
MXFP8 MoE, pdb 6, GA 2, command buffers incl. CUSTOM_CALL): before, arm B
failed at the first step; after, it runs (step 0.983 s, same as with
CUSTOM_CALL removed from the command buffers).

Depends on upstream PR NVIDIA#3906.

Signed-off-by: Deep Patel <[email protected]>
Comment thread docs/api/pytorch.rst Resolved
Comment thread docs/envvars.rst Outdated
.. envvar:: NVTE_MOE_OVERLAP

:Type: ``str`` (directory)
:Default: unset (overlap off)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why having default off? If the NCCL EP version and the cuDNN version is right, we should try to default to the overlap, no?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Because there is no IB support yet in TE 3.0.
I would prefer to have it off by default for now, until the support matrix is completed.

Comment thread docs/envvars.rst Outdated
Comment on lines +165 to +166
TODO(phuonguyen): once the CuTeDSL kernels are upstreamed into cuDNN, this should take a plain
0/1 (or false/true) toggle instead of a directory path.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Leaving a comment here as well to make sure that this TODO is not forgotten.

Comment thread tests/pytorch/test_ep_gemm.py Resolved
…SL backend and align the native call helper

Signed-off-by: Phuong Nguyen <[email protected]>
…ive each transport direction its own quantizer

Signed-off-by: Phuong Nguyen <[email protected]>
…e config leaves it unset

Signed-off-by: Phuong Nguyen <[email protected]>
…unches with shared per-direction helpers

Signed-off-by: Phuong Nguyen <[email protected]>
Comment thread transformer_engine/pytorch/ep.py Resolved
…XFP8 branches of the FusedMoE support reasons

Signed-off-by: Phuong Nguyen <[email protected]>
… stream rule, workspace alignment and SM support

Signed-off-by: Phuong Nguyen <[email protected]>
… the dprob view in the EP dispatch GEMM

Signed-off-by: Phuong Nguyen <[email protected]>
…ions to the SM 10.7 MXFP8 combine GEMM

Signed-off-by: Phuong Nguyen <[email protected]>
… int64 only on the MoE overlap path

Signed-off-by: Phuong Nguyen <[email protected]>
…ithout input gradients cannot stall its peers

Signed-off-by: Phuong Nguyen <[email protected]>
…ush tile in the EP overlap launcher

Signed-off-by: Phuong Nguyen <[email protected]>
Comment thread tests/pytorch/test_grouped_mlp.py Resolved
… tests into test_ep_gemm.py

Signed-off-by: Phuong Nguyen <[email protected]>
…overlap budgets to ep_initialize as arrays

Signed-off-by: Phuong Nguyen <[email protected]>
…d MLP keeps int32 offsets

Signed-off-by: Phuong Nguyen <[email protected]>
Comment thread tests/pytorch/distributed/run_ep.py Resolved

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants