Repository navigation
CUDA: fuse the top-k MoE weight norm with an additive sum bias - #32943
Merged
Merged
Conversation
Assisted-by: Claude Opus 5.5
harshk242
pushed a commit
to harshk242/llama.cpp
that referenced
this pull request
Nov 24, 2026
The TOPK_MOE_SIGMOID_NORM_BIAS fusion only matched a CLAMP of the weight sum. Inkling 2 (and DeepSeek-V4.1) normalize by sum + eps instead, ggml_scale_bias(sum, 1, eps) in build_moe_ffn, so their router ran as separate SIGMOID, ADD, ARGSORT, GET_ROWS, SUM_ROWS, SCALE, DIV and SCALE ops. A SCALE with scale 1 is now accepted in place of the CLAMP; the shader adds the bias (new push constant norm_bias) after the clamp. Same rule as the CUDA change in ggml-org/llama.cpp PR ggml-org#32943. B390, Inkling 2 Mini decode: 5837 -> 5501 dispatches per token, GPU time 75.2 -> 73.6 ms (GGML_VK_PERF_LOGGER). test-backend-ops TOPK_MOE 692/692. Perplexity (4 x 4096) 3.0817 -> 3.0829 (+-0.07). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
|
could we build the top-k MoE op list in one helper and use it from both ggml_cuda_try_fuse and ggml_backend_cuda_graph_optimize? The matcher and both callers currently describe the normalization sequence separately (ggml-cuda.cu, around lines 3946, 4631, and 5833) |
Contributor
Author
|
@anujj yeah sure, I think it's one of the TODOs in the ggml-cuda.cu file, feel free to make a PR for that |
Contributor
|
Drafted this PR: #33011 |
anujj
approved these changes
Nov 25, 2026
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.
Overview
The CUDA top-k MoE fusion only fuses the weight normalization when the sum is clamped from below (
SUM_ROWS -> CLAMP -> DIV). DeepSeek-V4.1 and Inkling 2 add a bias to the sum instead (ggml_scale_bias(sum, 1, eps)inbuild_moe_ffn). On CUDA the fused kernel then stops after the expert selection, and the sum, the bias, the division and the routed scale run as separate kernels.This accepts a
SCALEwith scale 1 in place of theCLAMP. The kernel computes the denominator asmax(sum, clamp) + bias, the same rule as the Metal fusion from #31615. For theCLAMPform the bias is 0, so other models do not change.Additional information
The
TOPK_MOEcases withnorm_bias=1in test-backend-ops (added with #31615) cover this form, and now go through the fused kernel.test-backend-ops -o TOPK_MOE: 692/692 pass on GB10.Inkling 2 Mini IQ4_NL (ggml-org), GB10, master 2a48778 vs this PR:
-r 3)The fused kernel removes 4 kernels per MoE layer pass (sum_rows, the bias scale, div and the routed scale). With CUDA graphs the tg128 change is within run-to-run noise on this shared machine.
KLD vs master over 40 chunks of 512: 0.0038 (same top p 97.8 %). For reference, master with
-ub 256against master gives 0.0040 on this model.Requirements