Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
34 changes: 31 additions & 3 deletions src/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)

Expand All @@ -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...)
Expand Down
35 changes: 34 additions & 1 deletion test/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
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
Expand Down Expand Up @@ -182,6 +191,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)
Expand Down
Loading