ggml-cpu: vectorize sum-of-squares reduction in RMS-norm F32 kernel - #33
Open
codspeed-hq[bot] wants to merge 1 commit into
Conversation
Replace the scalar double-precision sum-of-squares loop in ggml_compute_forward_rms_norm_f32 with the vectorized ggml_vec_dot_f32 kernel, since sum(x[i]*x[i]) == dot(x, x). This matches the idiom already used by ggml_vec_norm_f32 and improves the rms_norm benchmark by ~20% in CodSpeed simulation mode with no regressions.
Author
Merging this PR will improve performance by 20.85%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | Simulation | rms_norm |
561.2 µs | 464.4 µs | +20.85% |
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-sum-of-squares-reduction-in-the-fuse-1785230901711 (d8d6aa6) 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
Replaces the scalar sum-of-squares reduction in the F32 RMS-norm kernel (
ggml_compute_forward_rms_norm_f32inggml/src/ggml-cpu/ops.cpp) with the already-vectorized dot-product kernelggml_vec_dot_f32.Why this is the hot path
RMS-norm runs before every attention and feed-forward block. It is dispatched from the fused
ggml_compute_forward_rms_norm_mul_fusedpath, which flamegraph analysis of the walltime macro benchmarks showed to be the single largest self-time function ofprompt_layer[q4_k]. Its first pass computed the per-row sum of squares with a plain scalar loop that even carried the code's own// worth switching to explicit SIMD?comment. Theggml_float(double) accumulation forces scalar execution and leaves the vector units idle over the entire hidden dimension for every row.Change
sum(x[i]*x[i])is exactlydot(x, x), so the loop is replaced with the existing SIMD kernel (8-way multi-accumulator FMA; NEON/SVE on ARM, AVX/FMA on x86):This is the same idiom already used by
ggml_vec_norm_f32invec.h, so it introduces no new pattern. The change is architecture-neutral and benefits both the simulation micro path and the walltime macro path. Net −3 lines, scoped strictly to the reduction.Correctness
The scalar
1/sqrt(mean+eps)scale is unchanged; only the reduction's accumulation strategy differs (float multi-accumulator vs. double scalar). I verified this with a standalone check over 100 randomn=2048rows: the maximum relative difference in the resulting scale factor was1.4e-7(float-epsilon level), far within the relative-error tolerance the RMS_NORM backend test uses.Validation
Measured via CodSpeed simulation mode (micro benchmark suite), baseline vs. head:
rms_normThe other simulation benchmarks were unchanged — no regressions.