Skip to content

Smith series tolerance of the truncated pullbacks scaling like ΔA - #328

Open
leburgel wants to merge 5 commits into
mainfrom
lb/smith_rtol
Open

leburgel wants to merge 5 commits into
mainfrom
lb/smith_rtol

Conversation

@leburgel

@leburgel leburgel commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

svd_trunc_pullback!, eigh_trunc_pullback! and eig_trunc_pullback! stop the Smith series for the part outside the kept vectors once its terms drop below degeneracy_atol. That tolerance scales like the singular values or eigenvalues, while the terms scale like ΔA, i.e. inversely with them. The result therefore depends on the scale of A: for A scaled by 2^20 or more, the series stops after too few terms and ΔA is off by 1e-3 (svd, eigh) up to 2e-1 (eig) on the reproducer below.

This PR adds a keyword complement_atol to the three pullbacks. Its default, default_pullback_complement_atol, is eps(T)^(3/4) times the largest entry of the cotangents that scale like ΔA: ΔD and ΔV ./ transpose(D) (eig, eigh), or ΔS, ΔU ./ transpose(S) and ΔVᴴ ./ S (svd). The pullbacks then scale exactly with A, with errors of 1e-15 (eig: 5e-12) at every scale.

Reproducer

c ΔA(c A) against the full pullback at A, with c a power of two so the factors scale exactly (ΔS / c for c A), for spectra with kept values in [1, 10] and discarded ones at most 0.5 (main against this PR, Julia 1.12):

# The truncated pullbacks of A and of c A, c a power of two (the factors scale exactly): with the cotangents of the
# same function (ΔS / c for c A), c ΔA(c A) should equal ΔA(A), which the full pullback gives as reference.
# Spectra with a clear gap: kept values in [1, 10], discarded at most 0.5 (eig: by magnitude, complex).
using LinearAlgebra, Random, Printf
using MatrixAlgebraKit
using MatrixAlgebraKit: svd_trunc_pullback!, svd_pullback!, eigh_trunc_pullback!, eigh_pullback!,
    eig_trunc_pullback!, eig_pullback!

relerr(a, b) = norm(a - b) / norm(b)
unitary(rng, n) = Matrix(qr(randn(rng, ComplexF64, n, n)).Q)
const CS = (2.0^-40, 2.0^-20, 1.0, 2.0^20, 2.0^40)

function report(name, f, ref)
    errs = [c => relerr(c * f(c), ref) for c in CS]
    println(@sprintf("%-22s", name), join([@sprintf("c = 2^%-3d %.1e", round(Int, log2(c)), e) for (c, e) in errs], "   "))
    return flush(stdout)
end

function svd_case(m, n, k; rng = Xoshiro(1))
    r = min(m, n)
    σ = vcat(sort(1 .+ 9 .* rand(rng, k); rev = true), sort(0.5 .* rand(rng, r - k); rev = true))
    U, V = unitary(rng, m)[:, 1:r], unitary(rng, n)[:, 1:r]
    A = U * Diagonal(σ) * V'
    ΔU, ΔS, ΔVᴴ = randn(rng, ComplexF64, m, k), randn(rng, k), randn(rng, ComplexF64, k, n)
    trunc(c) = svd_trunc_pullback!(
        zeros(ComplexF64, m, n), c * A, (U[:, 1:k], Diagonal(c * σ[1:k]), V[:, 1:k]'),
        (copy(ΔU), Diagonal(ΔS / c), copy(ΔVᴴ)); gauge_atol = Inf
    )
    ref = svd_pullback!(
        zeros(ComplexF64, m, n), A, (U, Diagonal(σ), V'),
        (hcat(ΔU, zeros(m, r - k)), Diagonal(vcat(ΔS, zeros(r - k))), vcat(ΔVᴴ, zeros(r - k, n))); gauge_atol = Inf
    )
    return report("svd $m×$n, k = $k", trunc, ref)
end

function eigh_case(n, k; rng = Xoshiro(2))
    λ = vcat((1 .+ 9 .* rand(rng, k)) .* rand(rng, (-1, 1), k), 0.5 .* (2 .* rand(rng, n - k) .- 1))
    Q = unitary(rng, n)
    A = Hermitian(Q * Diagonal(λ) * Q') |> Matrix
    ΔD, ΔV = randn(rng, k), randn(rng, ComplexF64, n, k)
    trunc(c) = eigh_trunc_pullback!(
        zeros(ComplexF64, n, n), c * A, (Diagonal(c * λ[1:k]), Q[:, 1:k]), (Diagonal(ΔD / c), copy(ΔV)); gauge_atol = Inf
    )
    ref = eigh_pullback!(
        zeros(ComplexF64, n, n), A, (Diagonal(λ), Q), (Diagonal(vcat(ΔD, zeros(n - k))), hcat(ΔV, zeros(n, n - k)));
        gauge_atol = Inf
    )
    return report("eigh $n×$n, k = $k", trunc, ref)
end

function eig_case(n, k; rng = Xoshiro(3))
    phase(j) = cis.(2π .* rand(rng, j))
    λ = vcat((1 .+ 9 .* rand(rng, k)) .* phase(k), 0.5 .* rand(rng, n - k) .* phase(n - k))
    W = randn(rng, ComplexF64, n, n) + 4I # well conditioned
    W ./= sqrt.(sum(abs2, W; dims = 1))
    A = W * Diagonal(λ) / W
    ΔD, ΔV = randn(rng, ComplexF64, k), randn(rng, ComplexF64, n, k)
    # gauge invariant (vᵢᴴ Δvᵢ = 0), so that the full and the truncated pullback do not drop different gauge parts
    ΔV .-= W[:, 1:k] .* sum(conj.(W[:, 1:k]) .* ΔV; dims = 1)
    trunc(c) = eig_trunc_pullback!(
        zeros(ComplexF64, n, n), c * A, (Diagonal(c * λ[1:k]), W[:, 1:k]), (Diagonal(ΔD / c), copy(ΔV)); gauge_atol = Inf
    )
    ref = eig_pullback!(
        zeros(ComplexF64, n, n), A, (Diagonal(λ), W), (Diagonal(vcat(ΔD, zeros(n - k))), hcat(ΔV, zeros(n, n - k)));
        gauge_atol = Inf
    )
    return report("eig $n×$n, k = $k", trunc, ref)
end

println("=== ", ARGS[1], ": ‖c ΔA(c A) − ΔA_full(A)‖ / ‖ΔA_full(A)‖")
svd_case(150, 200, 20)
svd_case(200, 150, 20)
eigh_case(200, 20)
eig_case(200, 20)
=== main: ‖c ΔA(c A) − ΔA_full(A)‖ / ‖ΔA_full(A)‖
svd 150×200, k = 20   c = 2^-40 1.2e-15   c = 2^-20 1.2e-15   c = 2^0   4.5e-14   c = 2^20  1.7e-03   c = 2^40  1.7e-03
svd 200×150, k = 20   c = 2^-40 1.2e-15   c = 2^-20 1.2e-15   c = 2^0   5.4e-14   c = 2^20  2.7e-03   c = 2^40  2.7e-03
eigh 200×200, k = 20  c = 2^-40 8.6e-16   c = 2^-20 8.6e-16   c = 2^0   1.1e-15   c = 2^20  2.4e-03   c = 2^40  2.4e-03
eig 200×200, k = 20   c = 2^-40 4.6e-12   c = 2^-20 4.6e-12   c = 2^0   4.6e-12   c = 2^20  4.2e-05   c = 2^40  2.1e-01
=== this PR: ‖c ΔA(c A) − ΔA_full(A)‖ / ‖ΔA_full(A)‖
svd 150×200, k = 20   c = 2^-40 1.2e-15   c = 2^-20 1.2e-15   c = 2^0   1.2e-15   c = 2^20  1.2e-15   c = 2^40  1.2e-15
svd 200×150, k = 20   c = 2^-40 1.2e-15   c = 2^-20 1.2e-15   c = 2^0   1.2e-15   c = 2^20  1.2e-15   c = 2^40  1.2e-15
eigh 200×200, k = 20  c = 2^-40 1.1e-15   c = 2^-20 1.1e-15   c = 2^0   1.1e-15   c = 2^20  1.1e-15   c = 2^40  1.1e-15
eig 200×200, k = 20   c = 2^-40 4.6e-12   c = 2^-20 4.6e-12   c = 2^0   4.6e-12   c = 2^20  4.6e-12   c = 2^40  4.6e-12

🤖 Generated with Claude Code

leburgel and others added 2 commits November 18, 2026 13:51
…ht-hand side (stein_rtol) instead of degeneracy_atol

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leburgel
leburgel requested a review from Jutho November 18, 2026 14:46
@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.00000% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/pullbacks/eig.jl 50.00% 1 Missing ⚠️
src/pullbacks/eigh.jl 50.00% 1 Missing ⚠️
src/pullbacks/svd.jl 66.66% 1 Missing ⚠️
Files with missing lines Coverage Δ
src/common/defaults.jl 81.48% <100.00%> (+17.19%) ⬆️
src/pullbacks/eig.jl 94.31% <50.00%> (ø)
src/pullbacks/eigh.jl 86.90% <50.00%> (ø)
src/pullbacks/svd.jl 95.02% <66.66%> (ø)

... and 14 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/pullbacks/eigh.jl Outdated
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]),
complement_rtol::Real = eps(real(eltype(ΔA)))^(3 / 4)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is somewhat inconsistent that we use a relative tolerance for this tolerance, whereas we use an absolute tolerance for all the other parameters.

So I would suggest complement_atol, with a default value that scales with the size of the adjoints, i.e. complement_atol::Real = default_pullback_gauge_atol(ΔDV...), or with new default_pullback_complement_atol definition that is similar to default_pullback_gauge_atol.

The advantage of that approach is that, we could have that X₀ is very small, because the adjoints are of that nature that they are completely captured in the current subspace. So it could be that e.g. ΔD and ΔV are order 1, but that the resulting X₀ is small. It then doesn't make much sense to compute the series so that X is converged very precisely relative to the size of X₀.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That being said, I do agree that the previous atol passed to acccelartive_smith_iteration was completely wrong, since that was degeneracy_atol, with scales with the size of the primal values (i.e. D in this case) and not with the size of the adjoints. The only reasonable previous choice would have been gauge_atol, but a new parameter is certainly fine.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked explicitly, and the case where X₀ did require one or two more doublings than actually needed when using the relative tolerance. Switching to an absolute difference fixes this. The issue with choosing the default similar to default_pullback_gauge_atol(ΔDV...) is that the terms in the series scale like ΔA, not like ΔV. So this choice would reintroduce the issues that arise when scaling A.

I added complement_atol with a new default_pullback_complement_atol, built from cotangents that actually scale like ΔA (ΔD and ΔV ./ D for the eigh case, and ΔS, ΔU ./ S and ΔVᴴ ./ S for the SVD case). I also chose to multiply with eps^(3/4) * x rather than eps(x)^(3/4) like the other defaults. eps(x)^(3/4) only grows like x^(3/4), so the tolerance would still drift slightly with the scale of A.

@leburgel leburgel changed the title Smith series tolerance of the truncated pullbacks relative to the right-hand side Smith series tolerance of the truncated pullbacks scaling like ΔA Nov 25, 2026
Comment thread src/common/defaults.jl
default_pullback_gauge_atol(A) = iszerotangent(A) ? 0 : eps(norm(A, Inf))^(3 / 4)
function default_pullback_gauge_atol(A, As...)
As′ = filter(!iszerotangent, (A, As...))
return isempty(As′) ? 0 : eps(norm(As′, Inf))^(3 / 4)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this change, but because of how norm works in Julia (i.e. the recursive norm( list, Inf) does not apply norm(x, Inf) on the elements x in list, but just norm(x). Hence, I would suggest to change this to
eps(maximum(y->norm(y, Inf), As′))

Comment thread src/common/defaults.jl
x = zero(real(float(eltype(d))))
iszerotangent(ΔD) || (x = max(x, norm(ΔD, Inf)))
iszerotangent(ΔV) || (x = max(x, norm(ΔV ./ transpose(d), Inf)))
return eps(typeof(x))^(3 / 4) * x

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this is different from eps(x)^(3 / 4), so if this is better, we should probably use it consistently in the other default_..._atol functions as well. Also, the behavior to divide by ΔV by D, is that something that 🤖 would also recommend for the default_pullback_gauge_atol?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was just checking the same thing. The gauge tolerance default actually used to look like eps(T)^(3/4) * max(n, 1), which is nicely linear in the norm. This was changed in #92 to resolve some accuracy issues, but I think it's better to switch back to multiplying the norm with eps(T)^(3/4).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants