From c25b0fdddd7fbef407e141ecc68ccda9b47ec2fe Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 23 Nov 2026 20:09:19 +0100 Subject: [PATCH] inkling2 CPU backend: AVX2 / AVX-512 / NEON dense dot, and matmul parallel over the weight rows The q / k / v / o / gate projections of the CPU backend (matmul_f32) were `parallel for` over the activation rows with `if (M * N >= 4096)`: a one-token decode step ran most of them on ONE thread, and a prompt read every weight row M times from memory. They are parallel over the weight rows now (threshold on M * N * K), each row is read once and reused for all M activation rows from L1; every y[m][n] is the same dot call as before, so the result does not depend on the thread count. The dot itself (CPU backend, attention scores, inkling2_loop.cpp's helpers) is strata::quant::sk::dot_f32, one more function of the run-time tiers: 4 accumulators of the tier's vector width (AVX-512 / AVX2 with FMA / NEON), plain 4-accumulator C++ in the base tier (bit-identical to the sum it replaces: sk_tier_test checks it). The vector tiers sum in lane order, so a CPU-only run differs from 0.2.20 by float rounding (SQ-10 teacher-forced KL(old||new) 4e-4, repeatable run to run). Measured, CPU backend, SQ-10 prefill 128 / decode 8, alternating pairs: Apple M5 Max prefill 3.2 -> 7.7 ppts, decode 1.6 -> 2.5 tok/s (spread under 1 %); Zen 4 16 threads, UD-Q2_K_XL: prefill 11.1 -> 13.0, decode 2.7 -> 4.3. --- engine/include/strata/quant/sk_codecs.hpp | 4 ++ engine/src/inkling2/ink_cpu_backend.cpp | 29 ++++++------- engine/src/inkling2/inkling2_loop.cpp | 12 +----- engine/src/quant/sk_codecs.cpp | 51 +++++++++++++++++++++++ engine/src/quant/sk_dispatch.cpp | 4 +- engine/src/quant/sk_tiers.hpp | 1 + engine/tests/quant/sk_tier_test.cpp | 18 ++++++++ 7 files changed, 90 insertions(+), 29 deletions(-) diff --git a/engine/include/strata/quant/sk_codecs.hpp b/engine/include/strata/quant/sk_codecs.hpp index 9af133d545..60d7b78df2 100644 --- a/engine/include/strata/quant/sk_codecs.hpp +++ b/engine/include/strata/quant/sk_codecs.hpp @@ -114,6 +114,10 @@ void dequant_mx8_row( float* out, size_t ncols) noexcept; +// Dense float32 dot product (the CPU backend's trunk matmuls, attention scores): 4 accumulators of the tier's vector width (AVX-512 / AVX2 / NEON), plain 4-accumulator +// C++ in the base tier. Sums in lane order: differs from a sequential sum by float rounding only. +float dot_f32(const float* a, const float* b, size_t n) noexcept; + float dot_mx8_f32_row( const uint8_t* weights_e4m3, const uint8_t* scales_ue8m0, diff --git a/engine/src/inkling2/ink_cpu_backend.cpp b/engine/src/inkling2/ink_cpu_backend.cpp index 3455c06a28..9a85e1d644 100644 --- a/engine/src/inkling2/ink_cpu_backend.cpp +++ b/engine/src/inkling2/ink_cpu_backend.cpp @@ -61,27 +61,22 @@ std::vector decode_section1_tensor(const uint8_t* sec1, const TInfo& ti) return out; } +// Dense float32 dot: the ISA tier of strata::quant::sk (AVX-512 / AVX2 / NEON, chosen at run time by caps(); plain 4-accumulator C++ on a CPU with none of them). inline float dot_f32_fast(const float* a, const float* b, int n) noexcept { - float s0 = 0.0f, s1 = 0.0f, s2 = 0.0f, s3 = 0.0f; - int i = 0; - for (; i + 4 <= n; i += 4) { - s0 += a[i + 0] * b[i + 0]; - s1 += a[i + 1] * b[i + 1]; - s2 += a[i + 2] * b[i + 2]; - s3 += a[i + 3] * b[i + 3]; - } - float s = (s0 + s1) + (s2 + s3); - for (; i < n; ++i) s += a[i] * b[i]; - return s; + return strata::quant::sk::dot_f32(a, b, static_cast(n)); } +// y[M, N] = x[M, K] . w[N, K]^T (+ bias). Parallel over the weight rows, not over the activation rows: a one-token matmul (decode, M = 1) uses every thread, and +// a prompt matmul reads each weight row from memory once and reuses it for all M rows from L1. Every y[m][n] is the same dot product as before, so the result does +// not depend on the thread count. (It used to be parallel over m, and not at all when M * N < 4096: the q / k / v / o projections of a decode step ran on one thread.) void matmul_f32(const float* x, const float* w, float* y, int M, int N, int K, const float* bias = nullptr) { - #pragma omp parallel for schedule(static) if (M * N >= 4096) - for (int m = 0; m < M; ++m) { - const float* xm = x + static_cast(m) * K; - float* ym = y + static_cast(m) * N; - for (int n = 0; n < N; ++n) { - ym[n] = dot_f32_fast(xm, w + static_cast(n) * K, K) + (bias ? bias[n] : 0.0f); + const bool par = static_cast(M) * static_cast(N) * static_cast(K) >= (static_cast(1) << 20); + #pragma omp parallel for schedule(static) if (par) + for (int n = 0; n < N; ++n) { + const float* wn = w + static_cast(n) * K; + const float bn = bias ? bias[n] : 0.0f; + for (int m = 0; m < M; ++m) { + y[static_cast(m) * N + n] = dot_f32_fast(x + static_cast(m) * K, wn, K) + bn; } } } diff --git a/engine/src/inkling2/inkling2_loop.cpp b/engine/src/inkling2/inkling2_loop.cpp index af538ddf75..e1abb85b62 100644 --- a/engine/src/inkling2/inkling2_loop.cpp +++ b/engine/src/inkling2/inkling2_loop.cpp @@ -44,17 +44,7 @@ inline float fp16_bits_to_f32(uint16_t h) noexcept { } inline float dot_f32(const float* a, const float* b, size_t n) noexcept { - float s0 = 0.0f, s1 = 0.0f, s2 = 0.0f, s3 = 0.0f; - size_t i = 0; - for (; i + 4 <= n; i += 4) { - s0 += a[i + 0] * b[i + 0]; - s1 += a[i + 1] * b[i + 1]; - s2 += a[i + 2] * b[i + 2]; - s3 += a[i + 3] * b[i + 3]; - } - float s = (s0 + s1) + (s2 + s3); - for (; i < n; ++i) s += a[i] * b[i]; - return s; + return strata::quant::sk::dot_f32(a, b, n); // the run-time ISA tier (AVX-512 / AVX2 / NEON; plain C++ on a CPU with none) } } // namespace diff --git a/engine/src/quant/sk_codecs.cpp b/engine/src/quant/sk_codecs.cpp index bfd3ffc55f..e4531fbf9d 100644 --- a/engine/src/quant/sk_codecs.cpp +++ b/engine/src/quant/sk_codecs.cpp @@ -941,6 +941,57 @@ void dequant_mx8_row( } } +float dot_f32(const float* a, const float* b, size_t n) noexcept { + size_t i = 0; +#if defined(STRATA_SK_AVX512_VNNI) + __m512 s0 = _mm512_setzero_ps(), s1 = _mm512_setzero_ps(), s2 = _mm512_setzero_ps(), s3 = _mm512_setzero_ps(); + for (; i + 64 <= n; i += 64) { + s0 = _mm512_fmadd_ps(_mm512_loadu_ps(a + i), _mm512_loadu_ps(b + i), s0); + s1 = _mm512_fmadd_ps(_mm512_loadu_ps(a + i + 16), _mm512_loadu_ps(b + i + 16), s1); + s2 = _mm512_fmadd_ps(_mm512_loadu_ps(a + i + 32), _mm512_loadu_ps(b + i + 32), s2); + s3 = _mm512_fmadd_ps(_mm512_loadu_ps(a + i + 48), _mm512_loadu_ps(b + i + 48), s3); + } + for (; i + 16 <= n; i += 16) s0 = _mm512_fmadd_ps(_mm512_loadu_ps(a + i), _mm512_loadu_ps(b + i), s0); + float s = _mm512_reduce_add_ps(_mm512_add_ps(_mm512_add_ps(s0, s1), _mm512_add_ps(s2, s3))); +#elif defined(STRATA_SK_AVX2) + __m256 s0 = _mm256_setzero_ps(), s1 = _mm256_setzero_ps(), s2 = _mm256_setzero_ps(), s3 = _mm256_setzero_ps(); + for (; i + 32 <= n; i += 32) { + s0 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i), _mm256_loadu_ps(b + i), s0); + s1 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i + 8), _mm256_loadu_ps(b + i + 8), s1); + s2 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i + 16), _mm256_loadu_ps(b + i + 16), s2); + s3 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i + 24), _mm256_loadu_ps(b + i + 24), s3); + } + for (; i + 8 <= n; i += 8) s0 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i), _mm256_loadu_ps(b + i), s0); + const __m256 v = _mm256_add_ps(_mm256_add_ps(s0, s1), _mm256_add_ps(s2, s3)); + __m128 h = _mm_add_ps(_mm256_castps256_ps128(v), _mm256_extractf128_ps(v, 1)); + h = _mm_add_ps(h, _mm_movehl_ps(h, h)); + h = _mm_add_ss(h, _mm_shuffle_ps(h, h, 1)); + float s = _mm_cvtss_f32(h); +#elif defined(STRATA_SK_NEON_DOTPROD) + float32x4_t s0 = vdupq_n_f32(0.0f), s1 = vdupq_n_f32(0.0f), s2 = vdupq_n_f32(0.0f), s3 = vdupq_n_f32(0.0f); + for (; i + 16 <= n; i += 16) { + s0 = vfmaq_f32(s0, vld1q_f32(a + i), vld1q_f32(b + i)); + s1 = vfmaq_f32(s1, vld1q_f32(a + i + 4), vld1q_f32(b + i + 4)); + s2 = vfmaq_f32(s2, vld1q_f32(a + i + 8), vld1q_f32(b + i + 8)); + s3 = vfmaq_f32(s3, vld1q_f32(a + i + 12), vld1q_f32(b + i + 12)); + } + for (; i + 4 <= n; i += 4) s0 = vfmaq_f32(s0, vld1q_f32(a + i), vld1q_f32(b + i)); + float s = vaddvq_f32(vaddq_f32(vaddq_f32(s0, s1), vaddq_f32(s2, s3))); +#else + // the plain 4-accumulator sum the CPU backend used before (the base tier is bit-identical to it) + float s0 = 0.0f, s1 = 0.0f, s2 = 0.0f, s3 = 0.0f; + for (; i + 4 <= n; i += 4) { + s0 += a[i + 0] * b[i + 0]; + s1 += a[i + 1] * b[i + 1]; + s2 += a[i + 2] * b[i + 2]; + s3 += a[i + 3] * b[i + 3]; + } + float s = (s0 + s1) + (s2 + s3); +#endif + for (; i < n; ++i) s += a[i] * b[i]; + return s; +} + float dot_mx8_f32_row( const uint8_t* weights_e4m3, const uint8_t* scales_ue8m0, diff --git a/engine/src/quant/sk_dispatch.cpp b/engine/src/quant/sk_dispatch.cpp index e984fb201f..0d3fb2aa51 100644 --- a/engine/src/quant/sk_dispatch.cpp +++ b/engine/src/quant/sk_dispatch.cpp @@ -18,6 +18,7 @@ struct Table { float (*dot_sf4_f32_row)(const uint8_t*, const uint8_t*, float, const float*, size_t) noexcept; void (*dequant_mx8_row)(const uint8_t*, const uint8_t*, float*, size_t) noexcept; float (*dot_mx8_f32_row)(const uint8_t*, const uint8_t*, const float*, size_t) noexcept; + float (*dot_f32)(const float*, const float*, size_t) noexcept; void (*dequant_paired2_sk_rows)(const uint8_t*, SkCode, float*, float*, size_t) noexcept; void (*gemv_paired2_sk_q8_1)(const uint8_t*, SkCode, const BlockQ8_1_G64*, float*, size_t, size_t) noexcept; void (*gemm_paired2_sk_q8_1)(const uint8_t*, SkCode, const BlockQ8_1_G64*, float*, size_t, size_t, size_t) noexcept; @@ -27,7 +28,7 @@ struct Table { #define STRATA_SK_TABLE(ns, name) \ Table { \ &ns::quantize_q8_1_g64, &ns::fwht64_f32, &ns::dequant_sk_row, &ns::dot_sk_q8_1_row, &ns::gemm_sk_q8_1, \ - &ns::dequant_sf4_row, &ns::dot_sf4_f32_row, &ns::dequant_mx8_row, &ns::dot_mx8_f32_row, &ns::dequant_paired2_sk_rows, \ + &ns::dequant_sf4_row, &ns::dot_sf4_f32_row, &ns::dequant_mx8_row, &ns::dot_mx8_f32_row, &ns::dot_f32, &ns::dequant_paired2_sk_rows, \ &ns::gemv_paired2_sk_q8_1, &ns::gemm_paired2_sk_q8_1, name \ } @@ -66,6 +67,7 @@ void dequant_mx8_row(const uint8_t* weights_e4m3, const uint8_t* scales_ue8m0, f float dot_mx8_f32_row(const uint8_t* weights_e4m3, const uint8_t* scales_ue8m0, const float* x, size_t ncols) noexcept { return table().dot_mx8_f32_row(weights_e4m3, scales_ue8m0, x, ncols); } +float dot_f32(const float* a, const float* b, size_t n) noexcept { return table().dot_f32(a, b, n); } void dequant_paired2_sk_rows(const uint8_t* pair_raw, SkCode code, float* out_row0, float* out_row1, size_t ncols) noexcept { table().dequant_paired2_sk_rows(pair_raw, code, out_row0, out_row1, ncols); } diff --git a/engine/src/quant/sk_tiers.hpp b/engine/src/quant/sk_tiers.hpp index 78168995a4..ccb492a8d6 100644 --- a/engine/src/quant/sk_tiers.hpp +++ b/engine/src/quant/sk_tiers.hpp @@ -19,6 +19,7 @@ size_t ncols) noexcept; \ void dequant_mx8_row(const uint8_t* weights_e4m3, const uint8_t* scales_ue8m0, float* out, size_t ncols) noexcept; \ float dot_mx8_f32_row(const uint8_t* weights_e4m3, const uint8_t* scales_ue8m0, const float* x, size_t ncols) noexcept; \ + float dot_f32(const float* a, const float* b, size_t n) noexcept; \ void dequant_paired2_sk_rows(const uint8_t* pair_raw, SkCode code, float* out_row0, float* out_row1, size_t ncols) noexcept; \ void gemv_paired2_sk_q8_1(const uint8_t* weights, SkCode code, const BlockQ8_1_G64* x, float* out, size_t nrows, \ size_t ncols) noexcept; \ diff --git a/engine/tests/quant/sk_tier_test.cpp b/engine/tests/quant/sk_tier_test.cpp index b0a84d3c82..cc6968da5e 100644 --- a/engine/tests/quant/sk_tier_test.cpp +++ b/engine/tests/quant/sk_tier_test.cpp @@ -59,6 +59,7 @@ bool same(const void* a, const void* b, size_t n) { return std::memcmp(a, b, n) static float dotsf4(const uint8_t* p, const uint8_t* s, float os, const float* x, size_t n) { return NS::dot_sf4_f32_row(p, s, os, x, n); } \ static void deqmx8(const uint8_t* w, const uint8_t* s, float* o, size_t n) { NS::dequant_mx8_row(w, s, o, n); } \ static float dotmx8(const uint8_t* w, const uint8_t* s, const float* x, size_t n) { return NS::dot_mx8_f32_row(w, s, x, n); } \ + static float dotf32(const float* a, const float* b, size_t n) { return NS::dot_f32(a, b, n); } \ } TIER_API(base); TIER_API(avx2); @@ -125,6 +126,23 @@ void compare_tier(const char* name) { T::fwht(x.data(), o1.data(), 256); CHECK(same(o0.data(), o1.data(), 1024), "%s fwht64_f32", name); } + // dense float dot: the base tier is the old sequential 4-accumulator sum bit for bit; the vector tiers sum in lane order (rounding only) + for (size_t n : {1u, 3u, 4u, 7u, 16u, 33u, 64u, 100u, 128u, 2304u, 4097u}) { + std::vector a(n), b(n); + for (auto& v : a) v = (float)((int)(rng() % 2001) - 1000) * 0.002f; + for (auto& v : b) v = (float)((int)(rng() % 2001) - 1000) * 0.002f; + float s0 = 0.0f, s1 = 0.0f, s2 = 0.0f, s3 = 0.0f; + size_t i = 0; + for (; i + 4 <= n; i += 4) { s0 += a[i] * b[i]; s1 += a[i + 1] * b[i + 1]; s2 += a[i + 2] * b[i + 2]; s3 += a[i + 3] * b[i + 3]; } + float old_sum = (s0 + s1) + (s2 + s3); + for (; i < n; ++i) old_sum += a[i] * b[i]; + const float base_sum = base_t::dotf32(a.data(), b.data(), n); + CHECK(same(&old_sum, &base_sum, 4), "base dot_f32 n %zu is not the old sequential sum: %.9g vs %.9g", n, base_sum, old_sum); + double mag = 0; + for (size_t k = 0; k < n; ++k) mag += std::fabs((double)a[k] * b[k]); + const float t = T::dotf32(a.data(), b.data(), n); + CHECK(std::fabs((double)t - base_sum) <= 1e-5 * mag + 1e-30, "%s dot_f32 n %zu: %.9g vs %.9g", name, n, t, base_sum); + } // SF4 and MX8 for (size_t nc : {16u, 48u, 2304u, 1152u}) { std::vector p(nc / 2), sc(nc / 16);