From 3f0a1c52f714dee10001b1b705d566f65839493c Mon Sep 17 00:00:00 2001 From: Sarah Date: Sat, 5 Dec 2026 17:08:17 +0100 Subject: [PATCH] [Engine] quant: opt-in IQ4_XS four-row AVX2 kernel with fewer instructions, the same bits STRATA_QUANT_IQ4XS_X4_V2=1 makes dot_iq4_xs_q8_K_x4_avx2 (cpu_experts()' four weight rows a call for IQ4_XS) take a kernel with the same int32 sums and float steps in fewer instructions: a sub-block pair's nibbles in one 256-bit load, a block's 8 scales decoded together, the activations rearranged once a block for the 4 rows, and the IQ4_NL signs put on the activations. Default off: unchanged. One core, 1 GiB of random rows (kbench), GB/s of weights, one activation row: i9-12900H P-core: in cache 16.5 -> 21.0 (+27%), from RAM 11.0 -> 11.9 (+8%) E-core: in cache 6.5 -> 7.6 (+17%), from RAM 6.1 -> 6.4 (+4%) Engine, Qwen4-35B-A3B UD-IQ4_XS, RTX 4060 Laptop, --kv q8_0, 300 tokens, 4 rounds: default 79.16 +- 3.72 -> 78.06 +- 1.07 tok/s (the same within the noise); experts held still 61.33 +- 1.30 -> 62.56 +- 1.09 (+2.0%), CPU experts 8.6 -> 8.3 ms a token, the same tokens in all 8 runs. poly_cpu_x4_test: 0 of 1,536 IQ4_XS values differ from the one-row kernel with it on; a second ctest entry runs the test that way. A test request asks for CPUs without efficiency cores. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW --- docs/SETTINGS.md | 3 +- docs/TEST_REQUESTS.md | 1 + engine/cmake/strata_poly.cmake | 2 + engine/src/quant/x86/dot_avx2.cpp | 67 ++++++++++++++++++++++++++++ terminal/data/settings-register.json | 35 +++++++++++++++ 5 files changed, 107 insertions(+), 1 deletion(-) diff --git a/docs/SETTINGS.md b/docs/SETTINGS.md index d653eace9f..1ece8a011f 100644 --- a/docs/SETTINGS.md +++ b/docs/SETTINGS.md @@ -11,7 +11,7 @@ A number or a word is set like any environment variable, for example `STRATA_KVA (`terminal/strata-.json`), which the server passes to the engine. The Strata project's Terminal writes it there with `strata launch --set NAME=value`. -## Switches you can turn on (365) +## Switches you can turn on (366) Off unless you set them. The last column says where each one is described. @@ -314,6 +314,7 @@ Off unless you set them. The last column says where each one is described. | `STRATA_Q8_V70` | number | NVIDIA and AMD | [NVIDIA_V100.md](NVIDIA_V100.md) | | `STRATA_QSA_CLUSTER` | number | NVIDIA and AMD, Intel Arc | [DETAILS.md](DETAILS.md) | | `STRATA_QSA_SPLIT_ATTN` | number | NVIDIA and AMD | [DETAILS.md](DETAILS.md) | +| `STRATA_QUANT_IQ4XS_X4_V2` | on / off | NVIDIA and AMD | [TEST_REQUESTS.md](TEST_REQUESTS.md) | | `STRATA_RAM_ADAPT` | number | NVIDIA and AMD | [DETAILS.md](DETAILS.md), [release-notes-0.2.7.md](release-notes-0.2.7.md) | | `STRATA_RAM_KEEP_GIB` | number | NVIDIA and AMD | [DETAILS.md](DETAILS.md), [UNSLOTH_Q4.md](UNSLOTH_Q4.md) | | `STRATA_RDNA2_SGEMM` | number | AMD | [AMD_HIP.md](AMD_HIP.md), [README.md](bench-results/2026-10-04-rdna2-fp16-prompt/README.md) | diff --git a/docs/TEST_REQUESTS.md b/docs/TEST_REQUESTS.md index 980f4337e9..42ac3d7dd4 100644 --- a/docs/TEST_REQUESTS.md +++ b/docs/TEST_REQUESTS.md @@ -18,5 +18,6 @@ This list is edited by the maintainers. An item stays until a report answers it. | AMD RX 7000 series (RX 7900 XTX, 7900 XT, 7800 XT and similar, RDNA 3) on Linux or Windows | The RX 7000 builds have not been run on a real RX 7000 card by us; the Docker image for them is also untested on a card | none yet | | Windows with an AMD card: the app or the Terminal, one model, one picture sent | The Windows AMD build has not been run on a real card, and neither has its picture reader (CPU under 24 GB, GPU from 24 GB) | none yet | | A Mac with Apple silicon (macOS 15 or newer): the app or the Terminal with a real model | Mac builds have run on very few Macs. Tell us the Mac's memory, the model, the speed, and anything that failed. Pictures on the Mac (GPU reader) were run on one Mac mini M4 only: tell us if they work on yours. The signing script has not been run against Apple's real notary service yet, so a first-launch warning is expected and useful to hear about | none yet | +| A CPU without efficiency cores (AMD Ryzen, Intel before the 12th generation), a model with IQ4_XS experts outside VRAM (for example Qwen4-35B-A3B UD-IQ4_XS), decode with and without `STRATA_QUANT_IQ4XS_X4_V2=1` | The kernel it turns on gives the same bits with fewer instructions: 27% faster on one P-core of an i9-12900H with the weights in cache, 8% with them in RAM, and +2% decode with the experts held still there. Whether it helps on a CPU with fewer, faster cores per memory channel has not been measured | none yet | To add a request, open a pull request that adds a row, or tell the maintainers in an issue. diff --git a/engine/cmake/strata_poly.cmake b/engine/cmake/strata_poly.cmake index 9c47ce87cf..184b6afd6f 100644 --- a/engine/cmake/strata_poly.cmake +++ b/engine/cmake/strata_poly.cmake @@ -487,6 +487,8 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA add_executable(poly_cpu_x4_test src/poly/cpu_x4_test.cpp src/poly/cpu_experts.cpp) target_link_libraries(poly_cpu_x4_test PRIVATE strata_quant Threads::Threads) add_test(NAME poly_cpu_x4_test COMMAND poly_cpu_x4_test) + add_test(NAME poly_cpu_x4_test_iq4xs_v2 COMMAND poly_cpu_x4_test) # the opt-in IQ4_XS four-row kernel + set_tests_properties(poly_cpu_x4_test_iq4xs_v2 PROPERTIES ENVIRONMENT "STRATA_QUANT_IQ4XS_X4_V2=1") # T-MAC CPU cold-expert microbenchmark (native cpu_experts vs Path A vs Path B across ISAs and thread counts) add_executable(tmac_bench tests/quant/tmac_bench.cpp src/poly/cpu_experts.cpp) target_include_directories(tmac_bench PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/poly) diff --git a/engine/src/quant/x86/dot_avx2.cpp b/engine/src/quant/x86/dot_avx2.cpp index 111da012b6..fc8ec1fd7d 100644 --- a/engine/src/quant/x86/dot_avx2.cpp +++ b/engine/src/quant/x86/dot_avx2.cpp @@ -13,6 +13,9 @@ #include "../registry.hpp" #include "strata/quant/dot.hpp" +#include +#include + namespace strata::quant { namespace avx2 { @@ -1009,7 +1012,71 @@ void dot_q4_K_q8_K_x4_avx2(int n, float out[4], const void* STRATA_RESTRICT w_ba } } +// (PolyStrata, opt-in: STRATA_QUANT_IQ4XS_X4_V2=1) dot_iq4_xs_q8_K_x4_avx2's sums with fewer instructions. A pair of +// sub-blocks' 32 bytes of nibbles in one 256-bit load (A: their low nibbles, B: their high ones), the 8 scales of a block +// decoded together and spread by one shuffle a pair, the activations rearranged to match once a block for the 4 rows, +// and the IQ4_NL signs applied to the activations (_mm256_sign_epi8) with the |values| on the unsigned side. Each +// 32-bit lane ends with the int32 products it has in the kernel below, and the float steps are its, so every output has +// its bits (poly_cpu_x4_test). One i9-12900H P-core: 16.5 -> 21.0 GB/s with the weights in cache, 11.0 -> 11.9 from RAM. +static void dot_iq4_xs_q8_K_x4_v2(int n, float out[4], const void* STRATA_RESTRICT w_base, size_t row_bytes, + const void* STRATA_RESTRICT vy) { + const int nb = n / QK_K; + const uint8_t* wb = (const uint8_t*) w_base; + const block_q8_K* STRATA_RESTRICT y = (const block_q8_K*) vy; + static constexpr uint8_t kAbsIq4nl[16] = {127, 104, 83, 65, 49, 35, 22, 10, 1, 13, 25, 38, 53, 69, 89, 113}; + const __m256i abs256 = _mm256_broadcastsi128_si256(_mm_loadu_si128((const __m128i*) kAbsIq4nl)); + const __m256i m4b = _mm256_set1_epi8(0x0F); + const __m256i k78 = _mm256_set1_epi8(0x78); + const __m128i m4s = _mm_set1_epi8(0x0F); + const __m128i m30 = _mm_set1_epi16(0x30); + const __m128i k32 = _mm_set1_epi16(32); + const __m128i hmul = _mm_setr_epi16(1 << 14, 1 << 12, 1 << 10, 1 << 8, 1 << 6, 1 << 4, 1 << 2, 1); + // pair p's scales: int16 2p in the low half, 2p + 1 in the high half, eight times each + const __m256i sctl[4] = { + _mm256_setr_epi8(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3), + _mm256_setr_epi8(4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7), + _mm256_setr_epi8(8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11), + _mm256_setr_epi8(12, 13, 12, 13, 12, 13, 12, 13, 12, 13, 12, 13, 12, 13, 12, 13, 14, 15, 14, 15, 14, 15, 14, 15, 14, 15, 14, 15, 14, 15, 14, 15), + }; + __m256 accum[4] = {_mm256_setzero_ps(), _mm256_setzero_ps(), _mm256_setzero_ps(), _mm256_setzero_ps()}; + for (int ibl = 0; ibl < nb; ++ibl) { + const float yd = y[ibl].d; + __m256i ya[4], yb[4]; + for (int p = 0; p < 4; ++p) { + const __m256i y0 = _mm256_loadu_si256((const __m256i*) (y[ibl].qs + 64 * p)); + const __m256i y1 = _mm256_loadu_si256((const __m256i*) (y[ibl].qs + 64 * p + 32)); + ya[p] = _mm256_permute2x128_si256(y0, y1, 0x20); + yb[p] = _mm256_permute2x128_si256(y0, y1, 0x31); + } + for (int r = 0; r < 4; ++r) { + const block_iq4_xs& bx = ((const block_iq4_xs*) (wb + r * row_bytes))[ibl]; + // the 8 scales, negated (the signs below are the other way round): 32 - (low 4 bits | high 2 bits << 4) + uint32_t sl; + std::memcpy(&sl, bx.scales_l, 4); + const __m128i v = _mm_cvtsi32_si128((int) sl); + const __m128i l8 = _mm_unpacklo_epi8(_mm_and_si128(v, m4s), _mm_and_si128(_mm_srli_epi16(v, 4), m4s)); + const __m128i hi = _mm_and_si128(_mm_srli_epi16(_mm_mullo_epi16(_mm_set1_epi16((short) bx.scales_h), hmul), 10), m30); + const __m256i sall = _mm256_broadcastsi128_si256(_mm_sub_epi16(k32, _mm_or_si128(_mm_cvtepu8_epi16(l8), hi))); + __m256i sa = _mm256_setzero_si256(), sb = _mm256_setzero_si256(); + for (int p = 0; p < 4; ++p) { + const __m256i q = _mm256_loadu_si256((const __m256i*) (bx.qs + 32 * p)); + const __m256i na = _mm256_and_si256(q, m4b), nbv = _mm256_and_si256(_mm256_srli_epi16(q, 4), m4b); + const __m256i sc = _mm256_shuffle_epi8(sall, sctl[p]); + const __m256i pa = _mm256_maddubs_epi16(_mm256_shuffle_epi8(abs256, na), _mm256_sign_epi8(ya[p], _mm256_add_epi8(na, k78))); + const __m256i pb = _mm256_maddubs_epi16(_mm256_shuffle_epi8(abs256, nbv), _mm256_sign_epi8(yb[p], _mm256_add_epi8(nbv, k78))); + sa = _mm256_add_epi32(sa, _mm256_madd_epi16(pa, sc)); + sb = _mm256_add_epi32(sb, _mm256_madd_epi16(pb, sc)); + } + const __m256i tot = _mm256_add_epi32(_mm256_permute2x128_si256(sa, sb, 0x20), _mm256_permute2x128_si256(sa, sb, 0x31)); + accum[r] = _mm256_fmadd_ps(_mm256_set1_ps(h2f(bx.d) * yd), _mm256_cvtepi32_ps(tot), accum[r]); + } + } + for (int r = 0; r < 4; ++r) out[r] = hsum_float_8(accum[r]); +} + void dot_iq4_xs_q8_K_x4_avx2(int n, float out[4], const void* STRATA_RESTRICT w_base, size_t row_bytes, const void* STRATA_RESTRICT vy) { + static const bool v2 = [] { const char* e = std::getenv("STRATA_QUANT_IQ4XS_X4_V2"); return e && e[0] == '1'; }(); + if (v2) return dot_iq4_xs_q8_K_x4_v2(n, out, w_base, row_bytes, vy); assert(n % QK_K == 0); const int nb = n / QK_K; const uint8_t* wb = (const uint8_t*) w_base; diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 7397636882..05e8c3ab68 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -42439,6 +42439,41 @@ "retire_after": "", "retired_in": "0.2.16" }, + { + "name": "STRATA_QUANT_IQ4XS_X4_V2", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/quant/x86/dot_avx2.cpp", + "line": 1078, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": null, + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [ + "docs/TEST_REQUESTS.md" + ], + "first_commit": null, + "first_date": null, + "since": null, + "last_commit": null, + "last_date": null, + "status": "opt-in", + "owner": "kernels/cpu", + "note": "AVX2: the IQ4_XS four-row kernel with fewer instructions, the same bits", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_RAM_ADAPT", "backends": [