Skip to content

Commit 3551c31

Browse files
Katharine Hyattkshyatt
authored andcommitted
Support svd_full for Bisection
1 parent 1d142bf commit 3551c31

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎ext/MatrixAlgebraKitAMDGPUExt/MatrixAlgebraKitAMDGPUExt.jl‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,22 @@ gesvdj_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::Strided
9393
gesvdj_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
9494
YArocSOLVER.gesvdj_strided_batched!(As, Ss, Us, Vᴴs; kwargs...)
9595

96-
gesvdx_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
96+
function gesvdx_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat}
9797
YArocSOLVER.gesvdx_batched!(As, Ss, Us, Vᴴs; kwargs...)
98-
gesvdx_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
98+
_complete_svd_basis!(Us, Vᴴs, size(Ss, 1))
99+
return Ss, Us, Vᴴs
100+
end
101+
function gesvdx_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat}
99102
YArocSOLVER.gesvdx_strided_batched!(As, Ss, Us, Vᴴs; kwargs...)
103+
_complete_svd_basis!(Us, Vᴴs, size(Ss, 1))
104+
return Ss, Us, Vᴴs
105+
end
100106

101-
gesvdx!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...) =
107+
function gesvdx!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...)
102108
YArocSOLVER.gesvdx!(A, S, U, Vᴴ; kwargs...)
109+
_complete_svd_basis!(U, Vᴴ, length(S))
110+
return S, U, Vᴴ
111+
end
103112
gesdd!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...) =
104113
YArocSOLVER.gesdd!(A, S, U, Vᴴ; kwargs...)
105114

‎src/implementations/svd.jl‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,31 @@ function complete_svd_basis!(U::AbstractMatrix, Vᴴ::AbstractMatrix, minmn::Int
249249
return U, Vᴴ
250250
end
251251

252+
# Some methods (e.g. `gesvdx`) only compute the leading `min(m, n)` singular vectors.
253+
# If `U` or `Vᴴ` is square (`(batched_)svd_full!`), the remaining columns (row) of
254+
# `U` (`Vᴴ`) need to be filled with an orthonormal basis for the complement of the
255+
# computed singular vectors.
256+
function _complete_svd_basis!(U::AbstractMatrix, Vᴴ::AbstractMatrix, minmn::Int)
257+
if size(U, 2) > minmn
258+
N = qr_null!(copy(view(U, :, 1:minmn)))
259+
copyto!(view(U, :, (minmn + 1):size(U, 2)), N)
260+
end
261+
if size(Vᴴ, 1) > minmn
262+
V = view(Vᴴ, 1:minmn, :)
263+
N = qr_null!(adjoint!(similar(V, reverse(size(V))), V))
264+
adjoint!(view(Vᴴ, (minmn + 1):size(Vᴴ, 1), :), N)
265+
end
266+
return U, Vᴴ
267+
end
268+
function _complete_svd_basis!(U::AbstractArray{<:Any, 3}, Vᴴ::AbstractArray{<:Any, 3}, minmn::Int)
269+
(size(U, 2) > minmn || size(Vᴴ, 1) > minmn) || return U, Vᴴ
270+
for (u, vᴴ) in zip(eachslice(U, dims = 3), eachslice(Vᴴ, dims = 3))
271+
_complete_svd_basis!(u, vᴴ, minmn)
272+
end
273+
return U, Vᴴ
274+
end
275+
276+
252277
"""
253278
requires_tall(alg) -> Bool
254279

0 commit comments

Comments
 (0)