summaryrefslogtreecommitdiff
path: root/0008-feat-cpu-wire-optimized-matmul-fused-SDPA-kernels-in.patch
diff options
context:
space:
mode:
Diffstat (limited to '0008-feat-cpu-wire-optimized-matmul-fused-SDPA-kernels-in.patch')
-rw-r--r--0008-feat-cpu-wire-optimized-matmul-fused-SDPA-kernels-in.patch746
1 files changed, 746 insertions, 0 deletions
diff --git a/0008-feat-cpu-wire-optimized-matmul-fused-SDPA-kernels-in.patch b/0008-feat-cpu-wire-optimized-matmul-fused-SDPA-kernels-in.patch
new file mode 100644
index 0000000..bd61554
--- /dev/null
+++ b/0008-feat-cpu-wire-optimized-matmul-fused-SDPA-kernels-in.patch
@@ -0,0 +1,746 @@
+From 07add1ddf216e550a2462bbc61298f17b7a678e7 Mon Sep 17 00:00:00 2001
+From: Kaixin Liu <liukaixin1@h-partners.com>
+Date: Thu, 6 Aug 2026 16:20:13 +0800
+Subject: [PATCH 08/11] feat(cpu): wire optimized matmul & fused SDPA kernels
+ into ggml dispatch
+
+- type_traits_cpu: enable is_matmul_valid and hook matmul for F16 / Q8_0
+ (FP32 keeps the vec_dot path, matmul left commented out)
+- ggml_compute_forward_mul_mat: route F16/F32/Q8_0 matmuls to the new
+ packed micro-kernels (compute_forward_mul_mat_one_chunk_matmul_kernel),
+ add FP16 M-only scheduling and Q8_0 M-only scheduling heuristics
+- add GGML_OP_FUSED_CPP_SDPA_EXT compute + n_tasks dispatch cases
+- NEON fp16<->fp32 conversion fast paths in ggml_cpu_fp32_to_fp16 /
+ ggml_cpu_fp16_to_fp32
+---
+ ggml/src/ggml-cpu/ggml-cpu.c | 581 +++++++++++++++++++++++++++++++++--
+ 1 file changed, 547 insertions(+), 34 deletions(-)
+
+diff --git a/ggml/src/ggml-cpu/ggml-cpu.c b/ggml/src/ggml-cpu/ggml-cpu.c
+index c7426df2..2ee3134f 100644
+--- a/ggml/src/ggml-cpu/ggml-cpu.c
++++ b/ggml/src/ggml-cpu/ggml-cpu.c
+@@ -34,6 +34,8 @@
+ #include <limits.h>
+ #include <stdarg.h>
+ #include <signal.h>
++#include <fcntl.h>
++#include <unistd.h>
+ #if defined(__gnu_linux__)
+ #include <syscall.h>
+ #endif
+@@ -200,12 +202,20 @@ static const struct ggml_type_traits_cpu type_traits_cpu[GGML_TYPE_COUNT] = {
+ .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_f32,
+ .vec_dot_type = GGML_TYPE_F32,
+ .nrows = 1,
++#if defined(__ARM_FEATURE_SVE)
++ .is_matmul_valid = true,
++ // .matmul = (matmul_f) matmul_outer_packA_b_g_b32,
++#endif
+ },
+ [GGML_TYPE_F16] = {
+ .from_float = (ggml_from_float_t) ggml_cpu_fp32_to_fp16,
+ .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_f16,
+ .vec_dot_type = GGML_TYPE_F16,
+ .nrows = 1,
++#if defined(__ARM_FEATURE_SVE)
++ .is_matmul_valid = true,
++ .matmul = (matmul_f) matmul_outer_packA_b_g_b16,
++#endif
+ },
+ [GGML_TYPE_Q4_0] = {
+ .from_float = quantize_row_q4_0,
+@@ -247,6 +257,10 @@ static const struct ggml_type_traits_cpu type_traits_cpu[GGML_TYPE_COUNT] = {
+ .nrows = 2,
+ #else
+ .nrows = 1,
++#endif
++#if defined(__ARM_FEATURE_SVE)
++ .is_matmul_valid = true,
++ .matmul = (matmul_f) matmul_q8_0_mmla_spack_b_g,
+ #endif
+ },
+ [GGML_TYPE_Q8_1] = {
+@@ -1168,6 +1182,319 @@ void ggml_set_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2,
+
+ // ggml_compute_forward_mul_mat
+
++#if defined(__ARM_FEATURE_SVE)
++
++void compute_forward_mul_mat_one_chunk_matmul_kernel(
++ const struct ggml_compute_params * params,
++ struct ggml_tensor * dst,
++ const enum ggml_type type,
++ const int64_t ir0_start,
++ const int64_t ir0_end,
++ const int64_t ir1_start,
++ const int64_t ir1_end) {
++
++ if (ir0_start >= ir0_end || ir1_start >= ir1_end) return;
++ const struct ggml_tensor * src0 = dst->src[0];
++ const struct ggml_tensor * src1 = dst->src[1];
++
++ const int64_t K = src0->ne[0];
++
++ enum ggml_type const matmul_type = type_traits_cpu[type].vec_dot_type;
++
++ assert(type_traits_cpu[type].is_matmul_valid);
++ matmul_f matmul = type_traits_cpu[type].matmul;
++
++ // A[M, K], B[N, K], C[M, N]
++ const uint8_t* GGML_RESTRICT A = (const uint8_t*)(src0->data);
++ const uint8_t* GGML_RESTRICT B = (const uint8_t*)(src1->data);
++ if (matmul_type != src1->type) {
++ B = (const uint8_t*)(params->wdata);
++ }
++ uint8_t* GGML_RESTRICT C = (uint8_t*)(dst->data);
++ const size_t k_row_size = ggml_row_size(matmul_type, src1->ne[0]);
++ size_t byte_off_A = ir0_start * k_row_size;
++ size_t byte_off_B = ir1_start * k_row_size;
++ const int64_t M_th = ir0_end - ir0_start;
++ const int64_t N_th = ir1_end - ir1_start;
++ // Downgrade when has leftovers for outer kernels
++ int64_t M_th_outer = M_th & ~(16 - 1);
++ int64_t N_th_outer = N_th & ~(8 - 1);
++ // fprintf(stderr, "M_th=%d,N_th=%d\n", M_th,N_th);
++ // fprintf(stderr, "M_th_outer=%d,N_th_outer=%d\n", M_th_outer,N_th_outer);
++ if(matmul_type == GGML_TYPE_F16 || matmul_type == GGML_TYPE_F32) {
++ matmul_f leftovers_kernel;
++ switch (matmul_type) {
++ case GGML_TYPE_F16:
++ leftovers_kernel = ggml_matmul_f16_4x4_kernel;
++ break;
++ case GGML_TYPE_F32:
++ leftovers_kernel = ggml_matmul_f32_4x4_kernel;
++ break;
++ default:
++ GGML_ASSERT(false);
++ }
++ if(K % 8 == 0) {
++ if((M_th_outer != M_th || N_th_outer != N_th) && (type == GGML_TYPE_F16 || type == GGML_TYPE_F32)) {
++ void* GGML_RESTRICT ptr_C_matmul = &C[ir0_start * dst->nb[0] + ir1_start * dst->nb[1]];
++ void* GGML_RESTRICT ptr_C_leftovers_1 = &C[(ir0_start + M_th_outer) * dst->nb[0] + ir1_start * dst->nb[1]];
++ void* GGML_RESTRICT ptr_C_leftovers_2 = &C[ir0_start * dst->nb[0] + (ir1_start + N_th_outer) * dst->nb[1]];
++ size_t c_column_bytes = dst->nb[1];
++ if (M_th_outer > 0 && N_th_outer > 0) {
++ matmul(M_th_outer, N_th_outer, K, A + byte_off_A, B + byte_off_B, ptr_C_matmul, c_column_bytes);
++ }
++ if(M_th - M_th_outer > 0) {
++ leftovers_kernel(M_th - M_th_outer, N_th, K,
++ A + byte_off_A + (M_th_outer*k_row_size),
++ B+byte_off_B,
++ ptr_C_leftovers_1,
++ c_column_bytes);
++ }
++ if(N_th - N_th_outer > 0) {
++ leftovers_kernel(M_th_outer, N_th - N_th_outer, K,
++ A + byte_off_A,
++ B+byte_off_B + (N_th_outer*k_row_size),
++ ptr_C_leftovers_2,
++ c_column_bytes);
++ }
++ } else {
++ matmul(M_th, N_th, K, A + byte_off_A, B + byte_off_B, &C[ir0_start * dst->nb[0] + ir1_start * dst->nb[1]], dst->nb[1]);
++ }
++ } else {
++ leftovers_kernel(M_th, N_th, K, A + byte_off_A, B + byte_off_B, &C[ir0_start * dst->nb[0] + ir1_start * dst->nb[1]], dst->nb[1]);
++ }
++ } else if (matmul_type == GGML_TYPE_Q8_0) {
++ void* GGML_RESTRICT ptr_C_matmul = &C[ir0_start * dst->nb[0] + ir1_start * dst->nb[1]];
++ size_t c_column_bytes = dst->nb[1];
++ // fprintf(stderr, "run matmul for GGML_TYPE_Q8_0\n");
++ matmul(M_th, N_th, K, A + byte_off_A, B + byte_off_B, ptr_C_matmul, c_column_bytes);
++ } else {
++ GGML_ASSERT(false);
++ }
++}
++
++static void compute_forward_mul_mat_one_chunk_matmul_kernel_fp32(
++ const struct ggml_compute_params * params,
++ struct ggml_tensor * dst,
++ const enum ggml_type type,
++ const int64_t ir0_start,
++ const int64_t ir0_end,
++ const int64_t ir1_start,
++ const int64_t ir1_end) {
++
++ const struct ggml_tensor * src0 = dst->src[0];
++ const struct ggml_tensor * src1 = dst->src[1];
++
++ GGML_TENSOR_BINARY_OP_LOCALS
++
++ const bool src1_cont = ggml_is_contiguous(src1);
++
++ ggml_vec_dot_t const vec_dot = type_traits_cpu[type].vec_dot;
++ enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type;
++
++ // broadcast factors
++ const int64_t r2 = ne12 / ne02;
++ const int64_t r3 = ne13 / ne03;
++
++ // threads with no work simply yield (not sure if it helps)
++ if (ir0_start >= ir0_end || ir1_start >= ir1_end) {
++ return;
++ }
++
++ const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
++ const size_t row_size = ggml_row_size(vec_dot_type, ne10);
++
++ assert(ne12 % ne02 == 0);
++ assert(ne13 % ne03 == 0);
++
++ // block-tiling attempt
++ const int64_t blck_0 = 4;
++ const int64_t blck_1 = 4;
++ for (int64_t iir1 = ir1_start; iir1 < ir1_end; iir1 += blck_1) {
++ for (int64_t iir0 = ir0_start; iir0 < ir0_end; iir0 += blck_0) {
++ // Check if i02/i03 (src0 batch indices) would change within the 4x4 block.
++ // The 4x4 kernel assumes pr0 (src0 row pointers) are constant across all 4 ir1 rows.
++ // This is violated when i11 wraps at ne1 boundary, changing i12 -> i02 (if r2==1)
++ // or i13 -> i03 (if r3==1). Fall back to vec_dot in that case.
++ bool block_unsafe = false;
++ if (iir1 + blck_1 <= ir1_end) {
++ const int64_t first_i13 = iir1 / (ne12 * ne1);
++ const int64_t first_i12 = (iir1 - first_i13 * ne12 * ne1) / ne1;
++ const int64_t last_ir1 = iir1 + blck_1 - 1;
++ const int64_t last_i13 = last_ir1 / (ne12 * ne1);
++ const int64_t last_i12 = (last_ir1 - last_i13 * ne12 * ne1) / ne1;
++ if (first_i12 / r2 != last_i12 / r2 || first_i13 / r3 != last_i13 / r3) {
++ block_unsafe = true;
++ }
++ }
++ // handle leftovers using vec_dot
++ if (block_unsafe || iir1 + blck_1 > ir1_end || iir0 + blck_0 > ir0_end) {
++ for (int64_t ir1 = iir1; ir1 < iir1 + blck_1 && ir1 < ir1_end; ir1 += 1) {
++ const int64_t i13 = (ir1 / (ne12 * ne1));
++ const int64_t i12 = (ir1 - i13 * ne12 * ne1) / ne1;
++ const int64_t i11 = (ir1 - i13 * ne12 * ne1 - i12 * ne1);
++
++ // broadcast src0 into src1
++ const int64_t i03 = i13 / r3;
++ const int64_t i02 = i12 / r2;
++
++ const int64_t i1 = i11;
++ const int64_t i2 = i12;
++ const int64_t i3 = i13;
++
++ const char * src0_row = (const char*)src0->data + (0 + i02 * nb02 + i03 * nb03);
++ float * dst_col = (float*)((char*)dst->data + (i1 * nb1 + i2 * nb2 + i3 * nb3));
++
++ const char * src1_col = src1_cont ?
++ ((const char*)wdata + (i11 + i12 * ne11 + i13 * ne12 * ne11) * row_size):
++ ((const char*)wdata + (i11 * nb11 + i12 * nb12 + i13 * nb13));
++
++ float tmp4[4];
++ int ni = 0;
++ for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ++ir0, ++ni) {
++ vec_dot(ne00, &tmp4[ni], 0, src0_row + ir0 * nb01, 0, src1_col, 0, 1);
++ }
++ for (int i = 0; i < ni; i++) {
++ dst_col[iir0 + i] = tmp4[i];
++ }
++ }
++ } else {
++ // for every 4x4, collect pr1[4], pr0[4]
++ const float32_t *pr1[4];
++ const float32_t *pr0[4];
++ float32_t *pdst[4];
++ for (int64_t ir1 = iir1; ir1 < iir1 + blck_1 && ir1 < ir1_end; ir1 += 1) {
++ const int64_t i13 = (ir1 / (ne12 * ne1));
++ const int64_t i12 = (ir1 - i13 * ne12 * ne1) / ne1;
++ const int64_t i11 = (ir1 - i13 * ne12 * ne1 - i12 * ne1);
++
++ // broadcast src0 into src1
++ const int64_t i03 = i13 / r3;
++ const int64_t i02 = i12 / r2;
++
++ const int64_t i1 = i11;
++ const int64_t i2 = i12;
++ const int64_t i3 = i13;
++
++ if (ir1 == iir1) {
++ // Assert ir0 has same idx between ir1
++ for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ++ir0) {
++ const char * src0_row = (const char*)src0->data + (0 + i02 * nb02 + i03 * nb03);
++ int64_t irb0 = ir0 - iir0;
++ pr0[irb0] = (const float32_t*)(src0_row + ir0 * nb01);
++ }
++ }
++
++ int64_t irb1 = ir1 - iir1;
++
++ pr1[irb1] = (const float32_t*)(src1_cont ?
++ ((const char*)wdata + (i11 + i12 * ne11 + i13 * ne12 * ne11) * row_size) :
++ ((const char*)wdata + (i11 * nb11 + i12 * nb12 + i13 * nb13)));
++ pdst[irb1] = (float*)((char*)dst->data + (i1 * nb1 + i2 * nb2 + i3 * nb3)) + iir0;
++ }
++ #if 0
++ // scalar check
++ {
++ float tmp[4][4];
++ for (int64_t ib1 = 0; ib1 < 4; ib1++) {
++ for (int64_t ib0 = 0; ib0 < 4; ib0++) {
++ tmp[ib1][ib0] = 0.0f;
++ for (int64_t k = 0; k < ne00; k++) {
++ tmp[ib1][ib0] += pr0[ib0][k] * pr1[ib1][k];
++ }
++ }
++ }
++ for (int64_t ib1 = 0; ib1 < 4; ib1++) {
++ for (int64_t ib0 = 0; ib0 < 4; ib0++) {
++ pdst[ib1][ib0] = tmp[ib1][ib0];
++ }
++ }
++ }
++ #else
++ svfloat32_t acc00 = svdup_f32(0.0f);
++ svfloat32_t acc01 = svdup_f32(0.0f);
++ svfloat32_t acc02 = svdup_f32(0.0f);
++ svfloat32_t acc03 = svdup_f32(0.0f);
++ svfloat32_t acc10 = svdup_f32(0.0f);
++ svfloat32_t acc11 = svdup_f32(0.0f);
++ svfloat32_t acc12 = svdup_f32(0.0f);
++ svfloat32_t acc13 = svdup_f32(0.0f);
++ svfloat32_t acc20 = svdup_f32(0.0f);
++ svfloat32_t acc21 = svdup_f32(0.0f);
++ svfloat32_t acc22 = svdup_f32(0.0f);
++ svfloat32_t acc23 = svdup_f32(0.0f);
++ svfloat32_t acc30 = svdup_f32(0.0f);
++ svfloat32_t acc31 = svdup_f32(0.0f);
++ svfloat32_t acc32 = svdup_f32(0.0f);
++ svfloat32_t acc33 = svdup_f32(0.0f);
++ size_t vl = svcntw();
++ for (int64_t k = 0; k < ne00; k += vl) {
++ svbool_t pg = svwhilelt_b32(k, ne00);
++ svfloat32_t v00 = svld1_f32(pg, &pr0[0][k]);
++ svfloat32_t v01 = svld1_f32(pg, &pr0[1][k]);
++ svfloat32_t v02 = svld1_f32(pg, &pr0[2][k]);
++ svfloat32_t v03 = svld1_f32(pg, &pr0[3][k]);
++ svfloat32_t v10 = svld1_f32(pg, &pr1[0][k]);
++ svfloat32_t v11 = svld1_f32(pg, &pr1[1][k]);
++ svfloat32_t v12 = svld1_f32(pg, &pr1[2][k]);
++ svfloat32_t v13 = svld1_f32(pg, &pr1[3][k]);
++ acc00 = svmla_f32_m(pg, acc00, v00, v10);
++ acc01 = svmla_f32_m(pg, acc01, v00, v11);
++ acc02 = svmla_f32_m(pg, acc02, v00, v12);
++ acc03 = svmla_f32_m(pg, acc03, v00, v13);
++ acc10 = svmla_f32_m(pg, acc10, v01, v10);
++ acc11 = svmla_f32_m(pg, acc11, v01, v11);
++ acc12 = svmla_f32_m(pg, acc12, v01, v12);
++ acc13 = svmla_f32_m(pg, acc13, v01, v13);
++ acc20 = svmla_f32_m(pg, acc20, v02, v10);
++ acc21 = svmla_f32_m(pg, acc21, v02, v11);
++ acc22 = svmla_f32_m(pg, acc22, v02, v12);
++ acc23 = svmla_f32_m(pg, acc23, v02, v13);
++ acc30 = svmla_f32_m(pg, acc30, v03, v10);
++ acc31 = svmla_f32_m(pg, acc31, v03, v11);
++ acc32 = svmla_f32_m(pg, acc32, v03, v12);
++ acc33 = svmla_f32_m(pg, acc33, v03, v13);
++ }
++ svbool_t pg_all = svptrue_b32();
++ float32_t r00 = svaddv_f32(pg_all, acc00);
++ float32_t r01 = svaddv_f32(pg_all, acc01);
++ float32_t r02 = svaddv_f32(pg_all, acc02);
++ float32_t r03 = svaddv_f32(pg_all, acc03);
++ float32_t r10 = svaddv_f32(pg_all, acc10);
++ float32_t r11 = svaddv_f32(pg_all, acc11);
++ float32_t r12 = svaddv_f32(pg_all, acc12);
++ float32_t r13 = svaddv_f32(pg_all, acc13);
++ float32_t r20 = svaddv_f32(pg_all, acc20);
++ float32_t r21 = svaddv_f32(pg_all, acc21);
++ float32_t r22 = svaddv_f32(pg_all, acc22);
++ float32_t r23 = svaddv_f32(pg_all, acc23);
++ float32_t r30 = svaddv_f32(pg_all, acc30);
++ float32_t r31 = svaddv_f32(pg_all, acc31);
++ float32_t r32 = svaddv_f32(pg_all, acc32);
++ float32_t r33 = svaddv_f32(pg_all, acc33);
++ pdst[0][0] = r00;
++ pdst[1][0] = r01;
++ pdst[2][0] = r02;
++ pdst[3][0] = r03;
++ pdst[0][1] = r10;
++ pdst[1][1] = r11;
++ pdst[2][1] = r12;
++ pdst[3][1] = r13;
++ pdst[0][2] = r20;
++ pdst[1][2] = r21;
++ pdst[2][2] = r22;
++ pdst[3][2] = r23;
++ pdst[0][3] = r30;
++ pdst[1][3] = r31;
++ pdst[2][3] = r32;
++ pdst[3][3] = r33;
++ #endif
++ }
++ }
++ }
++}
++
++#endif // defined(__ARM_FEATURE_SVE)
++
+ static void ggml_compute_forward_mul_mat_one_chunk(
+ const struct ggml_compute_params * params,
+ struct ggml_tensor * dst,
+@@ -1247,7 +1574,14 @@ static void ggml_compute_forward_mul_mat_one_chunk(
+ //}
+
+ for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ir0 += num_rows_per_vec_dot) {
+- vec_dot(ne00, &tmp[ir0 - iir0], (num_rows_per_vec_dot > 1 ? 16 : 0), src0_row + ir0 * nb01, (num_rows_per_vec_dot > 1 ? nb01 : 0), src1_col, (num_rows_per_vec_dot > 1 ? src1_col_stride : 0), num_rows_per_vec_dot);
++ vec_dot(ne00,
++ &tmp[ir0 - iir0],
++ (num_rows_per_vec_dot > 1 ? 16 : 0),
++ src0_row + ir0 * nb01,
++ (num_rows_per_vec_dot > 1 ? nb01 : 0),
++ src1_col,
++ (num_rows_per_vec_dot > 1 ? src1_col_stride : 0),
++ num_rows_per_vec_dot);
+ }
+
+ for (int cn = 0; cn < num_rows_per_vec_dot; ++cn) {
+@@ -1273,6 +1607,7 @@ static void ggml_compute_forward_mul_mat(
+ enum ggml_type const vec_dot_type = type_traits_cpu[src0->type].vec_dot_type;
+ ggml_from_float_t const from_float = type_traits_cpu[vec_dot_type].from_float;
+ int64_t const vec_dot_num_rows = type_traits_cpu[src0->type].nrows;
++ bool const is_matmul_valid = type_traits_cpu[src0->type].is_matmul_valid;
+
+ GGML_ASSERT(ne0 == ne01);
+ GGML_ASSERT(ne1 == ne11);
+@@ -1288,7 +1623,7 @@ static void ggml_compute_forward_mul_mat(
+ GGML_ASSERT(nb0 <= nb1);
+ GGML_ASSERT(nb1 <= nb2);
+ GGML_ASSERT(nb2 <= nb3);
+-
++ // fprintf(stderr, "origin M=%d,N=%d,K=%d\n", ne0, ne1, ne00);
+ // nb01 >= nb00 - src0 is not transposed
+ // compute by src0 rows
+
+@@ -1402,58 +1737,190 @@ UseGgmlGemm2:;
+ chunk_size = 64;
+ }
+
++#if defined(__ARM_FEATURE_SVE)
++ if(is_matmul_valid) {
++ if (src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_F32) {
++ chunk_size = g_chunk_size_outer_packA;
++ }
++ else if (src0->type == GGML_TYPE_Q8_0) {
++ chunk_size = g_chunk_size_q8_0_mmla_spack;
++ }
++ }
++#endif // defined(__ARM_FEATURE_SVE)
+ // distribute the work across the inner or outer loop based on which one is larger
+ // The number of chunks in the 0/1 dim.
+ // CEIL(nr0/chunk_size)
+ int64_t nchunk0 = (nr0 + chunk_size - 1) / chunk_size;
+ int64_t nchunk1 = (nr1 + chunk_size - 1) / chunk_size;
+-
+- // If the chunking is poor for the number of threads on this setup, scrap the whole plan. Re-chunk it by thread.
+- // Also, chunking by thread was measured to have perform better on NUMA systems. See https://github.com/ggml-org/llama.cpp/pull/6915
+- // In theory, chunking should be just as useful on NUMA and non NUMA systems, but testing disagreed with that.
+- if (nchunk0 * nchunk1 < nth * 4 || ggml_is_numa()) {
+- // distribute the thread work across the inner or outer loop based on which one is larger
+- nchunk0 = nr0 > nr1 ? nth : 1; // parallelize by src0 rows
+- nchunk1 = nr0 > nr1 ? 1 : nth; // parallelize by src1 rows
++ /*
++ * FP16 PackA matmul scheduling optimization.
++ *
++ * Current optimized micro-kernel:
++ * M tile = 16
++ * N tile = 8
++ *
++ * Current outer blocking:
++ * M_BLK = g_chunk_size_outer_packA / 4
++ * N_BLK = g_chunk_size_outer_packA
++ *
++ * For sufficiently large N, prefer splitting only along M:
++ *
++ * nchunk0 = nth
++ * nchunk1 = 1
++ *
++ * so each thread owns a different M range but processes the full N range.
++ *
++ * Benefit:
++ *
++ * packA(M block)
++ * |
++ * +--> N block 0
++ * +--> N block 1
++ * +--> ...
++ *
++ * instead of splitting N across threads and packing the same A block again
++ * for every N chunk.
++ *
++ * Example, g_chunk_size_outer_packA = 128:
++ *
++ * N = 512 -> 4 N blocks / packed A
++ * N = 256 -> 2 N blocks / packed A
++ *
++ * We currently enable M-only scheduling when N contains at least two
++ * outer N blocks.
++ */
++ const int64_t matmul_m_tile = 16;
++ const int64_t matmul_n_block = g_chunk_size_outer_packA;
++
++ /*
++ * Only use this policy for the optimized FP16 PackA matmul path.
++ *
++ * Conditions:
++ *
++ * 1. matmul kernel is available;
++ * 2. source weight type is F16;
++ * 3. N is large enough to reuse one packed-A block across >= 2 N blocks;
++ * 4. each thread receives at least one complete 16-M micro-kernel tile;
++ * 5. M can be divided evenly across threads;
++ * 6. each thread's M range remains 16-aligned.
++ */
++ const bool prefer_m_only =
++ is_matmul_valid &&
++ src0->type == GGML_TYPE_F16 &&
++ nr1 >= 2 * matmul_n_block &&
++ nth > 0 &&
++ nr0 >= (int64_t) nth * matmul_m_tile &&
++ (nr0 % nth) == 0 &&
++ ((nr0 / nth) % matmul_m_tile) == 0;
++ const bool prefer_q8_m_only =
++ is_matmul_valid &&
++ src0->type == GGML_TYPE_Q8_0 &&
++ nth > 0 &&
++ nr0 >= (int64_t) nth * 8 &&
++ (nr0 % nth) == 0 &&
++ ((nr0 / nth) % 8) == 0;
++ if (prefer_q8_m_only || prefer_m_only) {
++ /*
++ * Examples:
++ *
++ * M=1024 N=512 nth=2:
++ * nchunk0=2, nchunk1=1
++ * each thread: M=512, N=512
++ *
++ * M=1024 N=512 nth=4:
++ * nchunk0=4, nchunk1=1
++ * each thread: M=256, N=512
++ *
++ * M=1024 N=256 nth=4:
++ * nchunk0=4, nchunk1=1
++ * each thread: M=256, N=256
++ */
++ nchunk0 = nth;
++ nchunk1 = 1;
++
++ } else if (nchunk0 * nchunk1 < nth * 4 || ggml_is_numa()) {
++ // Existing generic fallback.
++ nchunk0 = nr0 > nr1 ? nth : 1;
++ nchunk1 = nr0 > nr1 ? 1 : nth;
+ }
+-
+ // The number of elements in each chunk
+ const int64_t dr0 = (nr0 + nchunk0 - 1) / nchunk0;
+ const int64_t dr1 = (nr1 + nchunk1 - 1) / nchunk1;
+-
++ // if (ith == 0) {
++ // fprintf(stderr,
++ // "[MATMUL] M=%ld N=%ld K=%ld nth=%d "
++ // "chunk_size=%d nchunk0=%ld nchunk1=%ld "
++ // "dr0=%ld dr1=%ld numa=%d\n",
++ // (long) nr0,
++ // (long) nr1,
++ // (long) ne00,
++ // nth,
++ // chunk_size,
++ // (long) nchunk0,
++ // (long) nchunk1,
++ // (long) dr0,
++ // (long) dr1,
++ // ggml_is_numa());
++ // }
+ // The first chunk comes from our thread_id, the rest will get auto-assigned.
+ int current_chunk = ith;
+
+- while (current_chunk < nchunk0 * nchunk1) {
+- const int64_t ith0 = current_chunk % nchunk0;
+- const int64_t ith1 = current_chunk / nchunk0;
++#if defined(__ARM_FEATURE_SVE)
++ if(is_matmul_valid) {
++ while (current_chunk < nchunk0 * nchunk1) {
++ const int64_t ith0 = current_chunk % nchunk0;
++ const int64_t ith1 = current_chunk / nchunk0;
+
+- const int64_t ir0_start = dr0 * ith0;
+- const int64_t ir0_end = MIN(ir0_start + dr0, nr0);
++ const int64_t ir0_start = dr0 * ith0;
++ const int64_t ir0_end = MIN(ir0_start + dr0, nr0);
+
+- const int64_t ir1_start = dr1 * ith1;
+- const int64_t ir1_end = MIN(ir1_start + dr1, nr1);
++ const int64_t ir1_start = dr1 * ith1;
++ const int64_t ir1_end = MIN(ir1_start + dr1, nr1);
+
+- // dot kernels can handle 1 row and col at a time, but mmla kernels can process 2 rows and cols
+- int64_t num_rows_per_vec_dot = vec_dot_num_rows;
++ if (src0->type == GGML_TYPE_F32 /* && !ggml_is_contiguous(dst->src[1]) */) {
++ compute_forward_mul_mat_one_chunk_matmul_kernel_fp32(params, dst, src0->type, ir0_start, ir0_end, ir1_start, ir1_end);
++ } else {
++ compute_forward_mul_mat_one_chunk_matmul_kernel(params, dst, src0->type, ir0_start, ir0_end, ir1_start, ir1_end);
++ }
+
+- // these checks are needed to avoid crossing dim1 boundaries
+- // can be optimized, but the logic would become more complicated, so keeping it like this for simplicity
+- if ((nr0 % 2 != 0) || (ne11 % 2 != 0) || ((ir0_end - ir0_start) % 2 != 0) || ((ir1_end - ir1_start) % 2 != 0)) {
+- num_rows_per_vec_dot = 1;
+- }
+- ggml_compute_forward_mul_mat_one_chunk(params, dst, src0->type, num_rows_per_vec_dot, ir0_start, ir0_end, ir1_start, ir1_end);
++ if (nth >= nchunk0 * nchunk1) {
++ break;
++ }
+
+- if (nth >= nchunk0 * nchunk1) {
+- break;
++ current_chunk = atomic_fetch_add_explicit(&params->threadpool->current_chunk, 1, memory_order_relaxed);
+ }
++ } else
++#endif // defined(__ARM_FEATURE_SVE)
++ {
++ while (current_chunk < nchunk0 * nchunk1) {
++ const int64_t ith0 = current_chunk % nchunk0;
++ const int64_t ith1 = current_chunk / nchunk0;
++
++ const int64_t ir0_start = dr0 * ith0;
++ const int64_t ir0_end = MIN(ir0_start + dr0, nr0);
++
++ const int64_t ir1_start = dr1 * ith1;
++ const int64_t ir1_end = MIN(ir1_start + dr1, nr1);
++
++ // dot kernels can handle 1 row and col at a time, but mmla kernels can process 2 rows and cols
++ int64_t num_rows_per_vec_dot = vec_dot_num_rows;
++
++ // these checks are needed to avoid crossing dim1 boundaries
++ // can be optimized, but the logic would become more complicated, so keeping it like this for simplicity
++ if ((nr0 % 2 != 0) || (ne11 % 2 != 0) || ((ir0_end - ir0_start) % 2 != 0) || ((ir1_end - ir1_start) % 2 != 0)) {
++ num_rows_per_vec_dot = 1;
++ }
++ ggml_compute_forward_mul_mat_one_chunk(params, dst, src0->type, num_rows_per_vec_dot, ir0_start, ir0_end, ir1_start, ir1_end);
+
+- current_chunk = atomic_fetch_add_explicit(&params->threadpool->current_chunk, 1, memory_order_relaxed);
++ if (nth >= nchunk0 * nchunk1) {
++ break;
++ }
++
++ current_chunk = atomic_fetch_add_explicit(&params->threadpool->current_chunk, 1, memory_order_relaxed);
++ }
+ }
+ }
+
+-// ggml_compute_forward_mul_mat_id
+-
+ #define MMID_MATRIX_ROW(row_id, i1) matrix_rows[(row_id)*ids->ne[0]*ids->ne[1] + (i1)]
+
+ struct mmid_row_mapping {
+@@ -1713,7 +2180,14 @@ static void ggml_compute_forward_mul_mat_id(
+ }
+
+ /////////////////////////////////
+-
++// static inline uint64_t now_ns(void) {
++// struct timespec ts;
++// clock_gettime(CLOCK_MONOTONIC, &ts);
++// return (uint64_t) ts.tv_sec * 1000000000ull + ts.tv_nsec;
++// }
++
++// static _Atomic uint64_t g_op_ns[GGML_OP_COUNT];
++// static _Atomic uint64_t g_op_cnt[GGML_OP_COUNT];
+ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
+ GGML_ASSERT(params);
+
+@@ -1726,6 +2200,7 @@ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggm
+ return;
+ }
+
++ // uint64_t t0 = now_ns();
+ switch (tensor->op) {
+ case GGML_OP_DUP:
+ {
+@@ -1979,6 +2454,10 @@ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggm
+ {
+ ggml_compute_forward_flash_attn_ext(params, tensor->src[0], tensor->src[1], tensor->src[2], tensor->src[3], tensor);
+ } break;
++ case GGML_OP_FUSED_CPP_SDPA_EXT:
++ {
++ ggml_compute_forward_fused_cpp_sdpa_ext(params, tensor);
++ } break;
+ case GGML_OP_FLASH_ATTN_BACK:
+ {
+ int32_t t = ggml_get_op_params_i32(tensor, 0);
+@@ -2070,6 +2549,9 @@ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggm
+ GGML_ABORT("fatal error");
+ }
+ }
++ // uint64_t dt = now_ns() - t0;
++ // atomic_fetch_add_explicit(&g_op_ns[tensor->op], dt, memory_order_relaxed);
++ // atomic_fetch_add_explicit(&g_op_cnt[tensor->op], 1, memory_order_relaxed);
+ }
+
+ // Android's libc implementation "bionic" does not support setting affinity
+@@ -2296,6 +2778,10 @@ static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads) {
+ {
+ n_tasks = n_threads;
+ } break;
++ case GGML_OP_FUSED_CPP_SDPA_EXT:
++ {
++ n_tasks = n_threads;
++ } break;
+ case GGML_OP_WIN_PART:
+ case GGML_OP_WIN_UNPART:
+ case GGML_OP_GET_REL_POS:
+@@ -2885,7 +3371,19 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
+ }
+
+ ggml_barrier(state->threadpool);
+-
++ // for (int op = 0; op < GGML_OP_COUNT; ++op) {
++ // uint64_t ns = atomic_load_explicit(&g_op_ns[op], memory_order_relaxed);
++ // uint64_t cnt = atomic_load_explicit(&g_op_cnt[op], memory_order_relaxed);
++
++ // if (cnt > 0) {
++ // fprintf(stderr,
++ // "[op time] op=%s cnt=%lu time=%.3f ms avg=%.3f us\n",
++ // ggml_op_name((enum ggml_op) op),
++ // cnt,
++ // ns / 1e6,
++ // cnt ? (ns / 1e3 / cnt) : 0.0);
++ // }
++ // }
+ return 0;
+ }
+
+@@ -3205,6 +3703,13 @@ void ggml_cpu_fp32_to_fp16(const float * x, ggml_fp16_t * y, int64_t n) {
+ __m128i y_vec = _mm_cvtps_ph(x_vec, _MM_FROUND_TO_NEAREST_INT);
+ _mm_storel_epi64((__m128i *)(y + i), y_vec);
+ }
++#elif defined(__ARM_NEON) && defined(__aarch64__)
++ for (; i + 7 < n; i += 8) {
++ float32x4_t x0 = vld1q_f32(x + i);
++ float32x4_t x1 = vld1q_f32(x + i + 4);
++ float16x8_t y_vec = vcombine_f16(vcvt_f16_f32(x0), vcvt_f16_f32(x1));
++ vst1q_f16((float16_t *)(y + i), y_vec);
++ }
+ #endif
+ for (; i < n; ++i) {
+ y[i] = GGML_FP32_TO_FP16(x[i]);
+@@ -3231,6 +3736,14 @@ void ggml_cpu_fp16_to_fp32(const ggml_fp16_t * x, float * y, int64_t n) {
+ __m128 y_vec = _mm_cvtph_ps(x_vec);
+ _mm_storeu_ps(y + i, y_vec);
+ }
++#elif defined(__ARM_NEON) && defined(__aarch64__)
++ for (; i + 7 < n; i += 8) {
++ float16x8_t x_vec = vld1q_f16((const float16_t *)(x + i));
++ float32x4_t y0 = vcvt_f32_f16(vget_low_f16(x_vec));
++ float32x4_t y1 = vcvt_f32_f16(vget_high_f16(x_vec));
++ vst1q_f32(y + i, y0);
++ vst1q_f32(y + i + 4, y1);
++ }
+ #endif
+ for (; i < n; ++i) {
+ y[i] = GGML_FP16_TO_FP32(x[i]);
+--
+2.43.0
+