Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
cpu: Q2_0 AVX2 bit-plane kernel is opt-in (STRATA_Q2_BITPLANE=1), image built only when on, q2_bitplane_parity test
Niko12212467bb689941
diff --git a/CMakeLists.txt b/CMakeLists.txtindex bd8cb8929..73b90ccb2 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -1302,6 +1302,14 @@ if(STRATA_BUILD_TESTS)target_link_libraries(expert_multi_test PRIVATE strata_kernels_cpu)add_test(NAME expert_multi_test COMMAND expert_multi_test)+ # PR #706: the opt-in AVX-2 Q2_0 bit-plane kernel (STRATA_Q2_BITPLANE=1) against the legacy one, bitwise alone/window+ if(STRATA_BUILD_TESTS AND CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64|x64|X86")+ add_executable(q2_bitplane_parity src/kernels/cpu/q2_bitplane_parity.cpp)+ target_link_libraries(q2_bitplane_parity PRIVATE strata_kernels_cpu)+ add_test(NAME q2_bitplane_parity COMMAND q2_bitplane_parity)+ set_tests_properties(q2_bitplane_parity PROPERTIES ENVIRONMENT "STRATA_Q2_BITPLANE=1")+ endif()+add_executable(expert_parity src/kernels/cpu/expert_parity.cpp)target_link_libraries(expert_parity PRIVATE strata_kernels_cpu)# The pack is a REQUIRED input, not an optional one: this test compares against a real expert blob, and adiff --git a/include/strata/kernels/cpu/expert.hpp b/include/strata/kernels/cpu/expert.hppindex 0961fbc1b..0e92740ad 100644--- a/include/strata/kernels/cpu/expert.hpp+++ b/include/strata/kernels/cpu/expert.hpp@@ -68,7 +68,7 @@ struct ActQ {float hx[MAXC];int nchunks;/// The AVX-2 "bit-plane" image of `q` (src/kernels/cpu/q2_avx2.cpp explains it), filled only by- /// `act_quant_q8_1_avx2` and read only by `q2_0_gguf_rows_multi_avx2`. Per pair p of 64-value blocks:+ /// `act_quant_q8_1_avx2` with STRATA_Q2_BITPLANE=1 and read only by `q2_0_gguf_rows_multi_avx2`. Per pair p of 64-value blocks:/// `qp[128p + 32k + 16h + i] = q[128p + 64h + 4i + k]`, `psum[8p + 4h + m]` = the sum of q[128p + 64h + 16m ../// + 16), `pscale[8p + 2c + e] = scale[4p + c]`. `bp_pairs` is 0 when the image is absent (n % 128 != 0).alignas(32) int8_t qp[H];@@ -180,7 +180,9 @@ void q2_0_gguf_rows_multi(const uint8_t* w, size_t row_bytes, int nblocks, const/// The same two for CPUs without AVX-512 (src/kernels/cpu/q2_avx2.cpp, compiled for AVX2 only).void q2_0_gguf_rows_multi_avx2(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, int nt,float* const* out, int r0, int r1);-/// The pre-bit-plane AVX-2 kernel (what STRATA_Q2_LEGACY=1 selects), kept for A/B and parity.+/// STRATA_Q2_BITPLANE=1 (read once): the AVX-2 Q2_0 kernel reads the bit-plane image instead of the legacy one.+bool q2_bitplane_enabled();+/// The pre-bit-plane AVX-2 kernel (the default), kept for A/B and parity.void q2_0_gguf_rows_multi_avx2_legacy(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, int nt,float* const* out, int r0, int r1);void act_quant_q8_1_avx2(const float* x, int n, ActQ& a);diff --git a/src/kernels/cpu/q2_avx2.cpp b/src/kernels/cpu/q2_avx2.cppindex 40a82c3e2..cea933f2f 100644--- a/src/kernels/cpu/q2_avx2.cpp+++ b/src/kernels/cpu/q2_avx2.cpp@@ -71,7 +71,7 @@ void rows(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a,}}-// ================================ the "bit-plane" row dot (default) ================================+// ================================ the "bit-plane" row dot (opt-in: STRATA_Q2_BITPLANE=1) ================================//// The kernel above spends, per 32-value chunk and token, a maddubs, a madd, a convert, a scalar d*scale product// broadcast into an FMA, and a scalar correction FMA; plus a 14-op interleave to unpack 64 codes. Here:@@ -90,7 +90,8 @@ void rows(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a,//// The integer part is exact; only the float summation order differs from the kernel above (last bits). Every// token's operations are the same whatever the group width, so a token's rows are bitwise the same alone or in a-// verify window. STRATA_Q2_LEGACY=1 selects the kernel above (A/B).+// verify window. It changes the last bits of a Q2_0 expert on AVX2-only CPUs, so it is OFF by default+// (STRATA_Q2_BITPLANE=1 turns it on; unset: the kernel above, exactly as before).template <int NT>inline void row_bp_acc(const uint8_t* row, const ActQ* const* a, int npairs, __m256* acc) {for (int t = 0; t < NT; ++t) acc[t] = _mm256_setzero_ps();@@ -151,10 +152,15 @@ void rows_bp(const uint8_t* w, size_t row_bytes, int npairs, const ActQ* const*}}-const bool kLegacy = std::getenv("STRATA_Q2_LEGACY") != nullptr;+const bool kBitplane = [] {+ const char* e = std::getenv("STRATA_Q2_BITPLANE");+ return e != nullptr && e[0] == '1';+}();} // namespace+bool q2_bitplane_enabled() { return kBitplane; }+void q2_0_gguf_rows_multi_avx2_legacy(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, int nt,float* const* out, int r0, int r1) {switch (nt) {@@ -172,7 +178,7 @@ void q2_0_gguf_rows_multi_avx2_legacy(const uint8_t* w, size_t row_bytes, int nbvoid q2_0_gguf_rows_multi_avx2(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, int nt,float* const* out, int r0, int r1) {- bool bp = !kLegacy && nblocks % 2 == 0;+ bool bp = kBitplane && nblocks % 2 == 0;for (int t = 0; t < nt && bp; ++t) bp = a[t]->bp_pairs == nblocks / 2;if (!bp) {q2_0_gguf_rows_multi_avx2_legacy(w, row_bytes, nblocks, a, nt, out, r0, r1);@@ -265,7 +271,8 @@ void act_quant_q8_1_avx2(const float* x, int n, ActQ& a) {a.sum[k] = total;a.hx[k] = s * (float) total;}- bitplane_image(a);+ if (kBitplane) bitplane_image(a); // the image costs a pass per token: only when its kernel will read it+ else a.bp_pairs = 0;}} // namespace strata::kernels::cpudiff --git a/src/kernels/cpu/q2_bitplane_parity.cpp b/src/kernels/cpu/q2_bitplane_parity.cppnew file mode 100644index 000000000..cefa81f8e--- /dev/null+++ b/src/kernels/cpu/q2_bitplane_parity.cpp@@ -0,0 +1,90 @@+// src/kernels/cpu/q2_bitplane_parity.cpp - the opt-in AVX-2 Q2_0 bit-plane kernel (STRATA_Q2_BITPLANE=1, PR #706).+//+// 1. against the legacy AVX-2 kernel: the integer part is exact, only the float summation order differs, so the+// two agree to a few ulps of the largest term (a relative bound on the row's magnitude);+// 2. bitwise: a token's rows are the same alone, in a window of any width, and cut into any row ranges+// (the engine's pool splits rows between workers, a verify window changes the width);+// 3. the legacy path is untouched when the switch is off (this test only runs with the switch on: ctest sets it).+// Synthetic data, CPU only; skipped (pass) on a CPU without AVX2.+#include "strata/kernels/cpu/expert.hpp"++#include <cmath>+#include <cstdint>+#include <cstdio>+#include <cstdlib>+#include <cstring>+#include <random>+#include <vector>++namespace c = strata::kernels::cpu;++int main() {+ if (!c::q2_bitplane_enabled()) {+ std::printf("q2_bitplane_parity: STRATA_Q2_BITPLANE=1 is not set\n");+ return 1;+ }+ std::mt19937 rng(706);+ std::normal_distribution<float> nd(0.f, 1.f);+ int fail = 0;+ for (const int n : {128, 256, 1024, 2560}) { // 2560 = 40 blocks: an even count of pairs; 128 = one pair+ const int nblocks = n / 64;+ const size_t row_bytes = (size_t) nblocks * 18;+ const int rows = 37; // not a multiple of 4: the 4-row path and the tail+ std::vector<uint8_t> w((size_t) rows * row_bytes);+ for (int r = 0; r < rows; ++r)+ for (int b = 0; b < nblocks; ++b) {+ uint8_t* blk = &w[(size_t) r * row_bytes + (size_t) b * 18];+ const uint16_t d = (uint16_t) (0x1C00 + rng() % 0x0800); // fp16 ~0.004-0.016+ std::memcpy(blk, &d, 2);+ for (int i = 0; i < 16; ++i) blk[2 + i] = (uint8_t) rng();+ }+ const int NT = 8;+ static c::ActQ acts[8];+ std::vector<std::vector<float>> xs(NT, std::vector<float>((size_t) n));+ for (int t = 0; t < NT; ++t) {+ for (float& v : xs[(size_t) t]) v = nd(rng) * (t % 3 == 0 ? 8.f : 1.f);+ c::act_quant_q8_1_avx2(xs[(size_t) t].data(), n, acts[t]);+ if (acts[t].bp_pairs != nblocks / 2) { std::printf("FAIL n=%d: no bit-plane image\n", n); ++fail; }+ }+ const c::ActQ* ap[8];+ for (int t = 0; t < NT; ++t) ap[t] = &acts[t];+ // the whole window at once, with the bit-plane kernel+ std::vector<std::vector<float>> win(NT, std::vector<float>((size_t) rows)), ref = win, alone = win, cut = win;+ float* wo[8]; float* ro[8]; float* ao[8]; float* co[8];+ for (int t = 0; t < NT; ++t) { wo[t] = win[(size_t) t].data(); ro[t] = ref[(size_t) t].data();+ ao[t] = alone[(size_t) t].data(); co[t] = cut[(size_t) t].data(); }+ c::q2_0_gguf_rows_multi_avx2(w.data(), row_bytes, nblocks, ap, NT, wo, 0, rows);+ c::q2_0_gguf_rows_multi_avx2_legacy(w.data(), row_bytes, nblocks, ap, NT, ro, 0, rows);+ for (int t = 0; t < NT; ++t) { // one token at a time+ c::q2_0_gguf_rows_multi_avx2(w.data(), row_bytes, nblocks, ap + t, 1, ao + t, 0, rows);+ }+ for (int r0 = 0; r0 < rows; r0 += 5) // rows cut into ranges of five+ c::q2_0_gguf_rows_multi_avx2(w.data(), row_bytes, nblocks, ap, 3, co, r0, r0 + 5 < rows ? r0 + 5 : rows);+ for (int t = 0; t < NT; ++t)+ for (int r = 0; r < rows; ++r) {+ const float a = win[(size_t) t][(size_t) r], b = ref[(size_t) t][(size_t) r];+ if (std::memcmp(&a, &alone[(size_t) t][(size_t) r], 4) != 0) {+ std::printf("FAIL n=%d t=%d r=%d: a token alone differs from the window\n", n, t, r); ++fail;+ }+ if (t < 3 && std::memcmp(&a, &cut[(size_t) t][(size_t) r], 4) != 0) {+ std::printf("FAIL n=%d t=%d r=%d: a row range differs from the whole\n", n, t, r); ++fail;+ }+ // a few ulps of the row's magnitude (|d| * |q| * n bounds the sum's terms)+ const float tol = 1e-4f * (std::fabs(b) + 1.f);+ if (!(std::fabs(a - b) <= tol)) {+ std::printf("FAIL n=%d t=%d r=%d: bit-plane %.7g vs legacy %.7g\n", n, t, r, a, b); ++fail;+ }+ }+ }+ // a width that is not a multiple of 128 (an odd number of blocks): no image, the legacy kernel answers+ {+ const int n = 192, nblocks = 3;+ static c::ActQ a;+ std::vector<float> x((size_t) n);+ for (float& v : x) v = nd(rng);+ c::act_quant_q8_1_avx2(x.data(), n, a);+ if (a.bp_pairs != 0) { std::printf("FAIL: an odd block count got an image\n"); ++fail; }+ }+ std::printf(fail ? "FAIL (%d)\n" : "PASS\n", fail);+ return fail ? 1 : 0;+}