[PR #4944] [Engine] Table-lookup Q2_0 rows for Sandy / Ivy Bridge (STRATA_Q2_LUT=1, opt-in) #5259

Open
opened 2026-11-07 16:33:52 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4944
Author: @hiyazakite
Created: 11/1/2026
Status: 🔄 Open

Base: main ← Head: cpu-q2-lut


📝 Commits (2)

  • 47fc760 [CPU] Q2_0 native experts on the AVX1 rung (Sandy / Ivy Bridge)
  • 59089ab [CPU] Table-lookup Q2_0 rows for Sandy / Ivy Bridge (STRATA_Q2_LUT=1, opt-in)

📊 Changes

11 files changed (+790 additions, -11 deletions)

View changed files

📝 CMakeLists.txt (+20 -0)
📝 include/strata/kernels/cpu/expert_layout.hpp (+5 -4)
➕ include/strata/kernels/cpu/q2_lut.hpp (+42 -0)
📝 src/core/expert_source.cpp (+5 -0)
📝 src/kernels/cpu/kq_cpu_bench.cpp (+34 -3)
📝 src/kernels/cpu/pool.cpp (+13 -4)
➕ src/kernels/cpu/q2_lut.cpp (+255 -0)
➕ src/kernels/cpu/q2_lut_avx1.cpp (+210 -0)
➕ src/kernels/cpu/q2_lut_kernels.hpp (+33 -0)
➕ src/kernels/cpu/q2_lut_snb.cpp (+4 -0)
➕ src/kernels/cpu/q2_native_parity.cpp (+169 -0)

📄 Description

Problem and result

On Sandy Bridge and Ivy Bridge CPUs, pmaddubsw, pmaddwd, vector shifts, and mulps all issue exclusively on execution Port 0. As a result, the multiply-accumulate rows in q2_avx1.cpp are Port 0 bottlenecked (~2.4 weight x activation products per cycle on an E5-2603 core, ~7.2 GB/s on dual 4-core sockets).

This opt-in feature (STRATA_Q2_LUT=1) replaces the multiply-add loop with pshufb table lookups on Ports 1 and 5. Standalone weight bandwidth improves from 7.2 GB/s to 17.1 GB/s (2.4x) on the dual E5-2603, and CPU-side decode layer throughput increases 2.5x.

Continuation of the AVX1 lane work in #4854 and #4858.

Approach

  • Port-balanced table lookups: For each token and pair of activations, a 16-entry table is precomputed as an A and B byte plane (T + 762 = 32A + B). Four lookups accumulate directly in bytes without overflow (paddb on Ports 1/5), followed by punpck and pmaddubsw(..., [1, 32]) on Port 0.
  • Mirror representation: Expert weights are transposed into 16-row vector tiles on first use (same size as GGUF rows). Mirror creation is thread-safe and performed tile-by-tile without fallback to ensure deterministic float summation order.
  • Stable-range safety: Only weights residing in registered stable ranges (ArenaExpertSource) are mirrored; dynamic buffers bypass the rung and fall back to q2_rows_any.
  • Targeted builds: Separate units for Ivy Bridge (q2_lut_avx1.cpp, with hardware F16C) and Sandy Bridge (q2_lut_snb.cpp, software fp16 conversion), selected at runtime via CPUID.
  • Opt-in only: Triggered only when STRATA_Q2_LUT=1 on AVX1 CPUs, or STRATA_Q2_LUT=force on any AVX CPU. Default execution path remains 100% bit-identical.

Validation

Passed:

  • q2_native_parity: Passes with STRATA_Q2_LUT=force. Tested against ggml-cpu vec_dot and exact dot on E5-2603: relative error 4.7e-3..6.0e-3. Ragged row ranges and single-token slices match whole calls bitwise. Registered as ctest q2_native_parity_lut.
  • kq_cpu_bench on dual Xeon E5-2603 (8 threads):
    • 1 token: 7.2 GB/s -> 17.1 GB/s of weights (0.645 ms/expert vs 1.527 ms/expert)
    • 4 tokens: 2.7 GB/s -> 5.3 GB/s of weights
  • Server-side prefill quality: facts accuracy 92.3% (vs 91.5% baseline), KLD 0.094.
  • NIAH 128k context sweep: 15/15 successful passes on arm A (layer-major) and 5/5 on arm B, with 0 invalid rows and 0 stalls.
  • End-to-end standard benchmark (bench/prompts/standard-v1 on live server): 12/12 sanity checks passed (tool-call checks match reference behavior), decode 30-35 tok/s paired with a single 5060 8 GB and 128 GB quad channel per NUMA node DDR3.

Failed:

  • None observed.

Not run:

  • Non-x86 architectures (changes isolated to STRATA_CPU_X86 and x86 AVX1 translation units).
  • Full end-to-end A/B sweep of strata bench run (only standalone layer bench and live server decode measured so far).

Limits

  • Opt-in only via STRATA_Q2_LUT=1.
  • Mirror creation requires memory allocation matching the size of the CPU-resident Q2_0 experts (~4.3 GB for standard models).
  • The 4-token LUT kernel is uop-cache bound on Sandy Bridge (legacy decoders supply ~66% of uops), so scaling from 1 to 4 tokens on Sandy Bridge is limited compared to AVX2/AVX-512.
  • Not yet tested on Ivy Bridge CPU. I have 2 x 2650L v2 on order and can complement the tests with these soon.

🔄 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/4944 **Author:** [@hiyazakite](https://github.com/hiyazakite) **Created:** 11/1/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `cpu-q2-lut` --- ### 📝 Commits (2) - [`47fc760`](https://github.com/Niko1221/Strata/commit/47fc760ad8216887ed852bec8d81009d7ffced78) [CPU] Q2_0 native experts on the AVX1 rung (Sandy / Ivy Bridge) - [`59089ab`](https://github.com/Niko1221/Strata/commit/59089ab72bcf5db9ca9bc15a706b17ab61d97f0e) [CPU] Table-lookup Q2_0 rows for Sandy / Ivy Bridge (STRATA_Q2_LUT=1, opt-in) ### 📊 Changes **11 files changed** (+790 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+20 -0) 📝 `include/strata/kernels/cpu/expert_layout.hpp` (+5 -4) ➕ `include/strata/kernels/cpu/q2_lut.hpp` (+42 -0) 📝 `src/core/expert_source.cpp` (+5 -0) 📝 `src/kernels/cpu/kq_cpu_bench.cpp` (+34 -3) 📝 `src/kernels/cpu/pool.cpp` (+13 -4) ➕ `src/kernels/cpu/q2_lut.cpp` (+255 -0) ➕ `src/kernels/cpu/q2_lut_avx1.cpp` (+210 -0) ➕ `src/kernels/cpu/q2_lut_kernels.hpp` (+33 -0) ➕ `src/kernels/cpu/q2_lut_snb.cpp` (+4 -0) ➕ `src/kernels/cpu/q2_native_parity.cpp` (+169 -0) </details> ### 📄 Description ## Problem and result On Sandy Bridge and Ivy Bridge CPUs, `pmaddubsw`, `pmaddwd`, vector shifts, and `mulps` all issue exclusively on execution Port 0. As a result, the multiply-accumulate rows in `q2_avx1.cpp` are Port 0 bottlenecked (~2.4 weight x activation products per cycle on an E5-2603 core, ~7.2 GB/s on dual 4-core sockets). This opt-in feature (`STRATA_Q2_LUT=1`) replaces the multiply-add loop with `pshufb` table lookups on Ports 1 and 5. Standalone weight bandwidth improves from 7.2 GB/s to 17.1 GB/s (2.4x) on the dual E5-2603, and CPU-side decode layer throughput increases 2.5x. ## Related discussion Continuation of the AVX1 lane work in #4854 and #4858. ## Approach - **Port-balanced table lookups**: For each token and pair of activations, a 16-entry table is precomputed as an A and B byte plane ($T + 762 = 32A + B$). Four lookups accumulate directly in bytes without overflow (`paddb` on Ports 1/5), followed by `punpck` and `pmaddubsw(..., [1, 32])` on Port 0. - **Mirror representation**: Expert weights are transposed into 16-row vector tiles on first use (same size as GGUF rows). Mirror creation is thread-safe and performed tile-by-tile without fallback to ensure deterministic float summation order. - **Stable-range safety**: Only weights residing in registered stable ranges (`ArenaExpertSource`) are mirrored; dynamic buffers bypass the rung and fall back to `q2_rows_any`. - **Targeted builds**: Separate units for Ivy Bridge (`q2_lut_avx1.cpp`, with hardware F16C) and Sandy Bridge (`q2_lut_snb.cpp`, software fp16 conversion), selected at runtime via CPUID. - **Opt-in only**: Triggered only when `STRATA_Q2_LUT=1` on AVX1 CPUs, or `STRATA_Q2_LUT=force` on any AVX CPU. Default execution path remains 100% bit-identical. ## Validation **Passed:** - `q2_native_parity`: Passes with `STRATA_Q2_LUT=force`. Tested against ggml-cpu `vec_dot` and exact dot on E5-2603: relative error 4.7e-3..6.0e-3. Ragged row ranges and single-token slices match whole calls bitwise. Registered as `ctest q2_native_parity_lut`. - `kq_cpu_bench` on dual Xeon E5-2603 (8 threads): - 1 token: 7.2 GB/s -> 17.1 GB/s of weights (0.645 ms/expert vs 1.527 ms/expert) - 4 tokens: 2.7 GB/s -> 5.3 GB/s of weights - Server-side prefill quality: facts accuracy 92.3% (vs 91.5% baseline), KLD 0.094. - NIAH 128k context sweep: 15/15 successful passes on arm A (layer-major) and 5/5 on arm B, with 0 invalid rows and 0 stalls. - End-to-end standard benchmark (`bench/prompts/standard-v1` on live server): 12/12 sanity checks passed (tool-call checks match reference behavior), decode 30-35 tok/s paired with a single 5060 8 GB and 128 GB quad channel per NUMA node DDR3. **Failed:** - None observed. **Not run:** - Non-x86 architectures (changes isolated to `STRATA_CPU_X86` and x86 AVX1 translation units). - Full end-to-end A/B sweep of `strata bench run` (only standalone layer bench and live server decode measured so far). ## Limits - Opt-in only via `STRATA_Q2_LUT=1`. - Mirror creation requires memory allocation matching the size of the CPU-resident Q2_0 experts (~4.3 GB for standard models). - The 4-token LUT kernel is uop-cache bound on Sandy Bridge (legacy decoders supply ~66% of uops), so scaling from 1 to 4 tokens on Sandy Bridge is limited compared to AVX2/AVX-512. - Not yet tested on Ivy Bridge CPU. I have 2 x 2650L v2 on order and can complement the tests with these soon. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#5259
No description provided.