-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsvd.jl
More file actions
453 lines (403 loc) · 15.1 KB
/
svd.jl
File metadata and controls
453 lines (403 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# Input
# ------
copy_input(::typeof(svd_full), A::AbstractMatrix) = copy!(similar(A, float(eltype(A))), A)
copy_input(::typeof(svd_compact), A) = copy_input(svd_full, A)
copy_input(::typeof(svd_vals), A) = copy_input(svd_full, A)
copy_input(::typeof(svd_trunc), A) = copy_input(svd_compact, A)
copy_input(::typeof(svd_full), A::Diagonal) = copy(A)
# TODO: many of these checks are happening again in the LAPACK routines
function check_input(::typeof(svd_full!), A::AbstractMatrix, USVᴴ, ::AbstractAlgorithm)
m, n = size(A)
U, S, Vᴴ = USVᴴ
@assert U isa AbstractMatrix && S isa AbstractMatrix && Vᴴ isa AbstractMatrix
@check_size(U, (m, m))
@check_scalar(U, A)
@check_size(S, (m, n))
@check_scalar(S, A, real)
@check_size(Vᴴ, (n, n))
@check_scalar(Vᴴ, A)
return nothing
end
function check_input(::typeof(svd_compact!), A::AbstractMatrix, USVᴴ, ::AbstractAlgorithm)
m, n = size(A)
minmn = min(m, n)
U, S, Vᴴ = USVᴴ
@assert U isa AbstractMatrix && S isa Diagonal && Vᴴ isa AbstractMatrix
@check_size(U, (m, minmn))
@check_scalar(U, A)
@check_size(S, (minmn, minmn))
@check_scalar(S, A, real)
@check_size(Vᴴ, (minmn, n))
@check_scalar(Vᴴ, A)
return nothing
end
function check_input(::typeof(svd_vals!), A::AbstractMatrix, S, ::AbstractAlgorithm)
m, n = size(A)
minmn = min(m, n)
@assert S isa AbstractVector
@check_size(S, (minmn,))
@check_scalar(S, A, real)
return nothing
end
function check_input(::typeof(svd_full!), A::AbstractMatrix, USVᴴ, ::DiagonalAlgorithm)
m, n = size(A)
@assert m == n && isdiag(A)
U, S, Vᴴ = USVᴴ
@assert U isa AbstractMatrix && S isa Diagonal && Vᴴ isa AbstractMatrix
@check_size(U, (m, m))
@check_scalar(U, A)
@check_size(S, (m, n))
@check_scalar(S, A, real)
@check_size(Vᴴ, (n, n))
@check_scalar(Vᴴ, A)
return nothing
end
function check_input(
::typeof(svd_compact!), A::AbstractMatrix, USVᴴ, alg::DiagonalAlgorithm
)
return check_input(svd_full!, A, USVᴴ, alg)
end
function check_input(::typeof(svd_vals!), A::AbstractMatrix, S, ::DiagonalAlgorithm)
m, n = size(A)
@assert m == n && isdiag(A)
@assert S isa AbstractVector
@check_size(S, (m,))
@check_scalar(S, A, real)
return nothing
end
# Outputs
# -------
function initialize_output(::typeof(svd_full!), A::AbstractMatrix, ::AbstractAlgorithm)
m, n = size(A)
U = similar(A, (m, m))
S = similar(A, real(eltype(A)), (m, n)) # TODO: Rectangular diagonal type?
Vᴴ = similar(A, (n, n))
return (U, S, Vᴴ)
end
function initialize_output(::typeof(svd_compact!), A::AbstractMatrix, ::AbstractAlgorithm)
m, n = size(A)
minmn = min(m, n)
U = similar(A, (m, minmn))
S = Diagonal(similar(A, real(eltype(A)), (minmn,)))
Vᴴ = similar(A, (minmn, n))
return (U, S, Vᴴ)
end
function initialize_output(::typeof(svd_vals!), A::AbstractMatrix, ::AbstractAlgorithm)
return similar(A, real(eltype(A)), (min(size(A)...),))
end
function initialize_output(::typeof(svd_trunc!), A, alg::TruncatedAlgorithm)
return initialize_output(svd_compact!, A, alg.alg)
end
function initialize_output(::typeof(svd_full!), A::Diagonal, ::DiagonalAlgorithm)
TA = eltype(A)
TUV = Base.promote_op(sign_safe, TA)
return similar(A, TUV, size(A)), similar(A, real(TA)), similar(A, TUV, size(A))
end
function initialize_output(::typeof(svd_compact!), A::Diagonal, alg::DiagonalAlgorithm)
return initialize_output(svd_full!, A, alg)
end
function initialize_output(::typeof(svd_vals!), A::Diagonal, ::DiagonalAlgorithm)
return eltype(A) <: Real ? diagview(A) : similar(A, real(eltype(A)), size(A, 1))
end
# Implementation
# --------------
function svd_full!(A::AbstractMatrix, USVᴴ, alg::LAPACK_SVDAlgorithm)
check_input(svd_full!, A, USVᴴ, alg)
U, S, Vᴴ = USVᴴ
fill!(S, zero(eltype(S)))
m, n = size(A)
minmn = min(m, n)
if minmn == 0
one!(U)
zero!(S)
one!(Vᴴ)
return USVᴴ
end
do_gauge_fix = get(alg.kwargs, :fixgauge, default_fixgauge())::Bool
alg_kwargs = Base.structdiff(alg.kwargs, NamedTuple{(:fixgauge,)})
if alg isa LAPACK_QRIteration
isempty(alg_kwargs) ||
throw(ArgumentError("invalid keyword arguments for LAPACK_QRIteration"))
YALAPACK.gesvd!(A, view(S, 1:minmn, 1), U, Vᴴ)
elseif alg isa LAPACK_DivideAndConquer
isempty(alg_kwargs) ||
throw(ArgumentError("invalid keyword arguments for LAPACK_DivideAndConquer"))
YALAPACK.gesdd!(A, view(S, 1:minmn, 1), U, Vᴴ)
elseif alg isa LAPACK_Bisection
throw(ArgumentError("LAPACK_Bisection is not supported for full SVD"))
elseif alg isa LAPACK_Jacobi
throw(ArgumentError("LAPACK_Jacobi is not supported for full SVD"))
else
throw(ArgumentError("Unsupported SVD algorithm"))
end
for i in 2:minmn
S[i, i] = S[i, 1]
S[i, 1] = zero(eltype(S))
end
do_gauge_fix && gaugefix!(svd_full!, U, Vᴴ)
return USVᴴ
end
function svd_compact!(A::AbstractMatrix, USVᴴ, alg::LAPACK_SVDAlgorithm)
check_input(svd_compact!, A, USVᴴ, alg)
U, S, Vᴴ = USVᴴ
do_gauge_fix = get(alg.kwargs, :fixgauge, default_fixgauge())::Bool
alg_kwargs = Base.structdiff(alg.kwargs, NamedTuple{(:fixgauge,)})
if alg isa LAPACK_QRIteration
isempty(alg_kwargs) ||
throw(ArgumentError("invalid keyword arguments for LAPACK_QRIteration"))
YALAPACK.gesvd!(A, S.diag, U, Vᴴ)
elseif alg isa LAPACK_DivideAndConquer
isempty(alg_kwargs) ||
throw(ArgumentError("invalid keyword arguments for LAPACK_DivideAndConquer"))
YALAPACK.gesdd!(A, S.diag, U, Vᴴ)
elseif alg isa LAPACK_Bisection
YALAPACK.gesvdx!(A, S.diag, U, Vᴴ; alg_kwargs...)
elseif alg isa LAPACK_Jacobi
isempty(alg_kwargs) ||
throw(ArgumentError("invalid keyword arguments for LAPACK_Jacobi"))
YALAPACK.gesvj!(A, S.diag, U, Vᴴ)
else
throw(ArgumentError("Unsupported SVD algorithm"))
end
do_gauge_fix && gaugefix!(svd_compact!, U, Vᴴ)
return USVᴴ
end
function svd_vals!(A::AbstractMatrix, S, alg::LAPACK_SVDAlgorithm)
check_input(svd_vals!, A, S, alg)
U, Vᴴ = similar(A, (0, 0)), similar(A, (0, 0))
alg_kwargs = Base.structdiff(alg.kwargs, NamedTuple{(:fixgauge,)})
if alg isa LAPACK_QRIteration
isempty(alg_kwargs) ||
throw(ArgumentError("invalid keyword arguments for LAPACK_QRIteration"))
YALAPACK.gesvd!(A, S, U, Vᴴ)
elseif alg isa LAPACK_DivideAndConquer
isempty(alg_kwargs) ||
throw(ArgumentError("invalid keyword arguments for LAPACK_DivideAndConquer"))
YALAPACK.gesdd!(A, S, U, Vᴴ)
elseif alg isa LAPACK_Bisection
YALAPACK.gesvdx!(A, S, U, Vᴴ; alg_kwargs...)
elseif alg isa LAPACK_Jacobi
isempty(alg_kwargs) ||
throw(ArgumentError("invalid keyword arguments for LAPACK_Jacobi"))
YALAPACK.gesvj!(A, S, U, Vᴴ)
else
throw(ArgumentError("Unsupported SVD algorithm"))
end
return S
end
# nothing case here to handle GenericLinearAlgebra
function svd_trunc!(A, USVᴴϵ::Tuple{TU, TS, TVᴴ, Tϵ}, alg::TruncatedAlgorithm) where {TU, TS, TVᴴ, Tϵ}
U, S, Vᴴ, ϵ = USVᴴϵ
U, S, Vᴴ = svd_compact!(A, (U, S, Vᴴ), alg.alg)
USVᴴtrunc, ind = truncate(svd_trunc!, (U, S, Vᴴ), alg.trunc)
if !isempty(ϵ)
ϵ[1] = truncation_error!(diagview(S), ind)
end
return USVᴴtrunc..., ϵ
end
function svd_trunc!(A, USVᴴϵ::Tuple{Nothing, Tϵ}, alg::TruncatedAlgorithm) where {Tϵ}
USVᴴ, ϵ = USVᴴϵ
U, S, Vᴴ = svd_compact!(A, USVᴴ, alg.alg)
USVᴴtrunc, ind = truncate(svd_trunc!, (U, S, Vᴴ), alg.trunc)
if !isempty(ϵ)
ϵ[1] = truncation_error!(diagview(S), ind)
end
return USVᴴtrunc..., ϵ
end
function svd_trunc!(A, USVᴴ::Tuple{TU, TS, TVᴴ}, alg::TruncatedAlgorithm; compute_error::Bool = true) where {TU, TS, TVᴴ}
ϵ = similar(S, compute_error)
(U, S, Vᴴ, ϵ) = svd_trunc!(A, (USVᴴ..., ϵ), alg)
return compute_error ? (U, S, Vᴴ, ϵ[1]) : (U, S, Vᴴ, -one(eltype(ϵ)))
end
function svd_trunc!(A, USVᴴ::Nothing, alg::TruncatedAlgorithm; compute_error::Bool = true)
Tr = real(eltype(A))
ϵ = compute_error ? zeros(Tr, 1) : zeros(Tr, 0)
U, S, Vᴴ, ϵ = svd_trunc!(A, (USVᴴ, ϵ), alg)
return compute_error ? (U, S, Vᴴ, ϵ[1]::Tr) : (U, S, Vᴴ, -one(Tr))
end
# Diagonal logic
# --------------
function svd_full!(A::AbstractMatrix, USVᴴ, alg::DiagonalAlgorithm)
check_input(svd_full!, A, USVᴴ, alg)
Ad = diagview(A)
U, S, Vᴴ = USVᴴ
if isempty(Ad)
one!(U)
one!(Vᴴ)
return USVᴴ
end
p = sortperm(Ad; by = abs, rev = true)
zero!(U)
zero!(Vᴴ)
n = size(A, 1)
pV = (1:n) .+ (p .- 1) .* n
Vᴴ[pV] .= sign_safe.(view(Ad, p))
Sd = diagview(S)
if Ad === Sd
@. Sd = abs(Ad)
permute!(Sd, p)
else
Sd .= abs.(view(Ad, p))
end
p .+= (0:(n - 1)) .* n
U[p] .= Ref(one(eltype(U)))
return U, S, Vᴴ
end
function svd_compact!(A, USVᴴ, alg::DiagonalAlgorithm)
return svd_full!(A, USVᴴ, alg)
end
function svd_vals!(A::AbstractMatrix, S, alg::DiagonalAlgorithm)
check_input(svd_vals!, A, S, alg)
Ad = diagview(A)
S .= abs.(Ad)
sort!(S; rev = true)
return S
end
# GPU logic
# ---------
# placed here to avoid code duplication since much of the logic is replicable across
# CUDA and AMDGPU
###
function check_input(
::typeof(svd_trunc!), A::AbstractMatrix, USVᴴ, alg::CUSOLVER_Randomized
)
m, n = size(A)
minmn = min(m, n)
U, S, Vᴴ = USVᴴ
@assert U isa AbstractMatrix && S isa Diagonal && Vᴴ isa AbstractMatrix
@check_size(U, (m, m))
@check_scalar(U, A)
@check_size(S, (minmn, minmn))
@check_scalar(S, A, real)
@check_size(Vᴴ, (n, n))
@check_scalar(Vᴴ, A)
return nothing
end
function initialize_output(
::typeof(svd_trunc!), A::AbstractMatrix, alg::TruncatedAlgorithm{<:CUSOLVER_Randomized}
)
m, n = size(A)
minmn = min(m, n)
U = similar(A, (m, m))
S = Diagonal(similar(A, real(eltype(A)), (minmn,)))
Vᴴ = similar(A, (n, n))
return (U, S, Vᴴ)
end
function _gpu_gesvd!(
A::AbstractMatrix, S::AbstractVector, U::AbstractMatrix, Vᴴ::AbstractMatrix
)
throw(MethodError(_gpu_gesvd!, (A, S, U, Vᴴ)))
end
function _gpu_Xgesvdp!(
A::AbstractMatrix, S::AbstractVector, U::AbstractMatrix, Vᴴ::AbstractMatrix; kwargs...
)
throw(MethodError(_gpu_Xgesvdp!, (A, S, U, Vᴴ)))
end
function _gpu_Xgesvdr!(
A::AbstractMatrix, S::AbstractVector, U::AbstractMatrix, Vᴴ::AbstractMatrix; kwargs...
)
throw(MethodError(_gpu_Xgesvdr!, (A, S, U, Vᴴ)))
end
function _gpu_gesvdj!(
A::AbstractMatrix, S::AbstractVector, U::AbstractMatrix, Vᴴ::AbstractMatrix; kwargs...
)
throw(MethodError(_gpu_gesvdj!, (A, S, U, Vᴴ)))
end
function _gpu_gesvd_maybe_transpose!(A::AbstractMatrix, S::AbstractVector, U::AbstractMatrix, Vᴴ::AbstractMatrix)
m, n = size(A)
m ≥ n && return _gpu_gesvd!(A, S, U, Vᴴ)
# both CUSOLVER and ROCSOLVER require m ≥ n for gesvd (QR_Iteration)
# if this condition is not met, do the SVD via adjoint
minmn = min(m, n)
Aᴴ = min(m, n) > 0 ? adjoint!(similar(A'), A)::AbstractMatrix : similar(A')
Uᴴ = similar(U')
V = similar(Vᴴ')
if size(U) == (m, m)
_gpu_gesvd!(Aᴴ, view(S, 1:minmn, 1), V, Uᴴ)
else
_gpu_gesvd!(Aᴴ, S, V, Uᴴ)
end
length(U) > 0 && adjoint!(U, Uᴴ)
length(Vᴴ) > 0 && adjoint!(Vᴴ, V)
return U, S, Vᴴ
end
# GPU SVD implementation
function svd_full!(A::AbstractMatrix, USVᴴ, alg::GPU_SVDAlgorithm)
check_input(svd_full!, A, USVᴴ, alg)
U, S, Vᴴ = USVᴴ
fill!(S, zero(eltype(S)))
m, n = size(A)
minmn = min(m, n)
if minmn == 0
one!(U)
zero!(S)
one!(Vᴴ)
return USVᴴ
end
do_gauge_fix = get(alg.kwargs, :fixgauge, default_fixgauge())::Bool
alg_kwargs = Base.structdiff(alg.kwargs, NamedTuple{(:fixgauge,)})
if alg isa GPU_QRIteration
isempty(alg_kwargs) || @warn "invalid keyword arguments for GPU_QRIteration"
_gpu_gesvd_maybe_transpose!(A, view(S, 1:minmn, 1), U, Vᴴ)
elseif alg isa GPU_SVDPolar
_gpu_Xgesvdp!(A, view(S, 1:minmn, 1), U, Vᴴ; alg_kwargs...)
elseif alg isa GPU_Jacobi
_gpu_gesvdj!(A, view(S, 1:minmn, 1), U, Vᴴ; alg_kwargs...)
else
throw(ArgumentError("Unsupported SVD algorithm"))
end
diagview(S) .= view(S, 1:minmn, 1)
view(S, 2:minmn, 1) .= zero(eltype(S))
do_gauge_fix && gaugefix!(svd_full!, U, Vᴴ)
return USVᴴ
end
function svd_trunc!(A::AbstractMatrix, USVᴴϵ::Tuple{TU, TS, TVᴴ, Tϵ}, alg::TruncatedAlgorithm{<:GPU_Randomized}) where {TU, TS, TVᴴ, Tϵ}
U, S, Vᴴ, ϵ = USVᴴϵ
check_input(svd_trunc!, A, (U, S, Vᴴ), alg.alg)
_gpu_Xgesvdr!(A, S.diag, U, Vᴴ; alg.alg.kwargs...)
# TODO: make sure that truncation is based on maxrank, otherwise this might be wrong
(Utr, Str, Vᴴtr), _ = truncate(svd_trunc!, (U, S, Vᴴ), alg.trunc)
if !isempty(ϵ)
# normal `truncation_error!` does not work here since `S` is not the full singular value spectrum
ϵ = sqrt(norm(A)^2 - norm(diagview(Str))^2) # is there a more accurate way to do this?
end
do_gauge_fix = get(alg.alg.kwargs, :fixgauge, default_fixgauge())::Bool
do_gauge_fix && gaugefix!(svd_trunc!, Utr, Vᴴtr)
return Utr, Str, Vᴴtr, ϵ
end
function svd_compact!(A::AbstractMatrix, USVᴴ, alg::GPU_SVDAlgorithm)
check_input(svd_compact!, A, USVᴴ, alg)
U, S, Vᴴ = USVᴴ
do_gauge_fix = get(alg.kwargs, :fixgauge, default_fixgauge())::Bool
alg_kwargs = Base.structdiff(alg.kwargs, NamedTuple{(:fixgauge,)})
if alg isa GPU_QRIteration
isempty(alg_kwargs) || @warn "invalid keyword arguments for GPU_QRIteration"
_gpu_gesvd_maybe_transpose!(A, S.diag, U, Vᴴ)
elseif alg isa GPU_SVDPolar
_gpu_Xgesvdp!(A, S.diag, U, Vᴴ; alg_kwargs...)
elseif alg isa GPU_Jacobi
_gpu_gesvdj!(A, S.diag, U, Vᴴ; alg_kwargs...)
else
throw(ArgumentError("Unsupported SVD algorithm"))
end
do_gauge_fix && gaugefix!(svd_compact!, U, Vᴴ)
return USVᴴ
end
_argmaxabs(x) = reduce(_largest, x; init = zero(eltype(x)))
_largest(x, y) = abs(x) < abs(y) ? y : x
function svd_vals!(A::AbstractMatrix, S, alg::GPU_SVDAlgorithm)
check_input(svd_vals!, A, S, alg)
U, Vᴴ = similar(A, (0, 0)), similar(A, (0, 0))
alg_kwargs = Base.structdiff(alg.kwargs, NamedTuple{(:fixgauge,)})
if alg isa GPU_QRIteration
isempty(alg_kwargs) || @warn "invalid keyword arguments for GPU_QRIteration"
_gpu_gesvd_maybe_transpose!(A, S, U, Vᴴ)
elseif alg isa GPU_SVDPolar
_gpu_Xgesvdp!(A, S, U, Vᴴ; alg_kwargs...)
elseif alg isa GPU_Jacobi
_gpu_gesvdj!(A, S, U, Vᴴ; alg_kwargs...)
else
throw(ArgumentError("Unsupported SVD algorithm"))
end
return S
end