Skip to content

Commit 2abebc6

Browse files
committed
better gauge warning tolerance for factorisation rrules
1 parent aca1263 commit 2abebc6

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

ext/TensorKitChainRulesCoreExt/factorizations.jl

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ end
184184
#
185185
function svd_pullback!(ΔA::AbstractMatrix, U::AbstractMatrix, S::AbstractVector,
186186
Vd::AbstractMatrix, ΔU, ΔS, ΔVd;
187-
atol::Real=0,
188-
rtol::Real=atol > 0 ? 0 : eps(eltype(S))^(3 / 4))
187+
tol::Real=default_pullback_gaugetol(S))
189188

190189
# Basic size checks and determination
191190
m, n = size(U, 1), size(Vd, 2)
@@ -214,8 +213,7 @@ function svd_pullback!(ΔA::AbstractMatrix, U::AbstractMatrix, S::AbstractVector
214213
Vp = view(Vd, 1:p, :)'
215214
Sp = view(S, 1:p)
216215

217-
# tolerance and rank
218-
tol = atol > 0 ? atol : rtol * S[1, 1]
216+
# rank
219217
r = findlast(>=(tol), S)
220218

221219
# compute antihermitian part of projection of ΔU and ΔV onto U and V
@@ -302,16 +300,12 @@ function svd_pullback!(ΔA::AbstractMatrix, U::AbstractMatrix, S::AbstractVector
302300
end
303301

304302
function eig_pullback!(ΔA::AbstractMatrix, D::AbstractVector, V::AbstractMatrix, ΔD, ΔV;
305-
atol::Real=0,
306-
rtol::Real=atol > 0 ? 0 : eps(real(eltype(D)))^(3 / 4))
303+
tol::Real=default_pullback_gaugetol(D))
307304

308305
# Basic size checks and determination
309306
n = LinearAlgebra.checksquare(V)
310307
n == length(D) || throw(DimensionMismatch())
311308

312-
# tolerance and rank
313-
tol = atol > 0 ? atol : rtol * maximum(abs, D)
314-
315309
if !(ΔV isa AbstractZero)
316310
VdΔV = V' * ΔV
317311

@@ -345,16 +339,12 @@ function eig_pullback!(ΔA::AbstractMatrix, D::AbstractVector, V::AbstractMatrix
345339
end
346340

347341
function eigh_pullback!(ΔA::AbstractMatrix, D::AbstractVector, V::AbstractMatrix, ΔD, ΔV;
348-
atol::Real=0,
349-
rtol::Real=atol > 0 ? 0 : eps(real(eltype(D)))^(3 / 4))
342+
tol::Real=default_pullback_gaugetol(D))
350343

351344
# Basic size checks and determination
352345
n = LinearAlgebra.checksquare(V)
353346
n == length(D) || throw(DimensionMismatch())
354347

355-
# tolerance and rank
356-
tol = atol > 0 ? atol : rtol * maximum(abs, D)
357-
358348
if !(ΔV isa AbstractZero)
359349
VdΔV = V' * ΔV
360350
aVdΔV = rmul!(VdΔV - VdΔV', 1 / 2)
@@ -379,10 +369,8 @@ function eigh_pullback!(ΔA::AbstractMatrix, D::AbstractVector, V::AbstractMatri
379369
end
380370

381371
function qr_pullback!(ΔA::AbstractMatrix, Q::AbstractMatrix, R::AbstractMatrix, ΔQ, ΔR;
382-
atol::Real=0,
383-
rtol::Real=atol > 0 ? 0 : eps(real(eltype(R)))^(3 / 4))
372+
tol::Real=default_pullback_gaugetol(R))
384373
Rd = view(R, diagind(R))
385-
tol = atol > 0 ? atol : rtol * maximum(abs, Rd)
386374
p = findlast(>=(tol) abs, Rd)
387375
m, n = size(R)
388376

@@ -432,10 +420,8 @@ function qr_pullback!(ΔA::AbstractMatrix, Q::AbstractMatrix, R::AbstractMatrix,
432420
end
433421

434422
function lq_pullback!(ΔA::AbstractMatrix, L::AbstractMatrix, Q::AbstractMatrix, ΔL, ΔQ;
435-
atol::Real=0,
436-
rtol::Real=atol > 0 ? 0 : eps(real(eltype(L)))^(3 / 4))
423+
tol::Real=default_pullback_gaugetol(L))
437424
Ld = view(L, diagind(L))
438-
tol = atol > 0 ? atol : rtol * maximum(abs, Ld)
439425
p = findlast(>=(tol) abs, Ld)
440426
m, n = size(L)
441427

@@ -483,3 +469,8 @@ function lq_pullback!(ΔA::AbstractMatrix, L::AbstractMatrix, Q::AbstractMatrix,
483469
ldiv!(LowerTriangular(L11)', ΔA1)
484470
return ΔA
485471
end
472+
473+
function default_pullback_gaugetol(a)
474+
n = norm(a, Inf)
475+
return eps(eltype(n))^(3 / 4) * max(n, one(n))
476+
end

0 commit comments

Comments
 (0)