Fused MoE Kernel integration and optimization - #3388
Draft
helloworld1 wants to merge 8 commits into
Draft
Conversation
Summary: Bring the fused expert-parallel MoE TPU kernels into the repo as a self-contained, dependency-free package. The main kernel runs the entire EP MoE (gather, GMM1, activation, GMM2) with the ICI reduce-scatter fused into a single Pallas call, replacing the usual post-kernel all-reduce and keeping the collective overlapped with compute. A second, smaller kernel is included as a worked example of fusing the upstream all-gather into the grouped matmul (per-round push schedule) and is not performance-tuned. The code is fully standalone: device-specific tuned block-size tables and all internal references were stripped for external release, the two kernels share a single trimmed grouped-matmul base, and a README documents usage and future optimization directions. Test Plan: TODO - numerical verification requires a multi-device TPU mesh, which was not available. On CPU, verified the package byte-compiles and that both entry points (fused_moe_func_rs and the AG+GMM1 example) import cleanly with no unresolved references. Signed-off-by: rupengliu-meta <rupengliu@meta.com>
Signed-off-by: rupengliu-meta <rupengliu@meta.com>
* Change w1 weight loading before gathering token to overlap DMA start SALU ops * Change gather op from dynamic loop to static loop when possible to avoid additional cycles for conditioning and DMA start and allow compiler to perfom loop-invariant code motion.
qwen_coder_1024 Before 560.93 us After 456.98 us -18.53% qwen_coder_2048 Before 916.30 us After 581.96 us -36.49% qwen_coder_4096 Before 1694.44 us After 1024.17 us -39.56% qwen_coder_8192 Before 3140.65 us After 2001.39 us -36.28%
helloworld1
requested review from
Lumosis,
QiliangCui,
a1yssan13,
bythew3i,
gxd3,
jrplatin,
kyuyeunk,
lk-chen,
mrjunwan-lang,
sixiang-google and
vipannalla
as code owners
August 13, 2026 20:44
DescriptionStart with a short description of what the PR does and how this is a change from The rest of the description includes relevant details and context, examples:
If the change fixes a Github issue, please include a link, e.g.,: TestsPlease describe how you tested this change, and include any instructions and/or ChecklistBefore submitting this PR, please make sure:
|
helloworld1
marked this pull request as draft
August 13, 2026 20:56
helloworld1
force-pushed
the
moekernel2
branch
from
August 13, 2026 21:25
d551ba6 to
16a7094
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Add integration, optimization to the fused MoE kernel based on #3040
Performance is optimized for large m (> 1024). Tested on 10 run median of qwen coder 480b moe layer setup.
qwen_coder_1024
fused_moe_gmm 854 us
Original kernel 560.93 us
Optimized kernel 456.98 us
-18.53%
qwen_coder_2048
fused_moe_gmm: 1959 us
Original kernel 916.30 us
Optimized kernel 581.96 us
-36.49%
qwen_coder_4096
fused_moe_gmm: 6264 us
Original kernel 1694.44 us
Optimized kernel 1024.17 us
-39.56%
qwen_coder_8192
fused_moe_gmm 3513 us
Original kernel 3140.65 us
Optimized kernel 2001.39 us
-36.28%
If the change fixes a Github issue, please include a link, e.g.,:
FIXES: #123456
Tests
Please describe how you tested this change, and include any instructions and/or
commands to reproduce.
Checklist
Before submitting this PR, please make sure: