Skip to content

Commit c97dfa4

Browse files
Katharine Hyattkshyatt
authored andcommitted
rocSOLVER gesvdx returns no singular values for min(m, n) == 1
1 parent 7a06e62 commit c97dfa4

3 files changed

Lines changed: 86 additions & 13 deletions

File tree

‎ext/MatrixAlgebraKitAMDGPUExt/MatrixAlgebraKitAMDGPUExt.jl‎

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ MatrixAlgebraKit.default_driver(::Type{TA}) where {TA <: StridedROCArray{<:BlasF
2020
MatrixAlgebraKit.default_driver(::Type{TA}) where {TA <: AbstractVector{<:StridedROCMatrix{<:BlasFloat}}} = ROCSOLVER()
2121

2222
function MatrixAlgebraKit.default_svd_algorithm(::Type{T}; kwargs...) where {T <: StridedROCMatrix{<:BlasFloat}}
23-
return QRIteration(; kwargs...)
23+
return Bisection(; kwargs...)
2424
end
2525
function MatrixAlgebraKit.default_svd_algorithm(::Type{T}; kwargs...) where {T <: StridedROCArray{<:BlasFloat, 3}}
26-
return QRIteration(; kwargs...)
26+
return Bisection(; kwargs...)
2727
end
2828
function MatrixAlgebraKit.default_svd_algorithm(::Type{T}; kwargs...) where {T <: AbstractVector{<:StridedROCMatrix{<:BlasFloat}}}
29-
return QRIteration(; kwargs...)
29+
return Bisection(; kwargs...)
3030
end
3131
function MatrixAlgebraKit.default_eigh_algorithm(::Type{T}; kwargs...) where {T <: StridedROCVecOrMat{<:BlasFloat}}
3232
return DivideAndConquer(; kwargs...)
@@ -55,8 +55,14 @@ function gesvdj!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::Strid
5555
return MatrixAlgebraKit.svd_via_adjoint!(gesvdj!, ROCSOLVER(), A, S, U, Vᴴ; kwargs...)
5656
end
5757

58-
gesvdx!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...) =
59-
YArocSOLVER.gesvdx!(A, S, U, Vᴴ; kwargs...)
58+
function gesvdx!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...)
59+
if min(size(A)...) == 1
60+
_gesvdx_rank1!(A, S, U, Vᴴ; kwargs...)
61+
else
62+
YArocSOLVER.gesvdx!(A, S, U, Vᴴ; kwargs...)
63+
end
64+
return S, U, Vᴴ
65+
end
6066

6167
# rocSOLVER's batched `gesvd` requires m ≥ n, so wide matrices go through the adjoint
6268
function gesvd_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat}
@@ -80,14 +86,64 @@ gesvdj_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::Strided
8086
gesvdj_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
8187
YArocSOLVER.gesvdj_strided_batched!(As, Ss, Us, Vᴴs; kwargs...)
8288

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...)
89+
function gesvdx_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat}
90+
if min(size(first(As))...) == 1
91+
_gesvdx_rank1_batched!(As, Ss, Us, Vᴴs; kwargs...)
92+
else
93+
YArocSOLVER.gesvdx_batched!(As, Ss, Us, Vᴴs; kwargs...)
94+
end
95+
return Ss, Us, Vᴴs
96+
end
97+
function gesvdx_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat}
98+
if min(size(As, 1), size(As, 2)) == 1
99+
_gesvdx_rank1_batched!(As, Ss, Us, Vᴴs; kwargs...)
100+
else
101+
YArocSOLVER.gesvdx_strided_batched!(As, Ss, Us, Vᴴs; kwargs...)
102+
end
103+
return Ss, Us, Vᴴs
104+
end
87105

88106
gesdd!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...) =
89107
YArocSOLVER.gesdd!(A, S, U, Vᴴ; kwargs...)
90108

109+
function _gesvdx_rank1!(A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...)
110+
select_kwargs = Base.structdiff(NamedTuple(kwargs), (; irange = nothing, vl = nothing, vu = nothing))
111+
nrmA = norm(A)
112+
fill!(S, nrmA)
113+
if !isempty(U) && !isempty(Vᴴ)
114+
u, v = view(U, :, 1), view(Vᴴ, 1, :)
115+
x, e = size(A, 1) == 1 ? (v, u) : (u, v)
116+
if iszero(nrmA)
117+
zero!(x)
118+
fill!(view(x, 1:1), one(eltype(x)))
119+
else
120+
copyto!(x, vec(A))
121+
x ./= nrmA
122+
end
123+
fill!(e, one(eltype(e)))
124+
end
125+
_gesvdx_apply_range!(S, select_kwargs)
126+
return S, U, Vᴴ
127+
end
128+
129+
function _gesvdx_rank1_batched!(As, Ss::StridedROCMatrix, Us, Vᴴs; kwargs...)
130+
select_kwargs = Base.structdiff(NamedTuple(kwargs), (; irange = nothing, vl = nothing, vu = nothing))
131+
gesvdj_batched!(ROCSOLVER(), As, Ss, Us, Vᴴs; select_kwargs...)
132+
_gesvdx_apply_range!(Ss, select_kwargs)
133+
return Ss, Us, Vᴴs
134+
end
135+
136+
function _gesvdx_apply_range!(S, select::NamedTuple)
137+
if haskey(select, :irange)
138+
1 in convert(UnitRange{Int}, select.irange) || zero!(S)
139+
elseif haskey(select, :vl) || haskey(select, :vu)
140+
vl = convert(eltype(S), get(select, :vl, -Inf))
141+
vu = convert(eltype(S), get(select, :vu, Inf))
142+
S .= ifelse.((vl .<= S) .& (S .< vu), S, zero(eltype(S)))
143+
end
144+
return S
145+
end
146+
91147
heevj!(::ROCSOLVER, A::StridedROCMatrix, Dd::StridedROCVector, V::StridedROCMatrix; kwargs...) =
92148
YArocSOLVER.heevj!(A, Dd, V; kwargs...)
93149
heevd!(::ROCSOLVER, A::StridedROCMatrix, Dd::StridedROCVector, V::StridedROCMatrix; kwargs...) =

‎test/decompositions/svd.jl‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MatrixAlgebraKit
2-
using LinearAlgebra: Diagonal
2+
using Test
3+
using LinearAlgebra: Diagonal, isposdef
34
using CUDA, AMDGPU
45

56
if @isdefined(fast_tests) && fast_tests
@@ -12,6 +13,7 @@ end
1213

1314
@isdefined(TestSuite) || include("../testsuite/TestSuite.jl")
1415
using .TestSuite
16+
using .TestSuite: testargs_summary
1517

1618
is_buildkite = get(ENV, "BUILDKITE", "false") == "true"
1719

@@ -88,7 +90,7 @@ end
8890
# ------------
8991
if AMDGPU.functional()
9092
# ROCSOLVER algorithms:
91-
for T in BLASFloats, m in (0, 23), n in (0, 17, m, 27)
93+
for T in BLASFloats, m in (0, 1, 23), n in (0, 1, 17, m, 27)
9294
TestSuite.seed_rng!(123)
9395
TestSuite.test_svd(ROCMatrix{T}, (m, n))
9496
AMD_SVD_ALGS = (QRIteration(), Jacobi(), DivideAndConquer(), Bisection())
@@ -97,6 +99,21 @@ if AMDGPU.functional()
9799
TestSuite.test_svd_batched_algs(ROCMatrix{T}, (m, n), batch_size, AMD_SVD_ALGS)
98100
end
99101

102+
@testset "Bisection with min(m, n) == 1 $(testargs_summary(T, sz))" for T in BLASFloats,
103+
sz in ((1, 1), (2, 1), (5, 1), (1, 2), (1, 5))
104+
105+
TestSuite.seed_rng!(123)
106+
for _ in 1:16
107+
A = TestSuite.instantiate_matrix(ROCMatrix{T}, sz)
108+
U, S, Vᴴ = svd_compact(A; alg = Bisection())
109+
@test U * S * Vᴴ ≈ A
110+
@test isisometric(U)
111+
@test isisometric(Vᴴ; side = :right)
112+
@test isposdef(S)
113+
@test Array(S) ≈ Diagonal(svd_vals(Array(A)))
114+
end
115+
end
116+
100117
# Diagonal:
101118
for T in BLASFloats, m in (0, 23)
102119
TestSuite.seed_rng!(123)

‎test/testsuite/decompositions/svd.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ function test_svd_trunc(
468468
S₀ = collect(svd_vals(A))
469469
r = minmn - 2
470470

471-
if m > 0 && n > 0
471+
if m > 0 && n > 0 && r >= 0
472472
U1, S1, V1ᴴ, ϵ1 = @testinferred svd_trunc(A; trunc = truncrank(r))
473473
@test length(diagview(S1)) == r
474474
@test collect(diagview(S1)) ≈ S₀[1:r]
@@ -573,7 +573,7 @@ function test_svd_trunc_algs(
573573
S₀ = collect(svd_vals(A))
574574
r = minmn - 2
575575

576-
if m > 0 && n > 0
576+
if m > 0 && n > 0 && r >= 0
577577
U1, S1, V1ᴴ, ϵ1 = @testinferred svd_trunc(A; trunc = truncrank(r), alg)
578578
@test length(diagview(S1)) == r
579579
@test collect(diagview(S1)) ≈ S₀[1:r]

0 commit comments

Comments
 (0)