[Engine] AVX1 Q2_0 rows: the bit-plane pair's float part on 256 bits, a branch-free scale conversion

- row_bp_acc: a pair's two 64-value blocks share one 256-bit cvt / mul / mul / add (block 0 the low lanes, block 1
  the high lanes - the 128-bit form's lanes side by side, so each lane runs the same operations in the same order).
- h2f_scale: a normal fp16 block scale in four integer operations (h2f's normal branch, bit for bit); zero,
  subnormals, inf and NaN still take h2f.  Used by the bit-plane and the legacy rows.

Same bytes: FNV hashes of both kernels' outputs on fixed random blocks at 1-8 tokens are identical before and after,
for STRATA_Q2_BITPLANE=1 and for the legacy rows (harness: src/kernels/cpu/q2_avx1_parity.cpp on
https://github.com/Keoian/Strata/tree/rx-5700-xt-optimizations, not part of this change).  q2_bitplane_parity passes.

kq_cpu_bench --pairs q2_0/q2_0 --mb 1024 --reps 3, i7-3770 (AVX, DDR3-1600), STRATA_Q2_BITPLANE=1, ms per expert per
thread at 1 / 3 / 4 tokens: 0.418 / 0.807 / 0.954 -> 0.297 / 0.597 / 0.752 (4 threads at 1 token: 13.0 -> 18.3 GB/s).
Legacy rows: 0.517 / 1.13 / 1.40 -> 0.487 / 1.11 / 1.38.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
KeoianandClaude Opus 5.5 committed 2026-10-30 15:31:06 +00:00
1 parent 43e390e0e3
commit 7a68664aca
1 file changed
+47 -27
+47 -27
View File
@@ -53,6 +53,18 @@ inline float h2f(uint16_t h) {
return out;
}
// h2f for a block scale: a normal fp16 (every real scale) in four integer operations, the same bits as h2f's normal
// branch (the exponent rebiased by 127 - 15 = 112, the mantissa shifted into place); zero, subnormals, inf and NaN take
// h2f. The bit-plane rows convert two scales per 128 values, so the branches h2f takes per call were ~10% of a row.
inline float h2f_scale(uint16_t h) {
const uint32_t e = h & 0x7C00u;
if (__builtin_expect(e == 0 || e == 0x7C00u, 0)) return h2f(h);
const uint32_t f = ((uint32_t) (h & 0x8000u) << 16) | (((uint32_t) (h & 0x7FFFu) << 13) + 0x38000000u);
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) {
@@ -87,7 +99,7 @@ inline void row_multi(const uint8_t* row, const ActQ* const* a, int nblocks, flo
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)));
const float d = h2f_scale((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) {
@@ -122,38 +134,46 @@ void rows(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a,
}
}
// ---- 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).
// ---- the bit-plane row kernel (STRATA_Q2_BITPLANE=1): the integer part on 128-bit lanes, one 64-value block at a
// time; the float part of a pair's two blocks in one 256-bit register (block 0 the low half, block 1 the high half:
// the lanes of the 128-bit form side by side, so each lane takes the same operations in the same order and the
// bytes are the 128-bit form's - q2_avx1_parity checks it). 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). Half the float instructions of the 128-bit form: on an i7-3770, 0.81 -> 0.66 ms per 3-token expert (0.60 with h2f_scale).
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(); }
inline void row_bp_acc(const uint8_t* row, const ActQ* const* a, int npairs, __m256* acc) {
for (int t = 0; t < NT; ++t) acc[t] = _mm256_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;
uint16_t dh0, dh1;
std::memcpy(&dh0, blk, 2); // the blocks' fp16 scales: bytes 0..1 (block 1 at +18)
std::memcpy(&dh1, blk + 18, 2);
const __m256 d = _mm256_insertf128_ps(_mm256_castps128_ps256(_mm_set1_ps(h2f_scale(dh0))), _mm_set1_ps(h2f_scale(dh1)), 1);
__m128i c[2][4];
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 __m128i b = _mm_loadu_si128((const __m128i*) (blk + 2 + h * 18)); // its 16 code bytes
c[h][0] = _mm_and_si128(b, m3);
c[h][1] = _mm_and_si128(_mm_srli_epi16(b, 2), m3);
c[h][2] = _mm_and_si128(_mm_srli_epi16(b, 4), m3);
c[h][3] = _mm_and_si128(_mm_srli_epi16(b, 6), m3);
}
for (int t = 0; t < NT; ++t) {
__m128i i32[2];
for (int h = 0; h < 2; ++h) {
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));
__m128i s = _mm_maddubs_epi16(c[h][0], _mm_load_si128((const __m128i*) q));
s = _mm_add_epi16(s, _mm_maddubs_epi16(c[h][1], _mm_load_si128((const __m128i*) (q + 32))));
s = _mm_add_epi16(s, _mm_maddubs_epi16(c[h][2], _mm_load_si128((const __m128i*) (q + 64))));
s = _mm_add_epi16(s, _mm_maddubs_epi16(c[h][3], _mm_load_si128((const __m128i*) (q + 96))));
i32[h] = _mm_sub_epi32(_mm_madd_epi16(s, ones),
_mm_load_si128((const __m128i*) (a[t]->psum + p * 8 + h * 4)));
}
const __m256 v = _mm256_mul_ps(
_mm256_cvtepi32_ps(_mm256_insertf128_si256(_mm256_castsi128_si256(i32[0]), i32[1], 1)),
_mm256_load_ps(a[t]->pscale + p * 8));
acc[t] = _mm256_add_ps(acc[t], _mm256_mul_ps(d, v));
}
}
}
@@ -169,9 +189,9 @@ inline float reduce1(__m128 a, __m128 b) { // ((v0+v1)+(v2+v3)) + ((v4+v5)+(v6
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];
__m256 acc[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]);
for (int t = 0; t < NT; ++t) out[t][r] = reduce1(_mm256_castps256_ps128(acc[t]), _mm256_extractf128_ps(acc[t], 1));
}
}