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: IQ3_S and IQ2_S AVX2 decode without per-index shift/mask/or

Niko1221d6850f31ee06
diff --git a/src/kernels/cpu/iq_avx2.cpp b/src/kernels/cpu/iq_avx2.cpp
index da15e0032..ca722a582 100644
--- a/src/kernels/cpu/iq_avx2.cpp
+++ b/src/kernels/cpu/iq_avx2.cpp
@@ -66,6 +66,21 @@ inline __m256i sgn_vec(uint32_t m) {
return _mm256_or_si256(nz, _mm256_set1_epi8(1));
}
+// The same sign vector read in place from the half's four sign bytes (IQ3_S, IQ2_S): the bytes come in as one
+// broadcast load (vpbroadcastd from memory, a load uop) and ONE pshufb expands them, instead of sgn_vec's two
+// pshufb + inserti128 - three uops on the single shuffle port of Haswell-class cores, one here.
+inline __m256i sgn_vec_at(const uint8_t* p) {
+ const __m256i bits = _mm256_shuffle_epi8(_mm256_set1_epi32((int) u32(p)),
+ _mm256_setr_epi8(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));
+ const __m256i sel = _mm256_setr_epi8(1, 2, 4, 8, 16, 32, 64, (char) 0x80,
+ 1, 2, 4, 8, 16, 32, 64, (char) 0x80,
+ 1, 2, 4, 8, 16, 32, 64, (char) 0x80,
+ 1, 2, 4, 8, 16, 32, 64, (char) 0x80);
+ const __m256i nz = _mm256_cmpeq_epi8(_mm256_and_si256(bits, sel), sel);
+ return _mm256_or_si256(nz, _mm256_set1_epi8(1));
+}
+
// The two 16-value scales of one 32-value half (IQ2_XS, IQ2_S): int16 lanes 0-7 = a (values 0-15),
// 8-15 = b (values 16-31). NOT a broadcast dword [a,b] - that would alternate the scales lane by lane.
inline __m256i sc16(int a, int b) {
@@ -103,6 +118,54 @@ static constexpr EvenSigns even_signs{};
static_assert(even_signs.v[0] == 0x0101010101010101ull && even_signs.v[1] == 0xFF010101010101FFull,
"keven_signs_q2xs: byte k = 0xFF when bit k of ksigns_iq2xs[i] is set");
+// IQ3_S / IQ2_S grid indices: the high index bits of a 32-value half (one qh byte) spread one index per byte,
+// so a single punpcklbw puts them next to the low index bytes and every grid lookup is a 16-bit load plus the
+// table load - instead of a shift, a mask and an or per index. IQ3_S: byte k = bit k (the 9th bit of index k).
+// IQ2_S: byte k = bits 2k,2k+1 (bits 8-9 of index k), k < 4. constexpr for the same reason as EvenSigns.
+struct HiSpread {
+ uint64_t iq3s[256];
+ uint64_t iq2s[256];
+ constexpr HiSpread() : iq3s{}, iq2s{} {
+ for (int h = 0; h < 256; ++h) {
+ uint64_t r3 = 0, r2 = 0;
+ for (int k = 0; k < 8; ++k) r3 |= (uint64_t) ((h >> k) & 1) << (8 * k);
+ for (int k = 0; k < 4; ++k) r2 |= (uint64_t) ((h >> (2 * k)) & 3) << (8 * k);
+ iq3s[h] = r3;
+ iq2s[h] = r2;
+ }
+ }
+};
+static constexpr HiSpread hi_spread{};
+static_assert(hi_spread.iq3s[0x81] == 0x0100000000000001ull && hi_spread.iq2s[0xE4] == 0x0000000003020100ull,
+ "hi_spread: one high-bit group per index byte");
+
+// The (2s+1) scale vectors as tables (IQ3_S: one 4-bit scale per 32-value half; IQ2_S: one scale byte holding
+// the two 16-value scales of the half) - one load instead of the shift/or/broadcast chain of sc32()/sc16().
+struct ScaleVecs {
+ int16_t s32[16][16]; // all 16 lanes = 2s+1
+ int16_t s16[256][16]; // lanes 0-7 = 2*(b&15)+1, lanes 8-15 = 2*(b>>4)+1 (the layout sc16() builds)
+ constexpr ScaleVecs() : s32{}, s16{} {
+ for (int s = 0; s < 16; ++s)
+ for (int l = 0; l < 16; ++l) s32[s][l] = (int16_t) (2 * s + 1);
+ for (int b = 0; b < 256; ++b)
+ for (int l = 0; l < 8; ++l) {
+ s16[b][l] = (int16_t) (2 * (b & 15) + 1);
+ s16[b][l + 8] = (int16_t) (2 * (b >> 4) + 1);
+ }
+ }
+};
+static constexpr ScaleVecs scale_vecs{};
+static_assert(scale_vecs.s32[15][0] == 31 && scale_vecs.s16[0x2F][7] == 31 && scale_vecs.s16[0x2F][8] == 5,
+ "scale_vecs: 2s+1, low nibble in lanes 0-7");
+
+inline __m256i scale_vec(const int16_t* lanes) { return _mm256_loadu_si256((const __m256i*) lanes); }
+
+// The low index bytes q[0..7] interleaved with the spread high bits: eight 16-bit grid indices in sp[].
+inline void grid_indices(const uint8_t* q, uint64_t hi, uint16_t* sp) {
+ _mm_storeu_si128((__m128i*) sp, _mm_unpacklo_epi8(_mm_cvtsi64_si128((long long) u64(q)),
+ _mm_cvtsi64_si128((long long) hi)));
+}
+
inline float hsum8(__m256 v) {
const __m128 lo = _mm256_castps256_ps128(v), hi = _mm256_extractf128_ps(v, 1);
__m128 s = _mm_add_ps(lo, hi);
@@ -169,17 +232,13 @@ template <> struct Fmt32<22> { // IQ2_S: d, qs[64] (32 grid bytes, 32 sign byt
static constexpr int bytes = 82;
static constexpr float K = 0.125f;
static inline void decode(const uint8_t* b, int j, int half, __m256i& g, __m256i& sgn, __m256i& sc) {
- const uint8_t* qs = b + 2 + 8 * j;
- const uint8_t h = b[66 + 2 * j + half];
- const int o = 4 * half;
- g = _mm256_set_epi64x((long long) iq2s_grid[qs[o + 3] | ((h << 2) & 0x300)],
- (long long) iq2s_grid[qs[o + 2] | ((h << 4) & 0x300)],
- (long long) iq2s_grid[qs[o + 1] | ((h << 6) & 0x300)],
- (long long) iq2s_grid[qs[o] | ((h << 8) & 0x300)]);
- const uint64_t m = u64(b + 2 + 32 + 8 * j);
- sgn = sgn_vec(half ? (uint32_t) (m >> 32) : (uint32_t) m);
- const uint8_t sb = b[74 + 2 * j + half];
- sc = sc16(2 * (sb & 15) + 1, 2 * (sb >> 4) + 1);
+ // grid_indices reads 8 index bytes; only the first 4 are this half's (the rest stay inside the block)
+ alignas(16) uint16_t sp[8];
+ grid_indices(b + 2 + 8 * j + 4 * half, hi_spread.iq2s[b[66 + 2 * j + half]], sp);
+ g = _mm256_set_epi64x((long long) iq2s_grid[sp[3]], (long long) iq2s_grid[sp[2]],
+ (long long) iq2s_grid[sp[1]], (long long) iq2s_grid[sp[0]]);
+ sgn = sgn_vec_at(b + 2 + 32 + 8 * j + 4 * half);
+ sc = scale_vec(scale_vecs.s16[b[74 + 2 * j + half]]);
}
};
@@ -203,13 +262,14 @@ template <> struct Fmt32<21> { // IQ3_S: d, qs[64], qh[8], signs[32], scales[4
static inline void decode(const uint8_t* b, int j, int half, __m256i& g, __m256i& sgn, __m256i& sc) {
const uint8_t* q = b + 2 + 16 * j + 8 * half;
const uint32_t h = b[66 + 2 * j + half];
-#define G3(k) (int) iq3s_grid[q[k] | (((h >> k) & 1u) << 8)]
+ alignas(16) uint16_t sp[8];
+ grid_indices(q, hi_spread.iq3s[h], sp);
+#define G3(k) (int) iq3s_grid[sp[k]]
g = _mm256_set_epi32(G3(7), G3(6), G3(5), G3(4), G3(3), G3(2), G3(1), G3(0));
#undef G3
- const uint64_t m = u64(b + 74 + 8 * j);
- sgn = sgn_vec(half ? (uint32_t) (m >> 32) : (uint32_t) m);
+ sgn = sgn_vec_at(b + 74 + 8 * j + 4 * half);
const uint8_t s = b[106 + j];
- sc = sc32(half ? 2 * (s >> 4) + 1 : 2 * (s & 15) + 1);
+ sc = scale_vec(scale_vecs.s32[half ? s >> 4 : s & 15]);
}
};