ggml-quants: vectorize make_qkx2_quants scale search with AVX2/FMA - #24
Open
codspeed-hq[bot] wants to merge 2 commits into
Open
Conversation
Author
Merging this PR will improve performance by 64.88%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | Simulation | quantize_chunk[q4_k] |
10.4 ms | 6.3 ms | +64.88% |
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-qkx2-quants-k-quant-scale-searc-1785161336020 (7496180) with master (46819c9)
The runtime __builtin_cpu_supports() check references the libgcc __cpu_model symbol, which the Windows LLVM/clang toolchain does not provide, breaking the ggml-base link. Gate the AVX2 dispatch behind GGML_QKX2_AVX2_DISPATCH, which requires GNU-style target attributes and is disabled on Windows.
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
quantize_chunk[q4_k]is the most expensive micro-benchmark in the ggml suite, and the flamegraph shows ~90% of its time is spent inmake_qkx2_quants— specifically the scale-search sweep that runsnstep+1(21) times per 32-element block, recomputingnearest_int(iscale*(x[i]-min))and three weighted sums for every element. It is ~99% instruction-bound and was entirely scalar.This change adds an AVX2/FMA vectorized implementation of that inner sweep (
make_qkx2_sweep_avx2), processing 8 elements per iteration and accumulatingsum_l,sum_l2,sum_xlin vector registers before a horizontal reduction._mm256_cvtps_epi32uses round-half-to-even, matchingnearest_int()over the clamped range, so it produces the same quantized levels.Key implementation detail
ggml-quants.cis compiled intoggml-basewithout-mavx2/-mfma(those flags are only onggml-cpu), so a plain#if defined(__AVX2__)guard would compile to nothing. The helper is therefore isolated with#pragma GCC target("avx2,fma")+__attribute__((target("avx2,fma")))so the intrinsics compile regardless of the TU baseline ISA, and it is only entered after a runtime__builtin_cpu_supports("avx2")check. Non-x86 and pre-AVX2 CPUs keep the untouched scalar fallback, so portability is preserved.Correctness
test-quantize-fnspasses for all quant types (round-trip error within thresholds).Measured impact (CodSpeed simulation)
quantize_chunk[q4_k]No regressions across the other benchmarks (
q4_0,q4_1,q5_0,q6_k,q8_0unchanged).Base and head CodSpeed runs executed on different CI runners; the ~69% gain is far larger than any environment-induced variance.