Vectorize ggml_vec_max_f32 with AVX2 for the soft-max row-max scan - #20
Open
codspeed-hq[bot] wants to merge 1 commit into
Conversation
The scalar reduction in ggml_vec_max_f32 is a serial dependency the compiler does not auto-vectorize, dominating the soft-max row pass. Reduce eight lanes at a time with _mm256_max_ps and fold to a scalar, keeping a scalar tail and a scalar fallback for non-AVX2 targets.
Author
Merging this PR will improve performance by 16.81%
Performance Changes
Tip Curious why this is faster? Comment Comparing |
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
Vectorizes
ggml_vec_max_f32(inggml/src/ggml-cpu/vec.h), the row-maximum reduction used by scaled masked soft-max (ggml_compute_forward_soft_max_f32) — the normalization step at the heart of attention.Analysis
Flamegraph analysis of the
soft_maxbenchmark showedggml_vec_max_f32accounting for ~12% of self time, at ~100% instructions. The scalar reductionmax = MAX(max, x[i])is a serial dependency that the compiler does not auto-vectorize (thea>b?a:bform has NaN/signed-zero semantics that block a plain fmax reduction), so it processes one element per iteration over every attention-score row.Change
Reduce eight lanes at a time with a running
_mm256_max_psaccumulator, then fold to a scalar with a short SSE reduction, keeping a scalar tail for the remainder. A scalar fallback is retained for non-AVX2 targets and the Accelerate path is unchanged.Correctness
For the finite inputs soft-max operates on (the code already asserts
!isnan(wp[i])), the vectorized reduction returns a bit-identical maximum. Verified with a standalone A/B harness over 100,000 rows of varying lengths: 0 mismatches vs. the original scalar loop.Performance (CodSpeed, CPU simulation)
Measured with the repository's
benchmarks/ggml-benchmarkssuite in CPU-simulation mode:soft_maxThe other 14 benchmarks are unchanged — no regressions.