Skip to content

Commit 08ff30e

Browse files
Katharine HyattKatharine Hyatt
authored andcommitted
Add special path for AMDGPU, implement gesvdx batching there, and fix some errors
1 parent 5053f50 commit 08ff30e

3 files changed

Lines changed: 178 additions & 22 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, :bisection)
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ᴴ)
@@ -67,7 +72,7 @@ function gesvdx!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::Strid
6772
end
6873

6974
# rocSOLVER's batched `gesvd` requires m ≥ n, so wide matrices go through the adjoint
70-
function gesvd_batched!(::ROCSOLVER, As::Vector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat}
75+
function gesvd_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat}
7176
m, n = size(first(As))
7277
m >= n && return YArocSOLVER.gesvd_batched!(As, Ss, Us, Vᴴs; kwargs...)
7378
return MatrixAlgebraKit.batched_svd_via_adjoint!(gesvd_batched!, ROCSOLVER(), As, Ss, Us, Vᴴs; kwargs...)
@@ -78,17 +83,17 @@ function gesvd_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMa
7883
return MatrixAlgebraKit.batched_svd_via_adjoint!(gesvd_batched!, ROCSOLVER(), As, Ss, Us, Vᴴs; kwargs...)
7984
end
8085

81-
gesdd_batched!(::ROCSOLVER, As::Vector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
86+
gesdd_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
8287
YArocSOLVER.gesdd_batched!(As, Ss, Us, Vᴴs; kwargs...)
8388
gesdd_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
8489
YArocSOLVER.gesdd_strided_batched!(As, Ss, Us, Vᴴs; kwargs...)
8590

86-
gesvdj_batched!(::ROCSOLVER, As::Vector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
91+
gesvdj_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
8792
YArocSOLVER.gesvdj_batched!(As, Ss, Us, Vᴴs; kwargs...)
8893
gesvdj_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
8994
YArocSOLVER.gesvdj_strided_batched!(As, Ss, Us, Vᴴs; kwargs...)
9095

91-
gesvdx_batched!(::ROCSOLVER, As::Vector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
96+
gesvdx_batched!(::ROCSOLVER, As::AbstractVector{<:StridedROCMatrix}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
9297
YArocSOLVER.gesvdx_batched!(As, Ss, Us, Vᴴs; kwargs...)
9398
gesvdx_batched!(::ROCSOLVER, As::StridedROCArray{T, 3}, Ss::StridedROCMatrix, Us::StridedROCArray{T, 3}, Vᴴs::StridedROCArray{T, 3}; kwargs...) where {T <: BlasFloat} =
9499
YArocSOLVER.gesvdx_strided_batched!(As, Ss, Us, Vᴴs; kwargs...)

‎ext/MatrixAlgebraKitAMDGPUExt/yarocsolver.jl‎

Lines changed: 119 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ for (fname, elty, relty) in
107107
)
108108
@eval begin
109109
function gesvd_batched!(
110-
A::StridedROCVector{<:StridedROCMatrix{$elty}},
110+
A::AbstractVector{<:StridedROCMatrix{$elty}},
111111
S::StridedROCMatrix{$relty} = similar(first(A), $relty, (min(size(first(A))...), length(A))),
112112
U::StridedROCArray{$elty, 3} = similar(first(A), $elty, size(first(A), 1), min(size(first(A))...), length(A)),
113113
Vᴴ::StridedROCArray{$elty, 3} = similar(first(A), $elty, min(size(first(A))...), size(first(A), 2), length(A)),
@@ -166,7 +166,7 @@ for (fname, elty, relty) in
166166
E = ROCArray{$relty}(undef, length(A) * strideE)
167167
dh = rocBLAS.handle()
168168
dev_info = ROCVector{Cint}(undef, length(A))
169-
pA = map(pointer, A)
169+
pA = ROCVector(map(pointer, A))
170170
rocSOLVER.$fname(
171171
dh, jobu, jobvt, m, n, pA, lda,
172172
S, strideS, U, ldu, strideU, Vᴴ, ldv, strideV,
@@ -350,7 +350,7 @@ for (fname, elty, relty) in
350350
)
351351
@eval begin
352352
function gesdd_batched!(
353-
A::StridedROCVector{<:StridedROCMatrix{$elty}},
353+
A::AbstractVector{<:StridedROCMatrix{$elty}},
354354
S::StridedROCMatrix{$relty} = similar(first(A), $relty, (min(size(first(A))...), length(A))),
355355
U::StridedROCArray{$elty, 3} = similar(first(A), $elty, size(first(A), 1), min(size(first(A))...), length(A)),
356356
Vᴴ::StridedROCArray{$elty, 3} = similar(first(A), $elty, min(size(first(A))...), size(first(A), 2), length(A)),
@@ -406,7 +406,7 @@ for (fname, elty, relty) in
406406

407407
dh = rocBLAS.handle()
408408
dev_info = ROCVector{Cint}(undef, length(A))
409-
pA = map(pointer, A)
409+
pA = ROCVector(map(pointer, A))
410410
rocSOLVER.$fname(
411411
dh, jobu, jobvt, m, n, pA, lda,
412412
S, strideS, U, ldu, strideU, Vᴴ, ldv, strideV,
@@ -591,7 +591,7 @@ for (fname, elty, relty) in
591591
)
592592
@eval begin
593593
function gesvdj_batched!(
594-
A::StridedROCVector{<:StridedROCMatrix{$elty}},
594+
A::AbstractVector{<:StridedROCMatrix{$elty}},
595595
S::StridedROCMatrix{$relty} = similar(first(A), $relty, (min(size(first(A))...), length(A))),
596596
U::StridedROCArray{$elty, 3} = similar(first(A), $elty, size(first(A), 1), min(size(first(A))...), length(A)),
597597
Vᴴ::StridedROCArray{$elty, 3} = similar(first(A), $elty, min(size(first(A))...), size(first(A), 2), length(A)),
@@ -652,7 +652,7 @@ for (fname, elty, relty) in
652652
dev_n_sweeps = ROCVector{Cint}(undef, length(A))
653653

654654
dh = rocBLAS.handle()
655-
pA = map(pointer, A)
655+
pA = ROCVector(map(pointer, A))
656656
rocSOLVER.$fname(
657657
dh, jobu, jobvt, m, n, pA, lda, tol,
658658
dev_residual, max_sweeps, dev_n_sweeps,
@@ -848,6 +848,119 @@ for (fname, elty, relty) in
848848
end
849849
end
850850

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

‎src/implementations/batched_svd.jl‎

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ copy_input(::typeof(batched_svd_compact), A) = copy_input(batched_svd_full, A)
66
copy_input(::typeof(batched_svd_vals), A) = copy_input(batched_svd_full, A)
77

88
function check_input(::typeof(batched_svd_full!), A::AbstractVector{<:AbstractMatrix}, USVᴴ, ::AbstractAlgorithm)
9+
isempty(A) && return nothing
910
@assert all(==(size(first(A))), size.(A))
1011
m, n = size(first(A))
1112
batch_size = length(A)
@@ -20,6 +21,7 @@ function check_input(::typeof(batched_svd_full!), A::AbstractVector{<:AbstractMa
2021
return nothing
2122
end
2223
function check_input(::typeof(batched_svd_compact!), A::AbstractVector{<:AbstractMatrix}, USVᴴ, ::AbstractAlgorithm)
24+
isempty(A) && return nothing
2325
@assert all(==(size(first(A))), size.(A))
2426
m, n = size(first(A))
2527
batch_size = length(A)
@@ -35,6 +37,7 @@ function check_input(::typeof(batched_svd_compact!), A::AbstractVector{<:Abstrac
3537
return nothing
3638
end
3739
function check_input(::typeof(batched_svd_vals!), A::AbstractVector{<:AbstractMatrix}, S, ::AbstractAlgorithm)
40+
isempty(A) && return nothing
3841
@assert all(==(size(first(A))), size.(A))
3942
m, n = size(first(A))
4043
batch_size = length(A)
@@ -153,6 +156,9 @@ function initialize_output(::typeof(batched_svd_vals!), A::AbstractArray{T, 3},
153156
return similar(A, real(eltype(A)), (min(m, n), batch_size))
154157
end
155158

159+
_isempty_batch(A::AbstractArray{<:Any, 3}) = isempty(A)
160+
_isempty_batch(A::AbstractVector{<:AbstractMatrix}) = all(isempty, A)
161+
156162
for f! in (:gesdd_batched!, :gesvd_batched!, :gesvdj_batched!, :gesvdx_batched!)
157163
@eval $f!(driver::Driver, args...) = throw(ArgumentError("$driver does not provide $($(f!))"))
158164
end
@@ -234,8 +240,8 @@ for (f, f_lapack!, Alg) in (
234240
Us, Ss, Vᴴs = USVᴴ
235241
batches, rest = _ragged_batches(A, alg)
236242
for (inds, (m, n)) in batches
237-
Ab = _ragged_pack(A, inds, m, n)
238-
Ub, Sb, Vᴴb = batched_svd_compact!(Ab, initialize_output(batched_svd_compact!, Ab, alg), alg)
243+
Ab = _ragged_pack(A, inds, m, n, alg)
244+
Ub, Sb, Vᴴb = batched_svd_compact!(Ab, _packed_output(batched_svd_compact!, Ab, alg), alg)
239245
for (j, i) in enumerate(inds)
240246
copyto!(Us[i], view(Ub, axes(Us[i])..., j))
241247
copyto!(Ss[i], view(Sb, axes(Ss[i], 1), j))
@@ -258,8 +264,8 @@ for (f, f_lapack!, Alg) in (
258264
# `U` and `Vᴴ`, so only matrices of equal size are batched
259265
batches, rest = _ragged_batches(A, alg; pad = false)
260266
for (inds, (m, n)) in batches
261-
Ab = _ragged_pack(A, inds, m, n)
262-
Ub, Sb, Vᴴb = batched_svd_full!(Ab, initialize_output(batched_svd_full!, Ab, alg), alg)
267+
Ab = _ragged_pack(A, inds, m, n, alg)
268+
Ub, Sb, Vᴴb = batched_svd_full!(Ab, _packed_output(batched_svd_full!, Ab, alg), alg)
263269
for (j, i) in enumerate(inds)
264270
copyto!(Us[i], view(Ub, :, :, j))
265271
copyto!(Ss[i], view(Sb, :, :, j))
@@ -277,8 +283,8 @@ for (f, f_lapack!, Alg) in (
277283
check_input(batched_svd_vals!, A, S, alg)
278284
batches, rest = _ragged_batches(A, alg)
279285
for (inds, (m, n)) in batches
280-
Ab = _ragged_pack(A, inds, m, n)
281-
Sb = batched_svd_vals!(Ab, initialize_output(batched_svd_vals!, Ab, alg), alg)
286+
Ab = _ragged_pack(A, inds, m, n, alg)
287+
Sb = batched_svd_vals!(Ab, _packed_output(batched_svd_vals!, Ab, alg), alg)
282288
for (j, i) in enumerate(inds)
283289
copyto!(S[i], view(Sb, axes(S[i], 1), j))
284290
end
@@ -306,7 +312,7 @@ for (f, f_lapack!, Alg) in (
306312
# Implementation
307313
@eval begin
308314
function $svd_compact_f!(driver::Driver, A, U, S, Vᴴ; fixgauge::Bool = true, kwargs...)
309-
isempty(A) && return one!(U), zero!(S), one!(Vᴴ)
315+
_isempty_batch(A) && return one!(U), zero!(S), one!(Vᴴ)
310316
$f_lapack!(driver, A, S, U, Vᴴ; kwargs...)
311317
if fixgauge
312318
for (u, vᴴ) in zip(eachslice(U, dims = 3), eachslice(Vᴴ, dims = 3))
@@ -318,7 +324,7 @@ for (f, f_lapack!, Alg) in (
318324
function $svd_full_f!(driver::Driver, A, U, S, Vᴴ; fixgauge::Bool = true, kwargs...)
319325
supports_svd_full(driver, $(QuoteNode(f))) ||
320326
throw(ArgumentError(LazyString("driver ", driver, " does not provide `$($(QuoteNode(f_lapack!)))`")))
321-
isempty(A) && return one!(U), zero!(S), one!(Vᴴ)
327+
_isempty_batch(A) && return one!(U), zero!(S), one!(Vᴴ)
322328
zero!(S)
323329
m, n, batch_size = size(S)
324330
minmn = min(m, n)
@@ -335,13 +341,13 @@ for (f, f_lapack!, Alg) in (
335341
return U, S, Vᴴ
336342
end
337343
function $svd_vals_f!(driver::Driver, A::AbstractArray{T, 3}, S::AbstractMatrix; fixgauge::Bool = true, kwargs...) where {T}
338-
isempty(A) && return zero!(S)
344+
_isempty_batch(A) && return zero!(S)
339345
U, Vᴴ = similar(A, (0, 0, 0)), similar(A, (0, 0, 0))
340346
$f_lapack!(driver, A, S, U, Vᴴ; kwargs...)
341347
return S
342348
end
343349
function $svd_vals_f!(driver::Driver, A::AbstractVector{<:AbstractMatrix}, S::AbstractMatrix; fixgauge::Bool = true, kwargs...)
344-
isempty(A) && return zero!(S)
350+
_isempty_batch(A) && return zero!(S)
345351
U, Vᴴ = similar(first(A), (0, 0, 0)), similar(first(A), (0, 0, 0))
346352
$f_lapack!(driver, A, S, U, Vᴴ; kwargs...)
347353
return S
@@ -359,6 +365,16 @@ Larger matrices in a ragged batch are decomposed one at a time instead. Unlimite
359365
"""
360366
max_batched_blocksize(::AbstractAlgorithm, ::Type) = typemax(Int)
361367

368+
"""
369+
supports_pointer_batch(alg, T::Type) -> Bool
370+
371+
Whether the low-level batched driver version of `alg` accepts a batch of matrices of type `T`
372+
as an `AbstractVector` of separately allocated matrices. Such a group of matrices is handed
373+
to the driver as a vector of pointers, instead of being copied into one contiguous 3D array.
374+
`false` by default.
375+
"""
376+
supports_pointer_batch(::AbstractAlgorithm, ::Type) = false
377+
362378
# Fewest matrices in a ragged batch that are worth a batched call
363379
# Should this be settable by the user?
364380
const BATCHED_SVD_THRESHOLD::Int = 4
@@ -399,9 +415,31 @@ function _ragged_batches(A::AbstractVector{<:AbstractMatrix}, alg::AbstractAlgor
399415
return batches, rest
400416
end
401417

402-
# Copy `A[inds]` into a single `(m, n, length(inds))` batch, zero-padding where needed.
403-
function _ragged_pack(A::AbstractVector{<:AbstractMatrix}, inds, m::Int, n::Int)
418+
# Outputs for a batch that `_ragged_pack` produced, which is either a contiguous `(m, n, b)`
419+
# array or, for a pointer-batch driver, a view of `b` equally sized matrices. Either way the
420+
# outputs are packed into the 3D arrays the batched drivers write into.
421+
_packed_output(f!, A::AbstractArray{<:Any, 3}, alg::AbstractAlgorithm) = initialize_output(f!, A, alg)
422+
function _packed_output(::typeof(batched_svd_full!), A::AbstractVector{<:AbstractMatrix}, ::AbstractAlgorithm)
423+
a = first(A)
424+
m, n = size(a)
425+
b = length(A)
426+
return (similar(a, (m, m, b)), similar(a, real(eltype(a)), (m, n, b)), similar(a, (n, n, b)))
427+
end
428+
function _packed_output(::typeof(batched_svd_compact!), A::AbstractVector{<:AbstractMatrix}, ::AbstractAlgorithm)
429+
a = first(A)
430+
m, n = size(a)
431+
minmn, b = min(m, n), length(A)
432+
return (similar(a, (m, minmn, b)), similar(a, real(eltype(a)), (minmn, b)), similar(a, (minmn, n, b)))
433+
end
434+
function _packed_output(::typeof(batched_svd_vals!), A::AbstractVector{<:AbstractMatrix}, ::AbstractAlgorithm)
435+
a = first(A)
436+
return similar(a, real(eltype(a)), (min(size(a)...), length(A)))
437+
end
438+
439+
# Gather `A[inds]` into a single `(m, n, length(inds))` batch, zero-padding where needed.
440+
function _ragged_pack(A::AbstractVector{<:AbstractMatrix}, inds, m::Int, n::Int, alg::AbstractAlgorithm)
404441
uniform = all(i -> size(A[i]) == (m, n), inds)
442+
uniform && supports_pointer_batch(alg, typeof(A[first(inds)])) && return view(A, inds)
405443
# `stack` can't zero-pad
406444
# On the GPU it falls back to scalar indexing for matrices that are views
407445
uniform && A isa AbstractVector{<:Array} && return stack(view(A, inds))

0 commit comments

Comments
 (0)