Skip to content

ggml-cpu: x86 SIMD vec_dot for Q2_0 - #265

Open
danielhanchen wants to merge 2 commits into
base/upstream-8bb3cf0d2from
cpu-q2_0-x86-simd
Open

danielhanchen wants to merge 2 commits into
base/upstream-8bb3cf0d2from
cpu-q2_0-x86-simd

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

On x86, ggml_vec_dot_q2_0_q8_0 had no SIMD path. arch-fallback.h mapped it to the scalar generic code, so every CPU matmul on Q2_0 weights ran about 6.5x slower than IQ4_NL. That covers MoE experts offloaded to the CPU as well as CPU-only runs. ARM already has a NEON path.

This adds AVX2 and AVX/SSSE3 implementations in ggml/src/ggml-cpu/arch/x86/quants.c and removes the x86 alias in arch-fallback.h.

  • AVX2 (also used by AVX-512 builds): for each Q8_0 block, vpermd spreads each 4-byte group of qs over one lane, vpsrlvd shifts the copies by 0/2/4/6, a mask keeps 2 bits, and a pshufb puts the bytes back in Q8_0 order. The dot product goes through mul_sum_i8_pairs_float, so the AVX512-VNNI and AVX-VNNI paths are picked up automatically.
  • AVX / SSSE3: shift by 0/2/4/6 and restore the natural order with byte and word unpacks.
  • Plain SSE2: still uses the scalar code.
  • Accumulation order: follows the scalar reference (Q8_0 scale per half block, then the Q2_0 scale).
  • Other x86 matmul paths: none handles Q2_0 (repack, AMX, llamafile, KleidiAI), so nothing bypasses this.

Results

Xeon 8559C, shared host. Medians of 5 interleaved runs, with a repeated base run as noise check: up to 9% on the multi-threaded rows, about 1% single-threaded.

measurement before after speedup
single-thread kernel, AVX-512 (us per matrix-vector) 582 75 7.7x
single-thread kernel, AVX2 574 88 6.5x
single-thread kernel, SSE4.2 563 117 4.8x
MUL_MAT n=1, 4 threads (us) 158 25 6.3x
MUL_MAT n=512, 4 threads (ms) 81 12.3 6.6x
MUL_MAT_ID n=1, Qwen3.8 expert shape, 4 threads (us) 1648-1848 235-274 6.0-7.9x
llama-bench Qwen3-0.6B Q2_0, -ngl 0 -t 4, pp128 (t/s) 25.5 153 6.0x
same, tg64 (t/s) 21.7 90.1 4.2x
  • Single-thread: Q2_0 is now within 3-7% of Q4_0 on AVX2/AVX-512.
  • Offloaded experts, RTX PRO 6000 with VRAM capped: this matters for Qwen3.8-Flash-Next quants with Q2_0 ffn_down_exps (640 columns, so the 256-block K-quants and IQ types cannot be used). With experts spilled at 24 GiB and 16 GiB, generation is +16% and +24% faster than the scalar build, and prompt speed is unchanged.

Correctness

  • SIMD vs scalar harness, 8 builds (x64, sse42, avx, avx2, avx2+avxvnni, avx512, avx512+vnni, native):
    • Every byte value at every position matches the scalar code and an fp64 reference (4096 cases).
    • 20000 random rows with unit scales are identical.
    • 20000 rows with random signed scales: max error 1.4e-7, below the scalar code's own 1.47e-7.
    • Disassembly confirms each build takes the intended path.
  • test-backend-ops MUL_MAT and MUL_MAT_ID with type_a=q2_0: each SIMD build, loaded as a second CPU backend against the scalar x64 build, passes 157/157. That covers sse42, sandybridge, haswell, skylakex, icelake, alderlake and sapphirerapids. A deliberately broken kernel scores 0/157.

Not covered

  • AMD EPYC hardware.
  • MSVC, clang and 32-bit x86 builds.
  • The AVX-VNNI-INT8 branch of the dot-product helper (no host or build here has it).
  • Prompt processing is still well behind Q4_0, because Q2_0 has no repacked GEMM path.

Base is upstream 8bb3cf0d2 (base/upstream-8bb3cf0d2). Like every pin in scripts/unsloth/pr-set.json, this is merged onto the aged upstream b#### tag for the prebuilt and dropped once upstream has the equivalent.

Q2_0 had no x86 path: arch-fallback.h mapped ggml_vec_dot_q2_0_q8_0 to the
scalar generic code, so CPU matmuls on Q2_0 weights were several times
slower than Q4_0 / IQ4_NL.

AVX2 (also used by AVX-512 builds, with the VNNI dot products picked up by
mul_sum_i8_pairs_float): per Q8_0 block, vpermd spreads each 4-byte group of
qs over one lane, vpsrlvd shifts the copies by 0/2/4/6, a mask keeps 2 bits
and a pshufb transposes the 4x4 byte tiles back into Q8_0 order.

AVX / SSSE3: shift qs by 0/2/4/6 and restore the natural order with
byte and word unpacks, 16 values per register.

Accumulation order follows the scalar reference (Q8_0 scale per half block,
then the Q2_0 scale).
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T06:24:27.289693Z 3f9cad2 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ea543743bf

ℹ️ 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".

const __m256i qy_0 = _mm256_loadu_si256((const __m256i *) y_ptr[0].qs);
const __m256i qy_1 = _mm256_loadu_si256((const __m256i *) y_ptr[1].qs);

const __m256 p_0 = mul_sum_i8_pairs_float(qx_0, qy_0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve -128 values in Q8 dot products

When src1 is already a Q8_0 tensor containing -128 and the corresponding Q2_0 value is -1, the ordinary AVX2 path passes the operands through mul_sum_i8_pairs_float, whose sign/absolute-value transformation negates -128 in an 8-bit lane and wraps it back to -128. The old scalar path therefore returns +128*d0*d1 for that element while this path returns -128*d0*d1; the AVX/SSSE3 calls below have the same issue. Q8_0 stores unrestricted int8_t quants and the CPU accepts an existing Q8_0 RHS directly, so handle -128 without signed-byte negation rather than assuming canonical quantizer output.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches the existing x86 paths: ggml_vec_dot_q4_0_q8_0 and ggml_vec_dot_q8_0_q8_0 pass signed weights through the same mul_sum_i8_pairs_float sign trick, so a -128 activation paired with a negative weight wraps the same way there. Q8_0 activations come from quantize_row_q8_0, which scales by 127/amax and never produces -128, so the canonical path is exact. Keeping Q2_0 consistent with the other types rather than adding a special case.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 3f9cad2776

ℹ️ 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