Commit 4c0ad42e6 for llama.cpp

commit 4c0ad42e6886b748ca8f5892f59312b65076de2e
Author: stornic56 <71296607+stornic56@users.noreply.github.com>
Date:   Thu Dec 3 08:25:09 2026 -0500

    models: laguna/afmoe: move inp_out_ids crop before attn_o_proj in last layer (#33557)

    * models: laguna/afmoe: move inp_out_ids crop before attn_o_proj in last layer

    Advance the ggml_get_rows(..., inp_out_ids) crop of the last layer
    (il == n_layer - 1) to run before the attention output projection wo
    (attn_o_proj) in laguna.cpp and afmoe.cpp, and before attn_post_norm in
    afmoe.cpp.

    Since build_lora_mm (wo) and build_norm operate strictly row-wise
    without mixing positions or touching the KV cache, advancing the crop is
    mathematically equivalent and saves (n_tokens - n_outputs) projections
    in the last layer during prefill on all backends.

    test-llama-archs: CPU + Vulkan0, laguna/afmoe, all OK.
    NMSE laguna 2.38e-07, afmoe 8.83e-08 vs CPU.

    AI usage disclosure: assisted

    * tests: update MTL.csv fusion counts for afmoe

diff --git a/src/models/afmoe.cpp b/src/models/afmoe.cpp
index edd616035..9238d781f 100644
--- a/src/models/afmoe.cpp
+++ b/src/models/afmoe.cpp
@@ -174,6 +174,11 @@ llama_model_afmoe::graph::graph(const llm_graph_params & params) : llm_graph_con
             cb(cur, "attn_gated", il);

             // now apply output projection
+            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_lora_mm(model.layers[il].wo, cur);
             cb(cur, "attn_o_proj", il);
         }
@@ -184,11 +189,6 @@ llama_model_afmoe::graph::graph(const llm_graph_params & params) : llm_graph_con
                 LLM_NORM_RMS, il);
         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/laguna.cpp b/src/models/laguna.cpp
index 177b31451..07b5f5adf 100644
--- a/src/models/laguna.cpp
+++ b/src/models/laguna.cpp
@@ -251,15 +251,15 @@ llama_model_laguna::graph::graph(const llm_graph_params & params) : llm_graph_co
             }
             cb(cur, "attn_gated", 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);
+            }
+
             cur = build_lora_mm(model.layers[il].wo, cur);
             cb(cur, "attn_o_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 3bcd1d37a..075cfc7c3 100644
--- a/tests/fusion/MTL.csv
+++ b/tests/fusion/MTL.csv
@@ -1,8 +1,8 @@
 # test-fusion baseline for device MTL
 # arch              ,moe ,mode    ,label                       ,  count
 afmoe               ,1   ,any     ,MUL+ADD                     ,      2
-afmoe               ,1   ,any     ,RMS_NORM+MUL                ,     10
-afmoe               ,1   ,any     ,RMS_NORM+MUL+ADD            ,      3
+afmoe               ,1   ,any     ,RMS_NORM+MUL                ,      9
+afmoe               ,1   ,any     ,RMS_NORM+MUL+ADD            ,      4
 afmoe               ,1   ,any     ,UNARY+ADD+ARGSORT+GET_ROWS  ,      1
 apertus             ,0   ,any     ,RMS_NORM+MUL                ,      9
 arcee               ,0   ,any     ,RMS_NORM+MUL                ,      5