Skip to content

Commit

Permalink
Fix #116, #121: Sparse values and indices should be Vector{Any}
Browse files Browse the repository at this point in the history
  • Loading branch information
denizyuret committed Jul 24, 2020
1 parent 2230d03 commit 3c78a1e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
AutoGrad v1.2.3 Release Notes
=============================

* Fix #116, #121: Sparse values and indices should be Vector{Any}


AutoGrad v1.2.2 Release Notes
=============================
2230d03 2020-05-20

* Support for bessel derivatives.

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AutoGrad"
uuid = "6710c13c-97f1-543f-91c5-74e8f7d95b35"
authors = ["Deniz Yuret <[email protected]>"]
version = "1.2.2"
version = "1.2.3"

[deps]
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Expand Down
1 change: 1 addition & 0 deletions src/AutoGrad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module AutoGrad
export Param, params, grad, value, @diff
export gradloss, getval
export @primitive, @zerograd, @primitive1, @zerograd1, @primitive2, @zerograd2
export @gcheck

# Set ENV["AUTOGRAD_TIMER"]="true" and Pkg.build("AutoGrad") if you want profiling information in AutoGrad.to
using TimerOutputs, Libdl
Expand Down
4 changes: 2 additions & 2 deletions src/getindex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ back(::typeof(view),::Type{Arg{N}},o...) where {N} = nothing # Only the first ar
# efficiency when x is an array.
# x -> getindex -> xi -> grad -> dxi -> ungetindex -> dx -> grad -> ddx -> getindex -> ddxi

# ungetindex(x,dxi,i)=Sparse(x,[dxi],[i])
# ungetindex(x,dxi,i)=Sparse(x,Any[dxi],Any[i])

# For Object arrays, Dict, Tuple, Number no need to use Sparse:

Expand All @@ -60,7 +60,7 @@ function ungetindex(x::AbstractArray{T},dxi,i) where T
elseif recording()
addtoindex!(zero(x), dxi, i...)
else
Sparse(x,[dxi],[i])
Sparse(x,Any[dxi],Any[i])
end
else
# Using addtoindex! instead of setindex! to handle repeated indices
Expand Down
24 changes: 12 additions & 12 deletions src/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ than being overwritten. See https://github.com/JuliaLang/julia/issues/31392.
"""
struct Sparse{T,N} <: AbstractArray{T,N}
container
values
indices
values::Vector{Any}
indices::Vector{Any}
end

Sparse(a::AbstractArray{T,N},v,i) where {T,N} = Sparse{T,N}(a,v,i)
Sparse(a::AbstractArray{T,N},v::Vector{Any},i::Vector{Any}) where {T,N} = Sparse{T,N}(a,v,i)

# To add a Sparse to an Array without allocating extra space, we need to use:
# a .+= s OR a .= a .+ s
Expand Down Expand Up @@ -48,7 +48,7 @@ end
# These are used in Knet/src/update.jl:
import LinearAlgebra: axpy!, norm, lmul!
axpy!(a::Number, x::Sparse, y::AbstractArray) = addto!(y, a*x)
lmul!(a::Number, x::Sparse{T,N}) where {T,N} = Sparse{T,N}(x.container, [ a*v for v in x.values ], x.indices)
lmul!(a::Number, x::Sparse{T,N}) where {T,N} = Sparse{T,N}(x.container, Any[ a*v for v in x.values ], x.indices)

# This does not give the correct result when there are repeated indices, but should be good enough for gclip
norm(x::Sparse) = sqrt(sum(abs2, norm(v) for v in x.values))
Expand All @@ -62,12 +62,12 @@ full(x)=x
# Arithmetic with numbers
import Base: *, +, -, /
import Base.Broadcast: broadcasted
*(s::Sparse, n::Number) = Sparse(s.container, [ v*n for v in s.values ], s.indices)
*(n::Number, s::Sparse) = Sparse(s.container, [ v*n for v in s.values ], s.indices)
/(s::Sparse, n::Number) = Sparse(s.container, [ v/n for v in s.values ], s.indices)
broadcasted(::typeof(*), s::Sparse, n::Number) = Sparse(s.container, [ v.*n for v in s.values ], s.indices)
broadcasted(::typeof(*), n::Number, s::Sparse) = Sparse(s.container, [ v.*n for v in s.values ], s.indices)
broadcasted(::typeof(/), s::Sparse, n::Number) = Sparse(s.container, [ v./n for v in s.values ], s.indices)
*(s::Sparse, n::Number) = Sparse(s.container, Any[ v*n for v in s.values ], s.indices)
*(n::Number, s::Sparse) = Sparse(s.container, Any[ v*n for v in s.values ], s.indices)
/(s::Sparse, n::Number) = Sparse(s.container, Any[ v/n for v in s.values ], s.indices)
broadcasted(::typeof(*), s::Sparse, n::Number) = Sparse(s.container, Any[ v.*n for v in s.values ], s.indices)
broadcasted(::typeof(*), n::Number, s::Sparse) = Sparse(s.container, Any[ v.*n for v in s.values ], s.indices)
broadcasted(::typeof(/), s::Sparse, n::Number) = Sparse(s.container, Any[ v./n for v in s.values ], s.indices)

# Arithmetic with arrays (can use addto! which overwrites its first argument)
+(a::AbstractArray, s::Sparse) = addto!(copy(a), s)
Expand All @@ -79,12 +79,12 @@ broadcasted(::typeof(/), s::Sparse, n::Number) = Sparse(s.container, [ v./n for
# Issue #114: we may need to add multiple gradients
function +(a::Sparse, b::Sparse)
@assert matches(a.container, b.container) "$(summary.((a.container, b.container)))"
Sparse(a.container, [ a.values; b.values ], [ a.indices; b.indices ])
Sparse(a.container, Any[ a.values; b.values ], Any[ a.indices; b.indices ])
end

# Do we need these?
# sum(b::Sparse)=sum(sum(v) for v in b.values)
# zero(b::Sparse)=Sparse(b.container,[],[])
# zero(b::Sparse)=Sparse(b.container,Any[],Any[])
# ones(b::Sparse)=ones(b.container)
# length(b::Sparse)=length(b.container)

Expand Down
13 changes: 11 additions & 2 deletions test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include("header.jl")

# Issue #114 (a): plus for Sparse.
using AutoGrad: Sparse, full, addto!
a = Sparse(zeros(3,4), [ [1.,1.], [1.,1.], 1., 1. ], [ ([1,2],), (3:4,), (2,2), (1,) ])
a = Sparse(zeros(3,4), Any[ [1.,1.], [1.,1.], 1., 1. ], Any[ ([1,2],), (3:4,), (2,2), (1,) ])
b = a + a
@test b isa Sparse
@test full(b) == full(a) + full(a)
Expand All @@ -21,5 +21,14 @@ include("header.jl")
w = Param(randn(2,2))
J = @diff foo(w)
@test lmul!(0.5, grad(J,w)) isa Sparse


# Issue #116: Sparse values and indices should be Vector{Any}
foo(w) = sum(w[:,[1,2]]) + sum(w[:,[1 2]])
w = Param(randn(2,2))
@test @gcheck foo(w)

# Issue #121: Sparse values and indices should be Vector{Any}
w,x,y = Param(randn(7)), randn(10), randn(10)
pred(w,x)=w[5:6]'*tanh.(w[1:2]*x' .+ w[3:4]) .+ w[7]
@test @gcheck mean(abs2,y-pred(w,x)[1,:])
end

2 comments on commit 3c78a1e

@denizyuret
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/18408

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.2.3 -m "<description of version>" 3c78a1ebafa4ebd4222805accd924750415c96c5
git push origin v1.2.3

Please sign in to comment.