Repository navigation
Define neutral elements for the @fastmath reduction operators - #840
Open
unsound-mint wants to merge 2 commits into
Open
unsound-mint wants to merge 2 commits into
unsound-mint wants to merge 2 commits into
Conversation
maleadt
force-pushed
the
perf-833-fastmath-neutral-element
branch
from
October 27, 2026 20:07
a327728 to
119ba7c
Compare
`@fastmath reduce(max, x; dims, init)` reduces with `Base.FastMath.max_fast`, which had no `neutral_element` method. AcceleratedKernels then falls back to its slower empty-lane path even though `init` is given. Add methods for `add_fast`, `mul_fast`, `min_fast` and `max_fast`, matching the operators they replace. On an RTX 4070 Laptop GPU, `@fastmath reduce(max, x; dims=1, init=-Inf)` on a BFloat16 1024x1024x96 array goes from 2.4x to 1.0x the time of `reduce(max, ...)`, and on a 50304x8192 array from 1.7x to 1.0x. Bump GPUArraysCore to 0.2.2, as GPUArrays pins its exact version.
Since JuliaGPU#822, only operators known to commute use AcceleratedKernels' unordered kernels, and `add_fast`, `mul_fast`, `min_fast` and `max_fast` were not among them, so `@fastmath` reductions took the slower ordered path. They commute on the same types as the operators they replace, up to NaNs and the sign of zero, which `@fastmath` may ignore anyway.
maleadt
force-pushed
the
perf-833-fastmath-neutral-element
branch
from
October 27, 2026 20:12
119ba7c to
f36a90f
Compare
This branch has not been deployed
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.
Fixes #833.
@fastmathreplacesmax,min,+and*withmax_fast,min_fast,add_fastandmul_fast. Reductions with these were slower than with the plain operators, for two reasons:neutral_elementhad no methods for them, so AcceleratedKernels seeded every partial result with an empty lane instead. The first commit adds the four methods to GPUArraysCore. That makes this a GPUArraysCore 0.2.2 release, with the pin in the rootProject.tomlupdated as in Moveneutral_elementto GPUArraysCore #789.@fastmathoperators to_commutative, on the same types as the operators they replace. Strictly they commute only up to NaNs and the sign of zero, which@fastmathmay ignore anyway.Times in µs on an RTX 5080,
@fastmath reduce(op, x; dims, init)against the plainreduce(op, ...), L2 cache flushed, minimum of 20 runs:maxmax_fastonmainmax_fastwith this PRFloat321024×1024×96,dims=1BFloat161024×1024×96,dims=1Float32768×8192,dims=2add_fastand scalar (dims=:) reductions behaved the same way. Themaincolumn is from before #822, whenBFloat16maxtook 242 µs.BFloat16isn't anIEEEFloat, so #822 sends its plain reductions down the ordered path too. That's a separate issue.One visible difference: a
@fastmathsum of-0.0values now returns0.0, becauseadd_fastis seeded with+0.0.@fastmathpermits this.Tests:
main);_commutativefor the@fastmathoperators;@fastmathreduction per operator and real element type, compared against the CPU.