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 kernels: AVX-VNNI forms of the AVX-2 i-quant, IQ4_NL and Q2_0 rows (cpu_avxvnni_ok)
Francesco Albanob211ba173a32
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 1fab8796d..3a770e656 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -1002,6 +1002,34 @@ else()PROPERTIES COMPILE_OPTIONS "-mavx512f;-mavx512bw;-mavx512vl;-mavx512dq;-mavx512vnni;-mavx512vbmi;-mf16c;-mfma")set_source_files_properties(src/kernels/cpu/q2_avx2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mf16c")endif()+# AVX-VNNI (Alder Lake, Sapphire Rapids and later): the AVX-2 expert kernels' vpdpwssd / vpdpbusd copies (q2_avx2.cpp,+# iq_avx2.cpp; *_rows.inl), taken at run time where cpu_avxvnni_ok(). GCC and Clang compile only those functions with+# target("avxvnni") - never -mavxvnni on a file, which would let the compiler use it anywhere - and MSVC takes the+# intrinsics under /arch:AVX2. A compiler without them (GCC < 11, Clang < 12) builds the files without the copies.+include(CheckCXXSourceCompiles)+if(MSVC)+ set(CMAKE_REQUIRED_FLAGS "/arch:AVX2")+else()+ set(CMAKE_REQUIRED_FLAGS "-mavx2 -mfma -mf16c")+endif()+check_cxx_source_compiles([=[+#include <immintrin.h>+#if defined(__GNUC__) || defined(__clang__)+__attribute__((target("avxvnni")))+#endif+static __m256i dots(__m256i a, __m256i b) { return _mm256_dpbusd_avx_epi32(_mm256_dpwssd_avx_epi32(a, a, b), a, b); }+int main() {+ volatile int x = 1;+ return _mm256_extract_epi32(dots(_mm256_set1_epi32(x), _mm256_set1_epi32(x)), 0) == 0;+}+]=] STRATA_HAVE_AVXVNNI_INTRINSICS)+unset(CMAKE_REQUIRED_FLAGS)+if(STRATA_HAVE_AVXVNNI_INTRINSICS)+ set(_strata_avxvnni STRATA_AVXVNNI=1)+else()+ set(_strata_avxvnni STRATA_AVXVNNI=0)+endif()+set_property(SOURCE src/kernels/cpu/q2_avx2.cpp APPEND PROPERTY COMPILE_DEFINITIONS ${_strata_avxvnni})find_package(Threads REQUIRED)target_link_libraries(strata_kernels_cpu PUBLIC Threads::Threads)@@ -1084,6 +1112,7 @@ if(STRATA_NATIVE_EXPERTS)set_source_files_properties(src/kernels/cpu/kq_avx2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mf16c")set_source_files_properties(src/kernels/cpu/kq_avx1.cpp PROPERTIES COMPILE_OPTIONS "-mavx")endif()+ set_property(SOURCE src/kernels/cpu/iq_avx2.cpp APPEND PROPERTY COMPILE_DEFINITIONS ${_strata_avxvnni})if(STRATA_ISA_FLOOR STREQUAL "avx")target_compile_definitions(strata_kernels_cpu PRIVATE STRATA_ISA_FLOOR_AVX=1)elseif(STRATA_ISA_FLOOR STREQUAL "none")diff --git a/include/strata/kernels/cpu/expert.hpp b/include/strata/kernels/cpu/expert.hppindex 0e92740ad..8510598b3 100644--- a/include/strata/kernels/cpu/expert.hpp+++ b/include/strata/kernels/cpu/expert.hpp@@ -185,6 +185,9 @@ 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);+/// The same rows with AVX-VNNI on or off, not as cpu_avxvnni_ok() says (tests and benches; on only where it holds).+void q2_0_gguf_rows_multi_avx2_v(bool vnni, 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);void s2_expert_scalar(const uint8_t* blob, const float* x, float* out, bool quant_acts);diff --git a/include/strata/kernels/cpu/expert_layout.hpp b/include/strata/kernels/cpu/expert_layout.hppindex ed1754f39..615afa195 100644--- a/include/strata/kernels/cpu/expert_layout.hpp+++ b/include/strata/kernels/cpu/expert_layout.hpp@@ -70,6 +70,11 @@ bool cpu_gather_fast();/// cpu_gather_fast() and the CALLING THREAD runs on a performance core: CPUID 1Ah core type 40h on a hybrid CPU (its/// E-cores, 20h, gather slower than they assemble). Probed once per thread: the pool pins each worker to one core.bool cpu_gather_fast_here();+/// Whether the AVX-2 expert kernels take their AVX-VNNI forms (vpdpwssd / vpdpbusd: Alder Lake, Sapphire Rapids and+/// later, P- and E-cores alike; the same integer sums): cpu_avx2_ok() and CPUID 7.1:EAX[4], not capped by+/// STRATA_FORCE_ISA, and not an older-CPU build (STRATA_ISA_FLOOR, which stays on its floor and AVX2).+/// STRATA_NO_AVXVNNI=1 answers no.+bool cpu_avxvnni_ok();/// Whether a native pack's layer of this ggml type runs on Strata's own Q2_0 kernels (AVX2 / AVX-512). On a/// CPU without AVX2 (a native, non-portable build: ggml-cpu compiled for this CPU's SSE) a Q2_0 layer takes/// ggml-cpu's own Q2_0 vec_dot like every other native type: slower, but it runs.diff --git a/include/strata/kernels/cpu/iq_avx2.hpp b/include/strata/kernels/cpu/iq_avx2.hppindex d0729fecc..0a8876bbb 100644--- a/include/strata/kernels/cpu/iq_avx2.hpp+++ b/include/strata/kernels/cpu/iq_avx2.hpp@@ -20,7 +20,9 @@ void iq256_rows(int ggml_type, const uint8_t* w, size_t row_bytes, int n, const/// The kernels' variants, a mask. Every variant computes the same bits; they differ in speed from core to core./// kIq256Gather: IQ3_XXS, IQ3_S and IQ2_S read their grid entries with one gather (STRATA_IQ256_GATHER).+/// kIq256Vnni: AVX-VNNI's vpdpwssd / vpdpbusd, every format and iq4nl256_down_rows (cpu_avxvnni_ok).inline constexpr int kIq256Gather = 1;+inline constexpr int kIq256Vnni = 2;/// The variant iq256_gu_rows / iq256_rows take on the calling thread.int iq256_variant() noexcept;/// Every variant bit this build and this CPU can run (tests and benches).@@ -35,5 +37,8 @@ void iq256_rows_v(int variant, int ggml_type, const uint8_t* w, size_t row_bytes/// IQ4_NL is a 32-value-block format, so this does not go through iq256_rows (QK_K blocks, Q8_K acts).void iq4nl256_down_rows(const uint8_t* w, size_t row_bytes, int n, const void* const* hq, int nt,float* const* out, int r0, int r1);+/// The same in a given variant (only the kIq256Vnni bit matters here).+void iq4nl256_down_rows_v(int variant, const uint8_t* w, size_t row_bytes, int n, const void* const* hq, int nt,+ float* const* out, int r0, int r1);} // namespace strata::kernels::cpudiff --git a/src/kernels/cpu/expert_layout.cpp b/src/kernels/cpu/expert_layout.cppindex 92cae129c..0772d9889 100644--- a/src/kernels/cpu/expert_layout.cpp+++ b/src/kernels/cpu/expert_layout.cpp@@ -248,6 +248,21 @@ bool cpu_gather_fast_here() {return here == 1;}+bool cpu_avxvnni_ok() {+ static const bool ok = [] {+ if (const char* v = std::getenv("STRATA_NO_AVXVNNI"); v != nullptr && std::atoi(v) != 0) return false;+ if (isa_floor_build()[0] != '\0' || cpu_isa_cap() < 3 || !cpu_avx2_ok()) return false;+ unsigned r[4];+ cpuid_regs(0, 0, r);+ if (r[0] < 7) return false;+ cpuid_regs(7, 0, r);+ if (r[0] < 1) return false; // no sub-leaf 1+ cpuid_regs(7, 1, r);+ return ((r[0] >> 4) & 1u) != 0; // AVX-VNNI; VEX-encoded, so the OS state is AVX's (cpu_avx2_ok)+ }();+ return ok;+}+std::string cpu_name() {unsigned r[12] = {};#if defined(_MSC_VER)diff --git a/src/kernels/cpu/iq_avx2.cpp b/src/kernels/cpu/iq_avx2.cppindex b7b913401..333e0c458 100644--- a/src/kernels/cpu/iq_avx2.cpp+++ b/src/kernels/cpu/iq_avx2.cpp@@ -15,6 +15,9 @@// register and read with one vpgather, the same words into the same lanes, so the same bits. Where it is faster// depends on the core, so STRATA_IQ256_GATHER picks it per thread (expert_layout.hpp, cpu_gather_fast_here).//+// Where the CPU has AVX-VNNI (cpu_avxvnni_ok: Alder Lake, Sapphire Rapids and later) the row kernels take a second+// copy with vpdpwssd / vpdpbusd (iq_avx2_rows.inl), the same integer sums.+//// This file is compiled for AVX2 and must not run anything before the engine's cpu_avx2_ok() check: no runtime// initializer at namespace scope (#391, 016ea2e), the switches are read on first use.#include "strata/kernels/cpu/iq_avx2.hpp"@@ -30,6 +33,17 @@#include <cstdlib>#include <cstring>+// STRATA_AVXVNNI (CMake: the compiler has the AVX-VNNI intrinsics): GCC and Clang compile them only inside functions+// with target("avxvnni") - the VNNI copy of iq_avx2_rows.inl.+#if !defined(STRATA_AVXVNNI)+#define STRATA_AVXVNNI 0+#endif+#if STRATA_AVXVNNI && (defined(__GNUC__) || defined(__clang__))+#define STRATA_AVXVNNI_FN __attribute__((target("avxvnni")))+#else+#define STRATA_AVXVNNI_FN+#endif+namespace strata::kernels::cpu {namespace {@@ -292,213 +306,21 @@ template <> struct Fmt32<23> { // IQ4_XS: d, scales_h, scales_l[4], qs[128] -}};-template <int TY, int NT>-inline void row_dot(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res) {- const int pf = prefetch_distance();- __m256 accf[NT];- for (int t = 0; t < NT; ++t) accf[t] = _mm256_setzero_ps();- for (int i = 0; i < nblocks; ++i) {- const uint8_t* blk = row + (size_t) i * Fmt32<TY>::bytes;- rows_ahead(blk, pf);- __m256i acci[NT];- for (int t = 0; t < NT; ++t) acci[t] = _mm256_setzero_si256();- for (int j = 0; j < 4; ++j) {- for (int half = 0; half < 2; ++half) {- __m256i g, sgn, sc;- Fmt32<TY>::decode(blk, j, half, g, sgn, sc);- const int off = 64 * j + 32 * half;- for (int t = 0; t < NT; ++t) {- const __m256i yv = _mm256_loadu_si256((const __m256i*) (y[t][i].qs + off));- const __m256i ys = _mm256_sign_epi8(yv, sgn);- acci[t] = _mm256_add_epi32(acci[t], _mm256_madd_epi16(_mm256_maddubs_epi16(g, ys), sc));- }- }- }- const float dx = h2f(u16(blk)) * Fmt32<TY>::K;- for (int t = 0; t < NT; ++t)- accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(dx * y[t][i].d), _mm256_cvtepi32_ps(acci[t]), accf[t]);- }- for (int t = 0; t < NT; ++t) res[t] = hsum8(accf[t]);-}--// ---- IQ2_XS (17) with ggml's vectorized sign decode (arch/x86/quants.c, ggml_vec_dot_iq2_xs_q8_K):-// the 7-bit sign indices never touch ksigns_iq2xs - the 8th sign bit is reconstructed by parity (two-// shifts, a xor and ggml's bit_helper pshufb) and the per-half sign vectors come out of shuffles of one-// 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>-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,- 1, 2, 4, 8, 16, 32, 64, (uint8_t) 0x80, 1, 2, 4, 8, 16, 32, 64, (uint8_t) 0x80 };- static const char shuf_even[32] = { // sign bits of u16 0,1,2,3 (even bytes of the sign register)- 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2,- 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6 };- static const char shuf_odd[32] = { // sign bits of u16 4,5,6,7- 8, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 10, 10, 10,- 12, 12, 12, 12, 12, 12, 12, 12, 14, 14, 14, 14, 14, 14, 14, 14 };- static const uint8_t bit_help[32] = { // 0x80 when the 4-bit index has odd popcount (parity bit 7)- 0x00, (uint8_t) 0x80, (uint8_t) 0x80, 0x00, (uint8_t) 0x80, 0x00, 0x00, (uint8_t) 0x80,- (uint8_t) 0x80, 0x00, 0x00, (uint8_t) 0x80, 0x00, (uint8_t) 0x80, (uint8_t) 0x80, 0x00,- 0x00, (uint8_t) 0x80, (uint8_t) 0x80, 0x00, (uint8_t) 0x80, 0x00, 0x00, (uint8_t) 0x80,- (uint8_t) 0x80, 0x00, 0x00, (uint8_t) 0x80, 0x00, (uint8_t) 0x80, (uint8_t) 0x80, 0x00 };- static const uint8_t k_sc_shuffle[128] = { // half h -> scale bytes 2h (lanes 0-7), 2h+1 (lanes 8-15)- 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,- 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,- 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,- 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,- 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,- 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11,- 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13,- 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15 };- const __m256i bsel = _mm256_loadu_si256((const __m256i*) bit_sel);- const __m256i shev = _mm256_loadu_si256((const __m256i*) shuf_even);- const __m256i shod = _mm256_loadu_si256((const __m256i*) shuf_odd);- const __m256i bhelp = _mm256_loadu_si256((const __m256i*) bit_help);- const __m256i m511 = _mm256_set1_epi16(511);- const __m128i m4 = _mm_set1_epi8(0xf), m1 = _mm_set1_epi8(1);- const __m256i one8 = _mm256_set1_epi8(1);- const int pf = prefetch_distance();-- __m256 accf[NT];- for (int t = 0; t < NT; ++t) accf[t] = _mm256_setzero_ps();- for (int i = 0; i < nblocks; ++i) {- const uint8_t* blk = row + (size_t) i * 74;- rows_ahead(blk, pf);- // the 8 scale bytes -> 16 half-scales of 2*s+1, interleaved [a0, b0, a1, b1, ...] (ggml's unpack)- __m128i st = _mm_set1_epi64x((long long) u64(blk + 66));- st = _mm_unpacklo_epi8(_mm_and_si128(st, m4), _mm_and_si128(_mm_srli_epi16(st, 4), m4));- const __m128i scales = _mm_add_epi8(_mm_slli_epi16(st, 1), m1);- __m256i acc[NT][2];- for (int t = 0; t < NT; ++t) acc[t][0] = acc[t][1] = _mm256_setzero_si256();- for (int jj = 0; jj < 2; ++jj) { // 128 values (16 u16) per step- const __m256i q2 = _mm256_loadu_si256((const __m256i*) (blk + 2 + 32 * jj));- const __m256i p7 = _mm256_srli_epi16(q2, 9); // 7 sign bits per u16, even bytes- const __m256i p3 = _mm256_srli_epi16(q2, 13); // sign index bits 4-6- const __m256i fsb = _mm256_or_si256(p7,- _mm256_shuffle_epi8(bhelp, _mm256_xor_si256(p7, p3))); // + the parity sign bit 7- const __m256i s01 = _mm256_broadcastsi128_si256(_mm256_castsi256_si128(fsb)); // u16 0-7- const __m256i s23 = _mm256_broadcastsi128_si256(_mm256_extracti128_si256(fsb, 1)); // u16 8-15- alignas(32) uint16_t gi[16];- _mm256_store_si256((__m256i*) gi, _mm256_and_si256(q2, m511));- for (int h = 0; h < 4; ++h) { // the four 32-value halves of this group- const int H = 4 * jj + h; // global half index -> scales byte pair- const __m256i g = _mm256_set_epi64x((long long) iq2xs_grid[gi[4 * h + 3]],- (long long) iq2xs_grid[gi[4 * h + 2]],- (long long) iq2xs_grid[gi[4 * h + 1]],- (long long) iq2xs_grid[gi[4 * h]]);- const __m256i sb = _mm256_shuffle_epi8(h < 2 ? s01 : s23, (h & 1) ? shod : shev);- const __m256i sgn = _mm256_or_si256(- _mm256_cmpeq_epi8(_mm256_and_si256(sb, bsel), bsel), one8);- const __m256i sc = _mm256_cvtepi8_epi16(- _mm_shuffle_epi8(scales, _mm_loadu_si128((const __m128i*) k_sc_shuffle + H)));- const int off = 128 * jj + 32 * h;- for (int t = 0; t < NT; ++t) {- const __m256i yv = _mm256_loadu_si256((const __m256i*) (y[t][i].qs + off));- const __m256i ys = _mm256_sign_epi8(yv, sgn);- acc[t][h & 1] = _mm256_add_epi32(acc[t][h & 1],- _mm256_madd_epi16(_mm256_maddubs_epi16(g, ys), sc));- }- }- }- 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),- _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]);-}--template <int TY, int NT>-inline void row_dot_any(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res) {- if constexpr (TY == 17) row_dot_iq2xs<NT>(row, nblocks, y, res);- else row_dot<TY, NT>(row, nblocks, y, res);-}--template <int TY, int NT>-void gu_rows(const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act, float* const* ff,- int r0, int r1) {- const block_q8_K* y[NT];- for (int t = 0; t < NT; ++t) y[t] = (const block_q8_K*) act[t];- const int nb = n / QK_K;- float g[NT], u[NT];- for (int r = r0; r < r1; ++r) {- row_dot_any<TY, NT>(blob + (size_t) r * gu_row, nb, y, g);- row_dot_any<TY, NT>(blob + up_off + (size_t) r * gu_row, nb, y, u);- for (int t = 0; t < NT; ++t) ff[t][r] = (g[t] / (1.f + std::exp(-g[t]))) * u[t];- }-}--template <int TY, int NT>-void dot_rows(const uint8_t* w, size_t row_bytes, int n, const void* const* act, float* const* out, int r0, int r1) {- const block_q8_K* y[NT];- for (int t = 0; t < NT; ++t) y[t] = (const block_q8_K*) act[t];- float res[NT];- for (int r = r0; r < r1; ++r) {- row_dot_any<TY, NT>(w + (size_t) r * row_bytes, n / QK_K, y, res);- for (int t = 0; t < NT; ++t) out[t][r] = res[t];- }-}--template <int TY>-void gu_rows_nt(int nt, const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act,- float* const* ff, int r0, int r1) {- switch (nt) {- case 1: gu_rows<TY, 1>(blob, gu_row, up_off, n, act, ff, r0, r1); break;- case 2: gu_rows<TY, 2>(blob, gu_row, up_off, n, act, ff, r0, r1); break;- case 3: gu_rows<TY, 3>(blob, gu_row, up_off, n, act, ff, r0, r1); break;- case 4: gu_rows<TY, 4>(blob, gu_row, up_off, n, act, ff, r0, r1); break;- case 5: gu_rows<TY, 5>(blob, gu_row, up_off, n, act, ff, r0, r1); break;- case 6: gu_rows<TY, 6>(blob, gu_row, up_off, n, act, ff, r0, r1); break;- case 7: gu_rows<TY, 7>(blob, gu_row, up_off, n, act, ff, r0, r1); break;- default: gu_rows<TY, 8>(blob, gu_row, up_off, n, act, ff, r0, r1); break;- }-}--template <int TY>-void dot_rows_nt(int nt, const uint8_t* w, size_t row_bytes, int n, const void* const* act, float* const* out, int r0,- int r1) {- switch (nt) {- case 1: dot_rows<TY, 1>(w, row_bytes, n, act, out, r0, r1); break;- case 2: dot_rows<TY, 2>(w, row_bytes, n, act, out, r0, r1); break;- case 3: dot_rows<TY, 3>(w, row_bytes, n, act, out, r0, r1); break;- case 4: dot_rows<TY, 4>(w, row_bytes, n, act, out, r0, r1); break;- default: for (int t0 = 0; t0 < nt; t0 += 4) {- const int k = nt - t0 < 4 ? nt - t0 : 4;- dot_rows_nt<TY>(k, w, row_bytes, n, act + t0, out + t0, r0, r1);- }- }-}--// The decode of each format for a variant: the gathered one (kIq256Gather) for IQ3_XXS, IQ3_S and IQ2_S.-template <bool G>-void gu_type(int type, int nt, const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act,- float* const* ff, int r0, int r1) {- switch (type) {- case 16: gu_rows_nt<16>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;- case 17: gu_rows_nt<17>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;- case 18: gu_rows_nt<G ? 118 : 18>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;- case 21: gu_rows_nt<G ? 121 : 21>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;- case 22: gu_rows_nt<G ? 122 : 22>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;- case 23: gu_rows_nt<23>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;- default: break;- }-}--template <bool G>-void dot_type(int type, int nt, const uint8_t* w, size_t row_bytes, int n, const void* const* act, float* const* out,- int r0, int r1) {- switch (type) {- case 16: dot_rows_nt<16>(nt, w, row_bytes, n, act, out, r0, r1); break;- case 17: dot_rows_nt<17>(nt, w, row_bytes, n, act, out, r0, r1); break;- case 18: dot_rows_nt<G ? 118 : 18>(nt, w, row_bytes, n, act, out, r0, r1); break;- case 21: dot_rows_nt<G ? 121 : 21>(nt, w, row_bytes, n, act, out, r0, r1); break;- case 22: dot_rows_nt<G ? 122 : 22>(nt, w, row_bytes, n, act, out, r0, r1); break;- case 23: dot_rows_nt<23>(nt, w, row_bytes, n, act, out, r0, r1); break;- default: break;- }-}+// ---- the row kernels, twice: the AVX2 forms and the AVX-VNNI ones (iq_avx2_rows.inl)+namespace plain {+#define STRATA_ROWS_VNNI 0+#define STRATA_ROWS_FN+#include "iq_avx2_rows.inl"+} // namespace plain+#if STRATA_AVXVNNI+namespace vnni {+#define STRATA_ROWS_VNNI 1+#define STRATA_ROWS_FN STRATA_AVXVNNI_FN+#include "iq_avx2_rows.inl"+} // namespace vnni+#endif++bool vnni_on() { return STRATA_AVXVNNI && cpu_avxvnni_ok(); }} // namespace@@ -508,23 +330,39 @@ bool iq256_supported(int type) noexcept {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). The same bits either way.+ // hybrid CPU's pool runs on P- and E-cores). AVX-VNNI where cpu_avxvnni_ok(). The same bits either way.const int s = iq256_gather_setting();- return (s >= 0 ? s == 1 : cpu_gather_fast_here()) ? kIq256Gather : 0;+ return ((s >= 0 ? s == 1 : cpu_gather_fast_here()) ? kIq256Gather : 0) | (vnni_on() ? kIq256Vnni : 0);}-int iq256_variants() noexcept { return kIq256Gather; }+int iq256_variants() noexcept { return kIq256Gather | (vnni_on() ? kIq256Vnni : 0); }void iq256_gu_rows_v(int variant, int 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) {- if (variant & kIq256Gather) gu_type<true>(type, nt, blob, gu_row, up_off, n, act, ff, r0, r1);- else gu_type<false>(type, nt, blob, gu_row, up_off, n, act, ff, r0, r1);+ const bool g = (variant & kIq256Gather) != 0;+#if STRATA_AVXVNNI+ if (variant & kIq256Vnni) {+ if (g) vnni::gu_type<true>(type, nt, blob, gu_row, up_off, n, act, ff, r0, r1);+ else vnni::gu_type<false>(type, nt, blob, gu_row, up_off, n, act, ff, r0, r1);+ return;+ }+#endif+ if (g) plain::gu_type<true>(type, nt, blob, gu_row, up_off, n, act, ff, r0, r1);+ else plain::gu_type<false>(type, nt, blob, gu_row, up_off, n, act, ff, r0, r1);}void iq256_rows_v(int variant, int type, const uint8_t* w, size_t row_bytes, int n, const void* const* act, int nt,float* const* out, int r0, int r1) {- if (variant & kIq256Gather) dot_type<true>(type, nt, w, row_bytes, n, act, out, r0, r1);- else dot_type<false>(type, nt, w, row_bytes, n, act, out, r0, r1);+ const bool g = (variant & kIq256Gather) != 0;+#if STRATA_AVXVNNI+ if (variant & kIq256Vnni) {+ if (g) vnni::dot_type<true>(type, nt, w, row_bytes, n, act, out, r0, r1);+ else vnni::dot_type<false>(type, nt, w, row_bytes, n, act, out, r0, r1);+ return;+ }+#endif+ if (g) plain::dot_type<true>(type, nt, w, row_bytes, n, act, out, r0, r1);+ else plain::dot_type<false>(type, nt, w, row_bytes, n, act, out, r0, r1);}void iq256_gu_rows(int type, const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act, int nt,@@ -537,58 +375,23 @@ void iq256_rows(int type, const uint8_t* w, size_t row_bytes, int n, const void*iq256_rows_v(iq256_variant(), type, w, row_bytes, n, act, nt, out, r0, r1);}-// ---- IQ4_NL (type 20): 32-value blocks of 18 bytes (f16 d + 16 nibble bytes) against Q8_0 activations.-// ggml-cpu's own AVX-2 dot (ggml_vec_dot_iq4_nl_q8_0) is single-token: every token redoes the nibble ->-// kvalues_iq4nl pshufb decode and the |w| half of the signed x signed product. Here both are computed once-// per block; every token then costs a sign, a maddubs, a madd and an fmadd. The arithmetic is ggml's - only-// the order of the float additions differs.-template <int NT>-void iq4nl_rows(const uint8_t* w, size_t row_bytes, int n, const block_q8_0* const* y, float* const* out,- int r0, int r1) {- const __m128i values = _mm_loadu_si128((const __m128i*) kvalues_iq4nl);- const __m128i m4b = _mm_set1_epi8(0x0f);- const __m256i ones = _mm256_set1_epi16(1);- const int nb = n / QK4_NL;- const int pf = prefetch_distance();- for (int r = r0; r < r1; ++r) {- const uint8_t* row = w + (size_t) r * row_bytes;- __m256 accf[NT];- for (int t = 0; t < NT; ++t) accf[t] = _mm256_setzero_ps();- for (int ib = 0; ib < nb; ++ib) {- const uint8_t* blk = row + (size_t) ib * sizeof(block_iq4_nl);- rows_ahead(blk, pf);- const __m128i bits = _mm_loadu_si128((const __m128i*) (blk + 2));- const __m128i lo = _mm_and_si128(bits, m4b); // values 0..15- const __m128i hi = _mm_and_si128(_mm_srli_epi16(bits, 4), m4b); // values 16..31- const __m256i q4 = _mm256_inserti128_si256(_mm256_castsi128_si256(_mm_shuffle_epi8(values, lo)),- _mm_shuffle_epi8(values, hi), 1);- const __m256i aq = _mm256_sign_epi8(q4, q4); // |w|: the unsigned operand of maddubs- const float dx = h2f(u16(blk));- for (int t = 0; t < NT; ++t) {- const block_q8_0& b = y[t][ib];- const __m256i q8 = _mm256_loadu_si256((const __m256i*) b.qs);- const __m256i p = _mm256_madd_epi16(_mm256_maddubs_epi16(aq, _mm256_sign_epi8(q8, q4)), ones);- accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(dx * h2f(b.d)), _mm256_cvtepi32_ps(p), accf[t]);- }- }- for (int t = 0; t < NT; ++t) out[t][r] = hsum8(accf[t]);+void iq4nl256_down_rows_v(int variant, const uint8_t* w, size_t row_bytes, int n, const void* const* hq, int nt,+ float* const* out, int r0, int r1) {+ const block_q8_0* y[8];+ for (int t = 0; t < nt; ++t) y[t] = (const block_q8_0*) hq[t];+#if STRATA_AVXVNNI+ if (variant & kIq256Vnni) {+ vnni::iq4nl_nt(w, row_bytes, n, y, nt, out, r0, r1);+ return;}+#endif+ (void) variant;+ plain::iq4nl_nt(w, row_bytes, n, y, nt, out, r0, r1);}void iq4nl256_down_rows(const uint8_t* w, size_t row_bytes, int n, const void* const* hq, int nt, float* const* out,int r0, int r1) {- const block_q8_0* y[8];- for (int t = 0; t < nt; ++t) y[t] = (const block_q8_0*) hq[t];- switch (nt) {- case 1: iq4nl_rows<1>(w, row_bytes, n, y, out, r0, r1); break;- case 2: iq4nl_rows<2>(w, row_bytes, n, y, out, r0, r1); break;- case 3: iq4nl_rows<3>(w, row_bytes, n, y, out, r0, r1); break;- case 4: iq4nl_rows<4>(w, row_bytes, n, y, out, r0, r1); break;- default: for (int t0 = 0; t0 < nt; t0 += 4) {- const int k = nt - t0 < 4 ? nt - t0 : 4;- iq4nl256_down_rows(w, row_bytes, n, hq + t0, k, out + t0, r0, r1);- }- }+ iq4nl256_down_rows_v(vnni_on() ? kIq256Vnni : 0, w, row_bytes, n, hq, nt, out, r0, r1);}} // namespace strata::kernels::cpudiff --git a/src/kernels/cpu/iq_avx2_parity.cpp b/src/kernels/cpu/iq_avx2_parity.cppindex ff9f144c4..3671f96ca 100644--- a/src/kernels/cpu/iq_avx2_parity.cpp+++ b/src/kernels/cpu/iq_avx2_parity.cpp@@ -81,7 +81,7 @@ std::string variant_name(int) { return "dispatch"; }#elseconstexpr bool kVariants = true;int gu_variants() { return cpu::iq256_variants(); }-int down_variants() { return 0; }+int down_variants() { return cpu::iq256_variants() & cpu::kIq256Vnni; }void gu_rows_v(int v, int type, const uint8_t* blob, size_t gu_row, size_t up_off, const void* const* act, int nt,float* const* ff) {cpu::iq256_gu_rows_v(v, type, blob, gu_row, up_off, (int) kH, act, nt, ff, 0, (int) kFF);@@ -89,16 +89,17 @@ void gu_rows_v(int v, int type, const uint8_t* blob, size_t gu_row, size_t up_ofvoid gate_rows_v(int v, int type, const uint8_t* w, size_t row_bytes, const void* const* act, int nt, float* const* out) {cpu::iq256_rows_v(v, type, w, row_bytes, (int) kH, act, nt, out, 0, (int) kFF);}-void iq4nl_rows_v(int, const uint8_t* w, size_t row_bytes, const void* const* hq, int nt, float* const* out) {- cpu::iq4nl256_down_rows(w, row_bytes, (int) kFF, hq, nt, out, 0, (int) kH);+void iq4nl_rows_v(int v, const uint8_t* w, size_t row_bytes, const void* const* hq, int nt, float* const* out) {+ cpu::iq4nl256_down_rows_v(v, w, row_bytes, (int) kFF, hq, nt, out, 0, (int) kH);}-void q2_rows_v(int, const uint8_t* w, size_t row_bytes, const cpu::ActQ* const* a, int nt, float* const* out) {- cpu::q2_0_gguf_rows_multi_avx2(w, row_bytes, (int) (kFF / 64), a, nt, out, 0, (int) kH);+void q2_rows_v(int v, const uint8_t* w, size_t row_bytes, const cpu::ActQ* const* a, int nt, float* const* out) {+ cpu::q2_0_gguf_rows_multi_avx2_v((v & cpu::kIq256Vnni) != 0, w, row_bytes, (int) (kFF / 64), a, nt, out, 0, (int) kH);}std::string variant_name(int v) {if (v == 0) return "scalar";std::string s;if (v & cpu::kIq256Gather) s += "gather";+ if (v & cpu::kIq256Vnni) s += s.empty() ? "vnni" : "+vnni";return s;}#endifdiff --git a/src/kernels/cpu/iq_avx2_rows.inl b/src/kernels/cpu/iq_avx2_rows.inlnew file mode 100644index 000000000..a8af07fb3--- /dev/null+++ b/src/kernels/cpu/iq_avx2_rows.inl@@ -0,0 +1,290 @@+// src/kernels/cpu/iq_avx2_rows.inl - the row kernels of iq_avx2.cpp. Not a header: iq_avx2.cpp includes it twice,+//+// STRATA_ROWS_VNNI 0 the AVX2 forms (namespace plain);+// STRATA_ROWS_VNNI 1 the same loops with AVX-VNNI (namespace vnni, built with STRATA_AVXVNNI): vpdpwssd for each+// madd + add, vpdpbusd for maddubs + madd(ones). Integer sums, so the same bits.+//+// Every function here is STRATA_ROWS_FN: nothing in the plain copy, GCC/Clang's target("avxvnni") in the VNNI copy, so+// the AVX-VNNI intrinsics compile in these functions only and the plain copy stays AVX2 (-mavxvnni on the whole file+// would let the compiler use the extension anywhere). MSVC takes the intrinsics under /arch:AVX2. The VNNI copy is+// called only where cpu_avxvnni_ok().++// acc + madd(p16, sc). vpdpwssd is the madd and the add in one uop; both wrap the same way.+STRATA_ROWS_FN inline __m256i madd_add(__m256i acc, __m256i p16, __m256i sc) {+#if STRATA_ROWS_VNNI+ return _mm256_dpwssd_avx_epi32(acc, p16, sc);+#else+ return _mm256_add_epi32(acc, _mm256_madd_epi16(p16, sc));+#endif+}++// Four u8 x s8 products per int32 lane. vpdpbusd does not saturate where maddubs would, but maddubs cannot here: the+// u8 side is an IQ4_NL |w| <= 127, so a pair is at most 2 * 127 * 128.+STRATA_ROWS_FN inline __m256i dot4u(__m256i u8, __m256i s8) {+#if STRATA_ROWS_VNNI+ return _mm256_dpbusd_avx_epi32(_mm256_setzero_si256(), u8, s8);+#else+ return _mm256_madd_epi16(_mm256_maddubs_epi16(u8, s8), _mm256_set1_epi16(1));+#endif+}++template <int TY, int NT> 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();+ __m256 accf[NT];+ for (int t = 0; t < NT; ++t) accf[t] = _mm256_setzero_ps();+ for (int i = 0; i < nblocks; ++i) {+ const uint8_t* blk = row + (size_t) i * Fmt32<TY>::bytes;+ rows_ahead(blk, pf);+ __m256i acci[NT];+ for (int t = 0; t < NT; ++t) acci[t] = _mm256_setzero_si256();+ for (int j = 0; j < 4; ++j) {+ for (int half = 0; half < 2; ++half) {+ __m256i g, sgn, sc;+ Fmt32<TY>::decode(blk, j, half, g, sgn, sc);+ const int off = 64 * j + 32 * half;+ for (int t = 0; t < NT; ++t) {+ const __m256i yv = _mm256_loadu_si256((const __m256i*) (y[t][i].qs + off));+ const __m256i ys = _mm256_sign_epi8(yv, sgn);+ acci[t] = madd_add(acci[t], _mm256_maddubs_epi16(g, ys), sc);+ }+ }+ }+ const float dx = h2f(u16(blk)) * Fmt32<TY>::K;+ for (int t = 0; t < NT; ++t)+ accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(dx * y[t][i].d), _mm256_cvtepi32_ps(acci[t]), accf[t]);+ }+ for (int t = 0; t < NT; ++t) res[t] = hsum8(accf[t]);+}++// ---- IQ2_XS (17) with ggml's vectorized sign decode (arch/x86/quants.c, ggml_vec_dot_iq2_xs_q8_K):+// the 7-bit sign indices never touch ksigns_iq2xs - the 8th sign bit is reconstructed by parity (two+// shifts, a xor and ggml's bit_helper pshufb) and the per-half sign vectors come out of shuffles of one+// 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+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,+ 1, 2, 4, 8, 16, 32, 64, (uint8_t) 0x80, 1, 2, 4, 8, 16, 32, 64, (uint8_t) 0x80 };+ static const char shuf_even[32] = { // sign bits of u16 0,1,2,3 (even bytes of the sign register)+ 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2,+ 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6 };+ static const char shuf_odd[32] = { // sign bits of u16 4,5,6,7+ 8, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 10, 10, 10,+ 12, 12, 12, 12, 12, 12, 12, 12, 14, 14, 14, 14, 14, 14, 14, 14 };+ static const uint8_t bit_help[32] = { // 0x80 when the 4-bit index has odd popcount (parity bit 7)+ 0x00, (uint8_t) 0x80, (uint8_t) 0x80, 0x00, (uint8_t) 0x80, 0x00, 0x00, (uint8_t) 0x80,+ (uint8_t) 0x80, 0x00, 0x00, (uint8_t) 0x80, 0x00, (uint8_t) 0x80, (uint8_t) 0x80, 0x00,+ 0x00, (uint8_t) 0x80, (uint8_t) 0x80, 0x00, (uint8_t) 0x80, 0x00, 0x00, (uint8_t) 0x80,+ (uint8_t) 0x80, 0x00, 0x00, (uint8_t) 0x80, 0x00, (uint8_t) 0x80, (uint8_t) 0x80, 0x00 };+ static const uint8_t k_sc_shuffle[128] = { // half h -> scale bytes 2h (lanes 0-7), 2h+1 (lanes 8-15)+ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,+ 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,+ 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,+ 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,+ 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,+ 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11,+ 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13,+ 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15 };+ const __m256i bsel = _mm256_loadu_si256((const __m256i*) bit_sel);+ const __m256i shev = _mm256_loadu_si256((const __m256i*) shuf_even);+ const __m256i shod = _mm256_loadu_si256((const __m256i*) shuf_odd);+ const __m256i bhelp = _mm256_loadu_si256((const __m256i*) bit_help);+ const __m256i m511 = _mm256_set1_epi16(511);+ const __m128i m4 = _mm_set1_epi8(0xf), m1 = _mm_set1_epi8(1);+ const __m256i one8 = _mm256_set1_epi8(1);+ const int pf = prefetch_distance();++ __m256 accf[NT];+ for (int t = 0; t < NT; ++t) accf[t] = _mm256_setzero_ps();+ for (int i = 0; i < nblocks; ++i) {+ const uint8_t* blk = row + (size_t) i * 74;+ rows_ahead(blk, pf);+ // the 8 scale bytes -> 16 half-scales of 2*s+1, interleaved [a0, b0, a1, b1, ...] (ggml's unpack)+ __m128i st = _mm_set1_epi64x((long long) u64(blk + 66));+ st = _mm_unpacklo_epi8(_mm_and_si128(st, m4), _mm_and_si128(_mm_srli_epi16(st, 4), m4));+ const __m128i scales = _mm_add_epi8(_mm_slli_epi16(st, 1), m1);+ __m256i acc[NT][2];+ for (int t = 0; t < NT; ++t) acc[t][0] = acc[t][1] = _mm256_setzero_si256();+ for (int jj = 0; jj < 2; ++jj) { // 128 values (16 u16) per step+ const __m256i q2 = _mm256_loadu_si256((const __m256i*) (blk + 2 + 32 * jj));+ const __m256i p7 = _mm256_srli_epi16(q2, 9); // 7 sign bits per u16, even bytes+ const __m256i p3 = _mm256_srli_epi16(q2, 13); // sign index bits 4-6+ const __m256i fsb = _mm256_or_si256(p7,+ _mm256_shuffle_epi8(bhelp, _mm256_xor_si256(p7, p3))); // + the parity sign bit 7+ const __m256i s01 = _mm256_broadcastsi128_si256(_mm256_castsi256_si128(fsb)); // u16 0-7+ const __m256i s23 = _mm256_broadcastsi128_si256(_mm256_extracti128_si256(fsb, 1)); // u16 8-15+ alignas(32) uint16_t gi[16];+ _mm256_store_si256((__m256i*) gi, _mm256_and_si256(q2, m511));+ for (int h = 0; h < 4; ++h) { // the four 32-value halves of this group+ const int H = 4 * jj + h; // global half index -> scales byte pair+ const __m256i g = _mm256_set_epi64x((long long) iq2xs_grid[gi[4 * h + 3]],+ (long long) iq2xs_grid[gi[4 * h + 2]],+ (long long) iq2xs_grid[gi[4 * h + 1]],+ (long long) iq2xs_grid[gi[4 * h]]);+ const __m256i sb = _mm256_shuffle_epi8(h < 2 ? s01 : s23, (h & 1) ? shod : shev);+ const __m256i sgn = _mm256_or_si256(+ _mm256_cmpeq_epi8(_mm256_and_si256(sb, bsel), bsel), one8);+ const __m256i sc = _mm256_cvtepi8_epi16(+ _mm_shuffle_epi8(scales, _mm_loadu_si128((const __m128i*) k_sc_shuffle + H)));+ const int off = 128 * jj + 32 * h;+ for (int t = 0; t < NT; ++t) {+ const __m256i yv = _mm256_loadu_si256((const __m256i*) (y[t][i].qs + off));+ const __m256i ys = _mm256_sign_epi8(yv, sgn);+ acc[t][h & 1] = madd_add(acc[t][h & 1], _mm256_maddubs_epi16(g, ys), sc);+ }+ }+ }+ 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),+ _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]);+}++template <int TY, int NT> STRATA_ROWS_FN+inline void row_dot_any(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res) {+ if constexpr (TY == 17) row_dot_iq2xs<NT>(row, nblocks, y, res);+ else row_dot<TY, NT>(row, nblocks, y, res);+}++template <int TY, int NT> STRATA_ROWS_FN+void gu_rows(const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act, float* const* ff,+ int r0, int r1) {+ const block_q8_K* y[NT];+ for (int t = 0; t < NT; ++t) y[t] = (const block_q8_K*) act[t];+ const int nb = n / QK_K;+ float g[NT], u[NT];+ for (int r = r0; r < r1; ++r) {+ row_dot_any<TY, NT>(blob + (size_t) r * gu_row, nb, y, g);+ row_dot_any<TY, NT>(blob + up_off + (size_t) r * gu_row, nb, y, u);+ for (int t = 0; t < NT; ++t) ff[t][r] = (g[t] / (1.f + std::exp(-g[t]))) * u[t];+ }+}++template <int TY, int NT> STRATA_ROWS_FN+void dot_rows(const uint8_t* w, size_t row_bytes, int n, const void* const* act, float* const* out, int r0, int r1) {+ const block_q8_K* y[NT];+ for (int t = 0; t < NT; ++t) y[t] = (const block_q8_K*) act[t];+ float res[NT];+ for (int r = r0; r < r1; ++r) {+ row_dot_any<TY, NT>(w + (size_t) r * row_bytes, n / QK_K, y, res);+ for (int t = 0; t < NT; ++t) out[t][r] = res[t];+ }+}++template <int TY> STRATA_ROWS_FN+void gu_rows_nt(int nt, const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act,+ float* const* ff, int r0, int r1) {+ switch (nt) {+ case 1: gu_rows<TY, 1>(blob, gu_row, up_off, n, act, ff, r0, r1); break;+ case 2: gu_rows<TY, 2>(blob, gu_row, up_off, n, act, ff, r0, r1); break;+ case 3: gu_rows<TY, 3>(blob, gu_row, up_off, n, act, ff, r0, r1); break;+ case 4: gu_rows<TY, 4>(blob, gu_row, up_off, n, act, ff, r0, r1); break;+ case 5: gu_rows<TY, 5>(blob, gu_row, up_off, n, act, ff, r0, r1); break;+ case 6: gu_rows<TY, 6>(blob, gu_row, up_off, n, act, ff, r0, r1); break;+ case 7: gu_rows<TY, 7>(blob, gu_row, up_off, n, act, ff, r0, r1); break;+ default: gu_rows<TY, 8>(blob, gu_row, up_off, n, act, ff, r0, r1); break;+ }+}++template <int TY> STRATA_ROWS_FN+void dot_rows_nt(int nt, const uint8_t* w, size_t row_bytes, int n, const void* const* act, float* const* out, int r0,+ int r1) {+ switch (nt) {+ case 1: dot_rows<TY, 1>(w, row_bytes, n, act, out, r0, r1); break;+ case 2: dot_rows<TY, 2>(w, row_bytes, n, act, out, r0, r1); break;+ case 3: dot_rows<TY, 3>(w, row_bytes, n, act, out, r0, r1); break;+ case 4: dot_rows<TY, 4>(w, row_bytes, n, act, out, r0, r1); break;+ default: for (int t0 = 0; t0 < nt; t0 += 4) {+ const int k = nt - t0 < 4 ? nt - t0 : 4;+ dot_rows_nt<TY>(k, w, row_bytes, n, act + t0, out + t0, r0, r1);+ }+ }+}++// The decode of each format for a variant: the gathered one (kIq256Gather) for IQ3_XXS, IQ3_S and IQ2_S.+template <bool G> STRATA_ROWS_FN+void gu_type(int type, int nt, const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act,+ float* const* ff, int r0, int r1) {+ switch (type) {+ case 16: gu_rows_nt<16>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;+ case 17: gu_rows_nt<17>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;+ case 18: gu_rows_nt<G ? 118 : 18>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;+ case 21: gu_rows_nt<G ? 121 : 21>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;+ case 22: gu_rows_nt<G ? 122 : 22>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;+ case 23: gu_rows_nt<23>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;+ default: break;+ }+}++template <bool G> STRATA_ROWS_FN+void dot_type(int type, int nt, const uint8_t* w, size_t row_bytes, int n, const void* const* act, float* const* out,+ int r0, int r1) {+ switch (type) {+ case 16: dot_rows_nt<16>(nt, w, row_bytes, n, act, out, r0, r1); break;+ case 17: dot_rows_nt<17>(nt, w, row_bytes, n, act, out, r0, r1); break;+ case 18: dot_rows_nt<G ? 118 : 18>(nt, w, row_bytes, n, act, out, r0, r1); break;+ case 21: dot_rows_nt<G ? 121 : 21>(nt, w, row_bytes, n, act, out, r0, r1); break;+ case 22: dot_rows_nt<G ? 122 : 22>(nt, w, row_bytes, n, act, out, r0, r1); break;+ case 23: dot_rows_nt<23>(nt, w, row_bytes, n, act, out, r0, r1); break;+ default: break;+ }+}++// ---- IQ4_NL (type 20): 32-value blocks of 18 bytes (f16 d + 16 nibble bytes) against Q8_0 activations.+// ggml-cpu's own AVX-2 dot (ggml_vec_dot_iq4_nl_q8_0) is single-token: every token redoes the nibble ->+// kvalues_iq4nl pshufb decode and the |w| half of the signed x signed product. Here both are computed once+// per block; every token then costs a sign, a maddubs, a madd and an fmadd. The arithmetic is ggml's - only+// the order of the float additions differs.+template <int NT> STRATA_ROWS_FN+void iq4nl_rows(const uint8_t* w, size_t row_bytes, int n, const block_q8_0* const* y, float* const* out,+ int r0, int r1) {+ const __m128i values = _mm_loadu_si128((const __m128i*) kvalues_iq4nl);+ const __m128i m4b = _mm_set1_epi8(0x0f);+ const int nb = n / QK4_NL;+ const int pf = prefetch_distance();+ for (int r = r0; r < r1; ++r) {+ const uint8_t* row = w + (size_t) r * row_bytes;+ __m256 accf[NT];+ for (int t = 0; t < NT; ++t) accf[t] = _mm256_setzero_ps();+ for (int ib = 0; ib < nb; ++ib) {+ const uint8_t* blk = row + (size_t) ib * sizeof(block_iq4_nl);+ rows_ahead(blk, pf);+ const __m128i bits = _mm_loadu_si128((const __m128i*) (blk + 2));+ const __m128i lo = _mm_and_si128(bits, m4b); // values 0..15+ const __m128i hi = _mm_and_si128(_mm_srli_epi16(bits, 4), m4b); // values 16..31+ const __m256i q4 = _mm256_inserti128_si256(_mm256_castsi128_si256(_mm_shuffle_epi8(values, lo)),+ _mm_shuffle_epi8(values, hi), 1);+ const __m256i aq = _mm256_sign_epi8(q4, q4); // |w|: the unsigned operand of maddubs+ const float dx = h2f(u16(blk));+ for (int t = 0; t < NT; ++t) {+ const block_q8_0& b = y[t][ib];+ const __m256i q8 = _mm256_loadu_si256((const __m256i*) b.qs);+ const __m256i p = dot4u(aq, _mm256_sign_epi8(q8, q4));+ accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(dx * h2f(b.d)), _mm256_cvtepi32_ps(p), accf[t]);+ }+ }+ for (int t = 0; t < NT; ++t) out[t][r] = hsum8(accf[t]);+ }+}++STRATA_ROWS_FN void iq4nl_nt(const uint8_t* w, size_t row_bytes, int n, const block_q8_0* const* y, int nt,+ float* const* out, int r0, int r1) {+ switch (nt) {+ case 1: iq4nl_rows<1>(w, row_bytes, n, y, out, r0, r1); break;+ case 2: iq4nl_rows<2>(w, row_bytes, n, y, out, r0, r1); break;+ case 3: iq4nl_rows<3>(w, row_bytes, n, y, out, r0, r1); break;+ case 4: iq4nl_rows<4>(w, row_bytes, n, y, out, r0, r1); break;+ default: for (int t0 = 0; t0 < nt; t0 += 4) {+ const int k = nt - t0 < 4 ? nt - t0 : 4;+ iq4nl_nt(w, row_bytes, n, y + t0, k, out + t0, r0, r1);+ }+ }+}++#undef STRATA_ROWS_VNNI+#undef STRATA_ROWS_FNdiff --git a/src/kernels/cpu/q2_avx2.cpp b/src/kernels/cpu/q2_avx2.cppindex cea933f2f..c406d4277 100644--- a/src/kernels/cpu/q2_avx2.cpp+++ b/src/kernels/cpu/q2_avx2.cpp@@ -1,10 +1,13 @@// src/kernels/cpu/q2_avx2.cpp - plan v0.3 P6: the Q2_0 expert rows and the activation quantizer for CPUs// without AVX-512 (Intel Core 12th-14th gen and Core Ultra, AMD Zen 2/3).//-// Compiled with AVX2 only, so nothing here can fault on those CPUs. The arithmetic is the AVX-512 kernels':-// codes 0..3 against the int8 activation per 32-value chunk, times the weight scale and the chunk scale, minus-// the weight scale times the chunk's `hx` (the -1 code offset); the quantizer is the scalar rule, bit for bit.+// Compiled with AVX2 only, so nothing here can fault on those CPUs; the row kernel's AVX-VNNI copy (vpdpbusd, the+// same sums) is compiled under its own target attribute and called only where cpu_avxvnni_ok(). The arithmetic is+// the AVX-512 kernels': codes 0..3 against the int8 activation per 32-value chunk, times the weight scale and the+// chunk scale, minus the weight scale times the chunk's `hx` (the -1 code offset); the quantizer is the scalar rule,+// bit for bit.#include "strata/kernels/cpu/expert.hpp"+#include "strata/kernels/cpu/expert_layout.hpp"#include <immintrin.h>@@ -12,6 +15,16 @@#include <cstdlib>#include <cstring>+// STRATA_AVXVNNI (CMake: the compiler has the AVX-VNNI intrinsics), as in iq_avx2.cpp+#if !defined(STRATA_AVXVNNI)+#define STRATA_AVXVNNI 0+#endif+#if STRATA_AVXVNNI && (defined(__GNUC__) || defined(__clang__))+#define STRATA_AVXVNNI_FN __attribute__((target("avxvnni")))+#else+#define STRATA_AVXVNNI_FN+#endif+namespace strata::kernels::cpu {namespace {@@ -35,41 +48,19 @@ inline void unpack64(const uint8_t* codes, __m256i& lo, __m256i& hi) {hi = _mm256_set_m128i(_mm_unpackhi_epi16(b0, b1), _mm_unpacklo_epi16(b0, b1)); // values 32..63}-template <int NT>-inline void row_multi(const uint8_t* row, const ActQ* const* a, int nblocks, float* res) {- __m256 acc[NT];- float corr[NT];- for (int t = 0; t < NT; ++t) { acc[t] = _mm256_setzero_ps(); corr[t] = 0.f; }- const __m256i ones = _mm256_set1_epi16(1);- for (int b = 0; b < nblocks; ++b) {- const uint8_t* blk = row + (size_t) b * 18;- const float d = h2f(blk);- __m256i lo, hi;- unpack64(blk + 2, lo, hi);- for (int t = 0; t < NT; ++t) {- const int8_t* q = a[t]->q + b * 64;- const __m256i s0 = _mm256_madd_epi16(_mm256_maddubs_epi16(lo, _mm256_loadu_si256((const __m256i*) q)), ones);- const __m256i s1 = _mm256_madd_epi16(_mm256_maddubs_epi16(hi, _mm256_loadu_si256((const __m256i*) (q + 32))), ones);- acc[t] = _mm256_fmadd_ps(_mm256_set1_ps(d * a[t]->scale[2 * b]), _mm256_cvtepi32_ps(s0), acc[t]);- acc[t] = _mm256_fmadd_ps(_mm256_set1_ps(d * a[t]->scale[2 * b + 1]), _mm256_cvtepi32_ps(s1), acc[t]);- corr[t] += d * (a[t]->hx[2 * b] + a[t]->hx[2 * b + 1]);- }- }- for (int t = 0; t < NT; ++t) {- const __m128 h = _mm_add_ps(_mm256_castps256_ps128(acc[t]), _mm256_extractf128_ps(acc[t], 1));- const __m128 s = _mm_add_ps(h, _mm_movehl_ps(h, h));- res[t] = _mm_cvtss_f32(_mm_add_ss(s, _mm_movehdup_ps(s))) - corr[t];- }-}--template <int NT>-void rows(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, float* const* out, int r0, int r1) {- float res[NT];- for (int r = r0; r < r1; ++r) {- row_multi<NT>(w + (size_t) r * row_bytes, a, nblocks, res);- for (int t = 0; t < NT; ++t) out[t][r] = res[t];- }-}+// ---- the row kernel, twice: the AVX2 form and the AVX-VNNI one (q2_avx2_rows.inl)+namespace plain {+#define STRATA_ROWS_VNNI 0+#define STRATA_ROWS_FN+#include "q2_avx2_rows.inl"+} // namespace plain+#if STRATA_AVXVNNI+namespace vnni {+#define STRATA_ROWS_VNNI 1+#define STRATA_ROWS_FN STRATA_AVXVNNI_FN+#include "q2_avx2_rows.inl"+} // namespace vnni+#endif// ================================ the "bit-plane" row dot (opt-in: STRATA_Q2_BITPLANE=1) ================================//@@ -161,19 +152,21 @@ const bool kBitplane = [] {bool q2_bitplane_enabled() { return kBitplane; }+void q2_0_gguf_rows_multi_avx2_v(bool vnni_rows, const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a,+ int nt, float* const* out, int r0, int r1) {+#if STRATA_AVXVNNI+ if (vnni_rows) {+ vnni::rows_nt(w, row_bytes, nblocks, a, nt, out, r0, r1);+ return;+ }+#endif+ (void) vnni_rows;+ plain::rows_nt(w, row_bytes, nblocks, a, nt, out, r0, r1);+}+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) {- case 1: rows<1>(w, row_bytes, nblocks, a, out, r0, r1); break;- case 2: rows<2>(w, row_bytes, nblocks, a, out, r0, r1); break;- case 3: rows<3>(w, row_bytes, nblocks, a, out, r0, r1); break;- case 4: rows<4>(w, row_bytes, nblocks, a, out, r0, r1); break;- default:- for (int t0 = 0; t0 < nt; t0 += 4) {- const int k = nt - t0 < 4 ? nt - t0 : 4;- q2_0_gguf_rows_multi_avx2_legacy(w, row_bytes, nblocks, a + t0, k, out + t0, r0, r1);- }- }+ q2_0_gguf_rows_multi_avx2_v(STRATA_AVXVNNI && cpu_avxvnni_ok(), w, row_bytes, nblocks, a, nt, out, r0, r1);}void q2_0_gguf_rows_multi_avx2(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, int nt,diff --git a/src/kernels/cpu/q2_avx2_rows.inl b/src/kernels/cpu/q2_avx2_rows.inlnew file mode 100644index 000000000..a3e1ef575--- /dev/null+++ b/src/kernels/cpu/q2_avx2_rows.inl@@ -0,0 +1,67 @@+// src/kernels/cpu/q2_avx2_rows.inl - the Q2_0 row kernel of q2_avx2.cpp, included there twice like+// iq_avx2_rows.inl: the AVX2 form (STRATA_ROWS_VNNI 0, namespace plain) and the AVX-VNNI one (1, namespace vnni), every+// function under STRATA_ROWS_FN (GCC/Clang's target("avxvnni") in the VNNI copy). Not a header.++// The 2-bit codes (0..3) times the int8 activation, four products per int32 lane. vpdpbusd is one uop where maddubs+// + madd(ones) take two on the multiply ports; maddubs cannot saturate (a pair is at most 2 * 3 * 128), so the sums+// are the same.+STRATA_ROWS_FN inline __m256i dot4(__m256i codes, __m256i act) {+#if STRATA_ROWS_VNNI+ return _mm256_dpbusd_avx_epi32(_mm256_setzero_si256(), codes, act);+#else+ return _mm256_madd_epi16(_mm256_maddubs_epi16(codes, act), _mm256_set1_epi16(1));+#endif+}++template <int NT> STRATA_ROWS_FN+inline void row_multi(const uint8_t* row, const ActQ* const* a, int nblocks, float* res) {+ __m256 acc[NT];+ float corr[NT];+ for (int t = 0; t < NT; ++t) { acc[t] = _mm256_setzero_ps(); corr[t] = 0.f; }+ for (int b = 0; b < nblocks; ++b) {+ const uint8_t* blk = row + (size_t) b * 18;+ const float d = h2f(blk);+ __m256i lo, hi;+ unpack64(blk + 2, lo, hi);+ for (int t = 0; t < NT; ++t) {+ const int8_t* q = a[t]->q + b * 64;+ const __m256i s0 = dot4(lo, _mm256_loadu_si256((const __m256i*) q));+ const __m256i s1 = dot4(hi, _mm256_loadu_si256((const __m256i*) (q + 32)));+ acc[t] = _mm256_fmadd_ps(_mm256_set1_ps(d * a[t]->scale[2 * b]), _mm256_cvtepi32_ps(s0), acc[t]);+ acc[t] = _mm256_fmadd_ps(_mm256_set1_ps(d * a[t]->scale[2 * b + 1]), _mm256_cvtepi32_ps(s1), acc[t]);+ corr[t] += d * (a[t]->hx[2 * b] + a[t]->hx[2 * b + 1]);+ }+ }+ for (int t = 0; t < NT; ++t) {+ const __m128 h = _mm_add_ps(_mm256_castps256_ps128(acc[t]), _mm256_extractf128_ps(acc[t], 1));+ const __m128 s = _mm_add_ps(h, _mm_movehl_ps(h, h));+ res[t] = _mm_cvtss_f32(_mm_add_ss(s, _mm_movehdup_ps(s))) - corr[t];+ }+}++template <int NT> STRATA_ROWS_FN+void rows(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, float* const* out, int r0, int r1) {+ float res[NT];+ for (int r = r0; r < r1; ++r) {+ row_multi<NT>(w + (size_t) r * row_bytes, a, nblocks, res);+ for (int t = 0; t < NT; ++t) out[t][r] = res[t];+ }+}++STRATA_ROWS_FN void rows_nt(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) {+ case 1: rows<1>(w, row_bytes, nblocks, a, out, r0, r1); break;+ case 2: rows<2>(w, row_bytes, nblocks, a, out, r0, r1); break;+ case 3: rows<3>(w, row_bytes, nblocks, a, out, r0, r1); break;+ case 4: rows<4>(w, row_bytes, nblocks, a, out, r0, r1); break;+ default:+ for (int t0 = 0; t0 < nt; t0 += 4) {+ const int k = nt - t0 < 4 ? nt - t0 : 4;+ rows_nt(w, row_bytes, nblocks, a + t0, k, out + t0, r0, r1);+ }+ }+}++#undef STRATA_ROWS_VNNI+#undef STRATA_ROWS_FN