From 2a669f3c29c97b7d22fba5a32822b2bf1ea683df Mon Sep 17 00:00:00 2001 From: Alex Sadleir Date: Sun, 11 Oct 2026 23:08:05 +1100 Subject: [PATCH 1/9] cpu: the AVX2 router dot keeps its accumulators in registers (2.1x on a Zen 3) bf16_rows_dot_multi held its per-token sums in __m256 acc[MAXT] indexed by a variable. With eight 256-bit accumulators live GCC also unrolled the column loop and left the whole array in memory, so every token's FMA read and wrote a stack slot (vmovaps 0xc0(%rsp) ... vfmadd213ps 0xe0(%rsp)). On a Ryzen 7 5700X3D one layer's router [512 x 2560] at 4 tokens took 0.41 ms against 0.25 ms for the AVX1 copy in kq_avx1.cpp that kept its accumulators in registers, so every AVX2 CPU ran the slower of its two kernels (router_dot_parity --cpu 2, median of 3). Pragma GCC unroll 1 on the column loop changes nothing; the variable-indexed array is what forces the array into memory. Four tokens per pass, in four named accumulators. A row above four tokens is read a second time, from L1: nt=8 measures 0.37 ms, the same as the spilled form. On the same CPU the AVX2 kernel goes 0.41 -> 0.19 ms at 4 tokens and nt=2 0.36 -> 0.16 ms, and it now runs 1.3x ahead of the AVX1 copy instead of 1.6x behind it. The loop body carries no stack slot any more (was 21 memory ops per iteration). Untouched: the arithmetic and the reduction. Each token's FMA chain still runs over the same columns in the same order and ends in the same hsum_float_8, so every sum is bit-identical - the output for nt 1/2/4/8 at cols 2560/5120 hashes the same before and after (FNV-1a over the whole out[]), and router_dot_parity reports 0 failures with the AVX2 rel error unchanged at 3.16e-07. Left alone: the expert row kernels (iq_avx2.cpp, q2_avx2.cpp, kq256_gu_rows, kq256_rows) and the AVX1 path. The iq_avx2_parity bench over four format pairs x 1..8 tokens moved within 1.6% (noise), and the CPU parity tests (8 targets) pass. The router stays what it was, an estimate for which experts to prefetch. CUDA and HIP do not compile this file; the SYCL port does, and it builds there under icpx 2026.1 with no new warnings and no spills either. The AVX1 copy in kq_avx1.cpp kept a comment saying it reads its rows "exactly as the AVX2 kernel does"; with the row loop chunked four tokens at a time that is no longer the AVX2 kernel's shape, so the clause is dropped there. Comment only - no code change. --- src/kernels/cpu/kq_avx1.cpp | 2 +- src/kernels/cpu/kq_avx2.cpp | 32 ++++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/kernels/cpu/kq_avx1.cpp b/src/kernels/cpu/kq_avx1.cpp index fbdf73f4..738d94cd 100644 --- a/src/kernels/cpu/kq_avx1.cpp +++ b/src/kernels/cpu/kq_avx1.cpp @@ -43,7 +43,7 @@ inline __m256 bf16x8_to_f32x8_avx1(const uint16_t* p) { } // namespace void bf16_rows_dot_multi_avx1(const uint16_t* w, int rows, int cols, const float* x, int nt, float* out) { - // each row is read once for all `nt` (<= 8) tokens, exactly as the AVX2 kernel does + // each row is read once for all `nt` (<= 8) tokens for (int r = 0; r < rows; ++r) { const uint16_t* wr = w + (size_t) r * (size_t) cols; __m256 acc[8]; diff --git a/src/kernels/cpu/kq_avx2.cpp b/src/kernels/cpu/kq_avx2.cpp index f7228732..9dd251ca 100644 --- a/src/kernels/cpu/kq_avx2.cpp +++ b/src/kernels/cpu/kq_avx2.cpp @@ -200,17 +200,29 @@ void dot_rows(int type, const uint8_t* row, int n, const void* const* act, int n } // namespace void bf16_rows_dot_multi(const uint16_t* w, int rows, int cols, const float* x, int nt, float* out) { - // each row is read once for all `nt` (<= 8) tokens: the router (2.6 MB per layer) streams once per prediction - for (int r = 0; r < rows; ++r) { - const uint16_t* wr = w + (size_t) r * (size_t) cols; - __m256 acc[8]; - for (int t = 0; t < nt; ++t) acc[t] = _mm256_setzero_ps(); - for (int c = 0; c + 8 <= cols; c += 8) { - const __m256 wf = _mm256_castsi256_ps( - _mm256_slli_epi32(_mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i*) (wr + c))), 16)); - for (int t = 0; t < nt; ++t) acc[t] = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x + (size_t) t * cols + c), acc[t]); + for (int t0 = 0; t0 < nt; t0 += 4) { + const int m = nt - t0 < 4 ? nt - t0 : 4; + const float* x0 = x + (size_t) t0 * cols; + const float* x1 = x0 + cols; + const float* x2 = x1 + cols; + const float* x3 = x2 + cols; + for (int r = 0; r < rows; ++r) { + const uint16_t* wr = w + (size_t) r * (size_t) cols; + __m256 a0 = _mm256_setzero_ps(), a1 = _mm256_setzero_ps(), a2 = _mm256_setzero_ps(), + a3 = _mm256_setzero_ps(); + for (int c = 0; c + 8 <= cols; c += 8) { + const __m256 wf = _mm256_castsi256_ps( + _mm256_slli_epi32(_mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i*) (wr + c))), 16)); + a0 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x0 + c), a0); + if (m > 1) a1 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x1 + c), a1); + if (m > 2) a2 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x2 + c), a2); + if (m > 3) a3 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x3 + c), a3); + } + out[(size_t) t0 * rows + r] = hsum_float_8(a0); + if (m > 1) out[(size_t) (t0 + 1) * rows + r] = hsum_float_8(a1); + if (m > 2) out[(size_t) (t0 + 2) * rows + r] = hsum_float_8(a2); + if (m > 3) out[(size_t) (t0 + 3) * rows + r] = hsum_float_8(a3); } - for (int t = 0; t < nt; ++t) out[(size_t) t * rows + r] = hsum_float_8(acc[t]); // cols % 8 == 0 (2560) } } From 79376a2732050cfa469213a3e9412dab2a173cca Mon Sep 17 00:00:00 2001 From: Alex Sadleir Date: Tue, 13 Oct 2026 22:31:04 +1100 Subject: [PATCH 2/9] cpu: the AVX2 expert-row dots keep their accumulators in registers too q5_1_dot and q8_0_dot in kq_avx2.cpp held their per-token sums in the same runtime-indexed __m256 acc[MAXT] that made bf16_rows_dot_multi spill: with the array live the compiler leaves it in memory, and every token's FMA reads and writes a stack slot. Both are now template , dispatched 1..4 and, past that, run four tokens per pass with the row's bytes re-read from L1 - the same idiom the router change above and kq256_gu_rows already use. Ryzen 7 9800X3D (Zen 5), GCC 15.2.0, build/native_expert_parity --synthetic (ggml-quantized weights and activations), one thread pinned to core 0, at the verify window's four tokens: Q5_1 down rows [2560 x 640]: 190-197 us -> 155-156 us (1.24x) Q8_0 down rows [2560 x 640]: 135 us -> 104 us (1.30x) Same sums: 0 of 36 token-sets differ in any bit against ggml's own vec_dot over 1..8 tokens, both before and after; native_expert_parity reports 0 failures. Left alone: q4k_dot keeps its runtime-nt array. Its token accumulators are not what spills - the unpatched function carries 8 ymm stack ops for the sums against 24 (q5_1) and 59 (q8_0); what q4k_dot spills is its weight-side unpack arrays (q4l/q4h/scale_l/scale_h), which token chunking does not touch and re-does. The same change measured +5% at four tokens and -4..-7% at six to eight, so it is a wash at the window and a small regression above it, and q4k is the gate/up format that runs most. The AVX1 path, the iq_avx2/q2_avx2 rows and the engine's dispatch are untouched. Backends: CUDA and HIP do not compile this file. The SYCL port does; not built on this machine (no icpx here) - the router change in this branch built there. Tests: native_expert_parity --synthetic q4_K/q5_1 and q4_K/q8_0 (0 failures, 0 bits differ), iq_avx2_parity, iq_avx1_parity, q8k_quant_parity, router_dot_parity - all exit 0. --- src/kernels/cpu/kq_avx2.cpp | 52 +++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/src/kernels/cpu/kq_avx2.cpp b/src/kernels/cpu/kq_avx2.cpp index 9dd251ca..89ecc1fe 100644 --- a/src/kernels/cpu/kq_avx2.cpp +++ b/src/kernels/cpu/kq_avx2.cpp @@ -145,10 +145,11 @@ void q4k_dot(const block_q4_K* x, int nb, const void* const* act, int nt, float* } // ---- Q5_1 x Q8_1 (ggml_vec_dot_q5_1_q8_1, __AVX2__) -void q5_1_dot(const block_q5_1* x, int nb, const void* const* act, int nt, float* res) { - __m256 acc[MAXT]; - float summs[MAXT]; - for (int t = 0; t < nt; ++t) { acc[t] = _mm256_setzero_ps(); summs[t] = 0.0f; } +template +void q5_1_dot(const block_q5_1* x, int nb, const void* const* act, float* res) { + __m256 acc[NT]; + float summs[NT]; + for (int t = 0; t < NT; ++t) { acc[t] = _mm256_setzero_ps(); summs[t] = 0.0f; } for (int ib = 0; ib < nb; ++ib) { const __m256 dx = _mm256_set1_ps(half_at(&x[ib], 0)); const float xm = half_at(&x[ib], 1); @@ -156,7 +157,7 @@ void q5_1_dot(const block_q5_1* x, int nb, const void* const* act, int nt, float __m256i bxhi = bytes_from_bits_32(x[ib].qh); bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10)); qx = _mm256_or_si256(qx, bxhi); - for (int t = 0; t < nt; ++t) { + for (int t = 0; t < NT; ++t) { const block_q8_1* y = (const block_q8_1*) act[t] + ib; summs[t] += xm * half_at(y, 1); const __m256 dy = _mm256_set1_ps(half_at(y, 0)); @@ -165,18 +166,19 @@ void q5_1_dot(const block_q5_1* x, int nb, const void* const* act, int nt, float acc[t] = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc[t]); } } - for (int t = 0; t < nt; ++t) res[t] = hsum_float_8(acc[t]) + summs[t]; + for (int t = 0; t < NT; ++t) res[t] = hsum_float_8(acc[t]) + summs[t]; } // ---- Q8_0 x Q8_0 (ggml_vec_dot_q8_0_q8_0, __AVX2__; mul_sum_i8_pairs_float without AVX-VNNI-INT8) -void q8_0_dot(const block_q8_0* x, int nb, const void* const* act, int nt, float* res) { - __m256 acc[MAXT]; - for (int t = 0; t < nt; ++t) acc[t] = _mm256_setzero_ps(); +template +void q8_0_dot(const block_q8_0* x, int nb, const void* const* act, float* res) { + __m256 acc[NT]; + for (int t = 0; t < NT; ++t) acc[t] = _mm256_setzero_ps(); for (int ib = 0; ib < nb; ++ib) { const float xd = h2f(x[ib].d); const __m256i qx = _mm256_loadu_si256((const __m256i*) x[ib].qs); const __m256i ax = _mm256_sign_epi8(qx, qx); - for (int t = 0; t < nt; ++t) { + for (int t = 0; t < NT; ++t) { const block_q8_0* y = (const block_q8_0*) act[t] + ib; const __m256 d = _mm256_set1_ps(xd * h2f(y->d)); const __m256i qy = _mm256_loadu_si256((const __m256i*) y->qs); @@ -185,14 +187,38 @@ void q8_0_dot(const block_q8_0* x, int nb, const void* const* act, int nt, float acc[t] = _mm256_fmadd_ps(d, q, acc[t]); } } - for (int t = 0; t < nt; ++t) res[t] = hsum_float_8(acc[t]); + for (int t = 0; t < NT; ++t) res[t] = hsum_float_8(acc[t]); +} + +void q5_1_dot_nt(const block_q5_1* x, int nb, const void* const* act, int nt, float* res) { + for (int t0 = 0; t0 < nt; t0 += 4) { + const int k = nt - t0 < 4 ? nt - t0 : 4; + switch (k) { + case 1: q5_1_dot<1>(x, nb, act + t0, res + t0); break; + case 2: q5_1_dot<2>(x, nb, act + t0, res + t0); break; + case 3: q5_1_dot<3>(x, nb, act + t0, res + t0); break; + default: q5_1_dot<4>(x, nb, act + t0, res + t0); break; + } + } +} + +void q8_0_dot_nt(const block_q8_0* x, int nb, const void* const* act, int nt, float* res) { + for (int t0 = 0; t0 < nt; t0 += 4) { + const int k = nt - t0 < 4 ? nt - t0 : 4; + switch (k) { + case 1: q8_0_dot<1>(x, nb, act + t0, res + t0); break; + case 2: q8_0_dot<2>(x, nb, act + t0, res + t0); break; + case 3: q8_0_dot<3>(x, nb, act + t0, res + t0); break; + default: q8_0_dot<4>(x, nb, act + t0, res + t0); break; + } + } } void dot_rows(int type, const uint8_t* row, int n, const void* const* act, int nt, float* res) { switch (type) { case 12: q4k_dot((const block_q4_K*) row, n / QK_K, act, nt, res); break; - case 7: q5_1_dot((const block_q5_1*) row, n / QK5_1, act, nt, res); break; - case 8: q8_0_dot((const block_q8_0*) row, n / QK8_0, act, nt, res); break; + case 7: q5_1_dot_nt((const block_q5_1*) row, n / QK5_1, act, nt, res); break; + case 8: q8_0_dot_nt((const block_q8_0*) row, n / QK8_0, act, nt, res); break; default: break; } } From 8ee2005dae0bc41c53ea88e5fdb6e2ee995e169d Mon Sep 17 00:00:00 2001 From: InB4DevOps Date: Wed, 7 Oct 2026 20:19:52 +0200 Subject: [PATCH 3/9] cpu: add bit-exact AVX2 singleton expert rows --- include/strata/kernels/cpu/iq_avx2.hpp | 5 +++ src/kernels/cpu/iq_avx2.cpp | 28 ++++++++++++ src/kernels/cpu/iq_avx2_parity.cpp | 25 +++++++++++ src/kernels/cpu/iq_avx2_rows.inl | 62 +++++++++++++++++++++++--- src/kernels/cpu/native_expert.cpp | 12 +++++ src/kernels/native_expert_parity.cpp | 19 ++++++++ 6 files changed, 146 insertions(+), 5 deletions(-) diff --git a/include/strata/kernels/cpu/iq_avx2.hpp b/include/strata/kernels/cpu/iq_avx2.hpp index 519efa2c..3147ed55 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.cpp index 2884fcde..69bd6752 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.cpp index 8de7d87d..bc5f67bf 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 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 g(kMaxT * R), ff(kMaxT * R); @@ -449,6 +468,12 @@ int bench(const std::vector& pairs, const std::vector& nts, in for (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.inl index a8af07fb..48d4dea0 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 STRATA_ROWS_FN +inline void row_dot_iq2xs(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res); + +template 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::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::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 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(blob + (size_t) r * gu_row, n / QK_K, y); + const float u = row_dot_ggml_one(blob + up_off + (size_t) r * gu_row, n / QK_K, y); + ff[r] = (g / (1.f + std::exp(-g))) * u; + } +} + template STRATA_ROWS_FN inline 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 STRATA_ROWS_FN +template STRATA_ROWS_FN inline 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 STRATA_ROWS_FN diff --git a/src/kernels/cpu/native_expert.cpp b/src/kernels/cpu/native_expert.cpp index 1ab75b64..e869d1ae 100644 --- a/src/kernels/cpu/native_expert.cpp +++ b/src/kernels/cpu/native_expert.cpp @@ -15,6 +15,7 @@ #include "ggml-cpu.h" #include +#include #include #include #include @@ -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 hold diff --git a/src/kernels/native_expert_parity.cpp b/src/kernels/native_expert_parity.cpp index 3c979ebb..44b8d0b0 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& blob, int se a[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 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. From 77ddc2ff147dd16ecaa0228b28419e97fbc89e07 Mon Sep 17 00:00:00 2001 From: Stuart Chapin Date: Thu, 8 Oct 2026 04:06:23 -0700 Subject: [PATCH 4/9] cpu: IQ2_S in the bit-exact singleton rows; the singleton path only below the #152 rule Stacked on #1415. IQ2_S (type 22, 122 gathered) joins STRATA_CPU_IQ_ALL_EXACT1's list: ggml's AVX2 iq2_s dot sums the same int32 lanes as row_dot_ggml_one and ends with 0.125f * hsum_float_8. The singleton gate now also requires nt < native_gu_mt_min, so with STRATA_IQ_MT_MIN=1 a lone token keeps the multi-token kernel and native_expert_parity's width invariance passes with the switch on. --- src/kernels/cpu/iq_avx2.cpp | 6 ++++++ src/kernels/cpu/iq_avx2_parity.cpp | 4 ++-- src/kernels/cpu/iq_avx2_rows.inl | 2 +- src/kernels/cpu/native_expert.cpp | 6 ++++-- src/kernels/native_expert_parity.cpp | 2 +- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/kernels/cpu/iq_avx2.cpp b/src/kernels/cpu/iq_avx2.cpp index 69bd6752..3e10aa6e 100644 --- a/src/kernels/cpu/iq_avx2.cpp +++ b/src/kernels/cpu/iq_avx2.cpp @@ -466,6 +466,9 @@ void iq256_gu_rows_exact_one(int type, const uint8_t* blob, size_t gu_row, size_ } 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); + } else if (type == 22) { + if (iq256_variant() & kIq256Gather) vnni::gu_ggml_one<122>(blob, gu_row, up_off, n, y, ff, r0, r1); + else vnni::gu_ggml_one<22>(blob, gu_row, up_off, n, y, ff, r0, r1); } return; } @@ -478,6 +481,9 @@ void iq256_gu_rows_exact_one(int type, const uint8_t* blob, size_t gu_row, size_ } 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); + } else if (type == 22) { + if (iq256_variant() & kIq256Gather) plain::gu_ggml_one<122>(blob, gu_row, up_off, n, y, ff, r0, r1); + else plain::gu_ggml_one<22>(blob, gu_row, up_off, n, y, ff, r0, r1); } } diff --git a/src/kernels/cpu/iq_avx2_parity.cpp b/src/kernels/cpu/iq_avx2_parity.cpp index bc5f67bf..67544c9d 100644 --- a/src/kernels/cpu/iq_avx2_parity.cpp +++ b/src/kernels/cpu/iq_avx2_parity.cpp @@ -248,7 +248,7 @@ 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) { + if (type == 16 || type == 17 || type == 18 || type == 21 || type == 22) { size_t different = 0; std::vector exact(R); const ggml_vec_dot_t dot = traits_cpu(type)->vec_dot; @@ -468,7 +468,7 @@ int bench(const std::vector& pairs, const std::vector& nts, in for (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) + if (gt == 16 || gt == 17 || gt == 18 || gt == 21 || gt == 22) 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, diff --git a/src/kernels/cpu/iq_avx2_rows.inl b/src/kernels/cpu/iq_avx2_rows.inl index 48d4dea0..5489d8ee 100644 --- a/src/kernels/cpu/iq_avx2_rows.inl +++ b/src/kernels/cpu/iq_avx2_rows.inl @@ -63,7 +63,7 @@ inline float row_dot_ggml_one(const uint8_t* row, int nblocks, const block_q8_K* 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); + if constexpr (TY == 16 || TY == 17 || TY == 22 || TY == 122) return 0.125f * ggml_hsum8(accum); else if constexpr (TY == 18 || TY == 118) return 0.25f * ggml_hsum8(accum); else return ggml_hsum8(accum); } diff --git a/src/kernels/cpu/native_expert.cpp b/src/kernels/cpu/native_expert.cpp index e869d1ae..50153002 100644 --- a/src/kernels/cpu/native_expert.cpp +++ b/src/kernels/cpu/native_expert.cpp @@ -169,8 +169,10 @@ void native_gu_rows(const NativeFmt& f, const uint8_t* blob, const void* const* 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)) { + // only where ggml's dot runs today (below the #152 multi-token rule): with STRATA_IQ_MT_MIN=1 a lone token keeps the + // multi-token kernel, so a token's rows still do not depend on how many tokens share its expert + if (exact_one && nt == 1 && nt < native_gu_mt_min(f.gu_type) && avx2 && !cpu_avx512_ok() && + (f.gu_type == 16 || f.gu_type == 17 || f.gu_type == 18 || f.gu_type == 21 || f.gu_type == 22)) { 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); diff --git a/src/kernels/native_expert_parity.cpp b/src/kernels/native_expert_parity.cpp index 44b8d0b0..bbb1551a 100644 --- a/src/kernels/native_expert_parity.cpp +++ b/src/kernels/native_expert_parity.cpp @@ -100,7 +100,7 @@ int check_blob(const cpu::NativeFmt& f, const std::vector& blob, int se a[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()) { + if ((f.gu_type == 18 || f.gu_type == 21 || f.gu_type == 22) && 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 exact((size_t) FF); From ea08bd6e813b687f2331ad7f63068633e568e9db Mon Sep 17 00:00:00 2001 From: Zack <132591805+ZackO2o@users.noreply.github.com> Date: Wed, 7 Oct 2026 12:16:14 +0800 Subject: [PATCH 5/9] decode: size --lookup-chain's window cap from the cap in force, not the stale mtp_max_t With the default --suffix-draft 3 the suffix block above already raises --spec by 2 (4 -> 6) and writes mtp_max_t = 4. The --lookup-chain block then computed min(mtp_max_t + K, 8) = min(4 + K, 8), so with --spec 4: K=1 -> max(6, 5) = 6 (unchanged: the flag does nothing) K=2 -> max(6, 6) = 6 (unchanged: the flag does nothing) K=3 -> max(6, 7) = 7 Measured on 0.1.40 by reading the cap back from /metrics engine.spec after a restart per row; K=1 and K=2 were indistinguishable from not passing the flag. Size from the cap actually in force instead, so K adds up to K of headroom while the kVerifyMaxT = 8 ceiling and the mtp_max_t bound on the MTP's own drafts are preserved. Behavior is unchanged when --suffix-draft is 0 or when the cap is already at 8. --- src/program/generate.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/program/generate.cpp b/src/program/generate.cpp index ba00f94d..0b5e3c40 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -2568,10 +2568,13 @@ int main(int argc, char** argv) { o.mtp_max_t = o.spec; o.spec = std::min(o.spec + 2, 8); // kVerifyMaxT } - // --lookup-chain (opt-in): the MTP keeps its windows; a lookup chained after its drafts may lengthen one by up to K + // --lookup-chain (opt-in): the MTP keeps its windows; a lookup chained after its drafts may lengthen one by up to K. + // Sized from the cap actually in force: the suffix block above may already have raised --spec to --spec+2, so sizing + // from mtp_max_t left K=1 and K=2 with a window identical to not passing the flag at all. if (o.lookup_chain > 0 && o.spec >= 2) { if (o.mtp_max_t == 0) o.mtp_max_t = o.spec; - o.spec = std::max(o.spec, std::min(o.mtp_max_t + o.lookup_chain, 8)); // kVerifyMaxT + const int chain_base = o.spec; + o.spec = std::max(chain_base, std::min(std::max(chain_base, o.mtp_max_t) + o.lookup_chain, 8)); // kVerifyMaxT } strata::core::layer_set_shared_early(!o.shared_late); if (!o.native_preset.empty()) { From 1ab0fbd02a9a94d4a0b512973784786b6a2ef84a Mon Sep 17 00:00:00 2001 From: Xiaomi2008 Date: Sat, 10 Oct 2026 18:05:36 -0700 Subject: [PATCH 6/9] --pool-affinity measure: on a hybrid CPU, time the all and p-cores pool layouts at start and take the faster (opt-in) The pool's default layout puts a worker on every physical core, and a layer's experts are done when the slowest worker is. On a Core i7-13700K (8 P + 8 E cores) the E-cores make the verify window wait, by an amount that changes from run to run. `--pool-affinity measure` (hybrid CPUs, native packs) times both layouts on the pack's own experts before the pool is built: two experts of four tokens per layer through run_split_multi_native, every layer in turn, the host spinning ~150 us between two layers, three interleaved blocks per layout, about a second in all. p-cores is taken unless all is more than 10% faster. One line says what it read and took. It is one reading at start and is not repeated. Measured on this branch (0.1.42 + this change), Core i7-13700K, RTX 5090, 64 GB, Windows 11, Swift 1.5 IQ3_XXS, 128K context, one 8K prompt and the same 1,500 forced tokens (--spec-follow), three interleaved rounds: default (all, 15 workers) 146.6 / 151.8 / 158.0 tok/s, pool 6.3-7.6 ms per window --pool-affinity measure 200.0 / 201.5 / 204.8 tok/s, pool 2.1-2.2 ms (read all at 16.4-17.6 ms, took p-cores) --pool-affinity p-cores 200.3 / 203.9 / 204.9 tok/s, pool 2.1 ms Over four server starts of one day on 0.1.41 it read all at 14.8, 21.9 and 25.3 ms against 6.1-6.2 ms for p-cores, and once all at 6.6 ms against 8.6 ms, where it took all. Without the value nothing new runs: the option parses as before and the function is not called. The layout changes no result, only where threads run. Built for CUDA only (sm_120, CUDA 13.0.88, MSVC 14.50 with nvcc's --allow-unsupported-compiler); HIP and SYCL were not compiled (the change is in generate.cpp, which both build). Not measured: other hybrid CPUs, Linux. --- docs/DETAILS.md | 11 ++++ src/program/generate.cpp | 109 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 2 deletions(-) diff --git a/docs/DETAILS.md b/docs/DETAILS.md index 5bd5fbbb..6b796703 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -272,6 +272,17 @@ GPU nor the RAM budget holds (only pages - the experts computed are the same; `S is what runs [Unsloth's UD-Q4_K_XL](UNSLOTH_Q4.md) (72 GiB of experts) on a 64 GB PC: 7-8.5 tokens/s at N = 40 on an RTX 5070, against ~3 tokens/s before these changes. +**Opt-in switches measured on a hybrid Intel CPU under Windows (Core i7-13700K, 8 P + 8 E cores, RTX 5090, 64 GB RAM, +Windows 11, Swift 1.5 IQ3_XXS at 128K):** +- `--pool-affinity measure` (hybrid CPUs, native packs): the `all` and `p-cores` layouts are timed at start on the + pack's own experts (about a second) and the faster one is taken. Engine 0.1.41 built from source, the same prompt: + `all` (15 workers) spent 6.7 ms per verify window in the CPU pool and decoded 148 tok/s, `p-cores` (7 workers) 2.2 ms + and 188-191 tok/s; in another run of the same day `all` took 2.4 ms, so the difference comes and goes. The + measurement is taken once, at start: over four server starts of one day it read `all` at 14.8, 21.9 and 25.3 ms + against 6.1 to 6.2 ms for `p-cores` and took `p-cores`, and once read `all` at 6.6 ms against 8.6 ms and took `all`. + It does not look again during a session, so where the E-cores' speed changes while the engine runs, + `--pool-affinity p-cores` is the steady choice. Not measured: other CPUs, Linux, AMD cards. + **Switches added in 0.1.40 (all off unless noted; none changes the default output):** - `--kv-grow` (or `STRATA_KV_GROW=1`; `--no-kv-grow` turns it off): the K/V takes VRAM only for the cells the requests reach, and the expert cache holds the rest, giving slots back as the context grows. It needs one GPU, an expert diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 0b5e3c40..2b7a65d4 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -438,6 +438,9 @@ struct Options { int pool_tasks = 0; ///< Batched CPU expert tasks per phase; 0 keeps the existing policy /// #272: the pool's core layout; `all` (the default) is the layout it always had, auto / p-cores are opt-in strata::kernels::cpu::PoolAffinity pool_affinity = strata::kernels::cpu::PoolAffinity::All; + /// --pool-affinity measure (opt-in, hybrid CPUs, native packs): `all` and `p-cores` are timed on this pack's + /// own experts at start and the faster layout is taken (measure_pool_affinity) + bool pool_measure = false; /// --host-core first|last (STRATA_HOST_CORE): the host thread's core (see HostCore in pool.hpp) std::string host_core; /// --aux-cpus off|auto|LIST (STRATA_AUX_CPUS): where the non-pool, non-host threads run (strata/platform/aux_cpus.hpp) @@ -922,7 +925,9 @@ void usage() { " result. STRATA_AUX_CPUS sets it too; a request's strata_tune aux_cpus=0|1 beats it.\n" " --pool-affinity MODE Worker CPU affinity: all (default: one worker per physical core, as\n" " always), auto (hybrid CPUs: P-cores first, then their SMT siblings,\n" - " then E-cores) or p-cores (P-cores and their siblings only).\n" + " then E-cores) or p-cores (P-cores and their siblings only). measure (opt-in,\n" + " hybrid CPUs, native packs): all and p-cores are timed on the pack's own experts\n" + " at start (about a second) and the faster one is taken\n" " --coupled-draft enable coupled draft sampling for MTP drafter under sampling (STRATA_SPEC_COUPLED)\n" " --no-coupled-draft disable coupled draft sampling (propose argmax drafts)\n" " --mmap-experts R2.1: opt OUT of the resident expert arena, back to MapViewOfFile.\n" @@ -1726,6 +1731,103 @@ double pcie_frac_for_gbps(double gbps, double base) { return gbps <= 0.0 ? base : base * std::min(1.0, gbps / 20.0); } +// --pool-affinity measure: `all` against `p-cores` on a hybrid CPU, timed on this pack's own experts. +// +// The pool's default layout puts a worker on every physical core, and a layer's experts are done when the slowest +// worker is. Measured on a Core i7-13700K (8 P + 8 E), IQ3_XXS, RTX 5090, same prompt and build: `all` (15 workers) +// spent 6.7 ms per verify window in the pool and decoded 148 tok/s, `p-cores` (7 workers) 2.2 ms and 188-191 tok/s; +// in an earlier run of the same day `all` took 2.4 ms, so the difference is not constant. Hence a measurement at +// start and not a rule. +// +// What is timed is the decode's own call: two missed experts of four tokens per layer (run_split_multi_native on +// blobs from the arena), every layer in turn, with the host spinning ~150 us between two layers as it does on the +// GPU's flags. The two layouts take turns in kBlocks blocks and the medians are compared; `p-cores` is taken +// unless `all` is more than 10% faster (fewer threads wait on fewer cores when the two are close). The layout +// changes no result, only where threads run. It is one reading at start and is not repeated: on the same PC one +// server start in four read `all` as the faster layout (6.6 ms against 8.6), where the others read it 2.4 to 4 times +// slower. +strata::kernels::cpu::PoolAffinity measure_pool_affinity(strata::core::ExpertSource* src, int workers, bool host_works, + int tasks) { + namespace cpu = strata::kernels::cpu; + using Clock = std::chrono::steady_clock; + const cpu::ExpertLayout& lay = cpu::expert_layout(); + const cpu::CpuTopology topo = cpu::detect_cpu_topology(true, cpu::PoolAffinity::PCores); + if (!topo.is_hybrid) { + std::fprintf(stderr, "strata generate: --pool-affinity measure: this CPU is not a hybrid one; the layout stays all\n"); + return cpu::PoolAffinity::All; + } + constexpr int kExperts = 2, kTok = 4, kRounds = 10, kBlocks = 3; + const int64_t nl = lay.n_layers; + std::vector blobs((size_t) nl * kExperts, nullptr); + bool have = src != nullptr && lay.native && (int64_t) lay.fmt.size() >= nl && nl > 0; + uint32_t seed = 12345u; + for (int64_t l = 0; have && l < nl; ++l) + for (int j = 0; j < kExperts; ++j) { + seed = seed * 1664525u + 1013904223u; + const uint8_t* b = src->blob(l, (int64_t) ((seed >> 8) % (uint32_t) lay.n_expert)); + if (b == nullptr) { have = false; break; } + blobs[(size_t) l * kExperts + (size_t) j] = b; + } + if (!have) { + std::fprintf(stderr, "strata generate: --pool-affinity measure needs a native pack with its experts in RAM; " + "the layout stays all\n"); + return cpu::PoolAffinity::All; + } + std::vector nact((size_t) nl * kTok * cpu::kNativeActBytes); + std::vector qact((size_t) nl * kTok); + std::vector x((size_t) cpu::H), out((size_t) kExperts * kTok * cpu::H); + for (int64_t l = 0; l < nl; ++l) + for (int t = 0; t < kTok; ++t) { + for (int i = 0; i < cpu::H; ++i) x[(size_t) i] = std::sin((float) (i + 13 * t + 7 * (int) l) * 0.031f); + const size_t at = (size_t) l * kTok + (size_t) t; + if (cpu::q2_native_kernels(lay.fmt[(size_t) l].gu_type)) cpu::act_quant_any(x.data(), cpu::H, qact[at]); + else cpu::native_quant_act(lay.fmt[(size_t) l], x.data(), nact.data() + at * cpu::kNativeActBytes); + } + int n_workers[2] = {0, 0}; + auto block = [&](cpu::PoolAffinity aff, int which) -> double { + cpu::ExpertPool pool(workers, /*pin=*/true, host_works, aff, tasks); + n_workers[which] = pool.workers(); + const int host = cpu::planned_host_core(aff); + cpu::ThreadAffinity prev; + if (host >= 0) prev = cpu::pin_current_thread(host); + cpu::ExpertJobMulti jobs[kExperts]; + double ms = 0.0; + for (int r = -1; r < kRounds; ++r) // round -1 is not timed: the workers start and the rows are touched + for (int64_t l = 0; l < nl; ++l) { + for (int j = 0; j < kExperts; ++j) { + jobs[j].blob = blobs[(size_t) l * kExperts + (size_t) j]; + jobs[j].nt = kTok; + for (int t = 0; t < kTok; ++t) { + const size_t at = (size_t) l * kTok + (size_t) t; + jobs[j].act[t] = &qact[at]; + jobs[j].nact[t] = nact.data() + at * cpu::kNativeActBytes; + jobs[j].out[t] = out.data() + ((size_t) j * kTok + (size_t) t) * cpu::H; + } + } + const Clock::time_point t0 = Clock::now(); + pool.run_split_multi_native(lay.fmt[(size_t) l], jobs, kExperts); + const Clock::time_point t1 = Clock::now(); + if (r >= 0) ms += std::chrono::duration(t1 - t0).count(); + while (std::chrono::duration(Clock::now() - t1).count() < 150.0) {} + } + if (host >= 0) cpu::restore_thread_affinity(prev); + return ms / kRounds; + }; + double all_ms[kBlocks], p_ms[kBlocks]; + for (int b = 0; b < kBlocks; ++b) { + all_ms[b] = block(cpu::PoolAffinity::All, 0); + p_ms[b] = block(cpu::PoolAffinity::PCores, 1); + } + std::sort(all_ms, all_ms + kBlocks); + std::sort(p_ms, p_ms + kBlocks); + const double a = all_ms[kBlocks / 2], p = p_ms[kBlocks / 2]; + const bool take_p = p <= a * 1.10; + std::fprintf(stderr, "strata generate: --pool-affinity measure: %d experts x %d tokens per layer over %lld layers: " + "all (%d workers) %.2f ms, p-cores (%d workers) %.2f ms (medians of %d) -> %s\n", + kExperts, kTok, (long long) nl, n_workers[0], a, n_workers[1], p, kBlocks, take_p ? "p-cores" : "all"); + return take_p ? cpu::PoolAffinity::PCores : cpu::PoolAffinity::All; +} + // 0.1.42, CUDA single-GPU serve, no --pcie-frac: the share refined from what the CPU pool measures in the first decode // windows. The balance share of the missed experts is link / (link + CPU rate). The link is the start-up probe; the CPU // rate is the expert bytes over the pool's wall time per missed expert (ExpertDispatch::ms_run / multi_misses), which the @@ -2009,8 +2111,9 @@ int main(int argc, char** argv) { if (v == "auto") o.pool_affinity = strata::kernels::cpu::PoolAffinity::Auto; else if (v == "p-cores" || v == "pcores") o.pool_affinity = strata::kernels::cpu::PoolAffinity::PCores; else if (v == "all") o.pool_affinity = strata::kernels::cpu::PoolAffinity::All; + else if (v == "measure") o.pool_measure = true; else { - std::fprintf(stderr, "strata generate: unknown --pool-affinity value '%s' (expected auto, p-cores, or all)\n", v.c_str()); + std::fprintf(stderr, "strata generate: unknown --pool-affinity value '%s' (expected auto, p-cores, all or measure)\n", v.c_str()); return 2; } } @@ -5050,6 +5153,8 @@ int main(int argc, char** argv) { } } #endif + if (o.pool_measure) + o.pool_affinity = measure_pool_affinity(srcp, o.pool_workers, !o.no_host_worker, o.pool_tasks); strata::kernels::cpu::ExpertPool pool(o.pool_workers, /*pin=*/true, /*host_works=*/!o.no_host_worker, o.pool_affinity, o.pool_tasks); std::fprintf(stderr, "strata generate: CPU pool tasks/phase: %d%s, participating threads: %d\n", From 051b970303fe28d454fdc2a8a39296f479986119 Mon Sep 17 00:00:00 2001 From: Francesco Albano <119073350+Hardin22@users.noreply.github.com> Date: Tue, 6 Oct 2026 15:15:48 +0200 Subject: [PATCH 7/9] cpu pool: --host-core sibling, the host on the first core's other hardware thread (hybrid CPUs too) Windows sends a GPU's interrupts to one logical processor, usually the first, and the host thread spins there (--host-core first, the default). --host-core last moves it to the last physical core, but not on a hybrid CPU, whose first core is its best P-core, so a 12th-14th gen Intel or a Core Ultra keeps the host on logical processor 0 with the interrupts. --host-core sibling (or STRATA_HOST_CORE=sibling) keeps the host on that same first core but on its other hardware thread. The interrupts keep the first logical processor, which nothing else is pinned to, and the workers keep every other core, since they take one logical processor per core (with --pool-affinity auto / p-cores, which also use the P-cores' siblings, the host's sibling is taken out of their list). Without an SMT sibling it is --host-core first. It works the same on Linux (the sibling from sysfs), where the interrupts are usually spread anyway. detect_cpu_topology records the host core's sibling (CpuTopology::host_sibling) in each layout; pool_affinity_test checks, in every affinity mode, that sibling puts the host there with no worker on its core, and that it is first without SMT. The startup log names the placement. --- docs/DETAILS.md | 6 +++++- include/strata/kernels/cpu/pool.hpp | 16 ++++++++++------ src/core/session.cpp | 3 ++- src/kernels/cpu/pool.cpp | 24 ++++++++++++++++++++++++ src/kernels/cpu/pool_affinity_test.cpp | 25 +++++++++++++++++++++++++ src/program/generate.cpp | 16 +++++++++++----- 6 files changed, 77 insertions(+), 13 deletions(-) diff --git a/docs/DETAILS.md b/docs/DETAILS.md index 6b796703..20d3c5e5 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -290,7 +290,11 @@ Windows 11, Swift 1.5 IQ3_XXS at 128K):** and with `--batch`, `--vram-elastic` or `--peer-device`, the engine says so and stays off. - `--host-core last` (or `STRATA_HOST_CORE=last`, Windows): the host thread runs on the last physical core and the workers take the first. Windows sends a GPU's interrupts to the first core, where a host spinning on the GPU's flags - waits for them (`--host-core first` is the default; the startup log names the cores). + waits for them (`--host-core first` is the default; the startup log names the cores). It leaves a hybrid CPU as it + is. `--host-core sibling` (or `STRATA_HOST_CORE=sibling`) keeps the host on the first core but on its other + hardware thread (SMT), so the interrupts keep the first logical processor and the workers keep every core; it works + on hybrid CPUs too, and is `first` on a core without SMT. On a 4060 Ti + 5080 layer split with an i9-14900KF it + turned a decode that swung by ±6% from run to run into a steady one, 8% faster on average. - `STRATA_ADAPT_LAG=2` (#764): a window takes the adaptive tier's swaps once they are two windows old (default 1, as 0.1.39). `STRATA_PREFILL_EQUAL=1` (#693): a prompt segment is read in chunks of equal size, not full chunks and a short last one (changes the rounding). `STRATA_OWNED_PRICE=exact` (#796): the cache sizing prices the prompt path's diff --git a/include/strata/kernels/cpu/pool.hpp b/include/strata/kernels/cpu/pool.hpp index 6796e391..686abd6e 100644 --- a/include/strata/kernels/cpu/pool.hpp +++ b/include/strata/kernels/cpu/pool.hpp @@ -78,15 +78,19 @@ struct CpuTopology { int e_cores = 0; ///< Efficient cores std::vector worker_cores; ///< Ordered CPU IDs; on Windows, group * 64 + processor within the group int host_core = -1; ///< Logical core reserved for host thread + int host_sibling = -1; ///< The host core's SMT sibling (-1: none), the logical processor `Sibling` takes }; /// Which core the host thread - the layer loop that spins on the GPU's flags - takes when `skip_first` reserves one -/// (--host-core first|last, STRATA_HOST_CORE). `First` is the layout the pool has always used. `Last` (eddoursul's -/// fork, F12) puts the host on the last physical core and lets the workers have the first: Windows sends a GPU's -/// interrupts to one logical processor, usually the first, and every copy that lands raises one (~13 us in the ISR -/// and a DPC), which delays a spinning host there. The placement changes no result, only where threads run. A hybrid -/// CPU keeps `First` (its first core is the best P-core). -enum class HostCore { First, Last }; +/// (--host-core first|last|sibling, STRATA_HOST_CORE). `First` is the layout the pool has always used. `Last` +/// (eddoursul's fork, F12) puts the host on the last physical core and lets the workers have the first: Windows sends +/// a GPU's interrupts to one logical processor, usually the first, and every copy that lands raises one (~13 us in the +/// ISR and a DPC), which delays a spinning host there. The placement changes no result, only where threads run. A +/// hybrid CPU keeps `First` under `Last` (its first core is the best P-core). `Sibling` keeps the host on that first +/// core but on its other hardware thread: the interrupts stay on the first logical processor, no worker shares the +/// core (the workers take one logical processor per core), and it applies to hybrid CPUs too. Without an SMT sibling +/// it is `First`. +enum class HostCore { First, Last, Sibling }; void set_host_core(HostCore where); HostCore host_core_setting(); diff --git a/src/core/session.cpp b/src/core/session.cpp index 157c4cc4..0629b327 100644 --- a/src/core/session.cpp +++ b/src/core/session.cpp @@ -557,7 +557,8 @@ bool SessionLoopScratch::init(size_t parts_bytes_in, std::string& err) { // core or its SMT sibling. The symptom is not an error: it is a CPU path at 26.9 GB/s where the same pool // runs at 36.32. It was being done and undone on EVERY token, which is a syscall pair on the critical path // for a property that wants to hold for the whole session. - // The host's core is the pool's reserved one: the first physical core, or the last with --host-core last (F12). + // The host's core is the pool's reserved one: the first physical core, or the last with --host-core last (F12), + // or the first core's SMT sibling with --host-core sibling. const int host_core = strata::kernels::cpu::planned_host_core(); if (host_core >= 0) { pinned_core = strata::kernels::cpu::pin_current_thread(host_core); diff --git a/src/kernels/cpu/pool.cpp b/src/kernels/cpu/pool.cpp index 3bf8c93f..9641ba6a 100644 --- a/src/kernels/cpu/pool.cpp +++ b/src/kernels/cpu/pool.cpp @@ -89,6 +89,13 @@ CpuTopology detect_cpu_topology(bool skip_first, PoolAffinity affinity) { topo.worker_cores.insert(topo.worker_cores.begin(), topo.host_core); topo.host_core = last; } + // --host-core sibling: the host on its core's other hardware thread; the first logical processor stays to the + // interrupts and to nothing else (a worker on it would share the host's core) + if (skip_first && host_core_setting() == HostCore::Sibling && topo.host_sibling >= 0) { + topo.worker_cores.erase(std::remove(topo.worker_cores.begin(), topo.worker_cores.end(), topo.host_sibling), + topo.worker_cores.end()); + topo.host_core = topo.host_sibling; + } return topo; } @@ -180,6 +187,7 @@ static CpuTopology detect_cpu_topology_impl(bool skip_first, PoolAffinity affini if (skip_first && !topo.worker_cores.empty()) { topo.host_core = topo.worker_cores.front(); topo.worker_cores.erase(topo.worker_cores.begin()); + if (descs.front().lps.size() > 1) topo.host_sibling = descs.front().lps[1]; } return topo; } @@ -207,6 +215,11 @@ static CpuTopology detect_cpu_topology_impl(bool skip_first, PoolAffinity affini if (skip_first && !p_primaries.empty()) { topo.host_core = p_primaries.front(); p_primaries.erase(p_primaries.begin()); + for (const auto& c : descs) + if (c.lps[0] == topo.host_core) { + if (c.lps.size() > 1) topo.host_sibling = c.lps[1]; + break; + } } for (int cpu : p_primaries) topo.worker_cores.push_back(cpu); @@ -332,6 +345,15 @@ static CpuTopology detect_cpu_topology_impl(bool skip_first, PoolAffinity affini topo.p_threads = (int) all_cpus.size(); } + // the host core's SMT sibling, an allowed CPU of the same (package, core) (--host-core sibling) + auto sibling_of = [&](int cpu) { + for (const auto& h : all_cpus) + if (h.cpu == cpu && h.pkg >= 0 && h.core >= 0) + for (const auto& cl : all_cpus) + if (cl.cpu != cpu && cl.pkg == h.pkg && cl.core == h.core) return cl.cpu; + return -1; + }; + if (affinity == PoolAffinity::All || !topo.is_hybrid) { if (topo.is_hybrid) // #642: the P-cores first (see the Windows branch) std::stable_sort(all_cpus.begin(), all_cpus.end(), @@ -344,6 +366,7 @@ static CpuTopology detect_cpu_topology_impl(bool skip_first, PoolAffinity affini if (skip_first && !topo.worker_cores.empty()) { topo.host_core = topo.worker_cores.front(); topo.worker_cores.erase(topo.worker_cores.begin()); + topo.host_sibling = sibling_of(topo.host_core); } return topo; } @@ -365,6 +388,7 @@ static CpuTopology detect_cpu_topology_impl(bool skip_first, PoolAffinity affini if (skip_first && !p_primaries.empty()) { topo.host_core = p_primaries.front(); p_primaries.erase(p_primaries.begin()); + topo.host_sibling = sibling_of(topo.host_core); } for (int cpu : p_primaries) topo.worker_cores.push_back(cpu); diff --git a/src/kernels/cpu/pool_affinity_test.cpp b/src/kernels/cpu/pool_affinity_test.cpp index 052ae59b..6a947a49 100644 --- a/src/kernels/cpu/pool_affinity_test.cpp +++ b/src/kernels/cpu/pool_affinity_test.cpp @@ -106,7 +106,32 @@ static bool inherited_default(int core, ULONG id, int temporary_core, ULONG temp return check(ok && restored, "restore inherited process default"); } +// --host-core sibling: the host on the first layout's host core's SMT sibling, which no worker takes, and the first +// logical processor left to nothing; `first` on a core without SMT. Every affinity mode. +static bool host_sibling_layout() { + bool ok = true; + for (const auto affinity : {cpu::PoolAffinity::All, cpu::PoolAffinity::Auto, cpu::PoolAffinity::PCores}) { + cpu::set_host_core(cpu::HostCore::First); + const cpu::CpuTopology first = cpu::detect_cpu_topology(true, affinity); + cpu::set_host_core(cpu::HostCore::Sibling); + const cpu::CpuTopology sib = cpu::detect_cpu_topology(true, affinity); + cpu::set_host_core(cpu::HostCore::First); + const auto has = [&](int core) { + return std::find(sib.worker_cores.begin(), sib.worker_cores.end(), core) != sib.worker_cores.end(); + }; + if (first.host_sibling < 0) + ok = check(sib.host_core == first.host_core && sib.worker_cores == first.worker_cores, + "--host-core sibling without SMT must be first") && ok; + else + ok = check(sib.host_core == first.host_sibling && sib.host_core != first.host_core && !has(sib.host_core) && + !has(first.host_core), + "--host-core sibling: the host on the sibling, no worker on its core") && ok; + } + return ok; +} + int main() { + if (!host_sibling_layout()) return 1; const auto all = cpu::physical_cores(false); std::vector cores; std::vector ids; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 2b7a65d4..1d02f0ad 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -441,7 +441,7 @@ struct Options { /// --pool-affinity measure (opt-in, hybrid CPUs, native packs): `all` and `p-cores` are timed on this pack's /// own experts at start and the faster layout is taken (measure_pool_affinity) bool pool_measure = false; - /// --host-core first|last (STRATA_HOST_CORE): the host thread's core (see HostCore in pool.hpp) + /// --host-core first|last|sibling (STRATA_HOST_CORE): the host thread's core (see HostCore in pool.hpp) std::string host_core; /// --aux-cpus off|auto|LIST (STRATA_AUX_CPUS): where the non-pool, non-host threads run (strata/platform/aux_cpus.hpp) std::string aux_cpus; @@ -915,7 +915,9 @@ void usage() { " last (the last physical core, the workers on the others). Windows sends a GPU's\n" " interrupts to one logical processor, usually the first, and every copy that lands\n" " raises one: a spinning host there waits for them. Moves threads only, never a\n" - " result. Not on hybrid CPUs (STRATA_HOST_CORE sets it too).\n" + " result. Not on hybrid CPUs. sibling: the first core's other hardware thread\n" + " (SMT), so the interrupts keep the first logical processor and the workers keep\n" + " every core; hybrid CPUs too, first without SMT (STRATA_HOST_CORE sets it too).\n" " --aux-cpus SET Where the threads that are neither pool workers nor the host thread run (the\n" " adaptive tier's job thread, the prefill helpers, the CUDA driver's threads, ...):\n" " off (the default: wherever they were created), auto (the SMT siblings of the host's\n" @@ -2092,8 +2094,9 @@ int main(int argc, char** argv) { } else if (a == "--host-core") { o.host_core = next("--host-core"); - if (o.host_core != "first" && o.host_core != "last") { - std::fprintf(stderr, "strata generate: unknown --host-core value '%s' (expected first or last)\n", o.host_core.c_str()); + if (o.host_core != "first" && o.host_core != "last" && o.host_core != "sibling") { + std::fprintf(stderr, "strata generate: unknown --host-core value '%s' (expected first, last or sibling)\n", + o.host_core.c_str()); return 2; } } @@ -2398,6 +2401,7 @@ int main(int argc, char** argv) { if (hc.empty()) if (const char* e = std::getenv("STRATA_HOST_CORE")) hc = e; if (hc == "last") strata::kernels::cpu::set_host_core(strata::kernels::cpu::HostCore::Last); + if (hc == "sibling") strata::kernels::cpu::set_host_core(strata::kernels::cpu::HostCore::Sibling); } { // --aux-cpus / STRATA_AUX_CPUS: read the CPUs this process may use now, before any thread is pinned; the plan // follows once the pool exists (it needs the workers' CPUs) @@ -5175,7 +5179,9 @@ int main(int argc, char** argv) { std::fprintf(stderr, "strata generate: %d pool workers on logical processors %s, the host thread on %d%s " "(--host-core %s)\n", pool.workers(), on.c_str(), ht.host_core, pool.host_works() ? " (draining too)" : "", - strata::kernels::cpu::host_core_setting() == strata::kernels::cpu::HostCore::Last ? "last" : "first"); + strata::kernels::cpu::host_core_setting() == strata::kernels::cpu::HostCore::Last ? "last" + : strata::kernels::cpu::host_core_setting() == strata::kernels::cpu::HostCore::Sibling ? "sibling" + : "first"); // --aux-cpus: the plan, now the workers' CPUs are known; the threads that exist already (the CUDA driver's) move // now when it is on, the ones created later pin themselves (strata/platform/aux_cpus.hpp) const size_t nw = std::min((size_t) pool.workers(), ht.worker_cores.size()); From b558df67b40ebf98828ccefdfd4b823047e27f3d Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 14 Oct 2026 07:20:05 +0200 Subject: [PATCH 8/9] cpu: the exact singleton rows only where ggml-cpu is itself the AVX2 build (not AVX-512, not an ISA-floor build) STRATA_CPU_IQ_ALL_EXACT1 reproduces ggml-cpu's AVX2 single-token dot bit for bit, which only holds if the ggml-cpu in this binary is that dot: a STRATA_ISA_FLOOR build runs ggml's AVX1/SSE dots, and a native build on an AVX-512 CPU run with STRATA_FORCE_AVX2=1 runs its AVX-512 one. The guard is #2215's (ggml_cpu_has_avx2 / ggml_cpu_has_avx512). Co-authored-by: Andy Slater --- src/kernels/cpu/native_expert.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/kernels/cpu/native_expert.cpp b/src/kernels/cpu/native_expert.cpp index 50153002..c6cab9fd 100644 --- a/src/kernels/cpu/native_expert.cpp +++ b/src/kernels/cpu/native_expert.cpp @@ -165,9 +165,12 @@ 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; + // The exact rows are ggml-cpu's AVX2 single-token dot bit for bit, so they need ggml-cpu itself built for AVX2 and + // not AVX-512 (ggml_cpu_has_*): a STRATA_ISA_FLOOR build runs ggml's AVX1/SSE dots, a native build on an AVX-512 CPU + // run with STRATA_FORCE_AVX2=1 runs its AVX-512 one, and both round differently (#2215's guard). static const bool exact_one = [] { const char* v = std::getenv("STRATA_CPU_IQ_ALL_EXACT1"); - return v && v[0] == '1' && v[1] == '\0'; + return v && v[0] == '1' && v[1] == 0 && ggml_cpu_has_avx2() && !ggml_cpu_has_avx512(); }(); // only where ggml's dot runs today (below the #152 multi-token rule): with STRATA_IQ_MT_MIN=1 a lone token keeps the // multi-token kernel, so a token's rows still do not depend on how many tokens share its expert From 81ead832d60e1fb5b88460789b7141b9e090f309 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 14 Oct 2026 10:51:18 +0200 Subject: [PATCH 9/9] docs: STRATA_CPU_IQ_ALL_EXACT1 (the exact one-token expert rows of #1415 and #1544), what was measured --- docs/DETAILS.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/DETAILS.md b/docs/DETAILS.md index 20d3c5e5..c4b1b4d5 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -283,6 +283,20 @@ Windows 11, Swift 1.5 IQ3_XXS at 128K):** It does not look again during a session, so where the E-cores' speed changes while the engine runs, `--pool-affinity p-cores` is the steady choice. Not measured: other CPUs, Linux, AMD cards. +**Exact one-token expert rows (0.1.45, opt-in, `STRATA_CPU_IQ_ALL_EXACT1=1`, #1415 #1544):** below the #152 rule +(a lone token in an expert's group) an AVX-2 CPU without AVX-512 runs ggml-cpu's `vec_dot` for the i-quant gate/up rows. +With the switch, IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS and IQ3_S take Strata's AVX-2 decode instead, with ggml's float order +(the same per-lane integer sums, one fmadd per block, the format constant after `hsum_float_8`), so the output does not +change, only the time (`iq_avx2_parity`: 0 of 5,120 outputs per format differ from ggml's; the identity gate with the +switch on is 10/10 plus exact_pp 4K/20K identical on RTX 3060, 2x A4000 and R9700 boxes). It is used only where +ggml-cpu itself is the AVX2 build (not an ISA-floor build, not AVX-512) and not with `STRATA_IQ_MT_MIN=1`. Per expert, +gate/up, one thread: IQ3_S -32% on a Threadripper PRO 3975WX (Zen 2) and -11% on a Ryzen 5 5600X (Zen 3), IQ2_XXS -5..-6%, +IQ3_XXS -5% (Zen 2) / +1% (Zen 3), IQ2_S 0% (Zen 2) / +7% (Zen 3, slower); on a Core Ultra 7 265 (VNNI, gather) IQ3_S -60%, +IQ3_XXS -39%, IQ2_XXS -34%, IQ2_XS -24%, IQ2_S -3% on a P-core. End to end, with the engine's decode timing: the CPU part of a +window falls 5% on a Xeon E5-2643 v3 + RTX A4000 (22.2 -> 21.1 ms of a 46.9 ms window, decode +0.5% over 10 interleaved pairs, ++2.5% on the story and code prompts) and on a Core Ultra 7 265 + RTX 3060 (8.2 -> 7.8 ms of 39.5 ms, decode +0.4%), and does not +move on a Threadripper PRO 3975WX + R9700 (14.5 ms, +0.4%, 5 pairs); no case reached +3% end to end, so the switch stays off. + **Switches added in 0.1.40 (all off unless noted; none changes the default output):** - `--kv-grow` (or `STRATA_KV_GROW=1`; `--no-kv-grow` turns it off): the K/V takes VRAM only for the cells the requests reach, and the expert cache holds the rest, giving slots back as the context grows. It needs one GPU, an expert