Skip to content

Commit a72d6d9

Browse files
committed
Basic eig(h) forward rules
1 parent 584e948 commit a72d6d9

9 files changed

Lines changed: 168 additions & 30 deletions

File tree

‎ext/MatrixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using MatrixAlgebraKit: diagview, inv_safe, truncate
66
using MatrixAlgebraKit: qr_pullback!, lq_pullback!
77
using MatrixAlgebraKit: qr_null_pullback!, lq_null_pullback!
88
using MatrixAlgebraKit: eig_pullback!, eigh_pullback!, eig_vals_pullback!, eigh_vals_pullback!
9+
using MatrixAlgebraKit: eig_pushforward!, eigh_pushforward!, eig_vals_pushforward!, eigh_vals_pushforward!
910
using MatrixAlgebraKit: svd_pullback!, svd_vals_pullback!
1011
using MatrixAlgebraKit: left_polar_pullback!, right_polar_pullback!
1112
using MatrixAlgebraKit: left_polar_pushforward!, right_polar_pushforward!
@@ -119,18 +120,20 @@ for (f, pb) in (
119120
end
120121

121122
for (f, pf) in (
122-
(left_polar!, left_polar_pushforward!),
123-
(right_polar!, right_polar_pushforward!),
123+
(:right_polar!, :right_polar_pushforward!),
124+
(:left_polar!, :left_polar_pushforward!),
125+
(:eigh_full!, :eigh_pushforward!),
126+
(:eig_full!, :eig_pushforward!),
124127
)
125128
@eval begin
126129
function EnzymeRules.forward(
127130
config::EnzymeRules.FwdConfigWidth{1},
128131
func::Const{typeof($f)},
129132
::Type{RT},
130133
A::Annotation,
131-
arg::Annotation{TA},
134+
arg::Annotation,
132135
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
133-
) where {RT, TA}
136+
) where {RT}
134137
$f(A.val, arg.val, alg.val)
135138
if !isa(A, Const) && !isa(arg, Const)
136139
$pf(A.dval, A.val, arg.val, arg.dval)
@@ -367,9 +370,9 @@ for (f, trunc_f, full_f, pb) in (
367370
end
368371
end
369372

370-
for (f!, f_full!, pb!) in (
371-
(eig_vals!, eig_full!, eig_vals_pullback!),
372-
(eigh_vals!, eigh_full!, eigh_vals_pullback!),
373+
for (f!, f_full!, pb!, pf!) in (
374+
(:eig_vals!, :eig_full!, :eig_vals_pullback!, :eig_vals_pushforward!),
375+
(:eigh_vals!, :eigh_full!, :eigh_vals_pullback!, :eigh_vals_pushforward!),
373376
)
374377
@eval begin
375378
function EnzymeRules.augmented_primal(
@@ -418,6 +421,34 @@ for (f!, f_full!, pb!) in (
418421
!isa(D, Const) && !A_is_arg && make_zero!(D.dval)
419422
return (nothing, nothing, nothing)
420423
end
424+
function EnzymeRules.forward(
425+
config::EnzymeRules.FwdConfigWidth{1},
426+
func::Const{typeof($f!)},
427+
::Type{RT},
428+
A::Annotation{TA},
429+
D::Annotation,
430+
alg::Const{<:MatrixAlgebraKit.AbstractAlgorithm},
431+
) where {RT, TA}
432+
DV = $f_full!(A.val, alg.val)
433+
Dval, V = DV
434+
A_is_arg = !isa(A, Const) && TA <: Diagonal && diagview(A.dval) === D.dval
435+
if !isa(A, Const) && !isa(D, Const)
436+
ΔD = A_is_arg ? make_zero(D.dval) : D.dval
437+
$pf!(A.dval, A.val, (Diagonal(Dval), V), ΔD)
438+
A_is_arg && (D.dval .= ΔD)
439+
!A_is_arg && (D.val .= diagview(Dval))
440+
end
441+
!isa(A, Const) && !A_is_arg && make_zero!(A.dval)
442+
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
443+
return D
444+
elseif EnzymeRules.needs_primal(config)
445+
return D.val
446+
elseif EnzymeRules.needs_shadow(config)
447+
return D.dval
448+
else
449+
return nothing
450+
end
451+
end
421452
end
422453
end
423454

‎ext/MatrixAlgebraKitMooncakeExt/MatrixAlgebraKitMooncakeExt.jl‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ using MatrixAlgebraKit: inv_safe, diagview, copy_input, initialize_output, zero!
77
using MatrixAlgebraKit: qr_pullback!, lq_pullback!
88
using MatrixAlgebraKit: qr_null_pullback!, lq_null_pullback!
99
using MatrixAlgebraKit: eig_pullback!, eigh_pullback!, eig_vals_pullback!
10+
using MatrixAlgebraKit: eig_pushforward!, eig_vals_pushforward!
11+
using MatrixAlgebraKit: eigh_pushforward!, eigh_vals_pushforward!
1012
using MatrixAlgebraKit: eig_trunc_pullback!, eigh_trunc_pullback!, eigh_vals_pullback!
1113
using MatrixAlgebraKit: left_polar_pullback!, right_polar_pullback!
1214
using MatrixAlgebraKit: left_polar_pushforward!, right_polar_pushforward!
@@ -113,6 +115,8 @@ end
113115
for (f!, f, pf) in (
114116
(:left_polar!, :left_polar, :left_polar_pushforward!),
115117
(:right_polar!, :right_polar, :right_polar_pushforward!),
118+
(:eig_full!, :eig_full, :eig_pushforward!),
119+
(:eigh_full!, :eigh_full, :eigh_pushforward!),
116120
)
117121
@eval begin
118122
@is_primitive Mooncake.DefaultCtx Mooncake.ForwardMode Tuple{typeof($f!), Any, Tuple{<:Any, <:Any}, MatrixAlgebraKit.AbstractAlgorithm}
@@ -177,12 +181,12 @@ for (f!, f, pb, adj) in (
177181
end
178182
end
179183

180-
for (f!, f, f_full, pb, adj) in (
181-
(:eig_vals!, :eig_vals, :eig_full, :eig_vals_pullback!, :eig_vals_adjoint),
182-
(:eigh_vals!, :eigh_vals, :eigh_full, :eigh_vals_pullback!, :eigh_vals_adjoint),
184+
for (f!, f, f_full, pb, pf, adj) in (
185+
(:eig_vals!, :eig_vals, :eig_full, :eig_vals_pullback!, :eig_vals_pushforward!, :eig_vals_adjoint),
186+
(:eigh_vals!, :eigh_vals, :eigh_full, :eigh_vals_pullback!, :eigh_vals_pushforward!, :eigh_vals_adjoint),
183187
)
184188
@eval begin
185-
@is_primitive Mooncake.DefaultCtx Mooncake.ReverseMode Tuple{typeof($f!), Any, Any, MatrixAlgebraKit.AbstractAlgorithm}
189+
@is_primitive Mooncake.DefaultCtx Tuple{typeof($f!), Any, Any, MatrixAlgebraKit.AbstractAlgorithm}
186190
function Mooncake.rrule!!(::CoDual{typeof($f!)}, A_dA::CoDual, D_dD::CoDual, alg_dalg::CoDual)
187191
# compute primal
188192
A, dA = arrayify(A_dA)
@@ -210,7 +214,18 @@ for (f!, f, f_full, pb, adj) in (
210214
end
211215
return D_dD, $adj
212216
end
213-
@is_primitive Mooncake.DefaultCtx Mooncake.ReverseMode Tuple{typeof($f), Any, MatrixAlgebraKit.AbstractAlgorithm}
217+
function Mooncake.frule!!(::Dual{typeof($f!)}, A_dA::Dual, D_dD::Dual, alg_dalg::Dual)
218+
# compute primal
219+
A, dA = arrayify(A_dA)
220+
D, dD = arrayify(D_dD)
221+
# update primal
222+
DV = $f_full(A, Mooncake.primal(alg_dalg))
223+
V = DV[2]
224+
copyto!(D, diagview(DV[1]))
225+
$pf(dA, A, (D, V), dD)
226+
return D_dD
227+
end
228+
@is_primitive Mooncake.DefaultCtx Tuple{typeof($f), Any, MatrixAlgebraKit.AbstractAlgorithm}
214229
function Mooncake.rrule!!(::CoDual{typeof($f)}, A_dA::CoDual, alg_dalg::CoDual)
215230
# compute primal
216231
A, dA = arrayify(A_dA)
@@ -227,6 +242,18 @@ for (f!, f, f_full, pb, adj) in (
227242
end
228243
return output_codual, $adj
229244
end
245+
function Mooncake.frule!!(::Dual{typeof($f)}, A_dA::Dual, alg_dalg::Dual)
246+
# compute primal
247+
A, dA = arrayify(A_dA)
248+
# update primal
249+
DV = $f_full(A, Mooncake.primal(alg_dalg))
250+
V = DV[2]
251+
output = diagview(DV[1])
252+
output_dual = Dual(output, Mooncake.zero_tangent(output))
253+
D, dD = arrayify(output_dual)
254+
$pf(dA, A, DV, dD)
255+
return output_dual
256+
end
230257
end
231258
end
232259

‎src/MatrixAlgebraKit.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ include("pullbacks/svd.jl")
130130
include("pullbacks/polar.jl")
131131

132132
include("pushforwards/polar.jl")
133+
include("pushforwards/eig.jl")
134+
include("pushforwards/eigh.jl")
133135

134136
include("precompile.jl")
135137

‎src/pushforwards/eig.jl‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function eig_pushforward!(
2+
ΔA, A, DV, ΔDV;
3+
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
4+
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
5+
)
6+
D, V = DV
7+
ΔD, ΔV = ΔDV
8+
iVΔAV = inv(V) * ΔA * V
9+
if !iszerotangent(ΔD)
10+
diagview(ΔD) .= diagview(iVΔAV)
11+
end
12+
if !iszerotangent(ΔV)
13+
F = inv_safe.(transpose(diagview(D)) .- diagview(D), degeneracy_atol)
14+
K̇ = F .* iVΔAV
15+
mul!(ΔV, V, K̇, 1, 0)
16+
end
17+
return ΔDV
18+
end
19+
20+
function eig_vals_pushforward!(ΔA, A, DV, ΔD; kwargs...)
21+
return eig_pushforward!(ΔA, A, DV, (Diagonal(ΔD), nothing); kwargs...)
22+
end

‎src/pushforwards/eigh.jl‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function eigh_pushforward!(
2+
dA, A, DV, dDV;
3+
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
4+
gauge_atol::Real = default_pullback_gauge_atol(dDV[2])
5+
)
6+
D, V = DV
7+
dD, dV = dDV
8+
tmpV = V \ dA
9+
∂K = tmpV * V
10+
∂Kdiag = diag(∂K)
11+
if !iszerotangent(dD)
12+
diagview(dD) .= real.(∂Kdiag)
13+
end
14+
if !iszerotangent(dV)
15+
F = inv_safe.(transpose(diagview(D)) .- diagview(D), degeneracy_atol)
16+
diagview(F) .= zero(eltype(F))
17+
∂K .*= F
18+
∂V = mul!(tmpV, V, ∂K)
19+
copyto!(dV, ∂V)
20+
end
21+
return (dD, dV)
22+
end
23+
24+
function eigh_vals_pushforward!(ΔA, A, DV, ΔD; kwargs...)
25+
return eigh_pushforward!(ΔA, A, DV, (Diagonal(ΔD), nothing); kwargs...)
26+
end

‎test/enzyme/eig.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ is_buildkite = get(ENV, "BUILDKITE", "false") == "true"
1212

1313
m = 19
1414
for T in (BLASFloats..., GenericFloats...)
15-
TestSuite.seed_rng!(1234)
15+
TestSuite.seed_rng!(123456)
1616
if !is_buildkite
1717
TestSuite.test_enzyme_eig(T, (m, m); atol = m * m * TestSuite.precision(T), rtol = m * m * TestSuite.precision(T))
1818
AT = Diagonal{T, Vector{T}}

‎test/testsuite/enzyme/eigh.jl‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,44 @@ end
1515
"""
1616
test_enzyme_eigh_full(T, sz; rng, atol, rtol)
1717
18-
Test the Enzyme reverse-mode AD rule for `eigh_full` and its in-place variant.
18+
Test the Enzyme forward- and reverse-mode AD rule for `eigh_full` and its in-place variant.
1919
"""
2020
function test_enzyme_eigh_full(
2121
T, sz;
2222
rng = Random.default_rng(), atol::Real = 0, rtol::Real = precision(T),
2323
fdm = enzyme_fdm(T)
2424
)
25-
return @testset "eigh_full reverse: RT $RT, TA $TA" for RT in (Duplicated,), TA in (Duplicated,)
25+
return @testset "eigh_full: RT $RT, TA $TA" for RT in (Duplicated,), TA in (Duplicated,)
2626
A = make_eigh_matrix(T, sz)
2727
alg = MatrixAlgebraKit.select_algorithm(eigh_full, A)
2828
DV, ΔDV = ad_eigh_full_setup(A)
2929
test_reverse(eigh_wrapper, RT, (eigh_full, Const), (A, TA), (alg, Const); atol, rtol, output_tangent = ΔDV, fdm)
3030
test_reverse(eigh!_wrapper, RT, (eigh_full!, Const), (A, TA), (alg, Const); atol, rtol, output_tangent = ΔDV, fdm)
31+
A = make_eigh_matrix(T, sz)
32+
test_forward(eigh_wrapper, RT, (eigh_full, Const), (A, TA), (alg, Const); atol, rtol, fdm)
33+
test_forward(eigh!_wrapper, RT, (eigh_full!, Const), (A, TA), (alg, Const); atol, rtol, fdm)
3134
end
3235
end
3336

3437
"""
3538
test_enzyme_eigh_vals(T, sz; rng, atol, rtol)
3639
37-
Test the Enzyme reverse-mode AD rule for `eigh_vals` and its in-place variant.
40+
Test the Enzyme forward- and reverse-mode AD rule for `eigh_vals` and its in-place variant.
3841
"""
3942
function test_enzyme_eigh_vals(
4043
T, sz;
4144
rng = Random.default_rng(), atol::Real = 0, rtol::Real = precision(T),
4245
fdm = enzyme_fdm(T)
4346
)
44-
return @testset "eigh_vals reverse: RT $RT, TA $TA" for RT in (Duplicated,), TA in (Duplicated,)
47+
return @testset "eigh_vals: RT $RT, TA $TA" for RT in (Duplicated,), TA in (Duplicated,)
4548
A = make_eigh_matrix(T, sz)
4649
alg = MatrixAlgebraKit.select_algorithm(eigh_vals, A)
4750
D, ΔD = ad_eigh_vals_setup(A)
4851
test_reverse(eigh_wrapper, RT, (eigh_vals, Const), (A, TA), (alg, Const); atol, rtol, output_tangent = ΔD, fdm)
4952
test_reverse(eigh!_wrapper, RT, (eigh_vals!, Const), (A, TA), (alg, Const); atol, rtol, output_tangent = ΔD, fdm)
53+
A = make_eigh_matrix(T, sz)
54+
test_forward(eigh_wrapper, RT, (eigh_vals, Const), (A, TA), (alg, Const); atol, rtol, fdm)
55+
test_forward(eigh!_wrapper, RT, (eigh_vals!, Const), (A, TA), (alg, Const); atol, rtol, fdm)
5056
end
5157
end
5258

‎test/testsuite/mooncake/eig.jl‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ end
1515
"""
1616
test_mooncake_eig_full(T, sz; rng, atol, rtol)
1717
18-
Test the Mooncake reverse-mode AD rule for `eig_full` and its in-place variant.
18+
Test the Mooncake forward- and reverse-mode AD rule for `eig_full` and its in-place variant.
1919
"""
2020
function test_mooncake_eig_full(
2121
T, sz;
@@ -29,25 +29,39 @@ function test_mooncake_eig_full(
2929

3030
Mooncake.TestUtils.test_rule(
3131
rng, eig_full, A, alg;
32-
mode = Mooncake.ReverseMode, output_tangent, atol, rtol
32+
mode = Mooncake.ReverseMode,
33+
output_tangent, atol, rtol
3334
)
35+
Mooncake.TestUtils.test_rule(
36+
rng, call_and_zero!, eig_full!, A, alg;
37+
mode = Mooncake.ReverseMode,
38+
output_tangent, atol, rtol, is_primitive = false
39+
)
40+
if !(eltype(T) <: ComplexF64)
41+
Mooncake.TestUtils.test_rule(
42+
rng, eig_full, A, alg;
43+
mode = Mooncake.ForwardMode,
44+
atol, rtol
45+
)
46+
Mooncake.TestUtils.test_rule(
47+
rng, call_and_zero!, eig_full!, A, alg;
48+
mode = Mooncake.ForwardMode,
49+
atol, rtol, is_primitive = false
50+
)
51+
end
3452
if T <: Diagonal{<:Complex}
3553
Mooncake.TestUtils.test_rule(
3654
rng, eig_full!, A, (A, DV[2]), alg;
37-
mode = Mooncake.ReverseMode, output_tangent, atol, rtol
55+
output_tangent, atol, rtol
3856
)
3957
end
40-
Mooncake.TestUtils.test_rule(
41-
rng, call_and_zero!, eig_full!, A, alg;
42-
mode = Mooncake.ReverseMode, output_tangent, atol, rtol, is_primitive = false
43-
)
4458
end
4559
end
4660

4761
"""
4862
test_mooncake_eig_vals(T, sz; rng, atol, rtol)
4963
50-
Test the Mooncake reverse-mode AD rule for `eig_vals` and its in-place variant.
64+
Test the Mooncake forward- and reverse-mode AD rule for `eig_vals` and its in-place variant.
5165
"""
5266
function test_mooncake_eig_vals(
5367
T, sz;
@@ -61,17 +75,17 @@ function test_mooncake_eig_vals(
6175

6276
Mooncake.TestUtils.test_rule(
6377
rng, eig_vals, A, alg;
64-
mode = Mooncake.ReverseMode, output_tangent, atol, rtol
78+
output_tangent, atol, rtol
6579
)
6680
if T <: Diagonal{<:Complex}
6781
Mooncake.TestUtils.test_rule(
6882
rng, eig_vals!, A, A.diag, alg;
69-
mode = Mooncake.ReverseMode, output_tangent, atol, rtol
83+
output_tangent, atol, rtol
7084
)
7185
end
7286
Mooncake.TestUtils.test_rule(
7387
rng, call_and_zero!, eig_vals!, A, alg;
74-
mode = Mooncake.ReverseMode, output_tangent, atol, rtol, is_primitive = false
88+
output_tangent, atol, rtol, is_primitive = false
7589
)
7690
end
7791
end

‎test/testsuite/mooncake/eigh.jl‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ function test_mooncake_eigh_full(
3535
rng, eigh!_wrapper, eigh_full!, A, alg;
3636
mode = Mooncake.ReverseMode, output_tangent, is_primitive = false, atol, rtol
3737
)
38+
if !(eltype(T) <: Complex)
39+
Mooncake.TestUtils.test_rule(
40+
rng, eigh_wrapper, eigh_full, A, alg;
41+
mode = Mooncake.ForwardMode, output_tangent, is_primitive = false, atol, rtol
42+
)
43+
Mooncake.TestUtils.test_rule(
44+
rng, eigh!_wrapper, eigh_full!, A, alg;
45+
mode = Mooncake.ForwardMode, output_tangent, is_primitive = false, atol, rtol
46+
)
47+
end
3848
end
3949
end
4050

@@ -55,11 +65,11 @@ function test_mooncake_eigh_vals(
5565

5666
Mooncake.TestUtils.test_rule(
5767
rng, eigh_wrapper, eigh_vals, A, alg;
58-
mode = Mooncake.ReverseMode, output_tangent, is_primitive = false, atol, rtol
68+
output_tangent, is_primitive = false, atol, rtol
5969
)
6070
Mooncake.TestUtils.test_rule(
6171
rng, eigh!_wrapper, eigh_vals!, A, alg;
62-
mode = Mooncake.ReverseMode, output_tangent, is_primitive = false, atol, rtol
72+
output_tangent, is_primitive = false, atol, rtol
6373
)
6474
end
6575
end

0 commit comments

Comments
 (0)