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

Add basic sparse handling #246

Closed
wants to merge 12 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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
Expand Down
3 changes: 3 additions & 0 deletions src/ChainRules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using LinearAlgebra.BLAS
using Random
using Requires
using Statistics
using SparseArrays

# Basically everything this package does is overloading these, so we make an exception
# to the normal rule of only overload via `ChainRulesCore.rrule`.
Expand Down Expand Up @@ -37,6 +38,8 @@ include("rulesets/LinearAlgebra/dense.jl")
include("rulesets/LinearAlgebra/structured.jl")
include("rulesets/LinearAlgebra/factorization.jl")

include("rulesets/SparseArrays/sparsematrix.jl")

include("rulesets/Random/random.jl")

# Note: The following is only required because package authors sometimes do not
Expand Down
10 changes: 10 additions & 0 deletions src/rulesets/LinearAlgebra/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,13 @@ function rrule(::typeof(norm), x::Real, p::Real=2)
end
return norm(x, p), norm_pullback
end

function rrule(::typeof(diagm), x::AbstractVector)
diagm_pullback(∂x) = (NO_FIELDS, diag(∂x),)
return diagm(x), diagm_pullback
end

function rrule(::typeof(issymmetric), x)
issymmetric_pullback(∂x) = (NO_FIELDS, ∂x)
return issymmetric(x), issymmetric_pullback
end
15 changes: 15 additions & 0 deletions src/rulesets/SparseArrays/sparsematrix.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using SparseArrays
Copy link
Member

Choose a reason for hiding this comment

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

Not a full review, but just so it doesn't get missed, it is the pattern of this package, inline with BlueStyle,
to do all the usings at the top level file


function rrule(::Type{<:SparseMatrixCSC{T,N}}, arr) where {T,N}
function SparseMatrix_pullback(Δ)
return NO_FIELDS, collect(Δ)
end
return SparseMatrixCSC{T,N}(arr), SparseMatrix_pullback
end

function rrule(::typeof(Matrix), x::SparseMatrixCSC)
function Matrix_pullback(Δ)
NO_FIELDS, Δ
end
return Matrix(x), Matrix_pullback
end
8 changes: 8 additions & 0 deletions test/rulesets/SparseArrays/sparsematrix.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using SparseArrays

@testset "Sparse" begin
r = sparse(rand(3,3))
x, x̄ = rand(3,3), rand(3,3)
test_rrule(SparseMatrixCSC, r)
test_rrule(Matrix, r)
end
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using LinearAlgebra.BLAS
using LinearAlgebra: dot
using Random
using Statistics
using SparseArrays
using Test

Random.seed!(1) # Set seed that all testsets should reset to.
Expand Down Expand Up @@ -48,6 +49,12 @@ println("Testing ChainRules.jl")

print(" ")

@testset "SparseArrays" begin
include(joinpath("rulesets", "SparseArrays", "sparsematrix.jl"))
end

print(" ")

@testset "packages" begin
include(joinpath("rulesets", "packages", "NaNMath.jl"))
include(joinpath("rulesets", "packages", "SpecialFunctions.jl"))
Expand Down