Vectorize the fp32 RoPE rotate_pairs kernel with AVX2/FMA - #18
Open
codspeed-hq[bot] wants to merge 1 commit into
Open
Conversation
Add an AVX2/FMA fast path to rotate_pairs for the fp32, scale == 2 case (NEOX / MROPE / IMROPE / VISION). Processes 8 pairs per iteration using contiguous loads/stores, deinterleaved cos/sin vectors, and FMA. The fp16 instantiation, the GGML_ROPE_TYPE_NORMAL path, and non-AVX2 builds are unchanged and fall back to the original scalar loop.
Author
Merging this PR will improve performance by 47.33%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | rope |
558.6 µs | 379.2 µs | +47.33% |
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-fp32-rope-rotate-pairs-kernel-with-a-1785125858504 (3a274d0) 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
Adds an AVX2/FMA fast path to
rotate_pairsinggml/src/ggml-cpu/ops.cpp, the kernel that applies rotary position embedding (RoPE) to the query/key projections of every transformer layer.Analysis
Flamegraph analysis of the
ropebenchmark (558 µs baseline) showed 84% of self time insiderotate_pairs<float>, split roughly evenly between instructions and memory. The kernel was a purely scalar loop rotating one pair of elements per iteration, with per-element conversions and no SIMD, despite the build targeting AVX2/FMA.Change
For the fp32,
scale == 2case (GGML_ROPE_TYPE_NEOX/MROPE/IMROPE/VISION), the indexic = i0/2advances contiguously, sosrc[ic]andsrc[ic + n_offset]are two contiguous fp32 spans, and the cos/sin values live interleaved incache[2*ic]/cache[2*ic + 1].The new path processes 8 pairs per iteration:
__m256vectors,cos/sinvectors via_mm256_shuffle_ps+_mm256_permutevar8x32_ps,dst[ic] = x0*cos - x1*sinanddst[ic+n_offset] = x0*sin + x1*coswith FMA (_mm256_fmsub_ps/_mm256_fmadd_ps),A scalar tail handles the remainder. The path is guarded by
#if defined(__AVX2__) && defined(__FMA__)and a runtimescale == 2 && std::is_same<T, float>check, so:ggml_fp16_t) instantiation is unchanged,GGML_ROPE_TYPE_NORMAL(scale == 1) path is unchanged,Correctness
Verified via the repository's own tests:
tests/test-rope— passes.tests/test-backend-ops test -o ROPE -b CPU— 448/448 ROPE tests passed (274 of them fp32), 0 failures, across all modes (NEOX, MROPE, IMROPE, VISION) and shapes including non-multiple-of-8 tails. The CPU output is compared against the reference implementation.Performance (CodSpeed, CPU simulation)
rope14 other benchmarks unchanged, 0 regressions.