Skip to content

[Engine] Inkling 2: faster AVX2 IQ1_S dot product for CPU experts (same bits) - #219

Open
okada-shogo wants to merge 2 commits into
simplexai-labs:mainfrom
okada-shogo:port/cpu-iq1s
Open

okada-shogo wants to merge 2 commits into
simplexai-labs:mainfrom
okada-shogo:port/cpu-iq1s

Conversation

@okada-shogo

@okada-shogo okada-shogo commented Nov 22, 2026 •

Copy link
Copy Markdown

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:

Card Model Measure main (tok/s) this PR alone (tok/s) Change Experts in VRAM (main → PR)
RTX 2070 8 GB + Ryzen 7 5800X UD-Q2_K_XL (loop) decode after a 302-token prompt, default placement 29.98 30.04 +0.2% 2009 → 2009 (fixed at load)
RTX 2070 8 GB + Ryzen 7 5800X UD-Q2_K_XL (loop) decode after a 302-token prompt, all experts on the CPU 13.94 14.13 +1.4% none
RTX 3060 12 GB + Ryzen 7 3700X UD-Q2_K_XL (loop) decode after a 302-token prompt, all experts on the CPU 14.51 14.65 +1.0% none
RTX 2070 8 GB + Ryzen 7 5800X UD-Q2_K_XL (loop) decode after an 8K-token prompt, placement fixed at load 24.78 25.12 +1.4% 1696 → 1696 (fixed at load, --vram-reserve-mib 1024)
Each run (tok/s)

Decode tok/s, higher is better, each run:

Card Model Measure Build Run 1 (tok/s) Run 2 (tok/s) Run 3 (tok/s)
RTX 2070 8 GB + Ryzen 7 5800X UD-Q2_K_XL (loop) decode after a 302-token prompt, default placement main 30.02 29.98 29.94
RTX 2070 8 GB + Ryzen 7 5800X UD-Q2_K_XL (loop) decode after a 302-token prompt, default placement this PR alone 30.02 30.02 30.07
RTX 2070 8 GB + Ryzen 7 5800X UD-Q2_K_XL (loop) decode after a 302-token prompt, all experts on the CPU main 13.93 13.94 13.94
RTX 2070 8 GB + Ryzen 7 5800X UD-Q2_K_XL (loop) decode after a 302-token prompt, all experts on the CPU this PR alone 14.10 14.14 14.14
RTX 3060 12 GB + Ryzen 7 3700X UD-Q2_K_XL (loop) decode after a 302-token prompt, all experts on the CPU main 14.51 14.51 14.51
RTX 3060 12 GB + Ryzen 7 3700X UD-Q2_K_XL (loop) decode after a 302-token prompt, all experts on the CPU this PR alone 14.64 14.69 14.63
RTX 2070 8 GB + Ryzen 7 5800X UD-Q2_K_XL (loop) decode after an 8K-token prompt, placement fixed at load main 24.79 24.78 24.77
RTX 2070 8 GB + Ryzen 7 5800X UD-Q2_K_XL (loop) decode after an 8K-token prompt, placement fixed at load this PR alone 25.22 25.07 25.08

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):

Card Model Measure #218 (tok/s) #218 + this PR (+ #221) (tok/s) Change
RTX 2070 8 GB + Ryzen 7 5800X UD-Q2_K_XL (loop) decode after a 302-token prompt, default placement 36.42 36.73 +0.9%
RTX 2070 8 GB + Ryzen 7 5800X UD-Q2_K_XL (loop) decode after a 302-token prompt, all experts on the CPU 19.28 19.56 +1.5%
RTX 3060 12 GB + Ryzen 7 3700X UD-Q2_K_XL (loop) decode after a 302-token prompt, all experts on the CPU 20.18 20.54 +1.8%
RTX 2070 8 GB + Ryzen 7 5800X UD-Q2_K_XL (loop) decode after an 8K-token prompt, placement fixed at load 31.22 31.69 +1.5%

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_vidx to 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 the
same bits as the current kernel (avx2_iq1_s_q8_K, the ggml port). Inkling 2 Mini's UD-Q2_K_XL CPU experts use
it.

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):

CPU Case ggml port (µs) this PR alone (µs) Change
Ryzen 5 3600XT gate_up matrix read from DRAM 960 µs 762 µs −20.6%
Ryzen 5 3600XT gate_up matrix already in cache 947 µs 726 µs −23.3%

quant_bench --type iq1_s --n 2304 on 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

  • Listed before the ggml port, so dot_kernel() picks it on CPUs with AVX2. Other CPUs keep their kernel. The port stays
    listed, so quant_bench and the new test compare the two.
  • The 32 11-bit grid indices of a block are made as 16-bit lanes at once (qh repeated by pshufb, the shifts as
    mullo / mulhi). The ggml port does a scalar shift-and-mask per index (or BMI2 pdep, which Zen 1/2 run in
    microcode).
  • The delta term sumi1 comes from one madd of the bsums and one product with the signed scales, an exact integer.
  • The grid products, madd by 2 ls + 1, int32 lane sums, fmadd per block and the final sum are the port's, written
    the 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; named lowbit_avx2_test in the first commit): vs avx2_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-poly built with CUDA 12.9 for
    sm_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-experts
    for "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 the cpu-check-native settings:
    Ryzen 7 7700 (Zen 4; built on the box, GCC 11.4): quant_selftest 78 kernels PASS, quant_parity 38,401 comparisons,
    387 golden group checks, 0 failures, lowbit_avx2_test 40,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):

CPU Case main (GB/s) this PR alone (GB/s) Change
Ryzen 7 3700X (Zen 2) IQ1_S dot, weights read from DRAM 2.19 2.43 +11.0%
Ryzen 7 3700X (Zen 2) IQ1_S dot, weights in L2 cache 2.22 2.44 +9.9%
Ryzen 7 7700 (Zen 4) IQ1_S dot, weights read from DRAM 2.94 3.86 +31.3%
Ryzen 7 7700 (Zen 4) IQ1_S dot, weights in L2 cache 2.99 3.95 +32.1%
Ryzen 9 3900X (Zen 2) IQ1_S dot, weights read from DRAM 1.76 1.27 −27.8%
Ryzen 9 3900X (Zen 2) IQ1_S dot, weights in L2 cache 1.87 2.06 +10.2%
  • At the head (after the rename): GCC 11.4 build, ctest -R iq1s_avx2_test passed, quant_selftest 67 kernels PASS, quant_parity 32,065 comparisons, 0 failures.

Failed: none observed.

Not run:

  • An Intel CPU.
  • MSVC build (the golden file has MSVC-specific IQ1_S lines; the float source is the port's, so it should follow them).

Limits

Credits: ported from our Inkling 2 engine (#200, src/poly/cpu_lowbit.cpp), based on ggml's AVX2 kernel (ggml
authors, MIT).

🤖 Generated with Claude Code

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>
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
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant