ggml-cpu: fuse the row-max scan into the mask pass in ggml_compute_forward_soft_max_f32 - #16
Open
codspeed-hq[bot] wants to merge 1 commit into
Conversation
…d_soft_max_f32 The CPU soft-max kernel did a separate ggml_vec_max_f32 scan over the per-row work buffer to find the row maximum for numerically-stable exponentiation, re-reading the entire row the mask pass had just written. When an additive mask is present (the common attention case), accumulate the row maximum inside the same loop that applies the mask, dropping one full pass over the row. The dedicated scan is kept only for the no-mask path. The fused reduction is identical to ggml_vec_max_f32 (same seed, update, order and values), so results are unchanged. Verified with test-backend-ops (212/212 SOFT_MAX cases pass on CPU).
Author
Merging this PR will improve performance by 11.1%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | soft_max |
1.8 ms | 1.6 ms | +11.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-fuse-the-row-max-scan-into-the-mask-pass-in-ggml-c-1785123776772 (f642bed) with master (048bb65)
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
Optimizes
ggml_compute_forward_soft_max_f32inggml/src/ggml-cpu/ops.cpp— the CPU soft-max kernel that normalizes attention scores, exercised by thesoft_maxbenchmark.Analysis
Flamegraph analysis of
soft_max(baseline cpuTotal ~1.83 ms, ~72% instruction-bound) showed the kernel does several separate full passes over the per-row work bufferwp: copy, scale, additive-mask, then a standaloneggml_vec_max_f32scan to find the row maximum for the numerically-stable exponentiation. That separate max scan re-reads the entire row that the mask pass has just finished writing.Change
When an additive mask is present (the common attention case), the row maximum is now accumulated inside the same loop that applies the mask:
The dedicated
ggml_vec_max_f32(ne00, &max, wp)scan is kept only for the no-mask path, so the total number of full passes over the row drops by one whenever a mask is used.Correctness
The fused reduction is identical to
ggml_vec_max_f32: samemax = -INFINITYseed, samemax = MAX(max, wp[i])update, same iteration order, computed over the same finalwp[i]values (mask already applied). The subsequent sinks correction (max = MAX(max, sk[i02])) is unchanged.Verified with
test-backend-ops test -o SOFT_MAX -b CPU: 212/212 cases pass, covering f32/f16 masks, ALiBi (max_bias), sinks, broadcast, and no-mask rows.Performance (CodSpeed, CPU simulation)
Measured locally through the CodSpeed CLI against a clean baseline built from the default branch:
The instruction-count drop of ~11% corresponds exactly to the eliminated read pass; cache and memory time are unchanged. The other 14 benchmarks are unaffected — no regressions.