From 6ac471bd31850aad5e8df4a64e361cebc4d691a3 Mon Sep 17 00:00:00 2001 From: CarloLucibello Date: Wed, 11 Nov 2026 08:04:11 +0100 Subject: [PATCH 1/2] Make the getindex `add!` agree with its value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `add!` of the `InplaceableThunk` returned by the `getindex` and `view` rules added the raw cotangent, skipping the `ProjectTo(x)` that `val` applies, so it could differ from `unthunk` or throw. It now projects `dy` when `ProjectTo(x)` acts elementwise, staying O(length(dy)), and adds `val` otherwise. Scalar `∇getindex` also projects `dy` before building the `OneElement`, which avoids materializing it for mixed precision. Part 1 of #863. Co-Authored-By: Claude Opus 5.5 --- Project.toml | 2 +- src/rulesets/Base/indexing.jl | 34 +++++++++++++++++++++++++++++++--- test/rulesets/Base/indexing.jl | 30 +++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index faf9a412c..1d8c5e7b6 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ChainRules" uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2" -version = "1.74.13" +version = "1.74.14" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/src/rulesets/Base/indexing.jl b/src/rulesets/Base/indexing.jl index c6623822e..c6cf0c246 100644 --- a/src/rulesets/Base/indexing.jl +++ b/src/rulesets/Base/indexing.jl @@ -65,11 +65,39 @@ end function thunked_∇getindex(x, dy, inds...) return InplaceableThunk( - dx -> ∇getindex!(dx, unthunk(dy), Base.to_indices(x, inds)...), + dx -> _∇getindex_add!(dx, x, unthunk(dy), inds...), @thunk(∇getindex(x, unthunk(dy), inds...)), ) end +# Same as `add!!(dx, ∇getindex(x, dy, inds...))`, but only touches `dx[inds...]` when it can. +function _∇getindex_add!(dx, x, dy, inds...) + dy_proj = _project_getindex(x, dy, inds...) + dy_proj === nothing && return add!!(dx, ∇getindex(x, dy, inds...)) + dy_proj isa AbstractZero && return dx + return ∇getindex!(dx, dy_proj, Base.to_indices(x, inds)...) +end +_∇getindex_add!(dx, x, dy::AbstractZero, inds...) = dx + +""" + _project_getindex(x, dy, inds...) + +Applies to `dy` the part of `ProjectTo(x)` acting on `x[inds...]`, or returns `nothing` +if `ProjectTo(x)` doesn't act elementwise (e.g. for `Diagonal` or `Symmetric`). +""" +function _project_getindex(x::AbstractArray, dy, inds...) + if eltype(x) <: Number + project = ProjectTo(x) + project isa ProjectTo{AbstractArray} || return nothing + dy isa AbstractArray || return project.element(dy) + return ProjectTo{AbstractArray}(; element=project.element, axes=axes(dy))(dy) + elseif x isa Array + # one projector per element, so avoid `ProjectTo(x)` which costs O(length(x)) + return ProjectTo(x[inds...])(dy) + end + return nothing +end + """ ∇getindex(x, dy, inds...) @@ -82,8 +110,8 @@ function ∇getindex(x::AbstractArray{T,N}, dy, inds...) where {T,N} # leaving just Int / AbstractVector of Int plain_inds = Base.to_indices(x, inds) dx = if plain_inds isa NTuple{N, Int} && T<:Number - # scalar indexing - OneElement(dy, plain_inds, axes(x)) + # scalar indexing. Projecting `dy` first keeps `ProjectTo(x)` from materializing `dx`. + OneElement(something(_project_getindex(x, dy, inds...), dy), plain_inds, axes(x)) else # some from slicing (potentially noncontigous) dx = _setindex_zero(x, dy, plain_inds...) ∇getindex!(dx, dy, plain_inds...) diff --git a/test/rulesets/Base/indexing.jl b/test/rulesets/Base/indexing.jl index a88eb55a0..03becabf8 100644 --- a/test/rulesets/Base/indexing.jl +++ b/test/rulesets/Base/indexing.jl @@ -1,5 +1,9 @@ using ChainRules, - ChainRulesCore, ChainRulesTestUtils, GPUArraysCore, JLArrays, LinearAlgebra, Test + ChainRulesCore, ChainRulesTestUtils, FiniteDifferences, GPUArraysCore, JLArrays, + LinearAlgebra, Test + +# Otherwise the struct fallback also perturbs the index field +FiniteDifferences.to_vec(x::ChainRules.OneElement) = to_vec(collect(x)) struct FooTwoField x::Float64 @@ -182,6 +186,30 @@ end @test unthunk(bk3(jl(ones(3)))[2]) == jl([2 1 0; 0 0 0]) end + @testset "add! agrees with unthunk" begin + function test_add!(x, dy, inds...; acc=zero(x)) + dx = rrule(getindex, x, inds...)[2](dy)[2] + @test dx.add!(copy(acc)) == acc + unthunk(dx) + end + x, x32 = [1.0, 2.0, 3.0], Float32[1, 2, 3] + test_add!(x, 1.0 + 2.0im, 1) + test_add!(x, [1.0im, 2.0im], 1:2) + test_add!(x32, 0.1, 2; acc=zeros(3)) + test_add!(x32, [0.1, 0.2], 2:3; acc=zeros(3)) + test_add!(x32, [0.5, 0.25, 1.0], [2, 2, 1]) + test_add!(Diagonal(rand(3)), [1.0, 2.0, 3.0], 2, :; acc=zeros(3, 3)) + test_add!(Symmetric(rand(3, 3)), 1.0, 2, 3; acc=zeros(3, 3)) + xs, acc = [Float32[1, 2], Float32[3, 4]], [zeros(2), zeros(2)] + test_add!(xs, [0.1, 0.2], 2; acc) + test_add!(xs, [[0.1, 0.2], [0.3, 0.4]], 1:2; acc) + + @test unthunk(rrule(getindex, x32, 2)[2](0.1)[2]) isa ChainRules.OneElement{Float32} + + x_gpu = jl(Float32[1 2 3; 4 5 6]) + dx = rrule(getindex, x_gpu, :, 2)[2](jl([0.1, 0.2]))[2] + @test dx.add!(jl(zeros(2, 3))) == jl(Float64[0 0.1f0 0; 0 0.2f0 0]) + end + @testset "getindex(::Array{<:AbstractGPUArray})" begin x_gpu = jl(rand(1)) y, back = rrule(getindex, [x_gpu], 1) From b3e98263d2e44588c380e3226412497ea030aecc Mon Sep 17 00:00:00 2001 From: Carlo Lucibello Date: Thu, 19 Nov 2026 13:54:57 +0100 Subject: [PATCH 2/2] Update test/rulesets/Base/indexing.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/rulesets/Base/indexing.jl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/rulesets/Base/indexing.jl b/test/rulesets/Base/indexing.jl index 03becabf8..e1a158099 100644 --- a/test/rulesets/Base/indexing.jl +++ b/test/rulesets/Base/indexing.jl @@ -1,6 +1,11 @@ using ChainRules, - ChainRulesCore, ChainRulesTestUtils, FiniteDifferences, GPUArraysCore, JLArrays, - LinearAlgebra, Test + ChainRulesCore, + ChainRulesTestUtils, + FiniteDifferences, + GPUArraysCore, + JLArrays, + LinearAlgebra, + Test # Otherwise the struct fallback also perturbs the index field FiniteDifferences.to_vec(x::ChainRules.OneElement) = to_vec(collect(x))