Skip to content

Commit 4dd40de

Browse files
committed
Split off reusable doubling iteration, split cases for rectangular SVD
1 parent 6676448 commit 4dd40de

3 files changed

Lines changed: 52 additions & 52 deletions

File tree

‎src/common/pullbacks.jl‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,34 @@ 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+
_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. It is assumed that `w` is normalized such that
49+
`maximum(abs, w) == 1`, so that squaring it can only shrink it.
50+
"""
51+
function _smith_iteration!(X, Xₙ, G, w, atol, maxiter)
52+
Gₙ = similar(G)
53+
for k in 1:maxiter
54+
Xₙ = rmul!(mul!(Xₙ, G, X), Diagonal(w))
55+
if maximum(abs, Xₙ) < atol
56+
break
57+
end
58+
X .+= Xₙ
59+
if k == maxiter
60+
@warn "Sylvester iteration did not converge after $k iterations, final norm of X: $(maximum(abs, X))"
61+
break
62+
end
63+
w .= w .^ 2
64+
Gₙ = mul!(Gₙ, G, G)
65+
G, Gₙ = Gₙ, G
66+
end
67+
return X
68+
end

‎src/pullbacks/eigh.jl‎

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -158,33 +158,13 @@ function eigh_trunc_pullback!(
158158
dabsmin = minimum(abs, D)
159159
AP ./= dabsmin
160160
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ₖ
161+
X = _smith_iteration!(X₀, similar(X₀), AP, D⁻¹, degeneracy_atol, maxiter)
162+
Z .+= X
183163
# we cannot directly multiply Z * V' into ΔA, because we have to
184164
# take the Hermitian part, and cannot apply project_hermitian! to
185165
# the current contents of ΔA
186166
# TODO: add an `add_project_hermitian!`
187-
# recycle AP's storage, but overwrite it: the loop leaves APₖ in that buffer
167+
# recycle AP's storage, but overwrite it: `_smith_iteration!` may leave a power of AP in it
188168
ΔA′ = project_hermitian!(mul!(AP, Z, V'))
189169
ΔA .+= ΔA′
190170
else

‎src/pullbacks/svd.jl‎

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -244,36 +244,25 @@ function svd_trunc_pullback!(
244244
minS = @view S[end:end]
245245
AP ./= minS
246246
S⁻¹ = minS ./ S
247-
# sum the series on the smaller side only, the other side follows from Yᴴ = Y₀ᴴ + S⁻¹ X' AP;
248-
# for m > n, work with the adjoint problem, which swaps X and Yᴴ
249-
m > n && ((AP, X₀, Y₀ᴴ) = (AP', Y₀ᴴ', X₀'))
250-
X₁ = rmul!(AP * Y₀ᴴ', Diagonal(S⁻¹))
251-
X₁ .+= X₀
252-
Xₖ, Xₖ₊₁ = X₁, X₀
253-
APAᴴₖ = AP * AP'
254-
APAᴴₖ₊₁ = zero(APAᴴₖ)
255-
S⁻¹ₖ, S⁻¹ₖ₊₁ = S⁻¹ .^ 2, zero(S⁻¹)
256-
for k in 1:maxiter
257-
Xₖ₊₁ = rmul!(mul!(Xₖ₊₁, APAᴴₖ, Xₖ), Diagonal(S⁻¹ₖ))
258-
if norm(Xₖ₊₁, Inf) < degeneracy_atol
259-
break
260-
end
261-
Xₖ₊₁ .+= Xₖ
262-
if k == maxiter
263-
@warn "Sylvester iteration did not converge after $k iterations, final norm of X: $(maximum(abs, Xₖ₊₁))"
264-
break
265-
end
266-
S⁻¹ₖ₊₁ .= S⁻¹ₖ .^ 2
267-
APAᴴₖ₊₁ = mul!(APAᴴₖ₊₁, APAᴴₖ, APAᴴₖ)
268-
Xₖ, Xₖ₊₁ = Xₖ₊₁, Xₖ
269-
APAᴴₖ, APAᴴₖ₊₁ = APAᴴₖ₊₁, APAᴴₖ
270-
S⁻¹ₖ, S⁻¹ₖ₊₁ = S⁻¹ₖ₊₁, S⁻¹ₖ
247+
# sum the series on the smaller side only, the other side follows from
248+
# Yᴴ = Y₀ᴴ + S⁻¹ X' AP (m ≤ n) or X = X₀ + AP Y S⁻¹ (m > n)
249+
if m ≤ n
250+
X = rmul!(AP * Y₀ᴴ', Diagonal(S⁻¹))
251+
X .+= X₀
252+
X = _smith_iteration!(X, X₀, AP * AP', S⁻¹ .^ 2, degeneracy_atol, maxiter) # recycle X₀
253+
Yᴴ = lmul!(Diagonal(S⁻¹), X' * AP)
254+
Yᴴ .+= Y₀ᴴ
255+
ΔA = mul!(ΔA, X, Vᴴ, 1, 1)
256+
ΔA = mul!(ΔA, U, Yᴴ, 1, 1)
257+
else
258+
Y = rmul!(AP' * X₀, Diagonal(S⁻¹))
259+
Y .+= Y₀ᴴ'
260+
Y = _smith_iteration!(Y, similar(Y), AP' * AP, S⁻¹ .^ 2, degeneracy_atol, maxiter)
261+
X = rmul!(AP * Y, Diagonal(S⁻¹))
262+
X .+= X₀
263+
ΔA = mul!(ΔA, X, Vᴴ, 1, 1)
264+
ΔA = mul!(ΔA, U, Y', 1, 1)
271265
end
272-
Yₖᴴ = lmul!(Diagonal(S⁻¹), Xₖ' * AP)
273-
Yₖᴴ .+= Y₀ᴴ
274-
m > n && ((Xₖ, Yₖᴴ) = (Yₖᴴ', Xₖ'))
275-
ΔA = mul!(ΔA, Xₖ, Vᴴ, 1, 1)
276-
ΔA = mul!(ΔA, U, Yₖᴴ, 1, 1)
277266
end
278267
return ΔA
279268
end

0 commit comments

Comments
 (0)