Repository navigation
[Engine] Inkling 2: faster AVX2 IQ3_S dot product for CPU experts (same bits) - #221
Open
okada-shogo wants to merge 3 commits into
Open
okada-shogo wants to merge 3 commits into
okada-shogo wants to merge 3 commits into
Conversation
The Avx2 contract's IQ3_S x Q8_K kernel (ggml-cpu's AVX2 code, x86_dots.inc) is compute-bound on Zen 2: it builds the 9-bit grid indices in a vector, stores it, reads the 16 indices back one by one and makes each 8-word vector from eight table loads by vpinsrd. Inkling 2 Mini's IQ3_S files run their CPU experts through it. dot_avx2.cpp now has dot_iq3_s_q8_K_words: the same q8 sign flip, maddubs / madd by 2 ls + 1 lanes, int32 lane sums, one fmadd a block and hsum, so the same bits. Only the grid words are fetched another way: the 16 indices of two groups as 16-bit lanes (the 9th bits from a table by qh byte), moved to two general registers four at a time, split by shifts, each word read with vmovd and joined by unpacks. It is listed before the port, so dot_kernel() takes it on a CPU whose best level is AVX2 (AVX-512 CPUs keep avx512_iq3_s_q8_K). Every user of the layer's IQ3_S dot on such a CPU gets it, with the same bits. Dispatch: DotKernel gets `on` (null for the ggml ports). dot_kernel() and checked_dot_kernel() skip an own kernel whose on() is false and take the next of the contract; dot_kernels() still lists all. iq3s_own_on() reads STRATA_CPU_IQ3S once: STRATA_CPU_IQ3S=0 gives the port back. A later own IQ3_S kernel at another ISA can take the same switch. selfcheck's verdict key now has the kernel's place in the list, since two kernels can now share (type, contract, ISA). Class: same. quant_selftest compares every own kernel with the port of its contract to the bit: 0 of 32,500 IQ3_S dots differ (rows of 256, 2304 and 4096, five kinds of activation including raw codes with -128); a 1-ulp mutation of the kernel fails all of them. quant_parity (golden GCC 13.3 digests and the linked ggml-cpu) passes with both IQ3_S kernels. Speed: see the PR (quant_bench, one thread, and a 1-6 thread bench, Ryzen 5 3600XT). The idea of a faster IQ3_S CPU lane came from comparing with merbanan's inkling2-turing branch of Strata on his file; the code is our own (ported from our Inkling 2 engine, cpu_lowbit.cpp). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review follow-ups for the AVX2 IQ3_S kernel: - STRATA_CPU_IQ3S: only the exact value 0 turns the own kernel off (it was any value starting with '0'). The quant layer has no shared env-flag helper, so this is said at iq3s_own_on(). - quant_selftest: every activation row (all five kinds) gets a -128 code at a set place of each 32. Before, -128 came only from the raw random kind, one row per (n, kind); a saturating sign flip (subs_epi8 for sub_epi8) went unnoticed at n=256. With this change it fails at every n (9965 of 10000 dots differ at n=256). - dot_avx2.cpp: the two 64-bit moves (vmovq / vpextrq) go through lo64() / hi64(), which use memory on 32-bit x86. x86-64 code is unchanged. - selfcheck: the comments say the cached key includes the kernel's list position; registry.hpp keeps generic_dot_table and generic_act_table together. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tools/settings_register.py --check failed: the engine reads STRATA_CPU_IQ3S (engine/src/quant/dispatch.cpp) and the register did not list it. Add its entry as python tools/settings_register.py writes it, status rollback (0 gives back ggml's AVX2 IQ3_S port), and leave every other entry as it was. docs/SETTINGS.md is regenerated with --docs. That also adds three rollback switches that were registered on main without a docs rerun (STRATA_INK_FLASH, STRATA_POLY_BELL, STRATA_POLY_NV4_TILE). 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
…roduct for CPU experts (same bits) # Conflicts: # engine/src/quant/x86/dot_avx2.cpp 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 whose best instruction set is AVX2 (Zen 2, Zen 3 and similar), 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 a faster AVX2 dot
product for IQ3_S, with the same bits as the current one.
Decode up to +15.5% on Zen 2 (Ryzen 7 3700X, every expert on the CPU). Zen 3 gains little (−0.4% to +1.4%, within
run-to-run noise). On by default for every model that uses IQ3_S;
STRATA_CPU_IQ3S=0gives back the old kernel.Decode speed, this PR alone against main, in tokens per second (tok/s), higher is better. AD-IQ3_S, 256 tokens written, mean of 3 interleaved runs. Expert placement is fixed at load (
STRATA_FREEZE_EXPERTS=1) in every run, so both builds keep the same experts in VRAM. The token ids are identical to main's in every run.--vram-reserve-mib 1024)Decode speed on the RTX 2080 Ti box, in tok/s, higher is better. Same setup, median of 3 runs (its single runs were not kept):
Time the CPU spends on experts per written token, in milliseconds (ms), lower is better. Same cards, model and runs, median of the 3 runs:
What it changes, in plain words: Inkling 2 Mini's IQ3_S files run their CPU experts through the quant layer's
AVX2 dot product of IQ3_S weights with Q8_K activations (
avx2_iq3_s_q8_K, a port of ggml's). On Zen 2 that kernelis limited by its own computation, not by memory speed. This PR adds
avx2_iq3_s_q8_K_words, a faster AVX2 kernelwith the same bits as the port.
AVX2 gets the new kernel. This is on purpose: the quant layer is shared by all model families, and the results are
bit-identical to the port (checked below), so a gate would only hide the speed-up from other IQ3_S models.
avx512_iq3_s_q8_K(higher ISA level).STRATA_CPU_IQ3S=0gives back the ggml port. Only the exact value0turns it off. It is in the settings registeras a rollback switch.
quant_bench --type iq3_s --n 2304, 1 thread, 5 interleaved rounds: 4.03 GB/s vs2.88 GB/s for the ggml port (+40%), the same in every round.
Related discussion
Split out of #200, as the maintainer asked there:
#200 (comment)
Independent of the other split PRs. The AVX-512 VBMI IQ3_S PR (#220) is separate (not stacked on this one).
Approach
at a time (each read waits for the store). Then it builds each 8-word vector with eight
vpinsrdloads.qhbyte). It moves them to two general registers four at a time (no store), splits them with shifts, reads eachgrid word with
vmovdand joins the words with unpacks.maddubs/maddby2 ls + 1, int32 lane sums, onefmaddper block,the same horizontal sum), so the bits are the same.
Dispatch and the off switch
DotKernelhas a new fieldon(null for the ggml ports).dot_kernel()andchecked_dot_kernel()skip a kernelwhose
on()is false and take the next of the same contract.dot_kernels()still lists every kernel.iq3s_own_on()readsSTRATA_CPU_IQ3Sonce.(type, contract, ISA).
Validation
Revision f87ff76, Ubuntu 22.04, GCC 11.4, Ryzen 5 3600XT (AVX2, no AVX-512), Release CPU build with tests
(
quant_selftest quant_parity quant_benchtargets), unless stated.Passed:
quant_selftest: new kernel vsavx2_iq3_s_q8_K, bit for bit. n = 256 / 2304 / 4096; five kinds of activation (normal,tiny, one large value, every 7th large and negative, raw random codes); each activation row has a -128 code planted at
a fixed place in every 32. Result: 0 of 32,500 dots differ, PASS.
dot_kernel(IQ3_S)is the new kernel.STRATA_CPU_IQ3S=0 quant_selftest: PASS,dot_kernel(IQ3_S)is the ggml port.subs_epi8instead of
sub_epi8): FAIL at every n (n=256: 9,965 of 10,000 differ). A review found (b) passed at n=256 before the planting.quant_parity(all types, golden digests of GCC 13.3's ggml-cpu, plus the linkedggml-cpu, GCC 11.4): 32,065 comparisons, 0 rows differ, 0 failures, PASS.
quant_bench: every kernel of the contractgives the same bits, rc=0.
python3 tools/settings_register.py --check: rc=0.python3 tools/test_settings_register.py: 8 tests OK.python3 tools/check_vendored.py: ok.strata-polybuilt with CUDA 12.9 forsm_75 + sm_86, each box running nothing else. Every run's 256 token ids are identical to main's first run; the
default rows load the same expert split in every tree.
cpu-check-nativesettings, GCC 11.4, built on the box):quant_selftestprintsiq3_s takes avx512_iq3_s_q8_K (STRATA_CPU_IQ3S unset), so the dispatcher keeps the AVX-512kernel; 78 kernels, 32,500 own-kernel dots vs the port, 0 differ, PASS.
STRATA_CPU_IQ3S=0 quant_selftestPASS.quant_parity38,401 comparisons, 387 golden group checks, 0 failures. Idle 1-threadquant_bench, 5 rounds:avx2 words 5.81, ggml avx2 port 5.73, avx512 5.37 GB/s from DRAM.
quant_selftest,STRATA_CPU_IQ3S=0 quant_selftest,quant_parityPASS.Idle
quant_benchon the 3900X: 1.05 -> 1.11 GB/s from DRAM (slow-memory box), 2.46 -> 3.42 GB/s from L2.--vram-reserve-mib 1024(1064 of 3072 in VRAM): ids identical (runs in the table below). 2080 Ti rows: ids identical in all runs.Each run (tok/s)
STRATA_FREEZE_EXPERTS=1 strata-poly --model Inkling-2-Mini-Uncensored-AD-IQ3_S.gguf --tokens <302 or 8192 ids> --max-new 256 --temperature 0 --seed 1 --max-context 32768 --kv fp16,plus
--no-gpu-expertsfor "all on the CPU". One process a run, one warm-up (not counted), 3 rounds with main and thebranch interleaved (order reversed every other round). Decode tok/s of each run, higher is better:
A tree with this PR, #218 and #219 cherry-picked on main gives the same ids and the same speed (41.22 / 37.20 / 17.19
tok/s in the first three rows).
Failed: none observed.
Not run:
--vram-reserve-mib, the 8192-token case on the 8 GB and 11 GB cards fails in every tree, main included,with main's "no VRAM for the attention cache (8 MiB more)" (a frozen cache cannot give VRAM back); not this PR's issue.
(the new kernel needs AVX2).
Limits
engine/src/quant/x86/dot_avx2.cpp, in four places: the headercomment, the new kernel functions' place after
#include "x86_dots.inc", the comment abovekTable, and thekTablerows. Each is resolved by keeping both sides.
docs/SETTINGS.mdwas regenerated with--docs; this also adds three rollback switches registered on main without adocs rerun (
STRATA_INK_FLASH,STRATA_POLY_BELL,STRATA_POLY_NV4_TILE).Credits: merbanan's inkling2-turing branch of Strata prompted the comparison that found ggml's IQ3_S CPU dot was slow.
The code is our own.
🤖 Generated with Claude Code