Skip to content

LLM inference in C/C++

C++ ggml

qwen4exp: sum the indexer heads by slices (#28023)

Pascal09412af38a9f
diff --git a/src/models/qwen4exp.cpp b/src/models/qwen4exp.cpp
index abf6a0502f..74235f2295 100644
--- a/src/models/qwen4exp.cpp
+++ b/src/models/qwen4exp.cpp
@@ -576,12 +576,19 @@ ggml_tensor * llama_model_qwen4exp::graph::build_qsa_top_k(
// rectify each head dot product before the sum, as in the DeepSeek lightning indexer
// mul_mat matches ne[2], so the queries of stream s only meet the blocks of stream s
ggml_tensor * score = ggml_mul_mat(ctx0, pooled,
- ggml_reshape_3d(ctx0, ggml_cont(ctx0, q), idx_dim, n_idx_h*n_tps, n_stream));
+ ggml_reshape_3d(ctx0, q, idx_dim, n_idx_h*n_tps, n_stream));
score = ggml_reshape_4d(ctx0, score, n_blocks, n_idx_h, n_tps, n_stream);
score = ggml_relu(ctx0, score);
- score = ggml_cont(ctx0, ggml_permute(ctx0, score, 1, 0, 2, 3));
- score = ggml_sum_rows(ctx0, score);
- score = ggml_reshape_3d(ctx0, score, n_blocks, n_tps, n_stream);
+
+ // the heads sit side by side on ne[1] and there are only a few of them
+ ggml_tensor * summed = nullptr;
+ for (int64_t h = 0; h < n_idx_h; ++h) {
+ ggml_tensor * slice = ggml_view_3d(ctx0, score, n_blocks, n_tps, n_stream,
+ score->nb[2], score->nb[3], h*score->nb[1]);
+ summed = summed ? ggml_add(ctx0, summed, slice) : ggml_cont(ctx0, slice);
+ }
+
+ score = summed;
cb(score, "indexer_score", il);
// one value per block, so it is cheaper to bias here than after the cells are expanded