Skip to content
Merged
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
6 changes: 4 additions & 2 deletions src/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading