Repository navigation
Let reductions infer their result type - #809
Merged
Merged
Conversation
`_mapreduce` assigns `A` on several paths and captured it in the closure that checks for an empty reduced dimension, so `A` was boxed and every reduction (`sum`, `prod`, `mapreduce`, `dot`, ...) inferred as `Any`. Capture the size instead, and test that the reductions infer. Assisted-by: Claude Code (Opus 5.5)
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.
Requested by Valentin
Before: on GPUArrays 12 every reduction of a GPU array infers as
Any:sum(A),sum(A; dims=1),prod(A),mapreduce(*, +, A, B)anddot(a, b)(on 11.5 they inferredFloat64). Callers lose type stability, and Enzyme.jl'sdotrule forAbstractGPUArrayfails because the call it intercepts no longer has a concrete return type.After: these reductions infer their concrete result type again.
_mapreduceassignsAon several paths (only(As)or aBroadcasted) and the closured -> d <= ndims(A) && size(A)[d] == 0captured it, so Julia boxedAfor the whole function. The closure now capturessize(A)instead.How: one-line change in
src/host/mapreduce.jl, plus@inferredchecks forsum,sum(; dims),prodand two-argumentmapreducein thereductions/sum prodtestsuite. Without the fix that testsuite errors for JLArray ("return type Int64 does not match inferred return type Any"). With it,JLArray/reductions/sum prodandArray/reductions/sum prodpass locally (1128/1128, Julia 1.12.7).🤖 Generated with Claude Code