Skip to content

Commit bff3678

Browse files
committed
Lower allocations in LQ/QR pushforwards
1 parent 6fddde3 commit bff3678

4 files changed

Lines changed: 85 additions & 30 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: 35 additions & 16 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,35 @@ 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) || (ΔQ !== ΔA && Base.mightalias(ΔA, ΔQ))
37+
ΔA = copy(ΔA)
38+
end
39+
3340
Q₁ = view(Q, 1:p, :)
3441
# Julia 1.13 `ldiv!` checks `istriu` on the parent, which
3542
# falls back to scalar indexing for a view of a GPU array
3643
L₁₁ = LowerTriangular(L[1:p, 1:p])
3744
L₂₁ = view(L, (p + 1):m, 1:p)
3845

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

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₁
4756
ΔQ₁Q₁ᴴ = ΔQ₁ * Q₁'
4857
M = ΔQ₁Q₁ᴴ + ΔQ₁Q₁ᴴ'
4958
diagview(M) ./= 2
5059
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)
60+
mul!(ΔL₁₁, L₁₁, M)
61+
mul!(ΔQ₁, M, Q₁, -1, 1)
62+
mul!(ΔL₂₁, L₂₁, mul!(ΔQ₁Q₁ᴴ, ΔQ₁, Q₁'), -1, 1)
5563

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₂₁
64+
zero!(view(ΔQ, (p + 1):size(ΔQ, 1), :))
6165
if p == minmn && size(Q, 1) > minmn
6266
Q₃ = view(Q, (minmn + 1):size(Q, 1), :)
6367
ΔQ₃ = view(ΔQ, (minmn + 1):size(Q, 1), :)
@@ -66,6 +70,21 @@ function lq_pushforward!(
6670
return ΔL, ΔQ
6771
end
6872

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

‎src/pushforwards/qr.jl‎

Lines changed: 36 additions & 14 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,36 @@ 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+
# `ΔQ === ΔA` is handled by ordering the operations so
34+
# that every column of ΔA is used before the same column of ΔQ is overwritten;
35+
# any other overlap between the tangents forces a copy of ΔA.
36+
if Base.mightalias(ΔA, ΔR) || (ΔQ !== ΔA && Base.mightalias(ΔA, ΔQ))
37+
ΔA = copy(ΔA)
38+
end
39+
3340
Q₁ = view(Q, :, 1:p)
3441
R₁₁ = UpperTriangular(view(R, 1:p, 1:p))
3542
R₁₂ = view(R, 1:p, (p + 1):n)
3643

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

40-
ΔQ₁ = ΔA₁ / R₁₁
4154
Q₁ᴴΔQ₁ = Q₁' * ΔQ₁
4255
M = Q₁ᴴΔQ₁ + Q₁ᴴΔQ₁'
4356
diagview(M) ./= 2
4457
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)
58+
mul!(ΔR₁₁, M, R₁₁)
59+
mul!(ΔQ₁, Q₁, M, -1, 1)
60+
mul!(ΔR₁₂, mul!(Q₁ᴴΔQ₁, Q₁', ΔQ₁), R₁₂, -1, 1)
4961

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₁₂
62+
zero!(view(ΔQ, :, (p + 1):size(ΔQ, 2)))
5563
if p == minmn && size(Q, 2) > minmn
5664
# extra columns in the case of qr_full, orthogonality to Q₁ fixes their component along Q₁
5765
Q₃ = view(Q, :, (minmn + 1):size(Q, 2))
@@ -61,6 +69,20 @@ function qr_pushforward!(
6169
return ΔQ, ΔR
6270
end
6371

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

0 commit comments

Comments
 (0)