Commit e5cd6e34b for llama.cpp

commit e5cd6e34ba1070a3e39c49d2388d7f188ef4fe35
Author: Pascal <admin@serveurperso.com>
Date:   Tue Dec 1 18:38:28 2026 +0100

    Vulkan: one thread per row for soft_max and sum_rows on short rows (#33197)

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

    A row of at most 8 columns got a whole workgroup with a shared memory
    reduction. Each invocation now handles a whole row, with fastdiv for
    the row indices, and sum_rows covers mean through its weight. On 3.4M
    rows of 4 on an RTX PRO 6000, soft_max goes from 1653 to 42 us and
    sum_rows from 1662 to 10 us. Hy4 flash decode goes from 53 to 60 t/s
    at 4k context and from 9.6 to 54.8 t/s at 16k.

    * vulkan : initialize soft_max fastdiv push constants

    * vulkan : use vec4 loads in the short row soft_max and sum_rows

    Rows of 4 or 8 floats were read and written one element at a time, so
    each load of a subgroup hit addresses 16 or 32 bytes apart. When the row
    length is a multiple of 4 and the strides are aligned, move whole vec4
    instead, and keep the scalar loop otherwise.

    On a RTX PRO 6000 with 3.44M rows of 4 floats, soft_max goes from 42 to
    19 us and sum_rows/mean from 10.3 to 9.5 us.

    * tests : remove the temporary short row cases

    * vulkan : revert the vec4 loads in the short row soft_max and sum_rows

    The vec4 path halves the soft_max kernel time on rows of 4 floats, but
    Hy4 flash decode is unchanged on a RTX PRO 6000 and sum_rows gets slower
    with 8 floats per row, so keep the simpler scalar loop.

diff --git a/ggml/src/ggml-vulkan/ggml-vulkan-push-constants.h b/ggml/src/ggml-vulkan/ggml-vulkan-push-constants.h
index bd01b766f..72e7b26a4 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan-push-constants.h
+++ b/ggml/src/ggml-vulkan/ggml-vulkan-push-constants.h
@@ -614,8 +614,15 @@ struct vk_op_soft_max_push_constants {
     uint32_t n_head_log2;
     uint32_t nrows_x;
     uint32_t has_sinks;
+    uint32_t ne0_1mp, ne0_1L;
+    uint32_t ne0_12mp, ne0_12L;
 };

+template <> inline void init_pushconst_fastdiv(vk_op_soft_max_push_constants &p) {
+    init_fastdiv_values(p.ne01*p.ne02, p.ne0_12mp, p.ne0_12L);
+    init_fastdiv_values(p.ne01,        p.ne0_1mp,  p.ne0_1L);
+}
+
 struct vk_op_argsort_push_constants {
     uint32_t ncols;
     uint32_t ncols_padded;
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan-types.h b/ggml/src/ggml-vulkan/ggml-vulkan-types.h
index 47383d50a..ea22c6135 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan-types.h
+++ b/ggml/src/ggml-vulkan/ggml-vulkan-types.h
@@ -978,6 +978,7 @@ struct vk_device_struct {

     vk_pipeline pipeline_silu_back_f32;
     vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
+    vk_pipeline pipeline_soft_max_short_f32, pipeline_soft_max_short_f32_f16;
     vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
     vk_pipeline pipeline_soft_max_back_f32;

@@ -995,6 +996,7 @@ struct vk_device_struct {
     vk_pipeline pipeline_topk_radix_f32;
     vk_pipeline pipeline_topk_radix_qsa; // qwen4 QSA indexer fusion (f16 mask)
     vk_pipeline pipeline_sum_rows_f32;
+    vk_pipeline pipeline_sum_rows_short_f32;
     vk_pipeline pipeline_cross_entropy_loss_f32, pipeline_cross_entropy_loss_f32_wg512;
     vk_pipeline pipeline_cross_entropy_loss_back_f32, pipeline_cross_entropy_loss_back_f32_wg512;
     vk_pipeline pipeline_fwht_f32[num_fwht_pipelines];
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
index 1cdb392c4..0ce67f21d 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
@@ -4108,6 +4108,8 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
     ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_wg512, "soft_max_f32_wg512", soft_max_f32_len, soft_max_f32_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
     ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16, "soft_max_f32_f16", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
     ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16_wg512, "soft_max_f32_f16_wg512", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
+    ggml_vk_create_pipeline(device, device->pipeline_soft_max_short_f32, "soft_max_short_f32", soft_max_short_f32_len, soft_max_short_f32_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {256, 1, 1}, { 256 }, 1);
+    ggml_vk_create_pipeline(device, device->pipeline_soft_max_short_f32_f16, "soft_max_short_f32_f16", soft_max_short_f32_f16_len, soft_max_short_f32_f16_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {256, 1, 1}, { 256 }, 1);
     ggml_vk_create_pipeline(device, device->pipeline_soft_max_back_f32, "soft_max_back_f32", soft_max_back_f32_len, soft_max_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1, true);

     ggml_vk_create_pipeline(device, device->pipeline_soft_max_large1_f32,     "soft_max_large1_f32",     soft_max_large1_f32_len,     soft_max_large1_f32_data,     "main", 6, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 128, 4 }, 1, true);
@@ -4179,6 +4181,7 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
     ggml_vk_create_pipeline(device, device->pipeline_argmax_f32, "argmax_f32", argmax_f32_len, argmax_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);

     ggml_vk_create_pipeline(device, device->pipeline_sum_rows_f32, "sum_rows_f32", sum_rows_f32_len, sum_rows_f32_data, "main", 2, sizeof(vk_op_sum_rows_push_constants), {1, 1, 1}, { device->subgroup_size }, 1, true);
+    ggml_vk_create_pipeline(device, device->pipeline_sum_rows_short_f32, "sum_rows_short_f32", sum_rows_short_f32_len, sum_rows_short_f32_data, "main", 2, sizeof(vk_op_sum_rows_push_constants), {256, 1, 1}, { 256 }, 1, true);
     ggml_vk_create_pipeline(device, device->pipeline_cross_entropy_loss_f32, "cross_entropy_loss_f32", cross_entropy_loss_f32_len, cross_entropy_loss_f32_data, "main", 3, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
     ggml_vk_create_pipeline(device, device->pipeline_cross_entropy_loss_f32_wg512, "cross_entropy_loss_f32_wg512", cross_entropy_loss_f32_len, cross_entropy_loss_f32_data, "main", 3, sizeof(vk_op_push_constants), {1, 1, 1}, { 512 }, 1);
     ggml_vk_create_pipeline(device, device->pipeline_cross_entropy_loss_back_f32, "cross_entropy_loss_back_f32", cross_entropy_loss_back_f32_len, cross_entropy_loss_back_f32_data, "main", 4, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
@@ -10620,9 +10623,15 @@ static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const
         }

         if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
+            if (src0->ne[0] <= 8) {
+                return ctx->device->pipeline_soft_max_short_f32;
+            }
             return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
         }
         if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
+            if (src0->ne[0] <= 8) {
+                return ctx->device->pipeline_soft_max_short_f32_f16;
+            }
             return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
         }
         return nullptr;
@@ -10684,6 +10693,9 @@ static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const
     case GGML_OP_SUM_ROWS:
     case GGML_OP_MEAN:
         if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
+            if (op != GGML_OP_SUM && src0->ne[0] <= 8) {
+                return ctx->device->pipeline_sum_rows_short_f32;
+            }
             return ctx->device->pipeline_sum_rows_f32;
         }
         return nullptr;
@@ -12945,7 +12957,8 @@ void ggml_vk_soft_max(ggml_backend_vk_context * ctx, vk_context& subctx, const g
         m0, m1,
         n_head_log2,
         nrows_x,
-        src2 != nullptr
+        src2 != nullptr,
+        0, 0, 0, 0,
     };

     if (ncols <= 16384) {
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/soft_max_short.comp b/ggml/src/ggml-vulkan/vulkan-shaders/soft_max_short.comp
new file mode 100644
index 000000000..c11dcc21b
--- /dev/null
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/soft_max_short.comp
@@ -0,0 +1,94 @@
+#version 450
+
+#extension GL_EXT_control_flow_attributes : enable
+
+layout (push_constant) uniform parameter
+{
+    uint KX;
+    uint KY;
+    uint ne00;
+    uint ne01;
+    uint ne02;
+    uint ne12;
+    uint ne13;
+    uint nb11;
+    uint nb12;
+    uint nb13;
+    float scale;
+    float max_bias;
+    float m0;
+    float m1;
+    uint n_head_log2;
+    uint nrows_x;
+    uint has_sinks;
+    uint ne0_1mp, ne0_1L;
+    uint ne0_12mp, ne0_12L;
+} p;
+
+#include "types.glsl"
+#include "utils.glsl"
+
+layout(constant_id = 0) const uint BLOCK_SIZE = 256;
+layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
+
+layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
+layout (binding = 1) readonly buffer Y {B_TYPE data_b[];};
+layout (binding = 2) readonly buffer Z {float data_c[];};
+layout (binding = 3) buffer D {D_TYPE data_d[];};
+
+const uint MAX_COLS = 8;
+
+void main() {
+    const uint rowx = gl_GlobalInvocationID.z * 262144 + gl_GlobalInvocationID.y * 512 + gl_GlobalInvocationID.x;
+
+    if (rowx >= p.nrows_x) {
+        return;
+    }
+
+    const uint i03 = fastdiv(rowx, p.ne0_12mp, p.ne0_12L);
+    const uint i012 = rowx - i03 * p.ne01 * p.ne02;
+    const uint i02 = fastdiv(i012, p.ne0_1mp, p.ne0_1L);
+    const uint i01 = i012 - i02 * p.ne01;
+
+    uint rowy_start = 0;
+    if (p.KY > 0) {
+        rowy_start = i01 * p.nb11 + fastmod(i02, p.ne12) * p.nb12 + fastmod(i03, p.ne13) * p.nb13;
+    }
+
+    float slope = 1.0f;
+
+    if (p.max_bias > 0.0f) {
+        const float base = i02 < p.n_head_log2 ? p.m0 : p.m1;
+        const uint   exp = i02 < p.n_head_log2 ? i02 + 1 : 2*(i02 - p.n_head_log2) + 1;
+
+        slope = pow(base, exp);
+    }
+
+    FLOAT_TYPE vals[MAX_COLS];
+    FLOAT_TYPE max_val = p.has_sinks == 0 ? uintBitsToFloat(0xFF800000) : data_c[i02];
+
+    [[unroll]] for (uint col = 0; col < MAX_COLS; ++col) {
+        if (col < p.KX) {
+            const FLOAT_TYPE b = p.KY > 0 ? FLOAT_TYPE(data_b[rowy_start + col]) : FLOAT_TYPE(0.0f);
+            vals[col] = FLOAT_TYPE(data_a[rowx * p.KX + col]) * p.scale + slope * b;
+            max_val = max(max_val, vals[col]);
+        }
+    }
+
+    FLOAT_TYPE sum = p.has_sinks == 0 ? FLOAT_TYPE(0.0f) : FLOAT_TYPE(exp(FLOAT_TYPE(data_c[i02]) - max_val));
+
+    [[unroll]] for (uint col = 0; col < MAX_COLS; ++col) {
+        if (col < p.KX) {
+            vals[col] = exp(vals[col] - max_val);
+            sum += vals[col];
+        }
+    }
+
+    const FLOAT_TYPE rcpdivisor = 1.0/sum;
+
+    [[unroll]] for (uint col = 0; col < MAX_COLS; ++col) {
+        if (col < p.KX) {
+            data_d[rowx * p.KX + col] = D_TYPE(vals[col] * rcpdivisor);
+        }
+    }
+}
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/sum_rows_short.comp b/ggml/src/ggml-vulkan/vulkan-shaders/sum_rows_short.comp
new file mode 100644
index 000000000..a0bc50ed4
--- /dev/null
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/sum_rows_short.comp
@@ -0,0 +1,35 @@
+#version 450
+
+#include "types.glsl"
+#include "sum_rows.glsl"
+
+#extension GL_EXT_control_flow_attributes : enable
+
+layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
+
+layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
+layout (binding = 1) writeonly buffer D {D_TYPE data_d[];};
+
+layout (constant_id = 0) const uint BLOCK_SIZE = 256;
+
+void main() {
+    const uint row = gl_GlobalInvocationID.z * 262144 + gl_GlobalInvocationID.y * 512 + gl_GlobalInvocationID.x;
+
+    if (row >= p.ne01 * p.ne02 * p.ne03) { return; }
+
+    const uint i03 = fastdiv(row, p.ne0_12mp, p.ne0_12L);
+    const uint i03_offset = i03 * p.ne01*p.ne02;
+    const uint i02 = fastdiv(row - i03_offset, p.ne0_1mp, p.ne0_1L);
+    const uint i01 = row - i03_offset - i02*p.ne01;
+
+    const uint src_idx = get_aoffset() + i01 * p.nb01 + i02 * p.nb02 + i03 * p.nb03;
+    const uint dst_idx = get_doffset() + i01 * p.nb11 + i02 * p.nb12 + i03 * p.nb13;
+
+    FLOAT_TYPE sum = FLOAT_TYPE(0.0);
+
+    for (uint i = 0; i < p.n_cols; ++i) {
+        sum += FLOAT_TYPE(data_a[src_idx + i]);
+    }
+
+    data_d[dst_idx] = D_TYPE(sum * p.weight);
+}
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp b/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
index 8e1560097..dc0c61e73 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
@@ -1059,6 +1059,8 @@ void process_shaders() {

     string_to_spv("soft_max_f32", "soft_max.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));
     string_to_spv("soft_max_f32_f16", "soft_max.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float"}}));
+    string_to_spv("soft_max_short_f32", "soft_max_short.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));
+    string_to_spv("soft_max_short_f32_f16", "soft_max_short.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float16_t"}, {"D_TYPE", "float"}}));
     string_to_spv("soft_max_back_f32", "soft_max_back.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));

     string_to_spv("soft_max_large1_f32", "soft_max_large1.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));
@@ -1093,6 +1095,7 @@ void process_shaders() {

     string_to_spv("argmax_f32", "argmax.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "int"}}));
     string_to_spv("sum_rows_f32", "sum_rows.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float"}}));
+    string_to_spv("sum_rows_short_f32", "sum_rows_short.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"D_TYPE", "float"}}));
     string_to_spv("cross_entropy_loss_f32", "cross_entropy_loss.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));
     string_to_spv("cross_entropy_loss_back_f32", "cross_entropy_loss_back.comp", merge_maps(base_dict, {{"A_TYPE", "float"}, {"B_TYPE", "float"}, {"D_TYPE", "float"}}));
     string_to_spv("fwht_f32", "fwht.comp", {{"A_TYPE", "float"}});