mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #227] [CLOSED] AVX1: the Q2_0 expert kernels for CPUs that have AVX but no AVX2 #2512
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#2512
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/227
Author: @demetree
Created: 9/30/2026
Status: ❌ Closed
Base:
main← Head:strata-avx1-pr📝 Commits (3)
6f22d7dAVX1: the Q2_0 expert kernels for CPUs that have AVX but no AVX274a482cAVX1: the CANONICAL Q2_0 expert path, which is the one pool.cpp runsc7a0e36AVX1 tests: correct the AVX2 probe, and keep the /arch:AVX2 code out of the link📊 Changes
11 files changed (+1685 additions, -13 deletions)
View changed files
📝
CMakeLists.txt(+34 -1)📝
include/strata/kernels/cpu/expert.hpp(+22 -0)📝
include/strata/kernels/cpu/expert_layout.hpp(+31 -0)📝
src/kernels/cpu/expert_layout.cpp(+210 -3)📝
src/kernels/cpu/pool.cpp(+7 -7)➕
src/kernels/cpu/q2_avx1.cpp(+236 -0)➕
src/kernels/cpu/q2_avx1_parity.cpp(+382 -0)➕
src/kernels/cpu/s2_avx1_parity.cpp(+368 -0)➕
src/kernels/cpu/s2_avx512_stub.cpp(+98 -0)➕
src/kernels/cpu/s2_expert_avx1.cpp(+290 -0)📝
src/program/generate.cpp(+7 -2)📄 Description
What this adds
A third rung below the AVX2 one in the CPU expert ladder, for CPUs that have AVX but no AVX2 —
Sandy Bridge and Westmere Xeons, and the i7/i5 parts of the same vintage. They are still in service,
and today a pre-AVX-512 CPU either fails at startup or takes an illegal instruction.
Each rung is its own translation unit compiled for exactly that ISA, and the choice is made at run
time by the probes in
expert_layout.cpp. That containment is the point: a probe that returns thewrong answer costs speed, not a trap.
This was asked for on X, and built on a dual Xeon E5-2680 (Sandy Bridge-E) that happens to sit under
the CUDA floor — so it is contributed untested against a real model load, and the verification below
is kernel-level.
What it does not do
Worth being explicit, because it is the obvious first question:
capability 7.5, so a pre-Ampere card is refused regardless of CPU support. This PR is about the
CPU side only.
reduces eight float lanes and the AVX1 one reduces four, so the sums are grouped differently. The
two are not bit-identical to each other and are not expected to be; each is checked against the
scalar reference separately.
q2_avx1.cppdoes a software fp16 decode and a mul+add instead.Commits
2ed3b8c— the NATIVE-pack kernel (q2_avx1.cpp), thecpu_avx1_ok()probe, three-way dispatchin
q2_rows_any()/act_quant_any(), the startup gate, andq2_avx1_parity.d5eda68— the CANONICAL expert path (s2_expert_avx1.cpp), thes2_expert_*_anydispatchers, and
s2_avx1_parity. This path is not optional:generate.cppcalls the startup gatefor it unconditionally, and every function
pool.cppcalls lives in an/arch:AVX512translationunit — so the whole compute path needs a rung below AVX-512, not just the dequant.
1c2f8bc— two defects found in the above while validating, both described below.Why the tests here are self-contained
expert_parityneeds the 38 GB pack and an AVX-512 CPU, so it skips itself on exactly the hardwarethis PR targets. Without
q2_avx1_parity/s2_avx1_parity, a build of this would ship with thenew kernel untested. Both new tests synthesise their own expert blobs and check against an independent
longhand scalar reference, so they need neither the pack nor AVX-512 — they are the only tests in the
tree that run on a pre-AVX2 CPU.
Verification
Reference CPU: Xeon E5-2680, family 6 model 45 stepping 7 (Sandy Bridge-E) — no FMA3, no F16C, no
AVX2, no AVX-512, no BMI2. CPU-only build, MSVC 19.44, CMake 3.31.6, Ninja.
nt=2is bit-identical tont=1in both, which matters because the multi-token path is the one thatdecodes more than one token at a time.
ISA containment checked by disassembly of the built objects (
dumpbin), not by reading flags:The probe decisions were established by executing the candidate instructions on the reference CPU
(
vfmadd*andvcvtph2pseach raise0xC000001D,pmaddubswworks) rather than trusting CPUIDalone — which is what caught the AVX2 bug below.
Two bugs found in this work, both mine
Worth flagging because both were invisible to reading the code and only showed up when the tests were
actually run:
1.
cpu_avx2_ok()probed the wrong bit. It read leaf 1 ECX bit 5 and treated it as AVX2. Leaf 1ECX bit 5 is plain AVX (Sandy Bridge, 2011); AVX2 is leaf 7 subleaf 0 EBX bit 5 (Haswell, 2013). On a
CPU with AVX but no AVX2 it returned true, so the ladder would have handed it the
/arch:AVX2kernel onhardware that cannot execute it.
s2_avx1_paritycaught it by asserting the rung choice andreporting
avx2=1 avx1=1. Now reads leaf 7 EBX bit 5 with a max-leaf ≥ 7 guard.2. A BMI2 fault in a static initializer.
s2_avx1_parityexited0xC000001Dbeforemain()printed a line.
expert_layout_load()callsnative_fmt(), so linking the test againststrata_kernels_cpupulled innative_expert.cpp→iq_avx2.cpp, which is/arch:AVX2and containsBMI2 (
shlx). The reference CPU has no BMI2, and the fault was in a static initializer, so it ranbefore any dispatch mattered.
iq_avx2.cppdid not exist at the 0.1.20 base (df6980dadded it),which is exactly why the test passed before the rebase onto 0.1.28 and failed after.
Fixed by stubbing
native_fmtto break the link edge, in the same abort-on-call style as the existingAVX-512 stubs.
native_fmtis the one exception and returns false with a reason rather than aborting,because
expert_layout_load()legitimately calls it during setup; the other fivenative_*entrypoints abort, since reaching them means the AVX1 path was bypassed.
Notes for review
bbaaabb) — the current tip. No conflicts, despite that release touchingCMakeLists.txtandgenerate.cpp, which this also touches./arch:AVXflags are set per source file, matching howexpert.cppandq2_avx2.cpparealready handled. The MSVC branch deliberately omits
/mfmaand/mf16c; the GCC/Clang branchdeliberately omits
-mfmaand-f16c, because the target CPU has neither and enabling themproduces instructions that trap on the very machine this is written for.
since
expert.cppandiq_avx512.cppneedevex512. That is a toolchain limitation, not somethingin this branch; MSVC, which is what upstream targets, builds it fine.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.