ggml-quants: vectorize the make_qx_quants scale search with AVX2/FMA - #23
Open
codspeed-hq[bot] wants to merge 1 commit into
Conversation
Hoist the per-element weight `w` out of the ~19-candidate scale-search loop in make_qx_quants (it depends only on the fixed inputs) into a small stack buffer, and add an AVX2/FMA inner loop that computes the clamped quantized level and accumulates sumlx/suml2 eight lanes at a time. The scalar path is preserved for non-AVX2 builds and for rows larger than the stack buffer, and the rarely-taken L[] rewrite stays scalar so emitted levels are unchanged. Verified with tests/test-quantize-fns.cpp (all types pass). CodSpeed simulation: quantize_chunk[q6_k] 5.6 ms -> 3.0 ms, no regressions.
Author
Merging this PR will improve performance by 87.08%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | Simulation | quantize_chunk[q6_k] |
5.6 ms | 3 ms | +87.08% |
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-make-qx-quants-scale-search-with-avx-1785159633706 (5ab1f34) 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
Vectorized the scale-search sweep in
make_qx_quants(ggml/src/ggml-quants.c), the RMSE-optimal scale finder that dominates Q6_K quantization (and is also used by Q3_K, Q4_0 and Q5_0). This is a hot path of model quantization / conversion viaggml_quantize_chunk.Analysis
Flamegraph analysis of
quantize_chunk[q6_k](5.6 ms, 98.7% instruction-bound) showed ~84% of self time insidemake_qx_quants. For each row of 16 values the kernel runs a ~19-candidate scale search, and every candidate re-scans the row computing:l = clamp(nearest_int(iscale·x[i]), -nmax, nmax-1)sumlx += w·x[i]·l,suml2 += w·l·lwhere the weight
wwas recomputed (with a branchyqw ? ... : rmse_type == ...selection) on every single iteration even though it depends only on the fixed inputs.Change
w[i]is computed once into a small stack buffer and reused across all candidates, removing the per-iteration branchy weight selection.__AVX2__ && __FMA__): the clamped quantized level is computed 8 lanes at a time with_mm256_cvtps_epi32(round-half-to-even, matchingnearest_intin the clamped range) plus a min/max clamp, andsumlx/suml2are accumulated with_mm256_fmadd_ps, followed by a horizontal reduction and a scalar tail. Non-x86 / non-AVX2 builds keep the scalar path unchanged.The rarely-taken acceptance branch that rewrites
L[]is left scalar, so the emitted quantization levels are unchanged there.Correctness
tests/test-quantize-fns.cpp(the repo's RMSE-tolerance quantization test) passes for all types, including Q6_K / Q3_K / Q4_0 / Q5_0. The SIMD rounding mode matchesnearest_int; only the floating-point reduction order differs, which stays well within the test tolerance.Performance (CodSpeed, CPU simulation)
quantize_chunk[q6_k]The other 14 benchmarks are unchanged — no regressions.