From 77ddc2ff147dd16ecaa0228b28419e97fbc89e07 Mon Sep 17 00:00:00 2001 From: Stuart Chapin Date: Thu, 8 Oct 2026 04:06:23 -0700 Subject: [PATCH] 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);