|
| 1 | +module TensorOperationsEnzymeExt |
| 2 | + |
| 3 | +using TensorOperations |
| 4 | +using TensorOperations: AbstractBackend, DefaultAllocator, CUDAAllocator, ManualAllocator |
| 5 | +using VectorInterface |
| 6 | +using TupleTools |
| 7 | +using Enzyme, ChainRulesCore |
| 8 | +using Enzyme.EnzymeCore |
| 9 | +using Enzyme.EnzymeCore: EnzymeRules |
| 10 | + |
| 11 | +@inline EnzymeRules.inactive(::typeof(TensorOperations.tensorfree!), ::Any) = true |
| 12 | +Enzyme.@import_rrule(typeof(TensorOperations.tensoralloc), Any, Any, Any, Any) |
| 13 | + |
| 14 | +@inline EnzymeRules.inactive_type(v::Type{<:AbstractBackend}) = true |
| 15 | +@inline EnzymeRules.inactive_type(v::Type{DefaultAllocator}) = true |
| 16 | +@inline EnzymeRules.inactive_type(v::Type{<:CUDAAllocator}) = true |
| 17 | +@inline EnzymeRules.inactive_type(v::Type{ManualAllocator}) = true |
| 18 | +@inline EnzymeRules.inactive_type(v::Type{<:Index2Tuple}) = true |
| 19 | + |
| 20 | +function EnzymeRules.augmented_primal( |
| 21 | + config::EnzymeRules.RevConfigWidth{1}, |
| 22 | + func::Const{typeof(TensorOperations.tensorcontract!)}, |
| 23 | + ::Type{RT}, |
| 24 | + C_dC::Annotation{<:AbstractArray{TC}}, |
| 25 | + A_dA::Annotation{<:AbstractArray{TA}}, |
| 26 | + pA_dpA::Const{<:Index2Tuple}, |
| 27 | + conjA_dconjA::Const{Bool}, |
| 28 | + B_dB::Annotation{<:AbstractArray{TB}}, |
| 29 | + pB_dpB::Const{<:Index2Tuple}, |
| 30 | + conjB_dconjB::Const{Bool}, |
| 31 | + pAB_dpAB::Const{<:Index2Tuple}, |
| 32 | + α_dα::Annotation{Tα}, |
| 33 | + β_dβ::Annotation{Tβ}, |
| 34 | + ba_dba::Const..., |
| 35 | + ) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TB <: Number, TC <: Number} |
| 36 | + # form caches if needed |
| 37 | + cache_A = !isa(A_dA, Const) && EnzymeRules.overwritten(config)[3] ? copy(A_dA.val) : nothing |
| 38 | + cache_B = !isa(B_dB, Const) && EnzymeRules.overwritten(config)[6] ? copy(B_dB.val) : nothing |
| 39 | + cache_C = copy(C_dC.val) # do we need to do this, if we don't need the primal? |
| 40 | + ba = map(ba_ -> getfield(ba_, :val), ba_dba) |
| 41 | + TensorOperations.tensorcontract!(C_dC.val, A_dA.val, pA_dpA.val, conjA_dconjA.val, B_dB.val, pB_dpB.val, conjB_dconjB.val, pAB_dpAB.val, α_dα.val, β_dβ.val, ba...) |
| 42 | + primal = if EnzymeRules.needs_primal(config) |
| 43 | + C_dC.val |
| 44 | + else |
| 45 | + nothing |
| 46 | + end |
| 47 | + shadow = EnzymeRules.needs_shadow(config) ? C_dC.dval : nothing |
| 48 | + return EnzymeRules.AugmentedReturn(primal, shadow, (cache_A, cache_B, cache_C)) |
| 49 | +end |
| 50 | + |
| 51 | +function EnzymeRules.reverse( |
| 52 | + config::EnzymeRules.RevConfigWidth{1}, |
| 53 | + func::Const{typeof(TensorOperations.tensorcontract!)}, |
| 54 | + ::Type{RT}, |
| 55 | + cache, |
| 56 | + C_dC::Annotation{<:AbstractArray{TC}}, |
| 57 | + A_dA::Annotation{<:AbstractArray{TA}}, |
| 58 | + pA_dpA::Const{<:Index2Tuple}, |
| 59 | + conjA_dconjA::Const{Bool}, |
| 60 | + B_dB::Annotation{<:AbstractArray{TB}}, |
| 61 | + pB_dpB::Const{<:Index2Tuple}, |
| 62 | + conjB_dconjB::Const{Bool}, |
| 63 | + pAB_dpAB::Const{<:Index2Tuple}, |
| 64 | + α_dα::Annotation{Tα}, |
| 65 | + β_dβ::Annotation{Tβ}, |
| 66 | + ba_dba::Const..., |
| 67 | + ) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TB <: Number, TC <: Number} |
| 68 | + cache_A, cache_B, cache_C = cache |
| 69 | + Aval = something(cache_A, A_dA.val) |
| 70 | + Bval = something(cache_B, B_dB.val) |
| 71 | + Cval = cache_C |
| 72 | + dC = C_dC.dval |
| 73 | + dA = A_dA.dval |
| 74 | + dB = B_dB.dval |
| 75 | + ba = map(ba_ -> getfield(ba_, :val), ba_dba) |
| 76 | + α = α_dα.val |
| 77 | + β = β_dβ.val |
| 78 | + dC, dA, dB, dα, dβ = TensorOperations.tensorcontract_pullback!(dC, dA, dB, Cval, Aval, pA_dpA.val, conjA_dconjA.val, Bval, pB_dpB.val, conjB_dconjB.val, pAB_dpAB.val, α, β, ba...) |
| 79 | + return nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, dα, dβ, map(ba_ -> nothing, ba)... |
| 80 | +end |
| 81 | + |
| 82 | +function EnzymeRules.augmented_primal( |
| 83 | + config::EnzymeRules.RevConfigWidth{1}, |
| 84 | + ::Annotation{typeof(tensoradd!)}, |
| 85 | + ::Type{RT}, |
| 86 | + C_dC::Annotation{<:AbstractArray{TC}}, |
| 87 | + A_dA::Annotation{<:AbstractArray{TA}}, |
| 88 | + pA_dpA::Const{<:Index2Tuple}, |
| 89 | + conjA_dconjA::Const{Bool}, |
| 90 | + α_dα::Annotation{Tα}, |
| 91 | + β_dβ::Annotation{Tβ}, |
| 92 | + ba_dba::Const..., |
| 93 | + ) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number} |
| 94 | + # form caches if needed |
| 95 | + cache_A = !isa(A_dA, Const) && EnzymeRules.overwritten(config)[3] ? copy(A_dA.val) : nothing |
| 96 | + cache_C = copy(C_dC.val) |
| 97 | + ba = map(ba_ -> getfield(ba_, :val), ba_dba) |
| 98 | + α = α_dα.val |
| 99 | + β = β_dβ.val |
| 100 | + conjA = conjA_dconjA.val |
| 101 | + TensorOperations.tensoradd!(C_dC.val, A_dA.val, pA_dpA.val, conjA, α, β, ba...) |
| 102 | + primal = if EnzymeRules.needs_primal(config) |
| 103 | + C_dC.val |
| 104 | + else |
| 105 | + nothing |
| 106 | + end |
| 107 | + shadow = EnzymeRules.needs_shadow(config) ? C_dC.dval : nothing |
| 108 | + return EnzymeRules.AugmentedReturn(primal, shadow, (cache_A, cache_C)) |
| 109 | +end |
| 110 | + |
| 111 | +function EnzymeRules.reverse( |
| 112 | + config::EnzymeRules.RevConfigWidth{1}, |
| 113 | + ::Annotation{typeof(tensoradd!)}, |
| 114 | + ::Type{RT}, |
| 115 | + cache, |
| 116 | + C_dC::Annotation{<:AbstractArray{TC}}, |
| 117 | + A_dA::Annotation{<:AbstractArray{TA}}, |
| 118 | + pA_dpA::Const{<:Index2Tuple}, |
| 119 | + conjA_dconjA::Const{Bool}, |
| 120 | + α_dα::Annotation{Tα}, |
| 121 | + β_dβ::Annotation{Tβ}, |
| 122 | + ba_dba::Const..., |
| 123 | + ) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number} |
| 124 | + cache_A, cache_C = cache |
| 125 | + Aval = something(cache_A, A_dA.val) |
| 126 | + Cval = cache_C |
| 127 | + pA = pA_dpA.val |
| 128 | + conjA = conjA_dconjA.val |
| 129 | + α = α_dα.val |
| 130 | + β = β_dβ.val |
| 131 | + ba = map(ba_ -> getfield(ba_, :val), ba_dba) |
| 132 | + dC = C_dC.dval |
| 133 | + dA = A_dA.dval |
| 134 | + dC, dA, dα, dβ = TensorOperations.tensoradd_pullback!(dC, dA, Cval, Aval, pA, conjA, α, β, ba...) |
| 135 | + return nothing, nothing, nothing, nothing, dα, dβ, map(ba_ -> nothing, ba)... |
| 136 | +end |
| 137 | + |
| 138 | +function EnzymeRules.augmented_primal( |
| 139 | + config::EnzymeRules.RevConfigWidth{1}, |
| 140 | + ::Annotation{typeof(tensortrace!)}, |
| 141 | + ::Type{RT}, |
| 142 | + C_dC::Annotation{<:AbstractArray{TC}}, |
| 143 | + A_dA::Annotation{<:AbstractArray{TA}}, |
| 144 | + p_dp::Const{<:Index2Tuple}, |
| 145 | + q_dq::Const{<:Index2Tuple}, |
| 146 | + conjA_dconjA::Const{Bool}, |
| 147 | + α_dα::Annotation{Tα}, |
| 148 | + β_dβ::Annotation{Tβ}, |
| 149 | + ba_dba::Const..., |
| 150 | + ) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number} |
| 151 | + # form caches if needed |
| 152 | + cache_A = !isa(A_dA, Const) && EnzymeRules.overwritten(config)[3] ? copy(A_dA.val) : nothing |
| 153 | + cache_C = copy(C_dC.val) |
| 154 | + ba = map(ba_ -> getfield(ba_, :val), ba_dba) |
| 155 | + α = α_dα.val |
| 156 | + β = β_dβ.val |
| 157 | + conjA = conjA_dconjA.val |
| 158 | + TensorOperations.tensortrace!(C_dC.val, A_dA.val, p_dp.val, q_dq.val, conjA, α, β, ba...) |
| 159 | + primal = if EnzymeRules.needs_primal(config) |
| 160 | + C_dC.val |
| 161 | + else |
| 162 | + nothing |
| 163 | + end |
| 164 | + shadow = EnzymeRules.needs_shadow(config) ? C_dC.dval : nothing |
| 165 | + return EnzymeRules.AugmentedReturn(primal, shadow, (cache_A, cache_C)) |
| 166 | +end |
| 167 | + |
| 168 | +function EnzymeRules.reverse( |
| 169 | + config::EnzymeRules.RevConfigWidth{1}, |
| 170 | + ::Annotation{typeof(tensortrace!)}, |
| 171 | + ::Type{RT}, |
| 172 | + cache, |
| 173 | + C_dC::Annotation{<:AbstractArray{TC}}, |
| 174 | + A_dA::Annotation{<:AbstractArray{TA}}, |
| 175 | + p_dp::Const{<:Index2Tuple}, |
| 176 | + q_dq::Const{<:Index2Tuple}, |
| 177 | + conjA_dconjA::Const{Bool}, |
| 178 | + α_dα::Annotation{Tα}, |
| 179 | + β_dβ::Annotation{Tβ}, |
| 180 | + ba_dba::Const..., |
| 181 | + ) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number} |
| 182 | + cache_A, cache_C = cache |
| 183 | + Aval = something(cache_A, A_dA.val) |
| 184 | + Cval = cache_C |
| 185 | + p = p_dp.val |
| 186 | + q = q_dq.val |
| 187 | + conjA = conjA_dconjA.val |
| 188 | + α = α_dα.val |
| 189 | + β = β_dβ.val |
| 190 | + ba = map(ba_ -> getfield(ba_, :val), ba_dba) |
| 191 | + dC = C_dC.dval |
| 192 | + dA = A_dA.dval |
| 193 | + dC, dA, dα, dβ = TensorOperations.tensortrace_pullback!(dC, dA, Cval, Aval, p, q, conjA, α, β, ba...) |
| 194 | + return nothing, nothing, nothing, nothing, nothing, dα, dβ, map(ba_ -> nothing, ba)... |
| 195 | +end |
| 196 | + |
| 197 | +end |
0 commit comments