Commit 7135e2fd6 for llama.cpp

commit 7135e2fd6979d99f76337c81f42ef4466f801f5a
Author: Pascal <admin@serveurperso.com>
Date:   Wed Nov 25 22:39:14 2026 +0100

    metal : spread the short mat-vec lanes over src1 rows (#33034)

    * metal : spread the short mat-vec lanes over src1 rows

    The short mat-vec kernel (K < 32) mapped its 32 lanes to 32 src0 rows
    and handled one src1 row per threadgroup, so with few src0 rows most
    lanes were idle. It now covers min(ne01, 32) src0 rows of 32/that src1
    rows.

    This is the weighted sum over the depth states of Inkling 2 since
    #32855 (1-3 src0 rows, K = number of states, 2304 src1 rows).
    Inkling-2-Mini UD-Q4_K_XL, M5 Max, master -> this PR:
      pp512 888 -> 1052 t/s, pp2048 771 -> 894 t/s, tg128 unchanged.
    PPL and greedy output bit-identical, the graph is unchanged.

    Tested: test-backend-ops -b MTL0 -o MUL_MAT (new small-K cases).

    * cont : clean-up comment

    ---------

    Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>

diff --git a/ggml/src/ggml-metal/ggml-metal-device.cpp b/ggml/src/ggml-metal/ggml-metal-device.cpp
index c4e6ded8b..47a1b7c07 100644
--- a/ggml/src/ggml-metal/ggml-metal-device.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-device.cpp
@@ -1067,8 +1067,8 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_mul_mv(ggml_meta
             {
                 if (ne00 < 32) {
                     nsg = 1;
-                    nr0 = 32;
-                    nr1 = 1;
+                    nr0 = std::min(ne01, 32);
+                    nr1 = 32/nr0;
                     suffix = "_short";
                 } else {
                     nsg = std::min(4, (ne00 + 127) / 128);
diff --git a/ggml/src/ggml-metal/kernels/mul_mv.metal b/ggml/src/ggml-metal/kernels/mul_mv.metal
index 1d6123f42..08873502e 100644
--- a/ggml/src/ggml-metal/kernels/mul_mv.metal
+++ b/ggml/src/ggml-metal/kernels/mul_mv.metal
@@ -1187,11 +1187,14 @@ void kernel_mul_mv_t_t_short_impl(
         device       char * dst,
         uint3  tgpig,
         ushort tiisg) {
-    const int r0 = tgpig.x*32 + tiisg;
-    const int r1 = tgpig.y;
+    const int n0 = args.nr0;
+    const int n1 = 32/n0;
+
+    const int r0 = tgpig.x*n0 + tiisg%n0;
+    const int r1 = tgpig.y*n1 + tiisg/n0;
     const int im = tgpig.z;

-    if (r0 >= args.ne01) {
+    if (tiisg >= n0*n1 || r0 >= args.ne01 || r1 >= args.ne11) {
         return;
     }

diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 098350f9f..922d27cd0 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -12602,6 +12602,15 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
 #endif

     test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 31, 509, 2051, {1, 1}, {1, 1}));
+
+    // a few rows of a few columns against many rows, batched (the weighted sum over the depth states of Inkling 2)
+    for (int64_t m : {1, 3, 8, 33}) {
+        for (int64_t k : {2, 5, 24, 31}) {
+            test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, m, 2304, k, {1, 1}, {1, 1}));
+            test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, m, 97, k, {7, 2}, {1, 1}));
+            test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, m, 33, k, {2, 3}, {2, 1}));
+        }
+    }
     test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 32, 509, 2112, {1, 1}, {1, 1}));
     test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q8_0, GGML_TYPE_F32, 32, 509, 2112, {1, 1}, {1, 1}));