-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMatrixAlgebraKitEnzymeExt.jl
More file actions
459 lines (445 loc) · 20 KB
/
MatrixAlgebraKitEnzymeExt.jl
File metadata and controls
459 lines (445 loc) · 20 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
module MatrixAlgebraKitEnzymeExt
using MatrixAlgebraKit
using MatrixAlgebraKit: copy_input, initialize_output, zero!
using MatrixAlgebraKit: diagview, inv_safe, truncate
using MatrixAlgebraKit: qr_pullback!, lq_pullback!
using MatrixAlgebraKit: qr_null_pullback!, lq_null_pullback!
using MatrixAlgebraKit: eig_pullback!, eigh_pullback!, eig_vals_pullback!, eigh_vals_pullback!
using MatrixAlgebraKit: svd_pullback!, svd_vals_pullback!
using MatrixAlgebraKit: left_polar_pullback!, right_polar_pullback!
using Enzyme
using Enzyme.EnzymeCore
using Enzyme.EnzymeCore: EnzymeRules
using LinearAlgebra
@inline EnzymeRules.inactive_type(::Type{Alg}) where {Alg <: MatrixAlgebraKit.AbstractAlgorithm} = true
@inline EnzymeRules.inactive_type(::Type{TS}) where {TS <: MatrixAlgebraKit.TruncationStrategy} = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.select_algorithm), func::F, A::AbstractMatrix, alg::Alg) where {F, Alg} = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_algorithm), func::F, A::AbstractMatrix) where {F} = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.check_input), func::F, A::AbstractMatrix, alg::Alg) where {F, Alg} = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.check_input), func::F, A::AbstractMatrix, arg::Any, alg::Alg) where {F, Alg} = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.check_hermitian), A::AbstractMatrix, alg::Alg) where {Alg} = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.defaulttol), ::Any) = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_pullback_gauge_atol), ::Any) = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_pullback_gauge_atol), ::Any, ::Any...) = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_pullback_degeneracy_atol), ::Any) = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_pullback_rank_atol), ::Any) = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_hermitian_tol), ::AbstractMatrix) = true
#----------- NOTE about derivatives ---------
# Each Enzyme augmented_return + reverse pair
# has a "tape" or "cache" -- we can place
# variables on this tape that can be accessed
# in the return pass *after* they have been
# "filled in" with accumulated derivatives.
# For many of the rules here, we may create a
# placeholder (usually called `dret`) for
# variables which may be instantiated, then,
# earlier in the reverse pass, this `dret` is
# filled in with accumulated derivatives for
# the created variable. It can then be used
# to update the derivative of `A` or any
# other provided input variable.
#--------------------------------------------
# this rule is necessary for now as without it,
# a segfault occurs both on 1.10 and 1.12 -- likely
# a deeper internal bug
function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(copy_input)},
::Type{RT},
f::Annotation,
A::Annotation
) where {RT}
ret = func.val(f.val, A.val)
primal = EnzymeRules.needs_primal(config) ? ret : nothing
shadow = EnzymeRules.needs_shadow(config) ? zero(A.dval) : nothing
return EnzymeRules.AugmentedReturn(primal, shadow, shadow)
end
function EnzymeRules.reverse(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(copy_input)},
::Type{RT},
cache,
f::Annotation,
A::Annotation
) where {RT}
copy_shadow = cache
if !isa(A, Const) && !isnothing(copy_shadow)
A.dval .+= copy_shadow
end
return (nothing, nothing)
end
# two-argument factorizations like LQ, QR, EIG
for (f, pb) in (
(qr_full!, qr_pullback!),
(lq_full!, lq_pullback!),
(qr_compact!, qr_pullback!),
(lq_compact!, lq_pullback!),
(eig_full!, eig_pullback!),
(eigh_full!, eigh_pullback!),
(left_polar!, left_polar_pullback!),
(right_polar!, right_polar_pullback!),
)
@eval begin
function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof($f)},
::Type{RT},
A::Annotation,
arg::Annotation{Tuple{TA, TB}},
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
) where {RT, TA, TB}
# A is overwritten in the primal, but NOT used in the pullback,
# so we do not need to cache it. This may change if future pullbacks
# depend directly on A!
ret = func.val(A.val, arg.val, alg.val)
# if arg.val == ret, the annotation must be Duplicated or DuplicatedNoNeed
# if arg isa Const, ret may still be modified further down the call graph so we should
# copy it to protect ourselves
cache_arg = (arg.val !== ret) || EnzymeRules.overwritten(config)[3] ? copy.(ret) : nothing
dret = if EnzymeRules.needs_shadow(config)
(TA == Nothing && TB == Nothing) || isa(arg, Const) ? zero.(ret) : arg.dval
else
nothing
end
primal = EnzymeRules.needs_primal(config) ? ret : nothing
return EnzymeRules.AugmentedReturn(primal, dret, (cache_arg, dret))
end
function EnzymeRules.reverse(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof($f)},
::Type{RT},
cache,
A::Annotation,
arg::Annotation{Tuple{TA, TB}},
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
) where {RT, TA, TB}
cache_arg, darg = cache
# A is NOT used in the pullback, so we assign Aval = nothing
# to trigger an error in case the pullback is modified to directly
# use A (so that whoever does this is forced to handle caching A
# appropriately here)
Aval = nothing
argval = something(cache_arg, arg.val)
if !isa(A, Const)
$pb(A.dval, Aval, argval, darg)
end
!isa(arg, Const) && make_zero!(arg.dval)
return (nothing, nothing, nothing)
end
end
end
for (f, pb) in (
(qr_null!, qr_null_pullback!),
(lq_null!, lq_null_pullback!),
)
@eval begin
function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof($f)},
::Type{RT},
A::Annotation,
arg::Annotation{TA},
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
) where {RT, TA}
# here, A IS directly used in the pullback, and overwritten
# in the forward primal call, so we MUST cache its value
cache_A = copy(A.val)
ret = func.val(A.val, arg.val, alg.val)
# if arg.val == ret, the annotation must be Duplicated or DuplicatedNoNeed
# if arg isa Const, ret may still be modified further down the call graph so we should
# copy it to protect ourselves
cache_arg = (arg.val !== ret) || EnzymeRules.overwritten(config)[3] ? copy(ret) : nothing
# on 1.10, Enzyme can get confused about whether it needs the shadow
if EnzymeRules.needs_shadow(config)
dret = (TA == Nothing || isa(arg, Const)) ? zero(ret) : arg.dval
else
dret = nothing
end
primal = EnzymeRules.needs_primal(config) ? ret : nothing
return EnzymeRules.AugmentedReturn(primal, dret, (cache_A, cache_arg, dret))
end
function EnzymeRules.reverse(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof($f)},
::Type{RT},
cache,
A::Annotation,
arg::Annotation,
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
) where {RT}
cache_A, cache_arg, darg = cache
Aval = cache_A
argval = something(cache_arg, arg.val)
# on 1.10, Enzyme can get confused about whether it needs the shadow
# replace the dret with the arg.dval in the case it's nothing
argdval = something(darg, arg.dval)
if !isa(A, Const)
$pb(A.dval, Aval, argval, argdval)
end
!isa(arg, Const) && make_zero!(arg.dval)
return (nothing, nothing, nothing)
end
end
end
for f in (:svd_compact!, :svd_full!)
@eval begin
function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof($f)},
::Type{RT},
A::Annotation,
USVᴴ::Annotation{Tuple{TU, TS, TVᴴ}},
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
) where {RT, TU, TS, TVᴴ}
# A is overwritten in the primal, but NOT used in the pullback,
# so we do not need to cache it. This may change if future pullbacks
# depend directly on A!
ret = func.val(A.val, USVᴴ.val, alg.val)
# if USVᴴ.val == ret, the annotation must be Duplicated or DuplicatedNoNeed
# if USVᴴ isa Const, ret may still be modified further down the call graph so we should
# copy it to protect ourselves
cache_USVᴴ = (USVᴴ.val !== ret) || EnzymeRules.overwritten(config)[3] ? copy.(ret) : nothing
# the USVᴴ may be nothing for eltypes handled by GenericLinearAlgebra
dret = if EnzymeRules.needs_shadow(config) && ((TU == TS == TVᴴ == Nothing) || isa(USVᴴ, Const))
dU = zero(ret[1])
# special casing `Diagonal` seems to be necessary due to Enzyme's type analysis
dS = $(f == svd_compact!) ? Diagonal(zero(ret[2].diag)) : zero(ret[2])
dVᴴ = zero(ret[3])
(dU, dS, dVᴴ)
elseif EnzymeRules.needs_shadow(config)
USVᴴ.dval
else
nothing
end
primal = EnzymeRules.needs_primal(config) ? ret : nothing
return EnzymeRules.AugmentedReturn(primal, dret, (cache_USVᴴ, dret))
end
function EnzymeRules.reverse(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof($f)},
::Type{RT},
cache,
A::Annotation,
USVᴴ::Annotation,
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
) where {RT}
cache_USVᴴ, dUSVᴴ = cache
# A is NOT used in the pullback, so we assign Aval = nothing
# to trigger an error in case the pullback is modified to directly
# use A (so that whoever does this is forced to handle caching A
# appropriately here)
Aval = nothing
USVᴴval = something(cache_USVᴴ, USVᴴ.val)
if !isa(A, Const)
minmn = min(size(A.val)...)
if $(f == svd_compact!) # compact
svd_pullback!(A.dval, Aval, USVᴴval, dUSVᴴ)
else # full
# TODO: revisit this once `svd_pullback` supports `svd_full` output and adjoints
U, S, Vᴴ = USVᴴval
vU = view(U, :, 1:minmn)
vS = Diagonal(view(diagview(S), 1:minmn))
vVᴴ = view(Vᴴ, 1:minmn, :)
vdU = view(dUSVᴴ[1], :, 1:minmn)
vdS = Diagonal(view(diagview(dUSVᴴ[2]), 1:minmn))
vdVᴴ = view(dUSVᴴ[3], 1:minmn, :)
svd_pullback!(A.dval, Aval, (vU, vS, vVᴴ), (vdU, vdS, vdVᴴ))
end
end
!isa(USVᴴ, Const) && make_zero!(USVᴴ.dval)
return (nothing, nothing, nothing)
end
end
end
function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(svd_trunc_no_error!)},
::Type{RT},
A::Annotation,
USVᴴ::Annotation{Tuple{TU, TS, TVᴴ}},
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
) where {RT, TU, TS, TVᴴ}
# A is overwritten in the primal, but NOT used in the pullback,
# so we do not need to cache it. This may change if future pullbacks
# depend directly on A!
ret = svd_compact!(A.val, USVᴴ.val, alg.val.alg)
cache_USVᴴ = (USVᴴ.val !== ret) || EnzymeRules.overwritten(config)[3] ? copy.(ret) : nothing
USVᴴ′, ind = MatrixAlgebraKit.truncate(svd_trunc!, ret, alg.val.trunc)
primal = EnzymeRules.needs_primal(config) ? USVᴴ′ : nothing
# This creates new output shadow matrices, we use USVᴴ′ to ensure the
# eltypes and dimensions are correct.
# These new shadow matrices are "filled in" with the accumulated
# results from earlier in reverse-mode AD after this function exits
# and before `reverse` is called.
dret = if EnzymeRules.needs_shadow(config)
(zero(USVᴴ′[1]), Diagonal(zero(USVᴴ′[2].diag)), zero(USVᴴ′[3]))
else
nothing
end
return EnzymeRules.AugmentedReturn(primal, dret, (cache_USVᴴ, dret, ind))
end
function EnzymeRules.reverse(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(svd_trunc_no_error!)},
::Type{RT},
cache,
A::Annotation,
USVᴴ::Annotation,
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
) where {RT}
cache_USVᴴ, dUSVᴴ, ind = cache
# A is NOT used in the pullback, so we assign Aval = nothing
# to trigger an error in case the pullback is modified to directly
# use A (so that whoever does this is forced to handle caching A
# appropriately here)
Aval = nothing
USVᴴval = something(cache_USVᴴ, USVᴴ.val)
if !isa(A, Const)
svd_pullback!(A.dval, Aval, USVᴴval, dUSVᴴ, ind)
end
!isa(USVᴴ, Const) && make_zero!(USVᴴ.dval)
return (nothing, nothing, nothing)
end
for (f, trunc_f, full_f, pb) in (
(:eigh_trunc_no_error!, :eigh_trunc!, :eigh_full!, :eigh_pullback!),
(:eig_trunc_no_error!, :eig_trunc!, :eig_full!, :eig_pullback!),
)
@eval function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof($f)},
::Type{RT},
A::Annotation,
DV::Annotation{Tuple{TA, TB}},
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
) where {RT, TA, TB}
# A is overwritten in the primal, but NOT used in the pullback,
# so we do not need to cache it. This may change if future pullbacks
# depend directly on A!
ret = $full_f(A.val, DV.val, alg.val.alg)
cache_DV = (DV.val !== ret) || EnzymeRules.overwritten(config)[3] ? copy.(ret) : nothing
DV′, ind = truncate($trunc_f, ret, alg.val.trunc)
primal = EnzymeRules.needs_primal(config) ? DV′ : nothing
dret = if EnzymeRules.needs_shadow(config)
(Diagonal(zero(diagview(DV′[1]))), zero(DV′[2]))
else
nothing
end
return EnzymeRules.AugmentedReturn(primal, dret, (cache_DV, dret, ind))
end
@eval function EnzymeRules.reverse(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof($f)},
::Type{RT},
cache,
A::Annotation,
DV::Annotation,
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
) where {RT}
cache_DV, dDVtrunc, ind = cache
# A is NOT used in the pullback, so we assign Aval = nothing
# to trigger an error in case the pullback is modified to directly
# use A (so that whoever does this is forced to handle caching A
# appropriately here)
Aval = nothing
DVval = something(cache_DV, DV.val)
if !isa(A, Const)
$pb(A.dval, Aval, DVval, dDVtrunc, ind)
end
!isa(DV, Const) && make_zero!(DV.dval)
return (nothing, nothing, nothing)
end
end
for (f!, f_full!, pb!) in (
(eig_vals!, eig_full!, eig_vals_pullback!),
(eigh_vals!, eigh_full!, eigh_vals_pullback!),
)
@eval begin
function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof($f!)},
::Type{RT},
A::Annotation,
D::Annotation{TD},
alg::Annotation{<:MatrixAlgebraKit.AbstractAlgorithm},
) where {RT, TD}
# A is overwritten in the primal, but NOT used in the pullback,
# so we do not need to cache it. This may change if future pullbacks
# depend directly on A!
nD, V = $f_full!(A.val, alg.val)
ret = TD == Nothing ? diagview(nD) : copy!(D.val, diagview(nD))
cache_D = (D.val !== ret) || EnzymeRules.overwritten(config)[3] ? copy(ret) : nothing
primal = EnzymeRules.needs_primal(config) ? ret : nothing
# on 1.10, Enzyme can get confused about whether it needs the shadow
# create dret no matter what to account for this
dret = TD == Nothing || isa(D, Const) ? zero(ret) : D.dval
shadow = EnzymeRules.needs_shadow(config) ? dret : nothing
return EnzymeRules.AugmentedReturn(primal, shadow, (cache_D, dret, V))
end
function EnzymeRules.reverse(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof($f!)},
::Type{RT},
cache,
A::Annotation,
D::Annotation,
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
) where {RT}
cache_D, dD, V = cache
Dval = something(cache_D, D.val)
# A is NOT used in the pullback, so we assign Aval = nothing
# to trigger an error in case the pullback is modified to directly
# use A (so that whoever does this is forced to handle caching A
# appropriately here)
Aval = nothing
if !isa(A, Const)
$pb!(A.dval, Aval, (Diagonal(Dval), V), dD)
end
!isa(D, Const) && make_zero!(D.dval)
return (nothing, nothing, nothing)
end
end
end
function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(svd_vals!)},
::Type{RT},
A::Annotation,
S::Annotation{TS},
alg::Annotation{<:MatrixAlgebraKit.AbstractAlgorithm},
) where {RT, TS}
# A is overwritten in the primal, but NOT used in the pullback,
# so we do not need to cache it. This may change if future pullbacks
# depend directly on A!
U, nS, Vᴴ = svd_compact!(A.val, alg.val)
ret = TS == Nothing ? diagview(nS) : copy!(S.val, diagview(nS))
cache_S = (S.val !== ret) || EnzymeRules.overwritten(config)[3] ? copy(ret) : nothing
primal = EnzymeRules.needs_primal(config) ? ret : nothing
# on 1.10, Enzyme can get confused about whether it needs the shadow
# create dret no matter what to account for this
dret = TS == Nothing || isa(S, Const) ? zero(ret) : S.dval
shadow = EnzymeRules.needs_shadow(config) ? dret : nothing
return EnzymeRules.AugmentedReturn(primal, shadow, (cache_S, dret, U, Vᴴ))
end
function EnzymeRules.reverse(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(svd_vals!)},
::Type{RT},
cache,
A::Annotation,
S::Annotation,
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
) where {RT}
cache_S, dS, U, Vᴴ = cache
# A is NOT used in the pullback, so we assign Aval = nothing
# to trigger an error in case the pullback is modified to directly
# use A (so that whoever does this is forced to handle caching A
# appropriately here)
Aval = nothing
Sval = something(cache_S, S.val)
if !isa(A, Const)
svd_vals_pullback!(A.dval, Aval, (U, Diagonal(Sval), Vᴴ), dS)
end
!isa(S, Const) && make_zero!(S.dval)
return (nothing, nothing, nothing)
end
end