Repository navigation
Conversation
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.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ 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) |
There was a problem hiding this comment.
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)
Reviewed by Cursor Bugbot for commit b58250b. Configure here.


Summary
deepgemm_fp8grouped GEMM's weight-gradient kernel takes each expert's rows as a host list, so its backward didks_tensor.tolist(): one device-to-host sync per expert GEMM backward (3 per MoE layer, more with recompute).TorchTokenDispatcher(and the MXFP8 variant) now adds the aligned rows per local expert to the split-size copy it makes once per layer, andDeepEPTokenDispatcherreturns them from its dispatch op, which receives the counts on the host from DeepEP.TokenDispatcherBase.runpasses them to the experts as an optional second routing tensor, andDeepGemmFP8ExpertComputeforwards them togrouped_fp8_gemm.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.diff(offs)aligned to 128, the same valuestolist()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.test_backward_with_host_counts_does_not_sync_with_the_hosttotest_fp8_grouped_gemm.py: with host counts the backward runs undertorch.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 checkandruff formaton the changed filespytest tests/unit/train/models/test_moe.py tests/unit/train/models/test_fp8_grouped_gemm.pyon CPU: 28 passed, 22 skipped (the GPU tests, including the new one, skip without CUDA)aligned_rows_per_expertequals them_sizesfrom torchtitan'sgenerate_permute_indicesover 200 random routings and alignments 1, 8, 16 and 128TorchTokenDispatcher.runwith identity collectives (EP 1, 2 and 4; alignment 1, 8 and 128) hands the experts host counts equal tonum_tokens_per_experttests/unit/train/models/test_fp8_grouped_gemm.py, plus an EP run withdeepgemm_fp8under torch and DeepEP dispatch, checking for no host syncs in the expert backward and unchanged lossesNote
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 callks_tensor.tolist()on the device.TokenDispatcherBase.runpasses an optionalhost_num_tokens_per_expertwhen dispatch already materializes counts on the host (TorchTokenDispatcher,DeepEPTokenDispatcher).grouped_fp8_gemmandDeepGemmFP8ExpertComputeuse 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=errorwhen host counts are supplied.Reviewed by Cursor Bugbot for commit b58250b. Bugbot is set up for automated code reviews on this repo. Configure here.