Skip to content

Commit 0f142de

Browse files
committed
Working EIG + CUDA AD
1 parent 1d5b1d8 commit 0f142de

4 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎ext/MatrixAlgebraKitCUDAExt/MatrixAlgebraKitCUDAExt.jl‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ 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+
<<<<<<< HEAD
1112
import MatrixAlgebraKit: _gpu_Xgesvdr!, _sylvester, svd_rank, svd_pullback!, eigh_pullback!
13+
=======
14+
import MatrixAlgebraKit: _gpu_Xgesvdr!, _sylvester, svd_rank, svd_pullback!, eig_pullback!
15+
>>>>>>> 292f243 (Working EIG + CUDA AD)
1216
using CUDA, CUDA.cuBLAS
1317
using CUDA: i32
1418
using LinearAlgebra
@@ -209,4 +213,8 @@ function eigh_pullback!(ΔA::AnyCuMatrix, A, DV, ΔDV, ind::AnyCuVector; kwargs.
209213
return eigh_pullback!(ΔA, A, DV, ΔDV, collect(ind); kwargs...)
210214
end
211215

216+
function eig_pullback!(ΔA::AnyCuMatrix, A, DV, ΔDV, ind::AnyCuVector; kwargs...)
217+
return eig_pullback!(ΔA, A, DV, ΔDV, collect(ind); kwargs...)
218+
end
219+
212220
end

‎src/MatrixAlgebraKit.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using LinearAlgebra: norm # TODO: eleminate if we use VectorInterface.jl?
55
using LinearAlgebra: mul!, rmul!, lmul!, adjoint!, rdiv!, ldiv!
66
using LinearAlgebra: sylvester, lu!, diagm
77
using LinearAlgebra: isposdef, issymmetric
8-
using LinearAlgebra: Diagonal, diag, diagind, isdiag
8+
using LinearAlgebra: Diagonal, Hermitian, diag, diagind, isdiag
99
using LinearAlgebra: UpperTriangular, LowerTriangular
1010
using LinearAlgebra: BlasFloat, BlasReal, BlasComplex, BlasInt
1111

‎src/pullbacks/eig.jl‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function check_and_prepare_eig_cotangents(
3030
bc = Base.broadcasted(transpose(D), D, VᴴΔV₁) do d₁, d₂, v
3131
return abs(d₁ - d₂) < degeneracy_atol ? v : zero(v)
3232
end
33-
Δgauge = norm(bc, Inf)
33+
Δgauge = maximum(abs, bc; init = abs(zero(eltype(D))))
3434

3535
Δgauge ≤ gauge_atol ||
3636
@warn "`eig` cotangents sensitive to gauge choice: (|Δgauge| = $Δgauge)"
@@ -41,7 +41,8 @@ function check_and_prepare_eig_cotangents(
4141
if !iszerotangent(ΔDmat)
4242
ΔD = diagview(ΔDmat)
4343
length(indD) == length(ΔD) || throw(DimensionMismatch())
44-
view(diagview(VᴴAΔV), indD) .+= ΔD
44+
# needed to avoid GPUCompiler errors
45+
VᴴAΔV[diagind(VᴴAΔV)[indD]] .+= ΔD
4546
else
4647
ΔD = nothing
4748
end
@@ -243,7 +244,7 @@ function remove_eig_gauge_dependence!(
243244
Ddiag = diagview(D)
244245
gaugepart = V' * ΔV
245246
gaugepart[abs.(transpose(Ddiag) .- Ddiag) .>= degeneracy_atol] .= 0
246-
ViG = V / LinearAlgebra.cholesky!(V' * V)
247+
ViG = V / LinearAlgebra.cholesky!(Hermitian(V' * V))
247248
mul!(ΔV, ViG, gaugepart, -1, 1)
248249
return ΔV
249250
end

‎test/mooncake/eig.jl‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ for T in (BLASFloats..., GenericFloats...)
1818
AT = Diagonal{T, Vector{T}}
1919
TestSuite.test_mooncake_eig(AT, (m, m); atol = m * m * TestSuite.precision(T), rtol = m * m * TestSuite.precision(T))
2020
end
21+
if T ∈ BLASFloats && CUDA.functional()
22+
TestSuite.test_mooncake_eig(CuMatrix{T}, (m, m); atol = m * m * TestSuite.precision(T), rtol = m * m * TestSuite.precision(T))
23+
AT = Diagonal{T, CuVector{T}}
24+
TestSuite.test_mooncake_eig(AT, m; atol = m * m * TestSuite.precision(T), rtol = m * m * TestSuite.precision(T))
25+
end
2126
end

0 commit comments

Comments
 (0)