Skip to content
5 changes: 4 additions & 1 deletion ext/MatrixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ using LinearAlgebra
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_pullback_gauge_atol), ::Any, ::Any...) = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_pullback_degeneracy_atol), ::Any) = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_pullback_rank_atol), ::Any) = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_pullback_complement_atol), ::Any...) = true
@inline EnzymeRules.inactive(::typeof(MatrixAlgebraKit.default_hermitian_tol), ::AbstractMatrix) = true

#----------- NOTE about derivatives ---------
Expand Down Expand Up @@ -366,7 +367,9 @@ for (f, trunc_f, full_f, pb) in (
) where {RT, TA, TB}
# A is overwritten in the primal, but not used in the pullback, so we do not need to cache it.
ret = $full_f(A.val, DV.val, alg.val.alg)
cache_DV = (DV.val !== ret) || EnzymeRules.overwritten(config)[3] ? copy.(ret) : nothing
# but the output may share storage with A (e.g. D for complex Diagonal input), so then we cache it
A_is_DV = !isa(A, Const) && (has_equal_storage(A.val, DV.val[1]) || has_equal_storage(A.val, DV.val[2]))
cache_DV = (DV.val !== ret) || A_is_DV || EnzymeRules.overwritten(config)[3] ? copy.(ret) : nothing
DV′, ind = truncate($trunc_f, ret, alg.val.trunc)
primal = EnzymeRules.needs_primal(config) ? DV′ : nothing
dret = if EnzymeRules.needs_shadow(config)
Expand Down
36 changes: 32 additions & 4 deletions src/common/defaults.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,61 @@ quantity needs to be computed.
"""
defaulttol(x::Any) = eps(real(float(one(eltype(x)))))^(2 / 3)

# tolerance proportional to the scale `x`
_default_atol(x::Real) = eps(typeof(x))^(3 / 4) * x

"""
default_pullback_gauge_atol(ΔA...)

Default tolerance for deciding to warn if incoming adjoints of a pullback rule
has components that are not gauge-invariant.
"""
default_pullback_gauge_atol(A) = iszerotangent(A) ? 0 : eps(norm(A, Inf))^(3 / 4)
default_pullback_gauge_atol(A) = iszerotangent(A) ? 0 : _default_atol(norm(A, Inf))
function default_pullback_gauge_atol(A, As...)
As′ = filter(!iszerotangent, (A, As...))
return isempty(As′) ? 0 : eps(norm(As′, Inf))^(3 / 4)
return isempty(As′) ? 0 : _default_atol(maximum(y -> norm(y, Inf), As′))
Comment thread
leburgel marked this conversation as resolved.
Resolved
end

"""
default_pullback_degeneracy_atol(A)

Default tolerance for deciding when values should be considered as degenerate.
"""
default_pullback_degeneracy_atol(A) = eps(norm(A, Inf))^(3 / 4)
default_pullback_degeneracy_atol(A) = _default_atol(norm(A, Inf))

"""
default_pullback_rank_atol(A)

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) = _default_atol(norm(A, Inf))
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 _default_atol(x)
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 _default_atol(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