Skip to content

Commit 2433e7c

Browse files
leburgelclaude
andauthored
Smith series tolerance of the truncated pullbacks scaling like ΔA (#328)
* Smith series tolerance of the truncated pullbacks relative to the right-hand side (stein_rtol) instead of degeneracy_atol Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Rename stein_rtol to complement_rtol Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Smith series tolerance relative to each column's right-hand side Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Absolute Smith series tolerance complement_atol, in the units of ΔA * Reword the default_pullback_complement_atol docstring * Default pullback tolerances linear in the norm; gauge default from the largest entry of each cotangent * Cache the eigendecomposition in the truncated Enzyme rule when it shares storage with A --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e0a2556 commit 2433e7c

5 files changed

Lines changed: 67 additions & 18 deletions

File tree

‎ext/MatrixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ using LinearAlgebra
3030
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_pullback_gauge_atol), ::Any, ::Any...) = true
3131
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_pullback_degeneracy_atol), ::Any) = true
3232
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_pullback_rank_atol), ::Any) = true
33+
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_pullback_complement_atol), ::Any...) = true
3334
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_hermitian_tol), ::AbstractMatrix) = true
3435

3536
#----------- NOTE about derivatives ---------
@@ -366,7 +367,9 @@ for (f, trunc_f, full_f, pb) in (
366367
) where {RT, TA, TB}
367368
# A is overwritten in the primal, but not used in the pullback, so we do not need to cache it.
368369
ret = $full_f(A.val, DV.val, alg.val.alg)
369-
cache_DV = (DV.val !== ret) || EnzymeRules.overwritten(config)[3] ? copy.(ret) : nothing
370+
# but the output may share storage with A (e.g. D for complex Diagonal input), so then we cache it
371+
A_is_DV = !isa(A, Const) && (has_equal_storage(A.val, DV.val[1]) || has_equal_storage(A.val, DV.val[2]))
372+
cache_DV = (DV.val !== ret) || A_is_DV || EnzymeRules.overwritten(config)[3] ? copy.(ret) : nothing
370373
DV′, ind = truncate($trunc_f, ret, alg.val.trunc)
371374
primal = EnzymeRules.needs_primal(config) ? DV′ : nothing
372375
dret = if EnzymeRules.needs_shadow(config)

‎src/common/defaults.jl‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,61 @@ quantity needs to be computed.
99
"""
1010
defaulttol(x::Any) = eps(real(float(one(eltype(x)))))^(2 / 3)
1111

12+
# tolerance proportional to the scale `x`
13+
_default_atol(x::Real) = eps(typeof(x))^(3 / 4) * x
14+
1215
"""
1316
default_pullback_gauge_atol(ΔA...)
1417
1518
Default tolerance for deciding to warn if incoming adjoints of a pullback rule
1619
has components that are not gauge-invariant.
1720
"""
18-
default_pullback_gauge_atol(A) = iszerotangent(A) ? 0 : eps(norm(A, Inf))^(3 / 4)
21+
default_pullback_gauge_atol(A) = iszerotangent(A) ? 0 : _default_atol(norm(A, Inf))
1922
function default_pullback_gauge_atol(A, As...)
2023
As′ = filter(!iszerotangent, (A, As...))
21-
return isempty(As′) ? 0 : eps(norm(As′, Inf))^(3 / 4)
24+
return isempty(As′) ? 0 : _default_atol(maximum(y -> norm(y, Inf), As′))
2225
end
2326

2427
"""
2528
default_pullback_degeneracy_atol(A)
2629
2730
Default tolerance for deciding when values should be considered as degenerate.
2831
"""
29-
default_pullback_degeneracy_atol(A) = eps(norm(A, Inf))^(3 / 4)
32+
default_pullback_degeneracy_atol(A) = _default_atol(norm(A, Inf))
3033

3134
"""
3235
default_pullback_rank_atol(A)
3336
3437
Default tolerance for deciding what values should be considered equal to 0.
3538
"""
36-
default_pullback_rank_atol(A) = eps(norm(A, Inf))^(3 / 4)
39+
default_pullback_rank_atol(A) = _default_atol(norm(A, Inf))
3740
default_pullback_rank_atol(A::Diagonal) = default_pullback_rank_atol(diagview(A))
3841

42+
"""
43+
default_pullback_complement_atol(D, ΔD, ΔV)
44+
default_pullback_complement_atol(S, ΔU, ΔS, ΔVᴴ)
45+
46+
Default tolerance for the series that sums the contribution from outside the kept vectors in
47+
the truncated pullbacks. The terms of that series scale like `ΔA`, so the tolerance is set by
48+
the cotangents that scale like `ΔA`: `ΔD` and `ΔV ./ transpose(D)`, or `ΔS`, `ΔU ./ transpose(S)`
49+
and `ΔVᴴ ./ S`.
50+
"""
51+
function default_pullback_complement_atol(D, ΔD, ΔV)
52+
d = diagview(D)
53+
x = zero(real(float(eltype(d))))
54+
iszerotangent(ΔD) || (x = max(x, norm(ΔD, Inf)))
55+
iszerotangent(ΔV) || (x = max(x, norm(ΔV ./ transpose(d), Inf)))
56+
return _default_atol(x)
57+
end
58+
function default_pullback_complement_atol(S, ΔU, ΔS, ΔVᴴ)
59+
s = diagview(S)
60+
x = zero(real(float(eltype(s))))
61+
iszerotangent(ΔS) || (x = max(x, norm(ΔS, Inf)))
62+
iszerotangent(ΔU) || (x = max(x, norm(ΔU ./ transpose(s), Inf)))
63+
iszerotangent(ΔVᴴ) || (x = max(x, norm(ΔVᴴ ./ s, Inf)))
64+
return _default_atol(x)
65+
end
66+
3967
"""
4068
default_hermitian_tol(A)
4169

‎src/pullbacks/eig.jl‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ end
119119
eig_trunc_pullback!(
120120
ΔA::AbstractMatrix, ΔDV, A, DV;
121121
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
122-
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
122+
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]),
123+
complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...)
123124
)
124125
125126
Adds the pullback from the truncated eigenvalue decomposition of `A` to `ΔA`, given the
@@ -136,11 +137,15 @@ For this method to work correctly, it is also assumed that the remaining eigenva
136137
A warning will be printed if the cotangents are not gauge-invariant, i.e. if the restriction
137138
of `V' * ΔV` to rows `i` and columns `j` for which `abs(D[i] - D[j]) < degeneracy_atol`, is
138139
not small compared to `gauge_atol`.
140+
141+
The contribution from outside the kept eigenvectors is summed as a series, until its terms are
142+
below `complement_atol`.
139143
"""
140144
function eig_trunc_pullback!(
141145
ΔA::AbstractMatrix, A, DV, ΔDV;
142146
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
143147
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]),
148+
complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...),
144149
maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence?
145150
)
146151

@@ -165,7 +170,7 @@ function eig_trunc_pullback!(
165170
APᴴ = mul!(complex.(A'), ViG, (V * Dmat)', -1, 1)
166171
X₀ = iszerotangent(ΔV₊) ? APᴴ * Z : mul!(ΔV₊, APᴴ, Z, 1, 1)
167172
X₀ ./= D'
168-
X = accelerative_smith_iteration!(X₀, similar(X₀), APᴴ, inv.(conj.(D)), degeneracy_atol, maxiter)
173+
X = accelerative_smith_iteration!(X₀, similar(X₀), APᴴ, inv.(conj.(D)), complement_atol, maxiter)
169174
Z .+= X
170175
if eltype(ΔA) <: Real
171176
ΔAc = mul!(APᴴ, Z, V') # recycle APᴴ
@@ -178,10 +183,11 @@ end
178183
function eig_trunc_pullback!(
179184
ΔA::Diagonal, A, DV, ΔDV;
180185
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
181-
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
186+
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]),
187+
complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...)
182188
)
183189
ΔA_full = zero!(similar(ΔA, size(ΔA)))
184-
ΔA_full = eig_trunc_pullback!(ΔA_full, A, DV, ΔDV; degeneracy_atol, gauge_atol)
190+
ΔA_full = eig_trunc_pullback!(ΔA_full, A, DV, ΔDV; degeneracy_atol, gauge_atol, complement_atol)
185191
diagview(ΔA) .+= diagview(ΔA_full)
186192
return ΔA
187193
end

‎src/pullbacks/eigh.jl‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ end
121121
eigh_trunc_pullback!(
122122
ΔA::AbstractMatrix, A, DV, ΔDV;
123123
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
124-
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
124+
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]),
125+
complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...)
125126
)
126127
127128
Adds the pullback from the truncated Hermitian eigenvalue decomposition of `A` to `ΔA`,
@@ -138,11 +139,15 @@ For this method to work correctly, it is also assumed that the remaining eigenva
138139
A warning will be printed if the cotangents are not gauge-invariant, i.e. if the restriction
139140
of `V' * ΔV` to rows `i` and columns `j` for which `abs(D[i] - D[j]) < degeneracy_atol`, is
140141
not small compared to `gauge_atol`.
142+
143+
The contribution from outside the kept eigenvectors is summed as a series, until its terms are
144+
below `complement_atol`.
141145
"""
142146
function eigh_trunc_pullback!(
143147
ΔA::AbstractMatrix, A, DV, ΔDV;
144148
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
145149
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]),
150+
complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...),
146151
maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence?
147152
)
148153

@@ -162,7 +167,7 @@ function eigh_trunc_pullback!(
162167
if !iszerotangent(ΔV₊)
163168
X₀ = rdiv!(ΔV₊, Diagonal(D))
164169
AP = mul!(copy(A), V * Dmat, V', -1, 1)
165-
X = accelerative_smith_iteration!(X₀, similar(X₀), AP, inv.(D), degeneracy_atol, maxiter)
170+
X = accelerative_smith_iteration!(X₀, similar(X₀), AP, inv.(D), complement_atol, maxiter)
166171
Z .+= X
167172
# we cannot directly multiply Z * V' into ΔA, because we have to
168173
# take the Hermitian part, and cannot apply project_hermitian! to
@@ -180,10 +185,11 @@ end
180185
function eigh_trunc_pullback!(
181186
ΔA::Diagonal, A, DV, ΔDV;
182187
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
183-
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
188+
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]),
189+
complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...)
184190
)
185191
ΔA_full = zero!(similar(ΔA, size(ΔA)))
186-
ΔA_full = eigh_trunc_pullback!(ΔA_full, A, DV, ΔDV; degeneracy_atol, gauge_atol)
192+
ΔA_full = eigh_trunc_pullback!(ΔA_full, A, DV, ΔDV; degeneracy_atol, gauge_atol, complement_atol)
187193
diagview(ΔA) .+= diagview(ΔA_full)
188194
return ΔA
189195
end

‎src/pullbacks/svd.jl‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ end
201201
ΔA, A, USVᴴ, ΔUSVᴴ;
202202
rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
203203
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
204-
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...)
204+
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...),
205+
complement_atol::Real = default_pullback_complement_atol(USVᴴ[2], ΔUSVᴴ...)
205206
)
206207
207208
Adds the pullback from the truncated SVD of `A` to `ΔA`, given the output `USVᴴ` and the
@@ -217,12 +218,16 @@ singular values (not included in `S`) are (sufficiently) smaller than those in `
217218
A warning will be printed if the cotangents are not gauge-invariant, i.e. if the
218219
anti-hermitian part of `U' * ΔU + Vᴴ * ΔVᴴ'`, restricted to rows `i` and columns `j` for
219220
which `abs(S[i] - S[j]) < degeneracy_atol`, is not small compared to `gauge_atol`.
221+
222+
The contribution from outside the kept singular vectors is summed as a series, until its terms
223+
are below `complement_atol`.
220224
"""
221225
function svd_trunc_pullback!(
222226
ΔA::AbstractMatrix, A, USVᴴ, ΔUSVᴴ;
223227
rank_atol::Real = 0,
224228
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
225229
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...),
230+
complement_atol::Real = default_pullback_complement_atol(USVᴴ[2], ΔUSVᴴ...),
226231
maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence?
227232
)
228233
# Extract the SVD components
@@ -257,15 +262,15 @@ function svd_trunc_pullback!(
257262
if m ≤ n
258263
X = rmul!(AP * Y₀ᴴ', Diagonal(S⁻¹))
259264
X .+= X₀
260-
X = accelerative_smith_iteration!(X, X₀, AP * AP', S⁻¹ .^ 2, degeneracy_atol, maxiter) # recycle X₀
265+
X = accelerative_smith_iteration!(X, X₀, AP * AP', S⁻¹ .^ 2, complement_atol, maxiter) # recycle X₀
261266
Yᴴ = lmul!(Diagonal(S⁻¹), X' * AP)
262267
Yᴴ .+= Y₀ᴴ
263268
ΔA = mul!(ΔA, X, Vᴴ, 1, 1)
264269
ΔA = mul!(ΔA, U, Yᴴ, 1, 1)
265270
else
266271
Y = rmul!(AP' * X₀, Diagonal(S⁻¹))
267272
Y .+= Y₀ᴴ'
268-
Y = accelerative_smith_iteration!(Y, similar(Y), AP' * AP, S⁻¹ .^ 2, degeneracy_atol, maxiter)
273+
Y = accelerative_smith_iteration!(Y, similar(Y), AP' * AP, S⁻¹ .^ 2, complement_atol, maxiter)
269274
X = rmul!(AP * Y, Diagonal(S⁻¹))
270275
X .+= X₀
271276
ΔA = mul!(ΔA, X, Vᴴ, 1, 1)
@@ -279,10 +284,11 @@ function svd_trunc_pullback!(
279284
ΔA::Diagonal, A, USVᴴ, ΔUSVᴴ;
280285
rank_atol::Real = 0,
281286
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
282-
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3])
287+
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3]),
288+
complement_atol::Real = default_pullback_complement_atol(USVᴴ[2], ΔUSVᴴ...)
283289
)
284290
ΔA_full = zero!(similar(ΔA, size(ΔA)))
285-
ΔA_full = svd_trunc_pullback!(ΔA_full, A, USVᴴ, ΔUSVᴴ; rank_atol, degeneracy_atol, gauge_atol)
291+
ΔA_full = svd_trunc_pullback!(ΔA_full, A, USVᴴ, ΔUSVᴴ; rank_atol, degeneracy_atol, gauge_atol, complement_atol)
286292
diagview(ΔA) .+= diagview(ΔA_full)
287293
return ΔA
288294
end

0 commit comments

Comments
 (0)