diff --git a/src/MatrixAlgebraKit.jl b/src/MatrixAlgebraKit.jl index 3a9f4b413..42b3f82e2 100644 --- a/src/MatrixAlgebraKit.jl +++ b/src/MatrixAlgebraKit.jl @@ -93,6 +93,7 @@ include("common/defaults.jl") include("common/householder.jl") include("common/initialization.jl") include("common/pullbacks.jl") +include("common/pushforwards.jl") include("common/safemethods.jl") include("common/view.jl") include("common/regularinv.jl") diff --git a/src/common/pushforwards.jl b/src/common/pushforwards.jl new file mode 100644 index 000000000..dd7b1c3c6 --- /dev/null +++ b/src/common/pushforwards.jl @@ -0,0 +1,13 @@ +function _diagonal_qr_lq_pushforward!(Δa, q, l_or_r, Δq, Δl_or_r, p::Int) + Base.mightalias(Δa, Δl_or_r) && (Δa = copy(Δa)) + Δa₁ = view(Δa, 1:p) + q₁ = view(q, 1:p) + l_or_r₁ = view(l_or_r, 1:p) + Δq₁ = view(Δq, 1:p) + Δl_or_r₁ = view(Δl_or_r, 1:p) + @. Δl_or_r₁ = real(conj(q₁) * Δa₁) + @. Δq₁ = (Δa₁ - q₁ * Δl_or_r₁) / l_or_r₁ + zero!(view(Δq, (p + 1):length(Δq))) + zero!(view(Δl_or_r, (p + 1):length(Δl_or_r))) + return Δq, Δl_or_r +end diff --git a/src/pushforwards/lq.jl b/src/pushforwards/lq.jl index bed3261a2..881ddf942 100644 --- a/src/pushforwards/lq.jl +++ b/src/pushforwards/lq.jl @@ -13,6 +13,9 @@ remaining rows of `Q` and columns of `L` are set to zero. Similarly, for `lq_ful rows of `Q` are only determined up to a unitary rotation, and only their gauge-invariant tangent component along the first `r` rows of `Q` is computed. +The tangent `ΔQ` may be the same array as `ΔA` (as happens for an in-place `lq_compact!` +with `Q === A`), in which case `ΔA` is overwritten. + See also [`qr_pushforward!`](@ref). """ function lq_pushforward!( @@ -30,34 +33,36 @@ function lq_pushforward!( p = lq_rank(L; rank_atol) (m, n) == size(ΔA) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of L*Q ($m, $n)")) + if Base.mightalias(ΔA, ΔL) + ΔA = copy(ΔA) + end + + ΔA₁ = view(ΔA, 1:p, :) + ΔA₂ = view(ΔA, (p + 1):m, :) Q₁ = view(Q, 1:p, :) # Julia 1.13 `ldiv!` checks `istriu` on the parent, which # falls back to scalar indexing for a view of a GPU array L₁₁ = LowerTriangular(L[1:p, 1:p]) L₂₁ = view(L, (p + 1):m, 1:p) - ΔA₁ = view(ΔA, 1:p, :) - ΔA₂ = view(ΔA, (p + 1):m, :) + ΔQ₁ = view(ΔQ, 1:p, :) + ΔL₁₁ = view(ΔL, 1:p, 1:p) + ΔL₂₁ = view(ΔL, (p + 1):m, 1:p) + + zero!(ΔL) + + ΔQ === ΔA || copyto!(ΔQ₁, ΔA₁) + ldiv!(L₁₁, ΔQ₁) - # compute everything from ΔA before writing to ΔL and ΔQ, which may alias it - # (e.g. `lq_compact!` of a `Diagonal` returns `Q === A`) - # TODO rework this into two versions, one for `Q === A` and one for - # `Q !== A`, to minimize allocations - ΔQ₁ = L₁₁ \ ΔA₁ ΔQ₁Q₁ᴴ = ΔQ₁ * Q₁' - M = ΔQ₁Q₁ᴴ + ΔQ₁Q₁ᴴ' - diagview(M) ./= 2 - view(M, uppertriangularind(M)) .= zero(eltype(M)) - ΔL₁₁ = L₁₁ * M - ΔQ₁ = mul!(ΔQ₁, M, Q₁, -1, 1) - ΔL₂₁ = ΔA₂ * Q₁' - ΔL₂₁ = mul!(ΔL₂₁, L₂₁, ΔQ₁ * Q₁', -1, 1) + M = project_hermitian!(ΔQ₁Q₁ᴴ) + _scale(i, j) = ifelse(i == j, 1, ifelse(i < j, 0, 2)) + M .*= _scale.(1:p, transpose(1:p)) + mul!(ΔL₁₁, L₁₁, M) + mul!(ΔQ₁, M, Q₁, -1, 1) + mul!(ΔL₂₁, ΔA₂ - L₂₁ * ΔQ₁, Q₁', 1, 1) - zero!(ΔL) - zero!(ΔQ) - view(ΔQ, 1:p, :) .= ΔQ₁ - view(ΔL, 1:p, 1:p) .= ΔL₁₁ - view(ΔL, (p + 1):m, 1:p) .= ΔL₂₁ + zero!(view(ΔQ, (p + 1):size(ΔQ, 1), :)) if p == minmn && size(Q, 1) > minmn Q₃ = view(Q, (minmn + 1):size(Q, 1), :) ΔQ₃ = view(ΔQ, (minmn + 1):size(Q, 1), :) @@ -66,6 +71,21 @@ function lq_pushforward!( return ΔL, ΔQ end +function lq_pushforward!( + ΔA::Diagonal, A, LQ::Tuple{Diagonal, Diagonal}, ΔLQ::Tuple{Diagonal, Diagonal}; + rank_atol::Real = default_pullback_rank_atol(LQ[1]), kwargs... + ) + L, Q = LQ + isempty(Q) && !isempty(L) && + throw(ArgumentError("LQ differentiation with an omitted Q output is not supported; compute both L and Q.")) + ΔL, ΔQ = ΔLQ + size(ΔA) == size(Q) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of L*Q $(size(Q))")) + p = lq_rank(L; rank_atol) + _diagonal_qr_lq_pushforward!(diagview(ΔA), diagview(Q), diagview(L), diagview(ΔQ), diagview(ΔL), p) + return ΔL, ΔQ +end + + """ lq_null_pushforward!(ΔA, A, Nᴴ, ΔNᴴ; kwargs...) diff --git a/src/pushforwards/qr.jl b/src/pushforwards/qr.jl index 7c84ba0c2..698256306 100644 --- a/src/pushforwards/qr.jl +++ b/src/pushforwards/qr.jl @@ -12,6 +12,8 @@ columns of `Q` and the first `r` rows of `R` are differentiable, and the tangent remaining columns of `Q` and rows of `R` are set to zero. Similarly, for `qr_full` the extra columns of `Q` are only determined up to a unitary rotation, and only their gauge-invariant tangent component along the first `r` columns of `Q` is computed. + +The tangent `ΔQ` may be the same underlying data as `ΔA`, in which case `ΔA` is overwritten. """ function qr_pushforward!( ΔA, A, QR, ΔQR; @@ -28,30 +30,34 @@ function qr_pushforward!( p = qr_rank(R; rank_atol) (m, n) == size(ΔA) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of Q*R ($m, $n)")) - # TODO rework this into two versions, one for `Q === A` and one for - # `Q !== A`, to minimize allocations + if Base.mightalias(ΔA, ΔR) + ΔA = copy(ΔA) + end + + ΔA₁ = view(ΔA, :, 1:p) + ΔA₂ = view(ΔA, :, (p + 1):n) Q₁ = view(Q, :, 1:p) R₁₁ = UpperTriangular(view(R, 1:p, 1:p)) R₁₂ = view(R, 1:p, (p + 1):n) - ΔA₁ = view(ΔA, :, 1:p) - ΔA₂ = view(ΔA, :, (p + 1):n) + ΔQ₁ = view(ΔQ, :, 1:p) + ΔR₁₁ = view(ΔR, 1:p, 1:p) + ΔR₁₂ = view(ΔR, 1:p, (p + 1):n) + + zero!(ΔR) + + ΔQ === ΔA || copyto!(ΔQ₁, ΔA₁) + rdiv!(ΔQ₁, R₁₁) - ΔQ₁ = ΔA₁ / R₁₁ Q₁ᴴΔQ₁ = Q₁' * ΔQ₁ - M = Q₁ᴴΔQ₁ + Q₁ᴴΔQ₁' - diagview(M) ./= 2 - view(M, lowertriangularind(M)) .= zero(eltype(M)) - ΔR₁₁ = M * R₁₁ - ΔQ₁ = mul!(ΔQ₁, Q₁, M, -1, 1) - ΔR₁₂ = Q₁' * ΔA₂ - ΔR₁₂ = mul!(ΔR₁₂, Q₁' * ΔQ₁, R₁₂, -1, 1) + M = project_hermitian!(Q₁ᴴΔQ₁) + _scale(i, j) = ifelse(i == j, 1, ifelse(i > j, 0, 2)) + M .*= _scale.(1:p, transpose(1:p)) + mul!(ΔR₁₁, M, R₁₁) + mul!(ΔQ₁, Q₁, M, -1, 1) + mul!(ΔR₁₂, Q₁', ΔA₂ - ΔQ₁ * R₁₂, 1, 1) - zero!(ΔQ) - zero!(ΔR) - view(ΔQ, :, 1:p) .= ΔQ₁ - view(ΔR, 1:p, 1:p) .= ΔR₁₁ - view(ΔR, 1:p, (p + 1):n) .= ΔR₁₂ + zero!(view(ΔQ, :, (p + 1):size(ΔQ, 2))) if p == minmn && size(Q, 2) > minmn # extra columns in the case of qr_full, orthogonality to Q₁ fixes their component along Q₁ Q₃ = view(Q, :, (minmn + 1):size(Q, 2)) @@ -61,6 +67,20 @@ function qr_pushforward!( return ΔQ, ΔR end +function qr_pushforward!( + ΔA::Diagonal, A, QR::Tuple{Diagonal, Diagonal}, ΔQR::Tuple{Diagonal, Diagonal}; + rank_atol::Real = default_pullback_rank_atol(QR[2]), kwargs... + ) + Q, R = QR + isempty(Q) && !isempty(R) && + throw(ArgumentError("QR differentiation with an omitted Q output is not supported; compute both Q and R.")) + ΔQ, ΔR = ΔQR + size(ΔA) == size(Q) || throw(DimensionMismatch("size of ΔA ($(size(ΔA))) does not match size of Q*R $(size(Q))")) + p = qr_rank(R; rank_atol) + _diagonal_qr_lq_pushforward!(diagview(ΔA), diagview(Q), diagview(R), diagview(ΔQ), diagview(ΔR), p) + return ΔQ, ΔR +end + """ qr_null_pushforward!(ΔA, A, N, ΔN; kwargs...)