Skip to content
Open
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
25 changes: 25 additions & 0 deletions src/common/defaults.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ Default tolerance for deciding what values should be considered equal to 0.
default_pullback_rank_atol(A) = eps(norm(A, Inf))^(3 / 4)
default_pullback_rank_atol(A::Diagonal) = default_pullback_rank_atol(diagview(A))

"""
default_pullback_complement_atol(D, ΔD, ΔV)
default_pullback_complement_atol(S, ΔU, ΔS, ΔVᴴ)

Default tolerance for the series that sums the contribution from outside the kept vectors in
the truncated pullbacks. The terms of that series scale like `ΔA`, so the tolerance is set by
the cotangents that scale like `ΔA`: `ΔD` and `ΔV ./ transpose(D)`, or `ΔS`, `ΔU ./ transpose(S)`
and `ΔVᴴ ./ S`.
"""
function default_pullback_complement_atol(D, ΔD, ΔV)
d = diagview(D)
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).

end
function default_pullback_complement_atol(S, ΔU, ΔS, ΔVᴴ)
s = diagview(S)
x = zero(real(float(eltype(s))))
iszerotangent(ΔS) || (x = max(x, norm(ΔS, Inf)))
iszerotangent(ΔU) || (x = max(x, norm(ΔU ./ transpose(s), Inf)))
iszerotangent(ΔVᴴ) || (x = max(x, norm(ΔVᴴ ./ s, Inf)))
return eps(typeof(x))^(3 / 4) * x
end

"""
default_hermitian_tol(A)

Expand Down
14 changes: 10 additions & 4 deletions src/pullbacks/eig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ end
eig_trunc_pullback!(
ΔA::AbstractMatrix, ΔDV, A, DV;
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_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...)
)

Adds the pullback from the truncated eigenvalue decomposition of `A` to `ΔA`, given the
Expand All @@ -136,11 +137,15 @@ For this method to work correctly, it is also assumed that the remaining eigenva
A warning will be printed if the cotangents are not gauge-invariant, i.e. if the restriction
of `V' * ΔV` to rows `i` and columns `j` for which `abs(D[i] - D[j]) < degeneracy_atol`, is
not small compared to `gauge_atol`.

The contribution from outside the kept eigenvectors is summed as a series, until its terms are
below `complement_atol`.
"""
function eig_trunc_pullback!(
ΔA::AbstractMatrix, A, DV, ΔDV;
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]),
complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...),
maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence?
)

Expand All @@ -165,7 +170,7 @@ function eig_trunc_pullback!(
APᴴ = mul!(complex.(A'), ViG, (V * Dmat)', -1, 1)
X₀ = iszerotangent(ΔV₊) ? APᴴ * Z : mul!(ΔV₊, APᴴ, Z, 1, 1)
X₀ ./= D'
X = accelerative_smith_iteration!(X₀, similar(X₀), APᴴ, inv.(conj.(D)), degeneracy_atol, maxiter)
X = accelerative_smith_iteration!(X₀, similar(X₀), APᴴ, inv.(conj.(D)), complement_atol, maxiter)
Z .+= X
if eltype(ΔA) <: Real
ΔAc = mul!(APᴴ, Z, V') # recycle APᴴ
Expand All @@ -178,10 +183,11 @@ end
function eig_trunc_pullback!(
ΔA::Diagonal, A, DV, ΔDV;
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_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...)
)
ΔA_full = zero!(similar(ΔA, size(ΔA)))
ΔA_full = eig_trunc_pullback!(ΔA_full, A, DV, ΔDV; degeneracy_atol, gauge_atol)
ΔA_full = eig_trunc_pullback!(ΔA_full, A, DV, ΔDV; degeneracy_atol, gauge_atol, complement_atol)
diagview(ΔA) .+= diagview(ΔA_full)
return ΔA
end
Expand Down
14 changes: 10 additions & 4 deletions src/pullbacks/eigh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ end
eigh_trunc_pullback!(
ΔA::AbstractMatrix, A, DV, ΔDV;
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_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...)
)

Adds the pullback from the truncated Hermitian eigenvalue decomposition of `A` to `ΔA`,
Expand All @@ -138,11 +139,15 @@ For this method to work correctly, it is also assumed that the remaining eigenva
A warning will be printed if the cotangents are not gauge-invariant, i.e. if the restriction
of `V' * ΔV` to rows `i` and columns `j` for which `abs(D[i] - D[j]) < degeneracy_atol`, is
not small compared to `gauge_atol`.

The contribution from outside the kept eigenvectors is summed as a series, until its terms are
below `complement_atol`.
"""
function eigh_trunc_pullback!(
ΔA::AbstractMatrix, A, DV, ΔDV;
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]),
complement_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...),
maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence?
)

Expand All @@ -162,7 +167,7 @@ function eigh_trunc_pullback!(
if !iszerotangent(ΔV₊)
X₀ = rdiv!(ΔV₊, Diagonal(D))
AP = mul!(copy(A), V * Dmat, V', -1, 1)
X = accelerative_smith_iteration!(X₀, similar(X₀), AP, inv.(D), degeneracy_atol, maxiter)
X = accelerative_smith_iteration!(X₀, similar(X₀), AP, inv.(D), complement_atol, maxiter)
Z .+= X
# we cannot directly multiply Z * V' into ΔA, because we have to
# take the Hermitian part, and cannot apply project_hermitian! to
Expand All @@ -180,10 +185,11 @@ end
function eigh_trunc_pullback!(
ΔA::Diagonal, A, DV, ΔDV;
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_atol::Real = default_pullback_complement_atol(DV[1], ΔDV...)
)
ΔA_full = zero!(similar(ΔA, size(ΔA)))
ΔA_full = eigh_trunc_pullback!(ΔA_full, A, DV, ΔDV; degeneracy_atol, gauge_atol)
ΔA_full = eigh_trunc_pullback!(ΔA_full, A, DV, ΔDV; degeneracy_atol, gauge_atol, complement_atol)
diagview(ΔA) .+= diagview(ΔA_full)
return ΔA
end
Expand Down
16 changes: 11 additions & 5 deletions src/pullbacks/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ end
ΔA, A, USVᴴ, ΔUSVᴴ;
rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...)
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...),
complement_atol::Real = default_pullback_complement_atol(USVᴴ[2], ΔUSVᴴ...)
)

Adds the pullback from the truncated SVD of `A` to `ΔA`, given the output `USVᴴ` and the
Expand All @@ -217,12 +218,16 @@ singular values (not included in `S`) are (sufficiently) smaller than those in `
A warning will be printed if the cotangents are not gauge-invariant, i.e. if the
anti-hermitian part of `U' * ΔU + Vᴴ * ΔVᴴ'`, restricted to rows `i` and columns `j` for
which `abs(S[i] - S[j]) < degeneracy_atol`, is not small compared to `gauge_atol`.

The contribution from outside the kept singular vectors is summed as a series, until its terms
are below `complement_atol`.
"""
function svd_trunc_pullback!(
ΔA::AbstractMatrix, A, USVᴴ, ΔUSVᴴ;
rank_atol::Real = 0,
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...),
complement_atol::Real = default_pullback_complement_atol(USVᴴ[2], ΔUSVᴴ...),
maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence?
)
# Extract the SVD components
Expand Down Expand Up @@ -257,15 +262,15 @@ function svd_trunc_pullback!(
if m ≤ n
X = rmul!(AP * Y₀ᴴ', Diagonal(S⁻¹))
X .+= X₀
X = accelerative_smith_iteration!(X, X₀, AP * AP', S⁻¹ .^ 2, degeneracy_atol, maxiter) # recycle X₀
X = accelerative_smith_iteration!(X, X₀, AP * AP', S⁻¹ .^ 2, complement_atol, maxiter) # recycle X₀
Yᴴ = lmul!(Diagonal(S⁻¹), X' * AP)
Yᴴ .+= Y₀ᴴ
ΔA = mul!(ΔA, X, Vᴴ, 1, 1)
ΔA = mul!(ΔA, U, Yᴴ, 1, 1)
else
Y = rmul!(AP' * X₀, Diagonal(S⁻¹))
Y .+= Y₀ᴴ'
Y = accelerative_smith_iteration!(Y, similar(Y), AP' * AP, S⁻¹ .^ 2, degeneracy_atol, maxiter)
Y = accelerative_smith_iteration!(Y, similar(Y), AP' * AP, S⁻¹ .^ 2, complement_atol, maxiter)
X = rmul!(AP * Y, Diagonal(S⁻¹))
X .+= X₀
ΔA = mul!(ΔA, X, Vᴴ, 1, 1)
Expand All @@ -279,10 +284,11 @@ function svd_trunc_pullback!(
ΔA::Diagonal, A, USVᴴ, ΔUSVᴴ;
rank_atol::Real = 0,
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3])
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3]),
complement_atol::Real = default_pullback_complement_atol(USVᴴ[2], ΔUSVᴴ...)
)
ΔA_full = zero!(similar(ΔA, size(ΔA)))
ΔA_full = svd_trunc_pullback!(ΔA_full, A, USVᴴ, ΔUSVᴴ; rank_atol, degeneracy_atol, gauge_atol)
ΔA_full = svd_trunc_pullback!(ΔA_full, A, USVᴴ, ΔUSVᴴ; rank_atol, degeneracy_atol, gauge_atol, complement_atol)
diagview(ΔA) .+= diagview(ΔA_full)
return ΔA
end
Expand Down
Loading