mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3472] [Engine] Zen 1/2: build ggml-cpu without BMI2 pdep (IQ1_S/IQ1_M CPU dots 16-19x faster, same results) #4647
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#4647
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/3472
Author: @Licenser
Created: 10/20/2026
Status: 🔄 Open
Base:
main← Head:zen2-ggml-no-pdep📝 Commits (1)
c951794build: ggml-cpu without BMI2 pdep on a Zen 1 / Zen 2 build machine (STRATA_GGML_PDEP, IQ1_S / IQ1_M dots 16-19x faster there, same results)📊 Changes
4 files changed (+355 additions, -0 deletions)
View changed files
📝
CMakeLists.txt(+43 -0)📝
docs/DETAILS.md(+26 -0)➕
src/kernels/cpu/ggml_dot_bench.cpp(+161 -0)➕
tools/bench_ggml_pdep.py(+125 -0)📄 Description
Problem and result
On AMD Zen 1, Zen+ and Zen 2 (CPU family 17h, and Hygon's 18h) the BMI2 instruction
pdepruns in microcode: on theorder of a hundred cycles or more, against a few on Intel and Zen 3+. ggml-cpu's AVX2 IQ1_S and IQ1_M dot products
build their grid indices with
pdepwhenever__BMI2__is defined, which every-march=nativebuild on such a CPUdoes. There those two dots run 10-19x slower than the
#elsebranch beside them, which computes the same integersums with shifts and masks. Upstream GSQ-RCO IQ2_XS has IQ1_M gate/up experts in 3 of its 48 layers (8, 13, 37), so
on a Zen 2 PC those three layers take most of the CPU pool's time.
Measured on a Threadripper 3960X (Zen 2) + RTX 2080 Ti 11 GB, Linux, GCC 15.2, upstream IQ2_XS:
ggml_dot_bench, IQ1_S, ns per 256 weights (one thread)ggml_dot_bench, IQ1_M, ns per 256 weightskq_cpu_bench, one IQ1_M expert, ms per thread (24 threads, 1 / 2 tokens)The output does not change: both builds compute byte-identical results (Validation).
After this change, a native build on a Zen 1/2 build machine compiles ggml-cpu without
__BMI2__, so the two dotstake their existing
#elsebranch. Nothing changes anywhere else.Related discussion
issue: https://github.com/Niko1221/Strata/issues/5335
Approach
A new CMake option,
STRATA_GGML_PDEP(AUTO/ON/OFF, defaultAUTO):AUTO: in a native build (notSTRATA_PORTABLE, noSTRATA_ISA_FLOOR) whose build machine is AMD family 17h orHygon 18h (
/proc/cpuinfo, orPROCESSOR_IDENTIFIERon Windows), setsGGML_BMI2off and adds-U__BMI2__toggml-cpu (GCC and Clang:
-march=nativedefines__BMI2__whateverGGML_BMI2says). CMake prints a status linewhen this applies.
OFF: nopdepin any build.ON: never removed.pdepis fast; every other CPU; MSVC builds,which define
__BMI2__only throughGGML_BMI2(set only by the release build), so they already used the#elsebranch.
No ggml source is touched; the
#elsebranches are ggml's own code. Besidesggml_cpu_has_bmi2()(a report), thesetwo dots are ggml's only
__BMI2__code.Also adds
ggml_dot_bench(ggml-cpu's dot product per format, with a checksum of the results, CPU only, no model)and
tools/bench_ggml_pdep.py, which builds it with and withoutpdepand prints both side by side, so other PCscan measure.
docs/DETAILS.mddocuments the option.Validation
Passed:
python tools/bench_ggml_pdep.py --passes 2(this commit; 3960X, Linux, GCC 15.2): IQ1_S 379.3 -> 23.8 ns,IQ1_M 543.0 -> 28.6 ns per 256 weights. The 9 other formats (IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS, IQ3_S, IQ4_NL,
IQ4_XS, Q2_K, Q4_K) stay within 0.93-1.08x, and the checksums of all 11 formats are equal between the two builds.
objdumpof the engine built from this commit (CUDA sm_75,AUTOon the 3960X): 0pdep. Its two IQ1 dotfunctions are instruction-for-instruction those of the
-U__BMI2__engine used for the measurements below.kq_cpu_bench --pairs iq1_m/q2_0,iq2_s/q2_0 --threads 1,24 --nt 1,2: iq1_m/q2_0 7.53 -> 0.72 ms per expert perthread (24 threads, 1 token), 14.99 -> 1.42 (2 tokens). iq2_s/q2_0, which has no
pdep: 0.554 vs 0.571 ms.tools/ab_engine.py, 4 alternating rounds, upstream IQ2_XS,--expert-cache auto,--spec 4with the draftlayer,
STRATA_DECODE_TIMING=1: the engine rows of the table above (medians per request). Engines built from477ac13cplus local changes that are off by default, once as is and once with-DCMAKE_C_FLAGS=-U__BMI2__ -DCMAKE_CXX_FLAGS=-U__BMI2__, which gives the same object code as this option(see
objdumpabove).tools/quant_quality.py runwith a fixed placement (every expert on the CPU, no adaptation, so layers 8, 13 and37 run the IQ1_M dot): the two builds' outputs (4,995 positions: the target's logp, the argmax and the 64 most
likely tokens each) are byte-identical (sha256 245adafe...).
same tokens and the same draft and cache counters; with that same work, decode was 19-31% faster and short prompts
48-60%.
Failed:
Not run:
pdepis also microcoded, but not measured.AUTOdoes not apply there.tools/bench_ggml_pdep.pyshould show both builds close.PROCESSOR_IDENTIFIERbranch.Limits
6953d896); the other engines' release builds keeppdep, soZen 1/2 users of those binaries still pay for it. Release builds are out of scope here.
packs have none).
prices window sizes from measured round times, so a faster CPU picks other windows. With the windows held fixed
they are the same.
Disclaimer: I'm not a C++ dev / CMake is not a usual tool for me, this used Claude and I reviewed to the best of my ability
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.