Skip to content

Commit 5acf51e

Browse files
committed
Use eigh for sylvester and support rank deficient cases
1 parent 5664502 commit 5acf51e

6 files changed

Lines changed: 85 additions & 38 deletions

File tree

‎ext/MatrixAlgebraKitAMDGPUExt/MatrixAlgebraKitAMDGPUExt.jl‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ using MatrixAlgebraKit: default_qr_algorithm, default_lq_algorithm, default_svd_
99
import MatrixAlgebraKit: geqrf!, ungqr!, unmqr!, gesvd!, gesdd!, gesvdx!, gesvdj!
1010
import MatrixAlgebraKit: gesvdj_batched!, gesdd_batched!, gesvd_batched!, gesvdx_batched!
1111
import MatrixAlgebraKit: heevj!, heevd!, heev!, heevx!
12-
import MatrixAlgebraKit: _sylvester, svd_rank, svd_pullback!
12+
import MatrixAlgebraKit: svd_rank, svd_pullback!
1313
using AMDGPU
1414
using LinearAlgebra
1515
using LinearAlgebra: BlasFloat
@@ -165,9 +165,4 @@ function MatrixAlgebraKit._mul_herm!(C::StridedROCMatrix{T}, A::StridedROCMatrix
165165
return C
166166
end
167167

168-
function _sylvester(A::AnyROCMatrix, B::AnyROCMatrix, C::AnyROCMatrix)
169-
hX = sylvester(collect(A), collect(B), collect(C))
170-
return ROCArray(hX)
171-
end
172-
173168
end

‎ext/MatrixAlgebraKitCUDAExt/MatrixAlgebraKitCUDAExt.jl‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ using MatrixAlgebraKit: default_qr_algorithm, default_lq_algorithm, default_svd_
99
import MatrixAlgebraKit: geqrf!, ungqr!, unmqr!, gesvd!, gesvdp!, gesvdr!, gesvdj!
1010
import MatrixAlgebraKit: gesvdj_batched!
1111
import MatrixAlgebraKit: heevj!, heevd!, geev!
12-
import MatrixAlgebraKit: _gpu_Xgesvdr!, _sylvester
12+
import MatrixAlgebraKit: _gpu_Xgesvdr!
1313
using CUDA, CUDA.cuBLAS
1414
using CUDA: i32
1515
using LinearAlgebra
@@ -88,11 +88,4 @@ function MatrixAlgebraKit._mul_herm!(C::StridedCuMatrix{T}, A::StridedCuMatrix{T
8888
return C
8989
end
9090

91-
function _sylvester(A::AnyCuMatrix, B::AnyCuMatrix, C::AnyCuMatrix)
92-
# https://github.com/JuliaGPU/CUDA.jl/issues/3021
93-
# to add native sylvester to CUDA
94-
hX = sylvester(collect(A), collect(B), collect(C))
95-
return CuArray(hX)
96-
end
97-
9891
end

‎src/MatrixAlgebraKit.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module MatrixAlgebraKit
33
using LinearAlgebra: LinearAlgebra
44
using LinearAlgebra: norm # TODO: eleminate if we use VectorInterface.jl?
55
using LinearAlgebra: mul!, rmul!, lmul!, adjoint!, rdiv!, ldiv!
6-
using LinearAlgebra: sylvester, lu!, diagm
6+
using LinearAlgebra: lu!, diagm
77
using LinearAlgebra: isposdef, issymmetric
88
using LinearAlgebra: Diagonal, Hermitian, diag, diagind, isdiag
99
using LinearAlgebra: UpperTriangular, LowerTriangular

‎src/common/pullbacks.jl‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ function iszerotangent end
1111
iszerotangent(::Any) = false
1212
iszerotangent(::Nothing) = true
1313

14-
# fallback
15-
_sylvester(A, B, C) = LinearAlgebra.sylvester(A, B, C)
14+
# Solve the Sylvester equation `P * X + X * P = C` for a Hermitian positive semidefinite `P`
15+
# with eigendecomposition `P = U * Diagonal(d) * U'`.
16+
function _sylvester_psd(U, d, C, tol)
17+
Y = U' * C * U
18+
dd = d .+ transpose(d)
19+
Δ = maximum(abs, (abs.(dd) .< tol) .* Y; init = zero(real(eltype(Y))))
20+
Y .*= inv_safe.(dd, tol)
21+
return U * Y * U', Δ
22+
end
1623

1724
"""
1825
select_indices(r::AbstractRange, ind)

‎src/pullbacks/polar.jl‎

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
"""
2-
left_polar_pullback!(ΔA, A, WP, ΔWP)
2+
left_polar_pullback!(
3+
ΔA, A, WP, ΔWP;
4+
rank_atol::Real = default_pullback_rank_atol(WP[2]),
5+
gauge_atol::Real = default_pullback_gauge_atol(ΔWP...)
6+
)
37
48
Adds the pullback from the left polar decomposition of `A` to `ΔA` given the output `WP` and
59
cotangent `ΔWP` of `left_polar(A)`.
10+
11+
Eigenvalues of `P` smaller than `rank_atol` are treated as zero, in which case `A` is rank
12+
deficient and `W` is not uniquely determined on the null space of `P`. Components of `ΔW`
13+
along this null space are discarded.
614
"""
7-
function left_polar_pullback!(ΔA::AbstractMatrix, A, WP, ΔWP; kwargs...)
15+
function left_polar_pullback!(
16+
ΔA::AbstractMatrix, A, WP, ΔWP;
17+
rank_atol::Real = default_pullback_rank_atol(WP[2]),
18+
gauge_atol::Real = default_pullback_gauge_atol(ΔWP...)
19+
)
820
# Extract the Polar components
921
W, P = WP
1022

@@ -13,28 +25,47 @@ function left_polar_pullback!(ΔA::AbstractMatrix, A, WP, ΔWP; kwargs...)
1325
if !iszerotangent(ΔP)
1426
ΔP = project_hermitian(ΔP)
1527
end
28+
D, U = eigh_full(P)
29+
d = diagview(D)
1630
M = zero(P)
1731
!iszerotangent(ΔW) && mul!(M, W', ΔW, 1, 1)
1832
!iszerotangent(ΔP) && mul!(M, ΔP, P, -1, 1)
19-
C = _sylvester(P, P, M' - M)
33+
C, Δgauge = _sylvester_psd(U, d, M - M', rank_atol)
2034
!iszerotangent(ΔP) && (C .+= ΔP)
2135
ΔA = mul!(ΔA, W, C, 1, 1)
2236
if !iszerotangent(ΔW)
23-
ΔWP = ΔW / P
24-
WdΔWP = W' * ΔWP
25-
ΔWP = mul!(ΔWP, W, WdΔWP, -1, 1)
26-
ΔA .+= ΔWP
37+
# (1 - W * W') * ΔW * pinv(P)
38+
ΔWU = ΔW * U
39+
WᴴΔWU = W' * ΔWU
40+
ΔWU = mul!(ΔWU, W, WᴴΔWU, -1, 1)
41+
Δgauge = max(Δgauge, maximum(abs, (abs.(transpose(d)) .< rank_atol) .* ΔWU; init = zero(real(eltype(ΔWU)))))
42+
ΔWU .*= transpose(inv_safe.(d, rank_atol))
43+
ΔA = mul!(ΔA, ΔWU, U', 1, 1)
2744
end
45+
Δgauge ≤ gauge_atol ||
46+
@warn "`left_polar` cotangents sensitive to gauge choice: (|Δgauge| = $Δgauge)"
2847
return ΔA
2948
end
3049

3150
"""
32-
right_polar_pullback!(ΔA, A, PWᴴ, ΔPWᴴ)
51+
right_polar_pullback!(
52+
ΔA, A, PWᴴ, ΔPWᴴ;
53+
rank_atol::Real = default_pullback_rank_atol(PWᴴ[1]),
54+
gauge_atol::Real = default_pullback_gauge_atol(ΔPWᴴ...)
55+
)
3356
34-
Adds the pullback from the left polar decomposition of `A` to `ΔA` given the output `PWᴴ`
57+
Adds the pullback from the right polar decomposition of `A` to `ΔA` given the output `PWᴴ`
3558
and cotangent `ΔPWᴴ` of `right_polar(A)`.
59+
60+
Eigenvalues of `P` smaller than `rank_atol` are treated as zero, in which case `A` is rank
61+
deficient and `Wᴴ` is not uniquely determined on the null space of `P`. Components of `ΔWᴴ`
62+
along this null space are discarded.
3663
"""
37-
function right_polar_pullback!(ΔA::AbstractMatrix, A, PWᴴ, ΔPWᴴ; kwargs...)
64+
function right_polar_pullback!(
65+
ΔA::AbstractMatrix, A, PWᴴ, ΔPWᴴ;
66+
rank_atol::Real = default_pullback_rank_atol(PWᴴ[1]),
67+
gauge_atol::Real = default_pullback_gauge_atol(ΔPWᴴ...)
68+
)
3869
# Extract the Polar components
3970
P, Wᴴ = PWᴴ
4071

@@ -43,17 +74,24 @@ function right_polar_pullback!(ΔA::AbstractMatrix, A, PWᴴ, ΔPWᴴ; kwargs...
4374
if !iszerotangent(ΔP)
4475
ΔP = project_hermitian(ΔP)
4576
end
77+
D, U = eigh_full(P)
78+
d = diagview(D)
4679
M = zero(P)
4780
!iszerotangent(ΔWᴴ) && mul!(M, ΔWᴴ, Wᴴ', 1, 1)
4881
!iszerotangent(ΔP) && mul!(M, P, ΔP, -1, 1)
49-
C = _sylvester(P, P, M' - M)
82+
C, Δgauge = _sylvester_psd(U, d, M - M', rank_atol)
5083
!iszerotangent(ΔP) && (C .+= ΔP)
5184
ΔA = mul!(ΔA, C, Wᴴ, 1, 1)
5285
if !iszerotangent(ΔWᴴ)
53-
PΔWᴴ = P \ ΔWᴴ
54-
PΔWᴴW = PΔWᴴ * Wᴴ'
55-
PΔWᴴ = mul!(PΔWᴴ, PΔWᴴW, Wᴴ, -1, 1)
56-
ΔA .+= PΔWᴴ
86+
# pinv(P) * ΔWᴴ * (1 - Wᴴ' * Wᴴ)
87+
UᴴΔWᴴ = U' * ΔWᴴ
88+
UᴴΔWᴴW = UᴴΔWᴴ * Wᴴ'
89+
UᴴΔWᴴ = mul!(UᴴΔWᴴ, UᴴΔWᴴW, Wᴴ, -1, 1)
90+
Δgauge = max(Δgauge, maximum(abs, (abs.(d) .< rank_atol) .* UᴴΔWᴴ; init = zero(real(eltype(UᴴΔWᴴ)))))
91+
UᴴΔWᴴ .*= inv_safe.(d, rank_atol)
92+
ΔA = mul!(ΔA, U, UᴴΔWᴴ, 1, 1)
5793
end
94+
Δgauge ≤ gauge_atol ||
95+
@warn "`right_polar` cotangents sensitive to gauge choice: (|Δgauge| = $Δgauge)"
5896
return ΔA
5997
end

‎src/pushforwards/polar.jl‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
1-
function left_polar_pushforward!(ΔA, A, WP, ΔWP; kwargs...)
1+
function left_polar_pushforward!(
2+
ΔA, A, WP, ΔWP; rank_atol::Real = default_pullback_rank_atol(WP[2]), kwargs...
3+
)
24
W, P = WP
35
ΔW, ΔP = ΔWP
6+
D, U = eigh_full(P)
7+
d = diagview(D)
48
mul!(ΔP, adjoint(W), ΔA, +1, 0)
5-
K̇ = _sylvester(P, P, adjoint(ΔP) - ΔP)
6-
mul!(ΔW, ΔA, inv(P), +1, 0)
9+
K̇, = _sylvester_psd(U, d, ΔP - adjoint(ΔP), rank_atol)
10+
# ΔA * pinv(P)
11+
ΔAU = ΔA * U
12+
ΔAU .*= transpose(inv_safe.(d, rank_atol))
13+
mul!(ΔW, ΔAU, adjoint(U), +1, 0)
714
WᴴdAiP = W' * ΔW
815
mul!(ΔW, W, WᴴdAiP, -1, +1)
916
ΔW = mul!(ΔW, W, K̇, +1, +1)
1017
ΔP = mul!(ΔP, K̇, P, -1, +1)
1118
return (ΔW, ΔP)
1219
end
1320

14-
function right_polar_pushforward!(ΔA, A, PWᴴ, ΔPWᴴ; kwargs...)
21+
function right_polar_pushforward!(
22+
ΔA, A, PWᴴ, ΔPWᴴ; rank_atol::Real = default_pullback_rank_atol(PWᴴ[1]), kwargs...
23+
)
1524
P, Wᴴ = PWᴴ
1625
ΔP, ΔWᴴ = ΔPWᴴ
26+
D, U = eigh_full(P)
27+
d = diagview(D)
1728
mul!(ΔP, ΔA, adjoint(Wᴴ), +1, 0)
18-
K̇ = _sylvester(P, P, adjoint(ΔP) - ΔP)
19-
mul!(ΔWᴴ, inv(P), ΔA, +1, 0)
29+
K̇, = _sylvester_psd(U, d, ΔP - adjoint(ΔP), rank_atol)
30+
# pinv(P) * ΔA
31+
UᴴΔA = U' * ΔA
32+
UᴴΔA .*= inv_safe.(d, rank_atol)
33+
mul!(ΔWᴴ, U, UᴴΔA, +1, 0)
2034
iPdAW = ΔWᴴ * Wᴴ'
2135
mul!(ΔWᴴ, iPdAW, Wᴴ, -1, +1)
2236
ΔWᴴ = mul!(ΔWᴴ, K̇, Wᴴ, +1, +1)

0 commit comments

Comments
 (0)