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