Skip to content

Commit da27a65

Browse files
lkdvosclaude
andcommitted
Address batched eigh review: defaults, docs, consistency with batched SVD
- add a default_eigh_algorithm for vectors of matrices, use AbstractVector in the CUDA ext - fix the batched_eigh_full docstring - limit CUSOLVER's batched block size to Jacobi - move the batched eigh gaugefix! next to the batched SVD ones - drop the unused BATCHED_EIGH_THRESHOLD, fix typos, match SVD algorithm order - test ragged batch support per algorithm, drop unused test_trunc options Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 26187a9 commit da27a65

6 files changed

Lines changed: 62 additions & 30 deletions

File tree

‎ext/MatrixAlgebraKitCUDAExt/MatrixAlgebraKitCUDAExt.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ end
3838
function MatrixAlgebraKit.default_eigh_algorithm(::Type{T}; kwargs...) where {T <: StridedCuArray{<:BlasFloat, 3}}
3939
return Jacobi(; kwargs...)
4040
end
41-
function MatrixAlgebraKit.default_eigh_algorithm(::Type{T}; kwargs...) where {T <: Vector{<:StridedCuMatrix{<:BlasFloat}}}
41+
function MatrixAlgebraKit.default_eigh_algorithm(::Type{T}; kwargs...) where {T <: AbstractVector{<:StridedCuMatrix{<:BlasFloat}}}
4242
return Jacobi(; kwargs...)
4343
end
4444

@@ -58,7 +58,7 @@ end
5858
MatrixAlgebraKit.supports_svd_full(::CUSOLVER, f::Symbol) = f in (:qr_iteration, :jacobi, :svd_polar)
5959

6060
# `cusolverDnXgesvdjBatched` only accepts matrices up to 32x32
61-
MatrixAlgebraKit.max_batched_blocksize(::AbstractAlgorithm, ::CUSOLVER, ::Type{<:AnyCuArray}) = 32
61+
MatrixAlgebraKit.max_batched_blocksize(::Jacobi, ::CUSOLVER, ::Type{<:AnyCuArray}) = 32
6262
# CUSOLVER only provides a batched kernel for `Jacobi`
6363
MatrixAlgebraKit.supports_ragged_batch(::typeof(svd_full!), ::Jacobi, ::CUSOLVER, ::Type{<:AnyCuArray}) = true
6464
MatrixAlgebraKit.supports_ragged_batch(::typeof(svd_compact!), ::Jacobi, ::CUSOLVER, ::Type{<:AnyCuArray}) = true

‎src/common/gauge.jl‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ end
4545
# TODO: can do the multiplication @. Vᴴk = signs_t * Vᴴk in one fell swoop
4646
# earlier, or compute signs in the n > m case entirely with reference to V?
4747
# This might require modifying forward-mode AD rules.
48-
function gaugefix!(::typeof(batched_eigh_full!), V)
49-
for v in eachslice(V, dims = 3)
50-
gaugefix!(eigh_full!, v)
51-
end
52-
return V
53-
end
54-
5548
function gaugefix!(::typeof(svd_full!), U, Vᴴ)
5649
m, n = size(U, 2), size(Vᴴ, 1)
5750
k = min(m, n)
@@ -80,6 +73,13 @@ function gaugefix!(::Union{typeof(svd_compact!), typeof(svd_trunc!)}, U, Vᴴ)
8073
return (U, Vᴴ)
8174
end
8275

76+
function gaugefix!(::typeof(batched_eigh_full!), V)
77+
for v in eachslice(V, dims = 3)
78+
gaugefix!(eigh_full!, v)
79+
end
80+
return V
81+
end
82+
8383
function gaugefix!(::typeof(batched_svd_compact!), U, Vᴴ)
8484
for (u, vᴴ) in zip(eachslice(U, dims = 3), eachslice(Vᴴ, dims = 3))
8585
gaugefix!(svd_compact!, u, vᴴ)

‎src/implementations/batched_eigh.jl‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ for f! in (:heevj_batched!, :heev_batched!, :heevd_batched!, :heevx_batched!)
9797
end
9898

9999
for (f, f_lapack!, Alg) in (
100+
(:divide_and_conquer, :heevd_batched!, :DivideAndConquer),
100101
(:qr_iteration, :heev_batched!, :QRIteration),
101-
(:jacobi, :heevj_batched!, :Jacobi),
102102
(:bisection, :heevx_batched!, :Bisection),
103-
(:divideandconquer, :heevd_batched!, :DivideAndConquer),
103+
(:jacobi, :heevj_batched!, :Jacobi),
104104
)
105105
eigh_full_f! = Symbol(:batched_eigh_full_, f, :!)
106106
eigh_vals_f! = Symbol(:batched_eigh_vals_, f, :!)
@@ -111,7 +111,7 @@ for (f, f_lapack!, Alg) in (
111111
check_input(batched_eigh_full!, A, DV, alg)
112112
driver = get(alg.kwargs, :driver, default_driver(alg, eltype(A)))
113113
supports_pointer_batch(alg, driver, eltype(A)) ||
114-
throw(ArgumentError(LazyString("driver ", driver, " does not suppport ragged (non-uniform) batches")))
114+
throw(ArgumentError(LazyString("driver ", driver, " does not support ragged (non-uniform) batches")))
115115
return $eigh_full_f!(A, DV...; alg.kwargs...)
116116
end
117117
function batched_eigh_full!(A::AbstractArray{T, 3}, DV, alg::$Alg) where {T}
@@ -122,7 +122,7 @@ for (f, f_lapack!, Alg) in (
122122
check_input(batched_eigh_vals!, A, D, alg)
123123
driver = get(alg.kwargs, :driver, default_driver(alg, eltype(A)))
124124
supports_pointer_batch(alg, driver, eltype(A)) ||
125-
throw(ArgumentError(LazyString("driver ", driver, " does not suppport ragged (non-uniform) batches")))
125+
throw(ArgumentError(LazyString("driver ", driver, " does not support ragged (non-uniform) batches")))
126126
return $eigh_vals_f!(A, D; alg.kwargs...)
127127
end
128128
function batched_eigh_vals!(A::AbstractArray{T, 3}, D, alg::$Alg) where {T}
@@ -188,12 +188,11 @@ for (f, f_lapack!, Alg) in (
188188
# Implementation
189189
@eval begin
190190
function $eigh_full_f!(driver::Driver, A, D, V; fixgauge::Bool = true, kwargs...)
191+
zero!(D)
191192
if _isempty_batch(A)
192-
zero!(D)
193193
foreach(one!, eachslice(V, dims = 3))
194194
return D, V
195195
end
196-
zero!(D)
197196
$f_lapack!(driver, A, D, V; kwargs...)
198197
fixgauge && gaugefix!(batched_eigh_full!, V)
199198
return D, V
@@ -213,10 +212,6 @@ for (f, f_lapack!, Alg) in (
213212
end
214213
end
215214

216-
# Fewest matrices in a ragged batch that are worth a batched call
217-
# Should this be settable by the user?
218-
const BATCHED_EIGH_THRESHOLD::Int = 4
219-
220215
function _packed_output(::typeof(batched_eigh_full!), A::AbstractVector{<:AbstractMatrix}, ::AbstractAlgorithm)
221216
a = first(A)
222217
m, n = size(a)

‎src/interface/batched_eigh.jl‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# Batched EIGH functions
2-
# -------------
2+
# ----------------------
33
"""
44
batched_eigh_full(A; kwargs...) -> Ds, Vs
55
batched_eigh_full(A, alg::AbstractAlgorithm) -> Ds, Vs
66
batched_eigh_full!(A, [DV]; kwargs...) -> Ds, Vs
77
batched_eigh_full!(A, [DV], alg::AbstractAlgorithm) -> Ds, Vs
88
9-
Compute the *batched* full Hermitian eigenvalue decompositions (EIGH) of the square
10-
matrices `A` of size `(n, n)`, such that `A[:, :, i] = D[:, :, i] * V[:, :, i]`.
11-
Here, `V[:, :, i]` are unitary matrices of size `(n, n)`, and `D[:, :, i]` is
12-
a real diagonal matrix of size `(n, n)`.
9+
Compute the *batched* full Hermitian eigenvalue decompositions (EIGH) of the square
10+
matrices `A` of size `(n, n)`, such that `A[:, :, i] * V[:, :, i] = V[:, :, i] * Diagonal(D[:, i])`.
11+
Here, `V[:, :, i]` are unitary matrices of size `(n, n)`, and `D` is a real matrix of size
12+
`(n, batch_size)` holding the eigenvalues. For a vector of matrices `A`, the outputs are
13+
vectors of `Diagonal` eigenvalue matrices and eigenvector matrices instead.
1314
1415
!!! note
1516
The bang method `batched_eigh_full!` optionally accepts the output structure and
@@ -40,3 +41,4 @@ for f in (:batched_eigh_full!, :batched_eigh_vals!)
4041
return default_eigh_algorithm(A; kwargs...)
4142
end
4243
end
44+
default_eigh_algorithm(::Type{V}; kwargs...) where {M <: AbstractMatrix, V <: AbstractVector{M}} = default_eigh_algorithm(M; kwargs...)

‎test/decompositions/eigh.jl‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ for T in (BLASFloats..., GenericFloats...)
3434
TestSuite.test_eigh_algs(Diagonal{T, CuVector{T}}, m, (DiagonalAlgorithm(),))
3535

3636
TestSuite.test_eigh_batched(CuMatrix{T}, (m, m), batch_size)
37-
CUSOLVER_EIGH_ALGS = (Jacobi(), QRIteration())
38-
TestSuite.test_eigh_batched_algs(CuMatrix{T}, (m, m), batch_size, CUSOLVER_EIGH_ALGS)
37+
CUSOLVER_BATCHED_EIGH_ALGS = (Jacobi(), QRIteration())
38+
TestSuite.test_eigh_batched_algs(CuMatrix{T}, (m, m), batch_size, CUSOLVER_BATCHED_EIGH_ALGS)
39+
TestSuite.test_eigh_algs_batched_ragged_support(CuMatrix{T}, (Jacobi(),), batch_size)
40+
TestSuite.test_eigh_algs_batched_ragged_support(CuMatrix{T}, (QRIteration(), DivideAndConquer()), batch_size; supported = false)
3941
end
4042
if AMDGPU.functional()
4143
ROCSOLVER_EIGH_ALGS = (
@@ -52,6 +54,7 @@ for T in (BLASFloats..., GenericFloats...)
5254

5355
TestSuite.test_eigh_batched(ROCMatrix{T}, (m, m), batch_size)
5456
TestSuite.test_eigh_batched_algs(ROCMatrix{T}, (m, m), batch_size, ROCSOLVER_EIGH_ALGS)
57+
TestSuite.test_eigh_algs_batched_ragged_support(ROCMatrix{T}, ROCSOLVER_EIGH_ALGS, batch_size)
5558
end
5659
end
5760
if !is_buildkite

‎test/testsuite/decompositions/eigh.jl‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ function test_eigh(T::Type, sz; test_trunc = true, kwargs...)
1111
end
1212
end
1313

14-
function test_eigh_batched(T::Type, sz, batch_size::Int; test_trunc = false, kwargs...)
14+
function test_eigh_batched(T::Type, sz, batch_size::Int; kwargs...)
1515
summary_str = testargs_summary(T, sz)
1616
return @testset "eigh batched $summary_str batch_size $batch_size" begin
1717
test_eigh_full_batched(T, sz, batch_size; kwargs...)
18-
test_trunc && test_eigh_trunc_batched(T, sz, batch_size; kwargs...)
1918
end
2019
end
2120

@@ -27,11 +26,44 @@ function test_eigh_algs(T::Type, sz, algs; test_trunc = true, kwargs...)
2726
end
2827
end
2928

30-
function test_eigh_batched_algs(T::Type, sz, batch_size::Int, algs; test_trunc = false, kwargs...)
29+
function test_eigh_batched_algs(T::Type, sz, batch_size::Int, algs; kwargs...)
3130
summary_str = testargs_summary(T, sz)
3231
return @testset "eigh algorithms $summary_str batch_size $batch_size" begin
3332
test_eigh_full_algs_batched(T, sz, algs, batch_size; kwargs...)
34-
test_trunc && test_eigh_trunc_algs_batched(T, sz, algs, batch_size; kwargs...)
33+
end
34+
end
35+
36+
# Whether the driver for `alg` reports support for ragged batches of each EIGH function,
37+
# and if so, that ragged batches of all distinct sizes are decomposed correctly.
38+
function test_eigh_algs_batched_ragged_support(
39+
T::Type, algs, batch_size::Int;
40+
supported::Bool = true,
41+
atol::Real = 0, rtol::Real = precision(eltype(T)),
42+
kwargs...
43+
)
44+
summary_str = testargs_summary(T)
45+
return @testset "batched eigh ragged support, algorithm $alg $summary_str" for alg in algs
46+
driver = MatrixAlgebraKit.default_driver(alg, T)
47+
for f! in (eigh_full!, eigh_vals!)
48+
@test MatrixAlgebraKit.supports_ragged_batch(f!, alg, driver, T) == supported
49+
@test MatrixAlgebraKit.supports_ragged_batch(f!, alg, MatrixAlgebraKit.DefaultDriver(), T) == supported
50+
end
51+
52+
if supported
53+
Ar = [project_hermitian!(instantiate_matrix(T, (1 + i, 1 + i))) for i in 1:batch_size]
54+
@test allunique(size.(Ar))
55+
56+
D, V = @testinferred batched_eigh_full(Ar; alg)
57+
for (a, d, v) in zip(Ar, D, V)
58+
@test a * v ≈ v * d
59+
@test isunitary(v)
60+
end
61+
62+
Dv = @testinferred batched_eigh_vals(Ar; alg)
63+
for (d, dv) in zip(D, Dv)
64+
@test collect(diagview(d)) ≈ collect(dv)
65+
end
66+
end
3567
end
3668
end
3769

0 commit comments

Comments
 (0)