ggml-cpu: vectorize the fused RMS-norm kernel (ggml_compute_forward_rms_norm_f32) - #32
Open
codspeed-hq[bot] wants to merge 1 commit into
Conversation
Vectorize both hot loops of the fused RMS-norm path (ggml_compute_forward_rms_norm_f32 with GGML_RMS_NORM_FUSE_OP_MUL): - replace the scalar sum-of-squares reduction with the already-vectorized ggml_vec_dot_f32 (sum(x*x)) - replace the scalar output loop with a new ggml_vec_scale_mul_f32 helper computing y[i] = (x[i]*s)*w[i] using the existing GGML_SIMD macros, with generic SIMD, SVE, RISC-V, and scalar paths. The two multiplies are kept separate (no FMA) so the result matches the original scalar expression. rms_norm micro-benchmark: 563.2 us -> 325.5 us (simulation), no regressions.
Author
Merging this PR will improve performance by 73.53%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | Simulation | rms_norm |
561.2 µs | 323.4 µs | +73.53% |
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-fused-rms-norm-kernel-ggml-compute-f-1785230254392 (c1db5d3) 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
Vectorizes both hot loops of the fused RMS-norm path (
ggml_compute_forward_rms_norm_f32withGGML_RMS_NORM_FUSE_OP_MUL) inggml/src/ggml-cpu/ops.cpp. This is thenorm × weightop applied before every attention and feed-forward block, so it is a genuine inference hot path.Analysis
The CI walltime flamegraph for
prompt_layer[f32]showsggml_compute_forward_rms_norm_mul_fusedat ~26.8% self time — the second-hottest function after the matmul dot kernel. Both of its per-row loops were plain scalar loops that GCC does not auto-vectorize at-O2:sum += x[i]*x[i]), andy[i] = x[i]*scale*w[i]).Change
ops.cpp: replaced the scalar sum-of-squares reduction with a call to the already-vectorizedggml_vec_dot_f32(n, &sumf, …, x, …, x, …)(i.e.sum(x·x)), and replaced the scalar output loop with a newggml_vec_scale_mul_f32helper.vec.h: addedggml_vec_scale_mul_f32(n, y, x, w, s)computingy[i] = (x[i]*s)*w[i]with the existingGGML_SIMDmacros. Paths are provided for the generic SIMD build (AVX2/NEON, 4-accumulator idiom matchingggml_vec_scale_f32), SVE (2× unroll + predicated tail), and RISC-V, plus a scalar fallback. The two multiplies are kept separate (no FMA) so the result matches the original scalar expression.Validation
scale > 0.0finternally). Kept as two separate multiplies so the result is numerically equivalent to the original scalar expression.rms_normThe other 14 benchmarks are unchanged; no regressions.