Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inplace getindex rrule #240

Merged
merged 9 commits into from
Oct 19, 2020
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
1 change: 1 addition & 0 deletions src/ChainRules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ include("rulesets/Base/fastmath_able.jl")
include("rulesets/Base/evalpoly.jl")
include("rulesets/Base/array.jl")
include("rulesets/Base/arraymath.jl")
include("rulesets/Base/indexing.jl")
include("rulesets/Base/mapreduce.jl")

include("rulesets/Statistics/statistics.jl")
Expand Down
27 changes: 27 additions & 0 deletions src/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#####
##### getindex
#####

function rrule(::typeof(getindex), x::Array{<:Number}, inds...)
# removes any logical indexing, CartesianIndex etc
# leaving us just with a tuple of Int, Arrays of Int and Ranges of Int
plain_inds = Base.to_indices(x, inds)
y = getindex(x, plain_inds...)
function getindex_pullback(ȳ)
function getindex_add!(Δ)
# this a optimizes away for simple cases
for (ȳ_ii, ii) in zip(ȳ, Iterators.product(plain_inds...))
Δ[ii...] += ȳ_ii
end
return Δ
end

x̄ = InplaceableThunk(
@thunk(getindex_add!(zero(x))),
getindex_add!
)
return (NO_FIELDS, x̄, (DoesNotExist() for _ in inds)...)
end

return y, getindex_pullback
end
57 changes: 57 additions & 0 deletions test/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
@testset "getindex" begin
@testset "getindex(::Matrix{<:Number},...)" begin
x = [1.0 2.0 3.0; 10.0 20.0 30.0]
x̄ = [1.4 2.5 3.7; 10.5 20.1 30.2]
full_ȳ = [7.4 5.5 2.7; 8.5 11.1 4.2]

@testset "single element" begin
rrule_test(getindex, 2.3, (x, x̄), (2, nothing))
rrule_test(getindex, 2.3, (x, x̄), (2, nothing), (1, nothing))
rrule_test(getindex, 2.3, (x, x̄), (2, nothing), (2, nothing))

rrule_test(getindex, 2.3, (x, x̄), (CartesianIndex(2, 3), nothing))
end

@testset "slice/index postions" begin
rrule_test(getindex, [2.3, 3.1], (x, x̄), (2:3, nothing))
rrule_test(getindex, [2.3, 3.1], (x, x̄), (3:-1:2, nothing))
rrule_test(getindex, [2.3, 3.1], (x, x̄), ([3,2], nothing))
rrule_test(getindex, [2.3, 3.1], (x, x̄), ([2,3], nothing))

rrule_test(getindex, [2.3 3.1; 4.1 5.1], (x, x̄), (1:2, nothing), (2:3, nothing))
rrule_test(getindex, [2.3 3.1; 4.1 5.1], (x, x̄), (:, nothing), (2:3, nothing))

rrule_test(getindex, [2.3, 3.1], (x, x̄), (1:2, nothing), (1, nothing))
rrule_test(getindex, [2.3, 3.1], (x, x̄), (1, nothing), (1:2, nothing))

rrule_test(getindex, [2.3 3.1; 4.1 5.1], (x, x̄), (1:2, nothing), (2:3, nothing))
rrule_test(getindex, [2.3 3.1; 4.1 5.1], (x, x̄), (:, nothing), (2:3, nothing))


rrule_test(getindex, full_ȳ, (x, x̄), (:, nothing), (:, nothing))
rrule_test(getindex, full_ȳ[:], (x, x̄), (:, nothing))
end

@testset "masking" begin
rrule_test(getindex, full_ȳ, (x, x̄), (trues(size(x)), nothing))
rrule_test(getindex, full_ȳ[:], (x, x̄), (trues(length(x)), nothing))

mask = falses(size(x))
mask[2,3] = true
mask[1,2] = true
rrule_test(getindex, [2.3, 3.1], (x, x̄), (mask, nothing))

rrule_test(
getindex, full_ȳ[1,:], (x, x̄), ([true, false], nothing), (:, nothing)
)
end

@testset "By position with repeated elements" begin
rrule_test(getindex, [2.3, 3.1], (x, x̄), ([2, 2], nothing))
rrule_test(getindex, [2.3, 3.1, 4.1], (x, x̄), ([2, 2, 2], nothing))
rrule_test(
getindex, [2.3 3.1; 4.1 5.1], (x, x̄), ([2,2], nothing), ([3,3], nothing)
)
end
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ println("Testing ChainRules.jl")
include(joinpath("rulesets", "Base", "evalpoly.jl"))
include(joinpath("rulesets", "Base", "array.jl"))
include(joinpath("rulesets", "Base", "arraymath.jl"))
include(joinpath("rulesets", "Base", "indexing.jl"))
include(joinpath("rulesets", "Base", "mapreduce.jl"))
end

Expand Down