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
7 changes: 7 additions & 0 deletions src/lib/lib.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Base: RefValue
using Base: ismutabletype
using LinearAlgebra: Hermitian, Symmetric
using SparseArrays: SparseMatrixCSC

# Interfaces

Expand All @@ -15,6 +17,11 @@ accum(x, y, zs...) = accum(accum(x, y), zs...)

accum(x::Tuple, ys::Tuple...) = map(accum, x, ys...)
accum(x::AbstractArray, ys::AbstractArray...) = Base.broadcast_preserving_zero_d(accum, x, ys...)

const HermOrSymSparse{T, I} = Union{Hermitian{T, SparseMatrixCSC{T, I}}, Symmetric{T, SparseMatrixCSC{T, I}}}

accum(x::HermOrSymSparse, y::HermOrSymSparse) = x + y

accum(::Tuple{}, ::NamedTuple{}) = ()
accum(::NamedTuple{}, ::Tuple{}) = ()

Expand Down
12 changes: 12 additions & 0 deletions test/lib/lib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,17 @@
@test Zygote.accum(t1, t2) == (a = 2, b = 4, c = 3)
@test_throws ArgumentError Zygote.accum(t2, t1)
@test Zygote.accum(fill(0.0), fill(0.0)) == fill(0.0)

# HermOrSymSparse accumulation
S = sparse([1, 2, 2], [1, 1, 2], [1.0, 2.0, 3.0], 2, 2)
H1 = Hermitian(S + S')
H2 = Hermitian(2S + 2S')
@test Zygote.accum(H1, H2) == H1 + H2
@test Zygote.accum(H1, H2) isa Hermitian{Float64, <:SparseMatrixCSC}

Sym1 = Symmetric(S + S')
Sym2 = Symmetric(2S + 2S')
@test Zygote.accum(Sym1, Sym2) == Sym1 + Sym2
@test Zygote.accum(Sym1, Sym2) isa Symmetric{Float64, <:SparseMatrixCSC}
end
end
1 change: 1 addition & 0 deletions test/lib_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ChainRulesTestUtils
using LinearAlgebra: Diagonal, Hermitian, LowerTriangular, UpperTriangular, Symmetric
using LinearAlgebra: UnitLowerTriangular, UnitUpperTriangular
using SparseArrays: sparse, SparseMatrixCSC
using Zygote: ZygoteRuleConfig, _pullback, _reverse

include("lib/number.jl")
Expand Down
Loading