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..f4b3178f2 100644 --- a/src/rulesets/Base/indexing.jl +++ b/src/rulesets/Base/indexing.jl @@ -147,7 +147,7 @@ function ∇getindex!(dx::AbstractArray, dy, inds::Integer...) @views dx[inds...] += dy return dx end -function ∇getindex!(dx::AbstractArray, dy, inds...) +function ∇getindex!(dx::AbstractArray, dy, inds::Vararg{Any,N}) where {N} # force specialization view(dx, inds...) .+= dy return dx end @@ -176,7 +176,9 @@ function ∇getindex!(dx::AbstractGPUArray, dy, inds::Integer...) view(dx, inds...) .+= Ref(dy) return dx end -function ∇getindex!(dx::AbstractGPUArray, dy, inds::Union{Integer, AbstractUnitRange, Base.Slice}...) +function ∇getindex!( + dx::AbstractGPUArray, dy, inds::Vararg{Union{Integer,AbstractUnitRange,Base.Slice},N} +) where {N} view(dx, inds...) .+= dy return dx end diff --git a/test/rulesets/Base/indexing.jl b/test/rulesets/Base/indexing.jl index a88eb55a0..ad2cfb45a 100644 --- a/test/rulesets/Base/indexing.jl +++ b/test/rulesets/Base/indexing.jl @@ -189,6 +189,17 @@ end dxs_gpu = unthunk(back(jl([1.0]))[2]) @test dxs_gpu == [jl([1.0])] end + + @testset "∇getindex! does not allocate" begin + function allocs_∇getindex!(dx, dy, inds::Vararg{Any,N}) where {N} + ChainRules.∇getindex!(dx, dy, inds...) # compile + return @allocated ChainRules.∇getindex!(dx, dy, inds...) + end + dx = zeros(3, 4) + @test allocs_∇getindex!(dx, rand(3), Base.to_indices(dx, (:, 2))...) == 0 + @test allocs_∇getindex!(dx, rand(2, 2), 2:3, 1:2) == 0 + @test allocs_∇getindex!(dx, 1.0, 2, 3) == 0 + end end # first & tail handled by getfield rules