Skip to content

Commit 0dfcb52

Browse files
kshyattKatharine HyattJutholkdvos
authored
Some basic svd forward rules and tests (#247)
* Some basic svd forward rules and tests * Try to fix Enzyme? * Use GPU safe rank calculation * Another fix for GPU * Some more fixes * Update src/pushforwards/svd.jl Co-authored-by: Jutho <Jutho@users.noreply.github.com> * A little cleanup * Some small fixes again * Use sylvester fallback * Fix diagms * Add a one! method for vectors * Use UniformScaling for diagm * some changes * more gauge fixing in eig/eigh * Get stuff working on CUDA * Fix missing import * Run more complex test cases * The case of the missing excalamtion point * More touchups for comments * Try invsafe for gaugefixing in pushforward * various changes * Format and zero_dual touchups * Update ext/MatrixAlgebraKitMooncakeExt/MatrixAlgebraKitMooncakeExt.jl Co-authored-by: Lukas Devos <ldevos98@gmail.com> --------- Co-authored-by: Katharine Hyatt <katharine.s.hyatt@gmail.com> Co-authored-by: Jutho <Jutho@users.noreply.github.com> Co-authored-by: Jutho Haegeman <jutho.haegeman@ugent.be> Co-authored-by: Lukas Devos <ldevos98@gmail.com>
1 parent 25d24fc commit 0dfcb52

17 files changed

Lines changed: 409 additions & 206 deletions

File tree

‎ext/MatrixAlgebraKitCUDAExt/MatrixAlgebraKitCUDAExt.jl‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ using MatrixAlgebraKit: CUSOLVER, LQViaTransposedQR, TruncationByValue, Abstract
88
using MatrixAlgebraKit: default_qr_algorithm, default_lq_algorithm, default_svd_algorithm, default_eig_algorithm, default_eigh_algorithm
99
import MatrixAlgebraKit: geqrf!, ungqr!, unmqr!, gesvd!, gesvdp!, gesvdr!, gesvdj!
1010
import MatrixAlgebraKit: heevj!, heevd!, geev!
11-
import MatrixAlgebraKit: _gpu_Xgesvdr!, _sylvester, svd_rank, svd_pullback!, eigh_pullback!, eig_pullback!
11+
import MatrixAlgebraKit: _gpu_Xgesvdr!, _sylvester, svd_rank, svd_pullback!, eigh_pullback!, eig_pullback!, svd_pushforward!
1212
using CUDA, CUDA.cuBLAS
1313
using CUDA: i32
1414
using LinearAlgebra
@@ -213,4 +213,14 @@ function eig_pullback!(ΔA::AnyCuMatrix, A, DV, ΔDV, ind::AnyCuVector; kwargs..
213213
return eig_pullback!(ΔA, A, DV, ΔDV, collect(ind); kwargs...)
214214
end
215215

216+
# have to override this as methods are missing in GPUArrays for the various
217+
# views of Diagonal of ΔA
218+
function svd_pushforward!(
219+
ΔA::Diagonal{T, <:CuVector{T}}, A, USVᴴ, ΔUSVᴴ, ind = Colon();
220+
rank_atol::Real = MatrixAlgebraKit.default_pullback_rank_atol(USVᴴ[2]),
221+
degeneracy_atol::Real = MatrixAlgebraKit.default_pullback_rank_atol(USVᴴ[2])
222+
) where {T}
223+
return MatrixAlgebraKit.svd_pushforward!(diagm(diagview(ΔA)), A, USVᴴ, ΔUSVᴴ, ind; rank_atol, degeneracy_atol)
224+
end
225+
216226
end

‎ext/MatrixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl‎

Lines changed: 90 additions & 73 deletions
Large diffs are not rendered by default.

‎ext/MatrixAlgebraKitMooncakeExt/MatrixAlgebraKitMooncakeExt.jl‎

Lines changed: 108 additions & 101 deletions
Large diffs are not rendered by default.

‎src/MatrixAlgebraKit.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ include("common/safemethods.jl")
9292
include("common/view.jl")
9393
include("common/regularinv.jl")
9494
include("common/matrixproperties.jl")
95+
include("common/utility.jl")
9596

9697
include("yalapack.jl")
9798
include("algorithms.jl")
@@ -132,6 +133,7 @@ include("pullbacks/polar.jl")
132133
include("pushforwards/polar.jl")
133134
include("pushforwards/eig.jl")
134135
include("pushforwards/eigh.jl")
136+
include("pushforwards/svd.jl")
135137

136138
include("precompile.jl")
137139

‎src/common/utility.jl‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function has_equal_storage(A::Diagonal, B::Diagonal)
2+
return diagview(A) === diagview(B)
3+
end
4+
function has_equal_storage(A::AbstractMatrix, B::AbstractMatrix)
5+
return A === B
6+
end
7+
8+
function has_equal_storage(A::Diagonal, B::AbstractVector)
9+
return diagview(A) === B
10+
end
11+
function has_equal_storage(A::AbstractVector, B::Diagonal)
12+
return A === diagview(B)
13+
end
14+
has_equal_storage(A::AbstractMatrix, B::AbstractVector) = false
15+
has_equal_storage(A::AbstractVector, B::AbstractMatrix) = false

‎src/implementations/eig.jl‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function eig_full!(A::Diagonal, DV, alg::DiagonalAlgorithm)
165165
D, V = DV
166166
diagA = diagview(A)
167167
I = sortperm(diagA; by = eig_sortby)
168-
if D === A
168+
if has_equal_storage(A, D)
169169
permute!(diagA, I)
170170
else
171171
diagview(D) .= view(diagA, I)
@@ -179,8 +179,8 @@ end
179179

180180
function eig_vals!(A::Diagonal, D::AbstractVector, alg::DiagonalAlgorithm)
181181
check_input(eig_vals!, A, D, alg)
182-
Ad = diagview(A)
183-
D === Ad || copy!(D, Ad)
182+
diagA = diagview(A)
183+
has_equal_storage(A, D) || copy!(D, diagA)
184184
sort!(D; by = eig_sortby)
185185
return D
186186
end

‎src/pullbacks/eig.jl‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,15 @@ function eig_pullback!(
101101
end
102102
return ΔA
103103
end
104+
# Diagonal: do not specialize on `A`, since we may insert `A = nothing` to assert independence of `A` in the implementation
104105
function eig_pullback!(
105106
ΔA::Diagonal, A, DV, ΔDV, ind = Colon();
106107
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
107108
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
108109
)
110+
# TODO:
111+
# If A and ΔA are diagonal, then V is a permutation matrix and so is inv(V) = V'.
112+
# Furthermore, since V̇ is 0, the pullback ΔV cannot contribute and we only have to unpermute ΔD.
109113
ΔA_full = zero!(similar(ΔA, size(ΔA)))
110114
ΔA_full = eig_pullback!(ΔA_full, A, DV, ΔDV, ind; degeneracy_atol, gauge_atol)
111115
diagview(ΔA) .+= diagview(ΔA_full)

‎src/pullbacks/eigh.jl‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,15 @@ function eigh_pullback!(
9494
ΔA = mul!(ΔA, V * VᴴΔAV, V', 1, 1)
9595
return ΔA
9696
end
97+
# Diagonal: do not specialize on `A`, since we may insert `A = nothing` to assert independence of `A` in the implementation
9798
function eigh_pullback!(
9899
ΔA::Diagonal, A, DV, ΔDV, ind = Colon();
99100
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
100101
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
101102
)
103+
# TODO:
104+
# If A and ΔA are diagonal, then V is a permutation matrix and so is inv(V) = V'.
105+
# Furthermore, since V̇ is 0, the pullback ΔV cannot contribute and we only have to unpermute ΔD.
102106
ΔA_full = zero!(similar(ΔA, size(ΔA)))
103107
ΔA_full = eigh_pullback!(ΔA_full, A, DV, ΔDV, ind; degeneracy_atol, gauge_atol)
104108
diagview(ΔA) .+= diagview(ΔA_full)

‎src/pullbacks/svd.jl‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,16 @@ function svd_pullback!(
171171
end
172172
return ΔA
173173
end
174+
# Diagonal: do not specialize on `A`, since we may insert `A = nothing` to assert independence of `A` in the implementation
174175
function svd_pullback!(
175176
ΔA::Diagonal, A, USVᴴ, ΔUSVᴴ, ind = Colon();
176177
rank_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
177178
degeneracy_atol::Real = default_pullback_rank_atol(USVᴴ[2]),
178179
gauge_atol::Real = default_pullback_gauge_atol(ΔUSVᴴ...)
179180
)
181+
# TODO:
182+
# If A and ΔA are diagonal, then U and V are permutation matrices (up to signs/phases).
183+
# Furthermore, since U̇ and V̇ are 0, the pullbacks ΔU and ΔV cannot contribute and we only have to unpermute ΔS.
180184
ΔA_full = zero!(similar(ΔA, size(ΔA)))
181185
ΔA_full = svd_pullback!(ΔA_full, A, USVᴴ, ΔUSVᴴ, ind; rank_atol, degeneracy_atol, gauge_atol)
182186
diagview(ΔA) .+= diagview(ΔA_full)

‎src/pushforwards/eig.jl‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
function eig_pushforward!(
22
ΔA, A, DV, ΔDV;
3-
degeneracy_atol::Real = default_pullback_rank_atol(DV[1]),
4-
gauge_atol::Real = default_pullback_gauge_atol(ΔDV[2])
3+
degeneracy_atol::Real = default_pullback_rank_atol(DV[1])
54
)
65
D, V = DV
76
ΔD, ΔV = ΔDV
@@ -12,11 +11,16 @@ function eig_pushforward!(
1211
end
1312
if !iszerotangent(ΔV)
1413
∂K .*= inv_safe.(transpose(diagview(D)) .- diagview(D), degeneracy_atol)
15-
mul!(ΔV, V, ∂K, 1, 0)
14+
mul!(ΔV, V, ∂K)
15+
if eltype(V) <: Complex # fix gauge for `gaugefix!` compatibility
16+
_, I = findmax(abs, V; dims = 1)
17+
infinitesimal_phases = imag.(ΔV[I] ./ V[I])
18+
ΔV .-= im .* V .* infinitesimal_phases
19+
end
1620
end
1721
return ΔDV
1822
end
1923

2024
function eig_vals_pushforward!(ΔA, A, DV, ΔD; kwargs...)
21-
return eig_pushforward!(ΔA, A, DV, (Diagonal(ΔD), nothing); kwargs...)
25+
return eig_pushforward!(ΔA, A, DV, (diagonal(ΔD), nothing); kwargs...)
2226
end

0 commit comments

Comments
 (0)