Commit 3bf411934 for llama.cpp

commit 3bf411934ca8e79759b8dd676dfdf0cf6aad3f73
Author: stornic56 <71296607+stornic56@users.noreply.github.com>
Date:   Sat Dec 5 05:31:31 2026 -0500

    models: early token crop before gate/o_proj in last layer for 5 gated-attn architectures (#33734)

    * models: spark2-5: crop to output rows before gate and o_proj in the last layer

    Move the inp_out_ids crop of the last layer (il == n_layer - 1) to run
    right after build_attn and before the gate computation and wo output
    projection. The crop is applied to cur, attn_inp and inpSA. The old
    crop after attn_out_proj is removed. n_tokens_i = cur->ne[1] is already
    dynamic.
    This saves (n_tokens - n_outputs) projections in the last layer during
    prefill on all backends.

    * models: k2-horizon: crop to output rows before gate and o_proj in the last layer

    Move the inp_out_ids crop of the last layer (il == n_layer - 1) to run
    right after build_attn and before the gate/wo output projection. The
    crop is applied to cur, inpSA, and attn_inp (only when gated). The old
    crop after the gated block is removed. Without a gate, wo stays inside
    build_attn so nothing changes there.
    This saves (n_tokens - n_outputs) projections in the last layer during
    prefill on all backends.

    * models: muse-glimmer: crop to output rows before gate and o_proj in the last layer

    Move the wqkv_gate computation from before build_attn to after the
    inp_out_ids crop of the last layer (il == n_layer - 1). The crop is
    applied to cur, attn_inp and inpSA right after build_attn, and the
    gate is then computed from the cropped attn_inp, followed by sigmoid,
    mul and wo. The old crop after attn_post_norm is removed.
    This saves (n_tokens - n_outputs) projections in the last layer during
    prefill on all backends.

    * models: ricursive: crop to output rows before gate and o_proj in the last layer

    Move the inp_out_ids crop of the last layer (il == n_layer - 1) to run
    right after build_attn and before the gate/wo output projection. The
    crop is applied to cur and inpSA, and to attn_in only when layer.wqkv_gate
    exists. The old crop after the attention block is removed. The value
    residual (inp_vres/v_in) is not touched: it feeds the KV cache and must
    see all tokens.
    This saves (n_tokens - n_outputs) projections in the last layer during
    prefill on all backends.

    * models: muse-spark: crop to output rows before gate and o_proj in the last layer

    Save attn_inp = cur before build_qkv, and move the wqkv_gate computation
    from before build_attn to after the inp_out_ids crop of the last layer
    (il == n_layer - 1). The crop is applied to cur, attn_inp and inpSA
    right after build_attn, and the gate is then computed from the cropped
    attn_inp, followed by sigmoid, mul and wo. The attn_o_norm reshapes use
    n_tok_out = cur->ne[1]. The old crop after attn_post_norm is removed.
    This saves (n_tokens - n_outputs) projections in the last layer during
    prefill on all backends.

    * tests: update MTL.csv fusion counts for muse-glimmer and muse-spark

diff --git a/src/models/k2-horizon.cpp b/src/models/k2-horizon.cpp
index 5654d5759..d350131cd 100644
--- a/src/models/k2-horizon.cpp
+++ b/src/models/k2-horizon.cpp
@@ -273,6 +273,14 @@ llama_model_k2_horizon::graph::graph(const llm_graph_params & params) : llm_grap
                     gated ? nullptr : layer.wo, gated ? nullptr : layer.wo_b,
                     Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);

+            if (il == n_layer - 1 && inp_out_ids) {
+                cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
+                inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
+                if (gated) {
+                    attn_inp = ggml_get_rows(ctx0, attn_inp, inp_out_ids);
+                }
+            }
+
             if (gated) {
                 // softplus with beta = ln(2): log2(1 + 2^x)
                 constexpr float ln2 = 0.6931471805599453f;
@@ -289,11 +297,6 @@ llama_model_k2_horizon::graph::graph(const llm_graph_params & params) : llm_grap
             }
         }

-        if (il == n_layer - 1 && inp_out_ids) {
-            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
-            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
-        }
-
         ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
         cb(ffn_inp, "ffn_inp", il);

diff --git a/src/models/muse-glimmer.cpp b/src/models/muse-glimmer.cpp
index 128776b6b..88311adb0 100644
--- a/src/models/muse-glimmer.cpp
+++ b/src/models/muse-glimmer.cpp
@@ -93,10 +93,6 @@ llama_model_muse_glimmer::graph::graph(const llm_graph_params & params)
             auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
                     n_embd_head, n_head, n_head_kv, il);

-            // gate = wqkv_gate @ attn_inp (from pre-attn hidden state)
-            ggml_tensor * gate = build_lora_mm(model.layers[il].wqkv_gate, attn_inp);
-            cb(gate, "attn_gate_proj", il);
-
             // QK-norm. attn_q_norm weight was synthesized at conversion to broadcast
             // qk_scale_factor across head_dim; attn_k_norm is identity (ones).
             Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
@@ -116,6 +112,16 @@ llama_model_muse_glimmer::graph::graph(const llm_graph_params & params)
                     Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
             cb(cur, "attn_out", il);

+            if (il == n_layer - 1 && inp_out_ids) {
+                cur      = ggml_get_rows(ctx0, cur,      inp_out_ids);
+                attn_inp = ggml_get_rows(ctx0, attn_inp, inp_out_ids);
+                inpSA    = ggml_get_rows(ctx0, inpSA,    inp_out_ids);
+            }
+
+            // gate = wqkv_gate @ attn_inp (from pre-attn hidden state)
+            ggml_tensor * gate = build_lora_mm(model.layers[il].wqkv_gate, attn_inp);
+            cb(gate, "attn_gate_proj", il);
+
             gate = ggml_sigmoid(ctx0, gate);
             cb(gate, "attn_gate_sig", il);
             cur = ggml_mul(ctx0, cur, gate);
@@ -129,11 +135,6 @@ llama_model_muse_glimmer::graph::graph(const llm_graph_params & params)
         cur = ggml_mul(ctx0, cur, model.layers[il].attn_post_norm);
         cb(cur, "attn_post_norm", il);

-        if (il == n_layer - 1 && inp_out_ids) {
-            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
-            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
-        }
-
         ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
         cb(ffn_inp, "ffn_inp", il);

diff --git a/src/models/muse-spark.cpp b/src/models/muse-spark.cpp
index 6f8442481..23be465bf 100644
--- a/src/models/muse-spark.cpp
+++ b/src/models/muse-spark.cpp
@@ -103,12 +103,11 @@ llama_model_muse_spark::graph::graph(const llm_graph_params & params)
         cb(cur, "attn_norm", il);

         {
+            ggml_tensor * attn_inp = cur;  // save input for gate computation
+
             auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
                     n_embd_head, n_head, n_head_kv, il);

-            ggml_tensor * gate = build_lora_mm(model.layers[il].wqkv_gate, cur);
-            cb(gate, "attn_gate_proj", il);
-
             // attn_q_norm carries the metap scale, attn_k_norm is identity.
             Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
             Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
@@ -126,12 +125,24 @@ llama_model_muse_spark::graph::graph(const llm_graph_params & params)
                     Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
             cb(cur, "attn_out", il);

+            if (il == n_layer - 1 && inp_out_ids) {
+                cur      = ggml_get_rows(ctx0, cur,      inp_out_ids);
+                attn_inp = ggml_get_rows(ctx0, attn_inp, inp_out_ids);
+                inpSA    = ggml_get_rows(ctx0, inpSA,    inp_out_ids);
+            }
+
+            const int64_t n_tok_out = cur->ne[1];
+
             // Weightless norm on the attention output, applied per head.
-            cur = ggml_reshape_3d(ctx0, cur, n_embd_head, n_head, n_tokens);
+            cur = ggml_reshape_3d(ctx0, cur, n_embd_head, n_head, n_tok_out);
             cur = ggml_rms_norm(ctx0, cur, hparams.f_norm_rms_eps);
-            cur = ggml_reshape_2d(ctx0, cur, n_embd_head * n_head, n_tokens);
+            cur = ggml_reshape_2d(ctx0, cur, n_embd_head * n_head, n_tok_out);
             cb(cur, "attn_o_norm", il);

+            // gate = wqkv_gate @ attn_inp (from pre-attn hidden state)
+            ggml_tensor * gate = build_lora_mm(model.layers[il].wqkv_gate, attn_inp);
+            cb(gate, "attn_gate_proj", il);
+
             gate = ggml_sigmoid(ctx0, gate);
             cb(gate, "attn_gate_sig", il);
             cur = ggml_mul(ctx0, cur, gate);
@@ -145,11 +156,6 @@ llama_model_muse_spark::graph::graph(const llm_graph_params & params)
         cur = ggml_rms_norm(ctx0, cur, hparams.f_post_norm_eps);
         cb(cur, "attn_post_norm", il);

-        if (il == n_layer - 1 && inp_out_ids) {
-            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
-            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
-        }
-
         ggml_tensor * ffn_inp = ggml_add(ctx0,
                 ggml_mul(ctx0, inpSA, model.layers[il].attn_resid_alpha),
                 ggml_mul(ctx0, cur,   model.layers[il].attn_resid_beta));
diff --git a/src/models/ricursive.cpp b/src/models/ricursive.cpp
index 07ef4420e..9c7774a24 100644
--- a/src/models/ricursive.cpp
+++ b/src/models/ricursive.cpp
@@ -173,6 +173,14 @@ llama_model_ricursive::graph::graph(const llm_graph_params & params) : llm_graph
             cur = build_attn(inp_attn, NULL, NULL, Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
             cb(cur, "attn_out", il);

+            if (il == n_layer - 1 && inp_out_ids) {
+                cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
+                inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
+                if (layer.wqkv_gate) {
+                    attn_in = ggml_get_rows(ctx0, attn_in, inp_out_ids);
+                }
+            }
+
             if (layer.wqkv_gate) {
                 ggml_tensor * gate = build_lora_mm(layer.wqkv_gate, attn_in);
                 gate = ggml_sigmoid(ctx0, gate);
@@ -184,11 +192,6 @@ llama_model_ricursive::graph::graph(const llm_graph_params & params) : llm_graph
             cur = build_lora_mm(layer.wo, cur);
         }

-        if (il == n_layer - 1 && inp_out_ids) {
-            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
-            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
-        }
-
         cur = build_norm(cur, layer.attn_post_norm, NULL, LLM_NORM_RMS, il);
         cb(cur, "attn_post_norm", il);

diff --git a/src/models/spark2-5.cpp b/src/models/spark2-5.cpp
index c9076402a..2824c9289 100644
--- a/src/models/spark2-5.cpp
+++ b/src/models/spark2-5.cpp
@@ -87,6 +87,12 @@ llama_model_spark2_5::graph::graph(const llm_graph_params & params) : llm_graph_
                 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
         cb(cur, "attn_out", il);

+        if (il == n_layer - 1 && inp_out_ids) {
+            cur      = ggml_get_rows(ctx0, cur,      inp_out_ids);
+            attn_inp = ggml_get_rows(ctx0, attn_inp, inp_out_ids);
+            inpSA    = ggml_get_rows(ctx0, inpSA,    inp_out_ids);
+        }
+
         ggml_tensor * gate = build_lora_mm(model.layers[il].wqkv_gate, attn_inp);
         gate = ggml_sigmoid(ctx0, gate);
         cb(gate, "attn_gate", il);
@@ -101,11 +107,6 @@ llama_model_spark2_5::graph::graph(const llm_graph_params & params) : llm_graph_
         cur = build_lora_mm(model.layers[il].wo, cur);
         cb(cur, "attn_out_proj", il);

-        if (il == n_layer - 1 && inp_out_ids) {
-            cur = ggml_get_rows(ctx0, cur, inp_out_ids);
-            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
-        }
-
         ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
         cb(ffn_inp, "ffn_inp", il);

diff --git a/tests/fusion/MTL.csv b/tests/fusion/MTL.csv
index 075cfc7c3..536f23d76 100644
--- a/tests/fusion/MTL.csv
+++ b/tests/fusion/MTL.csv
@@ -274,12 +274,12 @@ mistral4            ,1   ,any     ,RMS_NORM+MUL                ,      9
 mistral4            ,1   ,any     ,UNARY+ADD+ARGSORT+GET_ROWS  ,      1
 moshi               ,0   ,any     ,RMS_NORM+MUL                ,      5
 mpt                 ,0   ,any     ,NORM+MUL+ADD                ,      5
-muse-glimmer        ,0   ,any     ,RMS_NORM+MUL                ,     10
-muse-glimmer        ,0   ,any     ,RMS_NORM+MUL+ADD            ,      3
+muse-glimmer        ,0   ,any     ,RMS_NORM+MUL                ,      9
+muse-glimmer        ,0   ,any     ,RMS_NORM+MUL+ADD            ,      4
 muse-spark          ,1   ,prefill ,MUL+ADD                     ,      2
 muse-spark          ,1   ,decode  ,MUL+ADD                     ,      4
 muse-spark          ,1   ,any     ,RMS_NORM+MUL                ,     11
-muse-spark          ,1   ,any     ,RMS_NORM+MUL+ADD            ,      3
+muse-spark          ,1   ,any     ,RMS_NORM+MUL+ADD            ,      4
 muse-spark          ,1   ,any     ,UNARY+ADD+ARGSORT+GET_ROWS+SUM_ROWS+CLAMP+DIV,      2
 nanbeige            ,0   ,any     ,RMS_NORM+MUL                ,      5
 nemotron            ,0   ,any     ,NORM+MUL+ADD                ,      5