Skip to content

CUDA: fuse the top-k MoE weight norm with an additive sum bias - #32943

Merged
ggerganov merged 1 commit into
ggml-org:masterfrom
am17an:cuda-topk-moe-eps-norm
Nov 26, 2026
Merged

ggerganov merged 1 commit into
ggml-org:masterfrom
am17an:cuda-topk-moe-eps-norm

Conversation

@am17an

@am17an am17an commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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) in build_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 SCALE with scale 1 in place of the CLAMP. The kernel computes the denominator as max(sum, clamp) + bias, the same rule as the Metal fusion from #31615. For the CLAMP form the bias is 0, so other models do not change.

Additional information

The TOPK_MOE cases with norm_bias=1 in 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:

master PR
kernels per token (tg32, CUDA graphs off, nsys) 7351 7159
GPU time per token, same run 35.03 ms 34.27 ms
tg128 t/s (2 interleaved rounds of -r 3) 39.3, 38.3 38.8, 38.5
pp512 t/s 826, 813 827, 811

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 256 against master gives 0.0040 on this model.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. Claude Code found the missed fusion in an nsys profile of Inkling 2 Mini decode, wrote the change, ran the tests and benchmarks above and wrote this description.

@am17an
am17an requested a review from a team as a code owner November 24, 2026 12:12
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Nov 24, 2026
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]>
@anujj

anujj commented Nov 25, 2026

Copy link
Copy Markdown
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)

@am17an

am17an commented Nov 25, 2026

Copy link
Copy Markdown
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

@anujj

anujj commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Drafted this PR: #33011

@am17an am17an added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Nov 25, 2026
@ggerganov
ggerganov merged commit 51ab45e into ggml-org:master Nov 26, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants