Repository navigation
Specialize any, all, count and findall on the predicate, and infer any and all as Bool - #191
Merged
Merged
Conversation
any, all, count and findall on the predicateany, all, count and findall on the predicate, and infer any and all as Bool
These methods, and most of the internal ones they call, only pass the predicate on, so Julia compiles them for any `Function`. In that code, `check_bool_result`'s `promote_op` runs inference on every call, which Julia reports as recompilation, and which costs allocations and a fraction of a microsecond per call. Since GPUArrays 12, Base's `any`, `all` and `findall` of GPU arrays take this path. Name the predicate's type in every method that takes it, as the reductions do, and test that a warm call compiles nothing.
Since JuliaGPU#189 looks up the tunings behind an inference barrier, `Auto()` picks a GPU's predicate algorithm (`ConcurrentWrite` or `ViaReduce`) from a value that inference does not see. The plan then does not infer, and neither does the result: `any` and `all` of a GPU array infer as `Any`, and so do callers of Base's `any` and `all` on one that return or use the result, e.g. `checkbounds(Bool, x, i::CuVector)`. Every algorithm returns a `Bool`, so assert it.
maleadt
force-pushed
the
ab/specialize-predicates
branch
from
October 28, 2026 09:16
6a9a350 to
584ec2f
Compare
Member
|
Thanks. Rebased and slightly reworked. |
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.
Since GPUArrays 12, Base's
any,allandfindallof GPU arrays go through AK, and on main a warm call still runs inference every time:any,all,count,findalland most of the internal methods they call only pass the predicate on, so Julia compiles them forpred::Function. In that code,check_bool_result'sBase.promote_op(pred, eltype(v))can't fold and runs inference on each call. This PR annotates the predicate aspred::F ... where {F}in those methods, as the reductions already do withf::F, op::OP. A warm call then compiles nothing, and allocates about half as much on the host: on an RTX 5080,any(>(0), x)goes from 63 allocations (2.9 KB) and a median of 12.3–12.7 µs to 48 allocations (1.5 KB) and 11.6–11.9 µs, andAK.any(isnan, x)on 10,000Float32s from 12.1–12.5 µs to 11.4–11.6 µs.Separately, since #189 the tunings are looked up behind
Base.inferencebarrier.Auto()picks a GPU's predicate algorithm,ConcurrentWriteorViaReduce, from the tuning, so the plan doesn't infer, and neither does the result:Base.infer_return_type(all, Tuple{typeof(isfinite), CuVector{Float64, CUDA.DeviceMemory}})isAnyon main, and so is every caller that returns or uses the result, e.g.checkbounds(Bool, x, i::CuVector{Int32}). Hiding the tuning is intended (it avoids invalidations when loading a backend), soanyandallnow assert that their result is aBool, which every algorithm returns. Run-time dispatch of the algorithm stays. The other operations that use tunings are not affected: reductions, scans, sorts,countandfindallstill infer withAuto()on CUDA, because their tuning only picks values or the algorithm's type doesn't change the result type.The tests check that a warm call of
any,allandfindallby dynamic dispatch compiles nothing, and thatanyandallinfer with each algorithm, includingAuto(). Both fail on main (the second only on GPUs). The CPU tests (--cpu --cpu-ka) and the CUDA tests for the predicates,findalland the reductions pass on Julia 1.12, and the new tests also pass on 1.10.sort!'sbyhas the same problem (promote_op(by, ...)in the sort setup still runs inference on each call). That's left for a separate PR.