← Back to Pull requests

Support GPUArrays 12 and array-returning `sum(x; dims)` reductions #3957

Closed
unknown closed this pull request without merging on Nov 17, 2026 14:40 UTC
View on GitHub
unknown unknown commented on Nov 06, 2026 21:25 UTC
Author

Supersedes #3788 (its commit is cherry-picked here with @vchuravy's authorship), #3784 and #3785.

Before: Enzyme caps GPUArrays at 11. Lifting the cap alone fails: on GPUArrays 12 EnzymeGPUArraysExt does not precompile, because its rules name GPUArrays.mapreducedim!, which 12 removed (reductions go through AcceleratedKernels now). Separately, sum(x; dims) in reverse mode fails on every GPUArrays version with a MethodError (#3808): the _mapreduce rules only cover a scalar Active return, but has_rrule_from_sig matches on Type{<:Annotation}, so Enzyme commits to a rule and then finds none.

After: Enzyme allows GPUArrays 10, 11 and 12, and sum(x; dims) differentiates in forward and reverse mode on any AbstractGPUArray.

How:

  • mapreducedim! rules are defined only @static if isdefined(GPUArrays, :mapreducedim!). The _mapreduce rule, which handles sum/mapreduce(identity, add_sum, …) on every backend, is unchanged. GPUArrays 12 passes init = _NoInit() (not nothing) when no init is given, so _dinit passes that marker through.

  • New augmented_primal/reverse for _mapreduce with an array (Duplicated/BatchDuplicated) return: the primal runs, a zero shadow of the result goes on the tape, and the reverse pass broadcasts it back along the reduced dims (dA .+= dR). Const-return variants cover an unused result.

  • Tests: test/ext/jlarrays.jl gains dims (1, 2, (1,2)), batched, init, unused-result and forward cases plus a scalar init case; test/cuda.jl gains dims tests (1, 2, (1,2), a dot-weighted variant, batched) and, on CUDA ≥ 6.5, a check that fill! on a Float32 CuArray is no longer ambiguous (#3807). The losses stay off broadcasts on purpose: on CUDA 6.5 + GPUArrays 12 a CuArray broadcast is a KernelAbstractions launch, which CUDACore's Enzyme rules do not cover yet (JuliaGPU/CUDA.jl#3407, see notes).
  • Results

  • Julia 1.13 and 1.10, Pkg.test(test_args=["ext/jlarrays"]): resolves JLArrays 0.4.2 / GPUArrays 12.2.0 (LLVM 9.13), all pass.

  • GPUArrays 11.5.16 / JLArrays 0.3.3 (pinned scratch env): the whole jlarrays file passes, no regression.

  • #3808 MWE: MethodError before, dx == ones after.

  • CUDA on an H200:

  • - CUDA 6.4.2 / GPUArrays 11.5.16 (what Buildkite resolves for this PR, up to the 6.4.1 patch level), Julia 1.13: "GPUArrays linalg rules" 14/14 (the fill! check is skipped below 6.5), "view and reshape keep the shadow buffer alive" 6/6.
    - CUDA 6.5.1 / GPUArrays 12.2.0 / LLVM.jl 10.4.3, stacked on the LLVM 10 PR: the whole test/cuda.jl passes on Julia 1.11; on Julia 1.13 "GPUArrays linalg rules" 15/15 and the refcount test 6/6 (with the CUDA.reclaim() change from the LLVM 10 PR). "Reverse Kernel" fails on 1.13 on this machine only; the same test segfaults here on plain main and passes on 1.11 and in upstream CI.
    - #3808 MWE on CUDA 6.5.1: dx == ones.

    Notes

  • GPUArrays 12 allows LLVM.jl 9, so this is mergeable on its own. CUDA 6.5, Metal 1.12 and AMDGPU 2.11 (the releases on GPUArrays 12) additionally need LLVM.jl 10, i.e. #3741; see #3958.

  • CUDA 6.5 dropped its own _mapreduce/mapreducedim!/fill! rules (JuliaGPU/CUDA.jl#3272), so this extension is now the only owner of them; that removes the fill! ambiguity behind #3807 once CUDA 6.5 is reachable. #3807's MWE still does not run on CUDA 6.5.1, though: its y .+= x is a broadcast, and on GPUArrays 12 broadcasts are KernelAbstractions kernels that CUDACore launches through KI.launch → kernel_launch → managed_kernel_launch, bypassing the HostKernel call its EnzymeCoreExt hooks. Enzyme then descends into the managed-memory bookkeeping and fails with IllegalTypeAnalysisException in CUDACore.acquire_managed(::Vector{Managed}, ::CuStream). On CUDA 6.4.1 / GPUArrays 11 broadcasts launched through @cuda, which has a rule, so sum(x .+ y) in reverse mode worked there (JuliaGPU/CUDA.jl#2455). This is the launch-path gap of JuliaGPU/CUDA.jl#3407 and needs a rule in CUDACore's EnzymeCoreExt. #3807 and #3453 stay open until then.

  • Found while validating, pre-existing on main and on CUDA 6.4.2 (#3370): under runtime activity, _isconst here tests x.dval === x.val to detect a shadow that aliases its primal. A reshape of a Const CuArray gets a shadow that is a different CuArray object over the same buffer, so the check misses it and the _mapreduce reverse rule accumulates the cotangent into the primal (sum(x) grows by the cotangent). The check needs to compare the underlying buffer, not object identity. Left for a separate PR.

  • Only identity/add_sum is covered; other f/op pairs still go through the backend kernel.

  • Observed while testing, unrelated to this PR: matrix dot on a JLArray fails in reverse mode on GPUArrays 11 (EnzymeNonScalarReturnException) and in forward mode on 12 (runtime-activity error inside mapreduce(dot, +)).
  • Closes #3808.

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01MsLzHnCigzwjd2ykwJsqYe

    Diff: 630 lines (+472 / -158) across 7 files Reviewer: vchuravy Review duration: 15435m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 630 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.386
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #3957"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.4% (MEDIUM) · Higher = larger blast radius
    50.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.9% · Safe to merge
    19.9%