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

Sparse grads for getindex #589

Closed
wants to merge 2 commits into from
Closed
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
42 changes: 41 additions & 1 deletion src/tracker/back.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,54 @@ back_(::Call{Missing}, Δ, once) = error("`back!` was already used")

accum!(x, Δ) = x .+ Δ
accum!(x::AbstractArray, Δ) = (x .+= Δ)
struct SparseGrad{T,N,S,P,O} <: AbstractArray{T,N} where O <: AbstractArray{T,N}
Δ::P
i::S
size::NTuple{N,Int}
function SparseGrad(Δ::P, i::S, size::NTuple{N,Int}, x::AbstractArray{T,N}) where {T,N,S,P}
new{T,N,S,P,typeof(x)}(Δ, i, Base.size(x))
end
end
accum!(x::AbstractArray, Δ::SparseGrad) = (@inbounds(x[Δ.i...] += Δ.Δ); return x)
Base.size(x::SparseGrad) = x.size
Base.similar(x::SparseGrad{T,N,S,P,O}) where {T,N,S,P,O} = similar(O, size(x))

#FIXME: Very slow getindex.
function Base.getindex(x::SparseGrad, i...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to just implement the scalar version and have the rest fall back? Or is this need for the GPU?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly, the scalar case is worth it only for a very small queries (the indexin()s are expensive), hence I added the allocating version for the general case.

Base.checkbounds_indices(Bool, map(Base.OneTo, size(x)), i) || throw(BoundsError(x, i))

out = zero(x)
@inbounds out[x.i...] = x.Δ
@inbounds out[i...]
end
function Base.getindex(x::SparseGrad{T,N,S,P,O}, i::Int...)::T where {T,N,S,P,O}
Base.checkbounds_indices(Bool, map(Base.OneTo, size(x)), i) || throw(BoundsError(x, i))

li = LinearIndices(size(x))
@inbounds nonempty = li[x.i...]
@inbounds queryindices = li[i...]

outidx = indexin(queryindices, nonempty)[1]
isnothing(outidx) ? zero(T) : @inbounds x.Δ[outidx]::T
end
function Base.getindex(x::SparseGrad{T,N,S,P,O}, i::Int...)::T where {T,N,O,S<:NTuple{N,Int},P<:T}
Base.checkbounds_indices(Bool, map(Base.OneTo, size(x)), i) || throw(BoundsError(x, i))
x.i == i ? x.Δ : zero(T)
end


function back(x::Tracked, Δ, once)
x.isleaf && (x.grad = accum!(x.grad, Δ); return)
ref = x.ref -= 1
grad = if isdefined(x, :grad)
x.grad = accum!(x.grad, Δ)
elseif ref > 0
x.grad = Δ
if Δ isa SparseGrad
x.grad = zero(Δ)
@inbounds x.grad[Δ.i...] = Δ.Δ
else
x.grad = Δ
end
else
Δ
end
Expand Down
5 changes: 2 additions & 3 deletions src/tracker/lib/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ Base.getindex(xs::TrackedArray, i...) = track(getindex, xs, i...)

@grad function getindex(xs::AbstractArray, i...)
data(xs)[i...], function (Δ)
Δ′ = zero(xs)
Δ′[i...] = data(Δ)
(nobacksies(:getindex, Δ′), map(_->nothing, i)...)
checkbounds(xs, i...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed given that we already did xs[i...]?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch:)

(nobacksies(:getindex, SparseGrad(data(Δ), i, size(data(xs)), data(xs))), map(_->nothing, i)...)
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/tracker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ gradtest(f, dims...) = gradtest(f, rand.(Float64, dims)...)

@testset "indexing & slicing" begin
gradtest(x->view(x, 1:2, 1:2), rand(4, 4))
gradtest(x->getindex(x, 2, :, 3:4, [3,1]), rand(4, 4, 4, 4))
gradtest(x->getindex(x, 1, 2, 3, 4), rand(4, 4, 4, 4))
end

function promotiontest(f, A, B, C)
Expand Down