Skip to content

Let reductions infer their result type - #809

Merged
vchuravy merged 1 commit into
mainfrom
vc/mapreduce-inference
Oct 9, 2026
Merged

vchuravy merged 1 commit into
mainfrom
vc/mapreduce-inference

Conversation

@vchuravy

@vchuravy vchuravy commented Oct 8, 2026

Copy link
Copy Markdown
Member

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) and dot(a, b) (on 11.5 they inferred Float64). Callers lose type stability, and Enzyme.jl's dot rule for AbstractGPUArray fails because the call it intercepts no longer has a concrete return type.

After: these reductions infer their concrete result type again.

_mapreduce assigns A on several paths (only(As) or a Broadcasted) and the closure d -> d <= ndims(A) && size(A)[d] == 0 captured it, so Julia boxed A for the whole function. The closure now captures size(A) instead.

How: one-line change in src/host/mapreduce.jl, plus @inferred checks for sum, sum(; dims), prod and two-argument mapreduce in the reductions/sum prod testsuite. Without the fix that testsuite errors for JLArray ("return type Int64 does not match inferred return type Any"). With it, JLArray/reductions/sum prod and Array/reductions/sum prod pass locally (1128/1128, Julia 1.12.7).

🤖 Generated with Claude Code

`_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)
@vchuravy vchuravy self-assigned this Oct 8, 2026
@vchuravy
vchuravy marked this pull request as ready for review October 9, 2026 12:24
@vchuravy
vchuravy merged commit 9655d1e into main Oct 9, 2026
18 checks passed
@vchuravy
vchuravy deleted the vc/mapreduce-inference branch October 9, 2026 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant