Hoist scale-shuffle masks out of the AVX2 ggml_vec_dot_q4_K_q8_K hot loop - #25
Open
codspeed-hq[bot] wants to merge 1 commit into
Conversation
…K hot loop Materialize the 8 per-quarter scale shuffle masks once before the superblock loop and fully unroll the QK_K/64 == 4 quarter-block loop so each mask is used as a compile-time constant. This removes the run-time-indexed reloads from the k_shuffle table that were previously repeated on every superblock. Pure unroll + constant-hoist: the computed result is identical. Verified with test-backend-ops (1177/1177 MUL_MAT tests pass).
Author
Merging this PR will improve performance by 12.4%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | Simulation | mul_mat[q4_k] |
72.4 ms | 64.4 ms | +12.4% |
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-hoist-scale-shuffle-masks-out-of-the-avx2-ggml-vec-1785163186472 (67db298) 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.
Summary
Optimizes the AVX2 implementation of
ggml_vec_dot_q4_K_q8_K(ggml/src/ggml-cpu/arch/x86/quants.c), the Q4_K × Q8_K dot product that dominatesmul_matfor Q4_K-quantized weights. The flamegraph ofmul_mat[q4_k]shows ~95% of the benchmark time inside this function.Analysis
The inner quarter-block loop applies the per-sub-block scales via
_mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+…)).get_scale_shuffle_k4()returns an_mm256_loadu_si256from a constant 256-byte table. Because the loop indexes it with the run-time variablej, the compiler could not hoist those loads — so the 8 shuffle-mask loads were re-issued from thek_shuffletable on every superblock, even though the masks are data-independent constants.Change
QK_K/64 == 4quarter-block loop (via a local macro) so each mask is used as a compile-time-constant operand.Everything else — the load/
and/srliunpack,maddubs,madd_epi16scaling and the integer/float accumulation — is byte-for-byte the same sequence of operations, just with the redundant table reloads removed and the tiny 4-iteration loop control eliminated. This is a pure unroll + constant-hoist, so the result is mathematically identical.Correctness
Verified locally with
test-backend-ops:All Q4_K MUL_MAT cases (
type_a=q4_K) pass, confirming the change is bit-consistent with the reference implementation.Performance (CodSpeed, CPU simulation)
mul_mat[q4_k]No regressions in the other benchmarks. The win is an instruction-count reduction (fewer table loads in the hot loop), which is exactly what the simulation instrument measures.