Repository navigation
Add a new truncation strategy to protect degenerate clusters #312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d245da1
59ec161
fd198af
33ec0df
873be72
fe7bb6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -163,6 +163,40 @@ _ind_union(A::Base.OneTo, B::Base.OneTo) = Base.OneTo(max(length(A), length(B))) | |
| _ind_union(A::AbstractUnitRange, B::AbstractUnitRange) = union(A, B) | ||
| _ind_union(A, B) = union(A, B) | ||
|
|
||
| function findtruncated(values::AbstractVector, strategy::TruncationMultiplets) | ||
| keep = _ind_to_mask(values, findtruncated(values, strategy.trunc)) | ||
| perm = sortperm(values; by = abs, rev = true) | ||
| return findall(_keep_multiplets!(keep, values, perm, strategy)) | ||
| end | ||
| function findtruncated_svd(values::AbstractVector, strategy::TruncationMultiplets) | ||
| # values are already sorted by decreasing magnitude | ||
| keep = _ind_to_mask(values, findtruncated_svd(values, strategy.trunc)) | ||
| return findall(_keep_multiplets!(keep, values, eachindex(values), strategy)) | ||
| end | ||
|
|
||
| _ind_to_mask(values::AbstractVector, ::Colon) = trues(length(values)) | ||
| _ind_to_mask(values::AbstractVector, ind::AbstractVector{Bool}) = BitVector(ind) | ||
| function _ind_to_mask(values::AbstractVector, ind) | ||
| keep = falses(length(values)) | ||
| keep[ind] .= true | ||
| return keep | ||
| end | ||
|
|
||
| # walk the values in sorted order `perm`, and make sure each group of consecutive degenerate | ||
| # values is either kept or discarded as a whole cluster | ||
| function _keep_multiplets!(keep::AbstractVector{Bool}, values::AbstractVector, perm, strategy::TruncationMultiplets) | ||
| σs = abs.(collect(view(values, perm))) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't the broadcasted
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, I'm not sure? If
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh collect as in back to CPU. No than not. But I thought this PR was CPU only so far?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It sort of accidentally supports the GPU by doing this collect which is a bit of a hack |
||
| start = firstindex(σs) | ||
| for k in eachindex(σs) | ||
| if k == lastindex(σs) || σs[k] - σs[k + 1] > max(strategy.atol, strategy.rtol * σs[k]) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably not GPU friendly yet but I think that can be handled in a separate PR
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing I am wondering about for this functionality, is if there is ever a case where absolute tolerance makes sense. Like, suppose
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed I was a bit torn on this question as well, but for now decided to keep |
||
| group = view(perm, start:k) | ||
| allequal(view(keep, group)) || (keep[group] .= strategy.mode == :up) | ||
| start = k + 1 | ||
| end | ||
| end | ||
| return keep | ||
| end | ||
|
|
||
| # Truncation error | ||
| # ---------------- | ||
| truncation_error(values::AbstractVector, ind) = truncation_error!(copy(values), ind) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a GPU-CPU related issue found by 🤖 that I don't understand...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes indeed, this PR wasn't made with the GPU stuff in mind at all, I can handle that in a separate PR imo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I think
BitArrayjust doesn't work on GPU lol)