Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions src/rulesets/SparseArrays/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function rrule(::typeof(findnz), A::AbstractSparseMatrix)

function findnz_pullback(Δ)
_, _, V̄ = unthunk(Δ)
V̄ = unthunk(V̄)
V̄ isa AbstractZero && return (NoTangent(), V̄)
return NoTangent(), sparse(I, J, V̄, m, n)
end
Expand All @@ -43,6 +44,7 @@ function rrule(::typeof(findnz), v::AbstractSparseVector)

function findnz_pullback(Δ)
_, V̄ = unthunk(Δ)
V̄ = unthunk(V̄)
V̄ isa AbstractZero && return (NoTangent(), V̄)
return NoTangent(), sparsevec(I, V̄, n)
end
Expand Down
8 changes: 8 additions & 0 deletions test/rulesets/SparseArrays/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ end
I, V = findnz(v)
V̄ = rand!(similar(V))
test_rrule(findnz, v ⊢ dv, output_tangent=(zeros(length(I)), V̄))

# cotangent components may be thunks
IA, JA, VA = findnz(A)
V̄A = rand!(similar(VA))
_, pb = rrule(findnz, A)
@test pb((ZeroTangent(), ZeroTangent(), @thunk(V̄A)))[2] == sparse(IA, JA, V̄A, 5, 5)
_, pb = rrule(findnz, v)
@test pb((ZeroTangent(), @thunk(V̄)))[2] == sparsevec(I, V̄, 5)
end

if Base.USE_GPL_LIBS # these rrules don't work without CHOLMOD from SuiteSparse.jl
Expand Down
Loading