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 5 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/array.jl")

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

# Note: The following is only required because package authors sometimes do not
Expand Down
16 changes: 16 additions & 0 deletions src/rulesets/SparseArrays/array.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using SparseArrays

function rrule(::Type{<:SparseMatrixCSC{T,N}}, arr) where {T,N}
DhairyaLGandhi marked this conversation as resolved.
Show resolved Hide resolved
function SparseMatrix_pullback(Δ)
Copy link
Contributor

Choose a reason for hiding this comment

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

please can you use 4-space indenting throughout, as per the style guide? https://github.com/invenia/BlueStyle#synopsis :)

(collect(Δ),)
DhairyaLGandhi marked this conversation as resolved.
Show resolved Hide resolved
end
SparseMatrixCSC{T,N}(arr), SparseMatrix_pullback
DhairyaLGandhi marked this conversation as resolved.
Show resolved Hide resolved
end

function rrlue(::typeof(diagm), x)
diagm(x), d -> (diag(d),)
end
DhairyaLGandhi marked this conversation as resolved.
Show resolved Hide resolved

function rrlue(::typeof(issymmetric), x)
issymmetric(x), d -> (d,)
end
DhairyaLGandhi marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions test/rulesets/SparseArrays/array.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using SparseArrays

@testset "Sparse" begin
x = rand(3,3)
rrule_test(sparse, x)
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", "array.jl"))
end

print(" ")

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