Skip to content

Fused MoE Kernel integration and optimization - #3388

Draft
helloworld1 wants to merge 8 commits into
mainfrom
moekernel2
Draft

Fused MoE Kernel integration and optimization#3388
helloworld1 wants to merge 8 commits into
mainfrom
moekernel2

Conversation

@helloworld1

Copy link
Copy Markdown
Collaborator

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:

  • I have performed a self-review of my code.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have made or will make corresponding changes to any relevant documentation.

rupengliu-meta and others added 6 commits August 13, 2026 19:47
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>
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%
@github-actions

Copy link
Copy Markdown

Description

Start with a short description of what the PR does and how this is a change from
the past.

The rest of the description includes relevant details and context, examples:

  • why is this change being made,
  • the problem being solved and any relevant context,
  • why this is a good solution,
  • some information about the specific implementation,
  • shortcomings of the solution and possible future improvements.

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:

  • I have performed a self-review of my code.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have made or will make corresponding changes to any relevant documentation.

@helloworld1 helloworld1 changed the title Moekernel2 Fused MoE Kernel integration and optimization Aug 13, 2026
@helloworld1
helloworld1 marked this pull request as draft August 13, 2026 20:56
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.

2 participants