Skip to content

ggml-quants: vectorize the make_qx_quants scale search with AVX2/FMA - #23

Open
codspeed-hq[bot] wants to merge 1 commit into
masterfrom
codspeed-optim-vectorize-the-make-qx-quants-scale-search-with-avx-1785159633706
Open

ggml-quants: vectorize the make_qx_quants scale search with AVX2/FMA#23
codspeed-hq[bot] wants to merge 1 commit into
masterfrom
codspeed-optim-vectorize-the-make-qx-quants-scale-search-with-avx-1785159633706

Conversation

@codspeed-hq

@codspeed-hq codspeed-hq Bot commented Jul 27, 2026

Copy link
Copy Markdown

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 via ggml_quantize_chunk.

Analysis

Flamegraph analysis of quantize_chunk[q6_k] (5.6 ms, 98.7% instruction-bound) showed ~84% of self time inside make_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·l

where the weight w was recomputed (with a branchy qw ? ... : rmse_type == ... selection) on every single iteration even though it depends only on the fixed inputs.

Change

  1. Hoist the weights: w[i] is computed once into a small stack buffer and reused across all candidates, removing the per-iteration branchy weight selection.
  2. AVX2/FMA inner loop (guarded by __AVX2__ && __FMA__): the clamped quantized level is computed 8 lanes at a time with _mm256_cvtps_epi32 (round-half-to-even, matching nearest_int in the clamped range) plus a min/max clamp, and sumlx/suml2 are 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 matches nearest_int; only the floating-point reduction order differs, which stays well within the test tolerance.

Performance (CodSpeed, CPU simulation)

Benchmark Base Head Change
quantize_chunk[q6_k] 5.6 ms 3.0 ms +86%

The other 14 benchmarks are unchanged — no regressions.

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.
@github-actions github-actions Bot added the ggml label Jul 27, 2026
@codspeed-hq

codspeed-hq Bot commented Jul 27, 2026

Copy link
Copy Markdown
Author

Merging this PR will improve performance by 87.08%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 27 untouched benchmarks

Performance Changes

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)

Open in CodSpeed

@codspeed-hq
codspeed-hq Bot marked this pull request as ready for review July 27, 2026 14:06
@codspeed-hq
codspeed-hq Bot requested a review from art049 July 27, 2026 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant