[PR #930] [CLOSED] cpu: IQ3_S / IQ2_S AVX2 decode without the per-index shift/mask/or (1.07-1.6x per row) #2920

Closed
opened 2026-11-04 02:04:11 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/930
Author: @sanastasiou
Created: 10/5/2026
Status: ❌ Closed

Base: main ← Head: perf/avx2-iq3s-iq2s-decode


📝 Commits (10+)

  • fa16567 setup: an earlier config that does not parse is skipped, not a crash; configs are written atomically (#459)
  • 749d957 setup (--backend hip): a system ROCm without the HIP development files falls back to the wheels (#446)
  • 5a30024 calibrate: the engine's arguments come from the server's engine_args; a failed tuning says why (#447)
  • 9f60d13 serve, setup: name the small split card that caps the prompt chunk; setup recommends the big card alone (#448)
  • eb8da1d /metrics: each request's speculative draft counts, and their sums in the totals (#457)
  • 6a09387 setup (--gguf-dir): GGUFs Strata cannot run are named as such, with the --family/--model to use (#444)
  • d6a91e2 docs: the Coder is weaker outside code, Chinese/CJK text included; the unpruned sizes for that (#438)
  • d8d4ad3 Merge branch 'f-135-eng' into rel-0.1.35
  • 7b600d5 Merge branch 'f-135-small' into rel-0.1.35
  • d9ab843 Engine 0.1.35: Windows AMD uses its bundled HIP runtime (#468 #461), the low-RAM resident mode on Windows 32 GB PCs (#467), fixes #460 #459 #446 #447 #457 #448 #444; setup requires it

📊 Changes

1 file changed (+87 additions, -19 deletions)

View changed files

📝 src/kernels/cpu/iq_avx2.cpp (+87 -19)

📄 Description

The AVX2 multi-token kernels in src/kernels/cpu/iq_avx2.cpp are compute-bound on the codebook decode, not on the per-token arithmetic: a hot (in-cache) and a cold (256 MiB) working set measure the same ns/row. This PR makes the IQ3_S and IQ2_S decode cheaper. The arithmetic is untouched (vpsignb, vpmaddubsw, vpmaddwd, exact int32 per QK_K block), so the results are bit-identical.

What changes (one file, IQ3_S and IQ2_S only)

  • Grid indices. The qh byte indexes a compile-time table that spreads its high bits one per index byte. One punpcklbw puts them next to the 8 low index bytes, so each lookup is a 16-bit load plus the grid load instead of a shift, a mask and an or per index.
  • Sign vectors. The half's four sign bytes are broadcast straight from the block (vpbroadcastd from memory, a load uop) and expanded with ONE pshufb, instead of sgn_vec's two pshufb + vinserti128. On Intel cores those are three uops on the single shuffle port.
  • Scales. The (2s+1) vectors come from compile-time tables: one load instead of shift/or/broadcast.
  • No static constructor. The tables are constexpr (like EvenSigns), so the AVX2 TU still has no static constructor (#391). static_asserts pin the table layout.
  • Gather opt-in kept, branch hoisted. IQ3_S's STRATA_IQ256_GATHER path becomes its own instantiation, chosen once per call in iq256_rows / iq256_gu_rows. With the check left inside decode(), the branch sat in the innermost loop and made the new IQ3_S decode 25-35 % slower at nt=2-3 (default path, Zen 3).
  • IQ3_XXS unchanged. The same hoist measured -5..+5 % there, which is not worth it.

Measured (ns per row of all nt tokens, hot, one pinned thread, best of 3 rounds with base and PR alternated; base = main @ 6f32ec0; the kernels compiled with -march=haswell):

CPU type nt=1 nt=2 nt=3 nt=4
Ryzen 9 5950X (Zen 3) IQ3_S 256.1 → 213.7 (1.20x) 304.4 → 267.5 (1.14x) 360.7 → 313.7 (1.15x) 399.8 → 374.1 (1.07x)
IQ2_S 208.0 → 144.1 (1.44x) 225.5 → 192.9 (1.17x) 255.6 → 222.7 (1.15x) 279.6 → 271.2 (1.03x)
Ryzen 5 3600 (Zen 2) IQ3_S 337.7 → 270.4 (1.25x) 386.4 → 320.5 (1.21x) 451.4 → 378.5 (1.19x) 524.8 → 437.3 (1.20x)
IQ2_S 244.5 → 172.7 (1.42x) 278.3 → 222.8 (1.25x) 320.0 → 265.8 (1.20x) 360.7 → 327.0 (1.10x)
Core i7-7700 (Kaby Lake, AVX2, no AVX-512) IQ3_S 405.6 → 354.6 (1.14x) 616.6 → 382.8 (1.61x) 507.2 → 426.5 (1.19x) 573.3 → 492.4 (1.16x)
IQ2_S 310.5 → 208.4 (1.49x) 359.8 → 254.6 (1.41x) 404.8 → 313.4 (1.29x) 440.1 → 365.9 (1.20x)

On the i7-7700, main's IQ3_S at nt=2 is slower than at nt=3. This reproduced on two cores and two runs; with this PR the curve is monotonic again.

I have not measured end to end in the engine, and I have not measured on a Haswell Xeon. The Kaby Lake core has the same single-shuffle-port layout, so I expect Haswell to land in the same range, but that is not measured. Numbers from an E5 v3 owner (#913) would close that gap.

Tests

  • Parity harness. A standalone harness (our private test bench; happy to contribute it as a CPU-only tests/ target if useful) compiles this exact iq_avx2.cpp and checks iq256_rows against ggml's _generic references and a float64 oracle.
    • Every type, nt 1..9, row ranges with odd counts and matrix borders: 844 tests shuffled, pass on Zen 3.
    • The strata_iq256 subset: 180 tests, pass on Zen 2 and Kaby Lake.
    • The same 844 tests also pass with STRATA_IQ256_GATHER=1.
  • Mutation-checked. Breaking the IQ3_S high bits or the IQ2_S sign offset fails the parity tests. Swapping the IQ2_S scale nibbles fails at compile time on the static_assert.
  • Upstream build. CPU-only cmake -DSTRATA_WERROR=ON -DSTRATA_BUILD_TESTS=ON builds clean.
    • ctest: 15/16 pass. expert_multi_test fails on this Zen 3 box because it requires AVX-512 ("this CPU cannot run the expert kernel"), not because of this change.
    • native_expert_parity needs CUDA plus the llama.cpp oracle and does not cover the IQ formats; I did not run it.
  • No AVX-512. Nothing in the shipped object uses EVEX or zmm; this is checked by disassembly.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/930 **Author:** [@sanastasiou](https://github.com/sanastasiou) **Created:** 10/5/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `perf/avx2-iq3s-iq2s-decode` --- ### 📝 Commits (10+) - [`fa16567`](https://github.com/Niko1221/Strata/commit/fa165679f9860552ac97ebd07118bb8db286be4a) setup: an earlier config that does not parse is skipped, not a crash; configs are written atomically (#459) - [`749d957`](https://github.com/Niko1221/Strata/commit/749d957a1790b1a171dd4a01ab3e31b3882c9892) setup (--backend hip): a system ROCm without the HIP development files falls back to the wheels (#446) - [`5a30024`](https://github.com/Niko1221/Strata/commit/5a3002446dbb9dfcabb7d0bd9f113da90cc2f3c0) calibrate: the engine's arguments come from the server's engine_args; a failed tuning says why (#447) - [`9f60d13`](https://github.com/Niko1221/Strata/commit/9f60d13246e46b02b1dd0841980b4d18b0faaea7) serve, setup: name the small split card that caps the prompt chunk; setup recommends the big card alone (#448) - [`eb8da1d`](https://github.com/Niko1221/Strata/commit/eb8da1d8c26ec41e6ffb084fdf579dba7cad10bd) /metrics: each request's speculative draft counts, and their sums in the totals (#457) - [`6a09387`](https://github.com/Niko1221/Strata/commit/6a093874fe037020f49bd5cf053879ff6a133c21) setup (--gguf-dir): GGUFs Strata cannot run are named as such, with the --family/--model to use (#444) - [`d6a91e2`](https://github.com/Niko1221/Strata/commit/d6a91e2d1770d0f469d6ec15890acc7ee93e764c) docs: the Coder is weaker outside code, Chinese/CJK text included; the unpruned sizes for that (#438) - [`d8d4ad3`](https://github.com/Niko1221/Strata/commit/d8d4ad337d3c98ba21d3bd0af1a16e9177e112b6) Merge branch 'f-135-eng' into rel-0.1.35 - [`7b600d5`](https://github.com/Niko1221/Strata/commit/7b600d55ff801caaaa11542af1523050992e049b) Merge branch 'f-135-small' into rel-0.1.35 - [`d9ab843`](https://github.com/Niko1221/Strata/commit/d9ab8435f654c368c586340d490915f6addf56a3) Engine 0.1.35: Windows AMD uses its bundled HIP runtime (#468 #461), the low-RAM resident mode on Windows 32 GB PCs (#467), fixes #460 #459 #446 #447 #457 #448 #444; setup requires it ### 📊 Changes **1 file changed** (+87 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `src/kernels/cpu/iq_avx2.cpp` (+87 -19) </details> ### 📄 Description The AVX2 multi-token kernels in `src/kernels/cpu/iq_avx2.cpp` are compute-bound on the codebook decode, not on the per-token arithmetic: a hot (in-cache) and a cold (256 MiB) working set measure the same ns/row. This PR makes the IQ3_S and IQ2_S decode cheaper. The arithmetic is untouched (`vpsignb`, `vpmaddubsw`, `vpmaddwd`, exact int32 per QK_K block), so the results are bit-identical. **What changes (one file, IQ3_S and IQ2_S only)** - **Grid indices.** The `qh` byte indexes a compile-time table that spreads its high bits one per index byte. One `punpcklbw` puts them next to the 8 low index bytes, so each lookup is a 16-bit load plus the grid load instead of a shift, a mask and an or per index. - **Sign vectors.** The half's four sign bytes are broadcast straight from the block (`vpbroadcastd` from memory, a load uop) and expanded with ONE `pshufb`, instead of `sgn_vec`'s two `pshufb` + `vinserti128`. On Intel cores those are three uops on the single shuffle port. - **Scales.** The `(2s+1)` vectors come from compile-time tables: one load instead of shift/or/broadcast. - **No static constructor.** The tables are `constexpr` (like `EvenSigns`), so the AVX2 TU still has no static constructor (#391). `static_assert`s pin the table layout. - **Gather opt-in kept, branch hoisted.** IQ3_S's `STRATA_IQ256_GATHER` path becomes its own instantiation, chosen once per call in `iq256_rows` / `iq256_gu_rows`. With the check left inside `decode()`, the branch sat in the innermost loop and made the new IQ3_S decode 25-35 % slower at nt=2-3 (default path, Zen 3). - **IQ3_XXS unchanged.** The same hoist measured -5..+5 % there, which is not worth it. **Measured** (ns per row of all nt tokens, hot, one pinned thread, best of 3 rounds with base and PR alternated; base = `main` @ 6f32ec0; the kernels compiled with `-march=haswell`): | CPU | type | nt=1 | nt=2 | nt=3 | nt=4 | |---|---|---|---|---|---| | Ryzen 9 5950X (Zen 3) | IQ3_S | 256.1 → 213.7 (**1.20x**) | 304.4 → 267.5 (1.14x) | 360.7 → 313.7 (1.15x) | 399.8 → 374.1 (1.07x) | | | IQ2_S | 208.0 → 144.1 (**1.44x**) | 225.5 → 192.9 (1.17x) | 255.6 → 222.7 (1.15x) | 279.6 → 271.2 (1.03x) | | Ryzen 5 3600 (Zen 2) | IQ3_S | 337.7 → 270.4 (**1.25x**) | 386.4 → 320.5 (1.21x) | 451.4 → 378.5 (1.19x) | 524.8 → 437.3 (1.20x) | | | IQ2_S | 244.5 → 172.7 (**1.42x**) | 278.3 → 222.8 (1.25x) | 320.0 → 265.8 (1.20x) | 360.7 → 327.0 (1.10x) | | Core i7-7700 (Kaby Lake, AVX2, no AVX-512) | IQ3_S | 405.6 → 354.6 (1.14x) | 616.6 → 382.8 (**1.61x**) | 507.2 → 426.5 (1.19x) | 573.3 → 492.4 (1.16x) | | | IQ2_S | 310.5 → 208.4 (**1.49x**) | 359.8 → 254.6 (1.41x) | 404.8 → 313.4 (1.29x) | 440.1 → 365.9 (1.20x) | On the i7-7700, `main`'s IQ3_S at nt=2 is slower than at nt=3. This reproduced on two cores and two runs; with this PR the curve is monotonic again. I have not measured end to end in the engine, and I have not measured on a Haswell Xeon. The Kaby Lake core has the same single-shuffle-port layout, so I expect Haswell to land in the same range, but that is not measured. Numbers from an E5 v3 owner (#913) would close that gap. **Tests** - **Parity harness.** A standalone harness (our private test bench; happy to contribute it as a CPU-only `tests/` target if useful) compiles this exact `iq_avx2.cpp` and checks `iq256_rows` against ggml's `_generic` references and a float64 oracle. - Every type, nt 1..9, row ranges with odd counts and matrix borders: 844 tests shuffled, pass on Zen 3. - The `strata_iq256` subset: 180 tests, pass on Zen 2 and Kaby Lake. - The same 844 tests also pass with `STRATA_IQ256_GATHER=1`. - **Mutation-checked.** Breaking the IQ3_S high bits or the IQ2_S sign offset fails the parity tests. Swapping the IQ2_S scale nibbles fails at compile time on the `static_assert`. - **Upstream build.** CPU-only `cmake -DSTRATA_WERROR=ON -DSTRATA_BUILD_TESTS=ON` builds clean. - ctest: 15/16 pass. `expert_multi_test` fails on this Zen 3 box because it requires AVX-512 ("this CPU cannot run the expert kernel"), not because of this change. - `native_expert_parity` needs CUDA plus the llama.cpp oracle and does not cover the IQ formats; I did not run it. - **No AVX-512.** Nothing in the shipped object uses EVEX or `zmm`; this is checked by disassembly. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-04 02:04:11 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#2920
No description provided.