mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #1949] [CLOSED] cpu(bug): the AVX2 dot kernels keep their accumulators in registers (2.1x router; +20-24% expert rows at 4 tokens) #3773
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#3773
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/1949
Author: @maxious
Created: 10/11/2026
Status: ❌ Closed
Base:
main← Head:fix/avx2-router-dot-spills📝 Commits (2)
fd0387fcpu: the AVX2 router dot keeps its accumulators in registers (2.1x on a Zen 3)6944471cpu: the AVX2 expert-row dots keep their accumulators in registers too📊 Changes
2 files changed (+62 additions, -24 deletions)
View changed files
📝
src/kernels/cpu/kq_avx1.cpp(+1 -1)📝
src/kernels/cpu/kq_avx2.cpp(+61 -23)📄 Description
Title
cpu(bug): the AVX2 dot kernels keep their accumulators in registers (2.1x router; +20-24% expert rows at 4 tokens)
Issue: none — no existing issue covers this (I searched upstream for router dot / AVX2 / spill / kq_avx2; the open CPU-pool ones, #921 #1549 #1913 #1923, are about other problems). Happy to file one and link it.
Summary
bf16_rows_dot_multi(the routing-aware prefetch's router dot,kq_avx2.cpp) held its per-token sums in__m256 acc[MAXT]indexed by a variable. With eight 256-bit accumulators live, neither GCC nor clang keeps them in registers — the array-indexed accumulator is a shape no mainstream compiler undoes by itself: GCC unrolls the column loop and leaves the whole array in memory, and clang spills it too (its object carries the same stack-slot FMA plus a per-rowmemset/memcpycall). Every token's FMA then read and wrote a stack slot. On a Ryzen 7 5700X3D that made the AVX2 kernel slower than the AVX1 copy written for pre-Haswell CPUs, and every AVX2 CPU calls the AVX2 one (expert_source.cpp:2271). The same effect measures on a Zen 5 Ryzen 7 9800X3D (2.06x on GCC, 3.2x on clang), so this is every AVX2 CPU on both compilers, not one GCC version's codegen on one core.Same sums, same tokens, four named accumulators instead of the array:
Ryzen 7 5700X3D (Zen 3, AVX2, no AVX-512, no AVX-VNNI), one layer's router
[512 experts x 2560 embeddings], 4 tokens, one thread pinned to core 2,router_dot_parity --cpu 2, median of 3:bf16_rows_dot_multibf16_rows_dot_multi_avx1(untouched)out[], nt 1/2/4/8 x cols 2560/5120)Ryzen 7 9800X3D (Zen 5, AVX2 + AVX-512), same bench, GCC 15.2.0 (the build's default compiler on this PC), one thread pinned to core 0, median of 3:
bf16_rows_dot_multibf16_rows_dot_multi_avx1(untouched)router_dot_parityfailuresThe compiler is not the variable — the array-indexed accumulator is. Both spill before the change (9800X3D, this file's exact flags
-O3 -DNDEBUG -mavx2 -mfma -mf16c, 4 tokens pinned to core 0):clang's unpatched object shows the same shape at the instruction level —
vfmadd213ps 0x60(%rsp,%rsi,1),%ymm0,%ymm1followed byvmovaps %ymm1,0x60(%rsp,%rsi,1)— so neither compiler recovers the registers itself, and on the Zen 5 box the clang build is slower still because of the per-rowmemset/memcpycall. With the accumulators in named registers both compilers land on the identical 0.124 ms, which is the point: the source shape, not the compiler, was the limit. It is not a flag problem either: the build carries no-march=native(the CPU kernel files get explicit per-file ISA flags), and adding it would not changekq_avx2.cpp's codegen.The expert-row dots: the same shape, the same fix
q5_1_dotandq8_0_dot(the Q5_1 and Q8_0 down-projection rows,kq_avx2.cpp) held their per-token sums in the same runtime-indexed__m256 acc[MAXT]. Both are nowtemplate <int NT>, dispatched 1..4 and chunked four tokens at a time past that - the same idiom the router change andkq256_gu_rowsalready use. Measured the same way (9800X3D, GCC 15.2.0,native_expert_parity --syntheticwith ggml-quantized weights and activations, one thread on core 0), at the verify window's four tokens:q5_1_dotq8_0_dotSums unchanged: 0 of 36 token-sets differ in any bit against ggml's own
vec_dotover 1..8 tokens, both before and after;native_expert_parity: 0 failures.Why
q4k_dotis left alone. The Q4_K gate/up dot keeps its runtime-nt array on purpose. Its token accumulators are not what spills - the unpatched function carries 8 ymm stack ops for the sums, against 24 (q5_1) and 59 (q8_0); what it spills is its weight-side unpack arrays (q4l/q4h/scale_l/scale_h), which token chunking does not touch and re-does. The same change measured +5% at four tokens and -4..-7% at six to eight, so it is a wash at the window and a small regression above it, and q4k is the gate/up format that runs most.What changed
src/kernels/cpu/kq_avx2.cpp:bf16_rows_dot_multi(the router), andq5_1_dot+q8_0_dot(the Q5_1 and Q8_0 down-projection rows); plus one comment line inkq_avx1.cpp.for (int t0 = 0; t0 < nt; t0 += 4)), the same idiomkq256_gu_rowsalready uses in this file, and each pass keeps its sums in four named__m256accumulators (a0..a3) instead of a variable-indexed__m256 acc[MAXT]. Four live accumulators fit in registers alongside the weight vector, so the column loop can unroll without the array going to memory — under GCC and clang alike.#pragma GCC unroll 1on the column loop is not part of the change: it changes nothing. GCC keeps the column unroll and the spills either way; the variable-indexed array is what forces the array into memory.What it leaves alone. 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 AVX1 kernel's code,q4k_dot(see above), the other expert row kernels (iq_avx2.cpp,q2_avx2.cpp,kq256_gu_rows,kq256_rows) and the engine's dispatch are untouched — the singlekq_avx1.cppline is its comment only, which had said the AVX1 kernel reads its rows "exactly as the AVX2 kernel does" and is no longer true once the AVX2 kernel chunks its row loop four tokens at a time. The router stays what it was — an estimate for which experts to prefetch, not part of the model's output.Extra Notes
How I checked:
out[]hashes the same (FNV-1a) before and after for nt 1/2/4/8 at cols 2560 and 5120, 8 of 8.router_dot_parityreports 0 failures with the AVX2 rel error against the double reference unchanged at 3.16e-07.ctest -R 'router_dot|iq_avx|q8k_quant|q2_bitplane'— 100% tests passed, 0 failed out of 8.iq_avx2_parity --benchrun over four format pairs x 1..8 tokens (255 MB of experts streamed from DRAM, one thread) moved within 1.6% across all 64 medians, which is noise; the expert path does not call this function.native_expert_parity --synthetic q4_K/q5_1andq4_K/q8_0report0 of 36 token-sets differin any bit and 0 failures over 1..8 tokens;iq_avx2_parity,iq_avx1_parity,q8k_quant_parityandrouter_dot_parityall exit 0. One thread pinned to core 0 throughout.sycl/CMakeLists.txt:176), andstrata_kernels_cpubuilds there under icpx 2026.1 (exit 0, no new warnings, and its object has no spills either). The expert-row change added since has not been through that SYCL build (no icpx on the measuring machine); it is the same file and the same kind of change.The benchmark, from a source build (CMake fetches the pinned llama.cpp commit for ggml-cpu when
STRATA_GGML_DIRis not given):This change, one thread pinned to one core (the first table is core 2 on the 5700X3D, the second core 0 on the 9800X3D; any core works, the medians move a little):
Both print
router_dot_parity: 0 failures. The compiler comparison, in a second build folder with the same file flags:The expert rows, for the no-regression check:
--cpu Npins inside the tool as well,--reps N(default 5) sets the repetitions behind each median,--pairs iq3_s/iq4_nl,...picks the format pairs and--mb 256the expert bytes streamed from DRAM per pair.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.