Skip to content

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

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

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

Conversation

@okada-shogo

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

Copy link
Copy Markdown

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=0 gives 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.

Card Model Measure main (tok/s) this PR alone (tok/s) Change Experts in VRAM (main → PR)
RTX 3060 12 GB + Ryzen 7 3700X (Zen 2) AD-IQ3_S decode after a 302-token prompt, default placement 39.39 41.24 +4.7% 2477 → 2477 (fixed at load)
RTX 3060 12 GB + Ryzen 7 3700X (Zen 2) AD-IQ3_S decode after a 302-token prompt, all experts on the CPU 14.92 17.23 +15.5% none
RTX 2070 8 GB + Ryzen 7 5800X (Zen 3) AD-IQ3_S decode after a 302-token prompt, default placement 24.24 24.35 +0.5% 1277 → 1277 (fixed at load)
RTX 2070 8 GB + Ryzen 7 5800X (Zen 3) AD-IQ3_S decode after a 302-token prompt, all experts on the CPU 16.10 16.33 +1.4% none
RTX 3060 12 GB + Ryzen 7 3700X (Zen 2) AD-IQ3_S decode after an 8K-token prompt, default placement 35.79 37.20 +3.9% fixed at load, same placement in both
RTX 2070 8 GB + Ryzen 7 5800X (Zen 3) AD-IQ3_S decode after an 8K-token prompt, 1024 MiB of VRAM kept free 21.79 21.70 −0.4% 1064 → 1064 (fixed at load, --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):

Card Model Measure main (tok/s) this PR alone (tok/s) Change Experts in VRAM (main → PR)
RTX 2080 Ti 11 GB + Ryzen 9 3900X (Zen 2) AD-IQ3_S decode after a 302-token prompt, default placement 20.66 21.87 +5.9% 2153 → 2153 (fixed at load)
RTX 2080 Ti 11 GB + Ryzen 9 3900X (Zen 2) AD-IQ3_S decode after a 302-token prompt, all experts on the CPU 7.51 7.70 +2.5% none

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:

Card Model Measure main (ms) this PR alone (ms) Change
RTX 3060 12 GB + Ryzen 7 3700X (Zen 2) AD-IQ3_S decode after a 302-token prompt, default placement 5.3 4.5 −15.1%
RTX 3060 12 GB + Ryzen 7 3700X (Zen 2) AD-IQ3_S decode after a 302-token prompt, all experts on the CPU 51.3 42.6 −17.0%
RTX 2070 8 GB + Ryzen 7 5800X (Zen 3) AD-IQ3_S decode after a 302-token prompt, default placement 13.4 13.3 −0.7%
RTX 2070 8 GB + Ryzen 7 5800X (Zen 3) AD-IQ3_S decode after a 302-token prompt, all experts on the CPU 43.0 41.8 −2.8%
RTX 2080 Ti 11 GB + Ryzen 9 3900X (Zen 2) AD-IQ3_S decode after a 302-token prompt, default placement 17.8 16.5 −7.3%
RTX 2080 Ti 11 GB + Ryzen 9 3900X (Zen 2) AD-IQ3_S decode after a 302-token prompt, all experts on the CPU 94.7 90.1 −4.9%
RTX 3060 12 GB + Ryzen 7 3700X (Zen 2) AD-IQ3_S decode after an 8K-token prompt, default placement 0.3 0.3 0%
RTX 2070 8 GB + Ryzen 7 5800X (Zen 3) AD-IQ3_S decode after an 8K-token prompt, 1024 MiB of VRAM kept free 0.9 0.9 0%

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 kernel
is limited by its own computation, not by memory speed. This PR adds avx2_iq3_s_q8_K_words, a faster AVX2 kernel
with the same bits as the port.

  • The change is global, not Inkling-gated: every user of the quant layer's IQ3_S dot on a CPU whose best level is
    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.
  • CPUs with AVX-512 keep avx512_iq3_s_q8_K (higher ISA level).
  • STRATA_CPU_IQ3S=0 gives back the ggml port. Only the exact value 0 turns it off. It is in the settings register
    as a rollback switch.
  • Kernel alone, idle Ryzen 7 3700X, quant_bench --type iq3_s --n 2304, 1 thread, 5 interleaved rounds: 4.03 GB/s vs
    2.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

  • The ggml kernel builds the 9-bit grid indices in a vector, stores it to memory, and reads the 16 indices back one
    at a time (each read waits for the store). Then it builds each 8-word vector with eight vpinsrd loads.
  • The new kernel makes the 16 indices of two groups as 16-bit lanes (the 9th bits come from a 256-entry table, by
    qh byte). It moves them to two general registers four at a time (no store), splits them with shifts, reads each
    grid word with vmovd and joins the words with unpacks.
  • The rest is the ggml kernel's (sign flip, maddubs / madd by 2 ls + 1, int32 lane sums, one fmadd per block,
    the same horizontal sum), so the bits are the same.
Dispatch and the off switch
  • DotKernel has a new field on (null for the ggml ports). dot_kernel() and checked_dot_kernel() skip a kernel
    whose on() is false and take the next of the same contract. dot_kernels() still lists every kernel.
  • iq3s_own_on() reads STRATA_CPU_IQ3S once.
  • selfcheck's cached verdict key now includes the kernel's position in the list, since two kernels can now share
    (type, contract, ISA).
  • The two 64-bit moves go through small helpers that use memory on 32-bit x86 (not built or tested there).

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_bench targets), unless stated.

Passed:

  • quant_selftest: new kernel vs avx2_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.
  • Mutation checks: (a) the kernel changed by 1 ulp: every dot differs, FAIL. (b) saturating sign flip (subs_epi8
    instead 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.
  • Default-path byte identity: quant_parity (all types, golden digests of GCC 13.3's ggml-cpu, plus the linked
    ggml-cpu, GCC 11.4): 32,065 comparisons, 0 rows differ, 0 failures, PASS. quant_bench: every kernel of the contract
    gives 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.
  • Inkling decode identity and speed (table above): f87ff76 vs main d1f711e, strata-poly built with CUDA 12.9 for
    sm_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.
  • AVX-512 CPU, Ryzen 7 7700 (Zen 4), CPU-only build of f87ff76 (cpu-check-native settings, GCC 11.4, built on the box):
    quant_selftest prints iq3_s takes avx512_iq3_s_q8_K (STRATA_CPU_IQ3S unset), so the dispatcher keeps the AVX-512
    kernel; 78 kernels, 32,500 own-kernel dots vs the port, 0 differ, PASS. STRATA_CPU_IQ3S=0 quant_selftest PASS.
    quant_parity 38,401 comparisons, 387 golden group checks, 0 failures. Idle 1-thread quant_bench, 5 rounds:
    avx2 words 5.81, ggml avx2 port 5.73, avx512 5.37 GB/s from DRAM.
  • Ryzen 9 3900X and EPYC 7662 (Zen 2): quant_selftest, STRATA_CPU_IQ3S=0 quant_selftest, quant_parity PASS.
    Idle quant_bench on the 3900X: 1.05 -> 1.11 GB/s from DRAM (slow-memory box), 2.46 -> 3.42 GB/s from L2.
  • 8 GB card, 8192-token prompt, every tree with --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-experts for "all on the CPU". One process a run, one warm-up (not counted), 3 rounds with main and the
branch interleaved (order reversed every other round). Decode tok/s of each run, higher is better:

Card Model Measure Build Run 1 (tok/s) Run 2 (tok/s) Run 3 (tok/s)
RTX 3060 12 GB + Ryzen 7 3700X (Zen 2) AD-IQ3_S decode after a 302-token prompt, default placement main 39.42 39.40 39.36
RTX 3060 12 GB + Ryzen 7 3700X (Zen 2) AD-IQ3_S decode after a 302-token prompt, default placement this PR alone 41.39 41.22 41.10
RTX 3060 12 GB + Ryzen 7 3700X (Zen 2) AD-IQ3_S decode after a 302-token prompt, all experts on the CPU main 14.91 14.97 14.88
RTX 3060 12 GB + Ryzen 7 3700X (Zen 2) AD-IQ3_S decode after a 302-token prompt, all experts on the CPU this PR alone 17.18 17.22 17.28
RTX 2070 8 GB + Ryzen 7 5800X (Zen 3) AD-IQ3_S decode after a 302-token prompt, default placement main 24.27 24.23 24.21
RTX 2070 8 GB + Ryzen 7 5800X (Zen 3) AD-IQ3_S decode after a 302-token prompt, default placement this PR alone 24.37 24.44 24.24
RTX 2070 8 GB + Ryzen 7 5800X (Zen 3) AD-IQ3_S decode after a 302-token prompt, all experts on the CPU main 16.11 16.09 16.11
RTX 2070 8 GB + Ryzen 7 5800X (Zen 3) AD-IQ3_S decode after a 302-token prompt, all experts on the CPU this PR alone 16.32 16.34 16.34
RTX 3060 12 GB + Ryzen 7 3700X (Zen 2) AD-IQ3_S decode after an 8K-token prompt, default placement main 35.90 35.74 35.74
RTX 3060 12 GB + Ryzen 7 3700X (Zen 2) AD-IQ3_S decode after an 8K-token prompt, default placement this PR alone 37.22 37.21 37.17
RTX 2070 8 GB + Ryzen 7 5800X (Zen 3) AD-IQ3_S decode after an 8K-token prompt, 1024 MiB of VRAM kept free main 21.78 21.81 21.77
RTX 2070 8 GB + Ryzen 7 5800X (Zen 3) AD-IQ3_S decode after an 8K-token prompt, 1024 MiB of VRAM kept free this PR alone 21.81 21.83 21.46

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:

  • Without --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.
  • An Intel CPU.
  • ARM / NEON (no change there, not built), 32-bit x86 (no 32-bit toolchain here), Windows / MSVC, an AVX-only CPU
    (the new kernel needs AVX2).

Limits

  • x86 with AVX2 only. 32-bit x86 compiles through a fallback path that has not been built or tested.
  • The speed-up depends on the CPU: +15% decode with every expert on a Zen 2 CPU, about +1% on Zen 3.
  • A trial merge with [Engine] Inkling 2: AVX2 Q2_0 dot product for CPU experts (same bits) #218 or [Engine] Inkling 2: faster AVX2 IQ1_S dot product for CPU experts (same bits) #219 conflicts only in engine/src/quant/x86/dot_avx2.cpp, in four places: the header
    comment, the new kernel functions' place after #include "x86_dots.inc", the comment above kTable, and the kTable
    rows. Each is resolved by keeping both sides.
  • docs/SETTINGS.md was regenerated with --docs; this also adds three rollback switches registered on main without a
    docs 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

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>
okada-shogo and others added 2 commits November 23, 2026 07:06
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
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>
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