|
| 1 | +module EnzymeSparseArraysExt |
| 2 | + |
| 3 | +using LinearAlgebra: LinearAlgebra |
| 4 | +using SparseArrays: SparseArrays |
| 5 | +using Enzyme |
| 6 | +using EnzymeCore: EnzymeRules |
| 7 | + |
| 8 | +@inline Enzyme.Compiler.ptreltype(::Type{SparseArrays.CHOLMOD.Dense{T}}) where {T} = T |
| 9 | +@inline Enzyme.Compiler.is_arrayorvararg_ty(::Type{SparseArrays.CHOLMOD.Dense{T}}) where {T} = true |
| 10 | + |
| 11 | +Enzyme.Compiler.isa_cholmod_struct(::Core.Type{<:SparseArrays.LibSuiteSparse.cholmod_dense_struct}) = true |
| 12 | +Enzyme.Compiler.isa_cholmod_struct(::Core.Type{<:SparseArrays.LibSuiteSparse.cholmod_sparse_struct}) = true |
| 13 | +Enzyme.Compiler.isa_cholmod_struct(::Core.Type{<:SparseArrays.LibSuiteSparse.cholmod_factor_struct}) = true |
| 14 | + |
| 15 | +function EnzymeRules.augmented_primal( |
| 16 | + config::EnzymeRules.RevConfig, |
| 17 | + func::Const{typeof(LinearAlgebra.mul!)}, |
| 18 | + ::Type{RT}, |
| 19 | + C::Annotation{<:StridedVecOrMat}, |
| 20 | + A::Annotation{<:SparseArrays.SparseMatrixCSCUnion}, |
| 21 | + B::Annotation{<:StridedVecOrMat}, |
| 22 | + α::Annotation{<:Number}, |
| 23 | + β::Annotation{<:Number} |
| 24 | + ) where {RT} |
| 25 | + |
| 26 | + cache_C = !(isa(β, Const)) ? copy(C.val) : nothing |
| 27 | + # Always need to do forward pass otherwise primal may not be correct |
| 28 | + func.val(C.val, A.val, B.val, α.val, β.val) |
| 29 | + |
| 30 | + primal = if EnzymeRules.needs_primal(config) |
| 31 | + C.val |
| 32 | + else |
| 33 | + nothing |
| 34 | + end |
| 35 | + |
| 36 | + shadow = if EnzymeRules.needs_shadow(config) |
| 37 | + C.dval |
| 38 | + else |
| 39 | + nothing |
| 40 | + end |
| 41 | + |
| 42 | + |
| 43 | + # Check if A is overwritten and B is active (and thus required) |
| 44 | + cache_A = ( |
| 45 | + EnzymeRules.overwritten(config)[5] |
| 46 | + && !(typeof(B) <: Const) |
| 47 | + && !(typeof(C) <: Const) |
| 48 | + ) ? copy(A.val) : nothing |
| 49 | + |
| 50 | + cache_B = ( |
| 51 | + EnzymeRules.overwritten(config)[6] |
| 52 | + && !(typeof(A) <: Const) |
| 53 | + && !(typeof(C) <: Const) |
| 54 | + ) ? copy(B.val) : nothing |
| 55 | + |
| 56 | + if !isa(α, Const) |
| 57 | + cache_α = A.val * B.val |
| 58 | + else |
| 59 | + cache_α = nothing |
| 60 | + end |
| 61 | + |
| 62 | + cache = (cache_C, cache_A, cache_B, cache_α) |
| 63 | + |
| 64 | + return EnzymeRules.AugmentedReturn(primal, shadow, cache) |
| 65 | +end |
| 66 | + |
| 67 | +function EnzymeRules.reverse( |
| 68 | + config::EnzymeRules.RevConfig, |
| 69 | + func::Const{typeof(LinearAlgebra.mul!)}, |
| 70 | + ::Type{RT}, cache, |
| 71 | + C::Annotation{<:StridedVecOrMat}, |
| 72 | + A::Annotation{<:SparseArrays.SparseMatrixCSCUnion}, |
| 73 | + B::Annotation{<:StridedVecOrMat}, |
| 74 | + α::Annotation{<:Number}, |
| 75 | + β::Annotation{<:Number} |
| 76 | + ) where {RT} |
| 77 | + |
| 78 | + cache_C, cache_A, cache_B, cache_α = cache |
| 79 | + Cval = !isnothing(cache_C) ? cache_C : C.val |
| 80 | + Aval = !isnothing(cache_A) ? cache_A : A.val |
| 81 | + Bval = !isnothing(cache_B) ? cache_B : B.val |
| 82 | + |
| 83 | + N = EnzymeRules.width(config) |
| 84 | + if !isa(C, Const) |
| 85 | + dCs = C.dval |
| 86 | + dBs = isa(B, Const) ? dCs : B.dval |
| 87 | + dα = if !isa(α, Const) |
| 88 | + if N == 1 |
| 89 | + Enzyme._project(typeof(α.val), conj(LinearAlgebra.dot(C.dval, cache_α))) |
| 90 | + else |
| 91 | + ntuple(Val(N)) do i |
| 92 | + Base.@_inline_meta |
| 93 | + Enzyme._project(typeof(α.val), conj(LinearAlgebra.dot(C.dval[i], cache_α))) |
| 94 | + end |
| 95 | + end |
| 96 | + else |
| 97 | + nothing |
| 98 | + end |
| 99 | + |
| 100 | + dβ = if !isa(β, Const) |
| 101 | + if N == 1 |
| 102 | + Enzyme._project(typeof(β.val), conj(LinearAlgebra.dot(C.dval, Cval))) |
| 103 | + else |
| 104 | + ntuple(Val(N)) do i |
| 105 | + Base.@_inline_meta |
| 106 | + Enzyme._project(typeof(β.val), conj(LinearAlgebra.dot(C.dval[i], Cval))) |
| 107 | + end |
| 108 | + end |
| 109 | + else |
| 110 | + nothing |
| 111 | + end |
| 112 | + |
| 113 | + for i in 1:N |
| 114 | + if !isa(A, Const) |
| 115 | + # dA .+= α'dC*B' |
| 116 | + # You need to be careful so that dA sparsity pattern does not change. Otherwise |
| 117 | + # you will get incorrect gradients. So for now we do the slow and bad way of accumulating |
| 118 | + dA = EnzymeRules.width(config) == 1 ? A.dval : A.dval[i] |
| 119 | + dC = EnzymeRules.width(config) == 1 ? C.dval : C.dval[i] |
| 120 | + # Now accumulate to preserve the correct sparsity pattern |
| 121 | + I, J, _ = SparseArrays.findnz(dA) |
| 122 | + for k in eachindex(I, J) |
| 123 | + Ik, Jk = I[k], J[k] |
| 124 | + # May need to widen if the eltype differ |
| 125 | + tmp = zero(promote_type(eltype(dA), eltype(dC))) |
| 126 | + for ti in axes(dC, 2) |
| 127 | + tmp += dC[Ik, ti] * conj(Bval[Jk, ti]) |
| 128 | + end |
| 129 | + dA[Ik, Jk] += Enzyme._project(eltype(dA), conj(α.val) * tmp) |
| 130 | + end |
| 131 | + # mul!(dA, dCs, Bval', α.val, true) |
| 132 | + end |
| 133 | + |
| 134 | + if !isa(B, Const) |
| 135 | + #dB .+= α*A'*dC |
| 136 | + # Get the type of all arguments since we may need to |
| 137 | + # project down to a smaller type during accumulation |
| 138 | + if N == 1 |
| 139 | + Targs = promote_type(eltype(Aval), eltype(dCs), typeof(α.val)) |
| 140 | + Enzyme._muladdproject!(Targs, dBs, Aval', dCs, conj(α.val)) |
| 141 | + else |
| 142 | + Targs = promote_type(eltype(Aval[i]), eltype(dCs[i]), typeof(α.val)) |
| 143 | + Enzyme._muladdproject!(Targs, dBs[i], Aval', dCs[i], conj(α.val)) |
| 144 | + end |
| 145 | + end |
| 146 | + #dC = dC*conj(β.val) |
| 147 | + if N == 1 |
| 148 | + dCs .*= Enzyme._project(eltype(dCs), conj(β.val)) |
| 149 | + else |
| 150 | + dCs[i] .*= Enzyme._project(eltype(dCs[i]), conj(β.val)) |
| 151 | + end |
| 152 | + end |
| 153 | + else |
| 154 | + # C is constant so there is no gradient information to compute |
| 155 | + |
| 156 | + dα = if !isa(α, Const) |
| 157 | + if N == 1 |
| 158 | + zero(α.val) |
| 159 | + else |
| 160 | + ntuple(Returns(zero(α.val)), Val(N)) |
| 161 | + end |
| 162 | + else |
| 163 | + nothing |
| 164 | + end |
| 165 | + |
| 166 | + |
| 167 | + dβ = if !isa(β, Const) |
| 168 | + if N == 1 |
| 169 | + zero(β.val) |
| 170 | + else |
| 171 | + ntuple(Returns(zero(β.val)), Val(N)) |
| 172 | + end |
| 173 | + else |
| 174 | + nothing |
| 175 | + end |
| 176 | + end |
| 177 | + |
| 178 | + return (nothing, nothing, nothing, dα, dβ) |
| 179 | +end |
| 180 | + |
| 181 | +end |
0 commit comments