Repository navigation
Expand file tree
/
Copy pathTensorOperationsEnzymeExt.jl
More file actions
419 lines (397 loc) · 15.3 KB
/
Copy pathTensorOperationsEnzymeExt.jl
File metadata and controls
419 lines (397 loc) · 15.3 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
module TensorOperationsEnzymeExt
using TensorOperations
using TensorOperations: AbstractBackend, DefaultAllocator, CUDAAllocator, ManualAllocator
using VectorInterface
using TupleTools
using Enzyme
using Enzyme.EnzymeCore
using Enzyme.EnzymeCore: EnzymeRules
@inline EnzymeRules.inactive(::typeof(TensorOperations.tensorfree!), ::Any) = true
@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
@inline EnzymeRules.inactive_type(v::Type{<:IndexTuple}) = true
function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(TensorOperations.tensoralloc)},
::Type{RT},
ttype::Const,
structure::Const,
istemp::Const{Bool},
allocator::Const
) where {RT}
primal = EnzymeRules.needs_primal(config) ? TensorOperations.tensoralloc(ttype.val, structure.val, Val(false), allocator.val) : nothing
shadow = EnzymeRules.needs_shadow(config) ? TensorOperations.tensoralloc(ttype.val, structure.val, Val(false), allocator.val) : nothing
return EnzymeRules.AugmentedReturn(primal, shadow, nothing)
end
function EnzymeRules.reverse(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(TensorOperations.tensoralloc)},
::Type{RT},
cache,
ttype::Const,
structure::Const,
istemp::Const{Bool},
allocator::Const,
) where {RT}
return nothing, nothing, nothing, nothing
end
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 = !isa(β_dβ, Const) ? 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?
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)
if !isa(A_dA, Const) && !isa(C_dC, Const)
ΔC = C_dC.dval
ΔA = A_dA.dval
TensorOperations.tensorcontract_pullback_dA!(ΔA, ΔC, Cval, Aval, pA, conjA, Bval, pB, conjB, pAB, α, ba...)
end
if !isa(B_dB, Const) && !isa(C_dC, Const)
ΔC = C_dC.dval
ΔB = B_dB.dval
TensorOperations.tensorcontract_pullback_dB!(ΔB, ΔC, Cval, Aval, pA, conjA, Bval, pB, conjB, pAB, α, ba...)
end
Δα = if !isa(α_dα, Const) && !isa(C_dC, Const)
ΔC = C_dC.dval
TensorOperations.tensorcontract_pullback_dα(ΔC, Cval, Aval, pA, conjA, Bval, pB, conjB, pAB, α, ba...)
elseif !isa(α_dα, Const)
zero(α_dα.val)
else
nothing
end
Δβ = if !isa(β_dβ, Const) && !isa(C_dC, Const)
ΔC = C_dC.dval
TensorOperations.pullback_dβ(ΔC, Cval, β)
elseif !isa(β_dβ, Const)
zero(β_dβ.val)
else
nothing
end
!isa(C_dC, Const) && TensorOperations.pullback_dC!(C_dC.dval, β)
return nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, Δα, Δβ, map(ba_ -> nothing, ba)...
end
function EnzymeRules.forward(
config::EnzymeRules.FwdConfigWidth{1},
func::Const{typeof(TensorOperations.tensorcontract!)},
::Type{RT},
C_dC::Annotation{<:AbstractArray{TC}},
A_dA::Annotation{<:AbstractArray{TA}},
pA_dpA::Annotation{<:Index2Tuple},
conjA_dconjA::Const{Bool},
B_dB::Annotation{<:AbstractArray{TB}},
pB_dpB::Annotation{<:Index2Tuple},
conjB_dconjB::Const{Bool},
pAB_dpAB::Annotation{<:Index2Tuple},
α_dα::Annotation{Tα},
β_dβ::Annotation{Tβ},
ba_dba::Const...,
) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TB <: Number, TC <: Number}
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)
if !isa(C_dC, Const)
scale!(C_dC.dval, β)
if !isa(β_dβ, Const)
add!(C_dC.dval, C_dC.val, β_dβ.dval)
end
if !isa(α_dα, Const)
tensorcontract!(C_dC.dval, A_dA.val, pA, conjA, B_dB.val, pB, conjB, pAB, α_dα.dval, One(), ba...)
end
if !isa(A_dA, Const)
tensorcontract!(C_dC.dval, A_dA.dval, pA, conjA, B_dB.val, pB, conjB, pAB, α, One(), ba...)
end
if !isa(B_dB, Const)
tensorcontract!(C_dC.dval, A_dA.val, pA, conjA, B_dB.dval, pB, conjB, pAB, α, One(), ba...)
end
end
TensorOperations.tensorcontract!(C_dC.val, A_dA.val, pA, conjA, B_dB.val, pB, conjB, pAB, α, β, ba...)
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
return C_dC
elseif EnzymeRules.needs_primal(config)
return C_dC.val
elseif EnzymeRules.needs_shadow(config)
return C_dC.dval
else
return nothing
end
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 = !isa(β_dβ, Const) ? 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)
if !isa(A_dA, Const) && !isa(C_dC, Const)
ΔC = C_dC.dval
ΔA = A_dA.dval
TensorOperations.tensoradd_pullback_dA!(ΔA, ΔC, Cval, Aval, pA, conjA, α, ba...)
end
Δα = if !isa(α_dα, Const) && !isa(C_dC, Const)
ΔC = C_dC.dval
TensorOperations.tensoradd_pullback_dα(ΔC, Cval, Aval, pA, conjA, α, ba...)
elseif !isa(α_dα, Const)
zero(α_dα.val)
else
nothing
end
Δβ = if !isa(β_dβ, Const) && !isa(C_dC, Const)
ΔC = C_dC.dval
TensorOperations.pullback_dβ(ΔC, Cval, β)
elseif !isa(β_dβ, Const)
zero(β_dβ.val)
else
nothing
end
!isa(C_dC, Const) && TensorOperations.pullback_dC!(C_dC.dval, β)
return nothing, nothing, nothing, nothing, Δα, Δβ, map(ba_ -> nothing, ba)...
end
function EnzymeRules.forward(
config::EnzymeRules.FwdConfigWidth{1},
::Annotation{typeof(tensoradd!)},
::Type{RT},
C_dC::Annotation{<:AbstractArray{TC}},
A_dA::Annotation{<:AbstractArray{TA}},
pA_dpA::Annotation{<:Index2Tuple},
conjA_dconjA::Const{Bool},
α_dα::Annotation{Tα},
β_dβ::Annotation{Tβ},
ba_dba::Const...,
) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number}
pA = pA_dpA.val
conjA = conjA_dconjA.val
α = α_dα.val
β = β_dβ.val
ba = map(ba_ -> getfield(ba_, :val), ba_dba)
# D = α * A + β *C
# dD = dα * A + α * dA + β dC + dβ * C
# dC′ = β dC + dβ * C
if !isa(C_dC, Const)
scale!(C_dC.dval, β)
if !isa(β_dβ, Const)
add!(C_dC.dval, C_dC.val, β_dβ.dval)
end
if !isa(A_dA, Const)
TensorOperations.tensoradd!(C_dC.dval, A_dA.dval, pA, conjA, α, One(), ba...)
end
if !isa(α_dα, Const)
TensorOperations.tensoradd!(C_dC.dval, A_dA.val, pA, conjA, α_dα.dval, One(), ba...)
end
end
TensorOperations.tensoradd!(C_dC.val, A_dA.val, pA, conjA, α, β, ba...)
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
return C_dC
elseif EnzymeRules.needs_primal(config)
return C_dC.val
elseif EnzymeRules.needs_shadow(config)
return C_dC.dval
else
return nothing
end
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 = !isa(β_dβ, Const) ? 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 = something(cache_C, C_dC.val)
p = p_dp.val
q = q_dq.val
conjA = conjA_dconjA.val
α = α_dα.val
β = β_dβ.val
ba = map(ba_ -> getfield(ba_, :val), ba_dba)
if !isa(A_dA, Const) && !isa(C_dC, Const)
ΔC = C_dC.dval
ΔA = A_dA.dval
TensorOperations.tensortrace_pullback_dA!(ΔA, ΔC, Cval, Aval, p, q, conjA, α, ba...)
end
Δα = if !isa(α_dα, Const) && !isa(C_dC, Const)
ΔC = C_dC.dval
TensorOperations.tensortrace_pullback_dα(ΔC, Cval, Aval, p, q, conjA, α, ba...)
elseif !isa(α_dα, Const)
zero(α_dα.val)
else
nothing
end
Δβ = if !isa(β_dβ, Const) && !isa(C_dC, Const)
ΔC = C_dC.dval
TensorOperations.pullback_dβ(ΔC, Cval, β)
elseif !isa(β_dβ, Const)
zero(β_dβ.val)
else
nothing
end
!isa(C_dC, Const) && TensorOperations.pullback_dC!(C_dC.dval, β)
return nothing, nothing, nothing, nothing, nothing, Δα, Δβ, map(ba_ -> nothing, ba)...
end
function EnzymeRules.forward(
config::EnzymeRules.RevConfigWidth{1},
::Annotation{typeof(tensortrace!)},
::Type{RT},
C_dC::Annotation{<:AbstractArray{TC}},
A_dA::Annotation{<:AbstractArray{TA}},
p_dp::Annotation{<:Index2Tuple},
q_dq::Annotation{<:Index2Tuple},
conjA_dconjA::Const{Bool},
α_dα::Annotation{Tα},
β_dβ::Annotation{Tβ},
ba_dba::Const...,
) where {RT, Tα <: Number, Tβ <: Number, TA <: Number, TC <: Number}
p = p_dp.val
q = q_dq.val
conjA = conjA_dconjA.val
α = α_dα.val
β = β_dβ.val
ba = map(ba_ -> getfield(ba_, :val), ba_dba)
# dD = dα * tr(A) + α * tr(dA) + dβ * C + β * dC
# dC1 = dβ * C + β * dC
if !isa(C_dC, Const)
scale!(C_dC.dval, β)
if !isa(β_dβ, Const)
add!(C_dC.dval, C_dC.val, β_dβ.dval)
end
if !isa(α_dα, Const)
TensorOperations.tensortrace!(C_dC.dval, A_dA.val, p, q, conjA, α_dα.dval, One(), ba...)
end
if !isa(A_dA, Const)
TensorOperations.tensortrace!(C_dC.dval, A_dA.dval, p, q, conjA, α, One(), ba...)
end
end
# D = α * tr(A) + β * C
TensorOperations.tensortrace!(C_dC.val, A_dA.val, p, q, conjA, α, β, ba...)
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
return C_dC
elseif EnzymeRules.needs_primal(config)
return C_dC.val
elseif EnzymeRules.needs_shadow(config)
return C_dC.dval
else
return nothing
end
end
end