Repository navigation
ggml-cpu: x86 SIMD vec_dot for Q2_0 - #265
danielhanchen wants to merge 2 commits into
Conversation
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).
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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); |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
On x86,
ggml_vec_dot_q2_0_q8_0had no SIMD path.arch-fallback.hmapped 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.cand removes the x86 alias inarch-fallback.h.vpermdspreads each 4-byte group ofqsover one lane,vpsrlvdshifts the copies by 0/2/4/6, a mask keeps 2 bits, and apshufbputs the bytes back in Q8_0 order. The dot product goes throughmul_sum_i8_pairs_float, so the AVX512-VNNI and AVX-VNNI paths are picked up automatically.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.
-ngl 0 -t 4, pp128 (t/s)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
Not covered
Base is upstream
8bb3cf0d2(base/upstream-8bb3cf0d2). Like every pin inscripts/unsloth/pr-set.json, this is merged onto the aged upstreamb####tag for the prebuilt and dropped once upstream has the equivalent.