Skip to content

Commit 80c9db9

Browse files
committed
Handle eigval sorting for trunctol
1 parent db00c52 commit 80c9db9

3 files changed

Lines changed: 40 additions & 12 deletions

File tree

‎test/testsuite/ad_utils.jl‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,31 @@ In-place variant of [`eig_vals_wrapper`](@ref), which zeros `A` after calling `f
7676
"""
7777
eig_vals!_wrapper(f!, A, alg) = sort_eigvals(call_and_zero!(f!, A, alg))
7878

79+
"""
80+
eig_trunc_wrapper(f, A, alg)
81+
82+
Wrapper that sorts the eigenpairs returned by `f(A, alg)` (`(D, V)` or `(D, V, ϵ)`) by
83+
modulus and then by imaginary part of the eigenvalues. Truncation by value keeps the retained
84+
eigenpairs in LAPACK's ordering, which can change discontinuously under small perturbations
85+
of `A` and breaks finite-difference checks; see [`eig_vals_wrapper`](@ref).
86+
"""
87+
eig_trunc_wrapper(f, A, alg) = sort_eig_trunc(f(A, alg))
88+
89+
"""
90+
eig_trunc!_wrapper(f!, A, alg)
91+
92+
In-place variant of [`eig_trunc_wrapper`](@ref), which zeros `A` after calling `f!`.
93+
"""
94+
eig_trunc!_wrapper(f!, A, alg) = sort_eig_trunc(call_and_zero!(f!, A, alg))
95+
7996
# sortperm is used here because Mooncake CAN differentiate that on CUDA,
8097
# but CANNOT differentiate sort
81-
sort_eigvals(D) = D[sortperm(collect(D); by = λ -> (abs(λ), imag(λ)))]
98+
eigvals_sortperm(D) = sortperm(collect(D); by = λ -> (abs(λ), imag(λ)))
99+
sort_eigvals(D) = D[eigvals_sortperm(D)]
100+
101+
# reorder `(D, V, rest...)` (or its tangent) by the permutation `p` of the eigenvalues
102+
permute_eigpairs(DV, p) = (Diagonal(diagview(DV[1])[p]), DV[2][:, p], Base.tail(Base.tail(DV))...)
103+
sort_eig_trunc(DV) = permute_eigpairs(DV, eigvals_sortperm(diagview(DV[1])))
82104

83105
"""
84106
qr_gauge_invariant_wrapper(f, A, alg, r)

‎test/testsuite/enzyme/eig.jl‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ function test_enzyme_eig_trunc(
8585
D = eig_vals(A)
8686
trunc = trunctol(atol = midgap_tol(D); by = abs)
8787
truncalg = TruncatedAlgorithm(alg, trunc)
88-
DV, _, ΔDV, ΔDVtrunc = ad_eig_trunc_setup(A, truncalg)
89-
test_reverse(eig_trunc_no_error, RT, (A, TA), (truncalg, Const); atol, rtol, output_tangent = ΔDVtrunc, fdm)
90-
test_reverse(call_and_zero!, RT, (eig_trunc_no_error!, Const), (copy(A), TA), (truncalg, Const); atol, rtol, output_tangent = ΔDVtrunc, fdm)
88+
DV, DVtrunc, ΔDV, ΔDVtrunc = ad_eig_trunc_setup(A, truncalg)
89+
# trunctol keeps LAPACK's eigenvalue ordering, so sort the outputs (and the tangent)
90+
ΔDVtrunc = permute_eigpairs(ΔDVtrunc, eigvals_sortperm(diagview(DVtrunc[1])))
91+
test_reverse(eig_trunc_wrapper, RT, (eig_trunc_no_error, Const), (A, TA), (truncalg, Const); atol, rtol, output_tangent = ΔDVtrunc, fdm)
92+
test_reverse(eig_trunc!_wrapper, RT, (eig_trunc_no_error!, Const), (copy(A), TA), (truncalg, Const); atol, rtol, output_tangent = ΔDVtrunc, fdm)
9193
# use max range here to try to dodge issues when the gap between eigenvalues is close to the FD perturbation
92-
test_forward(eig_trunc_no_error, RT, (A, TA), (truncalg, Const); atol, rtol, fdm = EnzymeTestUtils.FiniteDifferences.central_fdm(5, 1, max_range = 1.0e-3))
93-
test_forward(call_and_zero!, RT, (eig_trunc_no_error!, Const), (copy(A), TA), (truncalg, Const); atol, rtol, fdm = EnzymeTestUtils.FiniteDifferences.central_fdm(5, 1, max_range = 1.0e-3))
94+
test_forward(eig_trunc_wrapper, RT, (eig_trunc_no_error, Const), (A, TA), (truncalg, Const); atol, rtol, fdm = EnzymeTestUtils.FiniteDifferences.central_fdm(5, 1, max_range = 1.0e-3))
95+
test_forward(eig_trunc!_wrapper, RT, (eig_trunc_no_error!, Const), (copy(A), TA), (truncalg, Const); atol, rtol, fdm = EnzymeTestUtils.FiniteDifferences.central_fdm(5, 1, max_range = 1.0e-3))
9496
end
9597
end
9698
end

‎test/testsuite/mooncake/eig.jl‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,18 @@ function test_mooncake_eig_trunc(
148148
alg_trunc = TruncatedAlgorithm(alg, trunc)
149149

150150
DV, DVtrunc, ΔDV_arrays, ΔDVtrunc_arrays = ad_eig_trunc_setup(A, alg_trunc)
151+
# trunctol keeps LAPACK's eigenvalue ordering, so sort the outputs (and the tangent)
152+
p = eigvals_sortperm(diagview(DVtrunc[1]))
153+
DVtrunc = permute_eigpairs(DVtrunc, p)
154+
ΔDVtrunc_arrays = permute_eigpairs(ΔDVtrunc_arrays, p)
151155
ΔDVtrunc = Mooncake.primal_to_tangent!!(Mooncake.zero_tangent(DVtrunc), ΔDVtrunc_arrays)
152156

153157
Mooncake.TestUtils.test_rule(
154-
rng, eig_trunc_no_error, A, alg_trunc;
155-
mode = Mooncake.ReverseMode, output_tangent = ΔDVtrunc, atol, rtol
158+
rng, eig_trunc_wrapper, eig_trunc_no_error, A, alg_trunc;
159+
mode = Mooncake.ReverseMode, output_tangent = ΔDVtrunc, atol, rtol, is_primitive = false
156160
)
157161
Mooncake.TestUtils.test_rule(
158-
rng, call_and_zero!, eig_trunc_no_error!, A, alg_trunc;
162+
rng, eig_trunc!_wrapper, eig_trunc_no_error!, A, alg_trunc;
159163
mode = Mooncake.ReverseMode, output_tangent = ΔDVtrunc, atol, rtol, is_primitive = false
160164
)
161165

@@ -164,11 +168,11 @@ function test_mooncake_eig_trunc(
164168
ΔDVϵtrunc = (ΔDVtrunc..., Δϵ)
165169

166170
Mooncake.TestUtils.test_rule(
167-
rng, eig_trunc, A, alg_trunc;
168-
mode = Mooncake.ReverseMode, output_tangent = ΔDVϵtrunc, atol, rtol
171+
rng, eig_trunc_wrapper, eig_trunc, A, alg_trunc;
172+
mode = Mooncake.ReverseMode, output_tangent = ΔDVϵtrunc, atol, rtol, is_primitive = false
169173
)
170174
Mooncake.TestUtils.test_rule(
171-
rng, call_and_zero!, eig_trunc!, A, alg_trunc;
175+
rng, eig_trunc!_wrapper, eig_trunc!, A, alg_trunc;
172176
mode = Mooncake.ReverseMode, output_tangent = ΔDVϵtrunc, atol, rtol, is_primitive = false
173177
)
174178
end

0 commit comments

Comments
 (0)