Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

cpu: q2_avx1 - the Q2_0 rows and the Q8_1 quantizer for CPUs with AVX but no AVX2 (fixes #1699)

bossman7b14a443eea4
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f867b9add..3e1f9f470 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1245,7 +1245,7 @@ if(STRATA_NATIVE_EXPERTS)
# one-pass static linking has no cycle to resolve)
target_sources(strata_kernels_cpu PRIVATE src/kernels/cpu/native_expert.cpp src/kernels/cpu/iq_avx512.cpp
src/kernels/cpu/iq_avx2.cpp src/kernels/cpu/kq_avx2.cpp
- src/kernels/cpu/kq_avx1.cpp)
+ src/kernels/cpu/kq_avx1.cpp src/kernels/cpu/q2_avx1.cpp)
target_include_directories(strata_kernels_cpu PRIVATE third_party/ggml)
if(MSVC)
set_source_files_properties(src/kernels/cpu/iq_avx512.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX512")
@@ -1253,12 +1253,20 @@ if(STRATA_NATIVE_EXPERTS)
set_source_files_properties(src/kernels/cpu/kq_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2")
# the router lookahead's AVX1 dot (older CPUs; called behind cpu_avx1_ok): no FMA, no 256-bit integer ops
set_source_files_properties(src/kernels/cpu/kq_avx1.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX")
+ # the older CPUs' Q2_0 rows and activation quantizer (q2_avx1.cpp, issue #1699): 128-bit integer lanes,
+ # no FMA/F16C, its own #error guard; called only behind cpu_avx1_ok() && !cpu_avx2_ok()
+ set_source_files_properties(src/kernels/cpu/q2_avx1.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX")
else()
set_source_files_properties(src/kernels/cpu/iq_avx512.cpp PROPERTIES COMPILE_OPTIONS
"-mavx512f;-mavx512bw;-mavx512vl;-mavx512dq;-mavx512vnni;-mavx512vbmi;-mf16c;-mfma")
set_source_files_properties(src/kernels/cpu/iq_avx2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mf16c")
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")
+ # the older CPUs' Q2_0 rows and activation quantizer (q2_avx1.cpp, issue #1699): SSE4.2 + AVX with
+ # AVX2/FMA/F16C explicitly off, so a -march=native build host cannot leak AVX2 code into the file that
+ # runs on a Sandy Bridge Xeon
+ set_source_files_properties(src/kernels/cpu/q2_avx1.cpp PROPERTIES COMPILE_OPTIONS
+ "-msse4.2;-mavx;-mno-avx2;-mno-fma;-mno-f16c")
endif()
set_property(SOURCE src/kernels/cpu/iq_avx2.cpp APPEND PROPERTY COMPILE_DEFINITIONS ${_strata_avxvnni})
if(STRATA_ISA_FLOOR STREQUAL "avx")
diff --git a/include/strata/kernels/cpu/expert.hpp b/include/strata/kernels/cpu/expert.hpp
index 8510598b3..441dfac0a 100644
--- a/include/strata/kernels/cpu/expert.hpp
+++ b/include/strata/kernels/cpu/expert.hpp
@@ -189,6 +189,14 @@ void q2_0_gguf_rows_multi_avx2_legacy(const uint8_t* w, size_t row_bytes, int nb
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);
+/// The same for CPUs with AVX but no AVX2 (src/kernels/cpu/q2_avx1.cpp, the third rung of q2_rows_any /
+/// act_quant_any, issue #1699): same arithmetic, 128-bit integer lanes, mul+add instead of FMA (last bits),
+/// software fp16 conversion. Called only behind cpu_avx1_ok() && !cpu_avx2_ok().
+void q2_0_gguf_rows_multi_avx1(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, int nt,
+ float* const* out, int r0, int r1);
+void q2_0_gguf_rows_multi_avx1_legacy(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_avx1(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.hpp
index 615afa195..6b3fddb66 100644
--- a/include/strata/kernels/cpu/expert_layout.hpp
+++ b/include/strata/kernels/cpu/expert_layout.hpp
@@ -85,6 +85,9 @@ std::string cpu_name();
void q2_rows_any(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_any(const float* x, int n, ActQ& a);
+/// The same dispatch, always the pre-bit-plane kernel (tests and A/B; the bit-plane switch off by construction).
+void q2_rows_any_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 process-wide layout (canonical Q2_0 until `expert_layout_load` finds a native pack).
const ExpertLayout& expert_layout();
diff --git a/src/kernels/cpu/expert_layout.cpp b/src/kernels/cpu/expert_layout.cpp
index 4e13167a0..d8adb260f 100644
--- a/src/kernels/cpu/expert_layout.cpp
+++ b/src/kernels/cpu/expert_layout.cpp
@@ -348,12 +348,21 @@ void cpu_require_expert_support() {
void q2_rows_any(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, int nt, float* const* out,
int r0, int r1) {
if (cpu_avx512_ok()) q2_0_gguf_rows_multi(w, row_bytes, nblocks, a, nt, out, r0, r1);
- else q2_0_gguf_rows_multi_avx2(w, row_bytes, nblocks, a, nt, out, r0, r1);
+ else if (cpu_avx2_ok()) q2_0_gguf_rows_multi_avx2(w, row_bytes, nblocks, a, nt, out, r0, r1);
+ else q2_0_gguf_rows_multi_avx1(w, row_bytes, nblocks, a, nt, out, r0, r1); // issue #1699: the third rung
+}
+
+void q2_rows_any_legacy(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, int nt, float* const* out,
+ int r0, int r1) {
+ if (cpu_avx512_ok()) q2_0_gguf_rows_multi(w, row_bytes, nblocks, a, nt, out, r0, r1);
+ else if (cpu_avx2_ok()) q2_0_gguf_rows_multi_avx2_legacy(w, row_bytes, nblocks, a, nt, out, r0, r1);
+ else q2_0_gguf_rows_multi_avx1_legacy(w, row_bytes, nblocks, a, nt, out, r0, r1);
}
void act_quant_any(const float* x, int n, ActQ& a) {
if (cpu_avx512_ok()) act_quant_q8_1(x, n, a);
- else act_quant_q8_1_avx2(x, n, a);
+ else if (cpu_avx2_ok()) act_quant_q8_1_avx2(x, n, a);
+ else act_quant_q8_1_avx1(x, n, a); // issue #1699
}
#if !defined(STRATA_NATIVE_EXPERTS)
diff --git a/src/kernels/cpu/q2_avx1.cpp b/src/kernels/cpu/q2_avx1.cpp
new file mode 100644
index 000000000..6d5b96991
--- /dev/null
+++ b/src/kernels/cpu/q2_avx1.cpp
@@ -0,0 +1,294 @@
+// src/kernels/cpu/q2_avx1.cpp - the Q2_0 expert rows and the activation quantizer for CPUs that have AVX but
+// no AVX2 (Sandy Bridge through Ivy Bridge Xeons; issue #623, #1375), the third rung of q2_rows_any /
+// act_quant_any below the AVX-2 one (issue #1699: those dispatchers used to hand AVX2 code to an AVX1 CPU).
+//
+// The arithmetic is q2_avx2.cpp's: 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`. 128-bit integer lanes (maddubs and
+// madd are SSSE3/SSE2), 256-bit float accumulation with mul+add (no FMA), fp16 scales converted in software (no
+// F16C). The legacy row kernel keeps q2_avx2.cpp's lane pairing and reduction tree exactly, so the only
+// difference from an AVX2 CPU is its FMA becoming mul+add (last bits, unavoidable without FMA); the bit-plane
+// kernel's float summation order is its own (last bits, as documented for the AVX-2 one), and every token's
+// operations are the same whatever the window width (#152).
+//
+// Compiled with -msse4.2 -mavx -mno-avx2 -mno-fma -mno-f16c (CMakeLists.txt, the iq_avx1.cpp pattern) and called
+// only behind cpu_avx1_ok() && !cpu_avx2_ok(), so no CPU that lacks those features can reach it - and no CPU that
+// has AVX2 changes what it runs.
+#include "strata/kernels/cpu/expert.hpp"
+#include "strata/kernels/cpu/expert_layout.hpp"
+
+#include <immintrin.h>
+
+#include <cmath>
+#include <cstdint>
+#include <cstdlib>
+#include <cstring>
+
+#if defined(__AVX2__) || defined(__FMA__) || defined(__F16C__)
+#error "q2_avx1.cpp must be compiled without AVX2, FMA and F16C (see the per-source flags in CMakeLists.txt)"
+#endif
+
+namespace strata::kernels::cpu {
+namespace {
+
+// fp16 -> fp32 without F16C (the written-out conversion, subnormals included; iq_avx1.cpp's copy).
+inline float h2f(uint16_t h) {
+ const uint32_t sign = (uint32_t) (h >> 15) & 1u;
+ uint32_t exp = (h >> 10) & 0x1Fu, man = h & 0x3FFu, f;
+ if (exp == 0) {
+ if (man == 0) {
+ f = sign << 31;
+ } else {
+ exp = 127 - 15 + 1;
+ while (!(man & 0x400u)) { man <<= 1; --exp; }
+ man &= 0x3FFu;
+ f = (sign << 31) | (exp << 23) | (man << 13);
+ }
+ } else if (exp == 31) {
+ f = (sign << 31) | 0x7F800000u | (man << 13);
+ } else {
+ f = (sign << 31) | ((exp - 15 + 127) << 23) | (man << 13);
+ }
+ float out;
+ std::memcpy(&out, &f, 4);
+ return out;
+}
+
+// 16 bytes of 2-bit codes -> 64 codes in value order, four 16-byte vectors: v0 values 0..15, v1 16..31,
+// v2 32..47, v3 48..63 (q2_avx2.cpp's unpack64 cut to 128-bit lanes; the same interleave).
+inline void unpack64(const uint8_t* codes, __m128i& v0, __m128i& v1, __m128i& v2, __m128i& v3) {
+ const __m128i b = _mm_loadu_si128((const __m128i*) codes);
+ const __m128i m3 = _mm_set1_epi8(3);
+ const __m128i c0 = _mm_and_si128(b, m3);
+ const __m128i c1 = _mm_and_si128(_mm_srli_epi16(b, 2), m3);
+ const __m128i c2 = _mm_and_si128(_mm_srli_epi16(b, 4), m3);
+ const __m128i c3 = _mm_and_si128(_mm_srli_epi16(b, 6), m3);
+ const __m128i a0 = _mm_unpacklo_epi8(c0, c1), a1 = _mm_unpacklo_epi8(c2, c3); // bytes 0..7
+ const __m128i b0 = _mm_unpackhi_epi8(c0, c1), b1 = _mm_unpackhi_epi8(c2, c3); // bytes 8..15
+ v0 = _mm_unpacklo_epi16(a0, a1); // values 0..15
+ v1 = _mm_unpackhi_epi16(a0, a1); // values 16..31
+ v2 = _mm_unpacklo_epi16(b0, b1); // values 32..47
+ v3 = _mm_unpackhi_epi16(b0, b1); // values 48..63
+}
+
+// 16 codes against 16 activations, four products per int32 lane (q2_avx2_rows.inl's dot4 at 128-bit;
+// maddubs cannot saturate: a pair is at most 2 * 3 * 128).
+inline __m128i dot4(__m128i codes, __m128i act) {
+ return _mm_madd_epi16(_mm_maddubs_epi16(codes, act), _mm_set1_epi16(1));
+}
+
+// The legacy row kernel. The accumulator pairing matches q2_avx2.cpp's lane pairing: each 256-bit lane takes
+// the s0 contribution (values 0..31) and the s1 contribution (values 32..63) into the SAME lanes, block by
+// block, and the reduction tree is the same one ((L0+L4)+(L2+L6)) + ((L1+L5)+(L3+L7)) - the only difference
+// from an AVX2 CPU is that kernel's FMA being a mul+add here (last bits).
+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; }
+ for (int b = 0; b < nblocks; ++b) {
+ const uint8_t* blk = row + (size_t) b * 18;
+ const float d = h2f((uint16_t) (blk[0] | (blk[1] << 8)));
+ __m128i v0, v1, v2, v3;
+ unpack64(blk + 2, v0, v1, v2, v3);
+ for (int t = 0; t < NT; ++t) {
+ const int8_t* q = a[t]->q + b * 64;
+ // s0 = values 0..31: lanes 0..3 of accA's 128-bit halves; s1 = values 32..63 into the SAME lanes
+ const __m128i s0a = dot4(v0, _mm_loadu_si128((const __m128i*) q)); // values 0..15
+ const __m128i s0b = dot4(v1, _mm_loadu_si128((const __m128i*) (q + 16))); // values 16..31
+ const __m128i s1a = dot4(v2, _mm_loadu_si128((const __m128i*) (q + 32))); // values 32..47
+ const __m128i s1b = dot4(v3, _mm_loadu_si128((const __m128i*) (q + 48))); // values 48..63
+ const __m256 s0 = _mm256_insertf128_ps(_mm256_castps128_ps256(_mm_cvtepi32_ps(s0a)), _mm_cvtepi32_ps(s0b), 1);
+ const __m256 s1 = _mm256_insertf128_ps(_mm256_castps128_ps256(_mm_cvtepi32_ps(s1a)), _mm_cvtepi32_ps(s1b), 1);
+ // exactly the AVX-2 kernel's two fmadds: the whole s0 (values 0..31) by scale[2b], the whole s1
+ // (values 32..63) by scale[2b+1], into the same lanes - lane pairing and reduce tree follow suit
+ acc[t] = _mm256_add_ps(acc[t], _mm256_mul_ps(_mm256_set1_ps(d * a[t]->scale[2 * b]), s0));
+ acc[t] = _mm256_add_ps(acc[t], _mm256_mul_ps(_mm256_set1_ps(d * a[t]->scale[2 * b + 1]), s1));
+ 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 bit-plane row kernel (STRATA_Q2_BITPLANE=1), 128-bit lanes: one 64-value block at a time instead of
+// a pair per 256-bit register. The integer part is exact; the float summation order is this kernel's own (the
+// same statement as for the AVX-2 one), and every row reduces with the same tree alone or in a group (#152).
+template <int NT>
+inline void row_bp_acc(const uint8_t* row, const ActQ* const* a, int npairs, __m128* acc) {
+ for (int t = 0; t < NT; ++t) { acc[2 * t] = _mm_setzero_ps(); acc[2 * t + 1] = _mm_setzero_ps(); }
+ const __m128i m3 = _mm_set1_epi8(3);
+ const __m128i ones = _mm_set1_epi16(1);
+ for (int p = 0; p < npairs; ++p) {
+ const uint8_t* blk = row + (size_t) p * 36;
+ for (int h = 0; h < 2; ++h) {
+ uint16_t dh;
+ std::memcpy(&dh, blk + h * 18, 2); // the block's fp16 scale: bytes 0..1 (block 1 at +18)
+ const __m128 d = _mm_set1_ps(h2f(dh));
+ const __m128i c = _mm_loadu_si128((const __m128i*) (blk + 2 + h * 18)); // its 16 code bytes
+ const __m128i c0 = _mm_and_si128(c, m3);
+ const __m128i c1 = _mm_and_si128(_mm_srli_epi16(c, 2), m3);
+ const __m128i c2 = _mm_and_si128(_mm_srli_epi16(c, 4), m3);
+ const __m128i c3 = _mm_and_si128(_mm_srli_epi16(c, 6), m3);
+ for (int t = 0; t < NT; ++t) {
+ const int8_t* q = a[t]->qp + (size_t) p * 128 + h * 16;
+ __m128i s = _mm_maddubs_epi16(c0, _mm_load_si128((const __m128i*) q));
+ s = _mm_add_epi16(s, _mm_maddubs_epi16(c1, _mm_load_si128((const __m128i*) (q + 32))));
+ s = _mm_add_epi16(s, _mm_maddubs_epi16(c2, _mm_load_si128((const __m128i*) (q + 64))));
+ s = _mm_add_epi16(s, _mm_maddubs_epi16(c3, _mm_load_si128((const __m128i*) (q + 96))));
+ const __m128i i32 = _mm_sub_epi32(_mm_madd_epi16(s, ones),
+ _mm_load_si128((const __m128i*) (a[t]->psum + p * 8 + h * 4)));
+ const __m128 v = _mm_mul_ps(_mm_cvtepi32_ps(i32),
+ _mm_load_ps(a[t]->pscale + p * 8 + h * 4));
+ __m128& x = acc[2 * t + h];
+ x = _mm_add_ps(x, _mm_mul_ps(d, v));
+ }
+ }
+ }
+}
+
+inline float reduce1(__m128 a, __m128 b) { // ((v0+v1)+(v2+v3)) + ((v4+v5)+(v6+v7)), the AVX-2 kernel's tree
+ __m128 ha = _mm_hadd_ps(a, a); // [a0+a1, a2+a3, a0+a1, a2+a3]
+ ha = _mm_add_ss(ha, _mm_movehdup_ps(ha)); // (v0+v1)+(v2+v3)
+ __m128 hb = _mm_hadd_ps(b, b);
+ hb = _mm_add_ss(hb, _mm_movehdup_ps(hb)); // (v4+v5)+(v6+v7)
+ return _mm_cvtss_f32(_mm_add_ss(ha, hb));
+}
+
+template <int NT>
+void rows_bp(const uint8_t* w, size_t row_bytes, int npairs, const ActQ* const* a, float* const* out, int r0, int r1) {
+ for (int r = r0; r < r1; ++r) {
+ __m128 acc[2 * NT];
+ row_bp_acc<NT>(w + (size_t) r * row_bytes, a, npairs, acc);
+ for (int t = 0; t < NT; ++t) out[t][r] = reduce1(acc[2 * t], acc[2 * t + 1]);
+ }
+}
+
+const bool kBitplane = [] {
+ const char* e = std::getenv("STRATA_Q2_BITPLANE");
+ return e != nullptr && e[0] == '1';
+}();
+
+// ActQ::qp / psum / pscale from q and scale: q2_avx2.cpp's bitplane_image, which is already pure SSE2/SSSE3
+// (shuffle_epi8, sad, unpack) - copied rather than shared so q2_avx2.cpp stays untouched.
+void bitplane_image(ActQ& a) {
+ if (a.nchunks % 4 != 0) { a.bp_pairs = 0; return; }
+ const int nblk = a.nchunks / 2;
+ const __m128i shuf = _mm_setr_epi8(0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15);
+ const __m128i bias = _mm_set1_epi8((char) 0x80);
+ for (int b = 0; b < nblk; ++b) {
+ const int8_t* q = a.q + b * 64;
+ __m128i s[4];
+ for (int g = 0; g < 4; ++g) {
+ const __m128i v = _mm_loadu_si128((const __m128i*) (q + 16 * g));
+ s[g] = _mm_shuffle_epi8(v, shuf);
+ const __m128i sad = _mm_sad_epu8(_mm_xor_si128(v, bias), _mm_setzero_si128());
+ a.psum[(b >> 1) * 8 + (b & 1) * 4 + g] =
+ _mm_cvtsi128_si32(sad) + _mm_extract_epi16(sad, 4) - 16 * 128;
+ }
+ const __m128i t0 = _mm_unpacklo_epi32(s[0], s[1]), t1 = _mm_unpacklo_epi32(s[2], s[3]);
+ const __m128i t2 = _mm_unpackhi_epi32(s[0], s[1]), t3 = _mm_unpackhi_epi32(s[2], s[3]);
+ int8_t* dst = a.qp + (b >> 1) * 128 + (b & 1) * 16;
+ _mm_store_si128((__m128i*) (dst), _mm_unpacklo_epi64(t0, t1));
+ _mm_store_si128((__m128i*) (dst + 32), _mm_unpackhi_epi64(t0, t1));
+ _mm_store_si128((__m128i*) (dst + 64), _mm_unpacklo_epi64(t2, t3));
+ _mm_store_si128((__m128i*) (dst + 96), _mm_unpackhi_epi64(t2, t3));
+ }
+ for (int c = 0; c < a.nchunks; ++c) {
+ a.pscale[2 * c] = a.scale[c];
+ a.pscale[2 * c + 1] = a.scale[c];
+ }
+ a.bp_pairs = nblk / 2;
+}
+
+} // namespace
+
+void q2_0_gguf_rows_multi_avx1_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_avx1_legacy(w, row_bytes, nblocks, a + t0, k, out + t0, r0, r1);
+ }
+ }
+}
+
+void q2_0_gguf_rows_multi_avx1(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, int nt,
+ float* const* out, int r0, int r1) {
+ bool bp = kBitplane && nblocks % 2 == 0;
+ for (int t = 0; t < nt && bp; ++t) bp = a[t]->bp_pairs == nblocks / 2;
+ if (!bp) {
+ q2_0_gguf_rows_multi_avx1_legacy(w, row_bytes, nblocks, a, nt, out, r0, r1);
+ return;
+ }
+ const int np = nblocks / 2;
+ switch (nt) {
+ case 1: rows_bp<1>(w, row_bytes, np, a, out, r0, r1); break;
+ case 2: rows_bp<2>(w, row_bytes, np, a, out, r0, r1); break;
+ case 3: rows_bp<3>(w, row_bytes, np, a, out, r0, r1); break;
+ case 4: rows_bp<4>(w, row_bytes, np, 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_avx1(w, row_bytes, nblocks, a + t0, k, out + t0, r0, r1);
+ }
+ }
+}
+
+void act_quant_q8_1_avx1(const float* x, int n, ActQ& a) {
+ a.nchunks = n / QKA;
+ const __m128 absmask = _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff));
+ const __m128 half = _mm_set1_ps(0.5f), mhalf = _mm_set1_ps(-0.5f), zero = _mm_setzero_ps();
+ const __m128i lo = _mm_set1_epi32(-127), hi = _mm_set1_epi32(127);
+ for (int k = 0; k < a.nchunks; ++k) {
+ const float* xb = x + k * QKA;
+ __m128 v[8];
+ __m128 m = _mm_setzero_ps();
+ for (int i = 0; i < 8; ++i) {
+ v[i] = _mm_loadu_ps(xb + 4 * i);
+ m = _mm_max_ps(m, _mm_and_ps(v[i], absmask));
+ }
+ m = _mm_max_ps(m, _mm_movehl_ps(m, m));
+ const float amax = _mm_cvtss_f32(_mm_max_ss(m, _mm_movehdup_ps(m)));
+ const float s = amax > 0.f ? amax / 127.f : 0.f;
+ const float inv = s > 0.f ? 1.f / s : 0.f;
+ const __m128 vinv = _mm_set1_ps(inv);
+ __m128i sum = _mm_setzero_si128();
+ alignas(16) int32_t qi[QKA];
+ for (int i = 0; i < 8; ++i) {
+ const __m128 t = _mm_mul_ps(v[i], vinv);
+ const __m128 r = _mm_add_ps(t, _mm_blendv_ps(mhalf, half, _mm_cmp_ps(t, zero, _CMP_GE_OQ)));
+ __m128i q = _mm_cvttps_epi32(r);
+ q = _mm_min_epi32(_mm_max_epi32(q, lo), hi);
+ sum = _mm_add_epi32(sum, q);
+ _mm_store_si128((__m128i*) (qi + 4 * i), q);
+ }
+ for (int j = 0; j < QKA; ++j) a.q[k * QKA + j] = (int8_t) qi[j];
+ __m128i s4 = _mm_add_epi32(sum, _mm_shuffle_epi32(sum, 0x4E));
+ s4 = _mm_add_epi32(s4, _mm_shuffle_epi32(s4, 0xB1));
+ const int32_t total = _mm_cvtsi128_si32(s4);
+ a.scale[k] = s;
+ a.sum[k] = total;
+ a.hx[k] = s * (float) total;
+ }
+ if (kBitplane) bitplane_image(a);
+ else a.bp_pairs = 0;
+}
+
+} // namespace strata::kernels::cpu
diff --git a/src/kernels/cpu/q2_bitplane_parity.cpp b/src/kernels/cpu/q2_bitplane_parity.cpp
index cefa81f8e..65c1c1dc0 100644
--- a/src/kernels/cpu/q2_bitplane_parity.cpp
+++ b/src/kernels/cpu/q2_bitplane_parity.cpp
@@ -1,13 +1,15 @@
-// src/kernels/cpu/q2_bitplane_parity.cpp - the opt-in AVX-2 Q2_0 bit-plane kernel (STRATA_Q2_BITPLANE=1, PR #706).
+// src/kernels/cpu/q2_bitplane_parity.cpp - the opt-in Q2_0 bit-plane kernel (STRATA_Q2_BITPLANE=1, PR #706),
+// on the AVX-2 pair where the CPU has AVX2 and on the AVX1 pair below it (issue #1699) - the bit-plane is an
+// AVX2/AVX1 feature, the AVX-512 kernel does not read the image.
//
-// 1. against the legacy AVX-2 kernel: the integer part is exact, only the float summation order differs, so the
-// two agree to a few ulps of the largest term (a relative bound on the row's magnitude);
+// 1. against the legacy kernel of the same rung: the integer part is exact, only the float summation order
+// differs, so the two agree to a few ulps of the largest term (a relative bound on the row's magnitude);
// 2. bitwise: a token's rows are the same alone, in a window of any width, and cut into any row ranges
// (the engine's pool splits rows between workers, a verify window changes the width);
// 3. the legacy path is untouched when the switch is off (this test only runs with the switch on: ctest sets it).
-// Synthetic data, CPU only; skipped (pass) on a CPU without AVX2.
+// Synthetic data, CPU only; skipped (pass) on a CPU without AVX.
#include "strata/kernels/cpu/expert.hpp"
-
+#include "strata/kernels/cpu/expert_layout.hpp"
#include <cmath>
#include <cstdint>
#include <cstdio>
@@ -23,6 +25,16 @@ int main() {
std::printf("q2_bitplane_parity: STRATA_Q2_BITPLANE=1 is not set\n");
return 1;
}
+ if (!c::cpu_avx1_ok()) { std::printf("q2_bitplane_parity: no AVX on this CPU, skipped\n"); return 0; }
+ const bool avx2 = c::cpu_avx2_ok(); // the pair under test: AVX-2 where present, AVX1 below it
+ auto quant = [&](const float* x, int n, c::ActQ& q) { avx2 ? c::act_quant_q8_1_avx2(x, n, q)
+ : c::act_quant_q8_1_avx1(x, n, q); };
+ auto rows_bp_k = [&](const uint8_t* w, size_t rb, int nb, const c::ActQ* const* a, int nt, float* const* o,
+ int r0, int r1) { avx2 ? c::q2_0_gguf_rows_multi_avx2(w, rb, nb, a, nt, o, r0, r1)
+ : c::q2_0_gguf_rows_multi_avx1(w, rb, nb, a, nt, o, r0, r1); };
+ auto rows_lg = [&](const uint8_t* w, size_t rb, int nb, const c::ActQ* const* a, int nt, float* const* o,
+ int r0, int r1) { avx2 ? c::q2_0_gguf_rows_multi_avx2_legacy(w, rb, nb, a, nt, o, r0, r1)
+ : c::q2_0_gguf_rows_multi_avx1_legacy(w, rb, nb, a, nt, o, r0, r1); };
std::mt19937 rng(706);
std::normal_distribution<float> nd(0.f, 1.f);
int fail = 0;
@@ -43,7 +55,7 @@ int main() {
std::vector<std::vector<float>> xs(NT, std::vector<float>((size_t) n));
for (int t = 0; t < NT; ++t) {
for (float& v : xs[(size_t) t]) v = nd(rng) * (t % 3 == 0 ? 8.f : 1.f);
- c::act_quant_q8_1_avx2(xs[(size_t) t].data(), n, acts[t]);
+ quant(xs[(size_t) t].data(), n, acts[t]);
if (acts[t].bp_pairs != nblocks / 2) { std::printf("FAIL n=%d: no bit-plane image\n", n); ++fail; }
}
const c::ActQ* ap[8];
@@ -53,13 +65,13 @@ int main() {
float* wo[8]; float* ro[8]; float* ao[8]; float* co[8];
for (int t = 0; t < NT; ++t) { wo[t] = win[(size_t) t].data(); ro[t] = ref[(size_t) t].data();
ao[t] = alone[(size_t) t].data(); co[t] = cut[(size_t) t].data(); }
- c::q2_0_gguf_rows_multi_avx2(w.data(), row_bytes, nblocks, ap, NT, wo, 0, rows);
- c::q2_0_gguf_rows_multi_avx2_legacy(w.data(), row_bytes, nblocks, ap, NT, ro, 0, rows);
+ rows_bp_k(w.data(), row_bytes, nblocks, ap, NT, wo, 0, rows);
+ rows_lg(w.data(), row_bytes, nblocks, ap, NT, ro, 0, rows);
for (int t = 0; t < NT; ++t) { // one token at a time
- c::q2_0_gguf_rows_multi_avx2(w.data(), row_bytes, nblocks, ap + t, 1, ao + t, 0, rows);
+ rows_bp_k(w.data(), row_bytes, nblocks, ap + t, 1, ao + t, 0, rows);
}
for (int r0 = 0; r0 < rows; r0 += 5) // rows cut into ranges of five
- c::q2_0_gguf_rows_multi_avx2(w.data(), row_bytes, nblocks, ap, 3, co, r0, r0 + 5 < rows ? r0 + 5 : rows);
+ rows_bp_k(w.data(), row_bytes, nblocks, ap, 3, co, r0, r0 + 5 < rows ? r0 + 5 : rows);
for (int t = 0; t < NT; ++t)
for (int r = 0; r < rows; ++r) {
const float a = win[(size_t) t][(size_t) r], b = ref[(size_t) t][(size_t) r];
@@ -82,7 +94,7 @@ int main() {
static c::ActQ a;
std::vector<float> x((size_t) n);
for (float& v : x) v = nd(rng);
- c::act_quant_q8_1_avx2(x.data(), n, a);
+ quant(x.data(), n, a);
if (a.bp_pairs != 0) { std::printf("FAIL: an odd block count got an image\n"); ++fail; }
}
std::printf(fail ? "FAIL (%d)\n" : "PASS\n", fail);