Commit 7d58ebd90 for llama.cpp
commit 7d58ebd90fb169f49ef5c55c953ef8c8e1413096
Author: Jeff Bolz <jbolz@nvidia.com>
Date: Wed Dec 2 01:19:34 2026 -0600
vulkan: update topk_moe to handle bias rather than clamp (#32957)
* vulkan: update topk_moe to handle bias rather than clamp
* use log1p in topk_moe
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan-push-constants.h b/ggml/src/ggml-vulkan/ggml-vulkan-push-constants.h
index 72e7b26a4..9d957fa14 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan-push-constants.h
+++ b/ggml/src/ggml-vulkan/ggml-vulkan-push-constants.h
@@ -543,6 +543,7 @@ struct vk_op_topk_moe_push_constants {
uint32_t n_expert_used;
float clamp_min;
float clamp_max;
+ float norm_bias;
uint32_t gating_func;
uint32_t has_bias;
uint32_t with_norm;
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan-types.h b/ggml/src/ggml-vulkan/ggml-vulkan-types.h
index ea22c6135..cdb4327d3 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan-types.h
+++ b/ggml/src/ggml-vulkan/ggml-vulkan-types.h
@@ -549,21 +549,24 @@ static constexpr uint32_t fwht_max_subgroup_n = 512;
static constexpr uint32_t fwht_max_subgroup_el_w = 64;
static constexpr uint32_t fwht_rows = 4;
+template <ggml_op NormOp>
static constexpr std::initializer_list<ggml_op> topk_moe_early_softmax_norm{ GGML_OP_SOFT_MAX, GGML_OP_RESHAPE, GGML_OP_ARGSORT,
GGML_OP_VIEW, GGML_OP_GET_ROWS, GGML_OP_RESHAPE,
- GGML_OP_SUM_ROWS, GGML_OP_CLAMP, GGML_OP_DIV,
+ GGML_OP_SUM_ROWS, NormOp, GGML_OP_DIV,
GGML_OP_RESHAPE };
+template <ggml_op NormOp>
static constexpr std::initializer_list<ggml_op> topk_moe_sigmoid_norm_bias{ GGML_OP_UNARY, GGML_OP_RESHAPE, GGML_OP_ADD,
GGML_OP_ARGSORT, GGML_OP_VIEW, GGML_OP_GET_ROWS,
- GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP,
+ GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, NormOp,
GGML_OP_DIV, GGML_OP_RESHAPE };
+template <ggml_op NormOp>
static constexpr std::initializer_list<ggml_op> topk_moe_sqrt_softplus_norm_bias{ GGML_OP_UNARY, GGML_OP_SQRT,
GGML_OP_RESHAPE, GGML_OP_ADD,
GGML_OP_ARGSORT, GGML_OP_VIEW,
GGML_OP_GET_ROWS, GGML_OP_RESHAPE,
- GGML_OP_SUM_ROWS, GGML_OP_CLAMP,
+ GGML_OP_SUM_ROWS, NormOp,
GGML_OP_DIV, GGML_OP_RESHAPE };
static constexpr std::initializer_list<ggml_op> topk_moe_early_softmax { GGML_OP_SOFT_MAX, GGML_OP_RESHAPE, GGML_OP_ARGSORT,
@@ -618,9 +621,9 @@ static constexpr std::initializer_list<std::array<int, 3>> topk_moe_early_softma
{ 4, 1, 3 }, // get_rows->src[1] == view
{ 5, 0, 4 }, // reshape->src[0] == get_rows
{ 6, 0, 5 }, // sum_rows->src[0] == reshape
- { 7, 0, 6 }, // clamp->src[0] == sum_rows
+ { 7, 0, 6 }, // denom->src[0] == sum_rows
{ 8, 0, 5 }, // div->src[0] == reshape
- { 8, 1, 7 }, // div->src[1] == clamp
+ { 8, 1, 7 }, // div->src[1] == denom
{ 9, 0, 8 }, // reshape->src[0] == div
};
@@ -633,9 +636,9 @@ static constexpr std::initializer_list<std::array<int, 3>> topk_moe_sigmoid_norm
{ 5, 1, 4 }, // get_rows->src[1] == view
{ 6, 0, 5 }, // reshape->src[0] == get_rows
{ 7, 0, 6 }, // sum_rows->src[0] == reshape
- { 8, 0, 7 }, // clamp->src[0] == sum_rows
+ { 8, 0, 7 }, // denom->src[0] == sum_rows
{ 9, 0, 6 }, // div->src[0] == reshape
- { 9, 1, 8 }, // div->src[1] == clamp
+ { 9, 1, 8 }, // div->src[1] == denom
{10, 0, 9 }, // reshape->src[0] == div
};
@@ -649,9 +652,9 @@ static constexpr std::initializer_list<std::array<int, 3>> topk_moe_sqrt_softplu
{ 6, 1, 5 }, // get_rows->src[1] == view
{ 7, 0, 6 }, // reshape->src[0] == get_rows
{ 8, 0, 7 }, // sum_rows->src[0] == reshape
- { 9, 0, 8 }, // clamp->src[0] == sum_rows
+ { 9, 0, 8 }, // denom->src[0] == sum_rows
{10, 0, 7 }, // div->src[0] == reshape
- {10, 1, 9 }, // div->src[1] == clamp
+ {10, 1, 9 }, // div->src[1] == denom
{11, 0,10 }, // reshape->src[0] == div
};
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
index 0ce67f21d..67fafc52c 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
@@ -13058,23 +13058,19 @@ void ggml_vk_topk_moe(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_cg
pc.n_expert_used = n_expert_used;
pc.clamp_min = -std::numeric_limits<float>::infinity();
pc.clamp_max = std::numeric_limits<float>::infinity();
- if (mode == TOPK_MOE_EARLY_SOFTMAX_NORM) {
- ggml_tensor * clamp = cgraph->nodes[node_idx + 7];
- GGML_ASSERT(clamp->op == GGML_OP_CLAMP);
- pc.clamp_min = ggml_get_op_params_f32(clamp, 0);
- pc.clamp_max = ggml_get_op_params_f32(clamp, 1);
- }
- if (mode == TOPK_MOE_SIGMOID_NORM_BIAS) {
- ggml_tensor * clamp = cgraph->nodes[node_idx + 8];
- GGML_ASSERT(clamp->op == GGML_OP_CLAMP);
- pc.clamp_min = ggml_get_op_params_f32(clamp, 0);
- pc.clamp_max = ggml_get_op_params_f32(clamp, 1);
- }
- if (mode == TOPK_MOE_SQRT_SOFTPLUS_NORM_BIAS) {
- ggml_tensor * clamp = cgraph->nodes[node_idx + 9];
- GGML_ASSERT(clamp->op == GGML_OP_CLAMP);
- pc.clamp_min = ggml_get_op_params_f32(clamp, 0);
- pc.clamp_max = ggml_get_op_params_f32(clamp, 1);
+ const int denom_offset = mode == TOPK_MOE_EARLY_SOFTMAX_NORM ? 7 :
+ mode == TOPK_MOE_SIGMOID_NORM_BIAS ? 8 :
+ mode == TOPK_MOE_SQRT_SOFTPLUS_NORM_BIAS ? 9 : 0;
+ pc.with_norm = denom_offset != 0;
+ if (pc.with_norm) {
+ const ggml_tensor * denom = cgraph->nodes[node_idx + denom_offset];
+ if (denom->op == GGML_OP_SCALE) {
+ pc.norm_bias = ggml_get_op_params_f32(denom, 1);
+ } else {
+ GGML_ASSERT(denom->op == GGML_OP_CLAMP);
+ pc.clamp_min = ggml_get_op_params_f32(denom, 0);
+ pc.clamp_max = ggml_get_op_params_f32(denom, 1);
+ }
}
#define GATING_FUNC_SOFTMAX 0
@@ -13087,7 +13083,6 @@ void ggml_vk_topk_moe(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_cg
mode == TOPK_MOE_LATE_SOFTMAX ? GATING_FUNC_SOFTMAX_WEIGHT :
GATING_FUNC_SOFTMAX;
pc.has_bias = has_bias;
- pc.with_norm = mode == TOPK_MOE_EARLY_SOFTMAX_NORM || has_bias;
if (ctx->fused_topk_moe_scale) {
GGML_ASSERT(weights->op == GGML_OP_SCALE);
pc.output_scale = ggml_get_op_params_f32(weights, 0);
@@ -15740,6 +15735,32 @@ bool ggml_vk_can_fuse_ssm_conv(const ggml_backend_vk_context * ctx, const struct
return true;
}
+static const std::initializer_list<ggml_op> & ggml_vk_topk_moe_norm_pattern(
+ const ggml_cgraph * cgraph, int node_idx, topk_moe_mode mode) {
+ const auto has_norm_bias = [&](int pattern_size) {
+ const int denom_idx = node_idx + pattern_size - 3;
+ if (denom_idx >= cgraph->n_nodes) {
+ return false;
+ }
+ const ggml_tensor * denom = cgraph->nodes[denom_idx];
+ return denom->op == GGML_OP_SCALE && ggml_get_op_params_f32(denom, 0) == 1.0f;
+ };
+
+ switch (mode) {
+ case TOPK_MOE_EARLY_SOFTMAX_NORM:
+ return has_norm_bias(topk_moe_early_softmax_norm<GGML_OP_CLAMP>.size())
+ ? topk_moe_early_softmax_norm<GGML_OP_SCALE> : topk_moe_early_softmax_norm<GGML_OP_CLAMP>;
+ case TOPK_MOE_SIGMOID_NORM_BIAS:
+ return has_norm_bias(topk_moe_sigmoid_norm_bias<GGML_OP_CLAMP>.size())
+ ? topk_moe_sigmoid_norm_bias<GGML_OP_SCALE> : topk_moe_sigmoid_norm_bias<GGML_OP_CLAMP>;
+ case TOPK_MOE_SQRT_SOFTPLUS_NORM_BIAS:
+ return has_norm_bias(topk_moe_sqrt_softplus_norm_bias<GGML_OP_CLAMP>.size())
+ ? topk_moe_sqrt_softplus_norm_bias<GGML_OP_SCALE> : topk_moe_sqrt_softplus_norm_bias<GGML_OP_CLAMP>;
+ default:
+ GGML_ABORT("invalid normalized top-k MoE mode");
+ }
+}
+
bool ggml_vk_can_fuse_topk_moe(ggml_backend_vk_context * ctx, const struct ggml_cgraph * cgraph,
int node_idx, topk_moe_mode mode) {
@@ -16475,28 +16496,28 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
ctx->fused_topk_qsa = true;
fusion_string = "TOPK_QSA";
std::fill_n(op_srcs_fused_elementwise, ctx->num_additional_fused_ops + 1, false);
- } else if (ggml_can_fuse_subgraph(cgraph, i, topk_moe_early_softmax_norm, { i + 3, i + 9 }) &&
+ } else if (ggml_can_fuse_subgraph(cgraph, i, ggml_vk_topk_moe_norm_pattern(cgraph, i, TOPK_MOE_EARLY_SOFTMAX_NORM), { i + 3, i + 9 }) &&
ggml_check_edges(cgraph, i, topk_moe_early_softmax_norm_edges) &&
ggml_vk_can_fuse_topk_moe(ctx, cgraph, i, TOPK_MOE_EARLY_SOFTMAX_NORM)) {
- ctx->num_additional_fused_ops = topk_moe_early_softmax_norm.size() - 1;
+ ctx->num_additional_fused_ops = topk_moe_early_softmax_norm<GGML_OP_CLAMP>.size() - 1;
// view of argsort writes to memory
ctx->fused_ops_write_mask |= 1 << 3;
ctx->fused_topk_moe_mode = TOPK_MOE_EARLY_SOFTMAX_NORM;
fusion_string = "TOPK_MOE_EARLY_SOFTMAX_NORM";
std::fill_n(op_srcs_fused_elementwise, ctx->num_additional_fused_ops + 1, false);
- } else if (ggml_can_fuse_subgraph(cgraph, i, topk_moe_sigmoid_norm_bias, { i + 4, i + 10 }) &&
+ } else if (ggml_can_fuse_subgraph(cgraph, i, ggml_vk_topk_moe_norm_pattern(cgraph, i, TOPK_MOE_SIGMOID_NORM_BIAS), { i + 4, i + 10 }) &&
ggml_check_edges(cgraph, i, topk_moe_sigmoid_norm_bias_edges) &&
ggml_vk_can_fuse_topk_moe(ctx, cgraph, i, TOPK_MOE_SIGMOID_NORM_BIAS)) {
- ctx->num_additional_fused_ops = topk_moe_sigmoid_norm_bias.size() - 1;
+ ctx->num_additional_fused_ops = topk_moe_sigmoid_norm_bias<GGML_OP_CLAMP>.size() - 1;
// view of argsort writes to memory
ctx->fused_ops_write_mask |= 1 << 4;
ctx->fused_topk_moe_mode = TOPK_MOE_SIGMOID_NORM_BIAS;
fusion_string = "TOPK_MOE_SIGMOID_NORM_BIAS";
std::fill_n(op_srcs_fused_elementwise, ctx->num_additional_fused_ops + 1, false);
- } else if (ggml_can_fuse_subgraph(cgraph, i, topk_moe_sqrt_softplus_norm_bias, { i + 5, i + 11 }) &&
+ } else if (ggml_can_fuse_subgraph(cgraph, i, ggml_vk_topk_moe_norm_pattern(cgraph, i, TOPK_MOE_SQRT_SOFTPLUS_NORM_BIAS), { i + 5, i + 11 }) &&
ggml_check_edges(cgraph, i, topk_moe_sqrt_softplus_norm_bias_edges) &&
ggml_vk_can_fuse_topk_moe(ctx, cgraph, i, TOPK_MOE_SQRT_SOFTPLUS_NORM_BIAS)) {
- ctx->num_additional_fused_ops = topk_moe_sqrt_softplus_norm_bias.size() - 1;
+ ctx->num_additional_fused_ops = topk_moe_sqrt_softplus_norm_bias<GGML_OP_CLAMP>.size() - 1;
// view of argsort writes to memory
ctx->fused_ops_write_mask |= 1 << 5;
ctx->fused_topk_moe_mode = TOPK_MOE_SQRT_SOFTPLUS_NORM_BIAS;
@@ -16814,13 +16835,13 @@ void ggml_vk_graph_optimize(ggml_backend_t backend, struct ggml_cgraph * graph,
return keep_pattern(pattern);
};
- if (keep_topk_moe_pattern(topk_moe_early_softmax_norm)) {
+ if (keep_topk_moe_pattern(ggml_vk_topk_moe_norm_pattern(graph, first_unused, TOPK_MOE_EARLY_SOFTMAX_NORM))) {
continue;
}
- if (keep_topk_moe_pattern(topk_moe_sigmoid_norm_bias)) {
+ if (keep_topk_moe_pattern(ggml_vk_topk_moe_norm_pattern(graph, first_unused, TOPK_MOE_SIGMOID_NORM_BIAS))) {
continue;
}
- if (keep_topk_moe_pattern(topk_moe_sqrt_softplus_norm_bias)) {
+ if (keep_topk_moe_pattern(ggml_vk_topk_moe_norm_pattern(graph, first_unused, TOPK_MOE_SQRT_SOFTPLUS_NORM_BIAS))) {
continue;
}
if (keep_topk_moe_pattern(topk_moe_early_softmax)) {
@@ -16884,9 +16905,9 @@ void ggml_vk_graph_optimize(ggml_backend_t backend, struct ggml_cgraph * graph,
}
return false;
};
- if (match_pattern(topk_moe_early_softmax_norm, j) ||
- match_pattern(topk_moe_sigmoid_norm_bias, j) ||
- match_pattern(topk_moe_sqrt_softplus_norm_bias, j) ||
+ if (match_pattern(ggml_vk_topk_moe_norm_pattern(graph, j, TOPK_MOE_EARLY_SOFTMAX_NORM), j) ||
+ match_pattern(ggml_vk_topk_moe_norm_pattern(graph, j, TOPK_MOE_SIGMOID_NORM_BIAS), j) ||
+ match_pattern(ggml_vk_topk_moe_norm_pattern(graph, j, TOPK_MOE_SQRT_SOFTPLUS_NORM_BIAS), j) ||
match_pattern(topk_moe_early_softmax, j) ||
match_pattern(topk_moe_late_softmax, j) ||
match_pattern(snake_pattern, j) ||
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/topk_moe.comp b/ggml/src/ggml-vulkan/vulkan-shaders/topk_moe.comp
index d219201fd..c8fa332c4 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/topk_moe.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/topk_moe.comp
@@ -6,6 +6,7 @@
#extension GL_KHR_shader_subgroup_shuffle : enable
#include "types.glsl"
+#include "utils.glsl"
#define GATING_FUNC_SOFTMAX 0
#define GATING_FUNC_SIGMOID 1
@@ -19,6 +20,7 @@ layout (push_constant) uniform parameter
uint n_expert_used;
float clamp_min;
float clamp_max;
+ float norm_bias;
uint gating_func;
uint has_bias;
uint with_norm;
@@ -126,7 +128,7 @@ void main() {
for (uint i = 0; i < n_experts; i += WARP_SIZE) {
const uint expert = i + lane;
const float val = probs[i / WARP_SIZE];
- probs[i / WARP_SIZE] = (n_experts % WARP_SIZE == 0 || expert < n_experts) ? sqrt(val > 20.0f ? val : log(1.0f + exp(val))) : -INFINITY;
+ probs[i / WARP_SIZE] = (n_experts % WARP_SIZE == 0 || expert < n_experts) ? sqrt(val > 20.0f ? val : log1p(exp(val))) : -INFINITY;
}
}
@@ -198,7 +200,7 @@ void main() {
if (with_norm != 0) {
wt_sum = subgroupAdd(wt_sum);
- wt_sum = clamp(wt_sum, clamp_min, clamp_max);
+ wt_sum = clamp(wt_sum, clamp_min, clamp_max) + norm_bias;
const float inv_sum = 1.0f / wt_sum;
[[unroll]]
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index cbb3b6846..7acff64cf 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -15648,6 +15648,9 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_topk_moe({384, 1, 1, 1}, 6, true, true, GATING_FUNC_SIGMOID, scale_w, 1, true));
}
+ test_cases.emplace_back(new test_topk_moe({384, 1, 1, 1}, 6, true, true, GATING_FUNC_SQRT_SOFTPLUS, 1.5f, 1, true)); // DeepSeek-V4.1
+ test_cases.emplace_back(new test_topk_moe({128, 1, 1, 1}, 8, true, true, GATING_FUNC_SIGMOID, 8.0f, 1, true)); // Inkling 2 Mini
+
// Cover the supported boundaries, common k = 8 shapes, interleaved views and adds, and k = 16 fallback.
test_cases.emplace_back(new test_moe_reduce(63, 2, 17));
test_cases.emplace_back(new test_moe_reduce(2048, 8, 128));