ggml-quants: vectorize the per-block extremes scan in the reference quantizers - #43
Open
codspeed-hq[bot] wants to merge 1 commit into
Conversation
…uantizers The reference quantizers scan each block for its extremes with a sequential search whose every element depends on the selects of the previous one, which keeps the search scalar. Track the signed min/max over four independent accumulator groups instead so the compiler can collapse the scan into SIMD min/max, and derive the absolute maximum from max(|mn|, |mx|). The sequential scan has an observable tie-break (when a block holds both +amax and -amax the first one wins), so that case falls back to a short scan and the output stays bit-identical. Applied to q4_0, q4_1, q5_0, q5_1, q8_0, q8_1 and q8_K.
Author
Merging this PR will improve performance by 46.71%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | Simulation | quantize_chunk[q4_0] |
246.7 µs | 140.2 µs | +75.95% |
| ⚡ | Simulation | quantize_chunk[q4_1] |
213.6 µs | 144.8 µs | +47.53% |
| ⚡ | Simulation | quantize_chunk[q5_0] |
386 µs | 279.2 µs | +38.25% |
| ⚡ | Simulation | quantize_chunk[q8_0] |
175 µs | 135.6 µs | +29.1% |
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-vectorize-the-per-block-extremes-scan-in-the-refer-1785394478758 (15ee065) 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
Every legacy reference quantizer starts each block with a scan for its extremes, written as a sequential search:
Each element depends on the two selects of the previous one, so the compiler cannot vectorize it: on x86-64 at
-O2(the level used byRelWithDebInfoand the CI benchmark build)quantize_row_q4_0_refspends 10 scalar instructions per element in this scan - about 75% of the whole function - while the quantize/pack loop right below it is already auto-vectorized.This change replaces the scan with a branchless signed min/max reduction over four independent accumulator groups (
ggml_block_min_max), which the compiler collapses intominps/maxps: 6 instructions per 4 elements instead of 10 per element. The absolute maximum follows frommax(|mn|, |mx|), and the element reaching it from whichever side is larger.Applied to the
q4_0,q4_1,q5_0,q5_1,q8_0,q8_1andq8_Kreference quantizers.Correctness - bit-identical output
The sequential scan has an observable tie-break: when a block contains both
+amaxand-amax, the first one becomes the block scale, and the two choices produce different (though equally accurate) encodings.ggml_block_amaxtherefore detects that exact case (mx == -mn) and falls back to a short scan, so it reproduces the original element for element. Signed zeros are handled throughfabsf, so an all-zero block still yields the samed.Verified in this sandbox with an A/B harness that builds ggml at the base commit and at this commit and hashes the produced blocks byte for byte:
q4_0,q4_1,q5_0,q5_1,q8_0,q4_K,q5_K,q6_K,q2_K,q3_K,iq4_nl, plusquantize_row_q8_K_refcalled directly) x 210 data patterns of 16384 elements each: benchmark data, all zeros, all negative zeros, constant positive/negative, exact +-v ties (both orders, and with the tie late in the block), denormal and 1e30 magnitudes, and 200 randomized rounds sweeping magnitude (1e-10 .. 1e9), sparsity and symmetry.tests/test-quantize-fns.cpppasses for all 35 types.Measured impact (CodSpeed, CPU simulation)
Base run vs head run of the micro suite (
benchmarks/bench_ggml.cpp):quantize_chunk[q4_0]quantize_chunk[q4_1]quantize_chunk[q5_0]quantize_chunk[q8_0]The 11 other benchmarks are unchanged (byte-for-byte identical measurements) - no regressions.
Scope note
This lands on the model-quantization path (
llama-quantize/ggml_quantize_chunk), not on the matmul kernels that dominate the walltime macro suite.quantize_row_q8_K_refis also on the inference path (activation quantization forq4_K/q5_K/q6_Kmatmuls), but it accounts for only ~0.7% ofmul_mat[q4_k], so the gain there (71.72 ms -> 71.58 ms) is below the noise floor and not claimed. The change is architecture-independent: the same scan is scalar on every target, so aarch64 benefits from the samefmin/fmaxvectorization.