Skip to content

Commit 55c4e18

Browse files
committed
make gauge tolerances relative
1 parent fc68369 commit 55c4e18

File tree

6 files changed

+23
-22
lines changed

6 files changed

+23
-22
lines changed

src/common/defaults.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ quantity needs to be computed.
1010
defaulttol(x::Any) = eps(real(float(one(eltype(x)))))^(2 / 3)
1111

1212
"""
13-
default_pullback_gauge_atol(A)
13+
default_pullback_gauge_atol(ΔA...)
1414
1515
Default tolerance for deciding to warn if incoming adjoints of a pullback rule
1616
has components that are not gauge-invariant.
1717
"""
18-
default_pullback_gauge_atol(A) = eps(real(eltype(A)))^(3 / 4)
18+
default_pullback_gauge_atol(A) = eps(norm(A, Inf))^(3 / 4)
19+
default_pullback_gauge_atol(A, As...) = maximum(default_pullback_gauge_atol, (A, As...))
1920

2021
"""
2122
default_pullback_degeneracy_atol(A)

src/pullbacks/eig.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
eig_pullback!(
33
ΔA::AbstractMatrix, A, DV, ΔDV, [ind];
44
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
5-
gauge_atol::Real = default_pullback_gauge_atol(DV[1])
5+
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
66
)
77
88
Adds the pullback from the full eigenvalue decomposition of `A` to `ΔA`, given the output
@@ -22,7 +22,7 @@ not small compared to `gauge_atol`.
2222
function eig_pullback!(
2323
ΔA::AbstractMatrix, A, DV, ΔDV, ind = Colon();
2424
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
25-
gauge_atol::Real = default_pullback_gauge_atol(DV[1])
25+
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
2626
)
2727

2828
# Basic size checks and determination
@@ -83,7 +83,7 @@ end
8383
eig_trunc_pullback!(
8484
ΔA::AbstractMatrix, ΔDV, A, DV;
8585
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
86-
gauge_atol::Real = default_pullback_gauge_atol(DV[1])
86+
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
8787
)
8888
8989
Adds the pullback from the truncated eigenvalue decomposition of `A` to `ΔA`, given the
@@ -104,7 +104,7 @@ not small compared to `gauge_atol`.
104104
function eig_trunc_pullback!(
105105
ΔA::AbstractMatrix, A, DV, ΔDV;
106106
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
107-
gauge_atol::Real = default_pullback_gauge_atol(DV[1])
107+
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
108108
)
109109
110110
# Basic size checks and determination

src/pullbacks/eigh.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
eigh_pullback!(
33
ΔA::AbstractMatrix, A, DV, ΔDV, [ind];
44
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
5-
gauge_atol::Real = default_pullback_gauge_atol(DV[1])
5+
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
66
)
77
88
Adds the pullback from the Hermitian eigenvalue decomposition of `A` to `ΔA`, given the
@@ -22,7 +22,7 @@ anti-hermitian part of `V' * ΔV`, restricted to rows `i` and columns `j` for wh
2222
function eigh_pullback!(
2323
ΔA::AbstractMatrix, A, DV, ΔDV, ind = Colon();
2424
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
25-
gauge_atol::Real = default_pullback_gauge_atol(DV[1])
25+
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
2626
)
2727

2828
# Basic size checks and determination
@@ -73,7 +73,7 @@ end
7373
eigh_trunc_pullback!(
7474
ΔA::AbstractMatrix, A, DV, ΔDV;
7575
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
76-
gauge_atol::Real = default_pullback_gauge_atol(DV[1])
76+
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
7777
)
7878
7979
Adds the pullback from the truncated Hermitian eigenvalue decomposition of `A` to `ΔA`,
@@ -94,7 +94,7 @@ not small compared to `gauge_atol`.
9494
function eigh_trunc_pullback!(
9595
ΔA::AbstractMatrix, A, DV, ΔDV;
9696
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
97-
gauge_atol::Real = default_pullback_gauge_atol(DV[1])
97+
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
9898
)
9999
100100
# Basic size checks and determination

src/pullbacks/lq.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
lq_pullback!(
33
ΔA, A, LQ, ΔLQ;
44
rank_atol::Real = default_pullback_rank_atol(LQ[1]),
5-
gauge_atol::Real = default_pullback_gauge_atol(LQ[1])
5+
gauge_atol::Real = default_pullback_gauge_atol(ΔLQ[2])
66
)
77
88
Adds the pullback from the LQ decomposition of `A` to `ΔA` given the output `LQ` and
@@ -18,7 +18,7 @@ or rows exceed `gauge_atol`, a warning will be printed.
1818
function lq_pullback!(
1919
ΔA::AbstractMatrix, A, LQ, ΔLQ;
2020
rank_atol::Real = default_pullback_rank_atol(LQ[1]),
21-
gauge_atol::Real = default_pullback_gauge_atol(LQ[1])
21+
gauge_atol::Real = default_pullback_gauge_atol(ΔLQ[2])
2222
)
2323
# process
2424
L, Q = LQ
@@ -105,7 +105,7 @@ end
105105
"""
106106
lq_null_pullback!(
107107
ΔA::AbstractMatrix, A, Nᴴ, ΔNᴴ;
108-
gauge_atol::Real = default_pullback_gauge_atol(A)
108+
gauge_atol::Real = default_pullback_gauge_atol(ΔNᴴ)
109109
)
110110
111111
Adds the pullback from the left nullspace of `A` to `ΔA`, given the nullspace basis
@@ -115,7 +115,7 @@ See also [`lq_pullback!`](@ref).
115115
"""
116116
function lq_null_pullback!(
117117
ΔA::AbstractMatrix, A, Nᴴ, ΔNᴴ;
118-
gauge_atol::Real = default_pullback_gauge_atol(A)
118+
gauge_atol::Real = default_pullback_gauge_atol(ΔNᴴ)
119119
)
120120
if !iszerotangent(ΔNᴴ) && size(Nᴴ, 1) > 0
121121
aNᴴΔN = project_antihermitian!(Nᴴ * ΔNᴴ')

src/pullbacks/qr.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
ΔA, A, QR, ΔQR;
44
tol::Real = default_pullback_gaugetol(QR[2]),
55
rank_atol::Real = default_pullback_rank_atol(QR[2]),
6-
gauge_atol::Real = default_pullback_gauge_atol(QR[2])
6+
gauge_atol::Real = default_pullback_gauge_atol(ΔQR[1])
77
)
88
99
Adds the pullback from the QR decomposition of `A` to `ΔA` given the output `QR` and
@@ -19,7 +19,7 @@ and also the adjoint variables `ΔQ` and `ΔR` should have nonzero values only i
1919
function qr_pullback!(
2020
ΔA::AbstractMatrix, A, QR, ΔQR;
2121
rank_atol::Real = default_pullback_rank_atol(QR[2]),
22-
gauge_atol::Real = default_pullback_gauge_atol(QR[2])
22+
gauge_atol::Real = default_pullback_gauge_atol(ΔQR[1])
2323
)
2424
# process
2525
Q, R = QR
@@ -105,7 +105,7 @@ end
105105
"""
106106
qr_null_pullback!(
107107
ΔA::AbstractMatrix, A, N, ΔN;
108-
gauge_atol::Real = default_pullback_gauge_atol(A)
108+
gauge_atol::Real = default_pullback_gauge_atol(ΔN)
109109
)
110110
111111
Adds the pullback from the right nullspace of `A` to `ΔA`, given the nullspace basis
@@ -115,7 +115,7 @@ See also [`qr_pullback!`](@ref).
115115
"""
116116
function qr_null_pullback!(
117117
ΔA::AbstractMatrix, A, N, ΔN;
118-
gauge_atol::Real = default_pullback_gauge_atol(A)
118+
gauge_atol::Real = default_pullback_gauge_atol(ΔN)
119119
)
120120
if !iszerotangent(ΔN) && size(N, 2) > 0
121121
aNᴴΔN = project_antihermitian!(N' * ΔN)

src/pullbacks/svd.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
ΔA, A, USVᴴ, ΔUSVᴴ, [ind];
44
rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
55
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
6-
gauge_atol::Real = default_pullback_gauge_atol(USVᴴ[2])
6+
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3])
77
)
88
99
Adds the pullback from the SVD of `A` to `ΔA` given the output USVᴴ of `svd_compact` or
@@ -24,7 +24,7 @@ function svd_pullback!(
2424
ΔA::AbstractMatrix, A, USVᴴ, ΔUSVᴴ, ind = Colon();
2525
rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
2626
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
27-
gauge_atol::Real = default_pullback_gauge_atol(USVᴴ[2])
27+
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3])
2828
)
2929

3030
# Extract the SVD components
@@ -106,7 +106,7 @@ end
106106
ΔA, A, USVᴴ, ΔUSVᴴ;
107107
rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
108108
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
109-
gauge_atol::Real = default_pullback_gauge_atol(USVᴴ[2])
109+
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3])
110110
)
111111
112112
Adds the pullback from the truncated SVD of `A` to `ΔA`, given the output `USVᴴ` and the
@@ -127,7 +127,7 @@ function svd_trunc_pullback!(
127127
ΔA::AbstractMatrix, A, USVᴴ, ΔUSVᴴ;
128128
rank_atol::Real = 0,
129129
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
130-
gauge_atol::Real = default_pullback_gauge_atol(USVᴴ[2])
130+
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ[1], ΔUSVᴴ[3])
131131
)
132132
133133
# Extract the SVD components

0 commit comments

Comments
 (0)