diff --git a/Project.toml b/Project.toml index 1d8c5e7b6..6f9e69e11 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ChainRules" uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2" -version = "1.74.14" +version = "1.74.15" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/src/rulesets/Base/indexing.jl b/src/rulesets/Base/indexing.jl index f4b3178f2..27e861cc6 100644 --- a/src/rulesets/Base/indexing.jl +++ b/src/rulesets/Base/indexing.jl @@ -65,11 +65,28 @@ 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 +# `add!!(dx, ∇getindex(x, dy, inds...))`, only touching `dx[inds...]` if the projection is elementwise +function ∇getindex_add!(dx, x, dy, inds::Vararg{Any,N}) where {N} + if ProjectTo(x) isa ProjectTo{AbstractArray} + plain_inds = Base.to_indices(x, inds) + return ∇getindex!(dx, _project_getindex(x, dy, plain_inds...), plain_inds...) + else + return add!!(dx, ∇getindex(x, dy, inds...)) + end +end + +# Projects `dy` onto `x[inds...]`, which also checks its shape +_project_getindex(x, dy, inds...) = ProjectTo(@views x[inds...])(dy) +# Avoids scalar indexing, e.g. of GPU arrays +function _project_getindex(x::AbstractArray{<:Number}, dy, inds::Integer...) + return ProjectTo(view(x, inds...))(dy)[] +end + """ ∇getindex(x, dy, inds...) @@ -85,8 +102,13 @@ function ∇getindex(x::AbstractArray{T,N}, dy, inds...) where {T,N} # scalar indexing OneElement(dy, plain_inds, axes(x)) else # some from slicing (potentially noncontigous) - dx = _setindex_zero(x, dy, plain_inds...) - ∇getindex!(dx, dy, plain_inds...) + # also checks that `dy` has the shape of the slice, instead of broadcasting it + dy_proj = _project_getindex(x, dy, plain_inds...) + if dy_proj isa AbstractZero + dy_proj + else + ∇getindex!(_setindex_zero(x, dy_proj, plain_inds...), dy_proj, plain_inds...) + end end return ProjectTo(x)(dx) # since we have x, may as well do this inside, not in rules end diff --git a/src/rulesets/Base/sort.jl b/src/rulesets/Base/sort.jl index 88b02ccd8..932d08ad2 100644 --- a/src/rulesets/Base/sort.jl +++ b/src/rulesets/Base/sort.jl @@ -92,7 +92,7 @@ function rrule(::typeof(unique), x::AbstractArray{<:Number}; dims=:) end mask = isequal.(permutedims(ys), xs) # unique([0.0, -0.0, NaN, NaN]) mask .= (mask .== cumsum(mask, dims=1) .== true) # this implements findfirst(mask; dims=1) - keep = map(I -> I[1], findall(mask)) + keep = map(I -> I[1], findall(mask))::Vector{Int} # `eachslice` is not inferred if dims isa Colon # The function `∇getindex` allows second derivatives. dx = reshape(∇getindex(vec(x), vec(dy), keep), axes_x) ## TODO understand again why vec! diff --git a/test/rulesets/Base/indexing.jl b/test/rulesets/Base/indexing.jl index ad2cfb45a..1d24e146a 100644 --- a/test/rulesets/Base/indexing.jl +++ b/test/rulesets/Base/indexing.jl @@ -200,6 +200,54 @@ end @test allocs_∇getindex!(dx, rand(2, 2), 2:3, 1:2) == 0 @test allocs_∇getindex!(dx, 1.0, 2, 3) == 0 end + + @testset "add! agrees with unthunk" begin + function test_add!(x, dy, inds...; f=getindex, acc=zero(x)) + dx = rrule(f, 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] + # projection to real numbers + test_add!(x, 1.0 + 2.0im, 1) + test_add!(x, [1.0im, 2.0im], 1:2) + test_add!(x, fill(1.0 + 2.0im), 2; f=view) + test_add!(x, [1.0im, 2.0im], 1:2; f=view) + # projection to Float32, with exactly representable values for repeated indices + 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]; acc=zeros(3)) + test_add!(x32, [0.1, 0.2], [true, false, true]; acc=zeros(3)) + test_add!(rand(Float32, 2, 3), 0.1, CartesianIndex(2, 3); acc=zeros(2, 3)) + test_add!(rand(Float32, 2, 3), 0.1, 5; acc=zeros(2, 3)) # linear index + # zero cotangents + test_add!(x, @thunk(ZeroTangent()), 2) + test_add!(x, @thunk(ZeroTangent()), 1:2) + test_add!(x, [ZeroTangent(), ZeroTangent()], 1:2) + # structured arrays + 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)) + # arrays of arrays + 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) + # GPU arrays: broadcasting, and copying to the CPU for repeated indices + x_gpu = jl(Float32[1 2 3; 4 5 6]) + test_add!(x_gpu, jl([0.1, 0.2]), :, 2; acc=jl(zeros(2, 3))) + test_add!(x_gpu, jl([0.5, 0.25, 1.0]), 1, jl([2, 2, 1]); acc=jl(zeros(2, 3))) + # scalar index of a GPU array, without scalar indexing + (_, ind), back = rrule(findmax, x_gpu) + dx = back(Tangent{Any}(0.1 + 2.0im, NoTangent()))[2] + @test dx.add!(jl(zeros(2, 3))) == jl([0 0 0; 0 0 Float64(0.1f0)]) + end + + @testset "cotangent must match the slice" begin + for dy in ([0.5], 0.5, [0.1, 0.2]) + dx = rrule(getindex, rand(3), 1:3)[2](dy)[2] + @test_throws DimensionMismatch unthunk(dx) + @test_throws DimensionMismatch dx.add!(zeros(3)) + end + end end # first & tail handled by getfield rules