ggml-cpu: unroll the AVX2 q4_K x q8_K sub-block loop - #44
Open
codspeed-hq[bot] wants to merge 1 commit into
Open
Conversation
The sub-block loop in ggml_vec_dot_q4_K_q8_K has a compile-time trip count of QK_K/64 == 4 but stays rolled at -O2, so every superblock pays a pointer bump, a compare and a branch, and all loads go through a scaled index register. Write the four sub-block pairs out explicitly (guarded by a static_assert on QK_K) so the q4/q8 offsets become immediates in the addressing modes, and give each nibble half its own int32 accumulator instead of the single serial chain. The same vpmaddubsw/vpmaddwd products are summed, only the order of the exact int32 additions differs, so results are bit-identical.
Author
Merging this PR will improve performance by 12.44%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | Simulation | mul_mat[q4_k] |
72.4 ms | 64.4 ms | +12.44% |
Tip
Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.
Comparing codspeed-optim-unroll-the-avx2-ggml-vec-dot-q4-k-q8-k-sub-block-s-1785403756979 (44a67ae) with master (46819c9)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
ggml_vec_dot_q4_K_q8_Kis the kernel behind every Q4_K matmul. On AVX2 its sub-block loophas a compile-time trip count of 4, but GCC keeps it rolled at
-O2. Two sources of pureoverhead follow from that:
q4/q8loads gothrough a scaled index register instead of an immediate offset.
sumj = p16l + p16h; sumi += sumj) forces an extravmovdqaregister copy per iteration.
How
The four sub-block pairs are written out explicitly (guarded by a
static_assert(QK_K == 256)),so the
q4/q8offsets become immediates inside the addressing modes, and each nibble half getsits own
int32accumulator; the two accumulators are added together once per superblock.The arithmetic is unchanged: the same
vpmaddubsw/vpmaddwdproducts are summed, only the orderof the exact
int32additions differs.Only the AVX2 path in
ggml/src/ggml-cpu/arch/x86/quants.cis touched. Other architectures andthe scalar/AVX paths are untouched.
Correctness
tests/test-quantize-fnspasses (AVX2 build,GGML_NATIVE=OFF -DGGML_AVX2=ON).ggml_mul_matoutput bytes was compared between a baseline build and this build over 20 cases
(K in {256, 2048, 5632}, N in {64...2048}, M in {1, 3, 8, 17, 64}, 2-D and batched 3-D, with
1/2/8 threads). Every hash matches.
Measured impact
CodSpeed CPU simulation, macro suite,
q4_kbenchmarks (AVX2 baseline build, same machine):prompt_layer[q4_k]lm_head[q4_k]decode_deep[q4_k]decode_layer[q4_k]The gain lands where the change predicts: on
prompt_layer[q4_k]the instruction componentdominates (93% of the measured time) while the cache and memory components are unchanged. No
benchmark regresses meaningfully.
The x86 win is visible in the simulation benchmark job; the walltime macro job runs on aarch64
and is unaffected by this change.