Skip to content

Commit 3baa70f

Browse files
Jutholeburgel
authored andcommitted
some more changes for GPU
1 parent e426cf1 commit 3baa70f

3 files changed

Lines changed: 32 additions & 27 deletions

File tree

‎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 = 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 = zero(eltype(ΔR₂₂upper)))
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 = zero(eltype(aNᴴΔN)))
125125
Δgauge ≤ gauge_atol ||
126126
@warn "`qr_null` cotangent sensitive to gauge choice: (|Δgauge| = $Δgauge)"
127127
return

‎src/pullbacks/svd.jl‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ function check_and_prepare_svd_cotangents(
1919

2020
# Only the columns ind₀ ⊆ 1:r of UᴴΔAV are computed.
2121
# By keeping its hermitian and antihermitian parts separate, we can reconstruct the full UᴴΔAV
22-
J = indS .<= r
23-
ind₀ = indS[J]
22+
J₁ = findall(<=(r), indS)
23+
J₂ = findall(>(r), indS)
24+
ind₀ = indS[J₁]
2425
k = length(ind₀)
2526
full_rank = (ind₀ == 1:minmn)
2627

@@ -29,11 +30,13 @@ function check_and_prepare_svd_cotangents(
2930
m == size(ΔU, 1) || throw(DimensionMismatch(lazy"first dimension of ΔU ($(size(ΔU, 1))) does not match first dimension of U ($m)"))
3031
length(indU) == size(ΔU, 2) || throw(DimensionMismatch(lazy"length of selected U columns ($(length(indU))) does not match second dimension of ΔU ($(size(ΔU, 2)))"))
3132
if indU == indS
32-
ΔU₀ = ΔU[:, J]
33+
ΔU₀ = ΔU[:, J₁]
34+
ΔgaugeU = max(ΔgaugeU, maximum(abs, view(ΔU, :, J₂); init = zero(ΔgaugeU)))
3335
elseif full_rank && indU == 1:m
3436
ΔU₀ = ΔU[:, ind₀]
35-
U₃ = view(U, :, (r + 1):m)
36-
ΔU₃ = ΔU[:, (r + 1):m]
37+
J₃ = (r + 1):m
38+
U₃ = view(U, :, J₃)
39+
ΔU₃ = ΔU[:, J₃]
3740
U₁ᴴΔU₃ = U₁' * ΔU₃ # gauge-invariant part
3841
mul!(ΔU₀, U₃, U₁ᴴΔU₃', -1, 1)
3942
mul!(ΔU₃, U₁, U₁ᴴΔU₃, -1, 1)
@@ -54,11 +57,13 @@ function check_and_prepare_svd_cotangents(
5457
n == size(ΔVᴴ, 2) || throw(DimensionMismatch(lazy"second dimension of ΔVᴴ ($(size(ΔVᴴ, 2))) does not match second dimension of Vᴴ ($n)"))
5558
length(indV) == size(ΔVᴴ, 1) || throw(DimensionMismatch(lazy"length of selected Vᴴ rows ($(length(indV))) does not match first dimension of ΔVᴴ ($(size(ΔVᴴ, 1)))"))
5659
if indV == indS
57-
ΔV₀ᴴ = ΔVᴴ[J, :]
60+
ΔV₀ᴴ = ΔVᴴ[J₁, :]
61+
ΔgaugeV = max(ΔgaugeV, maximum(abs, view(ΔVᴴ, J₂, :); init = zero(ΔgaugeV)))
5862
elseif full_rank && indV == 1:n
5963
ΔV₀ᴴ = ΔVᴴ[ind₀, :]
60-
V₃ᴴ = view(Vᴴ, (r + 1):n, :)
61-
ΔV₃ᴴ = ΔVᴴ[(r + 1):n, :]
64+
J₃ = (r + 1):n
65+
V₃ᴴ = view(Vᴴ, J₃, :)
66+
ΔV₃ᴴ = ΔVᴴ[J₃, :]
6267
V₁ᴴΔV₃ = V₁ᴴ * (ΔV₃ᴴ)' # gauge-invariant part
6368
mul!(ΔV₀ᴴ, V₁ᴴΔV₃, V₃ᴴ, -1, 1)
6469
mul!(ΔV₃ᴴ, V₁ᴴΔV₃', V₁ᴴ, -1, 1)
@@ -86,8 +91,7 @@ function check_and_prepare_svd_cotangents(
8691
ΔS = diagview(ΔSmat)
8792
length(indS) == length(ΔS) || throw(DimensionMismatch(lazy"length of selected S values ($(length(indS))) does not match length of ΔS ($(length(ΔS)))"))
8893
diagview(view(hUᴴΔAV₁₀, ind₀, :)) .+= real.(view(ΔS, J)) # the diagonal entries
89-
badΔS = view(ΔS, map(!, J))
90-
Δgauge = max(Δgauge, maximum(abs, badΔS; init = zero(Δgauge)))
94+
Δgauge = max(Δgauge, maximum(abs, view(ΔS, J₂); init = zero(Δgauge)))
9195
end
9296

9397
Δgauge ≤ gauge_atol ||
@@ -134,21 +138,21 @@ function svd_pullback!(
134138
r = svd_rank(S; rank_atol)
135139
iszero(r) && return ΔA
136140

137-
U₁ = view(U, :, 1:r)
138-
V₁ᴴ = view(Vᴴ, 1:r, :)
139-
S₁ = view(S, 1:r)
140-
141+
# Extract and check the cotangents
141142
ΔU, ΔSmat, ΔVᴴ = ΔUSVᴴ
142143
hUᴴΔAV₁₀, aUᴴΔAV₁₀, ΔU₊, ΔV₊ᴴ, ind₀ = check_and_prepare_svd_cotangents(
143144
U, S, Vᴴ, ΔU, ΔSmat, ΔVᴴ, r, ind; degeneracy_atol, gauge_atol
144145
)
145146

147+
U₀ = U[:, ind₀] # ind₀ is not necessarily a range
148+
U₁ = view(U, :, 1:r)
149+
V₀ᴴ = Vᴴ[ind₀, :]
150+
V₁ᴴ = view(Vᴴ, 1:r, :)
151+
S₀ = view(S, ind₀)
152+
146153
# UᴴΔAV is nonzero only in its columns ind₀, which are hUᴴΔAV₁₀ + aUᴴΔAV₁₀, and its rows ind₀,
147154
# which are hUᴴΔAV₁₀' - aUᴴΔAV₁₀'. For k ≤ r / 2, applying these two blocks directly, in O(m n k),
148155
# is faster than forming UᴴΔAV.
149-
S₀ = view(S, ind₀)
150-
U₀ = U[:, ind₀]
151-
V₀ᴴ = Vᴴ[ind₀, :]
152156
if 2 * length(ind₀) <= r
153157
ΔA = mul!(ΔA, U₁ * (hUᴴΔAV₁₀ + aUᴴΔAV₁₀), V₀ᴴ, 1, 1)
154158
hUᴴΔAV₁₀[ind₀, :] .= zero(eltype(hUᴴΔAV₁₀))
@@ -176,6 +180,7 @@ function svd_pullback!(
176180
end
177181
return ΔA
178182
end
183+
179184
# Diagonal: do not specialize on `A`, since we may insert `A = nothing` to assert independence of `A` in the implementation
180185
function svd_pullback!(
181186
ΔA::Diagonal, A, USVᴴ, ΔUSVᴴ, ind = Colon();

0 commit comments

Comments
 (0)