Skip to content

Commit 599babb

Browse files
leburgelJutho
andauthored
Make svd_pullback! and eigh_pullback! cost proportional to the number of cotangent columns (#292)
* Make `svd_pullback!` and `eigh_pullback!` cost proportional to the number of cotangent columns * Use `ind` variable name and clarify what is actually happening * Use consistent names for indices and parts withing the rank * Update src/pullbacks/eigh.jl Co-authored-by: Jutho <Jutho@users.noreply.github.com> * Update src/pullbacks/eigh.jl Co-authored-by: Jutho <Jutho@users.noreply.github.com> * Update src/pullbacks/eigh.jl Co-authored-by: Jutho <Jutho@users.noreply.github.com> * Update src/pullbacks/eigh.jl Co-authored-by: Jutho <Jutho@users.noreply.github.com> * Update src/pullbacks/svd.jl Co-authored-by: Jutho <Jutho@users.noreply.github.com> * Use copies instead of views of the kept columns of U, Vᴴ and V Views with a vector index fall back to scalar indexing in mul! on GPU. * Rename the kept-column factors in svd_pullback! to Sₖ, Uₖ and Vᴴₖ * Use an explicit broadcast for the gauge check in check_and_prepare_svd_cotangents Co-authored-by: Jutho <Jutho@users.noreply.github.com> * Clarify the column indices in check_and_prepare_svd_cotangents * some changes for clarity and consistency * format fix * fix typos * Fix the remaining errors in the svd and eigh pullbacks * some more changes for GPU * more fixes * more gpu view fixes * one more gpu attempt * Copy the selected singular values and ΔS entries instead of viewing them --------- Co-authored-by: Jutho <Jutho@users.noreply.github.com> Co-authored-by: Jutho Haegeman <jutho.haegeman@ugent.be>
1 parent 018a00a commit 599babb

5 files changed

Lines changed: 149 additions & 119 deletions

File tree

‎src/common/pullbacks.jl‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,20 @@ function accelerative_smith_iteration!(X, Xₙ, G, w, atol, maxiter)
7171
end
7272
return X
7373
end
74+
75+
"""
76+
antihermitian_columns!(X, ind)
77+
78+
Given the columns `X = M[:, ind]` of a square matrix `M` that is nonzero only in its columns
79+
`ind`, overwrite `X` with the same columns of the antihermitian part `(M - M') / 2` and return it.
80+
Here `M'` is nonzero only in the rows `ind`, so it only contributes to the square block
81+
`X[ind, :] = M[ind, ind]` on the diagonal of `M`.
82+
"""
83+
function antihermitian_columns!(X, ind)
84+
# NOTE: all columns in order (e.g. from `ind = Colon()` in the pullback): the original in-place projection
85+
is_leading_index(ind, size(X, 1)) && return project_antihermitian!(X)
86+
Xdiag = project_antihermitian!(X[ind, :]) # the diagonal block M[ind, ind]
87+
X ./= 2
88+
X[ind, :] .= Xdiag
89+
return X
90+
end

‎src/pullbacks/eigh.jl‎

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,43 @@ 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
78
n, p = size(V)
8-
indD = select_indices(axes(D, 1), ind)
9-
indV = select_indices(axes(V, 2), ind)
9+
ind₀ = select_indices(axes(D, 1), ind)
1010
if !iszerotangent(ΔV)
1111
n == size(ΔV, 1) || throw(DimensionMismatch())
12-
length(indV) == size(ΔV, 2) || throw(DimensionMismatch())
13-
if is_leading_index(indV, p)
14-
ΔV₁ = copy(ΔV)
15-
else
16-
ΔV₁ = zero(V)
17-
ΔV₁[:, indV] = ΔV
18-
end
19-
VᴴΔV₁ = V' * ΔV₁
12+
length(ind₀) == size(ΔV, 2) || throw(DimensionMismatch())
13+
VᴴΔV₀ = V' * ΔV
2014
if p == n
21-
ΔV₊ = zero!(ΔV₁)
15+
ΔV₊ = zero(ΔV)
2216
else
23-
ΔV₊ = mul!(ΔV₁, V, VᴴΔV₁, -1, 1)
17+
ΔV₊ = mul!(copy(ΔV), V, VᴴΔV₀, -1, 1)
2418
end
25-
aVᴴΔV₁ = project_antihermitian!(VᴴΔV₁)
19+
aVᴴΔV₀ = antihermitian_columns!(VᴴΔV₀, ind₀)
2620
else
2721
ΔV₊ = nothing
28-
aVᴴΔV₁ = zero!(similar(V, (p, p)))
22+
aVᴴΔV₀ = zero!(similar(V, (p, length(ind₀))))
2923
end
3024

31-
bc = Base.broadcasted(transpose(D), D, aVᴴΔV₁) do d₁, d₂, v
32-
return abs(d₁ - d₂) < degeneracy_atol ? v : zero(v)
33-
end
34-
Δgauge = maximum(abs, Base.Broadcast.instantiate(bc); init = abs(zero(eltype(D))))
25+
D₀ = D[ind₀]
26+
gauge_part = (abs.(transpose(D₀) .- D) .< degeneracy_atol) .* aVᴴΔV₀
27+
Δgauge = maximum(abs, gauge_part; init = abs(zero(eltype(D))))
3528

3629
Δgauge ≤ gauge_atol ||
3730
@warn "`eigh` cotangents sensitive to gauge choice: (|Δgauge| = $Δgauge)"
3831

39-
aVᴴΔV₁ .*= inv_safe.(D' .- D, degeneracy_atol)
40-
VᴴAΔV = aVᴴΔV₁
32+
aVᴴΔV₀ .*= inv_safe.(transpose(D₀) .- D, degeneracy_atol)
33+
VᴴΔAV₀ = aVᴴΔV₀
4134

4235
if !iszerotangent(ΔDmat)
4336
ΔD = diagview(ΔDmat)
44-
length(indD) == length(ΔD) || throw(DimensionMismatch())
45-
VᴴAΔV[select_indices(diagind(VᴴAΔV), indD)] .+= real.(ΔD)
37+
length(ind₀) == length(ΔD) || throw(DimensionMismatch())
38+
VᴴΔAV₀[ind₀ .+ p .* (0:(length(ind₀) - 1))] .+= real.(ΔD)
4639
else
4740
ΔD = nothing
4841
end
4942

50-
return VᴴAΔV, ΔV₊
43+
return VᴴΔAV₀, ΔV₊, ind₀
5144
end
5245

5346
"""
@@ -86,11 +79,27 @@ function eigh_pullback!(
8679
iszero(n) && return ΔA
8780

8881
ΔDmat, ΔV = ΔDV
89-
VᴴΔAV, = check_and_prepare_eigh_cotangents(
82+
VᴴΔAV₀, _, ind₀ = check_and_prepare_eigh_cotangents(
9083
D, V, ΔDmat, ΔV, ind; degeneracy_atol, gauge_atol
9184
)
9285

93-
ΔA = mul!(ΔA, V * VᴴΔAV, V', 1, 1)
86+
# VᴴΔAV₀ is Hermitian and nonzero only in its rows and columns ind₀, which are VᴴΔAV₀' and VᴴΔAV₀.
87+
# For p = length(ind₀) ≤ n / 2, applying these two blocks directly, in O(n² p), is faster than forming VᴴΔAV.
88+
if 2 * length(ind₀) <= n
89+
V₀ = V[:, ind₀]
90+
ΔA = mul!(ΔA, V * VᴴΔAV₀, V₀', 1, 1)
91+
VᴴΔAV₀[ind₀, :] .= zero(eltype(VᴴΔAV₀))
92+
ΔA = mul!(ΔA, V₀, VᴴΔAV₀' * V', 1, 1)
93+
else
94+
if is_leading_index(ind₀, n) # NOTE: all columns in order (e.g. `ind = Colon()`): the original path
95+
VᴴΔAV = VᴴΔAV₀
96+
else
97+
VᴴΔAV = zero!(similar(VᴴΔAV₀, (n, n)))
98+
VᴴΔAV[ind₀, :] .= VᴴΔAV₀'
99+
VᴴΔAV[:, ind₀] .= VᴴΔAV₀
100+
end
101+
ΔA = mul!(ΔA, V * VᴴΔAV, V', 1, 1)
102+
end
94103
return ΔA
95104
end
96105
# Diagonal: do not specialize on `A`, since we may insert `A = nothing` to assert independence of `A` in the implementation

‎src/pullbacks/lq.jl‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ function check_and_prepare_lq_cotangents(
1717
ΔQ₃ = copy(view(ΔQ, (minmn + 1):size(Q, 1), :))
1818
ΔQ₃Q₁ᴴ = ΔQ₃ * Q₁'
1919
mul!(ΔQ₃, ΔQ₃Q₁ᴴ, Q₁, -1, 1)
20-
Δgauge_Q = norm(ΔQ₃, Inf)
20+
Δgauge_Q = maximum(abs, ΔQ₃; init = abs(zero(eltype(ΔQ))))
2121
mul!(ΔQ₁, ΔQ₃Q₁ᴴ', Q₃, -1, 1)
2222
elseif p == minmn
2323
Δgauge_Q = abs(zero(eltype(Q)))
2424
else
2525
ΔQ₂ = view(ΔQ, (p + 1):size(ΔQ, 1), :)
26-
Δgauge_Q = norm(ΔQ₂, Inf)
26+
Δgauge_Q = maximum(abs, ΔQ₂; init = abs(zero(eltype(ΔQ))))
2727
end
2828
Δgauge = max(Δgauge, Δgauge_Q)
2929
end
@@ -40,8 +40,8 @@ function check_and_prepare_lq_cotangents(
4040
I = lowertriangularind(ΔL₂₂)
4141
lower_inds = view(LinearIndices(ΔL), (p + 1):m, (p + 1):minmn)[I]
4242
ΔL₂₂lower = view(ΔL, lower_inds)
43-
Δgauge_L = norm(ΔL₂₂lower, Inf)
44-
Δgauge_L = max(Δgauge_L, norm(view(ΔL₂₂, diagind(ΔL₂₂)), Inf))
43+
Δgauge_L = maximum(abs, ΔL₂₂lower; init = abs(zero(eltype(ΔL))))
44+
Δgauge_L = max(Δgauge_L, maximum(abs, view(ΔL₂₂, diagind(ΔL₂₂)); init = zero(Δgauge_L)))
4545
Δgauge = max(Δgauge, Δgauge_L)
4646
end
4747
else
@@ -115,7 +115,7 @@ end
115115

116116
function check_lq_null_cotangents(Nᴴ, ΔNᴴ; gauge_atol::Real = default_pullback_gauge_atol(ΔNᴴ))
117117
aNᴴΔN = project_antihermitian!(Nᴴ * ΔNᴴ')
118-
Δgauge = norm(aNᴴΔN)
118+
Δgauge = maximum(abs, aNᴴΔN; init = abs(zero(eltype(aNᴴΔN))))
119119
Δgauge ≤ gauge_atol ||
120120
@warn "`lq_null` cotangent sensitive to gauge choice: (|Δgauge| = $Δgauge)"
121121
return

‎src/pullbacks/qr.jl‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ function check_and_prepare_qr_cotangents(
1818
Q₃ = view(Q, :, (minmn + 1):size(Q, 2))
1919
Q₁ᴴΔQ₃ = Q₁' * ΔQ₃
2020
mul!(ΔQ₃, Q₁, Q₁ᴴΔQ₃, -1, 1)
21-
Δgauge_Q = norm(ΔQ₃, Inf)
21+
Δgauge_Q = maximum(abs, ΔQ₃; init = abs(zero(eltype(ΔQ))))
2222
mul!(ΔQ₁, Q₃, Q₁ᴴΔQ₃', -1, 1)
2323
else
2424
ΔQ₂₃ = view(ΔQ, :, (p + 1):size(Q, 2))
25-
Δgauge_Q = norm(ΔQ₂₃, Inf)
25+
Δgauge_Q = maximum(abs, ΔQ₂₃; init = abs(zero(eltype(ΔQ))))
2626
end
2727
Δgauge = max(Δgauge, Δgauge_Q)
2828
end
@@ -39,8 +39,8 @@ function check_and_prepare_qr_cotangents(
3939
I = uppertriangularind(ΔR₂₂)
4040
upper_inds = view(LinearIndices(ΔR), (p + 1):minmn, (p + 1):n)[I]
4141
ΔR₂₂upper = view(ΔR, upper_inds)
42-
Δgauge_R = norm(ΔR₂₂upper, Inf)
43-
Δgauge_R = max(Δgauge_R, norm(view(ΔR₂₂, diagind(ΔR₂₂)), Inf))
42+
Δgauge_R = maximum(abs, ΔR₂₂upper; init = abs(zero(eltype(ΔR))))
43+
Δgauge_R = max(Δgauge_R, maximum(abs, view(ΔR₂₂, diagind(ΔR₂₂)); init = zero(Δgauge_R)))
4444
Δgauge = max(Δgauge, Δgauge_R)
4545
end
4646
else
@@ -121,7 +121,7 @@ end
121121

122122
function check_qr_null_cotangents(N, ΔN; gauge_atol::Real = default_pullback_gauge_atol(ΔN))
123123
aNᴴΔN = project_antihermitian!(N' * ΔN)
124-
Δgauge = norm(aNᴴΔN)
124+
Δgauge = maximum(abs, aNᴴΔN; init = abs(zero(eltype(aNᴴΔN))))
125125
Δgauge ≤ gauge_atol ||
126126
@warn "`qr_null` cotangent sensitive to gauge choice: (|Δgauge| = $Δgauge)"
127127
return

0 commit comments

Comments
 (0)