|
| 1 | +# Randomized truncated SVD |
| 2 | +# ------------------------ |
| 3 | +# A randomized SVD (e.g. `CUSOLVER_Randomized`) only computes the largest `k` singular triplets of |
| 4 | +# a matrix. For a tensor, `k` is needed *per block* before the decomposition. We derive it from |
| 5 | +# the truncation strategy: each block is decomposed into a reduced space that is guaranteed to |
| 6 | +# contain everything the strategy keeps, after which the strategy is applied as usual. |
| 7 | + |
| 8 | +const RandomizedSVDAlgorithm = TruncatedAlgorithm{<:MAK.GPU_Randomized} |
| 9 | + |
| 10 | +_keeps_largest(strategy) = strategy.rev && (strategy.by === abs || strategy.by === identity) |
| 11 | + |
| 12 | +""" |
| 13 | + _svd_rank_bound(strategy, c, n) |
| 14 | +
|
| 15 | +Upper bound on the number of singular values that `strategy` keeps in the block of sector `c`, |
| 16 | +which has `n` singular values in total, or `nothing` if the strategy does not bound it (or does |
| 17 | +not keep the largest values). |
| 18 | +""" |
| 19 | +_svd_rank_bound(::TruncationStrategy, c, n) = nothing |
| 20 | +function _svd_rank_bound(strategy::TruncationByOrder, c, n) |
| 21 | + _keeps_largest(strategy) || return nothing |
| 22 | + # values in block `c` are weighted by `dim(c)` |
| 23 | + return min(n, floor(Int, strategy.howmany / dim(c))) |
| 24 | +end |
| 25 | +function _svd_rank_bound(strategy::TruncationSpace, c, n) |
| 26 | + _keeps_largest(strategy) || return nothing |
| 27 | + return min(n, dim(strategy.space, c)) |
| 28 | +end |
| 29 | +function _svd_rank_bound(strategy::TruncationIntersection, c, n) |
| 30 | + bounds = map(s -> _svd_rank_bound(s, c, n), strategy.components) |
| 31 | + all(isnothing, bounds) && return nothing |
| 32 | + return minimum(b for b in bounds if !isnothing(b)) |
| 33 | +end |
| 34 | +function _svd_rank_bound(strategy::TruncationUnion, c, n) |
| 35 | + bounds = map(s -> _svd_rank_bound(s, c, n), strategy.components) |
| 36 | + any(isnothing, bounds) && return nothing |
| 37 | + return maximum(bounds) |
| 38 | +end |
| 39 | + |
| 40 | +# use `k` values per sector, taken from the truncation strategy and, |
| 41 | +# if `k` is provided by the algorithm, capped by that |
| 42 | +function _randomized_svd_space(t::AbstractTensorMap, alg::RandomizedSVDAlgorithm) |
| 43 | + V = infimum(fuse(codomain(t)), fuse(domain(t))) |
| 44 | + kmax = get(alg.alg.kwargs, :k, nothing) |
| 45 | + inds = SectorDict(c => Base.OneTo(_randomized_svd_rank(alg.trunc, kmax, c, dim(V, c))) for c in sectors(V)) |
| 46 | + return truncate_space(V, inds) |
| 47 | +end |
| 48 | +function _randomized_svd_rank(trunc, kmax, c, n) |
| 49 | + k = _svd_rank_bound(trunc, c, n) |
| 50 | + if isnothing(kmax) |
| 51 | + isnothing(k) && throw( |
| 52 | + ArgumentError( |
| 53 | + "randomized SVD needs the number of singular values to compute: use a truncation strategy that bounds the rank (`truncrank`, `truncspace`, or an intersection with one of these), or pass `k` to the algorithm" |
| 54 | + ) |
| 55 | + ) |
| 56 | + return k |
| 57 | + end |
| 58 | + return isnothing(k) ? min(kmax, n) : min(k, kmax) |
| 59 | +end |
| 60 | + |
| 61 | +function MAK.initialize_output( |
| 62 | + ::Union{typeof(svd_trunc!), typeof(svd_trunc_no_error!)}, t::AbstractTensorMap, |
| 63 | + alg::RandomizedSVDAlgorithm |
| 64 | + ) |
| 65 | + @timeit_debug GLOBAL_TIMER "alloc: initialize_output" begin |
| 66 | + Vk = _randomized_svd_space(t, alg) |
| 67 | + U = similar(t, codomain(t) ← Vk) |
| 68 | + S = similar_diagonal(t, real(scalartype(t)), Vk) |
| 69 | + Vᴴ = similar(t, Vk ← domain(t)) |
| 70 | + return U, S, Vᴴ |
| 71 | + end |
| 72 | +end |
| 73 | + |
| 74 | +function MAK.svd_trunc_no_error!(t::AbstractTensorMap, USVᴴ, alg::RandomizedSVDAlgorithm) |
| 75 | + U, S, Vᴴ = USVᴴ |
| 76 | + kwargs = Base.structdiff(alg.alg.kwargs, NamedTuple{(:k, :p, :fixgauge)}) |
| 77 | + fixgauge = get(alg.alg.kwargs, :fixgauge, MAK.default_fixgauge())::Bool |
| 78 | + @timeit_debug GLOBAL_TIMER "svd_trunc!" begin |
| 79 | + for (c, Sb) in blocks(S) |
| 80 | + tb, Ub, Vᴴb = block(t, c), block(U, c), block(Vᴴ, c) |
| 81 | + s = diagview(Sb) |
| 82 | + k = length(s) |
| 83 | + k == 0 && continue |
| 84 | + n = min(size(tb)...) |
| 85 | + p = get(alg.alg.kwargs, :p, n - k - 1) |
| 86 | + if p > 0 && k + p < n |
| 87 | + MAK._gpu_Xgesvdr!(tb, s, Ub, Vᴴb; k, p, kwargs...) |
| 88 | + else # the requested oversampling does not fit in this block |
| 89 | + Uf, Sf, Vᴴf = svd_compact!(tb, MAK.select_algorithm(svd_compact!, tb, nothing)) |
| 90 | + copyto!(Ub, view(Uf, :, 1:k)) |
| 91 | + copyto!(s, view(diagview(Sf), 1:k)) |
| 92 | + copyto!(Vᴴb, view(Vᴴf, 1:k, :)) |
| 93 | + end |
| 94 | + fixgauge && MAK.gaugefix!(svd_trunc!, Ub, Vᴴb) |
| 95 | + end |
| 96 | + end |
| 97 | + USVᴴtrunc, _ = MAK.truncate(svd_trunc!, (U, S, Vᴴ), alg.trunc) |
| 98 | + return USVᴴtrunc |
| 99 | +end |
| 100 | + |
| 101 | +function MAK.svd_trunc!(t::AbstractTensorMap, USVᴴ, alg::RandomizedSVDAlgorithm) |
| 102 | + normt = norm(t) |
| 103 | + Ũ, S̃, Ṽᴴ = svd_trunc_no_error!(t, USVᴴ, alg) |
| 104 | + normS = norm(S̃) |
| 105 | + ϵ = sqrt((normt + normS) * abs(normt - normS)) |
| 106 | + return Ũ, S̃, Ṽᴴ, ϵ |
| 107 | +end |
| 108 | + |
| 109 | +function MAK.initialize_output( |
| 110 | + ::typeof(svd_trunc!), t::AdjointTensorMap, alg::RandomizedSVDAlgorithm |
| 111 | + ) |
| 112 | + return reverse(adjoint.(MAK.initialize_output(svd_trunc!, adjoint(t), _adjoint(alg)))) |
| 113 | +end |
| 114 | +function MAK.svd_trunc!(t::AdjointTensorMap, F, alg::RandomizedSVDAlgorithm) |
| 115 | + U, S, Vᴴ, ϵ = svd_trunc!(adjoint(t), reverse(adjoint.(F)), _adjoint(alg)) |
| 116 | + return Vᴴ', S, U', ϵ |
| 117 | +end |
0 commit comments