Commit ab789a246 for llama.cpp

commit ab789a246c1a59fbade35a28100bc2d38ea19188
Author: Georgi Gerganov <ggerganov@gmail.com>
Date:   Mon Nov 23 21:01:41 2026 +0200

    metal : batch concat rows and copy them in vector chunks (#32824)

    * metal : batch concat rows and copy them in vector chunks

    The concat kernel copied one element per thread, so wide rows with many rows ran at a
    fraction of the memory bandwidth. Batch several rows per threadgroup and copy the
    contiguous dim-0 elements in 4-element chunks when the row is contiguous.

    Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)

    * test : cover concat vector and misaligned row paths

    Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (RTX 5090 32GB, DDR5 128GB)

    * metal : hoist the concat src1 row offset into scalars

    Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (RTX 5090 32GB, DDR5 128GB)

    * metal : peel to align the concat vector copy

    Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (RTX 5090 32GB, DDR5 128GB)

    * metal : vectorize concat rows along any dimension

    Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (RTX 5090 32GB, DDR5 128GB)

    * metal : specialize concat with function constants

    Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (RTX 5090 32GB, DDR5 128GB)

    * metal : drop the concat row-batch dispatch

    Batching several rows per threadgroup reduces the number of threads by
    the batch factor. On machines with a large GPU this leaves too few
    requests in flight and the copy becomes latency bound:

      M5 Ultra (36 CPU)   f16 [1024,8,256,1] d=0   8.1 us -> 17.2 us
                          f32 [4096,8,256,1] d=0  35.4 us -> 118.5 us
                          f16 [4096,8,256,1] d=2  50.9 us -> 102.0 us

    An interleaved sweep of the (nth, nrptg) dispatch over the concat perf
    cases on an M2 Ultra and an M5 Ultra shows that the base dispatch is
    best or tied for every shape once the rows are vectorized, and that the
    gains of the previous commit come from the vector copy alone:

      M2 Ultra  f16 [1024,8,256,1] d=0  32.4 us -> 17.0 us
      M5 Ultra  f16 [1024,8,256,1] d=0  13.3 us ->  8.1 us

    Keep the vector copy and the function-constant specialization, and
    return to the original dispatch.

    Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (RTX 5090 32GB, DDR5 128GB)

    * metal : revert the concat function constants

    Specializing the concat kernel on the concat dimension and on whether a
    row is split across threadgroups with function constants makes the
    strided scalar path slower. Interleaved over the concat perf cases on an
    M5 Ultra, the specialized kernel is not faster than the plain one, and
    it costs up to 18% on shapes that are already at the memory bandwidth
    limit:

      f32 [2050,8,256,1] b=255 d=0  21.0 us -> 25.1 us
      f32 [2050,8,256,1] b=256 d=2  31.1 us -> 36.7 us
      f16 [4096,8,256,1] b=256 d=2  31.2 us -> 36.7 us

    Revert the specialization and keep copying rows with the runtime
    arguments. The concat change is now limited to the kernel itself.

    Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (RTX 5090 32GB, DDR5 128GB)

diff --git a/ggml/src/ggml-metal/kernels/quantize.metal b/ggml/src/ggml-metal/kernels/quantize.metal
index 663e47d5f..b2624565d 100644
--- a/ggml/src/ggml-metal/kernels/quantize.metal
+++ b/ggml/src/ggml-metal/kernels/quantize.metal
@@ -159,6 +159,47 @@ template [[host_name("kernel_cpy_q8_0_f16")]] kernel cpy_q_f_t kernel_cpy_q_f32<

 template [[host_name("kernel_cpy_tq2_0_f16")]] kernel cpy_q_f_t kernel_cpy_q_f32<half4x4, block_tq2_0, QK_NL, dequantize_tq2_0>;

+template<typename T>
+static void concat_copy(device const char * src, device char * dst, const int n, const int tid, const int nth) {
+    if (n <= 0) {
+        return;
+    }
+
+    constexpr int N = 16/sizeof(T);
+    typedef vec<T, N> T16;
+
+    device const T * x = (device const T *)src;
+    device       T * y = (device       T *)dst;
+
+    const size_t rx = (size_t)x % 16;
+
+    if (rx != (size_t)y % 16 || rx % sizeof(T) != 0) {
+        for (int i = tid; i < n; i += nth) {
+            y[i] = x[i];
+        }
+
+        return;
+    }
+
+    const int np  = min((int)(((16 - rx) % 16)/sizeof(T)), n);
+    const int n16 = (n - np)/N;
+
+    for (int i = tid; i < np; i += nth) {
+        y[i] = x[i];
+    }
+
+    device const T16 * x16 = (device const T16 *)(x + np);
+    device       T16 * y16 = (device       T16 *)(y + np);
+
+    for (int i = tid; i < n16; i += nth) {
+        y16[i] = x16[i];
+    }
+
+    for (int i = np + n16*N + tid; i < n; i += nth) {
+        y[i] = x[i];
+    }
+}
+
 template<typename T>
 kernel void kernel_concat(
         constant ggml_metal_kargs_concat & args,
@@ -178,20 +219,39 @@ kernel void kernel_concat(
         return;
     }

-    int o[4] = {0, 0, 0, 0};
-    o[args.dim] = args.dim == 0 ? args.ne00 : (args.dim == 1 ? args.ne01 : (args.dim == 2 ? args.ne02 : args.ne03));
+    const int o0 = args.dim == 0 ? args.ne00 : 0;
+    const int o1 = args.dim == 1 ? args.ne01 : 0;
+    const int o2 = args.dim == 2 ? args.ne02 : 0;
+    const int o3 = args.dim == 3 ? args.ne03 : 0;

     // chunk ic of nc0 along the row
     const int n0  = (args.ne0 + args.nc0 - 1)/args.nc0;
-    const int i0e = min(args.ne0, (ic + 1)*n0);
+    const int i0b = ntg.y == 1 ? ic*n0 : 0;
+    const int i0e = ntg.y == 1 ? min(args.ne0, (ic + 1)*n0) : args.ne0;

-    for (int i0 = ic*n0 + tpitg.x; i0 < i0e; i0 += ntg.x) {
+    if (args.nb0 == sizeof(T) && args.nb00 == sizeof(T) && args.nb10 == sizeof(T)) {
+        const bool in0 = i1 < args.ne01 && i2 < args.ne02 && i3 < args.ne03;
+
+        const int na = in0 ? min(i0e - i0b, max(0, args.ne00 - i0b)) : 0;
+        const int nb = i0e - i0b - na;
+
+        device const char * r0 = src0 + i3*args.nb03 + i2*args.nb02 + i1*args.nb01;
+        device const char * r1 = src1 + (i3 - o3)*args.nb13 + (i2 - o2)*args.nb12 + (i1 - o1)*args.nb11;
+        device       char * rd = dst  + i3*args.nb3  + i2*args.nb2  + i1*args.nb1;
+
+        concat_copy<T>(r0 + i0b*sizeof(T), rd + i0b*sizeof(T), na, tpitg.x, ntg.x);
+        concat_copy<T>(r1 + (i0b + na - o0)*sizeof(T), rd + (i0b + na)*sizeof(T), nb, tpitg.x, ntg.x);
+
+        return;
+    }
+
+    for (int i0 = i0b + tpitg.x; i0 < i0e; i0 += ntg.x) {
         device const T * x;

         if (i0 < args.ne00 && i1 < args.ne01 && i2 < args.ne02 && i3 < args.ne03) {
             x = (device const T *)(src0 + (i3       )*args.nb03 + (i2       )*args.nb02 + (i1       )*args.nb01 + (i0       )*args.nb00);
         } else {
-            x = (device const T *)(src1 + (i3 - o[3])*args.nb13 + (i2 - o[2])*args.nb12 + (i1 - o[1])*args.nb11 + (i0 - o[0])*args.nb10);
+            x = (device const T *)(src1 + (i3 - o3)*args.nb13 + (i2 - o2)*args.nb12 + (i1 - o1)*args.nb11 + (i0 - o0)*args.nb10);
         }

         device T * y = (device T *)(dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
@@ -232,8 +292,10 @@ kernel void kernel_concat_q(
         return;
     }

-    int o[4] = {0, 0, 0, 0};
-    o[args.dim] = args.dim == 0 ? args.ne00 : (args.dim == 1 ? args.ne01 : (args.dim == 2 ? args.ne02 : args.ne03));
+    const int o0 = args.dim == 0 ? args.ne00 : 0;
+    const int o1 = args.dim == 1 ? args.ne01 : 0;
+    const int o2 = args.dim == 2 ? args.ne02 : 0;
+    const int o3 = args.dim == 3 ? args.ne03 : 0;

     // chunk ic of nc0 along the row
     const int n0  = (args.ne0 + args.nc0 - 1)/args.nc0;
@@ -245,7 +307,7 @@ kernel void kernel_concat_q(
         if (i0 < args.ne00 && i1 < args.ne01 && i2 < args.ne02 && i3 < args.ne03) {
             x = (device const block_q *)(src0 + (i3       )*args.nb03 + (i2       )*args.nb02 + (i1       )*args.nb01 + (i0       )*args.nb00);
         } else {
-            x = (device const block_q *)(src1 + (i3 - o[3])*args.nb13 + (i2 - o[2])*args.nb12 + (i1 - o[1])*args.nb11 + (i0 - o[0])*args.nb10);
+            x = (device const block_q *)(src1 + (i3 - o3)*args.nb13 + (i2 - o2)*args.nb12 + (i1 - o1)*args.nb11 + (i0 - o0)*args.nb10);
         }

         device block_q * y = (device block_q *)(dst + i3*args.nb3 + i2*args.nb2 + i1*args.nb1 + i0*args.nb0);
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 84f0e5709..a0268b638 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -13201,6 +13201,21 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
         test_cases.emplace_back(new test_concat(GGML_TYPE_Q8_0, {8192, 2, 1, 1}, dim == 0 ? 4096 : 1, dim, 0));
     }

+    for (ggml_type type : { GGML_TYPE_F32, GGML_TYPE_F16 }) {
+        test_cases.emplace_back(new test_concat(type, {64, 4, 512, 1}, 512, 2, 0));
+        test_cases.emplace_back(new test_concat(type, {66, 4, 512, 1}, 512, 2, 0));
+        test_cases.emplace_back(new test_concat(type, {128, 4, 512, 1}, 512, 2, 1));
+
+        for (int dim : { 0, 1, 3 }) {
+            test_cases.emplace_back(new test_concat(type, {300, 4, 5, 1}, 300, dim, 0));
+        }
+        test_cases.emplace_back(new test_concat(type, {1025, 4, 512, 1}, 1025, 0, 0));
+        test_cases.emplace_back(new test_concat(type, {3, 4, 512, 1}, 5, 0, 0));
+    }
+
+    test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {98306, 8, 1, 1}, 286, 0, 0));
+    test_cases.emplace_back(new test_concat(GGML_TYPE_F16, {98306, 8, 1, 1}, 286, 0, 0));
+
     // GDN conv_input
     for (int64_t ne_b_d : { 31, 32, 33, 64 }) {
         test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {3, 10240, 1, 1}, ne_b_d, 0, 16));
@@ -14195,6 +14210,19 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
         }
     }

+    for (ggml_type type : { GGML_TYPE_F32, GGML_TYPE_F16 }) {
+        for (int64_t ne0 : { 64, 1024, 4096, 16384 }) {
+            test_cases.emplace_back(new test_concat(type, {ne0, 8, 256, 1}, 256, 2, 0));
+        }
+        test_cases.emplace_back(new test_concat(type, {2050, 8, 256, 1}, 256, 2, 0));
+        test_cases.emplace_back(new test_concat(type, {1024, 8, 2048, 1}, 2048, 2, 0));
+
+        for (int64_t ne0 : { 64, 1024, 4096, 16384 }) {
+            test_cases.emplace_back(new test_concat(type, {ne0, 8, 256, 1}, 256, 0, 0));
+        }
+        test_cases.emplace_back(new test_concat(type, {2050, 8, 256, 1}, 255, 0, 0));
+    }
+
     // CONT of a 0<->2 permute at DeepSeek-V4 lightning-indexer shapes:
     // indexer_kq is [n_kv, n_tokens, n_head=64] and gets ggml_cont(ggml_permute(.., 2,1,0,3)).
     for (int64_t n_kv : { 1024, 1280, 2048, 2304 }) {