ggml-cpu: unpack the Q4_K scales/mins with SIMD in the AVX2 ggml_vec_dot_q4_K_q8_K - #35
Open
codspeed-hq[bot] wants to merge 1 commit into
Conversation
…_q8_K Replace the scalar utmp[] unpack of the 12 packed 6-bit scales/mins with two pshufb shuffles and three masked shifts/ors, keeping the expansion entirely in vector registers. This removes the 12-byte memcpy, ~20 GP operations and the utmp[] store/reload per superblock. The unpack is bit-exact and the dot-product loop is unchanged.
Author
Merging this PR will improve performance by 14.85%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | Simulation | mul_mat[q4_k] |
72.4 ms | 63 ms | +14.85% |
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-unpack-the-q4-k-scales-mins-with-simd-in-avx2-ggml-1785298752860 (3db79c4) 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
ggml_vec_dot_q4_K_q8_Kdominates the quantized transformer path in the CodSpeed profiles (~95% ofmul_mat[q4_k], ~83% oflm_head[q4_k], ~76% ofprompt_layer[q4_k]). A large share of its cost is the per-superblock prologue rather than the multiply-accumulate loop: the scalarutmp[]unpack of the 12 packed 6-bit scales/mins costs a 12-bytememcpy, ~20 general-purpose shift/and/or operations, and then a store ofutmp[]followed by a reload through_mm_set_epi32to get the bytes back into a vector register (a store-to-load forwarding round trip on every superblock).Change
ggml/src/ggml-cpu/arch/x86/quants.c, AVX2 branch only: the same 12 -> 16 byte expansion is now done entirely in vector registers with twopshufbbyte shuffles plus three masked shifts/ors, with the 5 shuffle/mask constants hoisted out of the loop. The dot-product loop, the mins/bsumshandling and the accumulation are untouched. The prologue drops from 45 to 28 instructions per 256-weight superblock and theutmp[]store/reload disappears.Scope: x86 AVX2 only (the branch used by the simulation CI job and by most x86 CPUs without VNNI). ARM, plain AVX and the generic paths are untouched.
Correctness
Verified locally in this sandbox (AVX2 host):
utmp[]transform over 2,000,000 random 12-byte inputs, all 12x256 single-byte-position cases, and the all-0x00/0xff/0xaa/0x55/0x0f/0xf0/0xc0/0x3fpatterns: 0 mismatches.0x6d5df0ec45020523).tests/test-quantize-fns.cpppasses against the patched build (no failures, includingq4_K).scales[0];block_q4_Kis{d, dmin, scales[12], qs[128]}, so the 4 trailing bytes belong toqs[]of the same 144-byte superblock and the load can never read out of bounds.Measured impact (CodSpeed, CPU simulation, this sandbox)
mul_mat[q4_k]mul_mat[q4_0]mul_mat[f32]No regressions. The macro (walltime) benchmarks are expected to improve on
lm_head[q4_k]andprompt_layer[q4_k];decode_layer[q4_k]is bandwidth-bound (a single token streaming the full weight matrices), so removing instructions does not move it.