[PR #1697] [CLOSED] cpu: AVX1 128-bit expert kernels (iq_avx1) - the i-quant rows on CPUs with no AVX2 #3616

Closed
opened 2026-11-07 15:29:34 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/1697
Author: @nashcap
Created: 10/9/2026
Status: ❌ Closed

Base: main ← Head: avx1-iq-kernels


📝 Commits (6)

  • c7db61b cpu: AVX1 128-bit expert kernels (iq_avx1) - the i-quant rows on CPUs with no AVX2
  • 0e11122 cpu: iq_avx1 - IQ4_XS gate/up stays on ggml's generic dot (the 128-bit grid kernel measures 0.7-1.0x against its auto-vectorized generic on an E5-2470 v2; the kernel stays built and parity-tested, only the dispatch declines it)
  • cd72eae cpu: q2_avx1 - the Q2_0 rows and the Q8_1 quantizer for CPUs with AVX but no AVX2 (fixes #1699)
  • 2220528 cpu: AVX1 kernel micro-optimizations, all bit-exact (parity re-verified)
  • ab034c2 cpu: AVX1 - decline IQ4_NL down rows, two-block iq4_nl kernel, fused cvt, split bench
  • c4a3634 cpu: AVX1 kernel audit - row-pair only at NT=1, drop dead dot_rows

📊 Changes

12 files changed (+1399 additions, -19 deletions)

View changed files

📝 CMakeLists.txt (+21 -1)
📝 include/strata/kernels/cpu/expert.hpp (+8 -0)
📝 include/strata/kernels/cpu/expert_layout.hpp (+3 -0)
➕ include/strata/kernels/cpu/iq_avx1.hpp (+37 -0)
📝 src/kernels/cpu/expert_layout.cpp (+11 -2)
➕ src/kernels/cpu/iq_avx1.cpp (+650 -0)
➕ src/kernels/cpu/iq_avx1_parity.cpp (+303 -0)
📝 src/kernels/cpu/native_expert.cpp (+27 -0)
➕ src/kernels/cpu/q2_avx1.cpp (+304 -0)
📝 src/kernels/cpu/q2_bitplane_parity.cpp (+23 -11)
📝 src/kernels/cpu/q8k_quant_parity.cpp (+8 -4)
📝 src/program/generate.cpp (+4 -1)

📄 Description

What

Completes the AVX1 floor for native packs. Since #394 an AVX-only CPU (Sandy Bridge through Ivy Bridge Xeons) starts and runs a native pack, but every i-quant dot in ggml-cpu is #if defined(__AVX2__) and falls to the scalar _generic path, and the fork's Q2_0 has no vectorized dot either - so the CPU-computed experts of a native pack run scalar there. This adds the missing sibling of kq_avx1.cpp for the expert rows:

  • src/kernels/cpu/iq_avx1.cpp (+ header): gate/up for IQ3_XXS, IQ3_S, IQ2_S and down for Q2_0, multi-token (nt=1..8), in the same separate-TU pattern as kq_avx1.cpp: compiled -msse4.2 -mavx -mno-avx2 -mno-fma -mno-f16c (a #error guards against FMA/F16C leaking into the object), gated at run time behind cpu_avx1_ok() and only when AVX2 is absent - no AVX2/AVX-512 kernel is touched, so no mainstream path changes rounding or speed.
  • 128-bit integer lanes (pshufb/sign_epi8/maddubs are SSSE3), 256-bit float accumulation, software half-to-float (no F16C), no gathers (the non-gather path; vpgatherdd is AVX2).
  • q8k_quant_avx1: the byte-identical Q8_K activation quantizer in SSE4.2/AVX1 (the AVX-2 copy's 256-bit packs become SSE2 packs, which need no lane-crossing fixup at 128-bit). 2.0x over ggml's scalar reference.
  • Dispatch in native_expert.cpp: gate/up rows behind the same mt_min policy as the AVX-2 kernels (STRATA_IQ_MT_MIN, default 1 on the AVX1 tier - measured below), down rows always; kill switch STRATA_NO_IQ128=1.

Two formats are built, tested, and deliberately NOT dispatched

Both are measured negative results, kept in the tree (built + parity-tested) so the question does not need re-answering:

  • IQ4_XS gate/up: ggml's generic dot auto-vectorizes well and beats the 128-bit grid kernel (0.75-0.84x at nt=1).
  • IQ4_NL down: unlike the other i-quants, ggml-cpu has a hand-written __AVX__ dot for IQ4_NL (two blocks per iteration, 256-bit float accumulation) - not the scalar generic. It beats the 128-bit kernel 1.15-1.2x even after matching its two-block shape and sharing the decode across tokens.

Every live path is the measured-best of {iq_avx1, ggml's best available path} on AVX1 hardware.

Tests

  • iq_avx1_parity (new, registered in CTest): all formats, nt=1..8 - every row bit-identical across widths (#152) and within the repo's 1e-5 aggregate bar against ggml's dots. It calls the kernels directly, so it exercises them on AVX2/AVX-512 CI as-is. The bench mode prints gate/up and down ratios separately (a combined ratio once hid a 0.78x down path inside a 2.1x line - see the commit history).
  • q8k_quant_parity extended: the AVX1 copy is byte-identical to quantize_row_q8_K_ref over all 16 degenerate/NaN/inf cases x 4 sizes; registered with a STRATA_FORCE_ISA=avx variant.
  • iq_multi_parity, router_top10_parity, router_dot_parity, dequant_s2_parity, dequant_q5_1_test, expert_layout_test pass on this machine.
  • Engine-level, end to end: strata serve on the hardware below runs with the kernels active (the startup line reports the expert kernels run on AVX1 128-bit (iq_avx1, the older-CPU build)), the start's GPU-vs-CPU expert check (slot 0 verified) passes, and it has served real traffic since.

Measured on the hardware this exists for

Intel Xeon E5-2470 v2 (Ivy Bridge, AVX1 only, 10c/20t), 78 GB DDR3 (5 of 6 DIMM slots), RTX 5060 Ti 16 GB, Qwen3.8-Flash-Next IQ3_S native pack, engine 0.1.41.

Per expert, weights streamed from DRAM, one thread (iq_avx1_parity --bench), gate/up and down measured separately (ours / ggml's best path on this CPU):

pair (gate/up / down) gu nt=1 gu nt=2 gu nt=4 down nt=1 down nt=4
iq3_xxs / iq4_nl 1.1-1.2x 1.5-2.1x 2.1-2.7x (ggml, declined) (ggml)
iq3_s / iq4_nl 1.3-1.4x 1.7-2.1x 2.7x (ggml, declined) (ggml)
iq2_s / iq4_nl 1.1-1.2x 1.5-1.6x 2.4x (ggml, declined) (ggml)
iq3_xxs / q2_0 1.1x 1.7x 2.6x 4.1x 4.5x
iq3_s / q2_0 1.4x 2.2x 2.7x 5.0x 5.2x
iq2_s / q2_0 1.2x 1.7x 2.3x 4.1x 3.8x

At nt=1 the gate/up rows run two rows per pass (independent decode chains in the same registers; the decode is latency-bound at one token), and the float stage uses a single fused cvtepi32 of the exact int32 sum. STRATA_IQ_MT_MIN=1 on the AVX1 tier: unlike the AVX-2 policy (#152), the AVX1 gate/up kernel beats ggml's generic even at one token (1.1-1.4x), so the draft window is not needed to amortize the decode.

Serve, same 5276-token synthetic prompt, greedy, 150 tokens, before -> after: cold prefill 430 -> 581-607 tok/s, warm-reuse decode 22-25 -> 35-46 tok/s (37-38 steady after the follow-up commits); real interactive traffic 22-23 -> 25-26.5 tok/s. Decode wall-time on this rig is GPU/PCIe-bound (about a third of decode CPU time is pool workers waiting on the GPU), so the kernel wins land mostly in prefill and in freeing CPU for the GPU feed path.

Found while testing (pre-existing)

pool_tasks_test dies with SIGILL on non-AVX-512 machines even on a clean tree (v0.1.41, this hardware): it predates the ISA floor and reaches an AVX-512 path unconditionally. Filed as #1699, fixed in #1701 (adds the missing AVX1 rungs for the q2 bitplane paths).


🔄 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/1697 **Author:** [@nashcap](https://github.com/nashcap) **Created:** 10/9/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `avx1-iq-kernels` --- ### 📝 Commits (6) - [`c7db61b`](https://github.com/Niko1221/Strata/commit/c7db61b79c1771d8db5086a4b8f790e6e2d6778f) cpu: AVX1 128-bit expert kernels (iq_avx1) - the i-quant rows on CPUs with no AVX2 - [`0e11122`](https://github.com/Niko1221/Strata/commit/0e11122137e011e45bcb63a0bb56c5031ed05c4a) cpu: iq_avx1 - IQ4_XS gate/up stays on ggml's generic dot (the 128-bit grid kernel measures 0.7-1.0x against its auto-vectorized generic on an E5-2470 v2; the kernel stays built and parity-tested, only the dispatch declines it) - [`cd72eae`](https://github.com/Niko1221/Strata/commit/cd72eae78425970304b6e50dcd1f7fe84e36eee1) cpu: q2_avx1 - the Q2_0 rows and the Q8_1 quantizer for CPUs with AVX but no AVX2 (fixes #1699) - [`2220528`](https://github.com/Niko1221/Strata/commit/222052822b68d8b3040bf08303dabe719deb7747) cpu: AVX1 kernel micro-optimizations, all bit-exact (parity re-verified) - [`ab034c2`](https://github.com/Niko1221/Strata/commit/ab034c2aa67cc0658032ca845fd662a6f82b998c) cpu: AVX1 - decline IQ4_NL down rows, two-block iq4_nl kernel, fused cvt, split bench - [`c4a3634`](https://github.com/Niko1221/Strata/commit/c4a36346f5015528df0fd398a8f080643ed80831) cpu: AVX1 kernel audit - row-pair only at NT=1, drop dead dot_rows ### 📊 Changes **12 files changed** (+1399 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+21 -1) 📝 `include/strata/kernels/cpu/expert.hpp` (+8 -0) 📝 `include/strata/kernels/cpu/expert_layout.hpp` (+3 -0) ➕ `include/strata/kernels/cpu/iq_avx1.hpp` (+37 -0) 📝 `src/kernels/cpu/expert_layout.cpp` (+11 -2) ➕ `src/kernels/cpu/iq_avx1.cpp` (+650 -0) ➕ `src/kernels/cpu/iq_avx1_parity.cpp` (+303 -0) 📝 `src/kernels/cpu/native_expert.cpp` (+27 -0) ➕ `src/kernels/cpu/q2_avx1.cpp` (+304 -0) 📝 `src/kernels/cpu/q2_bitplane_parity.cpp` (+23 -11) 📝 `src/kernels/cpu/q8k_quant_parity.cpp` (+8 -4) 📝 `src/program/generate.cpp` (+4 -1) </details> ### 📄 Description ## What Completes the AVX1 floor for native packs. Since #394 an AVX-only CPU (Sandy Bridge through Ivy Bridge Xeons) starts and runs a native pack, but every i-quant dot in ggml-cpu is `#if defined(__AVX2__)` and falls to the scalar `_generic` path, and the fork's Q2_0 has no vectorized dot either - so the CPU-computed experts of a native pack run scalar there. This adds the missing sibling of `kq_avx1.cpp` for the expert rows: - `src/kernels/cpu/iq_avx1.cpp` (+ header): gate/up for **IQ3_XXS, IQ3_S, IQ2_S** and down for **Q2_0**, multi-token (nt=1..8), in the same separate-TU pattern as `kq_avx1.cpp`: compiled `-msse4.2 -mavx -mno-avx2 -mno-fma -mno-f16c` (a `#error` guards against FMA/F16C leaking into the object), gated at run time behind `cpu_avx1_ok()` **and only when AVX2 is absent** - no AVX2/AVX-512 kernel is touched, so no mainstream path changes rounding or speed. - 128-bit integer lanes (`pshufb`/`sign_epi8`/`maddubs` are SSSE3), 256-bit float accumulation, software half-to-float (no F16C), no gathers (the non-gather path; `vpgatherdd` is AVX2). - `q8k_quant_avx1`: the byte-identical Q8_K activation quantizer in SSE4.2/AVX1 (the AVX-2 copy's 256-bit packs become SSE2 packs, which need no lane-crossing fixup at 128-bit). 2.0x over ggml's scalar reference. - Dispatch in `native_expert.cpp`: gate/up rows behind the same `mt_min` policy as the AVX-2 kernels (`STRATA_IQ_MT_MIN`, default **1** on the AVX1 tier - measured below), down rows always; kill switch `STRATA_NO_IQ128=1`. ## Two formats are built, tested, and deliberately NOT dispatched Both are measured negative results, kept in the tree (built + parity-tested) so the question does not need re-answering: - **IQ4_XS gate/up**: ggml's generic dot auto-vectorizes well and beats the 128-bit grid kernel (0.75-0.84x at nt=1). - **IQ4_NL down**: unlike the other i-quants, ggml-cpu has a **hand-written `__AVX__` dot** for IQ4_NL (two blocks per iteration, 256-bit float accumulation) - not the scalar generic. It beats the 128-bit kernel 1.15-1.2x even after matching its two-block shape and sharing the decode across tokens. Every live path is the measured-best of {iq_avx1, ggml's best available path} on AVX1 hardware. ## Tests - `iq_avx1_parity` (new, registered in CTest): all formats, nt=1..8 - every row bit-identical across widths (#152) and within the repo's 1e-5 aggregate bar against ggml's dots. It calls the kernels directly, so it exercises them on AVX2/AVX-512 CI as-is. The bench mode prints gate/up and down ratios **separately** (a combined ratio once hid a 0.78x down path inside a 2.1x line - see the commit history). - `q8k_quant_parity` extended: the AVX1 copy is byte-identical to `quantize_row_q8_K_ref` over all 16 degenerate/NaN/inf cases x 4 sizes; registered with a `STRATA_FORCE_ISA=avx` variant. - `iq_multi_parity`, `router_top10_parity`, `router_dot_parity`, `dequant_s2_parity`, `dequant_q5_1_test`, `expert_layout_test` pass on this machine. - Engine-level, end to end: `strata serve` on the hardware below runs with the kernels active (the startup line reports `the expert kernels run on AVX1 128-bit (iq_avx1, the older-CPU build)`), the start's GPU-vs-CPU expert check (`slot 0 verified`) passes, and it has served real traffic since. ## Measured on the hardware this exists for Intel **Xeon E5-2470 v2** (Ivy Bridge, AVX1 only, 10c/20t), 78 GB DDR3 (5 of 6 DIMM slots), RTX 5060 Ti 16 GB, Qwen3.8-Flash-Next IQ3_S native pack, engine 0.1.41. Per expert, weights streamed from DRAM, one thread (`iq_avx1_parity --bench`), gate/up and down measured separately (ours / ggml's best path on this CPU): | pair (gate/up / down) | gu nt=1 | gu nt=2 | gu nt=4 | down nt=1 | down nt=4 | |---|---|---|---|---|---| | iq3_xxs / iq4_nl | 1.1-1.2x | 1.5-2.1x | 2.1-2.7x | (ggml, declined) | (ggml) | | iq3_s / iq4_nl | 1.3-1.4x | 1.7-2.1x | 2.7x | (ggml, declined) | (ggml) | | iq2_s / iq4_nl | 1.1-1.2x | 1.5-1.6x | 2.4x | (ggml, declined) | (ggml) | | iq3_xxs / q2_0 | 1.1x | 1.7x | 2.6x | **4.1x** | 4.5x | | iq3_s / q2_0 | 1.4x | 2.2x | 2.7x | **5.0x** | 5.2x | | iq2_s / q2_0 | 1.2x | 1.7x | 2.3x | **4.1x** | 3.8x | At nt=1 the gate/up rows run two rows per pass (independent decode chains in the same registers; the decode is latency-bound at one token), and the float stage uses a single fused `cvtepi32` of the exact int32 sum. `STRATA_IQ_MT_MIN=1` on the AVX1 tier: unlike the AVX-2 policy (#152), the AVX1 gate/up kernel beats ggml's generic even at one token (1.1-1.4x), so the draft window is not needed to amortize the decode. Serve, same 5276-token synthetic prompt, greedy, 150 tokens, before -> after: cold prefill **430 -> 581-607 tok/s**, warm-reuse decode **22-25 -> 35-46 tok/s** (37-38 steady after the follow-up commits); real interactive traffic 22-23 -> 25-26.5 tok/s. Decode wall-time on this rig is GPU/PCIe-bound (about a third of decode CPU time is pool workers waiting on the GPU), so the kernel wins land mostly in prefill and in freeing CPU for the GPU feed path. ## Found while testing (pre-existing) `pool_tasks_test` dies with SIGILL on non-AVX-512 machines even on a clean tree (v0.1.41, this hardware): it predates the ISA floor and reaches an AVX-512 path unconditionally. Filed as #1699, fixed in #1701 (adds the missing AVX1 rungs for the q2 bitplane paths). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 15:29:34 +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#3616
No description provided.