Commit 445d5f041 for llama.cpp

commit 445d5f041cfa5b8259e4854bdf20680a42edaf67
Author: Alexander Urmuzov <urmuzov@gmail.com>
Date:   Thu Dec 3 07:31:53 2026 +0100

    deepseek41 : cut the indexer candidate mask memory (#33495)

    Assisted-by: Claude Opus 5.5

diff --git a/src/models/deepseek41.cpp b/src/models/deepseek41.cpp
index dccb067cb..419a852f0 100644
--- a/src/models/deepseek41.cpp
+++ b/src/models/deepseek41.cpp
@@ -600,13 +600,25 @@ ggml_tensor * llama_model_deepseek41::graph::build_attention41(
                     // own scores, restricted to the source's candidate blocks: 0 inside them, -inf elsewhere
                     ggml_tensor * bsel = ggml_cont(ctx0, rows(cand_sel));
                     const int64_t k_b = bsel->ne[0];
-                    ggml_tensor * bm    = ggml_fill(ctx0, ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, 1, n_blk, nc*n_stream, 1), -INFINITY);
                     ggml_tensor * zeros = ggml_fill(ctx0, ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, 1, k_b, nc*n_stream, 1), 0.0f);
-                    bm = ggml_set_rows(ctx0, bm, zeros, ggml_reshape_3d(ctx0, bsel, k_b, nc*n_stream, 1));
-                    bm = ggml_reshape_2d(ctx0, ggml_repeat_4d(ctx0, bm, bs, n_blk, nc*n_stream, 1), bs*n_blk, nc*n_stream);
-                    bm = ggml_reshape_4d(ctx0, ggml_cont(ctx0, ggml_view_2d(ctx0, bm, n_comp, nc*n_stream, bm->nb[1], 0)), n_comp, nc, 1, n_stream);
-                    cb(bm, "cand_mask", il);
-                    score = ggml_add(ctx0, score, bm);
+                    if (n_blk*bs == n_comp && ggml_is_contiguous(score)) {
+                        // add the per-block mask with broadcast, in place: no [n_comp, tokens] mask tensor
+                        // fill a computed node, not a new leaf: a leaf stays allocated for the whole graph
+                        ggml_tensor * bm = ggml_cont(ctx0, ggml_view_3d(ctx0, score, 1, n_blk, nc*n_stream, bs*ggml_element_size(score), score->nb[1], 0));
+                        bm = ggml_fill_inplace(ctx0, bm, -INFINITY);
+                        bm = ggml_set_rows(ctx0, bm, zeros, ggml_reshape_3d(ctx0, bsel, k_b, nc*n_stream, 1));
+                        bm = ggml_reshape_4d(ctx0, bm, 1, n_blk, nc, n_stream);
+                        cb(bm, "cand_mask", il);
+                        score = ggml_add_inplace(ctx0, ggml_reshape_4d(ctx0, score, bs, n_blk, nc, n_stream), bm);
+                        score = ggml_reshape_4d(ctx0, score, n_comp, nc, 1, n_stream);
+                    } else {
+                        ggml_tensor * bm = ggml_fill(ctx0, ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, 1, n_blk, nc*n_stream, 1), -INFINITY);
+                        bm = ggml_set_rows(ctx0, bm, zeros, ggml_reshape_3d(ctx0, bsel, k_b, nc*n_stream, 1));
+                        bm = ggml_reshape_2d(ctx0, ggml_repeat_4d(ctx0, bm, bs, n_blk, nc*n_stream, 1), bs*n_blk, nc*n_stream);
+                        bm = ggml_reshape_4d(ctx0, ggml_cont(ctx0, ggml_view_2d(ctx0, bm, n_comp, nc*n_stream, bm->nb[1], 0)), n_comp, nc, 1, n_stream);
+                        cb(bm, "cand_mask", il);
+                        score = ggml_add(ctx0, score, bm);
+                    }
                 }

                 parts_top_k.push_back(ggml_top_k(ctx0, score, std::min<int64_t>(n_comp, hparams.indexer_top_k)));
@@ -625,6 +637,8 @@ ggml_tensor * llama_model_deepseek41::graph::build_attention41(
             if (pick_blk) {
                 cand_sel    = join(parts_sel);
                 cand_n_comp = n_comp;
+                // compute it here: else the scores of this layer stay allocated until the first consumer layer
+                ggml_build_forward_expand(gf, cand_sel);
             }
             top_k = ggml_cont(ctx0, join(parts_top_k));
             cb(top_k, "idx_top_k", il);