mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #43] [MERGED] kernels: AVX2 multi-token i-quant row kernels (an AVX2-only CPU decoded every i-quant row once per token) #2428
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#2428
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/43
Author: @pipeob0
Created: 9/28/2026
Status: ✅ Merged
Merged: 9/28/2026
Merged by: @Niko1221
Base:
main← Head:gh/avx2-kernels📝 Commits (3)
ed14227kernels: AVX2 multi-token i-quant kernels for an AVX2-only CPUe4a95caparity tool: guard the AVX-512 calls, and check the AVX2 kernels too61126e9generate: say which expert kernels an AVX2-only CPU gets📊 Changes
6 files changed (+545 additions, -32 deletions)
View changed files
📝
CMakeLists.txt(+4 -1)➕
include/strata/kernels/cpu/iq_avx2.hpp(+26 -0)➕
src/kernels/cpu/iq_avx2.cpp(+419 -0)📝
src/kernels/cpu/native_expert.cpp(+22 -5)📝
src/kernels/native_expert_parity.cpp(+71 -25)📝
src/program/generate.cpp(+3 -1)📄 Description
Why
An AVX2-only CPU (Ryzen 5000 / any X3D, Ice Lake, Rocket Lake) runs a native pack, but every i-quant
gate/up row goes through ggml's
ggml_vec_dot_iq*_q8_K, which decodes the row once per token. With--spec 4a verify window is 3-4 tokens over the same expert rows, so the decode ran 3-4 times fornothing.
iq_avx512.cppcovers 512-bit machines andq2_avx2.cppcovers Q2_0; the i-quants had no AVX2path at all.
This is also the concrete answer to #34 ("among AVX2, AVX512 and AVX-VNNI, which helps?"): on a native
pack AVX2 is enough, but only with these kernels — otherwise AVX2 leaves ~15-20% of the expert pool on the
table.
What
src/kernels/cpu/iq_avx2.cpp— multi-token row kernels, one decode per row for the whole window:Per token in the window: load,
_mm256_sign_epi8,maddubs,madd, add. That is the whole per-token cost.For IQ2_XS the decode follows ggml's own AVX2
vec_dotrather than a table lookup per sign group: the7-bit sign indices of all 16
u16of a 128-value group become sign vectors with two shifts, a xor, onepshufbagainst ggml'sbit_helperparity table and an or; the four 32-value sign vectors come out ofshuffles of that one register; the 8 scale bytes become all 16 half-scales with ggml's unpack trick plus the
k_shufflemask. The grid lookups stay scalar (ggml does the same). Alternating accumulators break theaddchain.even_signsis thekeven_signs_q2xsequivalent — that table isstaticinarch/x86/quants.cso itcannot be reused from here; it is built once at load time from
ksigns_iq2xs, so it cannot drift fromthe shared table (checked: bit 7 of
ksigns_iq2xs[i]ispopcount(i) & 1, which is exactly whatbit_helperreconstructs, so the u64 table and the parity trick agree).Dispatch is in
native_expert.cppnext to the existing AVX-512 one, withSTRATA_NO_IQ256(gate/up) andSTRATA_NO_IQ4NL(down) as per-format fallbacks to ggml, and no AVX-512 gate — so the path is exercisableon any AVX2 machine.
generate.cppnow prints which kernel is live, because the old line claimed AVX2kernels even when the code had fallen back to
vec_dot.Correctness
native_expert_parityagainst ggml'svec_dot, every layer of the pack: rel 3.1e-08 … 3.4e-08, 0failures. Full-expert rel vs the GPU (1.2e-2 cpu / 1.1e-2 gpu) is unchanged from before this PR, i.e. the
activation quantization still dominates and the kernels are equivalent to the reference.
Measured
Machine: Ryzen 7 5700X3D (Zen 3, AVX2 only, 8 cores), 64 GB DDR4-2666, RTX 5060 Ti 16 GB at PCIe 4.0 x8,
native
swift-iq3_xxspack,--spec 4.Microbench, one expert's rows, single thread, µs (3 tokens = one verify window):
vec_dotEnd to end, greedy fixed-prompt runs, median of 6 interleaved old/new pairs, metric ms/round (tok/s is
not usable here: draft acceptance moves the round count by ±5% between runs):
5 of the 6 pairs favoured the new kernels; the outlier is a run that routed the most experts of any run in
the set (5.67 vs 5.10-5.53 distinct per layer). The
downphase, which is byte-identical code in bothbuilds, also improved (-6%): a less CPU-saturated pool. On this box the whole engine went from 43-46 tok/s
as shipped to ~59 tok/s in a direct run.
Honest limits
stream), so further decode work will pay less than these numbers suggest; the next lever there is
streaming loads, not more unrolling.
verify window. A
--spec 1/ single-token decode run should not take this path, which is why thedispatch keeps the token-count gate.
Also in this PR: the parity tool was unusable on an AVX2-only CPU
native_expert_paritycalled the AVX-512 row kernels and the AVX-512 quantizer unconditionally, so onexactly the machine whose kernels you would want to validate it died with SIGILL before printing a single
line. Both calls are now behind
cpu_avx512_ok(). The tool also gained a block that runs these AVX2 kernelsagainst ggml's
vec_doton Q8_0 activations and times them, so a kernel change can be validated andmeasured without starting an engine.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.