ggml-quants: evaluate the k-quant scale search error in O(1) in make_qkx2_quants - #27
Open
codspeed-hq[bot] wants to merge 1 commit into
Conversation
…_quants Rewrite the sum-of-squares scale/min search in make_qkx2_quants (the hot inner routine of k-quant quantization used by q4_K and q5_K). For the use_mad == false objective, the candidate reconstruction error is a quadratic form in the fitted (scale, min) whose coefficients are already accumulated during the first pass, so it is now evaluated in O(1) instead of a second scan over Laux. Because the error no longer needs a materialized candidate, the Laux[] staging store is dropped from the sweep and the Laux->L copy is removed from the improvement branch. Only the winning (iscale, min) is remembered, and the final L[] is recomputed once after the sweep. The use_mad == true path (q2_K) is left byte-for-byte unchanged. test-quantize-fns passes for all quant types; q4_K and q5_K reference implementation error remains 0.000000 (bit-identical to the reference).
Author
Merging this PR will improve performance by ×2.7
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | Simulation | quantize_chunk[q4_k] |
10.4 ms | 3.9 ms | ×2.7 |
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-evaluate-the-k-quant-scale-search-error-in-o-1-and-1785213170675 (8e3fe21) 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
Rewrote the sum-of-squares scale/min search in
make_qkx2_quants(ggml/src/ggml-quants.c), the hot inner routine of k-quant quantization used by q4_K and q5_K.Bottleneck
Flamegraphs of the
quantize_chunk[q4_k]simulation benchmark show ~90% of time inmake_qkx2_quants, ~99% instruction-bound. For each of thenstep+1(21 for q4_K) candidate scales, the original code did two full passes over the block:nearest_int, storing the candidate into aLaux[]buffer, and accumulatingsum_l/sum_l2/sum_xl;Laux[]to accumulate the reconstruction error;plus a
Laux[]→L[]copy every time a candidate improved the best error.Change (for the
use_mad == falseobjective only)Σ w·(a·L + b − x)²is a quadratic form in the fitted(scale, min)whose coefficients (sum_l,sum_l2,sum_xl,sum_w,sum_x,sum_wx2) are already accumulated during the first pass. It is now evaluated in O(1), removing the entire second scan.Laux[]staging store is removed from the sweep and theLaux→Lcopy is removed from the improvement branch. Only the winning(iscale, min)is remembered, and the finalL[]is recomputed once after the sweep.use_mad == truepath (q2_K) is left byte-for-byte unchanged.The winning-candidate reproduction uses the
minvalue as it stood at the start of the winning iteration (minis only updated after a candidate is accepted), so the emittedL[]matches the original algorithm exactly.Correctness
tests/test-quantize-fns.cppbuilt against the modifiedggml: all quant types pass, and for the affected types the fast path is bit-identical to the reference:q4_K reference implementation error: ok (0.000000), dot-product error0.002318(unchanged from baseline)q5_K reference implementation error: ok (0.000000), dot-product error0.000957(unchanged)q2_K(unchanged code path) stillokMeasured impact (CodSpeed, CPU simulation)
quantize_chunk[q4_k]quantize_chunk[q6_k]is unchanged (it usesmake_qx_quants, a different routine), and all other benchmarks are unchanged — confirming no collateral regression.