Commit 9fde456d3 for llama.cpp
commit 9fde456d34a7b617cd6b3b5a5a25a8674a0fc2a0
Author: Pascal <admin@serveurperso.com>
Date: Mon Nov 30 11:03:35 2026 +0100
metal : one thread per row for soft_max and sum_rows on short rows (#33189)
A row of at most 8 columns got a whole threadgroup, and sum_rows on
rows of 4 ran one thread per threadgroup. Each thread now handles a
whole row. On 3.4M rows of 4 on an M5 Max, soft_max goes from 2725
to 198 us, sum_rows from 2355 to 110 us and mean from 2352 to 109 us.
diff --git a/ggml/src/ggml-metal/ggml-metal-device.cpp b/ggml/src/ggml-metal/ggml-metal-device.cpp
index 010124873..7f3cb70b0 100644
--- a/ggml/src/ggml-metal/ggml-metal-device.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-device.cpp
@@ -382,9 +382,10 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_sum_rows(ggml_me
const char * t0_str = ggml_type_name(op->src[0]->type);
const char * t_str = ggml_type_name(op->type);
- const bool is_c4 = op->src[0]->ne[0] % 4 == 0;
+ const bool is_short = op->src[0]->ne[0] <= N_SHORT_ROW_NCOLS;
+ const bool is_c4 = op->src[0]->ne[0] % 4 == 0 && !is_short;
- snprintf(base, 256, "kernel_sum_rows_%s_%s%s", t0_str, t_str, is_c4 ? "_4" : "");
+ snprintf(base, 256, "kernel_sum_rows_%s%s_%s%s", is_short ? "short_" : "", t0_str, t_str, is_c4 ? "_4" : "");
snprintf(name, 256, "%s_op=%d", base, op_num);
ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
@@ -398,7 +399,7 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_sum_rows(ggml_me
ggml_metal_cv_free(cv);
}
- res.smem = 32*sizeof(float);
+ res.smem = is_short ? 0 : 32*sizeof(float);
if (is_c4) {
res.smem *= 4;
@@ -473,13 +474,15 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_soft_max(ggml_me
const char * suffix = "";
- if (op->src[0]->ne[0] % 4 == 0) {
+ const bool is_short = op->src[0]->ne[0] <= N_SHORT_ROW_NCOLS;
+
+ if (op->src[0]->ne[0] % 4 == 0 && !is_short) {
suffix = "_4";
}
const ggml_type tsrc1 = op->src[1] ? op->src[1]->type : GGML_TYPE_F32;
- snprintf(base, 256, "kernel_soft_max_%s%s", ggml_type_name(tsrc1), suffix);
+ snprintf(base, 256, "kernel_soft_max_%s%s%s", is_short ? "short_" : "", ggml_type_name(tsrc1), suffix);
snprintf(name, 256, "%s", base);
ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
@@ -487,7 +490,7 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_soft_max(ggml_me
res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
}
- res.smem = 32*sizeof(float);
+ res.smem = is_short ? 0 : 32*sizeof(float);
return res;
}
diff --git a/ggml/src/ggml-metal/ggml-metal-impl.h b/ggml/src/ggml-metal/ggml-metal-impl.h
index a8de8f7f6..c7e27dce1 100644
--- a/ggml/src/ggml-metal/ggml-metal-impl.h
+++ b/ggml/src/ggml-metal/ggml-metal-impl.h
@@ -15,6 +15,7 @@
#define N_MM_SIMD_GROUP_Y 2
#define N_ARGMAX_NPART_MAX 256
+#define N_SHORT_ROW_NCOLS 8
#define N_ARGMAX_CHUNK 4096
#define N_SG_CONV_2D 4
diff --git a/ggml/src/ggml-metal/ggml-metal-ops.cpp b/ggml/src/ggml-metal/ggml-metal-ops.cpp
index 81b6237ac..162d53733 100644
--- a/ggml/src/ggml-metal/ggml-metal-ops.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-ops.cpp
@@ -1085,6 +1085,14 @@ int ggml_metal_op_sum_rows(ggml_metal_op_t ctx, int idx) {
ggml_metal_encoder_set_buffer (enc, bid_src0, 1);
ggml_metal_encoder_set_buffer (enc, bid_dst, 2);
+ if (ne00 <= N_SHORT_ROW_NCOLS) {
+ const int nth = 256;
+
+ ggml_metal_encoder_dispatch_threadgroups(enc, (ne01*ne02 + nth - 1)/nth, 1, ne03, nth, 1, 1);
+
+ return 1;
+ }
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1);
@@ -1793,6 +1801,14 @@ int ggml_metal_op_soft_max(ggml_metal_op_t ctx, int idx) {
}
ggml_metal_encoder_set_buffer(enc, ggml_metal_get_buffer_id(op), 4);
+ if (ne00 <= N_SHORT_ROW_NCOLS) {
+ const int nth = 256;
+
+ ggml_metal_encoder_dispatch_threadgroups(enc, (ne01*ne02 + nth - 1)/nth, 1, ne03, nth, 1, 1);
+
+ return 1;
+ }
+
ggml_metal_encoder_set_threadgroup_memory_size(enc, smem, 0);
ggml_metal_encoder_dispatch_threadgroups(enc, ne01, ne02, ne03, nth, 1, 1);
diff --git a/ggml/src/ggml-metal/kernels/reduce.metal b/ggml/src/ggml-metal/kernels/reduce.metal
index 0af9e4f6c..fe5f86349 100644
--- a/ggml/src/ggml-metal/kernels/reduce.metal
+++ b/ggml/src/ggml-metal/kernels/reduce.metal
@@ -116,6 +116,34 @@ typedef decltype(kernel_sum_rows_impl<float, float>) kernel_sum_rows_t;
template [[host_name("kernel_sum_rows_f32_f32")]] kernel kernel_sum_rows_t kernel_sum_rows_impl<float, float>;
template [[host_name("kernel_sum_rows_f32_f32_4")]] kernel kernel_sum_rows_t kernel_sum_rows_impl<float4, float>;
+kernel void kernel_sum_rows_short_f32_f32(
+ constant ggml_metal_kargs_sum_rows & args,
+ device const char * src0,
+ device char * dst,
+ uint3 tgpig[[threadgroup_position_in_grid]],
+ uint3 tpitg[[thread_position_in_threadgroup]],
+ uint3 ntg[[threads_per_threadgroup]]) {
+ const int64_t ir = (int64_t) tgpig.x*ntg.x + tpitg.x;
+ if (ir >= args.ne01*args.ne02) {
+ return;
+ }
+
+ const int64_t i3 = tgpig.z;
+ const int64_t i2 = ir/args.ne01;
+ const int64_t i1 = ir%args.ne01;
+
+ device const float * src_row = (device const float *) (src0 + i1*args.nb01 + i2*args.nb02 + i3*args.nb03);
+ device float * dst_row = (device float *) (dst + i1*args.nb1 + i2*args.nb2 + i3*args.nb3);
+
+ float sumf = 0.0f;
+
+ for (int64_t i0 = 0; i0 < args.ne00; ++i0) {
+ sumf += src_row[i0];
+ }
+
+ dst_row[0] = FC_sum_rows_op == OP_SUM_ROWS_NUM_MEAN ? sumf/args.ne00 : sumf;
+}
+
template<typename T>
kernel void kernel_cumsum_blk(
constant ggml_metal_kargs_cumsum_blk & args,
diff --git a/ggml/src/ggml-metal/kernels/softmax.metal b/ggml/src/ggml-metal/kernels/softmax.metal
index f32fe2937..7e3cc0ab8 100644
--- a/ggml/src/ggml-metal/kernels/softmax.metal
+++ b/ggml/src/ggml-metal/kernels/softmax.metal
@@ -214,10 +214,80 @@ kernel void kernel_soft_max_4(
}
}
+template<typename T>
+kernel void kernel_soft_max_short(
+ constant ggml_metal_kargs_soft_max & args,
+ device const char * src0,
+ device const char * src1,
+ device const char * src2,
+ device char * dst,
+ uint3 tgpig[[threadgroup_position_in_grid]],
+ uint3 tpitg[[thread_position_in_threadgroup]],
+ uint3 tptg[[threads_per_threadgroup]]) {
+ const int32_t ir = tgpig.x*tptg.x + tpitg.x;
+ if (ir >= args.ne01*args.ne02) {
+ return;
+ }
+
+ const int32_t i03 = tgpig.z;
+ const int32_t i02 = ir/args.ne01;
+ const int32_t i01 = ir%args.ne01;
+
+ const int32_t i13 = i03%args.ne13;
+ const int32_t i12 = i02%args.ne12;
+ const int32_t i11 = i01;
+
+ device const float * psrc0 = (device const float *) (src0 + i01*args.nb01 + i02*args.nb02 + i03*args.nb03);
+ device const T * pmask = src1 != src0 ? (device const T * ) (src1 + i11*args.nb11 + i12*args.nb12 + i13*args.nb13) : nullptr;
+ device const float * psrc2 = src2 != src0 ? (device const float *) (src2) : nullptr;
+ device float * pdst = (device float *) (dst + i01*args.nb1 + i02*args.nb2 + i03*args.nb3);
+
+ float slope = 1.0f;
+
+ if (args.max_bias > 0.0f) {
+ const int32_t h = i02;
+
+ const float base = h < args.n_head_log2 ? args.m0 : args.m1;
+ const int exp = h < args.n_head_log2 ? h + 1 : 2*(h - args.n_head_log2) + 1;
+
+ slope = pow(base, exp);
+ }
+
+ float vals[N_SHORT_ROW_NCOLS];
+ float max_val = psrc2 ? psrc2[i02] : -INFINITY;
+
+ for (int i00 = 0; i00 < N_SHORT_ROW_NCOLS; ++i00) {
+ if (i00 < args.ne00) {
+ vals[i00] = psrc0[i00]*args.scale + (pmask ? slope*pmask[i00] : 0.0f);
+ max_val = MAX(max_val, vals[i00]);
+ }
+ }
+
+ float sum = psrc2 ? exp(psrc2[i02] - max_val) : 0.0f;
+
+ for (int i00 = 0; i00 < N_SHORT_ROW_NCOLS; ++i00) {
+ if (i00 < args.ne00) {
+ vals[i00] = exp(vals[i00] - max_val);
+ sum += vals[i00];
+ }
+ }
+
+ const float inv_sum = 1.0f/sum;
+
+ for (int i00 = 0; i00 < N_SHORT_ROW_NCOLS; ++i00) {
+ if (i00 < args.ne00) {
+ pdst[i00] = vals[i00]*inv_sum;
+ }
+ }
+}
+
typedef decltype(kernel_soft_max<float>) kernel_soft_max_t;
typedef decltype(kernel_soft_max_4<float4>) kernel_soft_max_4_t;
+typedef decltype(kernel_soft_max_short<float>) kernel_soft_max_short_t;
template [[host_name("kernel_soft_max_f16")]] kernel kernel_soft_max_t kernel_soft_max<half>;
template [[host_name("kernel_soft_max_f32")]] kernel kernel_soft_max_t kernel_soft_max<float>;
template [[host_name("kernel_soft_max_f16_4")]] kernel kernel_soft_max_4_t kernel_soft_max_4<half4>;
template [[host_name("kernel_soft_max_f32_4")]] kernel kernel_soft_max_4_t kernel_soft_max_4<float4>;
+template [[host_name("kernel_soft_max_short_f16")]] kernel kernel_soft_max_short_t kernel_soft_max_short<half>;
+template [[host_name("kernel_soft_max_short_f32")]] kernel kernel_soft_max_short_t kernel_soft_max_short<float>;
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 4896fb23e..3cd3d3a13 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -14640,6 +14640,7 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_mean());
test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 33, 1, 1, 1 }));
test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 33, 256, 1, 1 }));
+ test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 4, 37, 2, 3 }));
test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 32769, 1, 1, 1 }));
test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 32, 1, 1, 1 }));
test_cases.emplace_back(new test_mean(GGML_TYPE_F32, { 32, 256, 1, 1 }));