Skip to content

[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
simplexai-labs:mainfrom
okada-shogo:port/cpu-iq3s-avx512
Open

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

Conversation

@okada-shogo

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

Copy link
Copy Markdown

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=1 turns 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_K there). "Some experts on the GPU" = --expert-swaps 0 --vram-reserve-mib 5120; "placement fixed at load" adds STRATA_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.

Card Model Measure main (tok/s) this PR alone (tok/s) Change Experts in VRAM (main → PR)
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, all experts on the CPU 20.79 27.44 +32.0% none
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, some experts on the GPU, placement not fixed 73.81 85.03 +15.2% 2236 → 2236 (not fixed)
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, some experts on the GPU, placement fixed at load 56.30 68.80 +22.2% 2236 → 2236 (fixed at load)
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, all experts on the CPU 20.54 26.93 +31.1% none
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, some experts on the GPU, placement not fixed 67.58 77.61 +14.9% 2236 → 2236 (not fixed)
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, some experts on the GPU, placement fixed at load 56.57 67.93 +20.1% 2236 → 2236 (fixed at load)

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:

Card Model Measure main (ms) this PR alone (ms) Change
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, all experts on the CPU 38.3 26.1 −31.9%
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, some experts on the GPU, placement not fixed 3.2 2.3 −28.1%
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, some experts on the GPU, placement fixed at load 5.1 3.6 −29.4%
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, all experts on the CPU 36.7 24.1 −34.3%
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, some experts on the GPU, placement not fixed 0.2 0.1 −50.0%
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, some experts on the GPU, placement fixed at load 0.3 0.2 −33.3%

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

CPU Case main, avx512 kernel (GB/s) this PR alone, avx512vbmi kernel (GB/s) Change
Ryzen 7 7700 (Zen 4) IQ3_S dot 5.37 10.77 +100.7%

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_K loads the
64 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:

CPU Case main, avx512 kernel (GB/s) this PR alone, avx512vbmi kernel (GB/s) Change AVX2 kernel, for reference (GB/s)
Ryzen 7 7800X3D 1 thread 4.71 9.70 +105.9% 4.94
Ryzen 7 7800X3D 8 copies at once, total 31.6 69.5 +119.9% 32.5

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

  • New file src/quant/x86/dot_avx512vbmi.cpp and a new ISA level Isa::Avx512Vbmi (AVX-512 F/DQ/BW/VL + VNNI + VBMI, with
    the OS's AVX-512 state). Only IQ3_S has a kernel there; every other type keeps its kernel.
  • The 64 grid words of a block come from vpermi2b byte lookups (4 byte planes x 4 quarters of 128 entries) instead of
    64 scalar loads. Signs are a mask register, scales go through vpdpwssd. Integer lane sums and float steps are the
    AVX2 kernel's, so the bits are the Avx2 contract's.
  • The lookup planes are a function-local static, built on the kernel's first call, which only a VBMI CPU makes: no code of
    this file runs at start-up or on a CPU without VBMI. (The first version built them constexpr, which only GCC accepts.)
  • On a VBMI CPU, 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=1 turns VBMI off only (main's avx512_iq3_s_q8_K runs). STRATA_FORCE_ISA=avx2 turns off all of
    AVX-512, as before.

Validation

Passed:

  • Ryzen 7 7800X3D (Zen 4, WSL2, GCC 13.3), CPU-only build of this branch: quant_selftest 78 kernels, 0 failures;
    quant_parity --type iq3_s 18,817 comparisons, 0 failures; quant_parity (all types) 38,401 comparisons, 0 failures.
  • Same box, quant_bench --type iq3_s: every kernel checked row by row against the others, no difference; speed above.
  • Ryzen 5 3600XT (Zen 2, no AVX-512): this file compiled on SIMDe's emulated AVX-512 against 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 gives 32,000 of 32,000 different, so the check catches mistakes.
  • Zen 2: quant_selftest and quant_parity pass (the new kernel is skipped there; nothing else moves). Again at the head
    with GCC 11.4: quant_selftest 66 kernels PASS, quant_parity 31,489 comparisons, 0 failures.
  • Clang: clang++ 14 -fsyntax-only -Wall -Wextra of dot_avx512vbmi.cpp with 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_quant and quant_selftest links, and quant_selftest passes.
  • Inkling decode identity and speed, Ryzen 7 7700 (Zen 4, AVX-512 VBMI) + RTX 4070 Ti SUPER (rented, Ubuntu, CUDA, sm_89 build of
    strata-poly, main's way, no llama.cpp): table above. On the same box quant_selftest (78 kernels, best isa avx512vbmi),
    STRATA_FORCE_AVX2=1 quant_selftest (77 kernels, best isa avx512) and quant_parity --type iq3_s (18,817 comparisons)
    pass with 0 failures; quant_bench finds no difference between the three kernels.
  • The 7800X3D and 7700 results are from the first commit (afdff31; the 7700 ran the same file as 900d2aab1). The second
    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); with
STRATA_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:

Card Model Measure Build Run 1 (tok/s) Run 2 (tok/s) Run 3 (tok/s)
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, all experts on the CPU main 20.77 20.77 20.84
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, all experts on the CPU this PR alone 27.41 27.45 27.47
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, all experts on the CPU this PR, STRATA_FORCE_AVX2=1 (main's avx512 kernel) 20.90 20.79 20.79
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, all experts on the CPU this PR, STRATA_FORCE_ISA=avx2 (AVX2 kernel) 21.27 21.51 21.30
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, some experts on the GPU, placement not fixed main 73.91 73.82 73.70
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, some experts on the GPU, placement not fixed this PR alone 84.78 83.55 86.76
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, some experts on the GPU, placement not fixed this PR, STRATA_FORCE_AVX2=1 (main's avx512 kernel) 73.29 73.80 74.04
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, some experts on the GPU, placement not fixed this PR, STRATA_FORCE_ISA=avx2 (AVX2 kernel) 74.45 74.59 74.10
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, some experts on the GPU, placement fixed at load main 56.40 56.19 56.32
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after a 302-token prompt, some experts on the GPU, placement fixed at load this PR alone 68.65 68.81 68.94
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, all experts on the CPU main 20.59 20.56 20.48
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, all experts on the CPU this PR alone 26.99 26.91 26.88
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, all experts on the CPU this PR, STRATA_FORCE_AVX2=1 (main's avx512 kernel) 20.43 20.53 20.53
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, all experts on the CPU this PR, STRATA_FORCE_ISA=avx2 (AVX2 kernel) 21.06 21.15 21.10
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, some experts on the GPU, placement not fixed main 67.52 67.59 67.62
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, some experts on the GPU, placement not fixed this PR alone 77.37 77.58 77.89
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, some experts on the GPU, placement not fixed this PR, STRATA_FORCE_AVX2=1 (main's avx512 kernel) 67.50 66.92 67.63
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, some experts on the GPU, placement not fixed this PR, STRATA_FORCE_ISA=avx2 (AVX2 kernel) 68.52 68.81 68.72
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, some experts on the GPU, placement fixed at load main 56.61 56.35 56.76
RTX 4070 Ti SUPER 16 GB + Ryzen 7 7700 AD-IQ3_S decode after an 8K-token prompt, some experts on the GPU, placement fixed at load this PR alone 67.92 67.82 68.05

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 speed
is 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:

CPU Kernel Run 1 (GB/s) Run 2 (GB/s) Run 3 (GB/s) Run 4 (GB/s) Run 5 (GB/s)
Ryzen 7 7700 avx512vbmi (this PR alone) 10.48 10.84 10.85 10.85 10.85
Ryzen 7 7700 avx512 (main) 5.37 5.37 5.37 5.36 5.37
Ryzen 7 7700 avx2 (this PR's binary, for reference) 5.74 5.72 5.73 5.72 5.73

On the same box the AVX2 PRs
(#218, #219, #221) also pass quant_selftest / quant_parity.

Failed:

  • none observed

Not run:

  • MSVC, clang-cl and icpx (SYCL job) builds (the CMake lines are there, not compiled here; plain clang now passes).
  • CUDA/HIP/Metal: no GPU file is touched.

Limits

Credit: our kernel from #200's branch (inkling2-iq3s-avx512), written for VecSzn's quant layer.

🤖 Generated with Claude Code

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