Repository navigation
[Engine] Inkling 2: AVX-512 VBMI IQ3_S dot product for CPU experts, about 2x on Zen 4 - #220
Open
okada-shogo wants to merge 2 commits into
Open
okada-shogo wants to merge 2 commits into
okada-shogo wants to merge 2 commits into
Conversation
A new tier, Isa::Avx512Vbmi (AVX-512 F/DQ/BW/VL + VNNI + VBMI, with the OS's AVX-512 state: Zen 4 and later, Ice Lake and later), holds one kernel, src/quant/x86/dot_avx512vbmi.cpp's IQ3_S x Q8_K. It takes a block's 64 grid words from byte lookups instead of 64 scalar loads: each byte of the 512 grid words is a 512-byte table in four 128-byte quarters, vpermi2b looks up the 64 indices in a quarter at once, and the index's bit 7 and its 9th bit (qh, read as a mask) pick the quarter by masked blends. The indices go in an order that makes the unpacked byte planes the words in their order. Signs are a mask register (0 - q8), and maddubs + vpdpwssd by the sub-block scales. The int32 lane sums are the AVX2 kernel's and the float steps its own, so the bits are the Avx2 contract's (ggml's AVX2 ones). The tables are built at compile time, so no code of this file runs on other CPUs. Dispatch picks it as the highest level where the CPU has VBMI; other types are unchanged (Avx512 and lower kernels still win for them). STRATA_FORCE_ISA=avx2 turns it off with the rest of AVX-512; STRATA_FORCE_AVX2=1 turns VBMI off only, so the existing avx512_iq3_s_q8_K runs (for A/B). Class: same. Ported from our cpu_lowbit.cpp kernel (c142b89 on our inkling2-iq3s-avx512, a ggml-cpu build): there, on a Ryzen 7 7800X3D, 0 of 26,000 dots differed from ggml's, and from memory at 8 threads it gave 40.5 GB/s against 25.1 for the AVX2 one and 21.0 for ggml's. Here (Zen 2, no AVX-512): this file compiled on SIMDe's emulated AVX-512 against the layer's avx2_iq3_s_q8_K, 0 of 32,000 dots differ (n = 256 / 2304 / 4096, quantized and raw -128..127 activations); a swapped scale nibble or lookup order is caught. quant_selftest and quant_parity pass (the new kernel is skipped on this CPU). The run on the 7800X3D (quant_parity against the golden digests, quant_bench against avx2_ and avx512_iq3_s_q8_K) is pending. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The constexpr Iq3sPlanes constructor reads iq3s_grid, a plain static const table, so it is not a constant expression: GCC took it as an extension, but clang++ stops with "constexpr constructor never produces a constant expression" and "constexpr variable 'kIq3sPlanes' must be initialized by a constant expression" (icpx, which the SYCL build uses, and clang-cl the same; MSVC likely too). The planes are now a function-local static of dot_iq3_s_q8_K, built on the first call, which only a CPU with VBMI makes: no code of this AVX-512 file runs at start-up (the rule check_isa_guard.py states). Iq3sOrder reads no table and stays constexpr. Same bits. clang++ 14 and g++ 11.4 -fsyntax-only -Wall -Wextra: clean (clang++ failed before). A clang 14 build of strata_quant and quant_selftest links; quant_selftest PASS with both compilers, quant_parity PASS (GCC); this Zen 2 machine has no VBMI, so the kernel itself is not run here. Fix found by an independent review of the PR. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
okada-shogo
marked this pull request as ready for review
November 23, 2026 00:03
okada-shogo
added a commit
to okada-shogo/PolyStrata
that referenced
this pull request
Nov 23, 2026
…product for CPU experts, about 2x on Zen 4 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem and result
For CPUs with AVX-512 VBMI (Zen 4 and later, Ice Lake and later), when IQ3_S experts run on the CPU. That is
Inkling 2 Mini's IQ3_S files (e.g. AD-IQ3_S) on a card that does not hold every expert. It adds an IQ3_S dot-product
kernel that is about 2x faster than main's AVX-512 one, with the same bits as the AVX2 kernel.
Decode +31% to +32% with all experts on the CPU, +15% to +22% with some experts on the GPU (Ryzen 7 7700, first
table). On by default for every IQ3_S model on such a CPU;
STRATA_FORCE_AVX2=1turns it off.Decode speed, this PR alone against main, in tokens per second (tok/s), higher is better. RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 (Zen 4), AD-IQ3_S, 256 tokens written, mean of 3 interleaved runs. main = main's binary (it runs
avx512_iq3_s_q8_Kthere). "Some experts on the GPU" =--expert-swaps 0 --vram-reserve-mib 5120; "placement fixed at load" addsSTRATA_FREEZE_EXPERTS=1.Token ids are identical to main's in every row except "decode after a 302-token prompt, some experts on the GPU" without a fixed placement, where every
engine's runs differ run to run, main's too (see Validation); the "placement fixed at load" rows check that case.
Time the CPU spends on experts per written token, in milliseconds (ms), lower is better. Same card, model and runs, median of the 3 runs:
Kernel alone on the same CPU, IQ3_S dot-product throughput in GB/s of weights read, higher is better.
quant_bench --type iq3_s --n 2304, 1 thread, mean of 5 runs (each run under Validation):The AVX2 kernel gives 5.73 GB/s there (mean of the same 5 runs).
What it changes, in plain words: IQ3_S experts on the CPU are slow on Zen 4. Main's
avx512_iq3_s_q8_Kloads the64 grid words of a block one by one, and on a Ryzen 7 7800X3D it is no faster than the AVX2 kernel. This PR adds an
IQ3_S x Q8_K kernel for CPUs with AVX-512 VBMI. It gives the same bits as the AVX2 kernel and is about 2x faster.
IQ3_S dot-product throughput, in GB/s of weights read, higher is better.
quant_bench --type iq3_s, rows of 2304 read from DRAM, Ryzen 7 7800X3D:Inkling 2 Mini's IQ3_S files (e.g. AD-IQ3_S) use it for CPU experts. Every other IQ3_S user on such a CPU gets it too.
Related discussion
Split out of #200, as the maintainer asked there (one optimization per PR):
#200 (comment)
Not stacked: it is based on main, independent of the AVX2 IQ3_S PR (#221). Both touch the quant dispatch, so whichever merges
second may need a small rebase.
Approach
src/quant/x86/dot_avx512vbmi.cppand a new ISA levelIsa::Avx512Vbmi(AVX-512 F/DQ/BW/VL + VNNI + VBMI, withthe OS's AVX-512 state). Only IQ3_S has a kernel there; every other type keeps its kernel.
vpermi2bbyte lookups (4 byte planes x 4 quarters of 128 entries) instead of64 scalar loads. Signs are a mask register, scales go through
vpdpwssd. Integer lane sums and float steps are theAVX2 kernel's, so the bits are the Avx2 contract's.
this file runs at start-up or on a CPU without VBMI. (The first version built them
constexpr, which only GCC accepts.)best_isa()now returns the new top level, so the default IQ3_S kernel changes for every model there,not only Inkling 2. Same bits, so this follows the default-path rule.
STRATA_FORCE_AVX2=1turns VBMI off only (main'savx512_iq3_s_q8_Kruns).STRATA_FORCE_ISA=avx2turns off all ofAVX-512, as before.
Validation
Passed:
quant_selftest78 kernels, 0 failures;quant_parity --type iq3_s18,817 comparisons, 0 failures;quant_parity(all types) 38,401 comparisons, 0 failures.quant_bench --type iq3_s: every kernel checked row by row against the others, no difference; speed above.avx2_iq3_s_q8_K: 0 of32,000 dots differ (n = 256 / 2304 / 4096, quantized and raw -128..127 activations). A swapped scale nibble or lookup
order gives 32,000 of 32,000 different, so the check catches mistakes.
quant_selftestandquant_paritypass (the new kernel is skipped there; nothing else moves). Again at the headwith GCC 11.4:
quant_selftest66 kernels PASS,quant_parity31,489 comparisons, 0 failures.clang++14-fsyntax-only -Wall -Wextraofdot_avx512vbmi.cppwith the file's AVX-512 flags is clean at the head(the first commit failed: "constexpr variable 'kIq3sPlanes' must be initialized by a constant expression"). A clang 14
build of
strata_quantandquant_selftestlinks, andquant_selftestpasses.strata-poly, main's way, no llama.cpp): table above. On the same boxquant_selftest(78 kernels, best isa avx512vbmi),STRATA_FORCE_AVX2=1 quant_selftest(77 kernels, best isa avx512) andquant_parity --type iq3_s(18,817 comparisons)pass with 0 failures;
quant_benchfinds no difference between the three kernels.commit only changes when the planes are built, not their values or the kernel.
Each run (tok/s)
strata-poly --model Inkling-2-Mini-Uncensored-AD-IQ3_S.gguf --tokens <302 or 8192 ids> --max-new 256 --max-context 32768 --kv fp16(greedy), plus
--no-gpu-experts(all on the CPU) or--expert-swaps 0 --vram-reserve-mib 5120(a fixed part on the card).One process a run, warm-ups not counted, 3 rounds with the engines interleaved (order reversed every other round).
Engines: main's binary; this branch by default (vbmi); this branch with
STRATA_FORCE_AVX2=1(avx512); withSTRATA_FORCE_ISA=avx2(avx2). Decode tok/s of each run, higher is better (Zen 4); the "placement fixed at load" rows are the rerun described below:STRATA_FORCE_AVX2=1(main's avx512 kernel)STRATA_FORCE_ISA=avx2(AVX2 kernel)STRATA_FORCE_AVX2=1(main's avx512 kernel)STRATA_FORCE_ISA=avx2(AVX2 kernel)STRATA_FORCE_AVX2=1(main's avx512 kernel)STRATA_FORCE_ISA=avx2(AVX2 kernel)STRATA_FORCE_AVX2=1(main's avx512 kernel)STRATA_FORCE_ISA=avx2(AVX2 kernel)Token ids: all 12 runs of each all-CPU case are identical, and all 12 runs of "part on the card, 8192". In "part on the
card, 302" each of the 12 runs wrote different tokens, main's three runs among them (they part after 15 to 79 tokens):
this kit did not freeze the expert split (
STRATA_FREEZE_EXPERTS), so that row says nothing about identity, and its speedis from different token streams. So the part-on-card case was run again with
STRATA_FREEZE_EXPERTS=1(main vs this,3 rounds interleaved after a warm-up, same box; the "frozen" rows above). All 6 runs of each prompt write the same ids. (Frozen is slower than unfrozen: experts that
arrive after the first token are computed on the CPU.)
Idle 1-thread
quant_bench --type iq3_s --n 2304, 5 interleaved rounds, main's binary vs this branch's (load ~1), IQ3_S dot-product throughput in GB/s, higher is better:On the same box the AVX2 PRs
(#218, #219, #221) also pass
quant_selftest/quant_parity.Failed:
Not run:
Limits
DotKernel::onandiq3s_own_on()(STRATA_CPU_IQ3S=0). The two merge without a text conflict,but this PR's
avx512vbmi_iq3_s_q8_Kentry would have noon, soSTRATA_CPU_IQ3S=0would not turn it off on Zen 4.Whichever lands second should give the entry
.on = iq3s_own_on(main has noonfield yet, so it cannot be set here).STRATA_FORCE_AVX2=1means "no VBMI" here, whileexpert_layout.cppreads it as "no AVX-512". Bits are the same either way.Credit: our kernel from #200's branch (inkling2-iq3s-avx512), written for VecSzn's quant layer.
🤖 Generated with Claude Code