From 5ff70bc69a93b2aa6cbd8b55142d23391b21742b Mon Sep 17 00:00:00 2001 From: CoprDistGit Date: Fri, 27 Nov 2026 08:22:58 +0000 Subject: automatic import of llama.cpp --- 0011-add-f16-q80-unit-test.patch | 422 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 422 insertions(+) create mode 100644 0011-add-f16-q80-unit-test.patch (limited to '0011-add-f16-q80-unit-test.patch') diff --git a/0011-add-f16-q80-unit-test.patch b/0011-add-f16-q80-unit-test.patch new file mode 100644 index 0000000..727a5af --- /dev/null +++ b/0011-add-f16-q80-unit-test.patch @@ -0,0 +1,422 @@ +From 4994c01fe97410ace82d5418c51c5a3cb00a2713 Mon Sep 17 00:00:00 2001 +From: Kaixin Liu +Date: Thu, 13 Aug 2026 14:17:45 +0800 +Subject: [PATCH 11/11] add f16/q80 unit test + +--- + tests/CMakeLists.txt | 9 + + tests/test-sdpa-f16q80-opt.cpp | 382 +++++++++++++++++++++++++++++++++ + 2 files changed, 391 insertions(+) + create mode 100644 tests/test-sdpa-f16q80-opt.cpp + +diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt +index 83f7d1a4..84fb4857 100644 +--- a/tests/CMakeLists.txt ++++ b/tests/CMakeLists.txt +@@ -160,6 +160,15 @@ endif() + llama_build_and_test(test-gguf.cpp) + llama_build_and_test(test-backend-ops.cpp) + ++# fused SDPA + F16/Q8_0 GEMM optimization correctness test (mock-based). ++# No model is required: it feeds synthetic tensors through GGML_OP_FUSED_CPP_SDPA_EXT ++# and ggml_mul_mat on the CPU backend and compares against host-side reference ++# attention / naive matmul implementations. ++# Only built on ARM, where the fused SDPA operator is compiled into ggml-cpu. ++if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64|arm") ++ llama_build_and_test(test-sdpa-f16q80-opt.cpp LABEL "fused-sdpa") ++endif() ++ + llama_build_and_test(test-model-load-cancel.cpp LABEL "model") + llama_build_and_test(test-autorelease.cpp LABEL "model") + +diff --git a/tests/test-sdpa-f16q80-opt.cpp b/tests/test-sdpa-f16q80-opt.cpp +new file mode 100644 +index 00000000..9d3fa8a6 +--- /dev/null ++++ b/tests/test-sdpa-f16q80-opt.cpp +@@ -0,0 +1,382 @@ ++// Unit test for the fused SDPA operator (GGML_OP_FUSED_CPP_SDPA_EXT) and the ++// F16 / F32 / Q8_0 GEMM matmul path (ggml_mul_mat) using synthetic (mock) inputs. ++// ++// No model file is required. We feed in-memory generated q/k/v/mask tensors ++// through a real ggml CPU graph and compare the fused SDPA output against a ++// host-side reference attention computation, and the matmul outputs (F32/F16/ ++// Q8_0) against a host-side naive matmul. Each case passes when the normalized ++// relative error (NMSE) is below a threshold. ++// ++// The fused SDPA operator is only compiled into ggml-cpu on ARM, so this test ++// is gated on the aarch64/arm host in tests/CMakeLists.txt. ++ ++#include "ggml.h" ++#include "ggml-cpu.h" ++ ++#include ++#include ++#include ++#include ++#include ++ ++#undef NDEBUG ++#include ++#include ++ ++#if defined(_MSC_VER) ++#pragma warning(disable: 4244 4267) // possible loss of data ++#endif ++ ++static const char * RESULT_STR[] = { "ok", "FAILED" }; ++ ++// normalized mean squared error = mse(a, b) / mse(a, 0) ++static double nmse(const float * a, const float * b, size_t n) { ++ double mse_a_b = 0.0; ++ double mse_a_0 = 0.0; ++ for (size_t i = 0; i < n; i++) { ++ mse_a_b += (double)(a[i] - b[i]) * (double)(a[i] - b[i]); ++ mse_a_0 += (double) a[i] * (double) a[i]; ++ } ++ return mse_a_b / (mse_a_0 + 1e-12); ++} ++ ++// sine-based deterministic pseudo-random fill (reproducible, no RNG state) ++static void fill_float(float * p, size_t n, float lo, float hi, unsigned seed) { ++ for (size_t i = 0; i < n; i++) { ++ const double t = 0.5 + 0.5 * sin((double)(seed + i) * 0.0137); ++ p[i] = lo + (float)t * (hi - lo); ++ } ++} ++ ++// --------------------------------------------------------------------------- ++// SDPA host reference ++// ++// Layout matches what ggml_fused_cpp_sdpa_ext expects (post-permute): ++// q : [D, L, H, B] ++// k : [D, S, H, B] ++// v : [DV, S, H, B] ++// mask : [S, L, Hm, Bm] (broadcast over H/B), values are applied additively ++// out : [DV, H, L, B] ++// ++// out[b][h][l][:] = V^T * softmax( (K^T * q) * scale + mask ) ++// --------------------------------------------------------------------------- ++static void reference_sdpa(float * out, ++ const float * q, const float * k, const float * v, ++ const float * mask, // may be null ++ int64_t B, int64_t H, int64_t L, int64_t S, ++ int64_t D, int64_t DV, float scale) { ++ // Host buffers are memcpy'd raw into ggml tensors with ne: ++ // q : [D, L, H, B], k : [D, S, H, B], v : [DV, S, H, B] ++ // mask : [S, L], out : [DV, H, L, B] ++ // so indexing below matches ggml's contiguous (innermost = ne[0]) layout. ++ std::vector scores(S); ++ for (int64_t b = 0; b < B; b++) { ++ for (int64_t h = 0; h < H; h++) { ++ for (int64_t l = 0; l < L; l++) { ++ float smax = -INFINITY; ++ for (int64_t s = 0; s < S; s++) { ++ float acc = 0.0f; ++ for (int64_t d = 0; d < D; d++) { ++ const float qv = q[(b*(H*L*D) + h*(L*D) + l*D) + d]; ++ const float kv = k[(b*(H*S*D) + h*(S*D) + s*D) + d]; ++ acc += qv * kv; ++ } ++ float sc = scale * acc; ++ if (mask != nullptr) { ++ sc += mask[l*S + s]; ++ } ++ scores[s] = sc; ++ if (sc > smax) smax = sc; ++ } ++ // softmax over S ++ double denom = 0.0; ++ for (int64_t s = 0; s < S; s++) denom += exp((double)scores[s] - smax); ++ for (int64_t d2 = 0; d2 < DV; d2++) { ++ double acc = 0.0; ++ for (int64_t s = 0; s < S; s++) { ++ const double p = exp((double)scores[s] - smax) / denom; ++ acc += p * v[(b*(H*S*DV) + h*(S*DV) + s*DV) + d2]; ++ } ++ out[(b*(DV*H*L) + h*DV + l*(DV*H)) + d2] = (float) acc; ++ } ++ } ++ } ++ } ++} ++ ++static std::vector run_fused_sdpa(const float * q, const float * k, ++ const float * v, const float * mask, ++ int64_t B, int64_t H, int64_t L, int64_t S, int64_t D, int64_t DV, ++ float scale, int n_threads, bool mask_f16 = false) { ++ const size_t qnb = (size_t) D * L * H * B * sizeof(float); ++ const size_t knb = (size_t) D * S * H * B * sizeof(float); ++ const size_t vnb = (size_t) DV * S * H * B * sizeof(float); ++ const size_t mnb = mask != nullptr ? (size_t) S * L * sizeof(float) : 0; ++ const size_t onb = (size_t) DV * H * L * B * sizeof(float); ++ const size_t mem = ggml_tensor_overhead() * 16 + ggml_graph_overhead() ++ + qnb + knb + vnb + mnb + onb + 64 * 1024; ++ ggml_init_params params = { mem, nullptr, false }; ++ ggml_context * ctx = ggml_init(params); ++ GGML_ASSERT(ctx != nullptr); ++ ++ ggml_tensor * qq = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, D, L, H, B); ++ ggml_tensor * kk = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, D, S, H, B); ++ ggml_tensor * vv = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, DV, S, H, B); ++ ggml_tensor * mm = nullptr; ++ std::vector mask16; ++ if (mask != nullptr) { ++ mm = ggml_new_tensor_2d(ctx, mask_f16 ? GGML_TYPE_F16 : GGML_TYPE_F32, S, L); ++ if (mask_f16) { ++ mask16.resize((size_t) S * L); ++ const size_t n = (size_t) S * L; ++ for (size_t i = 0; i < n; i++) { ++ mask16[i] = ggml_fp32_to_fp16(mask[i]); ++ } ++ } ++ } ++ ++ memcpy(qq->data, q, ggml_nbytes(qq)); ++ memcpy(kk->data, k, ggml_nbytes(kk)); ++ memcpy(vv->data, v, ggml_nbytes(vv)); ++ if (mm != nullptr) { ++ memcpy(mm->data, mask_f16 ? (const void *) mask16.data() : (const void *) mask, ggml_nbytes(mm)); ++ } ++ ++ ggml_tensor * out = ggml_fused_cpp_sdpa_ext(ctx, qq, kk, vv, mm, scale); ++ GGML_ASSERT(out != nullptr); ++ ++ ggml_cgraph * gf = ggml_new_graph(ctx); ++ ggml_build_forward_expand(gf, out); ++ ++ GGML_ASSERT(ggml_graph_compute_with_ctx(ctx, gf, n_threads) == GGML_STATUS_SUCCESS); ++ ++ const size_t n_out = ggml_nelements(out); ++ std::vector res(n_out); ++ memcpy(res.data(), out->data, n_out * sizeof(float)); ++ ++ ggml_free(ctx); ++ return res; ++} ++ ++// --------------------------------------------------------------------------- ++// GEMM host reference: C = A * B, A[M,K], B[K,N]. ++// ggml_mul_mat output tensor is ne=[M, N] so its raw (contiguous) layout is ++// column-major: c[i + j*M]. The op computes out[i][j] = sum_k w[i][k] * act[j][k], ++// i.e. the activation is read transposed (N x K). a (wf) is M x K row-major, ++// b (aval) is read as N x K row-major. ++static void reference_matmul(float * c, const float * a, const float * b, ++ size_t M, size_t N, size_t K) { ++ for (size_t i = 0; i < M; i++) { ++ for (size_t j = 0; j < N; j++) { ++ double acc = 0.0; ++ for (size_t k = 0; k < K; k++) { ++ acc += (double) a[i*K + k] * (double) b[j*K + k]; ++ } ++ c[i + j*M] = (float) acc; ++ } ++ } ++} ++ ++// run ggml_mul_mat(weights, activations) -> out. ++// weights: [K, M] (ggml mul_mat treats src0 as [K, M] column-major internally) ++// activations: [K, N], out: [M, N] ++static std::vector run_mul_mat(ggml_type wtype, ++ const float * wf32, size_t M, size_t K, ++ const float * aval, size_t N, ++ int n_threads) { ++ const size_t mem_base = ggml_tensor_overhead() * 16 + ggml_graph_overhead(); ++ // allow headroom for quantized weights + activation buffers ++ const size_t mem = mem_base + ggml_row_size(wtype, M) * K + M * K * sizeof(float); ++ ggml_init_params params = { mem, nullptr, false }; ++ ggml_context * ctx = ggml_init(params); ++ GGML_ASSERT(ctx != nullptr); ++ ++ ggml_tensor * w = ggml_new_tensor_2d(ctx, wtype, K, M); ++ ggml_tensor * act = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, K, N); ++ ++ size_t w_nbytes = ggml_row_size(wtype, ggml_nelements(w)); ++ std::vector wq(w_nbytes); ++ if (wtype == GGML_TYPE_F32) { ++ memcpy(wq.data(), wf32, w_nbytes); ++ } else if (wtype == GGML_TYPE_F16) { ++ for (size_t i = 0; i < (size_t) ggml_nelements(w); i++) { ++ ((ggml_fp16_t *) wq.data())[i] = ggml_fp32_to_fp16(wf32[i]); ++ } ++ } else if (ggml_is_quantized(wtype)) { ++ GGML_ASSERT(ggml_nelements(w) % ggml_blck_size(wtype) == 0); ++ size_t nb = ggml_nelements(w) / ggml_blck_size(wtype); ++ ggml_quantize_chunk(wtype, wf32, wq.data(), 0, nb, ggml_blck_size(wtype), nullptr); ++ } else { ++ GGML_ABORT("unsupported weight type in run_mul_mat"); ++ } ++ memcpy(w->data, wq.data(), w_nbytes); ++ memcpy(act->data, aval, ggml_nbytes(act)); ++ ++ ggml_tensor * out = ggml_mul_mat(ctx, w, act); ++ GGML_ASSERT(out != nullptr); ++ ++ ggml_cgraph * gf = ggml_new_graph(ctx); ++ ggml_build_forward_expand(gf, out); ++ ++ GGML_ASSERT(ggml_graph_compute_with_ctx(ctx, gf, n_threads) == GGML_STATUS_SUCCESS); ++ ++ const size_t n_out = ggml_nelements(out); ++ std::vector res(n_out); ++ memcpy(res.data(), out->data, n_out * sizeof(float)); ++ ++ ggml_free(ctx); ++ return res; ++} ++ ++#define SDPA_CASE(b,h,l,s,d,dv,scale,use_mask) \ ++ do { \ ++ const int64_t B = b, H = h, L = l, S = s, D = d, DV = dv; \ ++ std::vector q(B*H*L*D), k(B*H*S*D), v(B*H*S*DV); \ ++ std::vector mask; \ ++ fill_float(q.data(), q.size(), -1, 1, 101); \ ++ fill_float(k.data(), k.size(), -1, 1, 202); \ ++ fill_float(v.data(), v.size(), -1, 1, 303); \ ++ if (use_mask) { mask.resize(S*L); fill_float(mask.data(), mask.size(), -2, 0, 404); } \ ++ const float * mp = use_mask ? mask.data() : nullptr; \ ++ std::vector ref(B*H*L*DV); \ ++ reference_sdpa(ref.data(), q.data(), k.data(), v.data(), mp, \ ++ B, H, L, S, D, DV, scale); \ ++ std::vector got = run_fused_sdpa(q.data(), k.data(), v.data(), \ ++ mp, B, H, L, S, D, DV, scale, 1); \ ++ const double err = nmse(ref.data(), got.data(), ref.size()); \ ++ const bool ok = err < 1e-4; \ ++ printf(" SDPA B=%lld H=%lld L=%lld S=%lld D=%lld DV=%lld mask=%d " \ ++ "NMSE=%.3e %s\n", (long long)B, (long long)H, (long long)L, \ ++ (long long)S, (long long)D, (long long)DV, use_mask, err, \ ++ RESULT_STR[!ok]); \ ++ failures += !ok; \ ++ } while (0) ++ ++#define GEMM_CASE(wt, m, n, k) \ ++ do { \ ++ const size_t M = m, N = n, K = k; \ ++ std::vector wf(M*K), aval(K*N), ref(M*N); \ ++ fill_float(wf.data(), wf.size(), -1, 1, 505); \ ++ fill_float(aval.data(), aval.size(), -1, 1, 606); \ ++ reference_matmul(ref.data(), wf.data(), aval.data(), M, N, K); \ ++ std::vector got = run_mul_mat(wt, wf.data(), M, K, \ ++ aval.data(), N, 1); \ ++ const double err = nmse(ref.data(), got.data(), ref.size()); \ ++ /* fp16 accumulation precision degrades with K; q8_0/f32 stay tight */ \ ++ const double thresh = (wt == GGML_TYPE_F16) ? 5e-3 : 1e-4; \ ++ const bool ok = err < thresh; \ ++ printf(" GEMM %-5s M=%zu N=%zu K=%zu NMSE=%.3e thr=%.1e %s\n", \ ++ ggml_type_name(wt), M, N, K, err, thresh, RESULT_STR[!ok]); \ ++ failures += !ok; \ ++ } while (0) ++ ++int main(void) { ++ int failures = 0; ++ ++ printf("=== fused SDPA (GGML_OP_FUSED_CPP_SDPA_EXT) ===\n"); ++ SDPA_CASE(1, 2, 8, 8, 32, 32, 0.125f, 0); ++ SDPA_CASE(1, 2, 8, 8, 32, 32, 0.125f, 1); ++ SDPA_CASE(2, 4, 6, 10, 64, 64, 0.088f, 0); ++ SDPA_CASE(2, 4, 6, 10, 64, 64, 0.088f, 1); ++ SDPA_CASE(1, 1, 16, 16, 32, 32, 0.125f, 0); ++ SDPA_CASE(1, 1, 16, 16, 32, 32, 0.125f, 1); ++ // L=S>64, multiple of 64: regression cases for the (removed) block-diagonal ++ // fast path. The full kernel must match the host reference for these shapes. ++ SDPA_CASE(1, 2, 128, 128, 64, 64, 0.088f, 0); ++ SDPA_CASE(1, 2, 128, 128, 64, 64, 0.088f, 1); ++ SDPA_CASE(1, 2, 256, 256, 64, 64, 0.0625f, 0); ++ SDPA_CASE(1, 2, 256, 256, 64, 64, 0.0625f, 1); ++ SDPA_CASE(1, 2, 512, 512, 64, 64, 0.0442f, 0); ++ SDPA_CASE(1, 2, 512, 512, 64, 64, 0.0442f, 1); ++ // causal-mask case: full LxS with -inf above diagonal ++ { ++ const int64_t B = 1, H = 2, L = 256, S = 256, D = 64, DV = 64; ++ std::vector q(B*H*L*D), k(B*H*S*D), v(B*H*S*DV); ++ fill_float(q.data(), q.size(), -1, 1, 101); ++ fill_float(k.data(), k.size(), -1, 1, 202); ++ fill_float(v.data(), v.size(), -1, 1, 303); ++ std::vector mask(S*L, -INFINITY); ++ for (int64_t l = 0; l < L; l++) { ++ for (int64_t s = 0; s <= l && s < S; s++) mask[l*S + s] = 0.0f; ++ } ++ std::vector ref(B*H*L*DV); ++ reference_sdpa(ref.data(), q.data(), k.data(), v.data(), mask.data(), ++ B, H, L, S, D, DV, 0.0625f); ++ std::vector got = run_fused_sdpa(q.data(), k.data(), v.data(), ++ mask.data(), B, H, L, S, D, DV, 0.0625f, 1); ++ const double err = nmse(ref.data(), got.data(), ref.size()); ++ const bool ok = err < 1e-4; ++ printf(" SDPA B=%lld H=%lld L=%lld S=%lld D=%lld DV=%lld mask=causal NMSE=%.3e %s\n", ++ (long long)B, (long long)H, (long long)L, (long long)S, ++ (long long)D, (long long)DV, err, RESULT_STR[!ok]); ++ failures += !ok; ++ } ++ // F16 mask path (coverage for ..._mask_f16 wrapper) ++ { ++ const int64_t B = 1, H = 2, L = 256, S = 256, D = 64, DV = 64; ++ std::vector q(B*H*L*D), k(B*H*S*D), v(B*H*S*DV), mask(S*L); ++ fill_float(q.data(), q.size(), -1, 1, 101); ++ fill_float(k.data(), k.size(), -1, 1, 202); ++ fill_float(v.data(), v.size(), -1, 1, 303); ++ fill_float(mask.data(), mask.size(), -2, 0, 404); ++ std::vector ref(B*H*L*DV); ++ reference_sdpa(ref.data(), q.data(), k.data(), v.data(), mask.data(), ++ B, H, L, S, D, DV, 0.0625f); ++ std::vector got = run_fused_sdpa(q.data(), k.data(), v.data(), ++ mask.data(), B, H, L, S, D, DV, 0.0625f, 1, true); ++ const double err = nmse(ref.data(), got.data(), ref.size()); ++ const bool ok = err < 1e-4; ++ printf(" SDPA B=%lld H=%lld L=%lld S=%lld D=%lld DV=%lld mask=f16 NMSE=%.3e %s\n", ++ (long long)B, (long long)H, (long long)L, (long long)S, ++ (long long)D, (long long)DV, err, RESULT_STR[!ok]); ++ failures += !ok; ++ } ++ // Large S (KV does not fit L3): exercises the taskloop path ++ { ++ const int64_t B = 1, H = 1, L = 64, S = 131072, D = 64, DV = 64; ++ std::vector q(B*H*L*D), k(B*H*S*D), v(B*H*S*DV); ++ fill_float(q.data(), q.size(), -1, 1, 111); ++ fill_float(k.data(), k.size(), -1, 1, 222); ++ fill_float(v.data(), v.size(), -1, 1, 333); ++ std::vector ref(B*H*L*DV); ++ reference_sdpa(ref.data(), q.data(), k.data(), v.data(), nullptr, ++ B, H, L, S, D, DV, 0.125f); ++ std::vector got = run_fused_sdpa(q.data(), k.data(), v.data(), ++ nullptr, B, H, L, S, D, DV, 0.125f, 1); ++ const double err = nmse(ref.data(), got.data(), ref.size()); ++ const bool ok = err < 1e-4; ++ printf(" SDPA B=%lld H=%lld L=%lld S=%lld D=%lld DV=%lld taskloop NMSE=%.3e %s\n", ++ (long long)B, (long long)H, (long long)L, (long long)S, ++ (long long)D, (long long)DV, err, RESULT_STR[!ok]); ++ failures += !ok; ++ } ++ ++ printf("\n=== GEMM (ggml_mul_mat, mock) ===\n"); ++ // Real model GEMM shapes (M = output, K = input, N = seq). ++ // bge-small-zh-v1.5: hidden=512, intermediate=2048 ++ // attn Q/K/V/O: M=512 K=512; ffn_up: M=2048 K=512; ffn_down: M=512 K=2048 ++ // bge-m3: hidden=1024, intermediate=4096 ++ // attn Q/K/V/O: M=1024 K=1024; ffn_up: M=4096 K=1024; ffn_down: M=1024 K=4096 ++ GEMM_CASE(GGML_TYPE_F32, 512, 8, 512); // bge-small attn ++ GEMM_CASE(GGML_TYPE_F16, 512, 8, 512); // bge-small attn (FP16 NEON packA) ++ GEMM_CASE(GGML_TYPE_Q8_0, 512, 8, 512); // bge-small attn (Q8_0 NEON mmla) ++ GEMM_CASE(GGML_TYPE_F16, 2048, 8, 512); // bge-small ffn_up ++ GEMM_CASE(GGML_TYPE_Q8_0, 2048, 8, 512); // bge-small ffn_up ++ GEMM_CASE(GGML_TYPE_F16, 512, 8, 2048); // bge-small ffn_down ++ GEMM_CASE(GGML_TYPE_Q8_0, 512, 8, 2048); // bge-small ffn_down ++ GEMM_CASE(GGML_TYPE_F32, 1024, 8, 1024); // bge-m3 attn ++ GEMM_CASE(GGML_TYPE_F16, 1024, 8, 1024); // bge-m3 attn (FP16 NEON packA) ++ GEMM_CASE(GGML_TYPE_Q8_0, 1024, 8, 1024); // bge-m3 attn (Q8_0 NEON mmla) ++ GEMM_CASE(GGML_TYPE_F16, 4096, 8, 1024); // bge-m3 ffn_up ++ GEMM_CASE(GGML_TYPE_Q8_0, 4096, 8, 1024); // bge-m3 ffn_up ++ GEMM_CASE(GGML_TYPE_F16, 1024, 8, 4096); // bge-m3 ffn_down ++ GEMM_CASE(GGML_TYPE_Q8_0, 1024, 8, 4096); // bge-m3 ffn_down ++ // coverage: f16 4x4 leftovers (M/N not multiple of 16x8), N=1 decode path, f32 vec_dot leftovers, q8 padded/4x4 tail ++ GEMM_CASE(GGML_TYPE_F16, 10, 6, 512); ++ GEMM_CASE(GGML_TYPE_F16, 16, 1, 512); ++ GEMM_CASE(GGML_TYPE_F32, 5, 3, 256); ++ GEMM_CASE(GGML_TYPE_Q8_0, 32, 6, 512); ++ ++ printf("\n=== %d cases failed ===\n", failures); ++ return failures == 0 ? 0 : 1; ++} +-- +2.43.0 + -- cgit v1.2.3