Skip to content

Commit 5664502

Browse files
authored
Lower allocations in LQ/QR pushforwards (#326)
* Lower allocations in LQ/QR pushforwards * Try in-place project hermitian * Fix project_hermitian * Drop the Q checks * restore some views * Use scale * Formatter * Fix tests * Comments
1 parent 1830245 commit 5664502

4 files changed

Lines changed: 90 additions & 36 deletions

File tree

‎src/MatrixAlgebraKit.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ include("common/defaults.jl")
9393
include("common/householder.jl")
9494
include("common/initialization.jl")
9595
include("common/pullbacks.jl")
96+
include("common/pushforwards.jl")
9697
include("common/safemethods.jl")
9798
include("common/view.jl")
9899
include("common/regularinv.jl")

‎src/common/pushforwards.jl‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function _diagonal_qr_lq_pushforward!(Δa, q, l_or_r, Δq, Δl_or_r, p::Int)
2+
Base.mightalias(Δa, Δl_or_r) && (Δa = copy(Δa))
3+
Δa₁ = view(Δa, 1:p)
4+
q₁ = view(q, 1:p)
5+
l_or_r₁ = view(l_or_r, 1:p)
6+
Δq₁ = view(Δq, 1:p)
7+
Δl_or_r₁ = view(Δl_or_r, 1:p)
8+
@. Δl_or_r₁ = real(conj(q₁) * Δa₁)
9+
@. Δq₁ = (Δa₁ - q₁ * Δl_or_r₁) / l_or_r₁
10+
zero!(view(Δq, (p + 1):length(Δq)))
11+
zero!(view(Δl_or_r, (p + 1):length(Δl_or_r)))
12+
return Δq, Δl_or_r
13+
end

‎src/pushforwards/lq.jl‎

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ remaining rows of `Q` and columns of `L` are set to zero. Similarly, for `lq_ful
1313
rows of `Q` are only determined up to a unitary rotation, and only their gauge-invariant
1414
tangent component along the first `r` rows of `Q` is computed.
1515
16+
The tangent `ΔQ` may be the same array as `ΔA` (as happens for an in-place `lq_compact!`
17+
with `Q === A`), in which case `ΔA` is overwritten.
18+
1619
See also [`qr_pushforward!`](@ref).
1720
"""
1821
function lq_pushforward!(
@@ -30,34 +33,36 @@ function lq_pushforward!(
3033
p = lq_rank(L; rank_atol)
3134
(m, n) == size(ΔA) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of L*Q ($m, $n)"))
3235

36+
if Base.mightalias(ΔA, ΔL)
37+
ΔA = copy(ΔA)
38+
end
39+
40+
ΔA₁ = view(ΔA, 1:p, :)
41+
ΔA₂ = view(ΔA, (p + 1):m, :)
3342
Q₁ = view(Q, 1:p, :)
3443
# Julia 1.13 `ldiv!` checks `istriu` on the parent, which
3544
# falls back to scalar indexing for a view of a GPU array
3645
L₁₁ = LowerTriangular(L[1:p, 1:p])
3746
L₂₁ = view(L, (p + 1):m, 1:p)
3847

39-
ΔA₁ = view(ΔA, 1:p, :)
40-
ΔA₂ = view(ΔA, (p + 1):m, :)
48+
ΔQ₁ = view(ΔQ, 1:p, :)
49+
ΔL₁₁ = view(ΔL, 1:p, 1:p)
50+
ΔL₂₁ = view(ΔL, (p + 1):m, 1:p)
51+
52+
zero!(ΔL)
53+
54+
ΔQ === ΔA || copyto!(ΔQ₁, ΔA₁)
55+
ldiv!(L₁₁, ΔQ₁)
4156

42-
# compute everything from ΔA before writing to ΔL and ΔQ, which may alias it
43-
# (e.g. `lq_compact!` of a `Diagonal` returns `Q === A`)
44-
# TODO rework this into two versions, one for `Q === A` and one for
45-
# `Q !== A`, to minimize allocations
46-
ΔQ₁ = L₁₁ \ ΔA₁
4757
ΔQ₁Q₁ᴴ = ΔQ₁ * Q₁'
48-
M = ΔQ₁Q₁ᴴ + ΔQ₁Q₁ᴴ'
49-
diagview(M) ./= 2
50-
view(M, uppertriangularind(M)) .= zero(eltype(M))
51-
ΔL₁₁ = L₁₁ * M
52-
ΔQ₁ = mul!(ΔQ₁, M, Q₁, -1, 1)
53-
ΔL₂₁ = ΔA₂ * Q₁'
54-
ΔL₂₁ = mul!(ΔL₂₁, L₂₁, ΔQ₁ * Q₁', -1, 1)
58+
M = project_hermitian!(ΔQ₁Q₁ᴴ)
59+
_scale(i, j) = ifelse(i == j, 1, ifelse(i < j, 0, 2))
60+
M .*= _scale.(1:p, transpose(1:p))
61+
mul!(ΔL₁₁, L₁₁, M)
62+
mul!(ΔQ₁, M, Q₁, -1, 1)
63+
mul!(ΔL₂₁, ΔA₂ - L₂₁ * ΔQ₁, Q₁', 1, 1)
5564

56-
zero!(ΔL)
57-
zero!(ΔQ)
58-
view(ΔQ, 1:p, :) .= ΔQ₁
59-
view(ΔL, 1:p, 1:p) .= ΔL₁₁
60-
view(ΔL, (p + 1):m, 1:p) .= ΔL₂₁
65+
zero!(view(ΔQ, (p + 1):size(ΔQ, 1), :))
6166
if p == minmn && size(Q, 1) > minmn
6267
Q₃ = view(Q, (minmn + 1):size(Q, 1), :)
6368
ΔQ₃ = view(ΔQ, (minmn + 1):size(Q, 1), :)
@@ -66,6 +71,21 @@ function lq_pushforward!(
6671
return ΔL, ΔQ
6772
end
6873

74+
function lq_pushforward!(
75+
ΔA::Diagonal, A, LQ::Tuple{Diagonal, Diagonal}, ΔLQ::Tuple{Diagonal, Diagonal};
76+
rank_atol::Real = default_pullback_rank_atol(LQ[1]), kwargs...
77+
)
78+
L, Q = LQ
79+
isempty(Q) && !isempty(L) &&
80+
throw(ArgumentError("LQ differentiation with an omitted Q output is not supported; compute both L and Q."))
81+
ΔL, ΔQ = ΔLQ
82+
size(ΔA) == size(Q) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of L*Q $(size(Q))"))
83+
p = lq_rank(L; rank_atol)
84+
_diagonal_qr_lq_pushforward!(diagview(ΔA), diagview(Q), diagview(L), diagview(ΔQ), diagview(ΔL), p)
85+
return ΔL, ΔQ
86+
end
87+
88+
6989
"""
7090
lq_null_pushforward!(ΔA, A, Nᴴ, ΔNᴴ; kwargs...)
7191

‎src/pushforwards/qr.jl‎

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ columns of `Q` and the first `r` rows of `R` are differentiable, and the tangent
1212
remaining columns of `Q` and rows of `R` are set to zero. Similarly, for `qr_full` the extra
1313
columns of `Q` are only determined up to a unitary rotation, and only their gauge-invariant
1414
tangent component along the first `r` columns of `Q` is computed.
15+
16+
The tangent `ΔQ` may be the same underlying data as `ΔA`, in which case `ΔA` is overwritten.
1517
"""
1618
function qr_pushforward!(
1719
ΔA, A, QR, ΔQR;
@@ -28,30 +30,34 @@ function qr_pushforward!(
2830
p = qr_rank(R; rank_atol)
2931
(m, n) == size(ΔA) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of Q*R ($m, $n)"))
3032

31-
# TODO rework this into two versions, one for `Q === A` and one for
32-
# `Q !== A`, to minimize allocations
33+
if Base.mightalias(ΔA, ΔR)
34+
ΔA = copy(ΔA)
35+
end
36+
37+
ΔA₁ = view(ΔA, :, 1:p)
38+
ΔA₂ = view(ΔA, :, (p + 1):n)
3339
Q₁ = view(Q, :, 1:p)
3440
R₁₁ = UpperTriangular(view(R, 1:p, 1:p))
3541
R₁₂ = view(R, 1:p, (p + 1):n)
3642

37-
ΔA₁ = view(ΔA, :, 1:p)
38-
ΔA₂ = view(ΔA, :, (p + 1):n)
43+
ΔQ₁ = view(ΔQ, :, 1:p)
44+
ΔR₁₁ = view(ΔR, 1:p, 1:p)
45+
ΔR₁₂ = view(ΔR, 1:p, (p + 1):n)
46+
47+
zero!(ΔR)
48+
49+
ΔQ === ΔA || copyto!(ΔQ₁, ΔA₁)
50+
rdiv!(ΔQ₁, R₁₁)
3951

40-
ΔQ₁ = ΔA₁ / R₁₁
4152
Q₁ᴴΔQ₁ = Q₁' * ΔQ₁
42-
M = Q₁ᴴΔQ₁ + Q₁ᴴΔQ₁'
43-
diagview(M) ./= 2
44-
view(M, lowertriangularind(M)) .= zero(eltype(M))
45-
ΔR₁₁ = M * R₁₁
46-
ΔQ₁ = mul!(ΔQ₁, Q₁, M, -1, 1)
47-
ΔR₁₂ = Q₁' * ΔA₂
48-
ΔR₁₂ = mul!(ΔR₁₂, Q₁' * ΔQ₁, R₁₂, -1, 1)
53+
M = project_hermitian!(Q₁ᴴΔQ₁)
54+
_scale(i, j) = ifelse(i == j, 1, ifelse(i > j, 0, 2))
55+
M .*= _scale.(1:p, transpose(1:p))
56+
mul!(ΔR₁₁, M, R₁₁)
57+
mul!(ΔQ₁, Q₁, M, -1, 1)
58+
mul!(ΔR₁₂, Q₁', ΔA₂ - ΔQ₁ * R₁₂, 1, 1)
4959

50-
zero!(ΔQ)
51-
zero!(ΔR)
52-
view(ΔQ, :, 1:p) .= ΔQ₁
53-
view(ΔR, 1:p, 1:p) .= ΔR₁₁
54-
view(ΔR, 1:p, (p + 1):n) .= ΔR₁₂
60+
zero!(view(ΔQ, :, (p + 1):size(ΔQ, 2)))
5561
if p == minmn && size(Q, 2) > minmn
5662
# extra columns in the case of qr_full, orthogonality to Q₁ fixes their component along Q₁
5763
Q₃ = view(Q, :, (minmn + 1):size(Q, 2))
@@ -61,6 +67,20 @@ function qr_pushforward!(
6167
return ΔQ, ΔR
6268
end
6369

70+
function qr_pushforward!(
71+
ΔA::Diagonal, A, QR::Tuple{Diagonal, Diagonal}, ΔQR::Tuple{Diagonal, Diagonal};
72+
rank_atol::Real = default_pullback_rank_atol(QR[2]), kwargs...
73+
)
74+
Q, R = QR
75+
isempty(Q) && !isempty(R) &&
76+
throw(ArgumentError("QR differentiation with an omitted Q output is not supported; compute both Q and R."))
77+
ΔQ, ΔR = ΔQR
78+
size(ΔA) == size(Q) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of Q*R $(size(Q))"))
79+
p = qr_rank(R; rank_atol)
80+
_diagonal_qr_lq_pushforward!(diagview(ΔA), diagview(Q), diagview(R), diagview(ΔQ), diagview(ΔR), p)
81+
return ΔQ, ΔR
82+
end
83+
6484
"""
6585
qr_null_pushforward!(ΔA, A, N, ΔN; kwargs...)
6686

0 commit comments

Comments
 (0)