From fc01285a96e22c808dd4ca6693f1f324749779ff Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:11:13 +0000 Subject: [PATCH] CUDA: topk-moe fusion with an additive epsilon in the weight norm; inkling2: pad each rel-pos bias table once Assisted-by: Claude Opus 5.5 --- ggml/src/ggml-cuda/ggml-cuda.cu | 11 +++++++---- ggml/src/ggml-cuda/topk-moe.cu | 8 ++++++-- ggml/src/ggml-cuda/topk-moe.cuh | 1 + src/models/inkling2.cpp | 7 ++++++- src/models/models.h | 1 + tests/test-backend-ops.cpp | 6 ++++++ 6 files changed, 27 insertions(+), 7 deletions(-) diff --git a/ggml/src/ggml-cuda/ggml-cuda.cu b/ggml/src/ggml-cuda/ggml-cuda.cu index b444c16d0b7f..e5a89ef1cb3f 100644 --- a/ggml/src/ggml-cuda/ggml-cuda.cu +++ b/ggml/src/ggml-cuda/ggml-cuda.cu @@ -4081,12 +4081,15 @@ 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 + //check RESHAPE->SUM_ROWS->CLAMP->DIV->RESHAPE, or SCALE (sum + eps) in place of CLAMP static const std::vector norm_ops = { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP }; args.norm = true; for (const ggml_op op : norm_ops) { - if (nodes[node_idx]->op == op && nodes[node_idx]->src[0] == nodes[node_idx - 1]) { + const ggml_tensor * n = nodes[node_idx]; + const bool eps_add = op == GGML_OP_CLAMP && n->op == GGML_OP_SCALE && ggml_get_op_params_f32(n, 0) == 1.0f; + if ((n->op == op || eps_add) && n->src[0] == nodes[node_idx - 1]) { + args.norm_eps = eps_add; node_idx++; } else { args.norm = false; @@ -5174,7 +5177,7 @@ static int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph if (args.norm) { ops.insert(ops.end(), - { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP, GGML_OP_DIV, GGML_OP_RESHAPE }); + { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, args.norm_eps ? GGML_OP_SCALE : GGML_OP_CLAMP, GGML_OP_DIV, GGML_OP_RESHAPE }); clamp = cgraph->nodes[i + ops.size() - 3]; } if (args.scale) { @@ -6725,7 +6728,7 @@ static void ggml_backend_cuda_graph_optimize(ggml_backend_t backend, ggml_cgraph if (args.norm) { ops.insert(ops.end(), - { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP, GGML_OP_DIV, GGML_OP_RESHAPE }); + { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, args.norm_eps ? 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 d6481baafb5a..03588c5bf5e3 100644 --- a/ggml/src/ggml-cuda/topk-moe.cu +++ b/ggml/src/ggml-cuda/topk-moe.cu @@ -10,6 +10,7 @@ struct topk_moe_config { bool use_sigmoid; bool use_sqrt_softplus; bool with_norm; + bool norm_add; // clamp_val is added to the sum instead of being its lower bound bool delayed_softmax; int n_groups; int n_group_used; @@ -334,7 +335,7 @@ __global__ void topk_moe_cuda(const float * logits, if (config.with_norm) { wt_sum = warp_reduce_sum(wt_sum); - wt_sum = max(wt_sum, clamp_val); + wt_sum = config.norm_add ? wt_sum + clamp_val : max(wt_sum, clamp_val); const float inv_sum = 1.0f / wt_sum; for (int i = 0; i < experts_per_thread; i++) { @@ -462,15 +463,18 @@ void ggml_cuda_op_topk_moe(ggml_backend_cuda_context & ctx, const bool with_norm = clamp != nullptr; + // CLAMP: the minimum of the sum; SCALE (with scale 1): the epsilon added to the sum + const bool norm_add = clamp && clamp->op == GGML_OP_SCALE; float clamp_val = -INFINITY; if (clamp) { - clamp_val = ggml_get_op_params_f32(clamp, 0); + clamp_val = ggml_get_op_params_f32(clamp, norm_add ? 1 : 0); } topk_moe_config config; config.use_sigmoid = args.sigmoid; config.use_sqrt_softplus = args.sqrt_softplus; config.with_norm = with_norm; + config.norm_add = norm_add; config.delayed_softmax = args.delayed_softmax; config.n_groups = args.n_groups; config.n_group_used = args.n_group_used; diff --git a/ggml/src/ggml-cuda/topk-moe.cuh b/ggml/src/ggml-cuda/topk-moe.cuh index 3637500ff99d..13e1b8575268 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_eps{}; // the norm adds an epsilon to the sum (SCALE) instead of clamping it (CLAMP) bool scale{}; int n_groups{}; // > 0: pick experts only from the top n_group_used expert groups int n_group_used{}; diff --git a/src/models/inkling2.cpp b/src/models/inkling2.cpp index 75207b04dd8e..23dacd52b01c 100644 --- a/src/models/inkling2.cpp +++ b/src/models/inkling2.cpp @@ -389,7 +389,12 @@ ggml_tensor * llama_model_inkling2::graph::build_layer(int il, int ip, ggml_tens // project r on the bias table, the attention gathers it by distance: [extent + 1, n_head, n_tokens] // the last bucket is a zero pad, for the cells that are farther than the extent - ggml_tensor * table = ggml_mul_mat(ctx0, ggml_pad(ctx0, layer.attn_rel_bias, 0, 1, 0, 0), Rcur); + // the looped cache slots share the weights of their physical layer, pad each once + ggml_tensor *& rel_bias = rel_bias_padded[layer.attn_rel_bias]; + if (!rel_bias) { + rel_bias = ggml_pad(ctx0, layer.attn_rel_bias, 0, 1, 0, 0); + } + ggml_tensor * table = ggml_mul_mat(ctx0, rel_bias, Rcur); // the sink logit t is per query: exp(s - t)/(1 + sum(exp(s - t))) = exp(s)/(exp(t) + sum(exp(s))), with a zero sink table = ggml_sub(ctx0, table, ggml_reshape_3d(ctx0, sinks, 1, n_head, n_tokens)); diff --git a/src/models/models.h b/src/models/models.h index 1792e9908bdd..754e2fd76bce 100644 --- a/src/models/models.h +++ b/src/models/models.h @@ -2570,6 +2570,7 @@ struct llama_model_inkling2 : public llama_model_base { ggml_tensor * zero_sinks = nullptr; std::map, ggml_tensor *> stacks; // see stack() + std::map rel_bias_padded; // relative-position bias table + a zero bucket }; std::unique_ptr build_arch_graph(const llm_graph_params & params) const override; diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index 0d9dbba9c3ea..476687ff26f6 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -15148,6 +15148,12 @@ static std::vector> make_test_cases_eval() { } } + // Inkling 2: sigmoid with a selection bias, 128 experts, 8 used, norm with an additive epsilon + for (float scale_w : {0.0f, 2.5f}) { + test_cases.emplace_back(new test_topk_moe({128, 1, 1, 1}, 8, true, true, GATING_FUNC_SIGMOID, scale_w, 1, true)); + test_cases.emplace_back(new test_topk_moe({128, 17, 1, 1}, 8, true, true, GATING_FUNC_SIGMOID, scale_w, 1, true)); + } + // sigmoid scoring always ranks a selection bias (GLM, DeepSeek-V2/V3.2) for (float scale_w : {0.0f, 2.0f}) { test_cases.emplace_back(new test_topk_moe({8, 22, 1, 1}, 4, true, true, GATING_FUNC_SIGMOID, scale_w, 1, true));