From 2a669f3c29c97b7d22fba5a32822b2bf1ea683df Mon Sep 17 00:00:00 2001 From: Alex Sadleir Date: Sun, 11 Oct 2026 23:08:05 +1100 Subject: [PATCH] cpu: the AVX2 router dot keeps its accumulators in registers (2.1x on a Zen 3) bf16_rows_dot_multi held its per-token sums in __m256 acc[MAXT] indexed by a variable. With eight 256-bit accumulators live GCC also unrolled the column loop and left the whole array in memory, so every token's FMA read and wrote a stack slot (vmovaps 0xc0(%rsp) ... vfmadd213ps 0xe0(%rsp)). On a Ryzen 7 5700X3D one layer's router [512 x 2560] at 4 tokens took 0.41 ms against 0.25 ms for the AVX1 copy in kq_avx1.cpp that kept its accumulators in registers, so every AVX2 CPU ran the slower of its two kernels (router_dot_parity --cpu 2, median of 3). Pragma GCC unroll 1 on the column loop changes nothing; the variable-indexed array is what forces the array into memory. Four tokens per pass, in four named accumulators. A row above four tokens is read a second time, from L1: nt=8 measures 0.37 ms, the same as the spilled form. On the same CPU the AVX2 kernel goes 0.41 -> 0.19 ms at 4 tokens and nt=2 0.36 -> 0.16 ms, and it now runs 1.3x ahead of the AVX1 copy instead of 1.6x behind it. The loop body carries no stack slot any more (was 21 memory ops per iteration). Untouched: the arithmetic and the reduction. Each token's FMA chain still runs over the same columns in the same order and ends in the same hsum_float_8, so every sum is bit-identical - the output for nt 1/2/4/8 at cols 2560/5120 hashes the same before and after (FNV-1a over the whole out[]), and router_dot_parity reports 0 failures with the AVX2 rel error unchanged at 3.16e-07. Left alone: the expert row kernels (iq_avx2.cpp, q2_avx2.cpp, kq256_gu_rows, kq256_rows) and the AVX1 path. The iq_avx2_parity bench over four format pairs x 1..8 tokens moved within 1.6% (noise), and the CPU parity tests (8 targets) pass. The router stays what it was, an estimate for which experts to prefetch. CUDA and HIP do not compile this file; the SYCL port does, and it builds there under icpx 2026.1 with no new warnings and no spills either. The AVX1 copy in kq_avx1.cpp kept a comment saying it reads its rows "exactly as the AVX2 kernel does"; with the row loop chunked four tokens at a time that is no longer the AVX2 kernel's shape, so the clause is dropped there. Comment only - no code change. --- src/kernels/cpu/kq_avx1.cpp | 2 +- src/kernels/cpu/kq_avx2.cpp | 32 ++++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/kernels/cpu/kq_avx1.cpp b/src/kernels/cpu/kq_avx1.cpp index fbdf73f4..738d94cd 100644 --- a/src/kernels/cpu/kq_avx1.cpp +++ b/src/kernels/cpu/kq_avx1.cpp @@ -43,7 +43,7 @@ inline __m256 bf16x8_to_f32x8_avx1(const uint16_t* p) { } // namespace void bf16_rows_dot_multi_avx1(const uint16_t* w, int rows, int cols, const float* x, int nt, float* out) { - // each row is read once for all `nt` (<= 8) tokens, exactly as the AVX2 kernel does + // each row is read once for all `nt` (<= 8) tokens for (int r = 0; r < rows; ++r) { const uint16_t* wr = w + (size_t) r * (size_t) cols; __m256 acc[8]; diff --git a/src/kernels/cpu/kq_avx2.cpp b/src/kernels/cpu/kq_avx2.cpp index f7228732..9dd251ca 100644 --- a/src/kernels/cpu/kq_avx2.cpp +++ b/src/kernels/cpu/kq_avx2.cpp @@ -200,17 +200,29 @@ void dot_rows(int type, const uint8_t* row, int n, const void* const* act, int n } // namespace void bf16_rows_dot_multi(const uint16_t* w, int rows, int cols, const float* x, int nt, float* out) { - // each row is read once for all `nt` (<= 8) tokens: the router (2.6 MB per layer) streams once per prediction - for (int r = 0; r < rows; ++r) { - const uint16_t* wr = w + (size_t) r * (size_t) cols; - __m256 acc[8]; - for (int t = 0; t < nt; ++t) acc[t] = _mm256_setzero_ps(); - for (int c = 0; c + 8 <= cols; c += 8) { - const __m256 wf = _mm256_castsi256_ps( - _mm256_slli_epi32(_mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i*) (wr + c))), 16)); - for (int t = 0; t < nt; ++t) acc[t] = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x + (size_t) t * cols + c), acc[t]); + for (int t0 = 0; t0 < nt; t0 += 4) { + const int m = nt - t0 < 4 ? nt - t0 : 4; + const float* x0 = x + (size_t) t0 * cols; + const float* x1 = x0 + cols; + const float* x2 = x1 + cols; + const float* x3 = x2 + cols; + for (int r = 0; r < rows; ++r) { + const uint16_t* wr = w + (size_t) r * (size_t) cols; + __m256 a0 = _mm256_setzero_ps(), a1 = _mm256_setzero_ps(), a2 = _mm256_setzero_ps(), + a3 = _mm256_setzero_ps(); + for (int c = 0; c + 8 <= cols; c += 8) { + const __m256 wf = _mm256_castsi256_ps( + _mm256_slli_epi32(_mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i*) (wr + c))), 16)); + a0 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x0 + c), a0); + if (m > 1) a1 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x1 + c), a1); + if (m > 2) a2 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x2 + c), a2); + if (m > 3) a3 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x3 + c), a3); + } + out[(size_t) t0 * rows + r] = hsum_float_8(a0); + if (m > 1) out[(size_t) (t0 + 1) * rows + r] = hsum_float_8(a1); + if (m > 2) out[(size_t) (t0 + 2) * rows + r] = hsum_float_8(a2); + if (m > 3) out[(size_t) (t0 + 3) * rows + r] = hsum_float_8(a3); } - for (int t = 0; t < nt; ++t) out[(size_t) t * rows + r] = hsum_float_8(acc[t]); // cols % 8 == 0 (2560) } }