-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add sparse(I, J, V, m, n, +) rrule * cleanup * fix test * sparse(A) and sparse(v) * SparseMatrixCSC and SparseVector * cleanup Co-authored-by: Michael Abbott <[email protected]>
- Loading branch information
1 parent
970fce4
commit 8108a77
Showing
5 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), Ω̄ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8108a77
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
8108a77
There was a problem hiding this comment.
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/53561
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: