Repository navigation
perf: fold contiguous reductions directly; NumPy's pairwise sum for float32/complex - #51
Merged
Merged
Conversation
…loat32/complex A float32 Sum of 2^20 elements took 6.85 ms on POWER9 against 0.23 ms for float64: 6.8 ns an element, while Add was only 1.5x slower. Every non-float64 reduction walked [outer][axisLen][inner] with the reduced axis outside, so a whole-array sum (inner == 1) took a slice and ran a one-iteration loop per element. A contiguous run is now folded directly, as NumPy's add and multiply reductions do: the first element, then the rest added to it, pairwise for float32 and complex (NumPy's FLOAT/CFLOAT_pairwise_sum, loops_utils.h.src v2.5.3), in order for integers and products. POWER9, 1 thread, n=2^20, benchstat over 6 interleaved runs (p=0.002): float32 Sum -76%, int64 -64%, int32 -54%, float64 (control) ~. More accurate too: error vs the exact sum 75 -> 0.87 on 2^16 mixed values, and 2^24 + 1000 ones no longer saturates at 2^24; the new tests fail on the old code. Coverage 100%; compiles for 386 and arm. Also: BenchmarkDTypeSum/Add, a perf.md section, and the roadmap brought up to date (fft, linalg and the Ruby binding shipped; float32 SIMD is what is open). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
The problem. A float32
Sumof 2²⁰ elements took 6.85 ms on POWER9 against 0.23 ms for float64: 6.8 ns per element. YetAddon float32 was only 1.5× slower than on float64, and a missing SIMD kernel explains a factor of 4–8, not 30.The cause. Every non-float64 reduction (
reduceNum) walked the[outer][axisLen][inner]layout with the reduced axis outside and the kept elements inside. A whole-array sum (inner == 1) therefore took a slice and ran a one-iteration loop for every element.The fix. A contiguous run is now folded directly, as NumPy's add and multiply reductions do: the result starts as the first element and the rest is added to it.
FLOAT/CFLOAT_pairwise_sum(numpy/_core/src/umath/loops_utils.h.src, v2.5.3): eight accumulators, blocks of 128, halved above that. The complex variant counts floats as NumPy does.inner > 1path is unchanged.Measured. On POWER9 (cfarm29), one thread pinned to the idlest CPU, the host 3% busy before and after. Six runs each with old and new binaries alternating, compared with
benchstat, p = 0.002:SumSumSumSum(own kernels, control)More accurate too. The expectations come from exact arithmetic, not from the code:
math/bigsum falls from 75 to 0.87.The new tests fail on the old code (saturation and error). Coverage stays at 100%, with a new test for the product along a non-contiguous axis, which the fast path no longer exercises incidentally. The tests compile for 386 and arm.
Also in this PR:
BenchmarkDTypeSumandBenchmarkDTypeAdd.docs/perf.mdsection.Sum) and the integer sums' extra conversion pass.Not claimed: bit-identity with NumPy for large float32 sums. The algorithm is NumPy's, but whether NumPy's iterator feeds the loop in one call for every size was not verified by running NumPy.
🤖 Generated with Claude Code