Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/utility/eigh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ function ChainRulesCore.rrule(
(D̃, Ṽ), inds = truncate(eigh_trunc!, (D, V), alg.fwd_alg.trunc)
truncerror = truncation_error(diagview(D), inds)

function eigh_trunc!_full_pullback(ΔDV)
function eigh_trunc!_full_pullback((ΔD, ΔV))
ΔDV = map(unthunk, (ΔD, ΔV))
Δt = eigh_pullback!(
zeros(storagetype(t), space(t)), t, (D, V), ΔDV, inds;
gauge_atol = _get_pullback_gauge_tol(ΔDV, alg.rrule_alg.verbosity), degeneracy_atol = alg.rrule_alg.degeneracy_atol,
Expand All @@ -333,7 +334,8 @@ function ChainRulesCore.rrule(
) where {F, R <: TruncPullback}
D, V, truncerror = eigh_trunc(t, alg)

function eigh_trunc!_trunc_pullback(ΔDV)
function eigh_trunc!_trunc_pullback((ΔD, ΔV))
ΔDV = map(unthunk, (ΔD, ΔV))
Δf = eigh_trunc_pullback!(
zeros(storagetype(t), space(t)), t, (D, V), ΔDV;
gauge_atol = _get_pullback_gauge_tol(ΔDV, alg.rrule_alg.verbosity), degeneracy_atol = alg.rrule_alg.degeneracy_atol,
Expand Down
10 changes: 6 additions & 4 deletions src/utility/orth.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ function ChainRulesCore.rrule(
QR = left_orth(t, alg)

"""Propagate QR cotangents with the configured gauge-sensitivity tolerance."""
function left_orth!_pullback(ΔQR)
function left_orth!_pullback((ΔQ, ΔR))
ΔQR = map(unthunk, (ΔQ, ΔR))
Δt = zeros(storagetype(t), space(t))
MatrixAlgebraKit.qr_pullback!(Δt, t, QR, unthunk.(ΔQR); gauge_atol = _get_pullback_gauge_tol(ΔQR, alg.rrule_alg.verbosity))
MatrixAlgebraKit.qr_pullback!(Δt, t, QR, ΔQR; gauge_atol = _get_pullback_gauge_tol(ΔQR, alg.rrule_alg.verbosity))
return NoTangent(), Δt, NoTangent()
end
function left_orth!_pullback(::Tuple{ZeroTangent, ZeroTangent})
Expand Down Expand Up @@ -129,9 +130,10 @@ function ChainRulesCore.rrule(
LQ = right_orth(t, alg)

"""Propagate LQ cotangents with the configured gauge-sensitivity tolerance."""
function right_orth!_pullback(ΔLQ)
function right_orth!_pullback((ΔL, ΔQ))
ΔLQ = map(unthunk, (ΔL, ΔQ))
Δt = zeros(storagetype(t), space(t))
MatrixAlgebraKit.lq_pullback!(Δt, t, LQ, unthunk.(ΔLQ); gauge_atol = _get_pullback_gauge_tol(ΔLQ, alg.rrule_alg.verbosity))
MatrixAlgebraKit.lq_pullback!(Δt, t, LQ, ΔLQ; gauge_atol = _get_pullback_gauge_tol(ΔLQ, alg.rrule_alg.verbosity))
return NoTangent(), Δt, NoTangent()
end
function right_orth!_pullback(::Tuple{ZeroTangent, ZeroTangent})
Expand Down
12 changes: 6 additions & 6 deletions src/utility/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ function ChainRulesCore.rrule(
result = f === svd_trunc! ?
(Ũ, S̃, Ṽᴴ, truncation_error(diagview(S), inds)) : (Ũ, S̃, Ṽᴴ)

function svd_trunc!_full_pullback(ΔUSVᴴ′)
ΔUSVᴴ = map(unthunk, ChainRulesCore.backing(ΔUSVᴴ′))
function svd_trunc!_full_pullback((ΔU, ΔS, ΔVᴴ))
ΔUSVᴴ = map(unthunk, (ΔU, ΔS, ΔVᴴ))
Δt = svd_pullback!(
zeros(storagetype(t), space(t)), t, (U, S, Vᴴ), ΔUSVᴴ, inds;
gauge_atol = _get_pullback_gauge_tol(ΔUSVᴴ, alg.rrule_alg.verbosity), degeneracy_atol = alg.rrule_alg.degeneracy_atol,
Expand Down Expand Up @@ -344,8 +344,8 @@ end

# Shared reverse pass wrapping MatrixAlgebraKit's svd_trunc_pullback! (also works for IterSVD).
function _svd_pullback(t, alg::SVDAdjoint{F, <:TruncPullback}, (U, S, Vᴴ)) where {F}
function svd_trunc!_trunc_pullback(ΔUSVᴴ′)
ΔUSVᴴ = map(unthunk, ChainRulesCore.backing(ΔUSVᴴ′))
function svd_trunc!_trunc_pullback((ΔU, ΔS, ΔVᴴ))
ΔUSVᴴ = map(unthunk, (ΔU, ΔS, ΔVᴴ))
Δf = svd_trunc_pullback!(
zeros(storagetype(t), space(t)), t, (U, S, Vᴴ), ΔUSVᴴ;
gauge_atol = _get_pullback_gauge_tol(ΔUSVᴴ, alg.rrule_alg.verbosity), degeneracy_atol = alg.rrule_alg.degeneracy_atol,
Expand All @@ -368,9 +368,9 @@ function _svd_pullback(
proper_tol = clamp(alg.rrule_alg.tol, eps(scalartype(S))^(3 / 4), 1.0e-2 * smallest_sval)
rrule_alg = @set $(alg.rrule_alg).tol = proper_tol

function svd_trunc!_itersvd_pullback(ΔUSVᴴ′)
function svd_trunc!_itersvd_pullback((ΔU, ΔS, ΔVᴴ))
Δf = similar(f)
ΔU, ΔS, ΔVᴴ, = map(unthunk, ChainRulesCore.backing(ΔUSVᴴ′))
ΔU, ΔS, ΔVᴴ, = map(unthunk, (ΔU, ΔS, ΔVᴴ))

for (c, b) in blocks(Δf)
Uc, Sc, Vᴴc = block(U, c), block(S, c), block(Vᴴ, c)
Expand Down
8 changes: 8 additions & 0 deletions test/testsuite/utility/eigh_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ function utility_eigh_wrapper(AT; default_alg = :QRIteration)
@test g_trunc[1] ≈ g_iter[1] rtol = rtol
end

@testset "Thunked eigen cotangents $(alg.rrule_alg)" for alg in (full_alg, trunc_alg)
alg = @set alg.fwd_alg = TruncatedAlgorithm(alg.fwd_alg, trunc)
(D, V, _), back = @inferred rrule(eigh_trunc!, copy(r), alg)
cot = (2D, zero(V))
thunked = Tangent{Any}(map(x -> Thunk(() -> x), cot)...)
@test (@inferred back(thunked))[2] ≈ (@inferred back(cot))[2] rtol = rtol
end

@testset "Truncated eigh broadening for $(alg.rrule_alg)" for alg in [full_alg, trunc_alg]
d, v = eigh_full(r)
d.data[1:2:n] .= d.data[2:2:n] # make every eigenvalue two-fold degenerate
Expand Down
10 changes: 9 additions & 1 deletion test/testsuite/utility/orth_wrapper.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Test, Random, LinearAlgebra
using TensorKit, MatrixAlgebraKit, PEPSKit, Zygote, Adapt
using ChainRulesCore: rrule, NoTangent, ZeroTangent
using ChainRulesCore: rrule, NoTangent, ZeroTangent, Tangent, Thunk

"""Check shared QR/LQ algorithms and pullbacks, including mixed dual fermionic legs."""
function utility_orth_wrapper(AT)
Expand All @@ -17,6 +17,14 @@ function utility_orth_wrapper(AT)
@test configured.rrule_alg === rrule_alg
@test configured == OrthAdjoint(fwd_alg, rrule_alg)

@testset "Thunked gauge tolerance $orth!" for orth! in (left_orth!, right_orth!)
A = adapt(AT, randn(ComplexF64, ℂ^4 ← ℂ^6))
factors, back = @inferred rrule(orth!, copy(A), configured)
cot = map(x -> 2x, factors)
thunked = Tangent{Any}(map(x -> Thunk(() -> x), cot)...)
@test (@inferred back(thunked))[2] ≈ (@inferred back(cot))[2]
end

@testset "$(typeof(alg.fwd_alg)), $(sectortype(V)), $nout ← $nin, $T" for alg in algorithms, V in spaces, (nout, nin) in ((2, 3), (3, 2)), T in (Float64, ComplexF64)
output, input = (prod(ntuple(i -> isodd(i) ? V : V', n)) for n in (nout, nin))
A = adapt(AT, randn(T, output ← input))
Expand Down
11 changes: 11 additions & 0 deletions test/testsuite/utility/svd_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ function utility_svd_wrapper(AT)
@test gradient ≈ reference rtol = rtol
end

@testset "Thunked cotangents $reverse, $f" for reverse in (:FullPullback, :TruncPullback, :GMRES, :Arnoldi, :BiCGStab), f in (svd_trunc!, svd_trunc_no_error!)
alg = SVDAdjoint(; rrule_alg = (; alg = reverse))
alg = @set alg.fwd_alg = TruncatedAlgorithm(alg.fwd_alg, krylov_trunc)
result, pb = @inferred rrule(f, copy(r), alg)
cot = (ZeroTangent(), 2result[2], ZeroTangent())
reference = (@inferred pb(cot))[2]
wrapped = Tangent{Any}(map(x -> Thunk(() -> x), cot)...)
@test (@inferred pb(Thunk(() -> cot)))[2] ≈ reference rtol = rtol
@test (@inferred pb(Thunk(() -> wrapped)))[2] ≈ reference rtol = rtol
end

@testset "Truncated SVD broadening for $f, $(alg.rrule_alg)" for f in (svd_trunc, svd_trunc_no_error), alg in [full_alg, trunc_alg]
u, s, v, = svd_compact(r)
s.data[1:2:m] .= s.data[2:2:m] # make every singular value two-fold degenerate
Expand Down
Loading