Skip to content

[Engine] quant: opt-in IQ4_XS four-row AVX2 kernel with fewer instructions, the same bits - #487

Closed
drakarah wants to merge 1 commit into
simplexai-labs:mainfrom
drakarah:feat/quant-iq4xs-x4-v2
Closed

drakarah wants to merge 1 commit into
simplexai-labs:mainfrom
drakarah:feat/quant-iq4xs-x4-v2

Conversation

@drakarah

@drakarah drakarah commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Problem and result

cpu_experts() computes the IQ4_XS experts outside VRAM (gate and up of Unsloth's UD-IQ4_XS files, Qwen4-35B-A3B among them) with dot_iq4_xs_q8_K_x4_avx2, four weight rows against one activation row a call. On one core that kernel is bound by its instructions, not by memory, when the weights are in cache: 16.5 GB/s of weights on an i9-12900H P-core, where the new kernel reaches 21.0 on the same data with the same results.

The new kernel computes the same int32 sums and the same float steps in fewer instructions. One core, 1 GiB of random rows (past the caches) or 512 KiB repeated (in cache), GB/s of weights, one activation row, two runs each (equal to 0.1):

main STRATA_QUANT_IQ4XS_X4_V2=1 Change
P-core, weights in cache 16.5 21.0 +27%
P-core, weights from RAM 11.0 11.9 +8%
E-core, weights in cache 6.5 7.6 +17%
E-core, weights from RAM 6.1 6.4 +4%

In the engine on this laptop the 14 CPU threads together are bound by RAM (about 47 of 62 GB/s), so the gain is small. Qwen4-35B-A3B UD-IQ4_XS, RTX 4060 Laptop 8 GB, --kv q8_0, 300 tokens, the arms interleaved, 4 rounds:

main STRATA_QUANT_IQ4XS_X4_V2=1
Default 79.16 ± 3.72 tokens/s 78.06 ± 1.07 (the same within the noise)
Experts held still (STRATA_FREEZE_EXPERTS=1 --expert-swaps 0, 23% of the experts' uses from VRAM) 61.33 ± 1.30 62.56 ± 1.09 (+2.0%); CPU experts 8.6 → 8.3 ms a token

A CPU with fewer cores for its memory bandwidth, or one whose cores are slower per clock, spends more of the experts' time in this kernel; that is the case this PR asks to be measured.

Related discussion

none (no issue). Follows feat/quant-iq4xs-q4k-rows (several activation rows a call), which is independent of it.

Approach

dot_iq4_xs_q8_K_x4_v2, taken by dot_iq4_xs_q8_K_x4_avx2 when the variable is 1 (read once):

  • a pair of sub-blocks' 32 bytes of nibbles in one 256-bit load: their low nibbles go with one sub-block, their high ones with the other;
  • a block's 8 six-bit scales decoded together (the high bits by one mullo_epi16, shift and mask), negated, and spread to a pair by one pshufb;
  • the activations rearranged once a block (two permute2x128) for all 4 rows, so each 32-bit lane holds the products it holds in the old kernel;
  • the IQ4_NL sign put on the activation (sign_epi8) and only |value| looked up, so maddubs takes the table value unsigned;
  • at the end of a block two cross-lane permutes and one add give each lane the int32 sum it has in the old kernel; the float conversion, the scale product and the fmadd are the old kernel's, so every output has its bits.
  • Kept out: making it the default (or the default only on CPUs without efficiency cores: the E-cores here are faster with it too, so pinning is not needed for it), the one-row and several-activation-row IQ4_XS kernels, AVX-512, NVFP4 and other formats.

Validation

Revision 3f0a1c52f. Linux, i9-12900H (6 P-cores, 8 E-cores, AVX2, no AVX-512), GCC in the CUDA 13.4 build container; RTX 4060 Laptop 8 GB for the engine runs.

Passed:

  • poly_cpu_x4_test (built with -DSTRATA_BUILD_TESTS=ON) with the variable set: 0 of 1,536 IQ4_XS values differ from the one-row kernel, bit for bit, row lengths 256 to 4,096; and without it, unchanged. A second ctest entry (poly_cpu_x4_test_iq4xs_v2) runs it with the variable set; ctest -R poly_cpu_x4: 2 of 2 passed.
  • Engine: with the experts held still, the same 300 tokens in all 8 runs, with and without the variable.
  • The speeds above (taskset -c 2 P-core, -c 16 E-core).
  • python3 tools/settings_register.py --check and python3 -m unittest tools.test_settings_register: OK.

Failed:

  • Nothing in this version. An earlier comparison against a different base kernel showed the E-cores 1.5% slower under full load; against main's kernel the E-core is faster alone (table above), but a full-load E-core run against main has not been repeated.

Not run:

  • CPUs without efficiency cores, AMD CPUs, AVX-512 CPUs (where a native build may take other kernels), Windows.
  • HIP/SYCL builds (the file is CPU code shared by every build; I built the CUDA one).
  • Prompt speed, batched serving; reproduction by anyone else; the whole test suite.

Limits

  • The engine gain on this laptop is 0 to 2%: RAM, not this kernel, bounds the CPU experts here.
  • One CPU.

🤖 Generated with Claude Code

…tions, 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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW
@VecSzn

VecSzn commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator

Integrated on main in commit c721c9836 (bit-exact verified with STRATA_QUANT_IQ4XS_X4_V2=1; included in upcoming v0.2.20-poly.11). Thanks @drakarah!

@VecSzn VecSzn closed this Dec 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants