Commit 019951ec4 for llama.cpp

commit 019951ec44642d01783fe98dfbb01b575deb7c1b
Author: Pascal <admin@serveurperso.com>
Date:   Mon Nov 30 18:32:15 2026 +0100

    CUDA: one thread per row for soft_max and sum_rows on short rows (#33187)

    * CUDA: one thread per row for soft_max and sum_rows on short rows

    A row shorter than a warp left most of the block idle, and pooling the
    Hy4 flash indexer keys softmaxes and sums millions of rows of 4. With
    ncols <= 8 each thread now handles a whole row. On 3.4M rows of 4,
    soft_max goes from 1550 to 24 us and sum_rows from 1549 to 11 us.
    Hy4 flash decode goes from 66 to 87 t/s at 8k context, from 39 to 73
    t/s at 32k and from 17 to 48 t/s at 107k.

    * CUDA: use fastdiv for the short row soft_max indices

    The one thread per row soft_max spent more time in its 64-bit row
    index divisions than on the row itself. On 3.4M rows of 4 without
    mask, soft_max goes from 23.9 to 18.2 us.

    * tests : cover a broadcast mask on the short row soft_max

    The short row cases only used a mask of the full shape, so the
    fastmodulo on the mask indices of dims 2 and 3 never ran on a
    broadcast mask.

diff --git a/ggml/src/ggml-cuda/reduce_rows.cuh b/ggml/src/ggml-cuda/reduce_rows.cuh
index 111fd838a..80cae88f6 100644
--- a/ggml/src/ggml-cuda/reduce_rows.cuh
+++ b/ggml/src/ggml-cuda/reduce_rows.cuh
@@ -33,6 +33,27 @@ static __device__ __forceinline__ float reduce_row_f32(const float * x, const in
     return sum;
 }

+#define CUDA_REDUCE_ROWS_SHORT_NCOLS 8
+
+template <bool norm>
+static __global__ void reduce_rows_f32_short(const float * x_ptr, float * dst_ptr, const int ncols, const int64_t nrows) {
+    const int64_t row = (int64_t) blockIdx.x*blockDim.x + threadIdx.x;
+
+    ggml_cuda_pdl_sync();
+    if (row >= nrows) {
+        return;
+    }
+
+    const float * GGML_CUDA_RESTRICT x = x_ptr + row*ncols;
+
+    float sum = 0.0f;
+    for (int i = 0; i < ncols; ++i) {
+        sum += x[i];
+    }
+
+    dst_ptr[row] = norm ? sum / ncols : sum;
+}
+
 // Row reduction kernel template - compute sum (norm=false) or mean (norm=true)
 template <bool norm>
 static __global__ void reduce_rows_f32(const float * x_ptr, float * dst_ptr, const int ncols) {
diff --git a/ggml/src/ggml-cuda/softmax.cu b/ggml/src/ggml-cuda/softmax.cu
index 18929fcd9..3d9465391 100644
--- a/ggml/src/ggml-cuda/softmax.cu
+++ b/ggml/src/ggml-cuda/softmax.cu
@@ -142,6 +142,64 @@ static __global__ void soft_max_f32(
     }
 }

+#define CUDA_SOFT_MAX_SHORT_NCOLS 8
+
+template <typename T>
+static __global__ void soft_max_f32_short(
+        const float * x, const T * mask, const float * sinks, float * dst, const soft_max_params p,
+        const uint3 ne01_fdv, const uint3 ne02_fdv, const uint3 ne12_fdv, const uint3 ne13_fdv) {
+    const uint32_t rowx = blockIdx.x*blockDim.x + threadIdx.x;
+    if (rowx >= p.nrows_x) {
+        return;
+    }
+
+    const uint32_t i012 = fastdiv(rowx, ne01_fdv);
+    const uint32_t i01  = rowx - i012*ne01_fdv.z;
+    const uint32_t i03  = fastdiv(i012, ne02_fdv);
+    const uint32_t i02  = i012 - i03*ne02_fdv.z;
+
+    const uint32_t i12 = fastmodulo(i02, ne12_fdv);
+    const uint32_t i13 = fastmodulo(i03, ne13_fdv);
+
+    const int ncols = p.ncols;
+
+    x    += (int64_t) rowx*ncols;
+    mask += (i01*p.nb11 + i12*p.nb12 + i13*p.nb13) / sizeof(T) * (mask != nullptr);
+    dst  += (int64_t) rowx*ncols;
+
+    const float slope = get_alibi_slope(p.max_bias, i02, p.n_head_log2, p.m0, p.m1);
+
+    float vals[CUDA_SOFT_MAX_SHORT_NCOLS];
+    float max_val = sinks ? sinks[i02] : -INFINITY;
+
+#pragma unroll
+    for (int col = 0; col < CUDA_SOFT_MAX_SHORT_NCOLS; ++col) {
+        if (col < ncols) {
+            vals[col] = x[col]*p.scale + (mask ? slope*t2f32(mask[col]) : 0.0f);
+            max_val = max(max_val, vals[col]);
+        }
+    }
+
+    float sum = sinks ? expf(sinks[i02] - max_val) : 0.0f;
+
+#pragma unroll
+    for (int col = 0; col < CUDA_SOFT_MAX_SHORT_NCOLS; ++col) {
+        if (col < ncols) {
+            vals[col] = expf(vals[col] - max_val);
+            sum += vals[col];
+        }
+    }
+
+    const float inv_sum = 1.0f / sum;
+
+#pragma unroll
+    for (int col = 0; col < CUDA_SOFT_MAX_SHORT_NCOLS; ++col) {
+        if (col < ncols) {
+            dst[col] = vals[col]*inv_sum;
+        }
+    }
+}
+
 template <int n_elem_per_thread = 0>
 static __device__ void soft_max_f32_parallelize_cols_single_row(const float * __restrict__ x,
                                                                 float * __restrict__ dst,
@@ -392,6 +450,15 @@ static void soft_max_f32_cuda(const float *                                x,
     int nth = WARP_SIZE;
     const int64_t ncols_x = params.ncols;

+    if (ncols_x <= CUDA_SOFT_MAX_SHORT_NCOLS && params.nrows_x <= INT_MAX) {
+        const uint3 ne01_fdv = init_fastdiv_values(params.ne01);
+        const uint3 ne02_fdv = init_fastdiv_values(params.ne02);
+        const uint3 ne12_fdv = init_fastdiv_values(params.ne12);
+        const uint3 ne13_fdv = init_fastdiv_values(params.ne13);
+        soft_max_f32_short<<<(params.nrows_x + 255) / 256, 256, 0, stream>>>(x, mask, sinks, dst, params, ne01_fdv, ne02_fdv, ne12_fdv, ne13_fdv);
+        return;
+    }
+
     while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
     const dim3 block_dims(nth,     1, 1);
     const dim3 block_nums(params.ne01, params.ne02, params.ne03);
diff --git a/ggml/src/ggml-cuda/sumrows.cu b/ggml/src/ggml-cuda/sumrows.cu
index aa8342b5f..a436078e5 100644
--- a/ggml/src/ggml-cuda/sumrows.cu
+++ b/ggml/src/ggml-cuda/sumrows.cu
@@ -4,6 +4,13 @@
 void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
     const int  id  = ggml_cuda_get_device();
     const int  nsm = ggml_cuda_info().devices[id].nsm;
+    if (ncols <= CUDA_REDUCE_ROWS_SHORT_NCOLS) {
+        const dim3 block_dims(256, 1, 1);
+        const dim3 block_nums((nrows + 255) / 256, 1, 1);
+        const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, 0, stream);
+        ggml_cuda_kernel_launch(reduce_rows_f32_short</*norm=*/false>, launch_params, x, dst, ncols, (int64_t) nrows);
+        return;
+    }
     const dim3 block_nums(nrows, 1, 1);
     if ((nrows / nsm) < 2) {
         const dim3 block_dims(512, 1, 1);
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 80116c9fc..636d30ca4 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -14330,6 +14330,18 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
         exponent <<= 1;
     }
 #endif
+    for (int64_t ne0 : {1, 3, 4, 8}) {
+        for (bool mask : {false, true}) {
+            for (bool sinks : {false, true}) {
+                for (float max_bias : {0.0f, 8.0f}) {
+                    if (!mask && max_bias > 0.0f) continue;
+                    test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0, 37, 2, 3}, mask, sinks, GGML_TYPE_F16, {1, 1}, 0.1f, max_bias));
+                }
+            }
+        }
+    }
+    test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {4, 37, 2, 3}, true, true, GGML_TYPE_F16, {2, 3}, 0.1f, 8.0f));
+    test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {4, 300000, 1, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f));
     for (bool mask : {false, true}) {
         for (bool sinks : {false, true}) {
             for (float max_bias : {0.0f, 8.0f}) {
@@ -14722,6 +14734,10 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
     test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 33, 1, 1, 1 }));
     test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 33, 1024, 1, 1 }));
     test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 33, 256, 1, 1 }));
+    test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 1, 37, 2, 3 }));
+    test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 4, 37, 2, 3 }));
+    test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 8, 37, 2, 3 }));
+    test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 4, 300000, 1, 1 }));
     test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 17, 65537, 1, 1 }));
     test_cases.emplace_back(new test_group_norm(GGML_TYPE_F32, {64, 64, 320, 1}));
     test_cases.emplace_back(new test_group_norm(GGML_TYPE_F32, {9, 9, 1280, 1}));