-
Notifications
You must be signed in to change notification settings - Fork 89
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
add sparse
rrule
#579
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8415583
add sparse(I, J, V, m, n, +) rrule
CarloLucibello 3b289b6
cleanup
CarloLucibello 184d5d0
fix test
CarloLucibello 39f2907
sparse(A) and sparse(v)
CarloLucibello c90ed2a
SparseMatrixCSC and SparseVector
CarloLucibello 9d7cbad
cleanup
CarloLucibello 0d22898
Merge branch 'main' into main
mcabbott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
is this ok or we need to project or something?