Skip to content

perf(moe): FP8 grouped GEMM weight gradients without host syncs - #4894

Open
MarioSieg wants to merge 3 commits into
mainfrom
mario/fp8-wgrad-no-host-sync
Open

MarioSieg wants to merge 3 commits into
mainfrom
mario/fp8-wgrad-no-host-sync

Conversation

@MarioSieg

@MarioSieg MarioSieg commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • The deepgemm_fp8 grouped GEMM's weight-gradient kernel takes each expert's rows as a host list, so its backward did ks_tensor.tolist(): one device-to-host sync per expert GEMM backward (3 per MoE layer, more with recompute).
  • The EP dispatchers already have these counts on the host: TorchTokenDispatcher (and the MXFP8 variant) now adds the aligned rows per local expert to the split-size copy it makes once per layer, and DeepEPTokenDispatcher returns them from its dispatch op, which receives the counts on the host from DeepEP. TokenDispatcherBase.run passes them to the experts as an optional second routing tensor, and DeepGemmFP8ExpertCompute forwards them to grouped_fp8_gemm.
  • Without host counts (the single-rank LocalTokenDispatcher, or a direct call) the backward falls back to copying them from the device, as before. Other expert computes (BF16, MXFP8, NVFP4, SonicMoE, LoRA) accept and ignore the extra argument.
  • Results should be bitwise identical: the host counts equal diff(offs) aligned to 128, the same values tolist() copied; only the syncs go away. Expected win: the CPU no longer waits on the GPU in every FP8 expert backward, so it can keep queueing kernels ahead, which matters most with EP and comm/compute overlap.
  • Adds test_backward_with_host_counts_does_not_sync_with_the_host to test_fp8_grouped_gemm.py: with host counts the backward runs under torch.cuda.set_sync_debug_mode("error") and gives the same bytes as the device-copy path.

GPU validation is still pending: this was written without access to a GPU.

Test plan

  • ruff check and ruff format on the changed files
  • pytest tests/unit/train/models/test_moe.py tests/unit/train/models/test_fp8_grouped_gemm.py on CPU: 28 passed, 22 skipped (the GPU tests, including the new one, skip without CUDA)
  • CPU script: aligned_rows_per_expert equals the m_sizes from torchtitan's generate_permute_indices over 200 random routings and alignments 1, 8, 16 and 128
  • CPU script: TorchTokenDispatcher.run with identity collectives (EP 1, 2 and 4; alignment 1, 8 and 128) hands the experts host counts equal to num_tokens_per_expert
  • GPU: tests/unit/train/models/test_fp8_grouped_gemm.py, plus an EP run with deepgemm_fp8 under torch and DeepEP dispatch, checking for no host syncs in the expert backward and unchanged losses

Note

Medium Risk
Touches MoE dispatch, DeepEP custom ops, and FP8 autograd; numerical paths should be identical but EP training backward behavior changes when host counts are wired through.

Overview
MoE EP dispatch now threads CPU-side per-expert token counts (and aligned row counts via new aligned_rows_per_expert) into expert compute so FP8 grouped GEMM weight gradients no longer call ks_tensor.tolist() on the device.

TokenDispatcherBase.run passes an optional host_num_tokens_per_expert when dispatch already materializes counts on the host (TorchTokenDispatcher, DeepEPTokenDispatcher). grouped_fp8_gemm and DeepGemmFP8ExpertCompute use those counts in backward; without them behavior is unchanged (device copy + sync). Other expert backends accept the extra argument and ignore it.

Adds a GPU test asserting bitwise-identical backward under sync_debug_mode=error when host counts are supplied.

Reviewed by Cursor Bugbot for commit b58250b. Bugbot is set up for automated code reviews on this repo. Configure here.

The k-grouped weight-gradient GEMM takes every expert's rows on the host, so the backward copied them there with
ks_tensor.tolist(): a device sync per expert GEMM backward. The EP dispatchers already have these counts on the host
(the torch dispatcher copies its split sizes there once per layer, DeepEP returns its counts on the host): they now
include the aligned rows per local expert and pass them to the experts as a second routing tensor, which the FP8
grouped GEMM uses instead. Without them (the single-rank local dispatcher) the backward still copies them from the
device.
@MarioSieg
MarioSieg marked this pull request as ready for review December 3, 2026 19:45

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit b58250b. Configure here.

loss.backward()

for actual, reference, label in zip((out.detach(), x_leaf.grad, weight_leaf.grad), expected, ("out", "x", "w")):
_assert_bitwise(actual, reference, label)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bitwise check includes uninitialized rows

Medium Severity

test_backward_with_host_counts_does_not_sync_with_the_host bitwise-compares the full forward output and input gradient. grouped_fp8_gemm allocates those with torch.empty and leaves rows past offs[-1] uninitialized, so the tail is allocator garbage and the two runs can disagree even when the written rows match.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b58250b. Configure here.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant