Skip to content

[Engine] quant: NVFP4 four rows against one activation row (AVX2, AVX-VNNI), the same bits - #488

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

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

Conversation

@drakarah

@drakarah drakarah commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Problem and result

Inkling 2 Mini's UD-Q4_K_XL stores the experts of layers 0 to 19 as NVFP4. The experts the card does not hold are computed on the CPU, one weight row at a time with dot_nvfp4_q8_0, and on an RTX 4060 Laptop (8 GB) that is 38% of a token's time (9.4 of about 25 ms; perf: dot_nvfp4_q8_0 is 42% of the process's CPU samples). That kernel is bound by its instructions, not by the RAM. With 14 threads of an i9-12900H, each over its own slice of random rows, it reads 42.2 GB/s of weights where Q4_K reaches 62.7 and Q8_0 65.2 GB/s (the RAM's limit).

The new kernel computes four weight rows against one activation row a call:

GB/s of weights (i9-12900H, rows past the caches) One row a call Four rows a call
14 threads (6 P-cores, 8 E-cores) 42.2 48.4 (+15%)
One P-core 9.8 12.2 (+24%)

Inkling 2 Mini UD-Q4_K_XL, RTX 4060 Laptop, 14 threads, a short chat (42-token prompt, 178 tokens written):

Off (STRATA_QUANT_NVFP4_X4=0) On
Experts held still (STRATA_FREEZE_EXPERTS=1 --expert-swaps 0), 6 interleaved pairs, alternating which runs first 20.44 ± 2.01 tokens/s; settled rounds 19.7 22.57 ± 2.01; settled rounds 21.3 (+7.8%)
CPU experts a token, the same runs 29.68 ± 3.30 ms; settled 30.8 25.97 ± 2.96; settled 27.9
Default settings, 4 runs each 39.94 ± 1.24 tokens/s, CPU experts 9.57 ms 40.44 ± 0.51, 9.03 ms (the same within the noise)

On was faster in each of the 6 pairs. The first pair (and the second run of the second) was faster for both, before the laptop settled; "settled" is rounds 3 to 6. The gain grows with the share of experts the CPU computes: smaller cards, longer answers on a full cache.

Related discussion

none (no issue).

Approach

  • dot_nvfp4_q8_0_x4 in a new src/quant/x86/x86_nvfp4_x4.inc, included by dot_avx2.cpp (int sums by maddubs + madd) and dot_avxvnni.cpp (by VPDPBUSD), as x86_dots.inc is:
    • the activation block pair and its two scales are loaded once for the four rows;
    • a block's 32 code bytes come in one 256-bit load; their 64-bit quarters go to [0 2 1 3] and one 64-bit unpack of the low and the high nibbles gives the one-row kernel's byte order for sub-blocks 0-1, the other for 2-3, so each int32 lane is the sum of the same four products;
    • |w| comes from a 16-entry magnitude table and w's sign (bit 3 of the code: kvalues_fp4 is sign and magnitude) goes on the activation, as ggml's abs/sign pair does, so the products are the same;
    • the four scales are the one-row kernel's float products (ue4m3_to_fp32(d) * dy), spread by two permutevar8x32, and the fmadd chain (sub-blocks 0-1, then 2-3) and hsum_float_8 are its.
  • dot_avxvnni.cpp gets a four-row table (avxvnni_dot_x4_table(), under STRATA_QUANT_AVXVNNI like its dot table) and dot_x4_kernel() reads it. cpu_experts()' rows_of() takes a four-row kernel of the one-row kernel's own tier only, and on CPUs with AVX-VNNI (Alder Lake and later, Zen 5) the one-row NVFP4 kernel is the AVX-VNNI one. Without that table the AVX2 four-row kernel would not be taken there.
  • STRATA_QUANT_NVFP4_X4=0 leaves NVFP4 out of dot_x4_kernel() (a rollback switch in terminal/data/settings-register.json, docs/SETTINGS.md regenerated); a section in docs/models/inkling.md.
  • poly_cpu_x4_test: fill() knows NVFP4 blocks (any scale byte, 0x7F and bit 7 included), so NVFP4 is checked now.
  • Tried and left out: decoding the four rows' UE4M3 scales by vector arithmetic (the same bits, but slower: 40.1 GB/s with 14 threads).
  • Kept out: MXFP4 (the same values with a per-32 E8M0 scale; the same approach should apply), an AVX-512 version, the existing AVX2 Q8_0 four-row kernel, which AVX-VNNI CPUs skip for the same reason (Q8_0 already reaches the RAM's limit here), and the translations of inkling.md.

Validation

Revision c5c048d03. Linux, i9-12900H (6 P-cores, 8 E-cores, AVX2 + AVX-VNNI, no AVX-512), 30 GB RAM, GCC in the CUDA 13.4 build container; RTX 4060 Laptop GPU 8 GB; Inkling 2 Mini UD-Q4_K_XL (Unsloth).

Passed:

  • poly_cpu_x4_test (built with -DSTRATA_BUILD_TESTS=ON): NVFP4 0 of 1,536 values differ from the one-row kernel (row lengths 256 to 4,096); in the AVX-VNNI tier and with STRATA_FORCE_ISA=avx2 (the AVX2 tier); PASS. With STRATA_QUANT_NVFP4_X4=0: NVFP4 one row at a time, PASS.
  • Engine: with the experts held still, the same tokens in all 12 runs of the second A/B and all 8 of the first, on and off.
  • The speeds above (a 14-thread bench with pinned threads and one start, over 3.5 GiB of rows; and the engine A/B).
  • python3 tools/settings_register.py --check and python3 -m unittest tools.test_settings_register: OK.

Failed:

  • The vector UE4M3 decode (see Approach): same bits, slower; not in the commit.

Not run:

  • AVX2 CPUs without AVX-VNNI on real hardware (the AVX2 tier was checked here through STRATA_FORCE_ISA=avx2), AMD CPUs, AVX-512 CPUs (which take the AVX-512 one-row kernel, so this kernel is not used there), Windows / MSVC.
  • HIP/SYCL builds (the files are CPU code shared by every build; I built the CUDA one).
  • Prompt speed, other NVFP4 models, other cards; reproduction by anyone else; the whole test suite.

Limits

  • On this laptop with the default settings the CPU computes about a fifth of the experts' uses, and the gain is within the noise; it shows where the CPU computes more of them.
  • One CPU, one model file.

🤖 Generated with Claude Code

…-VNNI), the same bits

cpu_experts() computed NVFP4 experts (Inkling 2 Mini UD-Q4_K_XL, layers 0-19)
one weight row at a time, and that kernel was bound by its instructions: with
14 threads of an i9-12900H it read 42.2 GB/s of weights where Q4_K and Q8_0
reach the RAM's 63-65 GB/s.

dot_nvfp4_q8_0_x4 (x86_nvfp4_x4.inc, built in the AVX2 and the AVX-VNNI tier)
loads the activations and their scales once for four rows, takes a block's 32
code bytes in one load and puts them in the one-row kernel's order with one
permute and two unpacks, and puts the code's sign on the activation; the int32
sums, the scale products and the fmadd chain are the one-row kernel's, so every
value has its bits. The AVX-VNNI tier gets a four-row table: cpu_experts takes a
four-row kernel of the one-row kernel's own tier only, and on AVX-VNNI CPUs
(Alder Lake and later) that is the AVX-VNNI one. STRATA_QUANT_NVFP4_X4=0 turns
it off (registered as a rollback switch).

i9-12900H, 1 GiB+ of random rows: 14 threads 42.2 -> 48.4 GB/s (+15%); one
P-core 9.8 -> 12.2 GB/s (+24%). Inkling 2 Mini UD-Q4_K_XL, RTX 4060 Laptop,
14 threads, experts held still: 6 interleaved pairs, alternating order, the
four-row kernel faster in each; settled rounds 19.7 -> 21.3 tokens/s, CPU
experts 30.8 -> 27.9 ms a token; the same tokens in all 12 runs. Default
settings: CPU experts 9.6 -> 9.0 ms, decode 39.9 -> 40.4 (within the noise).

poly_cpu_x4_test: NVFP4 0 of 1,536 values differ from the one-row kernel, in
the AVX-VNNI tier and with STRATA_FORCE_ISA=avx2; its NVFP4 blocks take any
scale byte (0x7F and bit 7 included).

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 26971b106 (bit-exact verified by poly_cpu_x4_test; 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