Skip to content

Commit d8ec612

Browse files
committed
Tests for new function setting
1 parent 9245c58 commit d8ec612

4 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎ext/MatrixAlgebraKitAMDGPUExt/MatrixAlgebraKitAMDGPUExt.jl‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ MatrixAlgebraKit.supports_svd_full(::ROCSOLVER, f::Symbol) = f in (:qr_iteration
4242
# of device pointers, so a group of equally sized matrices
4343
# doesn't need a copy into a 3D ROCArray.
4444
MatrixAlgebraKit.supports_pointer_batch(::AbstractAlgorithm, ::ROCSOLVER, ::Type{<:StridedROCMatrix{<:BlasFloat}}) = true
45-
MatrixAlgebraKit.supports_ragged_batch(f!, ::AbstractAlgorithm, ::ROCSOLVER, ::Type{<:AnyROCArray}) = true
45+
MatrixAlgebraKit.supports_ragged_batch(::typeof(svd_full!), ::AbstractAlgorithm, ::ROCSOLVER, ::Type{<:AnyROCArray}) = true
46+
MatrixAlgebraKit.supports_ragged_batch(::typeof(svd_compact!), ::AbstractAlgorithm, ::ROCSOLVER, ::Type{<:AnyROCArray}) = true
47+
MatrixAlgebraKit.supports_ragged_batch(::typeof(svd_vals!), ::AbstractAlgorithm, ::ROCSOLVER, ::Type{<:AnyROCArray}) = true
4648

4749
function gesvd!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...)
4850
m, n = size(A)

‎ext/MatrixAlgebraKitCUDAExt/MatrixAlgebraKitCUDAExt.jl‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ MatrixAlgebraKit.supports_svd_full(::CUSOLVER, f::Symbol) = f in (:qr_iteration,
4343
# `cusolverDnXgesvdjBatched` only accepts matrices up to 32x32
4444
MatrixAlgebraKit.max_batched_blocksize(::AbstractAlgorithm, ::CUSOLVER, ::Type{<:AnyCuArray}) = 32
4545
# CUSOLVER only provides a batched kernel for `Jacobi`
46-
MatrixAlgebraKit.supports_ragged_batch(f!, ::Jacobi, ::CUSOLVER, ::Type{<:AnyCuArray}) = true
46+
MatrixAlgebraKit.supports_ragged_batch(::typeof(svd_full!), ::Jacobi, ::CUSOLVER, ::Type{<:AnyCuArray}) = true
47+
MatrixAlgebraKit.supports_ragged_batch(::typeof(svd_compact!), ::Jacobi, ::CUSOLVER, ::Type{<:AnyCuArray}) = true
48+
MatrixAlgebraKit.supports_ragged_batch(::typeof(svd_vals!), ::Jacobi, ::CUSOLVER, ::Type{<:AnyCuArray}) = true
4749

4850
function gesvd!(::CUSOLVER, A::StridedCuMatrix, S::StridedCuVector, U::StridedCuMatrix, Vᴴ::StridedCuMatrix; kwargs...)
4951
m, n = size(A)

‎test/decompositions/svd.jl‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ if CUDA.functional()
6666
for T in BLASFloats
6767
TestSuite.seed_rng!(123)
6868
TestSuite.test_svd_algs_batched_oversized(CuMatrix{T}, (Jacobi(),), batch_size)
69+
TestSuite.test_svd_algs_batched_ragged_support(CuMatrix{T}, (Jacobi(),), batch_size)
70+
TestSuite.test_svd_algs_batched_ragged_support(CuMatrix{T}, (QRIteration(),), batch_size; supported = false)
6971
end
7072

7173
# Randomized SVD:
@@ -98,6 +100,11 @@ if AMDGPU.functional()
98100
TestSuite.test_svd_batched(ROCMatrix{T}, (m, n), batch_size)
99101
TestSuite.test_svd_batched_algs(ROCMatrix{T}, (m, n), batch_size, AMD_SVD_ALGS)
100102
end
103+
for T in BLASFloats
104+
TestSuite.seed_rng!(123)
105+
AMD_SVD_ALGS = (QRIteration(), Jacobi(), DivideAndConquer(), Bisection())
106+
TestSuite.test_svd_algs_batched_ragged_support(ROCMatrix{T}, AMD_SVD_ALGS, batch_size)
107+
end
101108

102109
@testset "Bisection with min(m, n) == 1 $(testargs_summary(T, sz))" for T in BLASFloats,
103110
sz in ((1, 1), (2, 1), (5, 1), (1, 2), (1, 5))

‎test/testsuite/decompositions/svd.jl‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,50 @@ function test_svd_algs_batched_oversized(
454454
end
455455
end
456456

457+
# Whether the driver for `alg` reports support for ragged batches of each SVD function,
458+
# and if so, that ragged batches of all distinct sizes are decomposed correctly.
459+
function test_svd_algs_batched_ragged_support(
460+
T::Type, algs, batch_size::Int;
461+
supported::Bool = true,
462+
atol::Real = 0, rtol::Real = precision(eltype(T)),
463+
kwargs...
464+
)
465+
summary_str = testargs_summary(T)
466+
return @testset "batched svd ragged support, algorithm $alg $summary_str" for alg in algs
467+
driver = MatrixAlgebraKit.default_driver(alg, T)
468+
for f! in (svd_compact!, svd_full!, svd_vals!)
469+
@test MatrixAlgebraKit.supports_ragged_batch(f!, alg, driver, T) == supported
470+
@test MatrixAlgebraKit.supports_ragged_batch(f!, alg, MatrixAlgebraKit.DefaultDriver(), T) == supported
471+
end
472+
# only the SVD functions have ragged batched implementations
473+
@test !MatrixAlgebraKit.supports_ragged_batch(qr_full!, alg, driver, T)
474+
475+
if supported
476+
Ar = [instantiate_matrix(T, (2 + i, 1 + (i * 7) % (batch_size + 3))) for i in 1:batch_size]
477+
@test allunique(size.(Ar))
478+
479+
U, S, Vᴴ = @testinferred batched_svd_compact(Ar; alg)
480+
for (a, u, s, vᴴ) in zip(Ar, U, S, Vᴴ)
481+
@test u * s * vᴴ ≈ a
482+
@test isisometric(u)
483+
@test isisometric(vᴴ; side = :right)
484+
end
485+
486+
Uf, Sf, Vfᴴ = @testinferred batched_svd_full(Ar; alg)
487+
for (a, u, s, vᴴ) in zip(Ar, Uf, Sf, Vfᴴ)
488+
@test u * s * vᴴ ≈ a
489+
@test isunitary(u)
490+
@test isunitary(vᴴ)
491+
end
492+
493+
Sv = @testinferred batched_svd_vals(Ar; alg)
494+
for (s, sv) in zip(S, Sv)
495+
@test collect(diagview(s)) ≈ collect(sv)
496+
end
497+
end
498+
end
499+
end
500+
457501
function test_svd_trunc(
458502
T::Type, sz;
459503
atol::Real = 0, rtol::Real = precision(eltype(T)),

0 commit comments

Comments
 (0)