Skip to content

Commit e2b2cd5

Browse files
Jutholkdvos
authored andcommitted
more updates to pullbacks of eig and eigh
1 parent d4d6ce7 commit e2b2cd5

4 files changed

Lines changed: 134 additions & 115 deletions

File tree

‎src/pullbacks/eig.jl‎

100644100755
Lines changed: 105 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,53 @@
1-
function check_eig_cotangents(
2-
D, VᴴΔV;
3-
degeneracy_atol::Real = default_pullback_rank_atol(D),
4-
gauge_atol::Real = default_pullback_gauge_atol(VᴴΔV)
1+
function check_and_prepare_eig_cotangents(
2+
D, V, ViG, ΔDmat, ΔV, ind = Colon();
3+
degeneracy_atol::Real = default_pullback_rank_atol(S),
4+
gauge_atol::Real = default_pullback_gauge_atol(ΔDmat, ΔV)
55
)
6-
mask = abs.(transpose(D) .- D) .< degeneracy_atol
7-
Δgauge = norm(view(VᴴΔV, mask))
6+
7+
n, p = size(V)
8+
indD = axes(D, 1)[ind]
9+
indV = axes(V, 2)[ind]
10+
if !iszerotangent(ΔV)
11+
n == size(ΔV, 1) || throw(DimensionMismatch())
12+
length(indV) == size(ΔV, 2) || throw(DimensionMismatch())
13+
if indV == 1:p
14+
ΔV₁ = copy(ΔV)
15+
else
16+
ΔV₁ = zero(V)
17+
for (j, i) in enumerate(indV)
18+
ΔV₁[:, i] .= view(ΔV, :, j)
19+
end
20+
end
21+
VᴴΔV₁ = V' * ΔV₁
22+
if p == n
23+
ΔV₊ = zero!(ΔV₁)
24+
else
25+
ΔV₊ = mul!(ΔV₁, ViG, VᴴΔV₁, -1, 1)
26+
end
27+
else
28+
ΔV₊ = nothing
29+
VᴴΔV₁ = zero!(similar(V, (p, p)))
30+
end
31+
bc = Base.broadcasted(transpose(D), D, VᴴΔV₁) do d₁, d₂, v
32+
return abs(d₁ - d₂) < degeneracy_atol ? v : zero(v)
33+
end
34+
Δgauge = norm(bc, Inf)
35+
836
Δgauge ≤ gauge_atol ||
937
@warn "`eig` cotangents sensitive to gauge choice: (|Δgauge| = $Δgauge)"
10-
return
38+
39+
VᴴΔV₁ .*= conj.(inv_safe.(transpose(D) .- D, degeneracy_atol))
40+
VᴴAΔV = VᴴΔV₁
41+
42+
if !iszerotangent(ΔDmat)
43+
ΔD = diagview(ΔDmat)
44+
length(indD) == length(ΔD) || throw(DimensionMismatch())
45+
view(diagview(VᴴAΔV), indD) .+= ΔD
46+
else
47+
ΔD = nothing
48+
end
49+
50+
return VᴴAΔV, ΔV₊
1151
end
1252

1353
"""
@@ -39,51 +79,24 @@ function eig_pullback!(
3979

4080
# Basic size checks and determination
4181
Dmat, V = DV
42-
D = diagview(Dmat)
43-
ΔDmat, ΔV = ΔDV
4482
n = LinearAlgebra.checksquare(V)
83+
D = diagview(Dmat)
4584
n == length(D) || throw(DimensionMismatch())
4685
(n, n) == size(ΔA) || throw(DimensionMismatch())
86+
ViG = inv(V)'
4787

48-
if !iszerotangent(ΔV)
49-
n == size(ΔV, 1) || throw(DimensionMismatch())
50-
pV = size(ΔV, 2)
51-
VᴴΔV = fill!(similar(V), 0)
52-
indV = axes(V, 2)[ind]
53-
length(indV) == pV || throw(DimensionMismatch())
54-
mul!(view(VᴴΔV, :, indV), V', ΔV)
55-
56-
check_eig_cotangents(D, VᴴΔV; degeneracy_atol, gauge_atol)
57-
58-
VᴴΔV .*= conj.(inv_safe.(transpose(D) .- D, degeneracy_atol))
59-
60-
if !iszerotangent(ΔDmat)
61-
ΔDvec = diagview(ΔDmat)
62-
pD = length(ΔDvec)
63-
indD = axes(D, 1)[ind]
64-
length(indD) == pD || throw(DimensionMismatch())
65-
view(diagview(VᴴΔV), indD) .+= ΔDvec
66-
end
67-
PΔV = V' \ VᴴΔV
68-
if eltype(ΔA) <: Real
69-
ΔAc = mul!(VᴴΔV, PΔV, V') # recycle VdΔV memory
70-
ΔA .+= real.(ΔAc)
71-
else
72-
ΔA = mul!(ΔA, PΔV, V', 1, 1)
73-
end
74-
elseif !iszerotangent(ΔDmat)
75-
ΔDvec = diagview(ΔDmat)
76-
pD = length(ΔDvec)
77-
indD = axes(D, 1)[ind]
78-
length(indD) == pD || throw(DimensionMismatch())
79-
Vp = view(V, :, indD)
80-
PΔV = Vp' \ Diagonal(ΔDvec)
81-
if eltype(ΔA) <: Real
82-
ΔAc = PΔV * Vp'
83-
ΔA .+= real.(ΔAc)
84-
else
85-
ΔA = mul!(ΔA, PΔV, V', 1, 1)
86-
end
88+
ΔDmat, ΔV = ΔDV
89+
VᴴΔAV, = check_and_prepare_eig_cotangents(
90+
D, V, ViG, ΔDmat, ΔV, ind; degeneracy_atol, gauge_atol
91+
)
92+
93+
if eltype(ΔA) <: Real
94+
Z = ViG * VᴴΔAV
95+
ΔAc = mul!(VᴴΔAV, Z, V') # recycle VᴴΔAV
96+
ΔA .+= real.(ΔAc)
97+
else
98+
Z = ViG * VᴴΔAV
99+
ΔA = mul!(ΔA, Z, V', 1, 1)
87100
end
88101
return ΔA
89102
end
@@ -123,44 +136,56 @@ not small compared to `gauge_atol`.
123136
function eig_trunc_pullback!(
124137
ΔA::AbstractMatrix, A, DV, ΔDV;
125138
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
126-
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
139+
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]),
140+
maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence?
127141
)
128142

129143
# Basic size checks and determination
130144
Dmat, V = DV
131-
D = diagview(Dmat)
132-
ΔDmat, ΔV = ΔDV
133145
(n, p) = size(V)
134-
p == length(D) || throw(DimensionMismatch())
135146
(n, n) == size(ΔA) || throw(DimensionMismatch())
147+
D = diagview(Dmat)
148+
p == length(D) || throw(DimensionMismatch())
136149
G = V' * V
150+
ViG = V / LinearAlgebra.cholesky!(G)
137151

138-
if !iszerotangent(ΔV)
139-
(n, p) == size(ΔV) || throw(DimensionMismatch())
140-
VᴴΔV = V' * ΔV
141-
check_eig_cotangents(D, VᴴΔV; degeneracy_atol, gauge_atol)
142-
143-
ΔVperp = ΔV - V * inv(G) * VᴴΔV
144-
VᴴΔV .*= conj.(inv_safe.(transpose(D) .- D, degeneracy_atol))
145-
else
146-
VᴴΔV = zero(G)
147-
end
148-
149-
if !iszerotangent(ΔDmat)
150-
ΔDvec = diagview(ΔDmat)
151-
p == length(ΔDvec) || throw(DimensionMismatch())
152-
diagview(VᴴΔV) .+= ΔDvec
153-
end
154-
Z = V' \ VᴴΔV
152+
ΔDmat, ΔV = ΔDV
153+
VᴴΔAV, ΔV₊ = check_and_prepare_eig_cotangents(
154+
D, V, ViG, ΔDmat, ΔV; degeneracy_atol, gauge_atol
155+
)
156+
Z = ViG * VᴴΔAV
155157

156158
# add contribution from orthogonal complement
157-
PA = A - (A * V) / V
158-
Y = mul!(ΔVperp, PA', Z, 1, 1)
159-
X = _sylvester(PA', -Dmat', Y)
160-
Z .+= X
161-
159+
AP = mul!(complex.(A), V * Dmat, ViG', -1, 1)
160+
X₀ = iszerotangent(ΔV₊) ? AP' * Z : mul!(ΔV₊, AP', Z, 1, 1)
161+
X₀ ./= D'
162+
dabsmax = maximum(abs, D)
163+
AP ./= dabsmax
164+
D̄⁻¹ = dabsmax ./ conj.(D)
165+
X₁ = rmul!(AP' * X₀, Diagonal(D̄⁻¹))
166+
X₁ .+= X₀
167+
Xₖ, Xₖ₊₁ = X₁, X₀
168+
APₖ, APₖ₊₁ = AP * AP, AP
169+
D̄⁻¹ₖ, D̄⁻¹ₖ₊₁ = D̄⁻¹ .^ 2, D̄⁻¹
170+
for k in 1:maxiter
171+
Xₖ₊₁ = rmul!(mul!(Xₖ₊₁, APₖ', Xₖ), Diagonal(D̄⁻¹ₖ))
172+
if norm(Xₖ₊₁, Inf) < degeneracy_atol
173+
break
174+
end
175+
Xₖ₊₁ .+= Xₖ
176+
if k == maxiter
177+
@warn "Sylvester iteration did not converge after $k iterations, final norm of X: $(norm(Xₖ₊₁, Inf)))"
178+
break
179+
end
180+
D̄⁻¹ₖ₊₁ .= D̄⁻¹ₖ .^ 2
181+
APₖ₊₁ = mul!(APₖ₊₁, APₖ, APₖ)
182+
Xₖ, Xₖ₊₁ = Xₖ₊₁, Xₖ
183+
APₖ, APₖ₊₁ = APₖ₊₁, APₖ
184+
D̄⁻¹ₖ, D̄⁻¹ₖ₊₁ = D̄⁻¹ₖ₊₁, D̄⁻¹ₖ
185+
end
186+
Z .+= Xₖ
162187
if eltype(ΔA) <: Real
163-
ΔAc = Z * V'
188+
ΔAc = mul!(AP, Z, V') # recycle AP
164189
ΔA .+= real.(ΔAc)
165190
else
166191
ΔA = mul!(ΔA, Z, V', 1, 1)
@@ -211,15 +236,13 @@ across eigenvectors associated with degenerate eigenvalues), so the correspondin
211236
`ΔV` are projected out.
212237
"""
213238
function remove_eig_gauge_dependence!(
214-
ΔV, D, V, ind = Colon();
239+
ΔV, D, V;
215240
degeneracy_atol = MatrixAlgebraKit.default_pullback_gauge_atol(D)
216241
)
217-
indV = axes(V, 2)[ind]
218-
length(indV) == size(ΔV, 2) || throw(DimensionMismatch("Incompatible size of selected `ind` and `ΔV`"))
219-
Vp = view(V, :, indV)
220-
Ddiag = view(diagview(D), indV)
221-
gaugepart = Vp' * ΔV
242+
Ddiag = diagview(D)
243+
gaugepart = V' * ΔV
222244
gaugepart[abs.(transpose(Ddiag) .- Ddiag) .>= degeneracy_atol] .= 0
223-
mul!(ΔV, Vp / (Vp' * Vp), gaugepart, -1, 1)
245+
ViG = V / LinearAlgebra.cholesky!(V' * V)
246+
mul!(ΔV, ViG, gaugepart, -1, 1)
224247
return ΔV
225248
end

‎src/pullbacks/eigh.jl‎

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ function check_and_prepare_eigh_cotangents(
1919
end
2020
end
2121
VᴴΔV₁ = V' * ΔV₁
22-
ΔV₊ = mul!(ΔV₁, V, VᴴΔV₁, -1, 1)
22+
if p == n
23+
ΔV₊ = zero!(ΔV₁)
24+
else
25+
ΔV₊ = mul!(ΔV₁, V, VᴴΔV₁, -1, 1)
26+
end
2327
aVᴴΔV₁ = project_antihermitian!(VᴴΔV₁)
2428
else
2529
ΔV₊ = nothing
2630
aVᴴΔV₁ = zero!(similar(V, (p, p)))
2731
end
28-
bc = Base.broadcasted(D', D, aVᴴΔV₁) do d₁, d₂, v
32+
bc = Base.broadcasted(transpose(D), D, aVᴴΔV₁) do d₁, d₂, v
2933
return abs(d₁ - d₂) < degeneracy_atol ? v : zero(v)
3034
end
3135
Δgauge = norm(bc, Inf)
@@ -47,19 +51,6 @@ function check_and_prepare_eigh_cotangents(
4751
return VᴴAΔV, ΔV₊
4852
end
4953

50-
51-
function check_eigh_cotangents(
52-
D, aVᴴΔV;
53-
degeneracy_atol::Real = default_pullback_rank_atol(D),
54-
gauge_atol::Real = default_pullback_gauge_atol(aVᴴΔV)
55-
)
56-
mask = abs.(D' .- D) .< degeneracy_atol
57-
Δgauge = norm(view(aVᴴΔV, mask))
58-
Δgauge ≤ gauge_atol ||
59-
@warn "`eigh` cotangents sensitive to gauge choice: (|Δgauge| = $Δgauge)"
60-
return
61-
end
62-
6354
"""
6455
eigh_pullback!(
6556
ΔA::AbstractMatrix, A, DV, ΔDV, [ind];
@@ -93,13 +84,13 @@ function eigh_pullback!(
9384
D = diagview(Dmat)
9485
n == length(D) || throw(DimensionMismatch())
9586
(n, n) == size(ΔA) || throw(DimensionMismatch())
96-
D = diagview(Dmat)
9787

9888
ΔDmat, ΔV = ΔDV
9989
VᴴΔAV, = check_and_prepare_eigh_cotangents(
10090
D, V, ΔDmat, ΔV, ind; degeneracy_atol, gauge_atol
10191
)
102-
ΔA = mul!(ΔA, V, VᴴΔAV * V', 1, 1)
92+
93+
ΔA = mul!(ΔA, V * VᴴΔAV, V', 1, 1)
10394
return ΔA
10495
end
10596
function eigh_pullback!(
@@ -139,27 +130,24 @@ function eigh_trunc_pullback!(
139130
ΔA::AbstractMatrix, A, DV, ΔDV;
140131
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
141132
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2]),
142-
maxiter::Int = 100
133+
maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence?
143134
)
144135

145136
# Basic size checks and determination
146137
Dmat, V = DV
147138
(n, p) = size(V)
148-
(n, n) == size(ΔA) || throw(DimensionMismatch())
149139
D = diagview(Dmat)
150140
p == length(D) || throw(DimensionMismatch())
141+
(n, n) == size(ΔA) || throw(DimensionMismatch())
151142

152143
ΔDmat, ΔV = ΔDV
153144
VᴴΔAV, ΔV₊ = check_and_prepare_eigh_cotangents(
154145
D, V, ΔDmat, ΔV; degeneracy_atol, gauge_atol
155146
)
156-
ΔAV = V * VᴴΔAV
157-
ΔA = mul!(ΔA, ΔAV, V', 1, 1)
158-
147+
Z = V * VᴴΔAV
159148
if !iszerotangent(ΔV₊)
160149
X₀ = rdiv!(ΔV₊, Diagonal(D))
161-
VD = mul!(ΔAV, V, Dmat) # recycle ΔAV
162-
AP = mul!(copy(A), VD, V', -1, 1)
150+
AP = mul!(copy(A), V * Dmat, V', -1, 1)
163151
dabsmax = maximum(abs, D)
164152
AP ./= dabsmax
165153
D⁻¹ = dabsmax ./ D
@@ -184,7 +172,15 @@ function eigh_trunc_pullback!(
184172
APₖ, APₖ₊₁ = APₖ₊₁, APₖ
185173
D⁻¹ₖ, D⁻¹ₖ₊₁ = D⁻¹ₖ₊₁, D⁻¹ₖ
186174
end
187-
ΔA = project_hermitian!(mul!(ΔA, Xₖ, V', 1, 1))
175+
Z .+= Xₖ
176+
# we cannot directly multiply Z * V' into ΔA, because we have to
177+
# take the Hermitian part, and cannot apply project_hermitian! to
178+
# the current contents of ΔA
179+
ΔA′ = project_hermitian!(mul!(AP, Z, V', 1, 1)) # recycle AP
180+
ΔA .+= ΔA′
181+
else
182+
# in this case, Z * V' is automatically Hermitian, so we can directly add it to ΔA
183+
ΔA = mul!(ΔA, Z, V', 1, 1)
188184
end
189185
return ΔA
190186
end

‎src/pullbacks/svd.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ function svd_trunc_pullback!(
210210
rank_atol::Real = 0,
211211
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
212212
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...),
213-
maxiter::Int = 100,
213+
maxiter::Int = 100 # TODO: better default, depending on expected number of steps using quadratic convergence?
214214
)
215215
# Extract the SVD components
216216
U, Smat, Vᴴ = USVᴴ

‎test/testsuite/chainrules.jl‎

100644100755
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,13 @@ function test_chainrules_eig(
258258
output_tangent = ΔDVtrunc, atol = atol, rtol = rtol
259259
)
260260
ind = MatrixAlgebraKit.findtruncated(diagview(DV[1]), truncalg.trunc)
261-
dA1 = MatrixAlgebraKit.eig_pullback!(zero(A), A, DV, ΔDVtrunc, ind)
262-
dA2 = MatrixAlgebraKit.eig_trunc_pullback!(zero(A), A, DVtrunc, ΔDVtrunc)
263-
@test isapprox(dA1, dA2; atol = atol, rtol = rtol)
261+
Ddiag = diagview(DV[1])
262+
p = sortperm(Ddiag, by = abs, rev = true)
263+
if abs(Ddiag[p[r + 1]]) < abs(Ddiag[p[r]])
264+
dA1 = MatrixAlgebraKit.eig_pullback!(zero(A), A, DV, ΔDVtrunc, ind)
265+
dA2 = MatrixAlgebraKit.eig_trunc_pullback!(zero(A), A, DVtrunc, ΔDVtrunc)
266+
@test isapprox(dA1, dA2; atol = atol, rtol = rtol)
267+
end
264268
end
265269
truncalg = TruncatedAlgorithm(alg, truncrank(5; by = real))
266270
DV, DVtrunc, ΔDV, ΔDVtrunc = ad_eig_trunc_setup(A, truncalg)
@@ -273,10 +277,6 @@ function test_chainrules_eig(
273277
cr_copy_eig_trunc_no_error, A, truncalg ⊢ NoTangent();
274278
output_tangent = ΔDVtrunc, atol = atol, rtol = rtol
275279
)
276-
ind = MatrixAlgebraKit.findtruncated(diagview(DV[1]), truncalg.trunc)
277-
dA1 = MatrixAlgebraKit.eig_pullback!(zero(A), A, DV, ΔDVtrunc, ind)
278-
dA2 = MatrixAlgebraKit.eig_trunc_pullback!(zero(A), A, DVtrunc, ΔDVtrunc)
279-
@test isapprox(dA1, dA2; atol = atol, rtol = rtol)
280280
end
281281
end
282282
end

0 commit comments

Comments
 (0)