Skip to content

Commit a16a53f

Browse files
leburgelJutho
andauthored
Sum only one Neumann series in svd_trunc_pullback! (#287)
* Sum only one Neumann series in `svd_trunc_pullback!` * Fix GPU safety * Update src/pullbacks/svd.jl Co-authored-by: Jutho <Jutho@users.noreply.github.com> * Split off reusable doubling iteration, split cases for rectangular SVD * Rename helper to `accelerative_smith_iteration!` and add reference * Update src/common/pullbacks.jl Co-authored-by: Jutho <Jutho@users.noreply.github.com> * Move normalization inside doubling iteration --------- Co-authored-by: Jutho <Jutho@users.noreply.github.com>
1 parent 7a06e62 commit a16a53f

3 files changed

Lines changed: 58 additions & 62 deletions

File tree

‎src/common/pullbacks.jl‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,39 @@ iterating over `ind`, so that this also works for an `ind` that lives on a devic
3535
"""
3636
is_leading_index(ind::AbstractRange, p::Int) = ind == 1:p
3737
is_leading_index(ind::AbstractVector, p::Int) = length(ind) == p && all(ind .== 1:p)
38+
39+
"""
40+
accelerative_smith_iteration!(X, Xₙ, G, w, atol, maxiter)
41+
42+
Solve `X = B + G * X * Diagonal(w)` by summing the Neumann series
43+
`X = Σₖ Gᵏ * B * Diagonal(w)ᵏ` by doubling (Smith's method), i.e. by repeatedly adding
44+
`G^(2ʲ) * X * Diagonal(w)^(2ʲ)` to `X` until the norm of that increment drops below `atol`,
45+
for at most `maxiter` steps.
46+
47+
On entry, `X` contains `B`, and it is overwritten with the result. `Xₙ` is used as a buffer,
48+
and `G` and `w` are overwritten. `w` is normalized such that `maximum(abs, w) == 1`, so that
49+
squaring it can only shrink it; `G` is scaled by the inverse factor to compensate.
50+
51+
Reference: https://doi.org/10.1016/j.aml.2009.01.012.
52+
"""
53+
function accelerative_smith_iteration!(X, Xₙ, G, w, atol, maxiter)
54+
Gₙ = similar(G)
55+
wmax = maximum(abs, w)
56+
w ./= wmax
57+
G .*= wmax
58+
for k in 1:maxiter
59+
Xₙ = rmul!(mul!(Xₙ, G, X), Diagonal(w))
60+
if maximum(abs, Xₙ) < atol
61+
break
62+
end
63+
X .+= Xₙ
64+
if k == maxiter
65+
@warn "Sylvester iteration did not converge after $k iterations, final norm of X: $(maximum(abs, X))"
66+
break
67+
end
68+
w .= w .^ 2
69+
Gₙ = mul!(Gₙ, G, G)
70+
G, Gₙ = Gₙ, G
71+
end
72+
return X
73+
end

‎src/pullbacks/eigh.jl‎

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -153,38 +153,13 @@ function eigh_trunc_pullback!(
153153
if !iszerotangent(ΔV₊)
154154
X₀ = rdiv!(ΔV₊, Diagonal(D))
155155
AP = mul!(copy(A), V * Dmat, V', -1, 1)
156-
# Normalize by the smallest retained |eigenvalue|, as `svd_trunc_pullback!` does
157-
# with `S[end]`. That caps `max|D⁻¹|` at 1, so squaring can only shrink it.
158-
dabsmin = minimum(abs, D)
159-
AP ./= dabsmin
160-
D⁻¹ = dabsmin ./ D
161-
X₁ = rmul!(AP * X₀, Diagonal(D⁻¹))
162-
X₁ .+= X₀
163-
Xₖ, Xₖ₊₁ = X₁, X₀
164-
APₖ, APₖ₊₁ = AP * AP, AP
165-
D⁻¹ₖ, D⁻¹ₖ₊₁ = D⁻¹ .^ 2, D⁻¹
166-
for k in 1:maxiter
167-
Xₖ₊₁ = rmul!(mul!(Xₖ₊₁, APₖ, Xₖ), Diagonal(D⁻¹ₖ))
168-
if norm(Xₖ₊₁, Inf) < degeneracy_atol
169-
break
170-
end
171-
Xₖ₊₁ .+= Xₖ
172-
if k == maxiter
173-
@warn "Sylvester iteration did not converge after $k iterations, final norm of X: $(norm(Xₖ₊₁, Inf)))"
174-
break
175-
end
176-
D⁻¹ₖ₊₁ .= D⁻¹ₖ .^ 2
177-
APₖ₊₁ = mul!(APₖ₊₁, APₖ, APₖ)
178-
Xₖ, Xₖ₊₁ = Xₖ₊₁, Xₖ
179-
APₖ, APₖ₊₁ = APₖ₊₁, APₖ
180-
D⁻¹ₖ, D⁻¹ₖ₊₁ = D⁻¹ₖ₊₁, D⁻¹ₖ
181-
end
182-
Z .+= Xₖ
156+
X = accelerative_smith_iteration!(X₀, similar(X₀), AP, inv.(D), degeneracy_atol, maxiter)
157+
Z .+= X
183158
# we cannot directly multiply Z * V' into ΔA, because we have to
184159
# take the Hermitian part, and cannot apply project_hermitian! to
185160
# the current contents of ΔA
186161
# TODO: add an `add_project_hermitian!`
187-
# recycle AP's storage, but overwrite it: the loop leaves APₖ in that buffer
162+
# recycle AP's storage, but overwrite it: `accelerative_smith_iteration!` may leave a power of AP in it
188163
ΔA′ = project_hermitian!(mul!(AP, Z, V'))
189164
ΔA .+= ΔA′
190165
else

‎src/pullbacks/svd.jl‎

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -247,41 +247,26 @@ function svd_trunc_pullback!(
247247
Y₀ᴴ = iszerotangent(ΔV₊ᴴ) ? zero(Vᴴ) : ldiv!(Diagonal(S), ΔV₊ᴴ)
248248
US = mul!(ΔAV, U, Smat) # recycle ΔAV
249249
AP = mul!(copy(A), US, Vᴴ, -1, 1)
250-
minS = @view S[end:end]
251-
AP ./= minS
252-
S⁻¹ = minS ./ S
253-
X₁ = rmul!(AP * Y₀ᴴ', Diagonal(S⁻¹))
254-
X₁ .+= X₀
255-
Y₁ᴴ = lmul!(Diagonal(S⁻¹), X₀' * AP)
256-
Y₁ᴴ .+= Y₀ᴴ
257-
Xₖ, Xₖ₊₁ = X₁, X₀
258-
Yₖᴴ, Yₖ₊₁ᴴ = Y₁ᴴ, Y₀ᴴ
259-
APAᴴₖ, AᴴPAₖ = AP * AP', AP' * AP
260-
APAᴴₖ₊₁, AᴴPAₖ₊₁ = zero(APAᴴₖ), zero(AᴴPAₖ)
261-
S⁻¹ₖ, S⁻¹ₖ₊₁ = S⁻¹ .^ 2, S⁻¹
262-
for k in 1:maxiter
263-
Xₖ₊₁ = rmul!(mul!(Xₖ₊₁, APAᴴₖ, Xₖ), Diagonal(S⁻¹ₖ))
264-
Yₖ₊₁ᴴ = lmul!(Diagonal(S⁻¹ₖ), mul!(Yₖ₊₁ᴴ, Yₖᴴ, AᴴPAₖ))
265-
if norm(Xₖ₊₁, Inf) < degeneracy_atol && norm(Yₖ₊₁ᴴ, Inf) < degeneracy_atol
266-
break
267-
end
268-
Xₖ₊₁ .+= Xₖ
269-
Yₖ₊₁ᴴ .+= Yₖᴴ
270-
if k == maxiter
271-
@warn "Sylvester iteration did not converge after $k iterations, final norms of X: $(norm(Xₖ₊₁, Inf)), Yᴴ: $(norm(Yₖ₊₁ᴴ, Inf)))"
272-
break
273-
end
274-
S⁻¹ₖ₊₁ .= S⁻¹ₖ .^ 2
275-
APAᴴₖ₊₁ = mul!(APAᴴₖ₊₁, APAᴴₖ, APAᴴₖ)
276-
AᴴPAₖ₊₁ = mul!(AᴴPAₖ₊₁, AᴴPAₖ, AᴴPAₖ)
277-
Xₖ, Xₖ₊₁ = Xₖ₊₁, Xₖ
278-
Yₖᴴ, Yₖ₊₁ᴴ = Yₖ₊₁ᴴ, Yₖᴴ
279-
APAᴴₖ, APAᴴₖ₊₁ = APAᴴₖ₊₁, APAᴴₖ
280-
AᴴPAₖ, AᴴPAₖ₊₁ = AᴴPAₖ₊₁, AᴴPAₖ
281-
S⁻¹ₖ, S⁻¹ₖ₊₁ = S⁻¹ₖ₊₁, S⁻¹ₖ
250+
S⁻¹ = inv.(S)
251+
# sum the series on the smaller side only, the other side follows from
252+
# Yᴴ = Y₀ᴴ + S⁻¹ X' AP (m ≤ n) or X = X₀ + AP Y S⁻¹ (m > n)
253+
if m ≤ n
254+
X = rmul!(AP * Y₀ᴴ', Diagonal(S⁻¹))
255+
X .+= X₀
256+
X = accelerative_smith_iteration!(X, X₀, AP * AP', S⁻¹ .^ 2, degeneracy_atol, maxiter) # recycle X₀
257+
Yᴴ = lmul!(Diagonal(S⁻¹), X' * AP)
258+
Yᴴ .+= Y₀ᴴ
259+
ΔA = mul!(ΔA, X, Vᴴ, 1, 1)
260+
ΔA = mul!(ΔA, U, Yᴴ, 1, 1)
261+
else
262+
Y = rmul!(AP' * X₀, Diagonal(S⁻¹))
263+
Y .+= Y₀ᴴ'
264+
Y = accelerative_smith_iteration!(Y, similar(Y), AP' * AP, S⁻¹ .^ 2, degeneracy_atol, maxiter)
265+
X = rmul!(AP * Y, Diagonal(S⁻¹))
266+
X .+= X₀
267+
ΔA = mul!(ΔA, X, Vᴴ, 1, 1)
268+
ΔA = mul!(ΔA, U, Y', 1, 1)
282269
end
283-
ΔA = mul!(ΔA, Xₖ, Vᴴ, 1, 1)
284-
ΔA = mul!(ΔA, U, Yₖᴴ, 1, 1)
285270
end
286271
return ΔA
287272
end

0 commit comments

Comments
 (0)