Skip to content

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++

native experts: IQ3_S takes the AVX-2 kernel for one token too where its grid is gathered

Francesco Albanod4b8b6ad5cfd
diff --git a/docs/DETAILS.md b/docs/DETAILS.md
index 5af8a1805..a04717566 100644
--- a/docs/DETAILS.md
+++ b/docs/DETAILS.md
@@ -90,7 +90,9 @@ slightly differently. How many tokens share an expert depends on the drafts in a
at temperature 0 can end in a different (equally good) answer when the drafting, the cache state or a resumed
conversation differ (issue #152). `STRATA_IQ_MT_MIN=1` (in the config's `env`) uses the multi-token kernels for
every group: the answer then no longer depends on the drafting. Measured on a Ryzen 7600 (AVX-512): IQ3_S decode
--1..-3%, the other models the same; the default stays the fastest rule. Through the server, two more things carry
+-1..-3%, the other models the same; the default stays the fastest rule. On an Intel CPU of Alder Lake or later
+without AVX-512, where the AVX-2 kernel gathers the IQ3_S grid, IQ3_S takes the multi-token kernel for one token by
+default, because there it is the faster one (`STRATA_IQ_MT_MIN=2` restores ggml's dot). Through the server, two more things carry
over from one request to the next (#410): the adaptive tier moves experts between RAM and VRAM (the GPU and the CPU
round an expert differently), and the prompt cache resumes a repeated prompt and reads only its tail through the
decode path. For byte-identical repeats add `--prompt-cache 0 --adapt-swaps 0 --pcie-frac 0` to the engine's args
diff --git a/include/strata/kernels/cpu/native_expert.hpp b/include/strata/kernels/cpu/native_expert.hpp
index 9b807427a..c41a1991b 100644
--- a/include/strata/kernels/cpu/native_expert.hpp
+++ b/include/strata/kernels/cpu/native_expert.hpp
@@ -39,6 +39,9 @@ void native_quant_act(const NativeFmt& f, const float* x, void* dst);
/// h (n_ff floats) -> the down activation (h_bytes).
void native_quant_h(const NativeFmt& f, const float* h, void* dst);
+/// From how many tokens native_gu_rows gives this gate/up type to a multi-token kernel (#152; ggml-cpu's per-token dot
+/// below that). 1: a token's rows are the same alone and in any group.
+int native_gu_mt_min(int gu_type);
/// ff[t][r] = silu(gate_r . a[t]) * (up_r . a[t]) for rows r in [r0, r1), `nt` tokens.
void native_gu_rows(const NativeFmt& f, const uint8_t* blob, const void* const* act, int nt, float* const* ff,
int r0, int r1);
diff --git a/src/kernels/cpu/expert_layout.cpp b/src/kernels/cpu/expert_layout.cpp
index 0772d9889..cc218f272 100644
--- a/src/kernels/cpu/expert_layout.cpp
+++ b/src/kernels/cpu/expert_layout.cpp
@@ -303,6 +303,7 @@ bool native_experts_available() noexcept { return false; }
bool native_fmt(int, int, int64_t, int64_t, NativeFmt&, std::string& err) { err = "built without native experts"; return false; }
void native_quant_act(const NativeFmt&, const float*, void*) { std::abort(); }
void native_quant_h(const NativeFmt&, const float*, void*) { std::abort(); }
+int native_gu_mt_min(int) { return 2; }
void native_gu_rows(const NativeFmt&, const uint8_t*, const void* const*, int, float* const*, int, int) { std::abort(); }
void native_down_rows(const NativeFmt&, const uint8_t*, const void* const*, int, float* const*, int, int) { std::abort(); }
#endif
diff --git a/src/kernels/cpu/iq_avx2_parity.cpp b/src/kernels/cpu/iq_avx2_parity.cpp
index 3671f96ca..8de7d87d1 100644
--- a/src/kernels/cpu/iq_avx2_parity.cpp
+++ b/src/kernels/cpu/iq_avx2_parity.cpp
@@ -6,7 +6,8 @@
// IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS, IQ3_S and IQ4_XS gate/up rows, IQ4_NL and Q2_0 down rows: every variant
// this CPU runs, 1..8 tokens, each token's rows bit for bit the scalar variant's rows for that token alone (so
// also the same at every width, #152); the scalar rows against ggml-cpu's vec_dot (Q2_0: a double
-// reference), rel <= 1e-5.
+// reference), rel <= 1e-5; and where the engine's dispatch (native_gu_rows) takes the multi-token kernel from
+// one token on (native_gu_mt_min), each token's rows the same alone and in a group.
// iq_avx2_parity --bench [--cpu N] [--nt 1,2,3,4] [--mb 256] [--reps 5] [--pairs iq3_s/iq4_nl,...] [--dispatch]
// ms per expert on one thread: each variant, ggml-cpu's per-token dot, and the engine's dispatch
// (native_gu_rows, native_down_rows / q2_rows_any) over --mb MB of expert blobs - far more than an L3 holds, so
@@ -331,6 +332,43 @@ int check_down(int d_type) {
return failures;
}
+#if !defined(STRATA_IQ_PARITY_DISPATCH_ONLY)
+// #152 through the engine's dispatch: where native_gu_rows gives a format the multi-token kernel from one token on
+// (native_gu_mt_min 1: IQ3_S where its grid is gathered, or every format under STRATA_IQ_MT_MIN=1), each token's
+// gate/up rows must be the same alone and in any group.
+int check_engine_width(int type) {
+ const int mt = cpu::native_gu_mt_min(type);
+ if (mt != 1) {
+ std::printf(" %-8s engine: ggml's dot below %d tokens, its one-token rows round differently (#152)\n",
+ type_name(type), mt);
+ return 0;
+ }
+ cpu::NativeFmt f;
+ std::string err;
+ if (!cpu::native_fmt(type, 20, kH, kFF, f, err)) return 1;
+ std::vector<uint8_t> blob(f.bytes);
+ uint64_t seed = 0xe9900000ull + (uint64_t) type;
+ fill_blob(blob.data(), f, seed);
+ const Acts a(f, 13u + (unsigned) type);
+ const size_t R = (size_t) kFF;
+ std::vector<float> one(kMaxT * R), ff(kMaxT * R);
+ for (int t = 0; t < kMaxT; ++t) {
+ float* o = &one[t * R];
+ cpu::native_gu_rows(f, blob.data(), &a.gup[t], 1, &o, 0, (int) kFF);
+ }
+ size_t differ = 0;
+ for (int nt = 2; nt <= kMaxT; ++nt) {
+ float* fp[kMaxT];
+ for (int t = 0; t < nt; ++t) fp[t] = &ff[t * R];
+ cpu::native_gu_rows(f, blob.data(), a.gup, nt, fp, 0, (int) kFF);
+ for (int t = 0; t < nt; ++t) differ += rows_differ(fp[t], &one[t * R], R);
+ }
+ std::printf(" %-8s engine: multi-token from one token on, 2..8 tokens: %zu gate/up rows differ from the one-token "
+ "rows\n", type_name(type), differ);
+ return differ ? 1 : 0;
+}
+#endif
+
// ---- bench
struct Method {
std::string name;
@@ -506,6 +544,9 @@ int main(int argc, char** argv) {
int failures = 0;
for (int type : {16, 17, 22, 18, 21, 23}) failures += check_gate_up(type);
for (int type : {20, 42}) failures += check_down(type);
+#if !defined(STRATA_IQ_PARITY_DISPATCH_ONLY)
+ for (int type : {16, 17, 22, 18, 21, 23}) failures += check_engine_width(type);
+#endif
std::printf("iq_avx2_parity: %d failures\n", failures);
return failures == 0 ? 0 : 1;
}
diff --git a/src/kernels/cpu/native_expert.cpp b/src/kernels/cpu/native_expert.cpp
index 0dc5d3aa8..22759ce69 100644
--- a/src/kernels/cpu/native_expert.cpp
+++ b/src/kernels/cpu/native_expert.cpp
@@ -76,6 +76,24 @@ void native_quant_h(const NativeFmt& f, const float* h, void* dst) {
traits(f.d_act)->from_float(h, dst, f.n_ff);
}
+int native_gu_mt_min(int gu_type) {
+ // #152: from how many tokens the multi-token kernels run (ggml's vec_dot below that). The default 2 is the
+ // measured-fastest rule, but a token's expert rows then round differently alone than in a group, so greedy output
+ // can depend on how many drafts a verify window held. STRATA_IQ_MT_MIN=1 (opt-in, 0.1.30) uses the multi-token
+ // kernels for every group: output independent of the drafting, at a measured -1..-3% decode on IQ3_S (AVX-512).
+ static const char* env = std::getenv("STRATA_IQ_MT_MIN");
+ static const int mt_min = env ? std::atoi(env) : 2;
+ // IQ3_S where the AVX-2 kernel gathers its grid (cpu_gather_fast, unless STRATA_IQ256_GATHER=0) on a CPU without
+ // AVX-512: there that kernel beats ggml's dot for ONE token too (14900KF P-core, an expert's gate/up rows: 0.34 ->
+ // 0.19 ms; its E-cores, which keep the scalar decode: 0.71 -> 0.68), and in decode most CPU experts serve one token
+ // of the window. So IQ3_S takes it for every group, and those rows no longer depend on the drafting. A machine-
+ // wide rule, not a per-core one: every core rounds an expert the same. Every probe here is in expert_layout.cpp
+ // and cpu_avx2_ok() comes first, so no AVX2 code runs before the check. STRATA_IQ_MT_MIN set keeps its rule.
+ static const bool iq3s_one = env == nullptr && cpu_avx2_ok() && std::getenv("STRATA_NO_IQ256") == nullptr &&
+ !cpu_avx512_ok() && iq256_gather_setting() != 0 && cpu_gather_fast();
+ return iq3s_one && gu_type == 21 ? 1 : mt_min;
+}
+
void native_gu_rows(const NativeFmt& f, const uint8_t* blob, const void* const* act, int nt, float* const* ff,
int r0, int r1) {
// the multi-token kernels decode the weights once for all tokens: 2.0-2.4x ggml-cpu at three tokens, no faster
@@ -92,11 +110,7 @@ void native_gu_rows(const NativeFmt& f, const uint8_t* blob, const void* const*
// baseline the build selected (AVX1 here) and covers the same types - IQ2_XXS and IQ2_S among
// them. That path loops over tokens itself, so it is correct for any `nt`, not just one.
static const bool avx2 = cpu_avx2_ok() && std::getenv("STRATA_NO_IQ256") == nullptr;
- // #152: from how many tokens the multi-token kernels run (ggml's vec_dot below that). The default 2 is the
- // measured-fastest rule, but a token's expert rows then round differently alone than in a group, so greedy output
- // can depend on how many drafts a verify window held. STRATA_IQ_MT_MIN=1 (opt-in, 0.1.30) uses the multi-token
- // kernels for every group: output independent of the drafting, at a measured -1..-3% decode on IQ3_S (AVX-512).
- static const int mt_min = [] { const char* e = std::getenv("STRATA_IQ_MT_MIN"); return e ? std::atoi(e) : 2; }();
+ 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 hold
// ~1.4 tokens and the weights stay in L1 across ggml's per-token calls; 1.01-1.13x in native_expert_parity).