[PR #3946] [Engine] faster Q2_0 expert rows on AVX-512 full 8-block groups (bit-exact kernel) #4845

Open
opened 2026-11-07 15:39:28 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3946
Author: @Ext3h
Created: 10/24/2026
Status: 🔄 Open

Base: main ← Head: opt/q2-0-rows


📝 Commits (4)

  • 16c857f cpu: faster q2_0 expert rows for full 8-block groups (bit-exact)
  • 635dc65 bench: the portable q2_0 rows microbenchmark that checks v2 bit for bit and times it
  • 3b46bce build: a CTest entry for the q2_0 rows bit-exact sweep
  • 0e217b6 bench: q2_0 rows measured against the original on a Ryzen 7 9800X3D

📊 Changes

14 files changed (+929 additions, -3 deletions)

View changed files

📝 CMakeLists.txt (+12 -0)
➕ bench/micro/q2g_rows_bench.cpp (+497 -0)
➕ bench/results/2026-10-24-q2_0-rows-kernel/README.md (+70 -0)
➕ bench/results/2026-10-24-q2_0-rows-kernel/q2g_ctest.txt (+24 -0)
➕ bench/results/2026-10-24-q2_0-rows-kernel/q2g_isa_guard.txt (+4 -0)
➕ bench/results/2026-10-24-q2_0-rows-kernel/q2g_rows_bench-clang-run2.txt (+27 -0)
➕ bench/results/2026-10-24-q2_0-rows-kernel/q2g_rows_bench-clang.txt (+27 -0)
➕ bench/results/2026-10-24-q2_0-rows-kernel/q2g_rows_bench-msvc-run2.txt (+27 -0)
➕ bench/results/2026-10-24-q2_0-rows-kernel/q2g_rows_bench-msvc.txt (+27 -0)
📝 include/strata/kernels/cpu/expert.hpp (+4 -0)
📝 metal/src/engine/stubs.cpp (+1 -0)
📝 src/kernels/cpu/expert.cpp (+204 -0)
📝 src/kernels/cpu/expert_layout.cpp (+3 -2)
📝 tools/check_isa_guard.py (+2 -1)

📄 Description

Summary

Adds q2_0_gguf_rows_multi_v2, an AVX-512 kernel that runs a full eight-block Q2_0 group as a written-out straight-line body instead of the masked path, and routes the q2_rows_any rungs to it. Warm full-group rows take 0.516-0.877 of the original's time on clang and 0.360-1.010 on MSVC. "Bit-exact" means 320 seeded harness cases compare equal with memcmp, not a proof over all inputs.

Why

The original sends every group through the masked tail: an eight-trip scalar scale gather and a runtime block loop. MSVC rejects #pragma clang loop unroll(full) with C4068, so the steps are written out; the tail was slower than the original's single loop, so rows with fewer than eight blocks keep the original.

Changes

  • Kernel: q2_0_gguf_rows_multi_v2 (token-major up to four tokens, block-major from five), the dispatch rungs, the Metal host stub, and the WIDE_PUBLIC guard entry.
  • Harness: bench/micro/q2g_rows_bench.cpp builds on POSIX and Windows, times v2 against the original, and exits 77 before either kernel on a CPU without AVX-512 (--validate-only runs the sweep alone).
  • Build: the CTest entry q2g_rows_bench_validate, with SKIP_RETURN_CODE 77.
  • Results: bench/results/2026-10-24-q2_0-rows-kernel/.
clang++ -std=c++17 -O3 -Iinclude -c bench/micro/q2g_rows_bench.cpp -o q2g_rows_bench.o
clang++ -std=c++17 -O3 -mavx512f -mavx512bw -mavx512vl -mavx512dq -mavx512vnni -mavx512vbmi -mf16c -mfma -Iinclude -c src/kernels/cpu/expert.cpp -o expert.o
clang++ -pthread q2g_rows_bench.o expert.o -o q2g_bench.exe

Results

v2/original; each cell spans the first run and a second run of the same binaries:

clang MSVC
warm full groups (nblocks >= 8) 0.516-0.877 0.360-1.010
cold 0.567-0.890 0.377-1.003
fewer than 8 blocks (original in both arms) 0.999-1.001 1.000-1.008

Medians and ratios come from the wall clock; the TSC only feeds cyc/op. The full table and both runs are in bench/results/2026-10-24-q2_0-rows-kernel/README.md.

Validation

Passed:

  • clang 23.1.4 and MSVC 19.51.36260 build the harness and the kernel (kernel at /arch:AVX512, bench TU at baseline flags).
  • 320/320 cases bit-exact on both compilers, no tolerance.
  • ISA guard on the MSVC build: 550 functions, 23 hold wide-ISA instructions, 0 not allowed (exit 0); tools.test_check_isa_guard passes 10 tests.
  • CTest: Passed; with STRATA_FORCE_AVX2=1 the same test is Skipped (exit 77 before any kernel).
  • The Metal stub defines the mangled symbol the expert_layout object leaves undefined.

Failed: none.

Not run:

  • Any POSIX build: no toolchain on this host and WSL does not start.
  • The macOS link (checked by symbol name only), and the full CMake suite (only the one CTest entry was run).
  • End-to-end model time, the other backends, and the AVX2/AVX1 rungs.

Limits

  • One machine: Ryzen 7 9800X3D, Windows 11, a 128 MiB flush against a 96 MB L3.
  • One synthetic weight layout; the harness calls the two exported kernels directly, not the dispatch path, and bit-exactness is kernel-level memcmp over 320 seeded cases, not full-model logits.

CI

Intake passes; Title area and Metal build are action_required on the fork, and no job has compiled this branch.

— Claude


🔄 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/3946 **Author:** [@Ext3h](https://github.com/Ext3h) **Created:** 10/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `opt/q2-0-rows` --- ### 📝 Commits (4) - [`16c857f`](https://github.com/Niko1221/Strata/commit/16c857f02f7636d05ba36bc81e44494080e2cfd8) cpu: faster q2_0 expert rows for full 8-block groups (bit-exact) - [`635dc65`](https://github.com/Niko1221/Strata/commit/635dc657fe76e2c628dab1fd637c8feb4f15a431) bench: the portable q2_0 rows microbenchmark that checks v2 bit for bit and times it - [`3b46bce`](https://github.com/Niko1221/Strata/commit/3b46bce790b91c8b14a2d076f533aaf818d9fe14) build: a CTest entry for the q2_0 rows bit-exact sweep - [`0e217b6`](https://github.com/Niko1221/Strata/commit/0e217b6bca97c52532634be941a2fc9965e49008) bench: q2_0 rows measured against the original on a Ryzen 7 9800X3D ### 📊 Changes **14 files changed** (+929 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+12 -0) ➕ `bench/micro/q2g_rows_bench.cpp` (+497 -0) ➕ `bench/results/2026-10-24-q2_0-rows-kernel/README.md` (+70 -0) ➕ `bench/results/2026-10-24-q2_0-rows-kernel/q2g_ctest.txt` (+24 -0) ➕ `bench/results/2026-10-24-q2_0-rows-kernel/q2g_isa_guard.txt` (+4 -0) ➕ `bench/results/2026-10-24-q2_0-rows-kernel/q2g_rows_bench-clang-run2.txt` (+27 -0) ➕ `bench/results/2026-10-24-q2_0-rows-kernel/q2g_rows_bench-clang.txt` (+27 -0) ➕ `bench/results/2026-10-24-q2_0-rows-kernel/q2g_rows_bench-msvc-run2.txt` (+27 -0) ➕ `bench/results/2026-10-24-q2_0-rows-kernel/q2g_rows_bench-msvc.txt` (+27 -0) 📝 `include/strata/kernels/cpu/expert.hpp` (+4 -0) 📝 `metal/src/engine/stubs.cpp` (+1 -0) 📝 `src/kernels/cpu/expert.cpp` (+204 -0) 📝 `src/kernels/cpu/expert_layout.cpp` (+3 -2) 📝 `tools/check_isa_guard.py` (+2 -1) </details> ### 📄 Description ## Summary Adds `q2_0_gguf_rows_multi_v2`, an AVX-512 kernel that runs a full eight-block Q2_0 group as a written-out straight-line body instead of the masked path, and routes the `q2_rows_any` rungs to it. Warm full-group rows take 0.516-0.877 of the original's time on clang and 0.360-1.010 on MSVC. "Bit-exact" means 320 seeded harness cases compare equal with `memcmp`, not a proof over all inputs. ## Why The original sends every group through the masked tail: an eight-trip scalar scale gather and a runtime block loop. MSVC rejects `#pragma clang loop unroll(full)` with C4068, so the steps are written out; the tail was slower than the original's single loop, so rows with fewer than eight blocks keep the original. ## Changes - Kernel: `q2_0_gguf_rows_multi_v2` (token-major up to four tokens, block-major from five), the dispatch rungs, the Metal host stub, and the `WIDE_PUBLIC` guard entry. - Harness: `bench/micro/q2g_rows_bench.cpp` builds on POSIX and Windows, times v2 against the original, and exits 77 before either kernel on a CPU without AVX-512 (`--validate-only` runs the sweep alone). - Build: the CTest entry `q2g_rows_bench_validate`, with `SKIP_RETURN_CODE 77`. - Results: `bench/results/2026-10-24-q2_0-rows-kernel/`. ```sh clang++ -std=c++17 -O3 -Iinclude -c bench/micro/q2g_rows_bench.cpp -o q2g_rows_bench.o clang++ -std=c++17 -O3 -mavx512f -mavx512bw -mavx512vl -mavx512dq -mavx512vnni -mavx512vbmi -mf16c -mfma -Iinclude -c src/kernels/cpu/expert.cpp -o expert.o clang++ -pthread q2g_rows_bench.o expert.o -o q2g_bench.exe ``` ## Results v2/original; each cell spans the first run and a second run of the same binaries: | | clang | MSVC | |---|---|---| | warm full groups (`nblocks >= 8`) | 0.516-0.877 | 0.360-1.010 | | cold | 0.567-0.890 | 0.377-1.003 | | fewer than 8 blocks (original in both arms) | 0.999-1.001 | 1.000-1.008 | Medians and ratios come from the wall clock; the TSC only feeds `cyc/op`. The full table and both runs are in [bench/results/2026-10-24-q2_0-rows-kernel/README.md](https://github.com/Ext3h/Strata/tree/0e217b6bca97c52532634be941a2fc9965e49008/bench/results/2026-10-24-q2_0-rows-kernel/README.md). ## Validation Passed: - clang 23.1.4 and MSVC 19.51.36260 build the harness and the kernel (kernel at `/arch:AVX512`, bench TU at baseline flags). - 320/320 cases bit-exact on both compilers, no tolerance. - ISA guard on the MSVC build: 550 functions, 23 hold wide-ISA instructions, 0 not allowed (exit 0); `tools.test_check_isa_guard` passes 10 tests. - CTest: `Passed`; with `STRATA_FORCE_AVX2=1` the same test is `Skipped` (exit 77 before any kernel). - The Metal stub defines the mangled symbol the `expert_layout` object leaves undefined. Failed: none. Not run: - Any POSIX build: no toolchain on this host and WSL does not start. - The macOS link (checked by symbol name only), and the full CMake suite (only the one CTest entry was run). - End-to-end model time, the other backends, and the AVX2/AVX1 rungs. ## Limits - One machine: Ryzen 7 9800X3D, Windows 11, a 128 MiB flush against a 96 MB L3. - One synthetic weight layout; the harness calls the two exported kernels directly, not the dispatch path, and bit-exactness is kernel-level `memcmp` over 320 seeded cases, not full-model logits. ## CI Intake passes; `Title area` and `Metal build` are `action_required` on the fork, and no job has compiled this branch. — Claude --- <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#4845
No description provided.