@@ -13,6 +13,9 @@ remaining rows of `Q` and columns of `L` are set to zero. Similarly, for `lq_ful
1313rows of `Q` are only determined up to a unitary rotation, and only their gauge-invariant
1414tangent 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+
1619See also [`qr_pushforward!`](@ref).
1720"""
1821function 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
6771end
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
0 commit comments