|
| 1 | +# tensorcontract! |
| 2 | +# --------------- |
| 3 | +# TODO: it might be beneficial to compare here if it would make sense to simply compute the |
| 4 | +# rrule of permute-permute-gemm-permute, rather than using the contractions directly. |
| 5 | +# This could possibly out save some permutations being carried out twice, at the cost of having |
| 6 | +# to store some more intermediate objects. |
| 7 | +# For example, the combination `ΔC, pΔC, false` appears in the pullback for ΔA and ΔB, so effectively |
| 8 | +# this permutation is done multiple times. |
| 9 | + |
| 10 | +function EnzymeRules.augmented_primal( |
| 11 | + config::EnzymeRules.RevConfigWidth{1}, |
| 12 | + func::Const{typeof(TensorKit.blas_contract!)}, |
| 13 | + ::Type{RT}, |
| 14 | + C::Annotation{<:AbstractTensorMap}, |
| 15 | + A::Annotation{<:AbstractTensorMap}, |
| 16 | + pA::Const{<:Index2Tuple}, |
| 17 | + B::Annotation{<:AbstractTensorMap}, |
| 18 | + pB::Const{<:Index2Tuple}, |
| 19 | + pAB::Const{<:Index2Tuple}, |
| 20 | + α::Annotation{<:Number}, |
| 21 | + β::Annotation{<:Number}, |
| 22 | + backend::Const, |
| 23 | + allocator::Const |
| 24 | + ) where {RT} |
| 25 | + Ccache = isa(β, Const) ? nothing : copy(C.val) |
| 26 | + A_needs_cache = EnzymeRules.overwritten(config)[3] && !(typeof(B) <: Const) && !(typeof(C) <: Const) |
| 27 | + Acache = A_needs_cache ? copy(A.val) : nothing |
| 28 | + B_needs_cache = EnzymeRules.overwritten(config)[5] && !(typeof(A) <: Const) && !(typeof(C) <: Const) |
| 29 | + Bcache = B_needs_cache ? copy(B.val) : nothing |
| 30 | + AB = if !isa(α, Const) |
| 31 | + AB = TO.tensorcontract(A.val, pA.val, false, B.val, pB.val, false, pAB.val, One(), backend.val, allocator.val) |
| 32 | + add!(C.val, AB, α.val, β.val) |
| 33 | + AB |
| 34 | + else |
| 35 | + TensorKit.blas_contract!(C.val, A.val, pA.val, B.val, pB.val, pAB.val, α.val, β.val, backend.val, allocator.val) |
| 36 | + nothing |
| 37 | + end |
| 38 | + primal = EnzymeRules.needs_primal(config) ? C.val : nothing |
| 39 | + shadow = EnzymeRules.needs_shadow(config) ? C.dval : nothing |
| 40 | + cache = (Ccache, Acache, Bcache, AB) |
| 41 | + return EnzymeRules.AugmentedReturn(primal, shadow, cache) |
| 42 | +end |
| 43 | + |
| 44 | +function EnzymeRules.reverse( |
| 45 | + config::EnzymeRules.RevConfigWidth{1}, |
| 46 | + func::Const{typeof(TensorKit.blas_contract!)}, |
| 47 | + ::Type{RT}, |
| 48 | + cache, |
| 49 | + C::Annotation{<:AbstractTensorMap}, |
| 50 | + A::Annotation{<:AbstractTensorMap}, |
| 51 | + pA::Const{<:Index2Tuple}, |
| 52 | + B::Annotation{<:AbstractTensorMap}, |
| 53 | + pB::Const{<:Index2Tuple}, |
| 54 | + pAB::Const{<:Index2Tuple}, |
| 55 | + α::Annotation{<:Number}, |
| 56 | + β::Annotation{<:Number}, |
| 57 | + backend::Const, |
| 58 | + allocator::Const |
| 59 | + ) where {RT} |
| 60 | + cacheC, cacheA, cacheB, AB = cache |
| 61 | + Cval = cacheC |
| 62 | + Aval = something(cacheA, A.val) |
| 63 | + Bval = something(cacheB, B.val) |
| 64 | + |
| 65 | + Δα = pullback_dα(α, C, AB) |
| 66 | + Δβ = pullback_dβ(β, C, Cval) |
| 67 | + |
| 68 | + if !isa(A, Const) |
| 69 | + TensorKit.blas_contract_pullback_ΔA!( |
| 70 | + A.dval, C.dval, Aval, pA.val, Bval, pB.val, pAB.val, α.val, backend.val, allocator.val |
| 71 | + ) # this typically returns nothing |
| 72 | + end |
| 73 | + if !isa(B, Const) |
| 74 | + TensorKit.blas_contract_pullback_ΔB!( |
| 75 | + B.dval, C.dval, Aval, pA.val, Bval, pB.val, pAB.val, α.val, backend.val, allocator.val |
| 76 | + ) # this typically returns nothing |
| 77 | + end |
| 78 | + !isa(C, Const) && pullback_dC!(C.dval, β.val) # this typically returns nothing |
| 79 | + return nothing, nothing, nothing, nothing, nothing, nothing, Δα, Δβ, nothing, nothing |
| 80 | +end |
| 81 | + |
| 82 | +function EnzymeRules.forward( |
| 83 | + config::EnzymeRules.FwdConfigWidth{1}, |
| 84 | + func::Const{typeof(TensorKit.blas_contract!)}, |
| 85 | + ::Type{RT}, |
| 86 | + C::Annotation{<:AbstractTensorMap}, |
| 87 | + A::Annotation{<:AbstractTensorMap}, |
| 88 | + pA::Annotation{<:Index2Tuple}, |
| 89 | + B::Annotation{<:AbstractTensorMap}, |
| 90 | + pB::Annotation{<:Index2Tuple}, |
| 91 | + pAB::Annotation{<:Index2Tuple}, |
| 92 | + α::Annotation{<:Number}, |
| 93 | + β::Annotation{<:Number}, |
| 94 | + backend::Const, |
| 95 | + allocator::Const |
| 96 | + ) where {RT} |
| 97 | + # ΔC′ = ΔC*β + C*Δβ + A*B*Δα + ΔA*B*α + A*ΔB*α |
| 98 | + if !isa(C, Const) |
| 99 | + if isa(β, Const) |
| 100 | + scale!(C.dval, β.val) |
| 101 | + else |
| 102 | + add!(C.dval, C.val, β.dval, β.val) |
| 103 | + end |
| 104 | + !isa(α, Const) && TensorKit.blas_contract!(C.dval, A.val, pA.val, B.val, pB.val, pAB.val, α.dval, One(), backend.val, allocator.val) |
| 105 | + !isa(A, Const) && TensorKit.blas_contract!(C.dval, A.dval, pA.val, B.val, pB.val, pAB.val, α.val, One(), backend.val, allocator.val) |
| 106 | + !isa(B, Const) && TensorKit.blas_contract!(C.dval, A.val, pA.val, B.dval, pB.val, pAB.val, α.val, One(), backend.val, allocator.val) |
| 107 | + end |
| 108 | + TensorKit.blas_contract!(C.val, A.val, pA.val, B.val, pB.val, pAB.val, α.val, β.val, backend.val, allocator.val) |
| 109 | + if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config) |
| 110 | + return C |
| 111 | + elseif EnzymeRules.needs_primal(config) |
| 112 | + return C.val |
| 113 | + elseif EnzymeRules.needs_shadow(config) |
| 114 | + return C.dval |
| 115 | + else |
| 116 | + return nothing |
| 117 | + end |
| 118 | +end |
| 119 | + |
| 120 | +# tensortrace! |
| 121 | +# ------------ |
| 122 | + |
| 123 | +function EnzymeRules.augmented_primal( |
| 124 | + config::EnzymeRules.RevConfigWidth{1}, |
| 125 | + func::Const{typeof(TensorKit.trace_permute!)}, |
| 126 | + ::Type{RT}, |
| 127 | + C::Annotation{<:AbstractTensorMap}, |
| 128 | + A::Annotation{<:AbstractTensorMap}, |
| 129 | + p::Const{<:Index2Tuple}, |
| 130 | + q::Const{<:Index2Tuple}, |
| 131 | + α::Annotation{<:Number}, |
| 132 | + β::Annotation{<:Number}, |
| 133 | + backend::Const, |
| 134 | + ) where {RT} |
| 135 | + C_cache = !isa(β, Const) ? copy(C.val) : nothing |
| 136 | + A_cache = EnzymeRules.overwritten(config)[3] ? copy(A.val) : nothing |
| 137 | + At = if !isa(α, Const) |
| 138 | + At = TO.tensortrace(A.val, p.val, q.val, false, One(), backend.val) |
| 139 | + add!(C.val, At, α.val, β.val) |
| 140 | + At |
| 141 | + else |
| 142 | + TensorKit.trace_permute!(C.val, A.val, p.val, q.val, α.val, β.val, backend.val) |
| 143 | + nothing |
| 144 | + end |
| 145 | + primal = EnzymeRules.needs_primal(config) ? C.val : nothing |
| 146 | + shadow = EnzymeRules.needs_shadow(config) ? C.dval : nothing |
| 147 | + cache = (C_cache, A_cache, At) |
| 148 | + return EnzymeRules.AugmentedReturn(primal, shadow, cache) |
| 149 | +end |
| 150 | + |
| 151 | + |
| 152 | +function EnzymeRules.reverse( |
| 153 | + config::EnzymeRules.RevConfigWidth{1}, |
| 154 | + func::Const{typeof(TensorKit.trace_permute!)}, |
| 155 | + ::Type{RT}, |
| 156 | + cache, |
| 157 | + C::Annotation{<:AbstractTensorMap}, |
| 158 | + A::Annotation{<:AbstractTensorMap}, |
| 159 | + p::Const{<:Index2Tuple}, |
| 160 | + q::Const{<:Index2Tuple}, |
| 161 | + α::Annotation{<:Number}, |
| 162 | + β::Annotation{<:Number}, |
| 163 | + backend::Const, |
| 164 | + ) where {RT} |
| 165 | + C_cache, A_cache, At = cache |
| 166 | + Aval = something(A_cache, A.val) |
| 167 | + Cval = something(C_cache, C.val) |
| 168 | + !isa(A, Const) && !isa(C, Const) && TensorKit.trace_permute_pullback_ΔA!(A.dval, C.dval, Aval, p.val, q.val, α.val, backend.val) |
| 169 | + Δαr = pullback_dα(α, C, At) |
| 170 | + Δβr = pullback_dβ(β, C, Cval) |
| 171 | + !isa(C, Const) && pullback_dC!(C.dval, β.val) |
| 172 | + return nothing, nothing, nothing, nothing, Δαr, Δβr, nothing |
| 173 | +end |
| 174 | + |
| 175 | +function EnzymeRules.forward( |
| 176 | + config::EnzymeRules.FwdConfigWidth{1}, |
| 177 | + func::Const{typeof(TensorKit.trace_permute!)}, |
| 178 | + ::Type{RT}, |
| 179 | + C::Annotation{<:AbstractTensorMap}, |
| 180 | + A::Annotation{<:AbstractTensorMap}, |
| 181 | + p::Annotation{<:Index2Tuple}, |
| 182 | + q::Annotation{<:Index2Tuple}, |
| 183 | + α::Annotation{<:Number}, |
| 184 | + β::Annotation{<:Number}, |
| 185 | + backend::Const, |
| 186 | + ) where {RT} |
| 187 | + # dD = dα * tr(A) + α * tr(dA) + dβ * C + β * dC |
| 188 | + # dC1 = dβ * C + β * dC |
| 189 | + if !isa(C, Const) |
| 190 | + if isa(β, Const) |
| 191 | + scale!(C.dval, β.val) |
| 192 | + else |
| 193 | + add!(C.dval, C.val, β.dval, β.val) |
| 194 | + end |
| 195 | + !isa(α, Const) && TensorKit.trace_permute!(C.dval, A.val, p.val, q.val, α.dval, One(), backend.val) |
| 196 | + !isa(A, Const) && TensorKit.trace_permute!(C.dval, A.dval, p.val, q.val, α.val, One(), backend.val) |
| 197 | + end |
| 198 | + TensorKit.trace_permute!(C.val, A.val, p.val, q.val, α.val, β.val, backend.val) |
| 199 | + if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config) |
| 200 | + return C |
| 201 | + elseif EnzymeRules.needs_primal(config) |
| 202 | + return C.val |
| 203 | + elseif EnzymeRules.needs_shadow(config) |
| 204 | + return C.dval |
| 205 | + else |
| 206 | + return nothing |
| 207 | + end |
| 208 | +end |
0 commit comments