Commit 51ab45e3c for llama.cpp
commit 51ab45e3c52304d7cc8e43e5a913baa8ccd7abcc
Author: Aman Gupta <amangupta052@gmail.com>
Date: Thu Nov 26 17:30:42 2026 +0800
CUDA: fuse the top-k MoE weight norm with an additive sum bias (#32943)
Assisted-by: Claude Opus 5.5
diff --git a/ggml/src/ggml-cuda/ggml-cuda.cu b/ggml/src/ggml-cuda/ggml-cuda.cu
index c014bbe5b..37ddb84f9 100644
--- a/ggml/src/ggml-cuda/ggml-cuda.cu
+++ b/ggml/src/ggml-cuda/ggml-cuda.cu
@@ -3842,6 +3842,7 @@ static bool ggml_cuda_topk_moe_fusion(const struct ggml_cgraph * cgraph, int nod
args.delayed_softmax = false;
args.prob_bias = false;
args.norm = false;
+ args.norm_bias = false;
args.n_groups = 0;
args.n_group_used = 0;
@@ -3962,8 +3963,8 @@ static bool ggml_cuda_topk_moe_fusion(const struct ggml_cgraph * cgraph, int nod
}
if (nodes[node_idx]->op == GGML_OP_RESHAPE) {
- //check RESHAPE->SUM_ROWS->CLAMP->DIV->RESHAPE
- static const std::vector<ggml_op> norm_ops = { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP };
+ //check RESHAPE->SUM_ROWS->CLAMP|SCALE->DIV->RESHAPE
+ static const std::vector<ggml_op> norm_ops = { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS };
args.norm = true;
for (const ggml_op op : norm_ops) {
@@ -3975,7 +3976,18 @@ static bool ggml_cuda_topk_moe_fusion(const struct ggml_cgraph * cgraph, int nod
}
}
- // DIV <- CLAMP, RESHAPE
+ // the sum is clamped from below, or a SCALE with scale 1 adds a bias (DeepSeek-V4.1, Inkling 2)
+ const ggml_tensor * denom = node_idx < n_nodes ? nodes[node_idx] : nullptr;
+ const bool is_clamp = denom && denom->op == GGML_OP_CLAMP;
+ const bool is_bias = denom && denom->op == GGML_OP_SCALE && ggml_get_op_params_f32(denom, 0) == 1.0f;
+ if ((!is_clamp && !is_bias) || denom->src[0] != nodes[node_idx - 1]) {
+ args.norm = false;
+ return true;
+ }
+ args.norm_bias = is_bias;
+ node_idx++;
+
+ // DIV <- CLAMP|SCALE, RESHAPE
if (nodes[node_idx]->op != GGML_OP_DIV || nodes[node_idx]->src[1] != nodes[node_idx - 1] ||
nodes[node_idx]->src[0] != nodes[node_idx - 3]) {
args.norm = false;
@@ -4605,7 +4617,7 @@ static int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph
ggml_tensor * weights = nullptr;
ggml_tensor * ids = nullptr;
const ggml_tensor * bias = nullptr;
- const ggml_tensor * clamp = nullptr;
+ const ggml_tensor * denom = nullptr;
const ggml_tensor * scale = nullptr;
if (!args.delayed_softmax) {
@@ -4633,9 +4645,9 @@ static int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph
ids = cgraph->nodes[out_nodes[0]];
if (args.norm) {
- ops.insert(ops.end(),
- { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP, GGML_OP_DIV, GGML_OP_RESHAPE });
- clamp = cgraph->nodes[i + ops.size() - 3];
+ ops.insert(ops.end(), { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, args.norm_bias ? GGML_OP_SCALE : GGML_OP_CLAMP,
+ GGML_OP_DIV, GGML_OP_RESHAPE });
+ denom = cgraph->nodes[i + ops.size() - 3];
}
if (args.scale) {
ops.insert(ops.end(), { GGML_OP_SCALE });
@@ -4648,7 +4660,7 @@ static int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph
if (ggml_can_fuse_subgraph(cgraph, i, ops.size(), ops.data(), out_nodes, 2) &&
ggml_cuda_should_use_topk_moe(node, logits, weights, ids) &&
ggml_cuda_check_fusion_memory_ranges(cgraph, i, ops.size(), out_nodes, 2, /*is_topk_moe=*/true)) {
- ggml_cuda_op_topk_moe(*cuda_ctx, logits, weights, ids, clamp, scale, bias, args);
+ ggml_cuda_op_topk_moe(*cuda_ctx, logits, weights, ids, denom, scale, bias, args);
return ops.size() - 1;
}
} else if (!args.norm && !args.prob_bias) {
@@ -4663,7 +4675,7 @@ static int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph
if (ggml_can_fuse_subgraph(cgraph, i, ops.size(), ops.data(), out_nodes, 2) &&
ggml_cuda_should_use_topk_moe(softmax, logits, weights, ids) &&
ggml_cuda_check_fusion_memory_ranges(cgraph, i, ops.size(), out_nodes, 2, /*is_topk_moe=*/true)) {
- ggml_cuda_op_topk_moe(*cuda_ctx, logits, weights, ids, clamp, scale, bias, args);
+ ggml_cuda_op_topk_moe(*cuda_ctx, logits, weights, ids, denom, scale, bias, args);
return ops.size() - 1;
}
}
@@ -5835,8 +5847,9 @@ static void ggml_backend_cuda_graph_optimize(ggml_backend_t backend, ggml_cgraph
ids = cgraph->nodes[out_nodes[0]];
if (args.norm) {
- ops.insert(ops.end(),
- { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP, GGML_OP_DIV, GGML_OP_RESHAPE });
+ ops.insert(ops.end(), { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS,
+ args.norm_bias ? GGML_OP_SCALE : GGML_OP_CLAMP, GGML_OP_DIV,
+ GGML_OP_RESHAPE });
}
if (args.scale) {
ops.insert(ops.end(), { GGML_OP_SCALE });
diff --git a/ggml/src/ggml-cuda/topk-moe.cu b/ggml/src/ggml-cuda/topk-moe.cu
index d6481baaf..0bb65f306 100644
--- a/ggml/src/ggml-cuda/topk-moe.cu
+++ b/ggml/src/ggml-cuda/topk-moe.cu
@@ -7,12 +7,13 @@
// Kernel config struct - passed by value to CUDA kernel
struct topk_moe_config {
- bool use_sigmoid;
- bool use_sqrt_softplus;
- bool with_norm;
- bool delayed_softmax;
- int n_groups;
- int n_group_used;
+ bool use_sigmoid;
+ bool use_sqrt_softplus;
+ bool with_norm;
+ bool delayed_softmax;
+ int n_groups;
+ int n_group_used;
+ float norm_bias;
};
// Warp-local softmax used for both the pre-top-k logits and the post-top-k delayed path.
@@ -334,7 +335,7 @@ __global__ void topk_moe_cuda(const float * logits,
if (config.with_norm) {
wt_sum = warp_reduce_sum<warp_size>(wt_sum);
- wt_sum = max(wt_sum, clamp_val);
+ wt_sum = max(wt_sum, clamp_val) + config.norm_bias;
const float inv_sum = 1.0f / wt_sum;
for (int i = 0; i < experts_per_thread; i++) {
@@ -438,7 +439,7 @@ void ggml_cuda_op_topk_moe(ggml_backend_cuda_context & ctx,
const ggml_tensor * logits,
ggml_tensor * weights,
ggml_tensor * ids,
- const ggml_tensor * clamp,
+ const ggml_tensor * denom,
const ggml_tensor * scale,
const ggml_tensor * bias,
const ggml_cuda_topk_moe_args & args) {
@@ -460,11 +461,15 @@ void ggml_cuda_op_topk_moe(ggml_backend_cuda_context & ctx,
const int n_expert_used = weights->ne[1];
- const bool with_norm = clamp != nullptr;
+ const bool with_norm = denom != nullptr;
+ // the sum is clamped from below (CLAMP) or gets an additive bias (SCALE with scale 1)
float clamp_val = -INFINITY;
- if (clamp) {
- clamp_val = ggml_get_op_params_f32(clamp, 0);
+ float norm_bias = 0.0f;
+ if (denom && denom->op == GGML_OP_SCALE) {
+ norm_bias = ggml_get_op_params_f32(denom, 1);
+ } else if (denom) {
+ clamp_val = ggml_get_op_params_f32(denom, 0);
}
topk_moe_config config;
@@ -474,6 +479,7 @@ void ggml_cuda_op_topk_moe(ggml_backend_cuda_context & ctx,
config.delayed_softmax = args.delayed_softmax;
config.n_groups = args.n_groups;
config.n_group_used = args.n_group_used;
+ config.norm_bias = norm_bias;
if (bias) {
launch_topk_moe_cuda<true>(ctx, logits_d, weights_d, ids_d, bias_d, n_rows, n_experts, n_expert_used, clamp_val,
diff --git a/ggml/src/ggml-cuda/topk-moe.cuh b/ggml/src/ggml-cuda/topk-moe.cuh
index 3637500ff..082153e6e 100644
--- a/ggml/src/ggml-cuda/topk-moe.cuh
+++ b/ggml/src/ggml-cuda/topk-moe.cuh
@@ -13,6 +13,7 @@ struct ggml_cuda_topk_moe_args {
bool delayed_softmax{};
bool prob_bias{};
bool norm{};
+ bool norm_bias{}; // the weight sum gets SCALE (sum + bias) instead of CLAMP
bool scale{};
int n_groups{}; // > 0: pick experts only from the top n_group_used expert groups
int n_group_used{};
@@ -22,7 +23,7 @@ void ggml_cuda_op_topk_moe(ggml_backend_cuda_context & ctx,
const ggml_tensor * logits,
ggml_tensor * weights,
ggml_tensor * ids,
- const ggml_tensor * clamp,
+ const ggml_tensor * denom,
const ggml_tensor * scale,
const ggml_tensor * bias,
const ggml_cuda_topk_moe_args & args);