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 sparse rrule #579

Merged
merged 7 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRules"
uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2"
version = "1.23"
version = "1.24"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand All @@ -9,6 +9,7 @@ IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RealDot = "c1ae055f-0cd5-4b69-90a6-9a35b1a98df9"
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 @@ -8,6 +8,7 @@ using LinearAlgebra
using LinearAlgebra.BLAS
using Random
using RealDot: realdot
using SparseArrays
using Statistics

# Basically everything this package does is overloading these, so we make an exception
Expand Down Expand Up @@ -42,6 +43,8 @@ include("rulesets/LinearAlgebra/structured.jl")
include("rulesets/LinearAlgebra/symmetric.jl")
include("rulesets/LinearAlgebra/factorization.jl")

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

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

end # module
25 changes: 25 additions & 0 deletions src/rulesets/SparseArrays/sparsematrix.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function rrule(::typeof(sparse), I::AbstractVector, J::AbstractVector, V::AbstractVector, m, n, combine::typeof(+))
project_V = ProjectTo(V)

function sparse_pullback(Ω̄)
ΔΩ = unthunk(Ω̄)
ΔV = project_V(ΔΩ[I .+ m .* (J .- 1)])
return NoTangent(), NoTangent(), NoTangent(), ΔV, NoTangent(), NoTangent(), NoTangent()
end

return sparse(I, J, V, m, n, combine), sparse_pullback
end

function rrule(::Type{T}, A::AbstractMatrix) where T <: SparseMatrixCSC
function sparse_pullback(Ω̄)
return NoTangent(), Ω̄
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is this ok or we need to project or something?

end
return T(A), sparse_pullback
end

function rrule(::Type{T}, v::AbstractVector) where T <: SparseVector
function sparse_pullback(Ω̄)
return NoTangent(), Ω̄
end
return T(v), sparse_pullback
end
19 changes: 19 additions & 0 deletions test/rulesets/SparseArrays/sparsematrix.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

@testset "sparse(I, J, V, m, n, +)" begin
m, n = 3, 5
s, t, w = [1,2], [2,3], [0.5,0.5]

test_rrule(sparse, s, t, w, m, n, +)
end

@testset "SparseMatrixCSC(A)" begin
A = rand(5, 3)
test_rrule(SparseMatrixCSC, A)
test_rrule(SparseMatrixCSC{Float32,Int}, A, rtol=1e-5)
end

@testset "SparseVector(v)" begin
v = rand(5)
test_rrule(SparseVector, v)
test_rrule(SparseVector{Float32}, Float32.(v), rtol=1e-5)
end
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using LinearAlgebra
using LinearAlgebra.BLAS
using LinearAlgebra: dot
using Random
using SparseArrays
using StaticArrays
using Statistics
using Test
Expand Down Expand Up @@ -72,6 +73,10 @@ end

println()

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

println()

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

println()
Expand Down