Skip to content

Commit 7fdc9b4

Browse files
Jutholkdvos
authored andcommitted
some changes from review
1 parent 44c63d7 commit 7fdc9b4

1 file changed

Lines changed: 31 additions & 44 deletions

File tree

‎src/pullbacks/svd.jl‎

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ function check_and_prepare_svd_cotangents(
4242
end
4343
end
4444
UᴴΔU₁ = U₁' * ΔU₁
45-
ΔU₁ = mul!(ΔU₁, U₁, UᴴΔU₁, -1, 1)
45+
ΔU₊ = mul!(ΔU₁, U₁, UᴴΔU₁, -1, 1)
4646
aUᴴΔU₁ = project_antihermitian!(UᴴΔU₁)
4747
Δgauge = max(Δgauge, ΔgaugeU)
4848
else
49-
ΔU₁ = nothing
49+
ΔU₊ = nothing
5050
aUᴴΔU₁ = zero!(similar(U₁, (r, r)))
5151
end
5252
if !iszerotangent(ΔVᴴ)
@@ -74,11 +74,11 @@ function check_and_prepare_svd_cotangents(
7474
end
7575
end
7676
VᴴΔV₁ = V₁ᴴ * ΔV₁ᴴ'
77-
ΔV₁ᴴ = mul!(ΔV₁ᴴ, VᴴΔV₁', V₁ᴴ, -1, 1)
77+
ΔV₊ᴴ = mul!(ΔV₁ᴴ, VᴴΔV₁', V₁ᴴ, -1, 1)
7878
aVᴴΔV₁ = project_antihermitian!(VᴴΔV₁)
7979
Δgauge = max(Δgauge, ΔgaugeV)
8080
else
81-
ΔV₁ᴴ = nothing
81+
ΔV₊ᴴ = nothing
8282
aVᴴΔV₁ = zero!(similar(V₁ᴴ, (r, r)))
8383
end
8484
mask = abs.(S₁' .- S₁) .< degeneracy_atol
@@ -101,7 +101,14 @@ function check_and_prepare_svd_cotangents(
101101

102102
Δgauge ≤ gauge_atol ||
103103
@warn "`svd` cotangents sensitive to gauge choice: (|Δgauge| = $Δgauge)"
104-
return ΔU₁, ΔS₁, ΔV₁ᴴ, aUᴴΔU₁, aVᴴΔV₁
104+
105+
UdΔAV = (aUᴴΔU₁ .+ aVᴴΔV₁) .* inv_safe.(S₁' .- S₁, degeneracy_atol) .+
106+
(aUᴴΔU₁ .- aVᴴΔV₁) .* inv_safe.(S₁' .+ S₁, degeneracy_atol)
107+
if !iszerotangent(ΔS₁)
108+
diagview(UdΔAV) .+= real.(ΔS₁)
109+
end
110+
111+
return UdΔAV, ΔU₊, ΔV₊ᴴ
105112
end
106113

107114
"""
@@ -145,25 +152,19 @@ function svd_pullback!(
145152
S₁ = view(S, 1:r)
146153

147154
ΔU, ΔSmat, ΔVᴴ = ΔUSVᴴ
148-
ΔU₁, ΔS₁, ΔV₁ᴴ, aUᴴΔU₁, aVᴴΔV₁ = check_and_prepare_svd_cotangents(
155+
UdΔAV, ΔU₊, ΔV₊ᴴ = check_and_prepare_svd_cotangents(
149156
U, S, Vᴴ, ΔU, ΔSmat, ΔVᴴ, r, ind; degeneracy_atol, gauge_atol
150157
)
151-
152-
UdΔAV = (aUᴴΔU₁ .+ aVᴴΔV₁) .* inv_safe.(S₁' .- S₁, degeneracy_atol) .+
153-
(aUᴴΔU₁ .- aVᴴΔV₁) .* inv_safe.(S₁' .+ S₁, degeneracy_atol)
154-
if !iszerotangent(ΔS₁)
155-
diagview(UdΔAV) .+= real.(ΔS₁)
156-
end
157158
ΔA = mul!(ΔA, U₁, UdΔAV * V₁ᴴ, 1, 1) # add the contribution to ΔA
158159

159160
# Add the remaining contributions
160-
if m > r && !iszerotangent(ΔU₁) # ΔU₁ is already orthogonal to U₁
161-
ΔU₁ ./= S₁'
162-
ΔA = mul!(ΔA, ΔU₁, V₁ᴴ, 1, 1)
161+
if m > r && !iszerotangent(ΔU₊) # ΔU₁ is already orthogonal to U₁
162+
ΔU₊ ./= S₁'
163+
ΔA = mul!(ΔA, ΔU₊, V₁ᴴ, 1, 1)
163164
end
164-
if n > r && !iszerotangent(ΔV₁ᴴ) # ΔV₁ᴴ is already orthogonal to V₁ᴴ
165-
ΔV₁ᴴ .= S₁ .\ ΔV₁ᴴ
166-
ΔA = mul!(ΔA, U₁, ΔV₁ᴴ, 1, 1)
165+
if n > r && !iszerotangent(ΔV₊ᴴ) # ΔV₁ᴴ is already orthogonal to V₁ᴴ
166+
ΔV₊ᴴ .= S₁ .\ ΔV₊ᴴ
167+
ΔA = mul!(ΔA, U₁, ΔV₊ᴴ, 1, 1)
167168
end
168169
return ΔA
169170
end
@@ -201,7 +202,7 @@ A warning will be printed if the cotangents are not gauge-invariant, i.e. if the
201202
anti-hermitian part of `U' * ΔU + Vᴴ * ΔVᴴ'`, restricted to rows `i` and columns `j` for
202203
which `abs(S[i] - S[j]) < degeneracy_atol`, is not small compared to `gauge_atol`.
203204
"""
204-
function svd_trunc_pullback!(
205+
function svd_trunc_pullback2!(
205206
ΔA::AbstractMatrix, A, USVᴴ, ΔUSVᴴ;
206207
rank_atol::Real = 0,
207208
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
@@ -220,24 +221,17 @@ function svd_trunc_pullback!(
220221

221222
# Extract and check the cotangents
222223
ΔU, ΔSmat, ΔVᴴ = ΔUSVᴴ
223-
ΔU, ΔS, ΔVᴴ, aUᴴΔU, aVᴴΔV = check_and_prepare_svd_cotangents(
224-
U, S, Vᴴ, ΔU, ΔSmat, ΔVᴴ, p; degeneracy_atol, gauge_atol
224+
UdΔAV, ΔU₊, ΔV₊ᴴ = check_and_prepare_svd_cotangents(
225+
U, S, Vᴴ, ΔU, ΔSmat, ΔVᴴ, r, ind; degeneracy_atol, gauge_atol
225226
)
226-
227-
# This part is the same as in `svd_pullback!`
228-
UdΔAV = (aUᴴΔU .+ aVᴴΔV) .* inv_safe.(S' .- S, degeneracy_atol) .+
229-
(aUᴴΔU .- aVᴴΔV) .* inv_safe.(S' .+ S, degeneracy_atol)
230-
if !iszerotangent(ΔS)
231-
diagview(UdΔAV) .+= real.(ΔS)
232-
end
233227
ΔA = mul!(ΔA, U, UdΔAV * Vᴴ, 1, 1) # add the contribution to ΔA
234228

235229
# The contributions from the orthogonal complement need to be treated differently
236230
# ΔU and ΔVᴴ are already orthogonal to U and Vᴴ
237-
if !(iszerotangent(ΔU) && iszerotangent(ΔVᴴ))
231+
if !(iszerotangent(ΔU₊) && iszerotangent(ΔV₊ᴴ))
238232
Aperp = mul!(copy(A), U, Smat * Vᴴ, -1, 1)
239-
x₀ = iszerotangent(ΔU) ? zero(U) : rdiv!(ΔU, Diagonal(S))
240-
y₀ᴴ = iszerotangent(ΔVᴴ) ? zero(Vᴴ) : ldiv!(Diagonal(S), ΔVᴴ)
233+
x₀ = iszerotangent(ΔU₊) ? zero(U) : rdiv!(ΔU₊, Diagonal(S))
234+
y₀ᴴ = iszerotangent(ΔV₊ᴴ) ? zero(Vᴴ) : ldiv!(Diagonal(S), ΔV₊ᴴ)
241235
X = copy(x₀)
242236
Yᴴ = copy(y₀ᴴ)
243237
xₖ, xₖ₊₁ = x₀, zero(x₀)
@@ -261,7 +255,7 @@ function svd_trunc_pullback!(
261255
end
262256
return ΔA
263257
end
264-
function svd_trunc_pullback2!(
258+
function svd_trunc_pullback!(
265259
ΔA::AbstractMatrix, A, USVᴴ, ΔUSVᴴ;
266260
rank_atol::Real = 0,
267261
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
@@ -280,23 +274,16 @@ function svd_trunc_pullback2!(
280274

281275
# Extract and check the cotangents
282276
ΔU, ΔSmat, ΔVᴴ = ΔUSVᴴ
283-
ΔU, ΔS, ΔVᴴ, aUᴴΔU, aVᴴΔV = check_and_prepare_svd_cotangents(
284-
U, S, Vᴴ, ΔU, ΔSmat, ΔVᴴ, p; degeneracy_atol, gauge_atol
277+
UdΔAV, ΔU₊, ΔV₊ᴴ = check_and_prepare_svd_cotangents(
278+
U, S, Vᴴ, ΔU, ΔSmat, ΔVᴴ, r, ind; degeneracy_atol, gauge_atol
285279
)
286-
287-
# This part is the same as in `svd_pullback!`
288-
UdΔAV = (aUᴴΔU .+ aVᴴΔV) .* inv_safe.(S' .- S, degeneracy_atol) .+
289-
(aUᴴΔU .- aVᴴΔV) .* inv_safe.(S' .+ S, degeneracy_atol)
290-
if !iszerotangent(ΔS)
291-
diagview(UdΔAV) .+= real.(ΔS)
292-
end
293280
ΔA = mul!(ΔA, U, UdΔAV * Vᴴ, 1, 1) # add the contribution to ΔA
294281

295282
# The contribtutions from the orthogonal complement need to be treated differently
296283
# ΔU and ΔVᴴ are already orthogonal to U and Vᴴ
297-
if !(iszerotangent(ΔU) && iszerotangent(ΔVᴴ))
298-
X₀ = iszerotangent(ΔU) ? zero(U) : rdiv!(ΔU, Diagonal(S))
299-
Y₀ᴴ = iszerotangent(ΔVᴴ) ? zero(Vᴴ) : ldiv!(Diagonal(S), ΔVᴴ)
284+
if !(iszerotangent(ΔU₊) && iszerotangent(ΔV₊ᴴ))
285+
X₀ = iszerotangent(ΔU₊) ? zero(U) : rdiv!(ΔU₊, Diagonal(S))
286+
Y₀ᴴ = iszerotangent(ΔV₊ᴴ) ? zero(Vᴴ) : ldiv!(Diagonal(S), ΔV₊ᴴ)
300287
AP = mul!(copy(A), U, Smat * Vᴴ, -1, 1)
301288
AP ./= S[1]
302289
S = S ./ S[1]

0 commit comments

Comments
 (0)