Skip to content

Commit 322f673

Browse files
kshyattKatharine HyattKatharine Hyatt
authored
Support Bisection for ROCSOLVER (#281)
* Support Bisection for ROCSOLVER * Also test Bisection for LAPACK * Respond to comments * Typo * Actually fix lq_null * Make sure U (V) doesn't have too many columns (rows) * Use inplace nulls * Try to fix the size check --------- Co-authored-by: Katharine Hyatt <katharine.hyatt@ugent.be> Co-authored-by: Katharine Hyatt <katharine.s.hyatt@gmail.com>
1 parent 9cd1f86 commit 322f673

5 files changed

Lines changed: 139 additions & 11 deletions

File tree

‎ext/MatrixAlgebraKitAMDGPUExt/MatrixAlgebraKitAMDGPUExt.jl‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ 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!, gesvdj!
9+
import MatrixAlgebraKit: geqrf!, ungqr!, unmqr!, gesvd!, gesvdx!, gesvdj!
1010
import MatrixAlgebraKit: heevj!, heevd!, heev!, heevx!
11-
import MatrixAlgebraKit: _sylvester, svd_rank, svd_pullback!
11+
import MatrixAlgebraKit: _sylvester, svd_rank, svd_pullback!, complete_svd_basis!
1212
using AMDGPU
1313
using LinearAlgebra
1414
using LinearAlgebra: BlasFloat
@@ -28,7 +28,7 @@ for f in (:geqrf!, :ungqr!, :unmqr!)
2828
@eval $f(::ROCSOLVER, args...) = YArocSOLVER.$f(args...)
2929
end
3030

31-
MatrixAlgebraKit.supports_svd_full(::ROCSOLVER, f::Symbol) = f in (:qr_iteration, :jacobi)
31+
MatrixAlgebraKit.supports_svd_full(::ROCSOLVER, f::Symbol) = f in (:qr_iteration, :jacobi, :bisection)
3232

3333
function gesvd!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...)
3434
m, n = size(A)
@@ -42,6 +42,12 @@ function gesvdj!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::Strid
4242
return MatrixAlgebraKit.svd_via_adjoint!(gesvdj!, ROCSOLVER(), A, S, U, Vᴴ; kwargs...)
4343
end
4444

45+
function gesvdx!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...)
46+
YArocSOLVER.gesvdx!(A, S, U, Vᴴ; kwargs...)
47+
complete_svd_basis!(U, Vᴴ, length(S))
48+
return S, U, Vᴴ
49+
end
50+
4551
heevj!(::ROCSOLVER, A::StridedROCMatrix, Dd::StridedROCVector, V::StridedROCMatrix; kwargs...) =
4652
YArocSOLVER.heevj!(A, Dd, V; kwargs...)
4753
heevd!(::ROCSOLVER, A::StridedROCMatrix, Dd::StridedROCVector, V::StridedROCMatrix; kwargs...) =

‎ext/MatrixAlgebraKitAMDGPUExt/yarocsolver.jl‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,96 @@ for (fname, elty, relty) in
175175
end
176176
end
177177

178+
# `gesvdx` computes all singular values, those in the half-open interval `[vl, vu)`, or those
179+
# with an index in `irange`. Unlike the other algorithms, it never forms the full `U` and `Vᴴ`,
180+
# so the only job modes are `singular` and `none`.
181+
function _gesvdx_range(::Type{T}, kwargs) where {T <: Real}
182+
if haskey(kwargs, :irange)
183+
irange = convert(UnitRange{Int}, kwargs[:irange])
184+
return rocSOLVER.rocblas_srange_index, zero(T), zero(T), first(irange), last(irange)
185+
elseif haskey(kwargs, :vl) || haskey(kwargs, :vu)
186+
vl = convert(T, get(kwargs, :vl, -Inf))
187+
vu = convert(T, get(kwargs, :vu, +Inf))
188+
return rocSOLVER.rocblas_srange_value, vl, vu, 0, 0
189+
else
190+
return rocSOLVER.rocblas_srange_all, zero(T), zero(T), 0, 0
191+
end
192+
end
193+
194+
function _gesvdx_jobs(U, Vᴴ, m::Integer, n::Integer, maxnsv::Integer)
195+
if length(U) == 0
196+
jobu = rocSOLVER.rocblas_svect_none
197+
else
198+
size(U, 1) == m ||
199+
throw(DimensionMismatch("row size mismatch between A ($m) and U ($(size(U, 1)))"))
200+
size(U, 2) >= maxnsv ||
201+
throw(DimensionMismatch("invalid column size of U"))
202+
jobu = rocSOLVER.rocblas_svect_singular
203+
end
204+
if length(Vᴴ) == 0
205+
jobvt = rocSOLVER.rocblas_svect_none
206+
else
207+
size(Vᴴ, 2) == n ||
208+
throw(DimensionMismatch("column size mismatch between A ($n) and Vᴴ ($(size(Vᴴ, 2)))"))
209+
size(Vᴴ, 1) >= maxnsv ||
210+
throw(DimensionMismatch("invalid row size of Vᴴ"))
211+
jobvt = rocSOLVER.rocblas_svect_singular
212+
end
213+
return jobu, jobvt
214+
end
215+
216+
# Wrapper for SVD via Bisection
217+
for (fname, elty, relty) in
218+
(
219+
(:rocsolver_sgesvdx, :Float32, :Float32),
220+
(:rocsolver_dgesvdx, :Float64, :Float64),
221+
(:rocsolver_cgesvdx, :ComplexF32, :Float32),
222+
(:rocsolver_zgesvdx, :ComplexF64, :Float64),
223+
)
224+
@eval begin
225+
function gesvdx!(
226+
A::StridedROCMatrix{$elty},
227+
S::StridedROCVector{$relty} = similar(A, $relty, min(size(A)...)),
228+
U::StridedROCMatrix{$elty} = similar(A, $elty, size(A, 1), min(size(A)...)),
229+
Vᴴ::StridedROCMatrix{$elty} = similar(A, $elty, min(size(A)...), size(A, 2));
230+
kwargs...
231+
)
232+
chkstride1(A, U, Vᴴ, S)
233+
m, n = size(A)
234+
minmn = min(m, n)
235+
srange, vl, vu, il, iu = _gesvdx_range($relty, kwargs)
236+
maxnsv = srange == rocSOLVER.rocblas_srange_index ? iu - il + 1 : minmn
237+
jobu, jobvt = _gesvdx_jobs(U, Vᴴ, m, n, maxnsv)
238+
length(S) == minmn ||
239+
throw(DimensionMismatch("length mismatch between A ($minmn) and S ($(length(S)))"))
240+
241+
lda = max(1, stride(A, 2))
242+
ldu = max(1, stride(U, 2))
243+
ldv = max(1, stride(Vᴴ, 2))
244+
ifail = ROCVector{Cint}(undef, minmn)
245+
nsv = ROCVector{Cint}(undef, 1)
246+
dh = rocBLAS.handle()
247+
dev_info = ROCVector{Cint}(undef, 1)
248+
rocSOLVER.$fname(
249+
dh, jobu, jobvt, srange, m, n,
250+
A, lda, vl, vu, il, iu, nsv,
251+
S, U, ldu, Vᴴ, ldv, ifail,
252+
dev_info
253+
)
254+
info = @allowscalar dev_info[1]
255+
rocSOLVER.chkargsok(BlasInt(info))
256+
# Zero the entries of `S` that `gesvdx` did not write.
257+
nv = @allowscalar Int(nsv[1])
258+
nv < length(S) && fill!(view(S, (nv + 1):length(S)), zero(eltype(S)))
259+
260+
AMDGPU.unsafe_free!(nsv)
261+
AMDGPU.unsafe_free!(ifail)
262+
AMDGPU.unsafe_free!(dev_info)
263+
return (S, U, Vᴴ)
264+
end
265+
end
266+
end
267+
178268
# for (jname, bname, fname, elty, relty) in
179269
# ((:sygvd!, :rocsolverDnSsygvd_bufferSize, :rocsolverDnSsygvd, :Float32, :Float32),
180270
# (:sygvd!, :rocsolverDnDsygvd_bufferSize, :rocsolverDnDsygvd, :Float64, :Float64),

‎src/implementations/svd.jl‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,16 @@ function svd_via_adjoint!(f!::F, driver::Driver, A, S, U, Vᴴ; kwargs...) where
142142
end
143143

144144
# LAPACK
145-
for f! in (:gesdd!, :gesvd!, :gesvdx!, :gesdvd!)
145+
for f! in (:gesdd!, :gesvd!, :gesdvd!)
146146
@eval $f!(::LAPACK, args...; kwargs...) = YALAPACK.$f!(args...; kwargs...)
147147
end
148148

149+
function gesvdx!(::LAPACK, A, S, U, Vᴴ; kwargs...)
150+
YALAPACK.gesvdx!(A, S, U, Vᴴ; kwargs...)
151+
complete_svd_basis!(U, Vᴴ, length(S))
152+
return S, U, Vᴴ
153+
end
154+
149155
function gesvdj!(::LAPACK, A, S, U, Vᴴ; kwargs...)
150156
m, n = size(A)
151157
m >= n && return YALAPACK.gesvdj!(A, S, U, Vᴴ)
@@ -221,7 +227,27 @@ for (f, f_lapack!, Alg) in (
221227
end
222228

223229
supports_svd_full(::Driver, ::Symbol) = false
224-
supports_svd_full(::LAPACK, f::Symbol) = f in (:safe_divide_and_conquer, :divide_and_conquer, :qr_iteration)
230+
supports_svd_full(::LAPACK, f::Symbol) = f in (:safe_divide_and_conquer, :divide_and_conquer, :qr_iteration, :bisection)
231+
232+
# Some methods (e.g. `gesvdx`) only compute the leading `min(m, n)` singular vectors.
233+
# If `U` or `Vᴴ` is square (`svd_full!`), the remaining columns (row) of
234+
# `U` (`Vᴴ`) need to be filled with an orthonormal basis for the complement of the
235+
# computed singular vectors.
236+
function complete_svd_basis!(U::AbstractMatrix, Vᴴ::AbstractMatrix, minmn::Int)
237+
if size(U, 2) > minmn
238+
Uc = copy_input(qr_null, view(U, :, 1:minmn))
239+
N = view(U, :, (minmn + 1):size(U, 2))
240+
N′ = qr_null!(Uc, N)
241+
N′ === N || copyto!(N, N′)
242+
end
243+
if size(Vᴴ, 1) > minmn
244+
Vc = copy_input(lq_null, view(Vᴴ, 1:minmn, :))
245+
Nᴴ = view(Vᴴ, (minmn + 1):size(Vᴴ, 1), :)
246+
Nᴴ′ = lq_null!(Vc, Nᴴ)
247+
Nᴴ′ === Nᴴ || copyto!(Nᴴ, Nᴴ′)
248+
end
249+
return U, Vᴴ
250+
end
225251

226252
function svd_trunc_no_error!(A, USVᴴ, alg::TruncatedAlgorithm)
227253
U, S, Vᴴ = svd_compact!(A, USVᴴ, alg.alg)

‎src/yalapack.jl‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,17 +2235,23 @@ for (gesvd, gesdd, gesvdx, gejsv, gesvj, elty, relty) in
22352235
else
22362236
size(U, 1) == m ||
22372237
throw(DimensionMismatch("row size mismatch between A ($m) and U ($(size(U, 1)))"))
2238-
size(U, 2) >= (range == 'I' ? iu - il + 1 : minmn) ||
2239-
throw(DimensionMismatch("invalid column size of U"))
2238+
if range == 'I'
2239+
(size(U, 2) >= iu - il + 1 && size(U, 2) <= m) || throw(DimensionMismatch("invalid column size of U"))
2240+
else
2241+
(size(U, 2) == minmn || size(U, 2) == m) || throw(DimensionMismatch("invalid column size of U"))
2242+
end
22402243
jobu = 'V'
22412244
end
22422245
if length(Vᴴ) == 0
22432246
jobvt = 'N'
22442247
else
22452248
size(Vᴴ, 2) == n ||
22462249
throw(DimensionMismatch("column size mismatch between A ($n) and Vᴴ ($(size(Vᴴ, 2)))"))
2247-
size(Vᴴ, 1) >= (range == 'I' ? iu - il + 1 : minmn) ||
2248-
throw(DimensionMismatch("invalid row size of Vᴴ"))
2250+
if range == 'I'
2251+
(size(Vᴴ, 1) >= iu - il + 1 && size(Vᴴ, 1) <= n) || throw(DimensionMismatch("invalid row size of Vᴴ"))
2252+
else
2253+
(size(Vᴴ, 1) == minmn || size(Vᴴ, 1) == n) || throw(DimensionMismatch("invalid row size of Vᴴ"))
2254+
end
22492255
jobvt = 'V'
22502256
end
22512257
length(S) == minmn ||

‎test/decompositions/svd.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if !is_buildkite
2121
# LAPACK algorithms:
2222
for T in BLASFloats, m in (0, 54), n in (0, 37, m, 63)
2323
TestSuite.seed_rng!(123)
24-
LAPACK_SVD_ALGS = (QRIteration(), DivideAndConquer(), SafeDivideAndConquer(; fixgauge = true))
24+
LAPACK_SVD_ALGS = (QRIteration(), DivideAndConquer(), SafeDivideAndConquer(; fixgauge = true), Bisection())
2525
TestSuite.test_svd(T, (m, n))
2626
TestSuite.test_svd_algs(T, (m, n), LAPACK_SVD_ALGS)
2727
@static if VERSION > v"1.11-" # Jacobi broken on 1.10
@@ -81,7 +81,7 @@ if AMDGPU.functional()
8181
for T in BLASFloats, m in (0, 23), n in (0, 17, m, 27)
8282
TestSuite.seed_rng!(123)
8383
TestSuite.test_svd(ROCMatrix{T}, (m, n))
84-
AMD_SVD_ALGS = (QRIteration(), Jacobi())
84+
AMD_SVD_ALGS = (QRIteration(), Jacobi(), Bisection())
8585
TestSuite.test_svd_algs(ROCMatrix{T}, (m, n), AMD_SVD_ALGS)
8686
end
8787

0 commit comments

Comments
 (0)