Skip to content

Support and tests for randomized SVD - #614

Open
kshyatt wants to merge 1 commit into
mainfrom
ksh/rand_svd
Open

kshyatt wants to merge 1 commit into
mainfrom
ksh/rand_svd

Conversation

@kshyatt

@kshyatt kshyatt commented Nov 6, 2026

Copy link
Copy Markdown
Member

Not sure about keeping the CUSOLVER_Randomized name, we might want to patch something in MAK for that...

@kshyatt
kshyatt requested a review from lkdvos November 6, 2026 10:22
@codecov

codecov Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.02985% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/factorizations/randomized.jl 94.02% 4 Missing ⚠️
Files with missing lines Coverage Δ
src/factorizations/factorizations.jl 84.61% <ø> (ø)
src/factorizations/randomized.jl 94.02% <94.02%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kshyatt
kshyatt marked this pull request as ready for review November 6, 2026 18:06
Comment on lines +19 to +38
_svd_rank_bound(::TruncationStrategy, c, n) = nothing
function _svd_rank_bound(strategy::TruncationByOrder, c, n)
_keeps_largest(strategy) || return nothing
# values in block `c` are weighted by `dim(c)`
return min(n, floor(Int, strategy.howmany / dim(c)))
end
function _svd_rank_bound(strategy::TruncationSpace, c, n)
_keeps_largest(strategy) || return nothing
return min(n, dim(strategy.space, c))
end
function _svd_rank_bound(strategy::TruncationIntersection, c, n)
bounds = map(s -> _svd_rank_bound(s, c, n), strategy.components)
all(isnothing, bounds) && return nothing
return minimum(b for b in bounds if !isnothing(b))
end
function _svd_rank_bound(strategy::TruncationUnion, c, n)
bounds = map(s -> _svd_rank_bound(s, c, n), strategy.components)
any(isnothing, bounds) && return nothing
return maximum(bounds)
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we use n as a maximum value for those truncation strategies that do provide an explicit bound, is there a problem also using n if there is no explicit bound?

Comment on lines +114 to +117
function MAK.svd_trunc!(t::AdjointTensorMap, F, alg::RandomizedSVDAlgorithm)
U, S, Vᴴ, ϵ = svd_trunc!(adjoint(t), reverse(adjoint.(F)), _adjoint(alg))
return Vᴴ', S, U', ϵ
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this definition specific to RandomizedSVDAlgorithm? It seems like this should work for any alg. Also, does svd_trunc_no_error! have or need a similar definition, for users that call that method directly?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general method seems in place (as expected), so why do they not capture this? Is this an ambiguity issue again? But why?

(No general method for svd_trunc_no_error! is in place, don't know if it is useful to add this.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it's an ambiguity error (also for initialize_output). Sorry for the horribly long dump here:

  MethodError: svd_trunc!(::TensorKit.AdjointTensorMap{Float64, TensorKit.GradedSpace{TensorKitSectors.ProductSector{Tuple{TensorKitSectors.FermionParity, TensorKitSectors.SU2Irrep, TensorKitSectors.U1Irrep}}, TensorKit.SortedVectorDict{TensorKitSectors.ProductSector{Tuple{TensorKitSectors.FermionParity, TensorKitSectors.SU2Irrep, TensorKitSectors.U1Irrep}}, Int64}}, 2, 2, TensorKit.TensorMap{Float64, TensorKit.GradedSpace{TensorKitSectors.ProductSector{Tuple{TensorKitSectors.FermionParity, TensorKitSectors.SU2Irrep, TensorKitSectors.U1Irrep}}, TensorKit.SortedVectorDict{TensorKitSectors.ProductSector{Tuple{TensorKitSectors.FermionParity, TensorKitSectors.SU2Irrep, TensorKitSectors.U1Irrep}}, Int64}}, 2, 2, CUDACore.CuArray{Float64, 1, CUDACore.DeviceMemory}}}, ::Tuple{TensorKit.AdjointTensorMap{Float64, TensorKit.GradedSpace{TensorKitSectors.ProductSector{Tuple{TensorKitSectors.FermionParity, TensorKitSectors.SU2Irrep, TensorKitSectors.U1Irrep}}, TensorKit.SortedVectorDict{TensorKitSectors.ProductSector{Tuple{TensorKitSectors.FermionParity, TensorKitSectors.SU2Irrep, TensorKitSectors.U1Irrep}}, Int64}}, 2, 1, TensorKit.TensorMap{Float64, TensorKit.GradedSpace{TensorKitSectors.ProductSector{Tuple{TensorKitSectors.FermionParity, TensorKitSectors.SU2Irrep, TensorKitSectors.U1Irrep}}, TensorKit.SortedVectorDict{TensorKitSectors.ProductSector{Tuple{TensorKitSectors.FermionParity, TensorKitSectors.SU2Irrep, TensorKitSectors.U1Irrep}}, Int64}}, 1, 2, CUDACore.CuArray{Float64, 1, CUDACore.DeviceMemory}}}, TensorKit.DiagonalTensorMap{Float64, TensorKit.GradedSpace{TensorKitSectors.ProductSector{Tuple{TensorKitSectors.FermionParity, TensorKitSectors.SU2Irrep, TensorKitSectors.U1Irrep}}, TensorKit.SortedVectorDict{TensorKitSectors.ProductSector{Tuple{TensorKitSectors.FermionParity, TensorKitSectors.SU2Irrep, TensorKitSectors.U1Irrep}}, Int64}}, CUDACore.CuArray{Float64, 1, CUDACore.DeviceMemory}}, TensorKit.AdjointTensorMap{Float64, TensorKit.GradedSpace{TensorKitSectors.ProductSector{Tuple{TensorKitSectors.FermionParity, TensorKitSectors.SU2Irrep, TensorKitSectors.U1Irrep}}, TensorKit.SortedVectorDict{TensorKitSectors.ProductSector{Tuple{TensorKitSectors.FermionParity, TensorKitSectors.SU2Irrep, TensorKitSectors.U1Irrep}}, Int64}}, 1, 2, TensorKit.TensorMap{Float64, TensorKit.GradedSpace{TensorKitSectors.ProductSector{Tuple{TensorKitSectors.FermionParity, TensorKitSectors.SU2Irrep, TensorKitSectors.U1Irrep}}, TensorKit.SortedVectorDict{TensorKitSectors.ProductSector{Tuple{TensorKitSectors.FermionParity, TensorKitSectors.SU2Irrep, TensorKitSectors.U1Irrep}}, Int64}}, 2, 1, CUDACore.CuArray{Float64, 1, CUDACore.DeviceMemory}}}}, ::MatrixAlgebraKit.TruncatedAlgorithm{MatrixAlgebraKit.CUSOLVER_Randomized{@NamedTuple{}}, MatrixAlgebraKit.TruncationByOrder{typeof(abs)}}) is ambiguous.
  Candidates:
    svd_trunc!(t::TensorKit.AdjointTensorMap, F, alg::MatrixAlgebraKit.TruncatedAlgorithm)
      @ TensorKit.Factorizations ~/.julia/dev/TensorKit/src/factorizations/adjoint.jl:127
    svd_trunc!(t::TensorKit.AbstractTensorMap, USVᴴ, alg::MatrixAlgebraKit.TruncatedAlgorithm{<:MatrixAlgebraKit.CUSOLVER_Randomized})
      @ TensorKit.Factorizations ~/.julia/dev/TensorKit/src/factorizations/randomized.jl:101

I'll see if I can't get the one in adjoint.jl to play fair

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe then AdjointTensorMap should not be handled via a separate method definition, but via a simple branch inside the algorithms, i.e. svd_trunc!(t::AbstractTensorMap, USVᴴ, alg) should start with

if t isa AdjointTensorMap
    svd_trunc(t', reverse(map(adjoint, USVᴴ)), _adjoint(alg))
    return ...
end
...

I think we've come across such issues before, where we had to do the method specialization in stages.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants