Skip to content

Commit 2242e68

Browse files
committed
Support CUSOLVER's randomized SVD for svd_trunc
1 parent 5664502 commit 2242e68

2 files changed

Lines changed: 62 additions & 12 deletions

File tree

  • src/implementations
  • test/testsuite/decompositions

‎src/implementations/svd.jl‎

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -319,19 +319,33 @@ end
319319

320320
# GPU logic (randomized SVD - CUSOLVER_Randomized has no CPU analog, kept as-is)
321321
# ---------------------------------------------------------------------------------
322+
# A randomized SVD only computes the `k` largest singular triplets, so the outputs only need
323+
# room for those: `U` is `m × r`, `S` is `r × r` and `Vᴴ` is `r × n` for some `k ≤ r ≤ minmn`
324+
# (`initialize_output` uses `r = k`). Only the first `k` columns/values/rows are written.
325+
326+
function _randomized_rank(alg::CUSOLVER_Randomized)
327+
haskey(alg.kwargs, :k) ||
328+
throw(ArgumentError("$alg requires the number of singular values to compute, `k`"))
329+
return alg.kwargs.k::Int
330+
end
322331

323332
function check_input(
324333
::Union{typeof(svd_trunc!), typeof(svd_trunc_no_error!)}, A::AbstractMatrix, USVᴴ, alg::CUSOLVER_Randomized
325334
)
326335
m, n = size(A)
327336
minmn = min(m, n)
337+
k = _randomized_rank(alg)
328338
U, S, Vᴴ = USVᴴ
329339
@assert U isa AbstractMatrix && S isa Diagonal && Vᴴ isa AbstractMatrix
330-
@check_size(U, (m, m))
340+
r = size(S, 1)
341+
k ≤ r ≤ minmn ||
342+
throw(DimensionMismatch("size of S ($r) must be between k ($k) and min(size(A)...) ($minmn)"))
343+
(size(U, 1) == m && k ≤ size(U, 2) ≤ m) ||
344+
throw(DimensionMismatch("size of U $(size(U)) must be ($m, r) with r between k ($k) and $m"))
345+
(size(Vᴴ, 2) == n && k ≤ size(Vᴴ, 1) ≤ n) ||
346+
throw(DimensionMismatch("size of Vᴴ $(size(Vᴴ)) must be (r, $n) with r between k ($k) and $n"))
331347
@check_scalar(U, A)
332-
@check_size(S, (minmn, minmn))
333348
@check_scalar(S, A, real)
334-
@check_size(Vᴴ, (n, n))
335349
@check_scalar(Vᴴ, A)
336350
return nothing
337351
end
@@ -340,10 +354,10 @@ function initialize_output(
340354
::Union{typeof(svd_trunc!), typeof(svd_trunc_no_error!)}, A::AbstractMatrix, alg::TruncatedAlgorithm{<:CUSOLVER_Randomized}
341355
)
342356
m, n = size(A)
343-
minmn = min(m, n)
344-
U = similar(A, (m, m))
345-
S = Diagonal(similar(A, real(eltype(A)), (minmn,)))
346-
Vᴴ = similar(A, (n, n))
357+
k = _randomized_rank(alg.alg)
358+
U = similar(A, (m, k))
359+
S = Diagonal(similar(A, real(eltype(A)), (k,)))
360+
Vᴴ = similar(A, (k, n))
347361
return (U, S, Vᴴ)
348362
end
349363

@@ -356,13 +370,18 @@ end
356370
function svd_trunc_no_error!(A::AbstractMatrix, USVᴴ, alg::TruncatedAlgorithm{<:GPU_Randomized})
357371
U, S, Vᴴ = USVᴴ
358372
check_input(svd_trunc_no_error!, A, (U, S, Vᴴ), alg.alg)
359-
_gpu_Xgesvdr!(A, diagview(S), U, Vᴴ; alg.alg.kwargs...)
373+
k = _randomized_rank(alg.alg)
374+
# only the first `k` values are computed: restrict to those, so that the truncation never
375+
# sees the (uninitialized) remainder of larger outputs
376+
Uk = size(U, 2) == k ? U : view(U, :, 1:k)
377+
Sk = size(S, 1) == k ? S : Diagonal(view(diagview(S), 1:k))
378+
Vᴴk = size(Vᴴ, 1) == k ? Vᴴ : view(Vᴴ, 1:k, :)
379+
kwargs = Base.structdiff(alg.alg.kwargs, NamedTuple{(:fixgauge,)})
380+
_gpu_Xgesvdr!(A, diagview(Sk), Uk, Vᴴk; kwargs...)
360381

361-
# TODO: make sure that truncation is based on maxrank, otherwise this might be wrong
362-
(Utr, Str, Vᴴtr), _ = truncate(svd_trunc!, (U, S, Vᴴ), alg.trunc)
382+
(Utr, Str, Vᴴtr), _ = truncate(svd_trunc!, (Uk, Sk, Vᴴk), alg.trunc)
363383

364384
do_gauge_fix = get(alg.alg.kwargs, :fixgauge, default_fixgauge())::Bool
365-
# the output matrices here are the same size as for svd_full!
366385
do_gauge_fix && gaugefix!(svd_trunc!, Utr, Vᴴtr)
367386

368387
return Utr, Str, Vᴴtr

‎test/testsuite/decompositions/svd.jl‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,39 @@ function test_randomized_svd(T::Type, sz, algs; kwargs...)
712712
Ac = deepcopy(A)
713713
m, n = size(A)
714714
minmn = min(m, n)
715+
k = alg.alg.k
715716
S₀ = collect(svd_vals(A))
717+
atol = sqrt(eps(real(eltype(T)))) * S₀[1]
718+
719+
# outputs only hold the `k` computed triplets
716720
U1, S1, V1ᴴ, ϵ1 = @testinferred svd_trunc(A; alg)
717-
@test collect(diagview(S1))[1:alg.alg.k] ≈ S₀[1:alg.alg.k]
721+
@test A == Ac
722+
@test size(U1) == (m, k) && size(S1) == (k, k) && size(V1ᴴ) == (k, n)
723+
@test collect(diagview(S1)) ≈ S₀[1:k]
724+
@test isisometric(U1)
725+
@test isisometric(V1ᴴ; side = :right)
726+
@test A * V1ᴴ' ≈ U1 * S1
727+
@test ϵ1 ≈ norm(S₀[(k + 1):end]) atol = atol
728+
729+
# further truncation only looks at the `k` computed values
730+
alg2 = MatrixAlgebraKit.TruncatedAlgorithm(alg.alg, truncrank(k - 2) & trunctol(; atol = S₀[k] / 2))
731+
U2, S2, V2ᴴ, ϵ2 = svd_trunc(A; alg = alg2)
732+
@test size(S2) == (k - 2, k - 2)
733+
@test collect(diagview(S2)) ≈ S₀[1:(k - 2)]
734+
@test ϵ2 ≈ norm(S₀[(k - 1):end]) atol = atol
735+
736+
# larger (e.g. full-size) outputs are accepted, only the first `k` are used
737+
USVᴴ = (similar(A, (m, m)), Diagonal(similar(A, real(eltype(A)), (minmn,))), similar(A, (n, n)))
738+
U3, S3, V3ᴴ, ϵ3 = svd_trunc!(copy(A), USVᴴ, alg)
739+
@test collect(diagview(S3)) ≈ S₀[1:k]
740+
@test ϵ3 ≈ ϵ1 atol = atol
741+
742+
# gauge fixing is not passed on to the driver
743+
alg4 = MatrixAlgebraKit.TruncatedAlgorithm(CUSOLVER_Randomized(; alg.alg.kwargs..., fixgauge = true), alg.trunc)
744+
_, S4, _, _ = svd_trunc(A; alg = alg4)
745+
@test collect(diagview(S4)) ≈ S₀[1:k]
746+
747+
# `k` is required
748+
@test_throws ArgumentError svd_trunc(A; alg = MatrixAlgebraKit.TruncatedAlgorithm(CUSOLVER_Randomized(), alg.trunc))
718749
end
719750
end

0 commit comments

Comments
 (0)