Skip to content

Commit ba679e9

Browse files
committed
Fix the remaining errors in the svd and eigh pullbacks
1 parent 3c9f39e commit ba679e9

2 files changed

Lines changed: 20 additions & 19 deletions

File tree

‎src/pullbacks/eigh.jl‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function check_and_prepare_eigh_cotangents(
44
gauge_atol::Real = default_pullback_gauge_atol(ΔDmat, ΔV)
55
)
66

7-
# Only the columns ind₀ of VᴴΔV and VᴴΔAV are computed; their rows ind follow by antihermiticity
7+
# Only the columns ind₀ of VᴴΔV and VᴴΔAV are computed; their rows ind₀ follow by antihermiticity
88
n, p = size(V)
99
ind₀ = select_indices(axes(D, 1), ind)
1010
k = length(ind₀)
@@ -79,8 +79,8 @@ function eigh_pullback!(
7979
(n, n) == size(ΔA) || throw(DimensionMismatch())
8080
iszero(n) && return ΔA
8181

82-
ΔDmat, ΔV, ind₀ = ΔDV
83-
VᴴΔAV₀, = check_and_prepare_eigh_cotangents(
82+
ΔDmat, ΔV = ΔDV
83+
VᴴΔAV₀, _, ind₀ = check_and_prepare_eigh_cotangents(
8484
D, V, ΔDmat, ΔV, ind; degeneracy_atol, gauge_atol
8585
)
8686

‎src/pullbacks/svd.jl‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function check_and_prepare_svd_cotangents(
1818
Δgauge = zero(eltype(S))
1919

2020
# Only the columns ind₀ ⊆ 1:r of UᴴΔAV are computed.
21-
# By keeping its hermitian and antihermitian parts seperate, we can reconstruct the full UᴴΔAV
21+
# By keeping its hermitian and antihermitian parts separate, we can reconstruct the full UᴴΔAV
2222
J = indS .<= r
2323
ind₀ = indS[J]
2424
k = length(ind₀)
@@ -67,7 +67,7 @@ function check_and_prepare_svd_cotangents(
6767
throw(ArgumentError(lazy"Unexpected selection of Vᴴ rows: indV = $indV, expected indS = $indS or 1:$n"))
6868
end
6969
VᴴΔV₁₀ = V₁ᴴ * ΔV₀ᴴ'
70-
ΔV₊ᴴ = mul!(ΔV₀ᴴ, VᴴΔV₀', V₁ᴴ, -1, 1)
70+
ΔV₊ᴴ = mul!(ΔV₀ᴴ, VᴴΔV₁₀', V₁ᴴ, -1, 1)
7171
aVᴴΔV₁₀ = antihermitian_columns!(VᴴΔV₁₀, ind₀)
7272
Δgauge = max(Δgauge, ΔgaugeV)
7373
else
@@ -76,8 +76,8 @@ function check_and_prepare_svd_cotangents(
7676
end
7777

7878
S₀ = view(S, ind₀)
79-
hUᴴΔAV₁₀ = (aUᴴΔU₁₀ .+ aVᴴΔV₁₀) .* inv_safe.(transpose(S₀) .- S₁, degeneracy_atol) # hermitian part of UᴴΔAV, restricted to rows 1:r and column ind₀
80-
aUᴴΔAV₁₀ = (aUᴴΔU₁₀ .- aVᴴΔV₁₀) .* inv_safe.(transpose(S₀) .+ S₁, degeneracy_atol) # antihermitian part of UᴴΔAV, restricted to rows 1:r and column ind₀
79+
hUᴴΔAV₁₀ = (aUᴴΔU₁₀ .+ aVᴴΔV₁₀) .* inv_safe.(transpose(S₀) .- S₁, degeneracy_atol) # hermitian part of UᴴΔAV, restricted to rows 1:r and columns ind₀
80+
aUᴴΔAV₁₀ = (aUᴴΔU₁₀ .- aVᴴΔV₁₀) .* inv_safe.(transpose(S₀) .+ S₁, degeneracy_atol) # antihermitian part of UᴴΔAV, restricted to rows 1:r and columns ind₀
8181

8282
gaugepart = (abs.(transpose(S₀) .- S₁) .< degeneracy_atol) .* (aUᴴΔU₁₀ .+ aVᴴΔV₁₀)
8383
Δgauge = max(Δgauge, maximum(abs, gaugepart; init = zero(Δgauge)))
@@ -93,7 +93,7 @@ function check_and_prepare_svd_cotangents(
9393
Δgauge ≤ gauge_atol ||
9494
@warn "`svd` cotangents sensitive to gauge choice: (|Δgauge| = $Δgauge)"
9595

96-
return hUᴴΔAV₁₀, aUᴴΔU₁₀, ΔU₊, ΔV₊ᴴ, ind₀
96+
return hUᴴΔAV₁₀, aUᴴΔAV₁₀, ΔU₊, ΔV₊ᴴ, ind₀
9797
end
9898

9999
"""
@@ -139,27 +139,28 @@ function svd_pullback!(
139139
S₁ = view(S, 1:r)
140140

141141
ΔU, ΔSmat, ΔVᴴ = ΔUSVᴴ
142-
hUᴴΔAV₁₀, aUᴴΔU₁₀, ΔU₊, ΔV₊ᴴ, ind₀ = check_and_prepare_svd_cotangents(
142+
hUᴴΔAV₁₀, aUᴴΔAV₁₀, ΔU₊, ΔV₊ᴴ, ind₀ = check_and_prepare_svd_cotangents(
143143
U, S, Vᴴ, ΔU, ΔSmat, ΔVᴴ, r, ind; degeneracy_atol, gauge_atol
144144
)
145145

146-
# UᴴΔAV is nonzero only in its rows and columns ind′, which are UᴴΔAVₖ and UᴴΔAVʳ'. For k ≤ r / 2,
147-
# applying these two blocks directly, in O(m n k), is faster than forming UᴴΔAV.
146+
# UᴴΔAV is nonzero only in its columns ind₀, which are hUᴴΔAV₁₀ + aUᴴΔAV₁₀, and its rows ind₀,
147+
# which are hUᴴΔAV₁₀' - aUᴴΔAV₁₀'. For k ≤ r / 2, applying these two blocks directly, in O(m n k),
148+
# is faster than forming UᴴΔAV.
148149
S₀ = view(S, ind₀)
149150
U₀ = U[:, ind₀]
150151
V₀ᴴ = Vᴴ[ind₀, :]
151152
if 2 * length(ind₀) <= r
152-
ΔA = mul!(ΔA, U₁ * (hUᴴΔAV₁₀ + aUᴴΔU₁₀), V₀ᴴ, 1, 1)
153+
ΔA = mul!(ΔA, U₁ * (hUᴴΔAV₁₀ + aUᴴΔAV₁₀), V₀ᴴ, 1, 1)
153154
hUᴴΔAV₁₀[ind₀, :] .= zero(eltype(hUᴴΔAV₁₀))
154-
aUᴴΔU₁₀[ind₀, :] .= zero(eltype(aUᴴΔU₁₀))
155-
ΔA = mul!(ΔA, U₀, (hUᴴΔAV₁₀' - aUᴴΔU₁₀') * V₁ᴴ, 1, 1)
155+
aUᴴΔAV₁₀[ind₀, :] .= zero(eltype(aUᴴΔAV₁₀))
156+
ΔA = mul!(ΔA, U₀, (hUᴴΔAV₁₀' - aUᴴΔAV₁₀') * V₁ᴴ, 1, 1)
156157
else
157158
if is_leading_index(ind₀, r) # NOTE: all columns in order (e.g. `ind = Colon()`): the original path
158-
UᴴΔAV = hUᴴΔAV₁₀ + aUᴴΔU₁₀
159+
UᴴΔAV = hUᴴΔAV₁₀ + aUᴴΔAV₁₀
159160
else
160161
UᴴΔAV = zero!(similar(hUᴴΔAV₁₀, (r, r)))
161-
UᴴΔAV[ind₀, :] .= hUᴴΔAV₁₀' .- aUᴴΔU₁₀'
162-
UᴴΔAV[:, ind₀] .= hUᴴΔAV₁₀' .+ aUᴴΔU₁₀'
162+
UᴴΔAV[ind₀, :] .= hUᴴΔAV₁₀' .- aUᴴΔAV₁₀'
163+
UᴴΔAV[:, ind₀] .= hUᴴΔAV₁₀ .+ aUᴴΔAV₁₀
163164
end
164165
ΔA = mul!(ΔA, U₁, UᴴΔAV * V₁ᴴ, 1, 1) # add the contribution to ΔA
165166
end
@@ -232,10 +233,10 @@ function svd_trunc_pullback!(
232233

233234
# Extract and check the cotangents
234235
ΔU, ΔSmat, ΔVᴴ = ΔUSVᴴ
235-
hUᴴΔAV, aUᴴΔU, ΔU₊, ΔV₊ᴴ = check_and_prepare_svd_cotangents(
236+
hUᴴΔAV, aUᴴΔAV, ΔU₊, ΔV₊ᴴ = check_and_prepare_svd_cotangents(
236237
U, S, Vᴴ, ΔU, ΔSmat, ΔVᴴ, p; degeneracy_atol, gauge_atol
237238
)
238-
UᴴΔAV = hUᴴΔAV .+ aUᴴΔU
239+
UᴴΔAV = hUᴴΔAV .+ aUᴴΔAV
239240
ΔAV = U * UᴴΔAV
240241
ΔA = mul!(ΔA, ΔAV, Vᴴ, 1, 1) # add the contribution to ΔA
241242

0 commit comments

Comments
 (0)