diff --git a/ext/MatrixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl b/ext/MatrixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl index bdbf82347..da680c45b 100644 --- a/ext/MatrixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl +++ b/ext/MatrixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl @@ -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 --------- @@ -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) diff --git a/src/common/defaults.jl b/src/common/defaults.jl index c64c24ad4..fb658d25c 100644 --- a/src/common/defaults.jl +++ b/src/common/defaults.jl @@ -9,16 +9,19 @@ 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′)) end """ @@ -26,16 +29,41 @@ end 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) diff --git a/src/pullbacks/eig.jl b/src/pullbacks/eig.jl index 11fbc02c9..b4ee584a2 100755 --- a/src/pullbacks/eig.jl +++ b/src/pullbacks/eig.jl @@ -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 @@ -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? ) @@ -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ᴴ @@ -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 diff --git a/src/pullbacks/eigh.jl b/src/pullbacks/eigh.jl index 2b3afacd0..3d3c22214 100755 --- a/src/pullbacks/eigh.jl +++ b/src/pullbacks/eigh.jl @@ -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`, @@ -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? ) @@ -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 @@ -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 diff --git a/src/pullbacks/svd.jl b/src/pullbacks/svd.jl index 576997b69..e90d1150d 100755 --- a/src/pullbacks/svd.jl +++ b/src/pullbacks/svd.jl @@ -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 @@ -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 @@ -257,7 +262,7 @@ 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) @@ -265,7 +270,7 @@ function svd_trunc_pullback!( 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) @@ -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