Skip to content

Commit 7a06e62

Browse files
kshyattKatharine HyattlkdvosJuthoKatharine Hyatt
authored
Batched SVD support for ROCSOLVER and CUSOLVER (#275)
* Batched SVD support for ROCSOLVER and CUSOLVER * Go back to QRIteration default * Fix default driver for CUDA * Fix typo * And fix bad check in yacusolver * One more API fix * Fix size of U and V bar * More API fixes * Remove redundant dummy one liners * Switch to batched_svd_f and get rid of Batched versions of algorithms * Fixup AMD default algos * Move ragged batch handling and cutoffs etc into MAK * Add support for a batched svd_via_adjoint for algos requiring tall matrices * Add batched svd_full too * Add special path for AMDGPU, implement gesvdx batching there, and fix some errors * Some fixes and actually test Bisection on AMD * A few more fixes for Bisection * Support svd_full for Bisection * Remove batched and non-batched bisection for now * Restore Bisection test * Avoid some allocs * one to one-liner and more checks for batch_size * Apply batched suggestions from code review Co-authored-by: Lukas Devos <ldevos98@gmail.com> Co-authored-by: Jutho <Jutho@users.noreply.github.com> * Fix sizes of dummy arrays * Don't return foreach * Missing batched Bisection piping * Refactor out gaugefixing for batches * Add a flag for whether the driver supports ragged batches * Dumb typos * Have batched_svd_compact use Diagonal * Fix dimension check in gesvdx_strided_batched! * supports_ragged_batch takes a driver, not an algorithm * Make sure to mark bisection as supporting svd full here * Move some batching utilities into a common file * Move supports_ragged_batch to Driver definition * Just put batches below algos * Put the basis completion in the implementation file * Move batch_size length checks into else * Apply batched suggestions from code review Co-authored-by: Jutho <Jutho@users.noreply.github.com> * Strides for safety * Use both alg and driver in the batch checks * batch_size checks for batched gesvdx * Force driver lookup for support checks * Dumb typo * Actually make sure matrices that are too big aren't sent to the batched function * Move batch_size check for CUSOLVER too * Reorder check_inputs * Add TODO for CUSOLVER * Use supports_pointer_batch and fix oversized test * Formatter * Check that inputs are preserved for in-place batch methods --------- Co-authored-by: Katharine Hyatt <katharine.hyatt@ugent.be> Co-authored-by: Lukas Devos <ldevos98@gmail.com> Co-authored-by: Jutho <Jutho@users.noreply.github.com> Co-authored-by: Katharine Hyatt <katharine.s.hyatt@gmail.com>
1 parent 2997686 commit 7a06e62

14 files changed

Lines changed: 1815 additions & 24 deletions

File tree

‎ext/MatrixAlgebraKitAMDGPUExt/MatrixAlgebraKitAMDGPUExt.jl‎

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,26 @@ using MatrixAlgebraKit: one!, zero!, uppertriangular!, lowertriangular!
66
using MatrixAlgebraKit: diagview, sign_safe
77
using MatrixAlgebraKit: ROCSOLVER, LQViaTransposedQR, TruncationStrategy, NoTruncation, TruncationByValue, AbstractAlgorithm
88
using MatrixAlgebraKit: default_qr_algorithm, default_lq_algorithm, default_svd_algorithm, default_eigh_algorithm
9-
import MatrixAlgebraKit: geqrf!, ungqr!, unmqr!, gesvd!, gesvdx!, gesvdj!
9+
import MatrixAlgebraKit: geqrf!, ungqr!, unmqr!, gesvd!, gesdd!, gesvdx!, gesvdj!
10+
import MatrixAlgebraKit: gesvdj_batched!, gesdd_batched!, gesvd_batched!, gesvdx_batched!
1011
import MatrixAlgebraKit: heevj!, heevd!, heev!, heevx!
11-
import MatrixAlgebraKit: _sylvester, svd_rank, svd_pullback!, complete_svd_basis!
12+
import MatrixAlgebraKit: _sylvester, svd_rank, svd_pullback!
1213
using AMDGPU
1314
using LinearAlgebra
1415
using LinearAlgebra: BlasFloat
1516

1617
include("yarocsolver.jl")
1718

18-
MatrixAlgebraKit.default_driver(::Type{TA}) where {TA <: StridedROCVecOrMat{<:BlasFloat}} = ROCSOLVER()
19+
MatrixAlgebraKit.default_driver(::Type{TA}) where {TA <: StridedROCArray{<:BlasFloat}} = ROCSOLVER()
20+
MatrixAlgebraKit.default_driver(::Type{TA}) where {TA <: AbstractVector{<:StridedROCMatrix{<:BlasFloat}}} = ROCSOLVER()
1921

20-
function MatrixAlgebraKit.default_svd_algorithm(::Type{T}; kwargs...) where {T <: StridedROCVecOrMat{<:BlasFloat}}
22+
function MatrixAlgebraKit.default_svd_algorithm(::Type{T}; kwargs...) where {T <: StridedROCMatrix{<:BlasFloat}}
23+
return QRIteration(; kwargs...)
24+
end
25+
function MatrixAlgebraKit.default_svd_algorithm(::Type{T}; kwargs...) where {T <: StridedROCArray{<:BlasFloat, 3}}
26+
return QRIteration(; kwargs...)
27+
end
28+
function MatrixAlgebraKit.default_svd_algorithm(::Type{T}; kwargs...) where {T <: AbstractVector{<:StridedROCMatrix{<:BlasFloat}}}
2129
return QRIteration(; kwargs...)
2230
end
2331
function MatrixAlgebraKit.default_eigh_algorithm(::Type{T}; kwargs...) where {T <: StridedROCVecOrMat{<:BlasFloat}}
@@ -28,7 +36,12 @@ for f in (:geqrf!, :ungqr!, :unmqr!)
2836
@eval $f(::ROCSOLVER, args...) = YArocSOLVER.$f(args...)
2937
end
3038

31-
MatrixAlgebraKit.supports_svd_full(::ROCSOLVER, f::Symbol) = f in (:qr_iteration, :jacobi, :bisection)
39+
MatrixAlgebraKit.supports_svd_full(::ROCSOLVER, f::Symbol) = f in (:qr_iteration, :jacobi, :divide_and_conquer, :bisection)
40+
41+
# rocSOLVER's `gesvd*_batched` functions take the batch as an array
42+
# of device pointers, so a group of equally sized matrices
43+
# doesn't need a copy into a 3D ROCArray.
44+
MatrixAlgebraKit.supports_pointer_batch(::AbstractAlgorithm, ::ROCSOLVER, ::Type{<:StridedROCMatrix{<:BlasFloat}}) = true
3245

3346
function gesvd!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...)
3447
m, n = size(A)
@@ -42,11 +55,38 @@ function gesvdj!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::Strid
4255
return MatrixAlgebraKit.svd_via_adjoint!(gesvdj!, ROCSOLVER(), A, S, U, Vᴴ; kwargs...)
4356
end
4457

45-
function gesvdx!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...)
58+
gesvdx!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...) =
4659
YArocSOLVER.gesvdx!(A, S, U, Vᴴ; kwargs...)
47-
complete_svd_basis!(U, Vᴴ, length(S))
48-
return S, U, Vᴴ
60+
61+
# rocSOLVER's batched `gesvd` requires m ≥ n, so wide matrices go through the adjoint
62+
function gesvd_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat}
63+
m, n = size(first(As))
64+
m >= n && return YArocSOLVER.gesvd_batched!(As, Ss, Us, Vᴴs; kwargs...)
65+
return MatrixAlgebraKit.batched_svd_via_adjoint!(gesvd_batched!, ROCSOLVER(), As, Ss, Us, Vᴴs; kwargs...)
4966
end
67+
function gesvd_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat}
68+
m, n, _ = size(As)
69+
m >= n && return YArocSOLVER.gesvd_strided_batched!(As, Ss, Us, Vᴴs; kwargs...)
70+
return MatrixAlgebraKit.batched_svd_via_adjoint!(gesvd_batched!, ROCSOLVER(), As, Ss, Us, Vᴴs; kwargs...)
71+
end
72+
73+
gesdd_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
74+
YArocSOLVER.gesdd_batched!(As, Ss, Us, Vᴴs; kwargs...)
75+
gesdd_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
76+
YArocSOLVER.gesdd_strided_batched!(As, Ss, Us, Vᴴs; kwargs...)
77+
78+
gesvdj_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
79+
YArocSOLVER.gesvdj_batched!(As, Ss, Us, Vᴴs; kwargs...)
80+
gesvdj_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
81+
YArocSOLVER.gesvdj_strided_batched!(As, Ss, Us, Vᴴs; kwargs...)
82+
83+
gesvdx_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
84+
YArocSOLVER.gesvdx_batched!(As, Ss, Us, Vᴴs; kwargs...)
85+
gesvdx_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
86+
YArocSOLVER.gesvdx_strided_batched!(As, Ss, Us, Vᴴs; kwargs...)
87+
88+
gesdd!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...) =
89+
YArocSOLVER.gesdd!(A, S, U, Vᴴ; kwargs...)
5090

5191
heevj!(::ROCSOLVER, A::StridedROCMatrix, Dd::StridedROCVector, V::StridedROCMatrix; kwargs...) =
5292
YArocSOLVER.heevj!(A, Dd, V; kwargs...)

0 commit comments

Comments
 (0)