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: add bit-exact AVX2 singleton expert rows
InB4DevOps8ee2005dae0b
diff --git a/include/strata/kernels/cpu/iq_avx2.hpp b/include/strata/kernels/cpu/iq_avx2.hppindex 519efa2c5..3147ed55d 100644--- a/include/strata/kernels/cpu/iq_avx2.hpp+++ b/include/strata/kernels/cpu/iq_avx2.hpp@@ -11,6 +11,11 @@namespace strata::kernels::cpu {bool iq256_supported(int ggml_type) noexcept;+/// Bit-identical to ggml's singleton gate/up for IQ2_XXS, IQ2_XS, IQ3_XXS and IQ3_S.+/// The caller checks AVX2 support and passes one Q8_K activation.+void iq256_gu_rows_exact_one(int ggml_type, const uint8_t* blob, size_t gu_row, size_t up_off, int n,+ const void* act, float* ff, int r0, int r1);+/// ff[t][r] = silu(gate_r . a[t]) * (up_r . a[t]), rows [r0, r1); gate rows at blob, up rows at blob + up_off.void iq256_gu_rows(int ggml_type, const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act,int nt, float* const* ff, int r0, int r1);diff --git a/src/kernels/cpu/iq_avx2.cpp b/src/kernels/cpu/iq_avx2.cppindex 2884fcde1..69bd67526 100644--- a/src/kernels/cpu/iq_avx2.cpp+++ b/src/kernels/cpu/iq_avx2.cpp@@ -453,6 +453,34 @@ void q8k_quant_avx2(const float* x, void* vy, int64_t k) {}}+void iq256_gu_rows_exact_one(int type, const uint8_t* blob, size_t gu_row, size_t up_off, int n,+ const void* act, float* ff, int r0, int r1) {+ const auto* y = (const block_q8_K*) act;+#if STRATA_AVXVNNI+ if (vnni_on()) {+ if (type == 16) vnni::gu_ggml_one<16>(blob, gu_row, up_off, n, y, ff, r0, r1);+ else if (type == 17) vnni::gu_ggml_one<17>(blob, gu_row, up_off, n, y, ff, r0, r1);+ else if (type == 18) {+ if (iq256_variant() & kIq256Gather) vnni::gu_ggml_one<118>(blob, gu_row, up_off, n, y, ff, r0, r1);+ else vnni::gu_ggml_one<18>(blob, gu_row, up_off, n, y, ff, r0, r1);+ } else if (type == 21) {+ if (iq256_variant() & kIq256Gather) vnni::gu_ggml_one<121>(blob, gu_row, up_off, n, y, ff, r0, r1);+ else vnni::gu_ggml_one<21>(blob, gu_row, up_off, n, y, ff, r0, r1);+ }+ return;+ }+#endif+ if (type == 16) plain::gu_ggml_one<16>(blob, gu_row, up_off, n, y, ff, r0, r1);+ else if (type == 17) plain::gu_ggml_one<17>(blob, gu_row, up_off, n, y, ff, r0, r1);+ else if (type == 18) {+ if (iq256_variant() & kIq256Gather) plain::gu_ggml_one<118>(blob, gu_row, up_off, n, y, ff, r0, r1);+ else plain::gu_ggml_one<18>(blob, gu_row, up_off, n, y, ff, r0, r1);+ } else if (type == 21) {+ if (iq256_variant() & kIq256Gather) plain::gu_ggml_one<121>(blob, gu_row, up_off, n, y, ff, r0, r1);+ else plain::gu_ggml_one<21>(blob, gu_row, up_off, n, y, ff, r0, r1);+ }+}+int iq256_variant() noexcept {// STRATA_IQ256_GATHER=0/1 for every core; unset, where the calling thread's core gathers faster (per thread: a// hybrid CPU's pool runs on P- and E-cores). AVX-VNNI where cpu_avxvnni_ok(). The same bits either way.diff --git a/src/kernels/cpu/iq_avx2_parity.cpp b/src/kernels/cpu/iq_avx2_parity.cppindex 8de7d87d1..bc5f67bf7 100644--- a/src/kernels/cpu/iq_avx2_parity.cpp+++ b/src/kernels/cpu/iq_avx2_parity.cpp@@ -248,6 +248,25 @@ int check_gate_up(int type) {const double r_ggml = rel(one_g.data(), ref.data(), one_g.size());std::printf(" %-8s gate rows vs ggml vec_dot: rel %.2e\n", type_name(type), r_ggml);if (!(r_ggml <= 1e-5)) { std::printf(" %-8s MISMATCH against ggml\n", type_name(type)); ++failures; }+ if (type == 16 || type == 17 || type == 18 || type == 21) {+ size_t different = 0;+ std::vector<float> exact(R);+ const ggml_vec_dot_t dot = traits_cpu(type)->vec_dot;+ for (int t = 0; t < kMaxT; ++t) {+ cpu::iq256_gu_rows_exact_one(type, blob.data(), f.gu_row, f.up_off, (int) kH, a.gup[t],+ exact.data(), 0, (int) kFF);+ for (size_t r = 0; r < R; ++r) {+ float g = 0.f, u = 0.f;+ dot((int) kH, &g, 0, blob.data() + r * f.gu_row, 0, a.gup[t], 0, 1);+ dot((int) kH, &u, 0, blob.data() + f.up_off + r * f.gu_row, 0, a.gup[t], 0, 1);+ const float want = (g / (1.f + std::exp(-g))) * u;+ different += std::memcmp(&exact[r], &want, sizeof(float)) != 0;+ }+ }+ std::printf(" %-8s exact singleton vs ggml: %zu of %zu expert outputs differ bitwise\n",+ type_name(type), different, (size_t) kMaxT * R);+ if (different) ++failures;+ }for (int v : subsets(gu_variants())) {size_t differ = 0;std::vector<float> g(kMaxT * R), ff(kMaxT * R);@@ -449,6 +468,12 @@ int bench(const std::vector<std::string>& pairs, const std::vector<int>& nts, infor (int t = 0; t < nt; ++t)ddot((int) kFF, &out[t][r], 0, blob + f.down_off + (size_t) r * f.d_row, 0, a.dnp[t], 0, 1);}});+ if (gt == 16 || gt == 17 || gt == 18 || gt == 21)+ ms.push_back({"exact1", [&, gt](const uint8_t* blob, int nt) {+ for (int t = 0; t < nt; ++t)+ cpu::iq256_gu_rows_exact_one(gt, blob, f.gu_row, f.up_off, (int) kH,+ a.gup[t], ff[t], 0, (int) kFF);+ }, down_v(0)});ms.push_back({"engine", // what the pool calls: native_gu_rows, native_down_rows or (Q2_0) q2_rows_any[&](const uint8_t* blob, int nt) { cpu::native_gu_rows(f, blob, a.gup, nt, ff, 0, (int) kFF); },[&, dt](const uint8_t* blob, int nt) {diff --git a/src/kernels/cpu/iq_avx2_rows.inl b/src/kernels/cpu/iq_avx2_rows.inlindex a8af07fb3..48d4dea01 100644--- a/src/kernels/cpu/iq_avx2_rows.inl+++ b/src/kernels/cpu/iq_avx2_rows.inl@@ -28,6 +28,56 @@ STRATA_ROWS_FN inline __m256i dot4u(__m256i u8, __m256i s8) {#endif}+STRATA_ROWS_FN inline float ggml_hsum8(__m256 x) {+ __m128 s = _mm_add_ps(_mm256_extractf128_ps(x, 1), _mm256_castps256_ps128(x));+ s = _mm_add_ps(s, _mm_movehl_ps(s, s));+ s = _mm_add_ss(s, _mm_movehdup_ps(s));+ return _mm_cvtss_f32(s);+}++template <int NT, bool GGML = false> STRATA_ROWS_FN+inline void row_dot_iq2xs(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res);++template <int TY> STRATA_ROWS_FN+inline float row_dot_ggml_one(const uint8_t* row, int nblocks, const block_q8_K* y) {+ if constexpr (TY == 17) {+ const block_q8_K* act[1] = {y};+ float result = 0.0f;+ row_dot_iq2xs<1, true>(row, nblocks, act, &result);+ return result;+ }+ __m256 accum = _mm256_setzero_ps();+ const int pf = prefetch_distance();+ for (int i = 0; i < nblocks; ++i) {+ const uint8_t* blk = row + (size_t) i * Fmt32<TY>::bytes;+ rows_ahead(blk, pf);+ __m256i sums[2] = {_mm256_setzero_si256(), _mm256_setzero_si256()};+ for (int j = 0; j < 4; ++j)+ for (int half = 0; half < 2; ++half) {+ __m256i g, sign, scale;+ Fmt32<TY>::decode(blk, j, half, g, sign, scale);+ const __m256i act = _mm256_loadu_si256((const __m256i*) (y[i].qs + 64 * j + 32 * half));+ const __m256i signed_act = _mm256_sign_epi8(act, sign);+ sums[half] = madd_add(sums[half], _mm256_maddubs_epi16(g, signed_act), scale);+ }+ const float d = h2f(u16(blk)) * y[i].d;+ accum = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sums[0], sums[1])), accum);+ }+ if constexpr (TY == 16 || TY == 17) return 0.125f * ggml_hsum8(accum);+ else if constexpr (TY == 18 || TY == 118) return 0.25f * ggml_hsum8(accum);+ else return ggml_hsum8(accum);+}++template <int TY> STRATA_ROWS_FN+void gu_ggml_one(const uint8_t* blob, size_t gu_row, size_t up_off, int n, const block_q8_K* y, float* ff,+ int r0, int r1) {+ for (int r = r0; r < r1; ++r) {+ const float g = row_dot_ggml_one<TY>(blob + (size_t) r * gu_row, n / QK_K, y);+ const float u = row_dot_ggml_one<TY>(blob + up_off + (size_t) r * gu_row, n / QK_K, y);+ ff[r] = (g / (1.f + std::exp(-g))) * u;+ }+}+template <int TY, int NT> STRATA_ROWS_FNinline void row_dot(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res) {const int pf = prefetch_distance();@@ -63,7 +113,7 @@ inline void row_dot(const uint8_t* row, int nblocks, const block_q8_K* const* y,// 32-byte load. The 8 scale bytes become all 16 half-scales with ggml's unpack trick. The grid lookups// stay scalar loads into set_epi64x (ggml does the same). Every token still only pays a load, a sign,// a maddubs, a madd and an add per half, into alternating accumulators.-template <int NT> STRATA_ROWS_FN+template <int NT, bool GGML> STRATA_ROWS_FNinline void row_dot_iq2xs(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res) {static const uint8_t bit_sel[32] = {1, 2, 4, 8, 16, 32, 64, (uint8_t) 0x80, 1, 2, 4, 8, 16, 32, 64, (uint8_t) 0x80,@@ -137,12 +187,14 @@ inline void row_dot_iq2xs(const uint8_t* row, int nblocks, const block_q8_K* con}}}- const float dx = h2f(u16(blk)) * 0.125f;- for (int t = 0; t < NT; ++t)- accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(dx * y[t][i].d),+ for (int t = 0; t < NT; ++t) {+ const float d = h2f(u16(blk)) * (GGML ? y[t][i].d : 0.125f);+ const float scale = GGML ? d : d * y[t][i].d;+ accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(scale),_mm256_cvtepi32_ps(_mm256_add_epi32(acc[t][0], acc[t][1])), accf[t]);+ }}- for (int t = 0; t < NT; ++t) res[t] = hsum8(accf[t]);+ for (int t = 0; t < NT; ++t) res[t] = GGML ? 0.125f * ggml_hsum8(accf[t]) : hsum8(accf[t]);}template <int TY, int NT> STRATA_ROWS_FNdiff --git a/src/kernels/cpu/native_expert.cpp b/src/kernels/cpu/native_expert.cppindex 1ab75b642..e869d1ae5 100644--- a/src/kernels/cpu/native_expert.cpp+++ b/src/kernels/cpu/native_expert.cpp@@ -15,6 +15,7 @@#include "ggml-cpu.h"#include <cmath>+#include <cstdio>#include <cstdlib>#include <cstring>#include <mutex>@@ -164,6 +165,17 @@ void native_gu_rows(const NativeFmt& f, const uint8_t* blob, const void* const*// as the AVX-2 kernels, and STRATA_IQ_MT_MIN=1 takes the kernel for one token too (its rows are then// independent of the drafting: iq_avx1_parity checks every width bit for bit).static const bool avx1 = !cpu_avx2_ok() && cpu_avx1_ok() && std::getenv("STRATA_NO_IQ128") == nullptr;+ static const bool exact_one = [] {+ const char* v = std::getenv("STRATA_CPU_IQ_ALL_EXACT1");+ return v && v[0] == '1' && v[1] == '\0';+ }();+ if (exact_one && nt == 1 && avx2 && !cpu_avx512_ok() &&+ (f.gu_type == 16 || f.gu_type == 17 || f.gu_type == 18 || f.gu_type == 21)) {+ static std::once_flag announced;+ std::call_once(announced, [] { std::fprintf(stderr, "strata ab: GATE=STRATA_CPU_IQ_ALL_EXACT1 active=1\n"); });+ iq256_gu_rows_exact_one(f.gu_type, blob, f.gu_row, f.up_off, (int) f.n_embd, act[0], ff[0], r0, r1);+ return;+ }const int mt_min = native_gu_mt_min(f.gu_type); // #152// Unsloth UD-Q4_K_XL's Q4_K gate/up: the multi-token kernel is bit-exact against ggml's per-token dot (any group// size, no #152 rule). Opt-in, STRATA_KQ256=1: measured no faster in the engine (a window's expert groups holddiff --git a/src/kernels/native_expert_parity.cpp b/src/kernels/native_expert_parity.cppindex 3c979ebb9..44b8d0b05 100644--- a/src/kernels/native_expert_parity.cpp+++ b/src/kernels/native_expert_parity.cpp@@ -100,6 +100,25 @@ int check_blob(const cpu::NativeFmt& f, const std::vector<uint8_t>& blob, int sea[k] = act[k].data();ffp[k] = ff[k].data();}+ if ((f.gu_type == 18 || f.gu_type == 21) && cpu::cpu_avx2_ok() && !cpu::cpu_avx512_ok()) {+ const auto dot = ggml_get_type_traits_cpu((ggml_type) f.gu_type)->vec_dot;+ size_t differ = 0;+ std::vector<float> exact((size_t) FF);+ for (int k = 0; k < NT; ++k) {+ cpu::iq256_gu_rows_exact_one(f.gu_type, blob.data(), f.gu_row, f.up_off, (int) H, a[k],+ exact.data(), 0, (int) FF);+ for (int r = 0; r < FF; ++r) {+ float g = 0.f, u = 0.f;+ dot((int) H, &g, 0, blob.data() + (size_t) r * f.gu_row, 0, a[k], 0, 1);+ dot((int) H, &u, 0, blob.data() + f.up_off + (size_t) r * f.gu_row, 0, a[k], 0, 1);+ const float want = (g / (1.f + std::exp(-g))) * u;+ differ += std::memcmp(&want, &exact[(size_t) r], sizeof(float)) != 0;+ }+ }+ std::printf("%s exact singleton: %zu of %lld real gate/up rows differ from ggml\n",+ label.c_str(), differ, (long long) (NT * FF));+ if (differ) ++failures;+ }cpu::native_gu_rows(f, blob.data(), a, NT, ffp, 0, (int) FF);// either multi-token kernel: IQ4_XS has an AVX-2 one and no AVX-512 one, so the gate cannot be// iq512_supported alone - that would leave the format untested on every CPU.