Skip to content

Commit 8e90e6f

Browse files
leburgelclaude
andcommitted
Conjugate-gradient tolerance relative to the right-hand side (cg_rtol) instead of degeneracy_atol
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d38116e commit 8e90e6f

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

‎src/pullbacks/eigh.jl‎

Lines changed: 7 additions & 2 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+
cg_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4)
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 solved by conjugate gradients, to an
144+
error of about `cg_rtol` relative to its right-hand side.
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+
cg_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4),
146151
maxiter::Int = 10 * size(ΔA, 1) # conjugate-gradient iterations
147152
)
148153

@@ -163,7 +168,7 @@ function eigh_trunc_pullback!(
163168
X₀ = rdiv!(ΔV₊, Diagonal(D))
164169
AP = mul!(copy(A), V * Dmat, V', -1, 1)
165170
X = hermitian_stein_cg!(
166-
X₀, nothing, () -> AP, inv.(D), degeneracy_atol, maxiter;
171+
X₀, nothing, () -> AP, inv.(D), cg_rtol * norm(X₀), maxiter;
167172
cost_apply = n^2, cost_form = 0, cost_square = n^3 + n^2 * p
168173
)
169174
Z .+= X

‎src/pullbacks/svd.jl‎

Lines changed: 8 additions & 3 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+
cg_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4)
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 solved by conjugate gradients, to
223+
an error of about `cg_rtol` relative to its right-hand side.
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+
cg_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4),
226231
maxiter::Int = 10 * minimum(size(ΔA)) # conjugate-gradient iterations
227232
)
228233
# Extract the SVD components
@@ -260,7 +265,7 @@ function svd_trunc_pullback!(
260265
APᴴZ = similar(X, n, p) # for applying AP AP' without forming it
261266
X = hermitian_stein_cg!(
262267
X, (GZ, Z) -> mul!(GZ, AP, mul!(view(APᴴZ, :, axes(Z, 2)), AP', Z)), () -> AP * AP',
263-
S⁻¹ .^ 2, degeneracy_atol, maxiter;
268+
S⁻¹ .^ 2, cg_rtol * norm(X), maxiter;
264269
cost_apply = 2 * m * n, cost_apply_formed = m^2, cost_form = m^2 * n
265270
)
266271
Yᴴ = lmul!(Diagonal(S⁻¹), X' * AP)
@@ -273,7 +278,7 @@ function svd_trunc_pullback!(
273278
APZ = similar(Y, m, p) # for applying AP' AP without forming it
274279
Y = hermitian_stein_cg!(
275280
Y, (GZ, Z) -> mul!(GZ, AP', mul!(view(APZ, :, axes(Z, 2)), AP, Z)), () -> AP' * AP,
276-
S⁻¹ .^ 2, degeneracy_atol, maxiter;
281+
S⁻¹ .^ 2, cg_rtol * norm(Y), maxiter;
277282
cost_apply = 2 * m * n, cost_apply_formed = n^2, cost_form = n^2 * m
278283
)
279284
X = rmul!(AP * Y, Diagonal(S⁻¹))

0 commit comments

Comments
 (0)