Skip to content

Commit

Permalink
Merge pull request #590 from CarloLucibello/cl/findnz2
Browse files Browse the repository at this point in the history
rrule for findnz
  • Loading branch information
mzgubic committed Feb 18, 2022
2 parents aab91c6 + 5d5741c commit b1daa7a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
@@ -1,6 +1,6 @@
name = "ChainRules"
uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2"
version = "1.26.1"
version = "1.27.0"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
30 changes: 28 additions & 2 deletions src/rulesets/SparseArrays/sparsematrix.jl
Expand Up @@ -10,16 +10,42 @@ function rrule(::typeof(sparse), I::AbstractVector, J::AbstractVector, V::Abstra
return sparse(I, J, V, m, n, combine), sparse_pullback
end

function rrule(::Type{T}, A::AbstractMatrix) where T <: SparseMatrixCSC
function rrule(::Type{T}, A::AbstractMatrix) where T <: AbstractSparseMatrix
function sparse_pullback(Ω̄)
return NoTangent(), Ω̄
end
return T(A), sparse_pullback
end

function rrule(::Type{T}, v::AbstractVector) where T <: SparseVector
function rrule(::Type{T}, v::AbstractVector) where T <: AbstractSparseVector
function sparse_pullback(Ω̄)
return NoTangent(), Ω̄
end
return T(v), sparse_pullback
end

function rrule(::typeof(findnz), A::AbstractSparseMatrix)
I, J, V = findnz(A)
m, n = size(A)

function findnz_pullback(Δ)
_, _, V̄ = unthunk(Δ)
isa AbstractZero && return (NoTangent(), V̄)
return NoTangent(), sparse(I, J, V̄, m, n)
end

return (I, J, V), findnz_pullback
end

function rrule(::typeof(findnz), v::AbstractSparseVector)
I, V = findnz(v)
n = length(v)

function findnz_pullback(Δ)
_, V̄ = unthunk(Δ)
isa AbstractZero && return (NoTangent(), V̄)
return NoTangent(), sparsevec(I, V̄, n)
end

return (I, V), findnz_pullback
end
20 changes: 18 additions & 2 deletions test/rulesets/SparseArrays/sparsematrix.jl
Expand Up @@ -9,11 +9,27 @@ end
@testset "SparseMatrixCSC(A)" begin
A = rand(5, 3)
test_rrule(SparseMatrixCSC, A)
test_rrule(SparseMatrixCSC{Float32,Int}, A, rtol=1e-5)
test_rrule(SparseMatrixCSC{Float32,Int}, A, rtol=1e-4)
end

@testset "SparseVector(v)" begin
v = rand(5)
test_rrule(SparseVector, v)
test_rrule(SparseVector{Float32}, Float32.(v), rtol=1e-5)
test_rrule(SparseVector{Float32}, Float32.(v), rtol=1e-4)
end

@testset "findnz" begin
A = sprand(5, 5, 0.5)
dA = similar(A)
rand!(dA.nzval)
I, J, V = findnz(A)
= rand!(similar(V))
test_rrule(findnz, A dA, output_tangent=(zeros(length(I)), zeros(length(J)), V̄))

v = sprand(5, 0.5)
dv = similar(v)
rand!(dv.nzval)
I, V = findnz(v)
= rand!(similar(V))
test_rrule(findnz, v dv, output_tangent=(zeros(length(I)), V̄))
end

2 comments on commit b1daa7a

@mzgubic
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

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/54889

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:

git tag -a v1.27.0 -m "<description of version>" b1daa7aa6849ed66d0004de73137207e6a2d007b
git push origin v1.27.0

Please sign in to comment.