Skip to content

Commit 1813d47

Browse files
committed
Implement DefaultAlgorithm support (#422)
* add `DefaultAlgorithm` overloads * add DefaultAlgorithm tests * disambiguate
1 parent 2876a6f commit 1813d47

6 files changed

Lines changed: 344 additions & 5 deletions

File tree

‎src/factorizations/matrixalgebrakit.jl‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,38 @@ end
6464

6565
MAK.zero!(t::AbstractTensorMap) = zerovector!(t)
6666

67+
# Default algorithm
68+
# -----------------
69+
for f in [
70+
:lq_full, :lq_compact, :lq_null,
71+
:qr_full, :qr_compact, :qr_null,
72+
:schur_full, :schur_vals,
73+
:eig_full, :eig_vals, :eig_trunc, :eig_trunc_no_error,
74+
:eigh_full, :eigh_vals, :eigh_trunc, :eigh_trunc_no_error,
75+
:svd_full, :svd_compact, :svd_trunc, :svd_trunc_no_error, :svd_vals,
76+
:left_polar, :right_polar,
77+
:left_orth, :right_orth, :left_null, :right_null,
78+
:project_hermitian, :project_antihermitian, :project_isometric,
79+
]
80+
f! = Symbol(f, :!)
81+
@eval MAK.$f!(t::AbstractTensorMap, alg::DefaultAlgorithm) =
82+
MAK.$f!(t, MAK.select_algorithm(MAK.$f!, t, nothing; alg.kwargs...))
83+
@eval MAK.$f!(t::AbstractTensorMap, out, alg::DefaultAlgorithm) =
84+
MAK.$f!(t, out, MAK.select_algorithm(MAK.$f!, t, nothing; alg.kwargs...))
85+
86+
# disambiguate
87+
@eval MAK.$f!(t::AdjointTensorMap, alg::DefaultAlgorithm) =
88+
MAK.$f!(t, MAK.select_algorithm(MAK.$f!, t, nothing; alg.kwargs...))
89+
@eval MAK.$f!(t::AdjointTensorMap, out, alg::DefaultAlgorithm) =
90+
MAK.$f!(t, out, MAK.select_algorithm(MAK.$f!, t, nothing; alg.kwargs...))
91+
92+
@eval MAK.$f!(t::DiagonalTensorMap, alg::DefaultAlgorithm) =
93+
MAK.$f!(t, MAK.select_algorithm(MAK.$f!, t, nothing; alg.kwargs...))
94+
@eval MAK.$f!(t::DiagonalTensorMap, out, alg::DefaultAlgorithm) =
95+
MAK.$f!(t, out, MAK.select_algorithm(MAK.$f!, t, nothing; alg.kwargs...))
96+
end
97+
98+
6799
# Singular value decomposition
68100
# ----------------------------
69101
function MAK.initialize_output(::typeof(svd_full!), t::AbstractTensorMap, ::AbstractAlgorithm)

‎test/cuda/factorizations.jl‎

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using Adapt, CUDA, cuTENSOR
22
using Test, TestExtras
33
using TensorKit
44
using LinearAlgebra: LinearAlgebra
5-
using MatrixAlgebraKit: diagview
5+
using MatrixAlgebraKit: DefaultAlgorithm, diagview
66
const CUDAExt = Base.get_extension(TensorKit, :TensorKitCUDAExt)
77
@assert !isnothing(CUDAExt) "Failed to load TensorKit - CUDA extension"
88
const CuTensorMap = getglobal(CUDAExt, :CuTensorMap)
@@ -33,21 +33,41 @@ for V in spacelist
3333
@test Q * R ≈ t
3434
@test isunitary(Q)
3535

36+
Q, R = @constinferred qr_full(t, DefaultAlgorithm())
37+
@test Q * R ≈ t
38+
@test isunitary(Q)
39+
3640
Q, R = @constinferred qr_compact(t)
3741
@test Q * R ≈ t
3842
@test isisometric(Q)
3943

44+
Q, R = @constinferred qr_compact(t, DefaultAlgorithm())
45+
@test Q * R ≈ t
46+
@test isisometric(Q)
47+
4048
Q, R = @constinferred left_orth(t)
4149
@test Q * R ≈ t
4250
@test isisometric(Q)
4351

52+
Q, R = @constinferred left_orth(t, DefaultAlgorithm())
53+
@test Q * R ≈ t
54+
@test isisometric(Q)
55+
4456
N = @constinferred qr_null(t)
4557
@test isisometric(N)
4658
@test norm(N' * t) ≈ 0 atol = 100 * eps(norm(t))
4759

60+
N = @constinferred qr_null(t, DefaultAlgorithm())
61+
@test isisometric(N)
62+
@test norm(N' * t) ≈ 0 atol = 100 * eps(norm(t))
63+
4864
N = @constinferred left_null(t)
4965
@test isisometric(N)
5066
@test norm(N' * t) ≈ 0 atol = 100 * eps(norm(t))
67+
68+
N = @constinferred left_null(t, DefaultAlgorithm())
69+
@test isisometric(N)
70+
@test norm(N' * t) ≈ 0 atol = 100 * eps(norm(t))
5171
end
5272

5373
# empty tensor
@@ -59,19 +79,38 @@ for V in spacelist
5979
@test isunitary(Q)
6080
@test dim(R) == dim(t) == 0
6181

82+
Q, R = @constinferred qr_full(t, DefaultAlgorithm())
83+
@test Q * R ≈ t
84+
@test isunitary(Q)
85+
@test dim(R) == dim(t) == 0
86+
6287
Q, R = @constinferred qr_compact(t)
6388
@test Q * R ≈ t
6489
@test isisometric(Q)
6590
@test dim(Q) == dim(R) == dim(t)
6691

92+
Q, R = @constinferred qr_compact(t, DefaultAlgorithm())
93+
@test Q * R ≈ t
94+
@test isisometric(Q)
95+
@test dim(Q) == dim(R) == dim(t)
96+
6797
Q, R = @constinferred left_orth(t)
6898
@test Q * R ≈ t
6999
@test isisometric(Q)
70100
@test dim(Q) == dim(R) == dim(t)
71101

102+
Q, R = @constinferred left_orth(t, DefaultAlgorithm())
103+
@test Q * R ≈ t
104+
@test isisometric(Q)
105+
@test dim(Q) == dim(R) == dim(t)
106+
72107
N = @constinferred qr_null(t)
73108
@test isunitary(N)
74109
@test norm(N' * t) ≈ 0 atol = 100 * eps(norm(t))
110+
111+
N = @constinferred qr_null(t, DefaultAlgorithm())
112+
@test isunitary(N)
113+
@test norm(N' * t) ≈ 0 atol = 100 * eps(norm(t))
75114
end
76115
end
77116

@@ -88,17 +127,33 @@ for V in spacelist
88127
@test L * Q ≈ t
89128
@test isunitary(Q)
90129

130+
L, Q = @constinferred lq_full(t, DefaultAlgorithm())
131+
@test L * Q ≈ t
132+
@test isunitary(Q)
133+
91134
L, Q = @constinferred lq_compact(t)
92135
@test L * Q ≈ t
93136
@test isisometric(Q; side = :right)
94137

138+
L, Q = @constinferred lq_compact(t, DefaultAlgorithm())
139+
@test L * Q ≈ t
140+
@test isisometric(Q; side = :right)
141+
95142
L, Q = @constinferred right_orth(t)
96143
@test L * Q ≈ t
97144
@test isisometric(Q; side = :right)
98145

146+
L, Q = @constinferred right_orth(t, DefaultAlgorithm())
147+
@test L * Q ≈ t
148+
@test isisometric(Q; side = :right)
149+
99150
Nᴴ = @constinferred lq_null(t)
100151
@test isisometric(Nᴴ; side = :right)
101152
@test norm(t * Nᴴ') ≈ 0 atol = 100 * eps(norm(t))
153+
154+
Nᴴ = @constinferred lq_null(t, DefaultAlgorithm())
155+
@test isisometric(Nᴴ; side = :right)
156+
@test norm(t * Nᴴ') ≈ 0 atol = 100 * eps(norm(t))
102157
end
103158

104159
for T in eltypes
@@ -110,19 +165,38 @@ for V in spacelist
110165
@test isunitary(Q)
111166
@test dim(L) == dim(t) == 0
112167

168+
L, Q = @constinferred lq_full(t, DefaultAlgorithm())
169+
@test L * Q ≈ t
170+
@test isunitary(Q)
171+
@test dim(L) == dim(t) == 0
172+
113173
L, Q = @constinferred lq_compact(t)
114174
@test L * Q ≈ t
115175
@test isisometric(Q; side = :right)
116176
@test dim(Q) == dim(L) == dim(t)
117177

178+
L, Q = @constinferred lq_compact(t, DefaultAlgorithm())
179+
@test L * Q ≈ t
180+
@test isisometric(Q; side = :right)
181+
@test dim(Q) == dim(L) == dim(t)
182+
118183
L, Q = @constinferred right_orth(t)
119184
@test L * Q ≈ t
120185
@test isisometric(Q; side = :right)
121186
@test dim(Q) == dim(L) == dim(t)
122187

188+
L, Q = @constinferred right_orth(t, DefaultAlgorithm())
189+
@test L * Q ≈ t
190+
@test isisometric(Q; side = :right)
191+
@test dim(Q) == dim(L) == dim(t)
192+
123193
Nᴴ = @constinferred lq_null(t)
124194
@test isunitary(Nᴴ)
125195
@test norm(t * Nᴴ') ≈ 0 atol = 100 * eps(norm(t))
196+
197+
Nᴴ = @constinferred lq_null(t, DefaultAlgorithm())
198+
@test isunitary(Nᴴ)
199+
@test norm(t * Nᴴ') ≈ 0 atol = 100 * eps(norm(t))
126200
end
127201
end
128202

@@ -141,6 +215,11 @@ for V in spacelist
141215
@test isisometric(w)
142216
@test isposdef(p)
143217

218+
w, p = @constinferred left_polar(t, DefaultAlgorithm())
219+
@test w * p ≈ t
220+
@test isisometric(w)
221+
@test isposdef(p)
222+
144223
w, p = @constinferred left_orth(t; alg = :polar)
145224
@test w * p ≈ t
146225
@test isisometric(w)
@@ -160,6 +239,11 @@ for V in spacelist
160239
@test isisometric(wᴴ; side = :right)
161240
@test isposdef(p)
162241

242+
p, wᴴ = @constinferred right_polar(t, DefaultAlgorithm())
243+
@test p * wᴴ ≈ t
244+
@test isisometric(wᴴ; side = :right)
245+
@test isposdef(p)
246+
163247
p, wᴴ = @constinferred right_orth(t; alg = :polar)
164248
@test p * wᴴ ≈ t
165249
@test isisometric(wᴴ; side = :right)
@@ -180,16 +264,31 @@ for V in spacelist
180264
@test isunitary(u)
181265
@test isunitary(vᴴ)
182266

267+
u, s, vᴴ = @constinferred svd_full(t, DefaultAlgorithm())
268+
@test u * s * vᴴ ≈ t
269+
@test isunitary(u)
270+
@test isunitary(vᴴ)
271+
183272
u, s, vᴴ = @constinferred svd_compact(t)
184273
@test u * s * vᴴ ≈ t
185274
@test isisometric(u)
186275
@test isposdef(s)
187276
@test isisometric(vᴴ; side = :right)
188277

278+
u, s, vᴴ = @constinferred svd_compact(t, DefaultAlgorithm())
279+
@test u * s * vᴴ ≈ t
280+
@test isisometric(u)
281+
@test isposdef(s)
282+
@test isisometric(vᴴ; side = :right)
283+
189284
s′ = @constinferred svd_vals(t)
190285
@test parent(s′) ≈ parent(diagview(s))
191286
@test s′ isa TensorKit.SectorVector
192287

288+
s′ = @constinferred svd_vals(t, DefaultAlgorithm())
289+
@test parent(s′) ≈ parent(diagview(s))
290+
@test s′ isa TensorKit.SectorVector
291+
193292
s2 = @constinferred DiagonalTensorMap(s′)
194293
@test s2 ≈ s
195294

@@ -228,9 +327,18 @@ for V in spacelist
228327
@test isunitary(U)
229328
@test isunitary(Vᴴ)
230329

330+
U, S, Vᴴ = @constinferred svd_full(t, DefaultAlgorithm())
331+
@test U * S * Vᴴ ≈ t
332+
@test isunitary(U)
333+
@test isunitary(Vᴴ)
334+
231335
U, S, Vᴴ = @constinferred svd_compact(t)
232336
@test U * S * Vᴴ ≈ t
233337
@test dim(U) == dim(S) == dim(Vᴴ) == dim(t) == 0
338+
339+
U, S, Vᴴ = @constinferred svd_compact(t, DefaultAlgorithm())
340+
@test U * S * Vᴴ ≈ t
341+
@test dim(U) == dim(S) == dim(Vᴴ) == dim(t) == 0
234342
end
235343
end
236344

@@ -251,6 +359,12 @@ for V in spacelist
251359
@test isisometric(U)
252360
@test isisometric(Vᴴ; side = :right)
253361

362+
U, S, Vᴴ, ϵ = @constinferred svd_trunc(t, DefaultAlgorithm(; trunc = notrunc()))
363+
@test U * S * Vᴴ ≈ t
364+
@test ϵ ≈ 0
365+
@test isisometric(U)
366+
@test isisometric(Vᴴ; side = :right)
367+
254368
# dimension of S is a float for IsingBimodule
255369
nvals = round(Int, dim(domain(S)) / 2)
256370
trunc = truncrank(nvals)
@@ -314,10 +428,17 @@ for V in spacelist
314428
d, v = @constinferred eig_full(t)
315429
@test t * v ≈ v * d
316430

431+
d, v = @constinferred eig_full(t, DefaultAlgorithm())
432+
@test t * v ≈ v * d
433+
317434
d′ = @constinferred eig_vals(t)
318435
@test parent(d′) ≈ parent(diagview(d))
319436
@test d′ isa TensorKit.SectorVector
320437

438+
d′ = @constinferred eig_vals(t, DefaultAlgorithm())
439+
@test parent(d′) ≈ parent(diagview(d))
440+
@test d′ isa TensorKit.SectorVector
441+
321442
d2 = @constinferred DiagonalTensorMap(d′)
322443
@test d2 ≈ d
323444

@@ -330,12 +451,21 @@ for V in spacelist
330451
@test t * v ≈ v * d
331452
#test_dim_isapprox(domain(d), nvals)
332453

454+
d, v = @constinferred eig_trunc(t, DefaultAlgorithm(; trunc = truncrank(nvals)))
455+
@test t * v ≈ v * d
456+
#test_dim_isapprox(domain(d), nvals)
457+
333458
t2 = @constinferred project_hermitian(t)
334459
D, V = eigen(t2)
335460
@test isisometric(V)
336461
D̃, Ṽ = @constinferred eigh_full(t2)
337462
@test D ≈ D̃
338463
@test V ≈ Ṽ
464+
465+
D̃, Ṽ = @constinferred eigh_full(t2, DefaultAlgorithm())
466+
@test D ≈ D̃
467+
@test V ≈ Ṽ
468+
339469
λ = minimum(real, parent(diagview(D)))
340470
@test cond(Ṽ) ≈ one(real(T))
341471
@test isposdef(t2) == isposdef(λ)
@@ -350,6 +480,10 @@ for V in spacelist
350480
@test parent(d′) ≈ parent(diagview(d))
351481
@test d′ isa TensorKit.SectorVector
352482

483+
d′ = @constinferred eigh_vals(t2, DefaultAlgorithm())
484+
@test parent(d′) ≈ parent(diagview(d))
485+
@test d′ isa TensorKit.SectorVector
486+
353487
λ = minimum(real, parent(diagview(d)))
354488
@test cond(v) ≈ one(real(T))
355489
@test isposdef(t2) == isposdef(λ)
@@ -359,6 +493,10 @@ for V in spacelist
359493
d, v = @constinferred eigh_trunc(t2; trunc = truncrank(nvals))
360494
@test t2 * v ≈ v * d
361495
#test_dim_isapprox(domain(d), nvals)
496+
497+
d, v = @constinferred eigh_trunc(t2, DefaultAlgorithm(; trunc = truncrank(nvals)))
498+
@test t2 * v ≈ v * d
499+
#test_dim_isapprox(domain(d), nvals)
362500
end
363501
end
364502

@@ -421,6 +559,11 @@ for V in spacelist
421559
th′ = @constinferred project_hermitian(t)
422560
@test ishermitian(th′)
423561
@test th′ ≈ th
562+
563+
th′ = @constinferred project_hermitian(t, DefaultAlgorithm())
564+
@test ishermitian(th′)
565+
@test th′ ≈ th
566+
424567
@test t == tc
425568
th_approx = th + noisefactor * ta
426569
@test !ishermitian(th_approx) || (T <: Real && t isa DiagonalTensorMap)
@@ -429,6 +572,11 @@ for V in spacelist
429572
ta′ = project_antihermitian(t)
430573
@test isantihermitian(ta′)
431574
@test ta′ ≈ ta
575+
576+
ta′ = @constinferred project_antihermitian(t, DefaultAlgorithm())
577+
@test isantihermitian(ta′)
578+
@test ta′ ≈ ta
579+
432580
@test t == tc
433581
ta_approx = ta + noisefactor * th
434582
@test !isantihermitian(ta_approx)
@@ -446,6 +594,10 @@ for V in spacelist
446594
)
447595
t2 = project_isometric(t)
448596
@test isisometric(t2)
597+
t2′ = @constinferred project_isometric(t, DefaultAlgorithm())
598+
@test isisometric(t2′)
599+
@test t2′ * ((t2′)' * t) ≈ t
600+
449601
t3 = project_isometric(t2)
450602
@test t3 ≈ t2 # stability of the projection
451603
@test t2 * (t2' * t) ≈ t

0 commit comments

Comments
 (0)