From 9dcfe15052060a92031b367da1e6f708b87250b5 Mon Sep 17 00:00:00 2001 From: Julian P Samaroo Date: Tue, 1 Dec 2026 09:49:24 -0700 Subject: [PATCH] Use native product scans for implicit partition initialization AMDGPU and oneAPI's generic accumulate defaults to zero, so lowering cumprod to accumulate(Base.mul_prod) zeroes each partition. Dispatch implicit product partitions through the backend's native cumprod while retaining explicit initialization on the first partition and the existing carry combination. Generic * keeps its distinct promotion behavior. Add both-axis GPU and small-integer/init CPU regressions. On the pinned Julia 1.11.9 / AMDGPU 2.1.0 environment, the same DArray checks go from 12 type passes plus 12 numerical failures to 24 passes. Registered CPU mapreduce passes 492 assertions, with 76 additional helper boundary checks. After 10 warm batches and five GC-counter samples, minimum allocations for 1,000 CPU helper calls fall from 1,424,000 bytes / 25,000 allocations to 240,000 / 8,000 on the implicit path. The explicit-init control is unchanged at 1,504,016 bytes / 28,001 allocations. These are scoped helper controls, not whole-task or GPU throughput measurements; hosted CI is not yet run. Written by an AI agent on jpsamaroo's review feedback. --- AGENTS.md | 25 +++++++++++++++++++++++++ src/array/map-reduce.jl | 12 +++++++++++- test/array/mapreduce.jl | 12 ++++++++++++ test/gpu.jl | 2 ++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index ad8c5740e..47cc77fc5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1217,3 +1217,28 @@ this section verbatim). Each entry is one bullet starting with `- `. the protocol alone is not enough for them. Test with a remote reader *and* a remote writer, in both orders: in one process every slot is the argument itself, so none of this is visible. + +109. **A backend's generic scan and native product scan may initialize + differently.** AMDGPU and oneAPI default generic `accumulate` to a zero + initializer, even for multiplication; their native `cumprod` supplies the + product initialization. Lowering every product partition to generic + `accumulate(Base.mul_prod, ...)` therefore turns it into zeros. Use the + native product operation for partitions without an explicit initializer, + retaining the explicit initializer only on the first partition and the + existing carry combination afterwards. Preserve small-integer promotion + when choosing the native operation: generic `*` and `Base.mul_prod` are + different contracts. This does not supply a generic scan initializer: + without one, every independent scan lane starts from its own first value. + +110. **Validate the private GPU compilation cache before blaming a change.** + With an incomplete private cache, `--compiled-modules=existing` produced + `LLVM.Interop.pointerset` InvalidIR errors even for a tiny broadcast in a + pinned Julia 1.11.9 / AMDGPU 2.1.0 environment. Allowing normal precompilation + into that private depot made the unchanged scan probe run: three controls + passed and three genuine numerical scan failures became visible. Package + sources and versions were unchanged. This does not make existing-only mode + universally invalid; it means a clean backend copy/kernel preflight and a + coherent private cache are prerequisites for interpreting a compiler error. + Never repair this by modifying installed package caches or disabling GPU + validation, and keep backend-only preflight and Dagger integration in + separate processes to isolate process-level state during diagnosis. diff --git a/src/array/map-reduce.jl b/src/array/map-reduce.jl index 67d13a37c..2af0c2031 100644 --- a/src/array/map-reduce.jl +++ b/src/array/map-reduce.jl @@ -224,7 +224,17 @@ std(f::Function, x::DArray; dims=nothing) = # cannot do. Scan each partition on its own instead, then combine the running # last slice of the partitions before it along `dims` into it. Like `reduce`, # this assumes `op` is associative. -_accumulate_part(op, x, RT, dims, kw) = accumulate(op, _widen(x, RT); dims, kw...) +function _accumulate_part(op, x, RT, dims, kw) + part = _widen(x, RT) + # Some array backends default generic accumulate to a zero initializer, + # including for multiplication. Their native product scan supplies the + # correct operation-specific initialization. An explicit init still belongs + # to the first partition; later partitions use the ordinary product scan. + if op === Base.mul_prod && isempty(kw) + return cumprod(part; dims) + end + return accumulate(op, part; dims, kw...) +end # Only the first partition along `dims` starts from `init`. When `init` widens # the eltype, the others would scan in the narrower one and could overflow or # lose precision before the carry reaches them, so widen the partitions first. diff --git a/test/array/mapreduce.jl b/test/array/mapreduce.jl index 64332ed39..e4bf58b01 100644 --- a/test/array/mapreduce.jl +++ b/test/array/mapreduce.jl @@ -125,6 +125,18 @@ end w8 = fill(Int8(100), 10, 4) @test collect(accumulate(+, distribute(w8, Blocks(3, 2)); dims=1, init=0)) == accumulate(+, w8; dims=1, init=0) @test collect(accumulate(+, distribute(w8[:, 1], Blocks(3)); init=0)) == accumulate(+, w8[:, 1]; init=0) + p8 = fill(Int8(2), 10, 4) + Dp = distribute(p8, Blocks(3, 2)) + for dims in (1, 2) + expected = cumprod(p8; dims) + R = cumprod(Dp; dims) + @test eltype(R) == eltype(expected) + @test collect(R) == expected + expected_init = accumulate(Base.mul_prod, p8; dims, init=Int16(2)) + Rinit = accumulate(Base.mul_prod, Dp; dims, init=Int16(2)) + @test eltype(Rinit) == eltype(expected_init) + @test collect(Rinit) == expected_init + end @test_throws ArgumentError cumsum(Dv; dims=0) @test_throws ArgumentError accumulate(+, Dv; foo=1) end diff --git a/test/gpu.jl b/test/gpu.jl index 94f81e1d8..d0d288796 100644 --- a/test/gpu.jl +++ b/test/gpu.jl @@ -104,7 +104,9 @@ end return end @test collect(cumsum(DA; dims=1)) ≈ cumsum(A; dims=1) + @test collect(cumprod(DA; dims=1)) ≈ cumprod(A; dims=1) @test collect(cumprod(DA; dims=2)) ≈ cumprod(A; dims=2) + @test collect(accumulate(Base.mul_prod, DA; dims=2, init=2f0)) ≈ accumulate(Base.mul_prod, A; dims=2, init=2f0) @test collect(accumulate(max, DA; dims=1)) == accumulate(max, A; dims=1) @test collect(accumulate(+, DA; dims=2, init=1f0)) ≈ accumulate(+, A; dims=2, init=1f0) # Metal has no Float64