Skip to content

Commit d07afe8

Browse files
kshyattKatharine HyattKatharine Hyatt
authored
Start on batched SVD support (#567)
* Start on batched SVD support * Offload a lot to MAK * Update Project.toml * Get rid of unneeded bypass * Force the batched path to actually run and use QRIteration for AMD + isometric * Update Project.toml * Fix blocksectors handling * Try the select_inds fixer * More CUDA tests and drop sources * Diagonal + Jacobi doesn't work * Another gesvdx painpoint on AMD * Make a specialization to avoid Diagonal batching for now * Use MAK main * Respond to comments * Update Project.toml * Work around ROCm bug in batched QRIteration * Update test comments --------- Co-authored-by: Katharine Hyatt <katharine.hyatt@ugent.be> Co-authored-by: Katharine Hyatt <katharine.s.hyatt@gmail.com>
1 parent a2add70 commit d07afe8

4 files changed

Lines changed: 201 additions & 89 deletions

File tree

‎ext/TensorKitGPUArraysExt.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ end
410410
st_dst, offs_dst = @inbounds structs_dst[blk.dst_offset + i + 1]
411411
i_dst = _linear_index(coords, st_dst, offs_dst)
412412

413-
# dst_i = β * dst_i + α * Σ_j U[i, j] * permute(src_j, p): each output tree is a
413+
# dst_i = β * dst_i + α * Σ_j U[i, j] * permute(op(src_j), p): each output tree is a
414414
# linear combination of the input trees weighted by the recoupling coefficients.
415415
# The permutation of src_j was already done by permuting its strides before the
416416
# kernel launched.

‎src/factorizations/matrixalgebrakit.jl‎

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ end
3535
# -----------------------
3636
for f! in (
3737
:qr_compact!, :qr_full!, :lq_compact!, :lq_full!,
38-
:eig_full!, :eigh_full!, :svd_compact!, :svd_full!,
39-
:left_polar!, :right_polar!,
38+
:eig_full!, :eigh_full!, :left_polar!, :right_polar!,
4039
)
4140
@eval function MAK.$f!(t::AbstractTensorMap, F, alg::AbstractAlgorithm)
4241
$(f! in (:eig_full!, :eigh_full!) && :(LinearAlgebra.checksquare(t)))
@@ -56,10 +55,38 @@ for f! in (
5655
end
5756
end
5857

58+
# these have batched versions, use them if the driver supports it
59+
# which blocks are worth batching, and how to pack them, is decided by MatrixAlgebraKit
60+
for (f!, bf!) in ((:svd_compact!, :batched_svd_compact!), (:svd_full!, :batched_svd_full!))
61+
@eval function MAK.$f!(t::AbstractTensorMap, F, alg::AbstractAlgorithm)
62+
@timeit_debug GLOBAL_TIMER $(string(f!)) begin
63+
U, S, Vᴴ = F
64+
driver = get(alg.kwargs, :driver, MAK.DefaultDriver())
65+
cs = collect(union(blocksectors.((t, U, S, Vᴴ))...))
66+
if !isempty(cs) && MAK.supports_ragged_batch(MAK.$f!, alg, driver, storagetype(t))
67+
As = [block(t, c) for c in cs]
68+
Fs = ([block(U, c) for c in cs], [block(S, c) for c in cs], [block(Vᴴ, c) for c in cs])
69+
@timeit_debug GLOBAL_TIMER "batched: MatrixAlgebraKit" MAK.$bf!(As, Fs, alg)
70+
return F
71+
end
72+
foreachblock(t, U, S, Vᴴ) do _, (tblock, Fblocks...)
73+
@timeit_debug GLOBAL_TIMER "dense: MatrixAlgebraKit" begin
74+
Fblocks′ = $f!(tblock, Fblocks, alg)
75+
# deal with the case where the output is not in-place
76+
for (b′, b) in zip(Fblocks′, Fblocks)
77+
b === b′ || copy!(b, b′)
78+
end
79+
end
80+
return nothing
81+
end
82+
return F
83+
end
84+
end
85+
end
86+
5987
# Handle these separately because single output instead of tuple
6088
for f! in (
61-
:qr_null!, :lq_null!,
62-
:svd_vals!, :eig_vals!, :eigh_vals!,
89+
:qr_null!, :lq_null!, :eig_vals!, :eigh_vals!,
6390
:project_hermitian!, :project_antihermitian!, :project_isometric!,
6491
:exponential!,
6592
)
@@ -79,6 +106,30 @@ for f! in (
79106
end
80107
end
81108

109+
# Handle these separately because single output instead of tuple AND batching is supported
110+
for (f!, bf!) in ((:svd_vals!, :batched_svd_vals!),)
111+
@eval function MAK.$f!(t::AbstractTensorMap, S, alg::AbstractAlgorithm)
112+
@timeit_debug GLOBAL_TIMER $(string(f!)) begin
113+
driver = get(alg.kwargs, :driver, MAK.DefaultDriver())
114+
cs = blocksectors(t)
115+
if !isempty(cs) && MAK.supports_ragged_batch(MAK.$f!, alg, driver, storagetype(t))
116+
As, Ss = last.(blocks(t)), last.(blocks(S))
117+
@timeit_debug GLOBAL_TIMER "batched: MatrixAlgebraKit" MAK.$bf!(As, Ss, alg)
118+
return S
119+
end
120+
foreachblock(t, S) do _, (tblock, Sblock)
121+
@timeit_debug GLOBAL_TIMER "dense: MatrixAlgebraKit" begin
122+
Sblock′ = $f!(tblock, Sblock, alg)
123+
# deal with the case where the output is not the same as the input
124+
Sblock === Sblock′ || copy!(Sblock, Sblock′)
125+
end
126+
return nothing
127+
end
128+
end
129+
return S
130+
end
131+
end
132+
82133
# Exponential with Tuple
83134
function MAK.exponential!((τ, t)::Tuple{E, T}, N, alg::AbstractAlgorithm) where {E <: Number, T <: AbstractTensorMap}
84135
LinearAlgebra.checksquare(t)

0 commit comments

Comments
 (0)