[PR #4854] [CLOSED] AVX1 Q2_0 Native Rows #5212

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

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4854
Author: @Keoian
Created: 10/31/2026
Status: ❌ Closed

Base: main ← Head: avx1-q2-native-rows


📝 Commits (2)

  • 43e390e [Engine] AVX-only CPUs: a native pack's Q2_0 layers on Strata's AVX1 Q2_0 rows instead of ggml-cpu's vec_dot
  • 7a68664 [Engine] AVX1 Q2_0 rows: the bit-plane pair's float part on 256 bits, a branch-free scale conversion

📊 Changes

2 files changed (+52 additions, -31 deletions)

View changed files

📝 include/strata/kernels/cpu/expert_layout.hpp (+5 -4)
📝 src/kernels/cpu/q2_avx1.cpp (+47 -27)

📄 Description

Problem and result

On a CPU with AVX but no AVX2 (the experimental older-CPU source build, STRATA_ISA_FLOOR=avx), q2_native_kernels() admitted AVX2 only. Every native pack's Q2_0 expert therefore ran on ggml-cpu's Q2_0 vec_dot and never reached Strata's AVX1 rows in q2_avx1.cpp. With this PR those layers take the AVX1 rung of q2_rows_any / act_quant_any, and the AVX1 bit-plane row does its float work on 256 bits.

On an i7-3770, a one-token Q2_0 expert goes from 0.975 to 0.297 ms per thread with STRATA_Q2_BITPLANE=1 (0.487 ms without it). End to end on 2x RX 5700 XT with Qwen3.8-Flash-Next Q2_0, decode goes from 14.97 to 22.38 tok/s (+49.6%).

#4728. #1824 proposed the same gate change and was closed as covered by q2_avx1.cpp; the issue shows that the gate still sends native packs to ggml-cpu. #1699 / #1701 added the AVX1 rung.

Approach

  1. q2_native_kernels() also admits cpu_avx1_ok(). A CPU without AVX keeps ggml-cpu.
  2. row_bp_acc computes a bit-plane pair's float part (two 64-value blocks) in one 256-bit register: block 0 in the low lanes, block 1 in the high lanes. Each lane runs the same operations in the same order as the 128-bit form, so the bytes do not change. h2f_scale converts a normal fp16 block scale in four integer operations (h2f's normal branch, bit for bit); zero, subnormals, inf and NaN still take h2f.

Compatibility: on AVX-only CPUs the first commit changes bits against ggml-cpu's path, because the AVX1 rows sum floats in their own order (AVX2 CPUs already take Strata's rows). AVX2 and AVX-512 CPUs and the release engines (AVX2 baseline) run the same code as before.

Validation

Environment: i7-3770 (Ivy Bridge, AVX, no AVX2), 32 GB DDR3-1600, 2x RX 5700 XT 8 GB (gfx1010), Ubuntu with kernel 7.0, GCC 15.2, ROCm 7.14.0a20260612 (TheRock gfx101X-dgpu). HIP build: -DSTRATA_ENABLE_HIP=ON -DCMAKE_HIP_ARCHITECTURES=gfx1010 -DSTRATA_ISA_FLOOR=avx -DSTRATA_BUILD_TESTS=ON, base 870aedd9.

Passed:

  • Build of this branch (7a68664a): ninja strata kq_cpu_bench q2_bitplane_parity pool_tasks_test expert_layout_test native_arithmetic_parity iq_avx1_parity, 0 errors.

  • ctest -R '^(q2_bitplane_parity|pool_tasks_test|expert_layout_test|native_arithmetic_parity.*|iq_avx1_parity)$': 12 of 12 passed (ROCm's lib directory on LD_LIBRARY_PATH; expert_layout_test links the HIP runtime).

  • kq_cpu_bench --pairs q2_0/q2_0 --threads 1,4 --nt 1,3,4 --mb 1024 --reps 3, ms per expert per thread at 1 / 3 / 4 tokens:

    Build Legacy rows STRATA_Q2_BITPLANE=1
    main 0.975 / 2.89 / 3.78 0.975 / 2.89 / 3.79 (not used)
    commit 1 0.517 / 1.13 / 1.40 0.418 / 0.807 / 0.954
    commits 1 + 2 0.487 / 1.11 / 1.38 0.297 / 0.597 / 0.752
  • Same bytes for commit 2: FNV hashes of both kernels' outputs on fixed random blocks at 1-8 tokens are identical before and after, with STRATA_Q2_BITPLANE=1 and with the legacy rows. The harness is src/kernels/cpu/q2_avx1_parity.cpp on https://github.com/Keoian/Strata/tree/rx-5700-xt-optimizations and is not part of this PR.

  • Decode A/B against main (870aedd9): 2x RX 5700 XT 8 GB (layer split 27), Qwen3.8-Flash-Next ISTA-DASLab Q2_0, --spec 4 --spec-min-p 0.8 --mtp --kv int8, STRATA_Q2_BITPLANE=1, STRATA_HC_Q8=0. 3 prompts x 2 reps, 256 tokens greedy, a fresh engine start per arm, ABBA over two starts each. Mean 14.97 -> 22.38 tok/s (+49.6%), 11 of 12 pairs faster. The branch binary was built before the comment in expert_layout.hpp was corrected; the code is the same.

    Per-run decode tok/s
    • main: 8.6 14.2 17.2 12.6 15.9 17.7 14.5 14.0 18.1 12.8 15.6 18.4
    • this branch: 26.6 14.3 23.1 26.3 23.2 24.6 26.7 22.2 25.6 15.0 23.6 17.4

Failed:
none observed.

Not run:

  • CUDA and SYCL builds: no toolkit on this machine. The two files are CPU code that those builds compile too, so this PR stays a draft until someone can build them.
  • AVX2 / AVX-512 CPUs: none here. The change does not touch those paths.
  • A CPU without AVX (STRATA_ISA_FLOOR=none), Sandy Bridge, Windows / MSVC.

Limits

Measured on one CPU model (Ivy Bridge). Only Q2_0 changes; the i-quants keep their own AVX1 kernels.

🤖 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/4854 **Author:** [@Keoian](https://github.com/Keoian) **Created:** 10/31/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `avx1-q2-native-rows` --- ### 📝 Commits (2) - [`43e390e`](https://github.com/Niko1221/Strata/commit/43e390e0e37f3e69b423fd5deae96429f825e7ac) [Engine] AVX-only CPUs: a native pack's Q2_0 layers on Strata's AVX1 Q2_0 rows instead of ggml-cpu's vec_dot - [`7a68664`](https://github.com/Niko1221/Strata/commit/7a68664acae21c1f46e999c3a0d4b213c1d7d319) [Engine] AVX1 Q2_0 rows: the bit-plane pair's float part on 256 bits, a branch-free scale conversion ### 📊 Changes **2 files changed** (+52 additions, -31 deletions) <details> <summary>View changed files</summary> 📝 `include/strata/kernels/cpu/expert_layout.hpp` (+5 -4) 📝 `src/kernels/cpu/q2_avx1.cpp` (+47 -27) </details> ### 📄 Description ## Problem and result On a CPU with AVX but no AVX2 (the experimental older-CPU source build, `STRATA_ISA_FLOOR=avx`), `q2_native_kernels()` admitted AVX2 only. Every native pack's Q2_0 expert therefore ran on ggml-cpu's Q2_0 `vec_dot` and never reached Strata's AVX1 rows in `q2_avx1.cpp`. With this PR those layers take the AVX1 rung of `q2_rows_any` / `act_quant_any`, and the AVX1 bit-plane row does its float work on 256 bits. On an i7-3770, a one-token Q2_0 expert goes from 0.975 to 0.297 ms per thread with `STRATA_Q2_BITPLANE=1` (0.487 ms without it). End to end on 2x RX 5700 XT with Qwen3.8-Flash-Next Q2_0, decode goes from 14.97 to 22.38 tok/s (+49.6%). ## Related discussion #4728. #1824 proposed the same gate change and was closed as covered by `q2_avx1.cpp`; the issue shows that the gate still sends native packs to ggml-cpu. #1699 / #1701 added the AVX1 rung. ## Approach 1. `q2_native_kernels()` also admits `cpu_avx1_ok()`. A CPU without AVX keeps ggml-cpu. 2. `row_bp_acc` computes a bit-plane pair's float part (two 64-value blocks) in one 256-bit register: block 0 in the low lanes, block 1 in the high lanes. Each lane runs the same operations in the same order as the 128-bit form, so the bytes do not change. `h2f_scale` converts a normal fp16 block scale in four integer operations (h2f's normal branch, bit for bit); zero, subnormals, inf and NaN still take `h2f`. Compatibility: on AVX-only CPUs the first commit changes bits against ggml-cpu's path, because the AVX1 rows sum floats in their own order (AVX2 CPUs already take Strata's rows). AVX2 and AVX-512 CPUs and the release engines (AVX2 baseline) run the same code as before. ## Validation Environment: i7-3770 (Ivy Bridge, AVX, no AVX2), 32 GB DDR3-1600, 2x RX 5700 XT 8 GB (gfx1010), Ubuntu with kernel 7.0, GCC 15.2, ROCm 7.14.0a20260612 (TheRock gfx101X-dgpu). HIP build: `-DSTRATA_ENABLE_HIP=ON -DCMAKE_HIP_ARCHITECTURES=gfx1010 -DSTRATA_ISA_FLOOR=avx -DSTRATA_BUILD_TESTS=ON`, base 870aedd9. **Passed:** - Build of this branch (7a68664a): `ninja strata kq_cpu_bench q2_bitplane_parity pool_tasks_test expert_layout_test native_arithmetic_parity iq_avx1_parity`, 0 errors. - `ctest -R '^(q2_bitplane_parity|pool_tasks_test|expert_layout_test|native_arithmetic_parity.*|iq_avx1_parity)$'`: 12 of 12 passed (ROCm's lib directory on `LD_LIBRARY_PATH`; `expert_layout_test` links the HIP runtime). - `kq_cpu_bench --pairs q2_0/q2_0 --threads 1,4 --nt 1,3,4 --mb 1024 --reps 3`, ms per expert per thread at 1 / 3 / 4 tokens: | Build | Legacy rows | `STRATA_Q2_BITPLANE=1` | | --- | --- | --- | | main | 0.975 / 2.89 / 3.78 | 0.975 / 2.89 / 3.79 (not used) | | commit 1 | 0.517 / 1.13 / 1.40 | 0.418 / 0.807 / 0.954 | | commits 1 + 2 | 0.487 / 1.11 / 1.38 | 0.297 / 0.597 / 0.752 | - Same bytes for commit 2: FNV hashes of both kernels' outputs on fixed random blocks at 1-8 tokens are identical before and after, with `STRATA_Q2_BITPLANE=1` and with the legacy rows. The harness is `src/kernels/cpu/q2_avx1_parity.cpp` on https://github.com/Keoian/Strata/tree/rx-5700-xt-optimizations and is not part of this PR. - Decode A/B against main (870aedd9): 2x RX 5700 XT 8 GB (layer split 27), Qwen3.8-Flash-Next ISTA-DASLab Q2_0, `--spec 4 --spec-min-p 0.8 --mtp --kv int8`, `STRATA_Q2_BITPLANE=1`, `STRATA_HC_Q8=0`. 3 prompts x 2 reps, 256 tokens greedy, a fresh engine start per arm, ABBA over two starts each. Mean 14.97 -> 22.38 tok/s (+49.6%), 11 of 12 pairs faster. The branch binary was built before the comment in `expert_layout.hpp` was corrected; the code is the same. <details><summary>Per-run decode tok/s</summary> - main: 8.6 14.2 17.2 12.6 15.9 17.7 14.5 14.0 18.1 12.8 15.6 18.4 - this branch: 26.6 14.3 23.1 26.3 23.2 24.6 26.7 22.2 25.6 15.0 23.6 17.4 </details> **Failed:** none observed. **Not run:** - CUDA and SYCL builds: no toolkit on this machine. The two files are CPU code that those builds compile too, so this PR stays a draft until someone can build them. - AVX2 / AVX-512 CPUs: none here. The change does not touch those paths. - A CPU without AVX (`STRATA_ISA_FLOOR=none`), Sandy Bridge, Windows / MSVC. ## Limits Measured on one CPU model (Ivy Bridge). Only Q2_0 changes; the i-quants keep their own AVX1 kernels. 🤖 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-07 16:33:31 +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#5212
No description provided.