Skip to content

Commit 6746ea1

Browse files
author
Katharine Hyatt
committed
Add special path for AMDGPU, implement gesvdx batching there, and fix some errors
1 parent fbdb564 commit 6746ea1

3 files changed

Lines changed: 227 additions & 61 deletions

File tree

‎ext/MatrixAlgebraKitAMDGPUExt/MatrixAlgebraKitAMDGPUExt.jl‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ end
4848

4949
MatrixAlgebraKit.supports_svd_full(::ROCSOLVER, f::Symbol) = f in (:qr_iteration, :jacobi, :divide_and_conquer)
5050

51+
# rocSOLVER's `gesvd*_batched` functions take the batch as an array
52+
# of device pointers, so a group of equally sized matrices
53+
# doesn't need a copy into a 3D ROCArray.
54+
MatrixAlgebraKit.supports_pointer_batch(::AbstractAlgorithm, ::Type{<:StridedROCMatrix{<:BlasFloat}}) = true
55+
5156
function gesvd!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...)
5257
m, n = size(A)
5358
m >= n && return YArocSOLVER.gesvd!(A, S, U, Vᴴ)
@@ -61,7 +66,7 @@ function gesvdj!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::Strid
6166
end
6267

6368
# rocSOLVER's batched `gesvd` requires m ≥ n, so wide matrices go through the adjoint
64-
function gesvd_batched!(::ROCSOLVER, As::Vector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat}
69+
function gesvd_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat}
6570
m, n = size(first(As))
6671
m >= n && return YArocSOLVER.gesvd_batched!(As, Ss, Us, Vᴴs; kwargs...)
6772
return MatrixAlgebraKit.batched_svd_via_adjoint!(gesvd_batched!, ROCSOLVER(), As, Ss, Us, Vᴴs; kwargs...)
@@ -72,17 +77,17 @@ function gesvd_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMa
7277
return MatrixAlgebraKit.batched_svd_via_adjoint!(gesvd_batched!, ROCSOLVER(), As, Ss, Us, Vᴴs; kwargs...)
7378
end
7479

75-
gesdd_batched!(::ROCSOLVER, As::Vector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
80+
gesdd_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
7681
YArocSOLVER.gesdd_batched!(As, Ss, Us, Vᴴs; kwargs...)
7782
gesdd_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
7883
YArocSOLVER.gesdd_strided_batched!(As, Ss, Us, Vᴴs; kwargs...)
7984

80-
gesvdj_batched!(::ROCSOLVER, As::Vector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
85+
gesvdj_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
8186
YArocSOLVER.gesvdj_batched!(As, Ss, Us, Vᴴs; kwargs...)
8287
gesvdj_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
8388
YArocSOLVER.gesvdj_strided_batched!(As, Ss, Us, Vᴴs; kwargs...)
8489

85-
gesvdx_batched!(::ROCSOLVER, As::Vector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
90+
gesvdx_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
8691
YArocSOLVER.gesvdx_batched!(As, Ss, Us, Vᴴs; kwargs...)
8792
gesvdx_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
8893
YArocSOLVER.gesvdx_strided_batched!(As, Ss, Us, Vᴴs; kwargs...)

‎ext/MatrixAlgebraKitAMDGPUExt/yarocsolver.jl‎

Lines changed: 168 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ for (fname, elty, relty) in
103103
)
104104
@eval begin
105105
function gesvd_batched!(
106-
A::StridedROCVector{<:StridedROCMatrix{$elty}},
106+
A::AbstractVector{<:StridedROCMatrix{$elty}},
107107
S::StridedROCMatrix{$relty} = similar(first(A), $relty, (min(size(first(A))...), length(A))),
108108
U::StridedROCArray{$elty, 3} = similar(first(A), $elty, size(first(A), 1), min(size(first(A))...), length(A)),
109109
Vᴴ::StridedROCArray{$elty, 3} = similar(first(A), $elty, min(size(first(A))...), size(first(A), 2), length(A)),
@@ -162,7 +162,7 @@ for (fname, elty, relty) in
162162
E = ROCArray{$relty}(undef, length(A) * strideE)
163163
dh = rocBLAS.handle()
164164
dev_info = ROCVector{Cint}(undef, length(A))
165-
pA = map(pointer, A)
165+
pA = ROCVector(map(pointer, A))
166166
rocSOLVER.$fname(
167167
dh, jobu, jobvt, m, n, pA, lda,
168168
S, strideS, U, ldu, strideU, Vᴴ, ldv, strideV,
@@ -346,7 +346,7 @@ for (fname, elty, relty) in
346346
)
347347
@eval begin
348348
function gesdd_batched!(
349-
A::StridedROCVector{<:StridedROCMatrix{$elty}},
349+
A::AbstractVector{<:StridedROCMatrix{$elty}},
350350
S::StridedROCMatrix{$relty} = similar(first(A), $relty, (min(size(first(A))...), length(A))),
351351
U::StridedROCArray{$elty, 3} = similar(first(A), $elty, size(first(A), 1), min(size(first(A))...), length(A)),
352352
Vᴴ::StridedROCArray{$elty, 3} = similar(first(A), $elty, min(size(first(A))...), size(first(A), 2), length(A)),
@@ -402,7 +402,7 @@ for (fname, elty, relty) in
402402

403403
dh = rocBLAS.handle()
404404
dev_info = ROCVector{Cint}(undef, length(A))
405-
pA = map(pointer, A)
405+
pA = ROCVector(map(pointer, A))
406406
rocSOLVER.$fname(
407407
dh, jobu, jobvt, m, n, pA, lda,
408408
S, strideS, U, ldu, strideU, Vᴴ, ldv, strideV,
@@ -584,7 +584,7 @@ for (fname, elty, relty) in
584584
)
585585
@eval begin
586586
function gesvdj_batched!(
587-
A::StridedROCVector{<:StridedROCMatrix{$elty}},
587+
A::AbstractVector{<:StridedROCMatrix{$elty}},
588588
S::StridedROCMatrix{$relty} = similar(first(A), $relty, (min(size(first(A))...), length(A))),
589589
U::StridedROCArray{$elty, 3} = similar(first(A), $elty, size(first(A), 1), min(size(first(A))...), length(A)),
590590
Vᴴ::StridedROCArray{$elty, 3} = similar(first(A), $elty, min(size(first(A))...), size(first(A), 2), length(A)),
@@ -645,7 +645,7 @@ for (fname, elty, relty) in
645645
dev_n_sweeps = ROCVector{Cint}(undef, length(A))
646646

647647
dh = rocBLAS.handle()
648-
pA = map(pointer, A)
648+
pA = ROCVector(map(pointer, A))
649649
rocSOLVER.$fname(
650650
dh, jobu, jobvt, m, n, pA, lda, tol,
651651
dev_residual, max_sweeps, dev_n_sweeps,
@@ -748,6 +748,48 @@ for (fname, elty, relty) in
748748
end
749749
end
750750

751+
# `gesvdx` computes all singular values, those in the half-open interval `[vl, vu)`, or those
752+
# with an index in `irange`. Unlike the other algorithms, it never forms the full `U` and `Vᴴ`,
753+
# so the only job modes are `singular` and `none`.
754+
function _gesvdx_range(::Type{T}, kwargs) where {T <: Real}
755+
if haskey(kwargs, :irange)
756+
irange = convert(UnitRange{Int}, kwargs[:irange])
757+
return rocSOLVER.rocblas_srange_index, zero(T), zero(T), first(irange), last(irange)
758+
elseif haskey(kwargs, :vl) || haskey(kwargs, :vu)
759+
vl = convert(T, get(kwargs, :vl, -Inf))
760+
vu = convert(T, get(kwargs, :vu, +Inf))
761+
return rocSOLVER.rocblas_srange_value, vl, vu, 0, 0
762+
else
763+
return rocSOLVER.rocblas_srange_all, zero(T), zero(T), 0, 0
764+
end
765+
end
766+
767+
function _gesvdx_maxnsv(srange, il::Integer, iu::Integer, minmn::Integer)
768+
return srange == rocSOLVER.rocblas_srange_index ? iu - il + 1 : minmn
769+
end
770+
771+
function _gesvdx_jobs(A, U, Vᴴ, m::Integer, n::Integer, maxnsv::Integer)
772+
if length(U) == 0
773+
jobu = rocSOLVER.rocblas_svect_none
774+
else
775+
size(U, 1) == m ||
776+
throw(DimensionMismatch("row size mismatch between A ($m) and U ($(size(U, 1)))"))
777+
size(U, 2) >= maxnsv ||
778+
throw(DimensionMismatch("invalid column size of U"))
779+
jobu = rocSOLVER.rocblas_svect_singular
780+
end
781+
if length(Vᴴ) == 0
782+
jobvt = rocSOLVER.rocblas_svect_none
783+
else
784+
size(Vᴴ, 2) == n ||
785+
throw(DimensionMismatch("column size mismatch between A ($n) and Vᴴ ($(size(Vᴴ, 2)))"))
786+
size(Vᴴ, 1) >= maxnsv ||
787+
throw(DimensionMismatch("invalid row size of Vᴴ"))
788+
jobvt = rocSOLVER.rocblas_svect_singular
789+
end
790+
return jobu, jobvt
791+
end
792+
751793
# Wrapper for SVD via Bisection
752794
for (fname, elty, relty) in
753795
(
@@ -764,66 +806,147 @@ for (fname, elty, relty) in
764806
Vᴴ::StridedROCMatrix{$elty} = similar(A, $elty, min(size(A)...), size(A, 2));
765807
kwargs...
766808
)
767-
#! format: on
768809
chkstride1(A, U, Vᴴ, S)
769810
m, n = size(A)
770811
minmn = min(m, n)
771-
if haskey(kwargs, :irange)
772-
irange = convert(UnitRange{Int}, kwargs[:irange])
773-
il = first(irange)
774-
iu = last(irange)
775-
vl = vu = zero($relty)
776-
range = 'I'
777-
elseif haskey(kwargs, :vl) || haskey(kwargs, :vu)
778-
vl = convert($relty, get(kwargs, :vl, -Inf))
779-
vu = convert($relty, get(kwargs, :vu, +Inf))
780-
il = iu = 0
781-
range = 'V'
782-
else
783-
il = iu = 0
784-
vl = vu = zero($relty)
785-
range = 'A'
786-
end
787-
788-
if length(U) == 0
789-
jobu = 'N'
790-
else
791-
size(U, 1) == m ||
792-
throw(DimensionMismatch("row size mismatch between A ($m) and U ($(size(U, 1)))"))
793-
size(U, 2) >= (range == 'I' ? iu - il + 1 : minmn) ||
794-
throw(DimensionMismatch("invalid column size of U"))
795-
jobu = 'V'
796-
end
797-
if length(Vᴴ) == 0
798-
jobvt = 'N'
799-
else
800-
size(Vᴴ, 2) == n ||
801-
throw(DimensionMismatch("column size mismatch between A ($n) and Vᴴ ($(size(Vᴴ, 2)))"))
802-
size(Vᴴ, 1) >= (range == 'I' ? iu - il + 1 : minmn) ||
803-
throw(DimensionMismatch("invalid row size of Vᴴ"))
804-
jobvt = 'V'
805-
end
812+
srange, vl, vu, il, iu = _gesvdx_range($relty, kwargs)
813+
maxnsv = _gesvdx_maxnsv(srange, il, iu, minmn)
814+
jobu, jobvt = _gesvdx_jobs(A, U, Vᴴ, m, n, maxnsv)
806815
length(S) == minmn ||
807816
throw(DimensionMismatch("length mismatch between A ($minmn) and S ($(length(S)))"))
808817

809818
lda = max(1, stride(A, 2))
810819
ldu = max(1, stride(U, 2))
811820
ldv = max(1, stride(Vᴴ, 2))
812821
ifail = ROCVector{Cint}(undef, minmn)
813-
ns = ROCVector{Cint}(undef, 1)
822+
nsv = ROCVector{Cint}(undef, 1)
814823
dh = rocBLAS.handle()
815824
dev_info = ROCVector{Cint}(undef, 1)
816825
rocSOLVER.$fname(
817-
dh, jobu, jobvt, m, n,
826+
dh, jobu, jobvt, srange, m, n,
818827
A, lda, vl, vu, il, iu, nsv,
819828
S, U, ldu, Vᴴ, ldv, ifail,
820829
dev_info
821830
)
822831
info = @allowscalar dev_info[1]
823832
rocSOLVER.chkargsok(BlasInt(info))
824833

825-
AMDGPU.unsafe_free!(ns)
834+
AMDGPU.unsafe_free!(nsv)
835+
AMDGPU.unsafe_free!(ifail)
836+
AMDGPU.unsafe_free!(dev_info)
837+
return (S, U, Vᴴ)
838+
end
839+
end
840+
end
841+
842+
# Wrappers for batched SVD via Bisection
843+
for (fname, elty, relty) in
844+
(
845+
(:rocsolver_sgesvdx_batched, :Float32, :Float32),
846+
(:rocsolver_dgesvdx_batched, :Float64, :Float64),
847+
(:rocsolver_cgesvdx_batched, :ComplexF32, :Float32),
848+
(:rocsolver_zgesvdx_batched, :ComplexF64, :Float64),
849+
)
850+
@eval begin
851+
function gesvdx_batched!(
852+
A::AbstractVector{<:StridedROCMatrix{$elty}},
853+
S::StridedROCMatrix{$relty} = similar(first(A), $relty, (min(size(first(A))...), length(A))),
854+
U::StridedROCArray{$elty, 3} = similar(first(A), $elty, size(first(A), 1), min(size(first(A))...), length(A)),
855+
Vᴴ::StridedROCArray{$elty, 3} = similar(first(A), $elty, min(size(first(A))...), size(first(A), 2), length(A));
856+
kwargs...
857+
)
858+
for A_ in A
859+
chkstride1(A_, U, Vᴴ, S)
860+
end
861+
m, n = size(first(A))
862+
minmn = min(m, n)
863+
batch_count = length(A)
864+
srange, vl, vu, il, iu = _gesvdx_range($relty, kwargs)
865+
maxnsv = _gesvdx_maxnsv(srange, il, iu, minmn)
866+
jobu, jobvt = _gesvdx_jobs(A, U, Vᴴ, m, n, maxnsv)
867+
length(S) == minmn * batch_count ||
868+
throw(DimensionMismatch("length mismatch between A and S"))
869+
870+
lda = max(1, stride(first(A), 2))
871+
ldu = max(1, stride(U, 2))
872+
strideU = ldu * size(U, 2)
873+
ldv = max(1, stride(Vᴴ, 2))
874+
strideV = ldv * n
875+
strideS = minmn
876+
strideF = minmn
877+
878+
dh = rocBLAS.handle()
879+
nsv = ROCVector{Cint}(undef, batch_count)
880+
ifail = ROCVector{Cint}(undef, minmn * batch_count)
881+
dev_info = ROCVector{Cint}(undef, batch_count)
882+
pA = ROCVector(map(pointer, A))
883+
rocSOLVER.$fname(
884+
dh, jobu, jobvt, srange, m, n, pA, lda,
885+
vl, vu, il, iu, nsv,
886+
S, strideS, U, ldu, strideU, Vᴴ, ldv, strideV,
887+
ifail, strideF, dev_info, batch_count
888+
)
889+
AMDGPU.unsafe_free!(pA)
890+
891+
rocSOLVER.chkargsok.(BlasInt.(collect(dev_info)))
892+
893+
AMDGPU.unsafe_free!(nsv)
894+
AMDGPU.unsafe_free!(ifail)
895+
AMDGPU.unsafe_free!(dev_info)
896+
return (S, U, Vᴴ)
897+
end
898+
end
899+
end
900+
901+
for (fname, elty, relty) in
902+
(
903+
(:rocsolver_sgesvdx_strided_batched, :Float32, :Float32),
904+
(:rocsolver_dgesvdx_strided_batched, :Float64, :Float64),
905+
(:rocsolver_cgesvdx_strided_batched, :ComplexF32, :Float32),
906+
(:rocsolver_zgesvdx_strided_batched, :ComplexF64, :Float64),
907+
)
908+
@eval begin
909+
function gesvdx_strided_batched!(
910+
A::StridedROCArray{$elty, 3},
911+
S::StridedROCMatrix{$relty} = similar(A, $relty, (min(size(A, 1), size(A, 2)), size(A, 3))),
912+
U::StridedROCArray{$elty, 3} = similar(A, $elty, size(A, 1), min(size(A, 1), size(A, 2)), size(A, 3)),
913+
Vᴴ::StridedROCArray{$elty, 3} = similar(A, $elty, min(size(A, 1), size(A, 2)), size(A, 2), size(A, 3));
914+
kwargs...
915+
)
916+
chkstride1(A, U, Vᴴ, S)
917+
m, n, batch_count = size(A)
918+
minmn = min(m, n)
919+
srange, vl, vu, il, iu = _gesvdx_range($relty, kwargs)
920+
maxnsv = _gesvdx_maxnsv(srange, il, iu, minmn)
921+
jobu, jobvt = _gesvdx_jobs(A, U, Vᴴ, m, n, maxnsv)
922+
length(S) == minmn * batch_count ||
923+
throw(DimensionMismatch("length mismatch between A and S"))
924+
925+
lda = max(1, stride(A, 2))
926+
strideA = stride(A, 3)
927+
ldu = max(1, stride(U, 2))
928+
strideU = ldu * size(U, 2)
929+
ldv = max(1, stride(Vᴴ, 2))
930+
strideV = ldv * n
931+
strideS = minmn
932+
strideF = minmn
933+
934+
dh = rocBLAS.handle()
935+
nsv = ROCVector{Cint}(undef, batch_count)
936+
ifail = ROCVector{Cint}(undef, minmn * batch_count)
937+
dev_info = ROCVector{Cint}(undef, batch_count)
938+
rocSOLVER.$fname(
939+
dh, jobu, jobvt, srange, m, n, A, lda, strideA,
940+
vl, vu, il, iu, nsv,
941+
S, strideS, U, ldu, strideU, Vᴴ, ldv, strideV,
942+
ifail, strideF, dev_info, batch_count
943+
)
944+
945+
rocSOLVER.chkargsok.(BlasInt.(collect(dev_info)))
946+
947+
AMDGPU.unsafe_free!(nsv)
826948
AMDGPU.unsafe_free!(ifail)
949+
AMDGPU.unsafe_free!(dev_info)
827950
return (S, U, Vᴴ)
828951
end
829952
end

0 commit comments

Comments
 (0)