Repository navigation
[Engine] Inkling 2: faster AVX2 IQ1_S dot product for CPU experts (same bits) - #219
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
avx2_iq1_s_q8_K_vidx is ggml's AVX2 IQ1_S kernel (the port in x86_dots.inc, ggml authors, MIT) with its scalar parts done in vectors: the 32 11-bit grid indices of a block in 16-bit lanes at once (no BMI2 pdep, which Zen 1/2 run in microcode, and none of the per-index scalar shifts of the non-BMI2 branch), and the delta term sumi1 from one madd of the bsums and the signed scales. The integers are the same, and the float steps are written as ggml's are, so they contract the same way under any compiler: the result has its bits. It is listed before the ggml port in the Avx2 contract, so dot_kernel() picks it on CPUs with AVX2; the port stays listed for quant_bench. tests/quant/lowbit_avx2_test checks it against avx2_iq1_s_q8_K bit for bit on 40,000 rows with random fp16 scales and Q8_K blocks (exit 77 without AVX2). Ported from our Inkling 2 engine (PR simplexai-labs#200, src/poly/cpu_lowbit.cpp). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 22, 2026
The Q2_0 PR (simplexai-labs#218) also added engine/tests/quant/lowbit_avx2_test.cpp and the same quant.cmake block, so the two PRs had an add/add conflict. Name each PR's test after its type, and put its quant.cmake block after quant_selftest's, away from the Q2_0 one, so both merge on their own (the test itself is unchanged). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
okada-shogo
added a commit
to okada-shogo/PolyStrata
that referenced
this pull request
Nov 22, 2026
The IQ1_S PR (simplexai-labs#219) also added engine/tests/quant/lowbit_avx2_test.cpp and the same quant.cmake block, so the two PRs had an add/add conflict. Name each PR's test after its type, so both merge on their own (the test itself is unchanged). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
okada-shogo
marked this pull request as ready for review
November 23, 2026 00:06
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 x86 CPUs with AVX2, when IQ1_S experts run on the CPU. That is Inkling 2 Mini UD-Q2_K_XL on a card that does
not hold every expert. It adds a faster AVX2 dot product for IQ1_S, with the same bits as the current one. On by
default on every CPU with AVX2.
On its own vs with #218. On its own this kernel moves Inkling 2 decode by only +0.2% to +1.4%, because IQ1_S is a small share of the CPU-expert work in UD-Q2_K_XL. Together with #218 (Q2_0) it adds a further +0.9% to +1.8% on top of #218 alone, with identical token ids (same bits), so the two are best taken together:
Decode speed, this PR alone against main, in tokens per second (tok/s), higher is better. UD-Q2_K_XL loop GGUF, 256 tokens written, mean of 3 interleaved runs. "Experts in VRAM" counts the experts kept on the GPU; on the "fixed" rows they are fixed at load (
STRATA_FREEZE_EXPERTS=1) so both builds use the same placement:--vram-reserve-mib 1024)Each run (tok/s)
Decode tok/s, higher is better, each run:
Decode speed on top of #218, in tok/s, higher is better. Same setup as above, median of 3 runs (the stacked trees' single runs are not kept):
The combined tree also carries #221 (IQ3_S), which this file barely uses.
What it changes, in plain words: this PR adds
avx2_iq1_s_q8_K_vidxto the AVX2 table(
src/quant/x86/dot_avx2.cpp). It is ggml's AVX2 IQ1_S kernel with its scalar parts done in vectors. It gives thesame bits as the current kernel (
avx2_iq1_s_q8_K, the ggml port). Inkling 2 Mini's UD-Q2_K_XL CPU experts useit.
Kernel time, in microseconds (µs), lower is better. What it times: one IQ1_S dot product over one Inkling 2 Mini expert matrix (gate_up, 2304 rows of 2304), 1 thread, Ryzen 5 3600XT (the machine was busy with other work):
quant_bench --type iq1_s --n 2304on that loaded machine: 1.13 -> 1.59 GB/s from DRAM (rough). The idle rerun is under Validation.Related discussion
Split out of #200, as the maintainer asked there:
#200 (comment)
Independent of the other split PRs (no stack).
Approach
dot_kernel()picks it on CPUs with AVX2. Other CPUs keep their kernel. The port stayslisted, so
quant_benchand the new test compare the two.qhrepeated bypshufb, the shifts asmullo/mulhi). The ggml port does a scalar shift-and-mask per index (or BMI2pdep, which Zen 1/2 run inmicrocode).
sumi1comes from onemaddof the bsums and one product with the signed scales, an exact integer.maddby2 ls + 1, int32 lane sums,fmaddper block and the final sum are the port's, writtenthe same way, so they contract the same way under any compiler.
Validation
Passed:
GCC 11.4 build (CPU, tests on) of this commit.
iq1s_avx2_test(new; namedlowbit_avx2_testin the first commit): vsavx2_iq1_s_q8_K, 40,000 rows with random fp16 scales on every block, n = 256..6144,1 in 4 rows raw random Q8_K blocks: 0 rows differ. PASS.
quant_selftest: 67 dot kernels, 0 failures, PASS.quant_parity(golden GCC 13.3 digests + linked ggml-cpu, every listed kernel): PASS, 0 failures.quant_bench --type iq1_s: the kernels of the contract give the same bits (65,536 rows).Inkling decode identity and speed (table above): 0158654 vs main d1f711e,
strata-polybuilt with CUDA 12.9 forsm_75 + sm_86, each box running nothing else:
STRATA_FREEZE_EXPERTS=1 strata-poly --model Inkling-2-Mini-UD-Q2_K_XL.loop.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 warm-up, 3 rounds interleaved. Every run's 256 token ids are identical to main's first run.
Each run is in the table under "Each run (tok/s)" above.
Other CPUs, revision 0158654 (test still named
lowbit_avx2_test), CPU-only build of thecpu-check-nativesettings:Ryzen 7 7700 (Zen 4; built on the box, GCC 11.4):
quant_selftest78 kernels PASS,quant_parity38,401 comparisons,387 golden group checks, 0 failures,
lowbit_avx2_test40,000 rows 0 differ. Ryzen 7 3700X, Ryzen 9 3900X, EPYC 7662(Zen 2): the same tests PASS (67 kernels, 32,065 comparisons).
Idle
quant_bench --type iq1_s --n 2304, 1 thread, 5 interleaved rounds, main vs this, GB/s (every round within 0.03 GB/s).The 3900X is slower from DRAM at 1 thread on that slow-memory box, faster from L2; its decode with all experts on the
CPU: 8.24 -> 8.24 tok/s, ids identical.
IQ1_S dot-product throughput, in GB/s of weights read, higher is better.
quant_bench --type iq1_s --n 2304, 1 thread, idle machines, 5 interleaved rounds (every round within 0.03 GB/s):ctest -R iq1s_avx2_testpassed,quant_selftest67 kernels PASS,quant_parity32,065 comparisons, 0 failures.Failed: none observed.
Not run:
Limits
dot_avx2.cppas the IQ3_S PR ([Engine] Inkling 2: faster AVX2 IQ3_S dot product for CPU experts (same bits) #221) and the Q2_0 PR ([Engine] Inkling 2: AVX2 Q2_0 dot product for CPU experts (same bits) #218). A trial merge with either conflicts in those lines only; whichever merges second keeps both sides' table entries.iq1s_avx2_test) and its CMake block do not clash with [Engine] Inkling 2: AVX2 Q2_0 dot product for CPU experts (same bits) #218's (a trial merge of the two heads conflicts indot_avx2.cpponly).Credits: ported from our Inkling 2 engine (#200,
src/poly/cpu_lowbit.cpp), based on ggml's AVX2 kernel (ggmlauthors, MIT).
🤖 Generated with Claude Code