Skip to content

ggml-cpu : AVX-512 Q4_K, Q5_K, Q6_K and IQ4_XS dot products - #513

Draft
danielhanchen wants to merge 1 commit into
base/upstream-9a811bab3from
pin-avx512-kq-dot
Draft

danielhanchen wants to merge 1 commit into
base/upstream-9a811bab3from
pin-avx512-kq-dot

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Not proposed for the prebuilt in this form (Zen 4 result below: correct and reached, but q4_K / q5_K single-column dots are 7% / 11.5% slower than the AVX2 code and nothing is faster at model level). Kept open as a draft for a rework.

Summary

The unslothai/llama.cpp prebuilt is an aged upstream b#### tag plus the pins listed in scripts/unsloth/pr-set.json, merged in order. This pin is one commit on top of upstream b12966 (9a811bab3c4) and needs no other pin. Once upstream has an equivalent change, the pin is dropped.

The problem

On x86 CPUs with AVX-512, the Q4_K, Q5_K, Q6_K and IQ4_XS x Q8_K dot products still run their AVX2 kernels: two 256-bit maddubs / madd chains per step. These four types are most of the weights of the K-quant and UD GGUFs. On x86 the CPU repack buffer already takes Q4_K weights whose row count is a multiple of 8 (the q4_K_8x8 GEMV), so in a default run the Q5_K, Q6_K and IQ4_XS tensors are the ones that reach these dots; Q4_K reaches its dot with --no-repack or for tensors the repack buffer does not take.

The fix

  • ggml/src/ggml-cpu/arch/x86/quants.c: with __AVX512F__ and __AVX512BW__, ggml_vec_dot_{q4_K,q5_K,q6_K,iq4_xs}_q8_K take an AVX-512 form. One 512-bit maddubs / madd (vpdpwssd where AVX512_VNNI is available, which gives the same int32 as madd + add) covers both 256-bit halves of the AVX2 kernel, the two halves are folded back to the AVX2 kernel's 8 int32 lanes, and the float part is the AVX2 kernel's own. The output is bit-identical to the AVX2 kernels.
  • Builds without AVX-512 (and every other architecture) are unchanged. There is no runtime switch; the choice is made at compile time like the other x86 kernels in this file.

Evidence

All on one Intel Xeon Platinum 8559C host (192 threads, avx512f / bw / vl / vnni), CPU-only builds of b12966 (base) and b12966 + this pin (head), same flags.

Check Result
Builds: GGML_NATIVE=ON; GGML_NATIVE=OFF + GGML_AVX512=ON (adds -mavx512bw) with and without GGML_AVX512_VNNI all compile, no new warnings in quants.c
Engagement (disassembly of libggml-cpu) head: ggml_vec_dot_q4_K_q8_K / q5_K jump to the AVX-512 forms (32 / 54 zmm instructions), q6_K 39 and iq4_xs 51 zmm instructions inlined; base: 0
Bit-exactness harness: ggml_get_type_traits_cpu(t)->vec_dot for the four types, n = 256 / 512 / 2048 / 4096 / 10752, 2000 inputs each (quantized random, heavy-tailed, saturating +-1 x 127/-128, all-positive, and raw random block bytes with sane fp16 scales), 40000 results as raw float bits byte-identical across base native, base AVX-512 non-VNNI, head native (VNNI), head AVX-512 VNNI, head AVX-512 non-VNNI and head AVX2-only builds. Negative control: dropping the upper-half shift in the Q5_K high-bit extraction changes 9998 / 10000 Q5_K results and no other type
test-quantize-fns base and head pass (0 failures) on the native and the non-VNNI builds
test-backend-ops -b CPU -o MUL_MAT / -o MUL_MAT_ID 3042 / 3042 and 1310 / 1310 on base, head and head without VNNI (CPU-only, so this compares the CPU backend with itself: crash / shape coverage only, the harness above is the numeric check)
Greedy md5, 128 tokens, -ngl 0 -t 16, native builds identical base vs head: Qwen3-0.6B Q4_K_M (dense) 49b92a5d3c67, gemma-3-1b-it Q4_K_M 3dd1a9f996f1, granite-4.0-h-tiny Q4_K_M (hybrid MoE) 739d9863a717, Qwen3.5-0.8B UD-Q4_K_XL (holds Q4_K, Q5_K, Q6_K and IQ4_XS tensors) 31a09d806406
Greedy md5, non-VNNI builds (head vs base built with the same flags) identical: granite 2d19ea45f7bd, Qwen3.5-0.8B 562f4dd48e2f
Pin set sim (llama_pinset.py sim, private copy, b13023, 12-06): master pr-set + open candidates #485-#503 (without #495) + #505 + #508-#513 + #516 at their current heads, 74 pins every pin merges (git or additive); merge_checks clean; pin_contract: all pins intact once #491 is listed at its current head e6e34ffd3c2 (the copy listed it at 131d93897cd, whose comment-trimmed line arrives through #505 / #512, a listing artifact). CUDA mmvq.cu / mmvf.cu / mmid-gemm.cu / fusion.cu compile on the merged tree (sm_75 + sm_100a)

Zen 4 result (Ryzen 5 7600, AVX-512 F/BW/VL/VNNI/VBMI, pin-set tree vs + this pin)

Check Result
test-quantize-fns 0 failures on both; dot-product errors identical (q4_K 0.002318, q5_K 0.001038, q6_K 0.000211, iq4_xs 0.002499)
Greedy md5 with --no-repack identical on Qwen3-0.6B Q4_K_M and Qwen3-1.7B IQ4_XS; default settings identical on Inkling-2-Mini and Qwen3.8-Flash-Next
test-backend-ops perf -b CPU -o MUL_MAT, A_us / B_us (above 1 = faster), single run per arm q4_K n=1 median 0.928 (A/A 1.018), q5_K n=1 0.885 (A/A 0.996), q6_K n=1 1.019 (A/A 1.055), iq4_xs n=1 1.032 (A/A 0.997); n=8 within noise
llama-bench --repack 0, 6 cores, 5 rounds pp512 / tg128 within the A/A range on both models

The new dots are reached and correct, but on Zen 4 the q4_K and q5_K single-column dots are slower than the existing AVX2 code and nothing is faster at model level. Not proposed for the prebuilt in this form.

Not proven

  • Speed: the change is meant to lower the instruction count of these dots, but the local CPU llama-bench tg64 (Qwen3-0.6B Q4_K_M, Qwen3.5-0.8B UD-Q4_K_XL, 16 threads, 2 rounds) was inside the shared host's noise (base A/A spread 6 to 17%), so no speed number is claimed here. An A/B on an idle AVX-512 machine (Zen 4 / Sapphire Rapids) is still needed.
  • AMD Zen 4 (double-pumped 512-bit units) was not tested; only the Intel Xeon Platinum 8559C (Emerald Rapids) host above.
  • The larger no-break models (Qwen4-35B-A3B, Qwen3.8-27B, Qwen3.8-Flash-Next, Inkling-2-Mini) were not run on CPU here; by construction their outputs are bit-identical as well.

On CPUs with AVX512F and AVX512BW, the Q4_K, Q5_K, Q6_K and IQ4_XS x Q8_K
dot products process both 256-bit halves of the AVX2 kernels with one
512-bit maddubs / madd (vpdpwssd with AVX512_VNNI). The int32 results and
the float accumulation order are those of the AVX2 kernels, so the output
is bit-identical; only the instruction count drops.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T08:44:44.065912Z b6da446 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen danielhanchen self-assigned this Dec 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: b6da446946

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

No deployments
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.

1 participant