Skip to content

Follow Base when accumulating along a dimension beyond the array's - #841

Merged
maleadt merged 2 commits into
mainfrom
tb/accumulate-beyond-ndims
Oct 27, 2026
Merged

maleadt merged 2 commits into
mainfrom
tb/accumulate-beyond-ndims

Conversation

@maleadt

@maleadt maleadt commented Oct 27, 2026

Copy link
Copy Markdown
Member

JuliaLang/julia#64233 (1.15.0-DEV.147, marked for backport to 1.14) changed what accumulate does with a dims beyond ndims(A). Base used to copy the array and ignore init. Now it treats the extra dimension as a trailing singleton, so each element starts a scan of its own: it is combined with init, or passed through reduce_first when no init is given.

julia> accumulate(+, [1 2; 3 4]; dims=3, init=10)
2×2 Matrix{Int64}:
 11  12      # Julia 1.13: [1 2; 3 4]
 13  14

GPUArrays still copied, so the testsuite's accumulate comparisons against Base fail on Julia nightly for every back-end, e.g. OpenCL.jl (log). JLArrays runs Base's scans on the host, so its tests never reach this code path and stay green.

This PR checks once at load time which behavior Base has, instead of comparing VERSION, because the change will also reach a 1.14 release. On older versions GPUArrays keeps copying. The testsuite gets a case with a tuple init and a non-commutative operator, which catches swapped operands and broadcasting over init, and one with an empty input.

With no elements to process, `map!` returned `nothing` instead of `dest`.
`map` of arguments with different lengths goes through `map!` and
returned `nothing` too, e.g. `map(+, empty, nonempty)`.
JuliaLang/julia#64233 (Julia 1.15.0-DEV.147, marked for backport to 1.14)
made `accumulate` treat a `dims` beyond `ndims(A)` as a trailing singleton
dimension: each element now starts a scan of its own, combined with
`init` or passed through `reduce_first`, instead of being copied. GPUArrays
still copied, so the testsuite's comparisons against Base fail on Julia
nightly for every back-end.

Detect which behavior Base has rather than checking `VERSION`, because
the change will also reach a 1.14 release.
@maleadt
maleadt force-pushed the tb/accumulate-beyond-ndims branch from f9efe4c to 529e9dd Compare October 27, 2026 14:01
@maleadt
maleadt changed the base branch from main to tb/map-empty-return October 27, 2026 14:01
@maleadt
maleadt added this pull request to stack #843 October 27, 2026 14:01
Base automatically changed from tb/map-empty-return to main October 27, 2026 19:52
@maleadt
maleadt merged commit 03221a3 into main Oct 27, 2026
17 of 18 checks passed
@maleadt
maleadt deleted the tb/accumulate-beyond-ndims branch October 27, 2026 19:52
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