Skip to content

[WIP] Solve the truncated svd and eigh pullbacks by conjugate gradients - #296

Draft
leburgel wants to merge 8 commits into
mainfrom
lb/trunc_pullback_cg
Draft

leburgel wants to merge 8 commits into
mainfrom
lb/trunc_pullback_cg

Conversation

@leburgel

@leburgel leburgel commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

Draft.

For every kept column i, svd_trunc_pullback! and eigh_trunc_pullback! solve (1 - wᵢ G) xᵢ = bᵢ with G Hermitian (SVD: G = P Pᴴ or Pᴴ P, whichever is smaller, and wᵢ = 1/σᵢ²; eigh: G = P and wᵢ = 1/λᵢ). main solves it by doubling, at O(n³) per squaring of G. This PR uses conjugate gradients on all columns at once, at O(n² k) per iteration for k kept columns, which pays off when k ≪ n (which is common in the context of CTMRG runs in PEPSKit.jl, which motivated this investigation).

  • The conjugate-gradient loop follows KrylovKit's CG, batched over the columns.
  • Each column is solved for its right-hand side normalized, and stops once its residual is below complement_rtol times the smallest Ritz value, so that its error is about complement_rtol relative to its right-hand side. complement_rtol (default eps(T)^(3/4)) is a new keyword, as in Smith series tolerance of the truncated pullbacks scaling like ΔA #328, which fixes the same tolerance for doubling: main stops at degeneracy_atol, which is in the units of the singular values.
  • maxiter now caps the conjugate-gradient iterations (default 10 times the dimension), with a warning if they do not converge.
  • SVD: G is applied as two products until forming it pays off. eigh: G = P is indefinite, so long runs restart on (1 - wᵢ² P²) xᵢ = (1 + wᵢ P) bᵢ, which needs about half the iterations.
  • The solver, hermitian_stein_cg!, goes into a new file src/common/stein.jl, together with accelerative_smith_iteration!, moved there unchanged (still used by eig_trunc_pullback!).

Timed against main's solver (doubling, at main's tolerance) on the same equations, in one process with the calls interleaved, on 204 cases in real and complex arithmetic (script below, BENCH_MINTOTAL=1), on a shared 80-thread workstation with 4 BLAS threads:

set dec speed-up: median (range) slower largest error (PR / main)
CTMRG-like svd 2.60× (0.88–5.96) 1/8 3.4e-14 / 1.0e-14
CTMRG-like eigh 3.81× (0.88–7.25) 1/8 2.6e-13 / 2.4e-13
sweep svd 1.76× (0.79–5.03) 9/64 1.6e-11 / 1.6e-11
sweep eigh 1.58× (0.59–6.04) 10/64 2.4e-09 / 2.4e-09
randn svd 0.87× (0.51–1.70) 21/30 3.7e-14 / 6.5e-13
randn eigh 0.85× (0.43–1.85) 25/30 5.5e-14 / 1.1e-12
  • CTMRG-like: n = χD² for (D, χ) from (3, 20) to (6, 80), k = χ, values decaying by 0.98 (SVD) or 0.956 (eigh) per value. Sweep: n = 400, 1200, k/n from 1/64 to 1/2, ratio at the cut from 0.9 to 0.9999. randn: n = 100, 400, 1000, k from n/19 to 17n/19.
  • On random matrices this PR is slower on median: 46 of 60 cases, down to 0.43× at n = 100 (calls under 2 ms) and 0.61× for n ≥ 400. The same comparison for the previous head of this PR, with the tolerance taken from degeneracy_atol, gave set medians within about 20% of these (2.43×, 3.16×, 1.67×, 1.65×, 0.93×, 0.89× in the order of the table).

eig_trunc_pullback! keeps doubling. Its G = APᴴ is not Hermitian, so conjugate gradients do not apply, and for eigenvalues spread over a disk no Krylov method converges faster than the Neumann series itself.

Benchmark script

Run with this branch developed; the numbers above used BENCH_MINTOTAL=1 (21 minutes).

# Time svd_trunc_pullback! / eigh_trunc_pullback! of this PR ("auto") against main's solver, forced
# on this branch ("dbl": `accelerative_smith_iteration!` on the full right-hand side), in one process with the
# calls interleaved, on CTMRG-like spectra, a sweep of the ratio at the cut and random matrices.
# Error relative to the full pullback with the truncation indices. BENCH_MINTOTAL: seconds per solver.
using LinearAlgebra, Random, Printf
using MatrixAlgebraKit
using MatrixAlgebraKit: diagview, findtruncated, svd_pullback!, svd_trunc_pullback!,
    eigh_pullback!, eigh_trunc_pullback!, remove_svd_gauge_dependence!, remove_eigh_gauge_dependence!
BLAS.set_num_threads(parse(Int, get(ENV, "BENCH_BLAS", "4")))
const SETS = split(get(ENV, "BENCH_SETS", "ctmrg,sweep,scaled"), ",")
const MINTOTAL = parse(Float64, get(ENV, "BENCH_MINTOTAL", "3"))
const MODE = Ref(:auto)
const DEC = Ref(:svd)
# main's tolerance, the default `degeneracy_atol` = eps(largest kept value)^(3/4), from w = 1/σ² (svd) or 1/λ (eigh)
main_atol(w) = eps(Main.DEC[] === :svd ? 1 / sqrt(minimum(w)) : 1 / minimum(abs, w))^(3 / 4)
# "dbl": main's solver, `accelerative_smith_iteration!` (maxiter 100, main's default), on the same equation with
# main's tolerance
@eval MatrixAlgebraKit function hermitian_stein_cg!(X::AbstractMatrix, applyG!, formG, w::AbstractVector, atol::Real, maxiter::Int; kwargs...)
    Main.MODE[] === :dbl && return accelerative_smith_iteration!(X, similar(X), formG(), copy(w), Main.main_atol(w), 100)
    return invoke(hermitian_stein_cg!, Tuple{Any, Any, Any, Any, Any, Any}, X, applyG!, formG, w, atol, maxiter; kwargs...)
end
const MODES = (:auto, :dbl)
# cases to run, by position ("all" by default)
const CASES = get(ENV, "BENCH_CASES", "all") == "all" ? Set(1:10_000) : Set(parse.(Int, split(ENV["BENCH_CASES"], ",")))
const COUNT = Ref(0)
function interleaved(f)
    T = Dict(m => Float64[] for m in MODES)
    for m in keys(T)
        MODE[] = m; f()
    end
    for _ in 1:200
        for m in MODES
            MODE[] = m; s = time_ns(); f(); push!(T[m], (time_ns() - s) / 1.0e9)
        end
        all(sum(t) > MINTOTAL for t in values(T)) && break
    end
    MODE[] = :auto
    return T
end
median(x) = (y = sort(x); n = length(y); isodd(n) ? y[(n + 1) ÷ 2] : (y[n ÷ 2] + y[n ÷ 2 + 1]) / 2)
relerr(a, b) = norm(a - b) / norm(b)
function out(set, dec, A, p, ρ, f, ref)
    T = interleaved(f)
    e = Dict{Symbol, Float64}()
    for m in MODES
        MODE[] = m; e[m] = relerr(f(), ref)
    end
    MODE[] = :auto
    @printf("RETIME case=%d set=%s dec=%s T=%s n=%d p=%d rho=%.4f rounds=%d", COUNT[], set, dec, eltype(A), size(A, 1), p, ρ, length(T[:auto]))
    for m in MODES
        @printf(" %s_min=%.3e %s_med=%.3e err_%s=%.2e", m, minimum(T[m]), m, median(T[m]), m, e[m])
    end
    println()
    flush(stdout)
end
function run_svd(set, A, ind, rng)
    if !((COUNT[] += 1) in CASES)
        randn!(rng, similar(A)); randn!(rng, similar(A)); randn(rng, real(eltype(A)), size(A, 1))
        return
    end
    DEC[] = :svd
    U, S, Vᴴ = svd_compact(A)
    ΔU, ΔVᴴ = remove_svd_gauge_dependence!(randn!(rng, similar(U)), randn!(rng, similar(Vᴴ)), U, S, Vᴴ)
    ΔS = Diagonal(randn(rng, real(eltype(A)), size(S, 1)))
    trunc = (U[:, ind], Diagonal(diagview(S)[ind]), Vᴴ[ind, :])
    Δt = (ΔU[:, ind], Diagonal(diagview(ΔS)[ind]), ΔVᴴ[ind, :])
    p = length(ind); ρ = diagview(S)[p + 1] / diagview(S)[p]
    ref = svd_pullback!(zero(A), A, (U, S, Vᴴ), Δt, ind)
    f() = svd_trunc_pullback!(zero(A), A, trunc, Δt)
    out(set, "svd", A, p, ρ, f, ref)
end
function run_eigh(set, H, ind, rng)
    if !((COUNT[] += 1) in CASES)
        randn!(rng, similar(H)); randn(rng, real(eltype(H)), size(H, 1))
        return
    end
    DEC[] = :eigh
    D, V = eigh_full(H)
    ΔV = remove_eigh_gauge_dependence!(randn!(rng, similar(V)), D, V)
    ΔD = Diagonal(randn(rng, real(eltype(H)), size(D, 1)))
    trunc = (Diagonal(diagview(D)[ind]), V[:, ind]); Δt = (Diagonal(diagview(ΔD)[ind]), ΔV[:, ind])
    rest = setdiff(axes(D, 1), ind); p = length(ind)
    ρ = maximum(abs, diagview(D)[rest]) / minimum(abs, diagview(D)[ind])
    ref = eigh_pullback!(zero(H), H, (D, V), Δt, ind)
    f() = eigh_trunc_pullback!(zero(H), H, trunc, Δt)
    out(set, "eigh", H, p, ρ, f, ref)
end
println("MatrixAlgebraKit from ", pathof(MatrixAlgebraKit), ", BLAS threads ", BLAS.get_num_threads())
rng = Xoshiro(123)   # matrices
crng = Xoshiro(456)  # cotangents
for T in (Float64, ComplexF64)
    if "ctmrg" in SETS
        for (D, χ) in ((3, 20), (4, 40), (5, 60), (6, 80))
            n = χ * D^2
            Q1, Q2 = Matrix(qr(randn(rng, T, n, n)).Q), Matrix(qr(randn(rng, T, n, n)).Q)
            A = Q1 * Diagonal(max.(0.98 .^ (0:(n - 1)), 1.0e-10)) * Q2'
            run_svd("ctmrg", A, findtruncated(svdvals(A), truncrank(χ)), crng)
            λ = max.(0.956 .^ (0:(n - 1)), 1.0e-10) .* rand(rng, (-1, 1), n)
            H = Matrix(Hermitian(Q1 * Diagonal(λ) * Q1'))
            run_eigh("ctmrg", H, findtruncated(eigh_vals(H), truncrank(χ; by = abs)), crng)
        end
    end
    if "sweep" in SETS
        for n in (400, 1200), f in (64, 16, 4, 2), ρc in (0.9, 0.99, 0.999, 0.9999)
            p = n ÷ f
            σ = vcat(0.99 .^ (0:(p - 1)), 0.99^(p - 1) * ρc .* 0.99 .^ (0:(n - p - 1)))
            Q1, Q2 = Matrix(qr(randn(rng, T, n, n)).Q), Matrix(qr(randn(rng, T, n, n)).Q)
            run_svd("sweep", Q1 * Diagonal(σ) * Q2', collect(1:p), crng)
            H = Matrix(Hermitian(Q1 * Diagonal(σ .* rand(rng, (-1, 1), n)) * Q1'))
            run_eigh("sweep", H, findtruncated(eigh_vals(H), truncrank(p; by = abs)), crng)
        end
    end
    if "scaled" in SETS
        for m in (100, 400, 1000)
            A = randn(rng, T, m, m)
            H = Matrix(Hermitian(randn(rng, T, m, m)))
            for f in (1, 5, 9, 13, 17)
                r = max(1, round(Int, f / 19 * m))
                run_svd("scaled", A, findtruncated(svdvals(A), truncrank(r)), crng)
                run_eigh("scaled", H, findtruncated(eigh_vals(H), truncrank(r; by = abs)), crng)
            end
        end
    end
end

@leburgel
leburgel marked this pull request as draft October 4, 2026 08:08
@leburgel
leburgel force-pushed the lb/trunc_pullback_cg branch from 1a38822 to ffa3c6a Compare October 4, 2026 08:19
@codecov

codecov Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Patch % Lines
src/common/stein.jl 97.16% 3 Missing ⚠️
Files with missing lines Coverage Δ
src/MatrixAlgebraKit.jl 100.00% <ø> (ø)
src/common/pullbacks.jl 100.00% <ø> (+5.40%) ⬆️
src/pullbacks/eigh.jl 87.05% <100.00%> (+0.15%) ⬆️
src/pullbacks/svd.jl 95.13% <100.00%> (+0.10%) ⬆️
src/common/stein.jl 97.16% <97.16%> (ø)

... and 6 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/common/stein.jl Resolved
Comment thread src/pullbacks/svd.jl Resolved
Comment thread src/common/stein.jl
exceeds `cost_square` (forming G² and the new right-hand side): its spectrum [1 - γᵢ², 1] needs
about half the iterations. A column stops once its residual is below `atol` times the smallest
Ritz value of the slowest column, so that its error is below about `atol`.
"""

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.

This is far from a "simple CG". It will take me quite some time to review it. I also don't particularly like the coding style, though I don't know to pinpoint that more precisely, or thus, what action could improve it 😄 . Maybe after I understand the code I will have some suggestions.

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.

This was more a placeholder for pointing out that CG is a good Stein solver for these specific cases. Other than requesting it to be "as close to the KrylovKit CG as possible", I didn't really check the actual implementation. I'm perfectly fine with replacing the entire solver, I just didn't have the expertise to write a good one myself.

@leburgel
leburgel force-pushed the lb/trunc_pullback_cg branch 2 times, most recently from 36b10e5 to 965274f Compare October 8, 2026 06:26
@leburgel
leburgel force-pushed the lb/trunc_pullback_cg branch from 965274f to d38116e Compare November 17, 2026 13:30
leburgel and others added 2 commits November 18, 2026 12:11
…) instead of degeneracy_atol

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leburgel
leburgel force-pushed the lb/trunc_pullback_cg branch from 84a492e to c9ffc28 Compare November 18, 2026 12:50
leburgel and others added 2 commits November 18, 2026 14:16
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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