Skip to content

Port over project/unproject from KrylovKit - #66

Open
kshyatt wants to merge 5 commits into
mainfrom
ksh/projections
Open

kshyatt wants to merge 5 commits into
mainfrom
ksh/projections

Conversation

@kshyatt

@kshyatt kshyatt commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

This PR moves over some functionality related to OrthnormalBasis from KrylovKit.jl, including:

  • project! (now with just one !)
  • unproject!!
  • rank1update!
  • basistransform!

As well as the definition and helper functions for OrthonormalBasis itself. It also adds some fast-paths for dense arrays which should allow us to avoid using the kernel selector logic of Jutho/KrylovKit.jl#180. Finally, it adds some tests and makes sure to run the tests in multithreaded mode so that those paths do get exercised.

Not everything is moved in, including orthogonalize and reorthogonalize, since those involve all the Gram-Schmidt algorithms and if we want them here, I think that could be a follow-up? Unless others feel strongly they should come in at the same time.

I also didn't add a matrix-backed OrthonormalBasis which could be done as a follow up PR. Basically, I want to move things without modifying how they work too much to make this easier to review, then we could do follow-on updates if needed.

@kshyatt
kshyatt requested a review from Jutho December 1, 2026 15:09
@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.22047% with 35 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/abstractarray.jl 89.92% 13 Missing ⚠️
src/orthonormal.jl 59.09% 9 Missing ⚠️
src/VectorInterface.jl 57.14% 6 Missing ⚠️
src/fallbacks.jl 92.30% 5 Missing ⚠️
src/splitrange.jl 91.66% 2 Missing ⚠️
Files with missing lines Coverage Δ
src/interface.jl 100.00% <ø> (ø)
src/splitrange.jl 91.66% <91.66%> (ø)
src/fallbacks.jl 88.26% <92.30%> (+2.30%) ⬆️
src/VectorInterface.jl 65.38% <57.14%> (-9.62%) ⬇️
src/orthonormal.jl 59.09% <59.09%> (ø)
src/abstractarray.jl 92.22% <89.92%> (-4.65%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kshyatt
kshyatt force-pushed the ksh/projections branch 2 times, most recently from e5396cd to c9b96ed Compare December 1, 2026 15:58
Comment thread src/orthonormal.jl Resolved
Comment thread src/VectorInterface.jl Resolved
Comment thread src/splitrange.jl Resolved
Comment thread src/interface.jl Resolved
Comment thread src/fallbacks.jl Resolved
Comment thread src/abstractarray.jl
Comment on lines +138 to +143
d = Array(Q' * xv)
if β == 0
y .= α .* d
else
y .= β .* y .+ α .* d
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.

Any reason not to use

Suggested change
d = Array(Q' * xv)
if β == 0
y .= α .* d
else
y .= β .* y .+ α .* d
end
y = mul!(y, Q', xv, α, β)

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.

Only that I ported what already existed, indeed I like this change better

Comment thread src/abstractarray.jl
return β == 1 ? y : β == 0 ? zerovector!(y) : scale!(y, β)
end
Q = _basis_matrix(b, r, yv)
xd = copyto!(similar(yv, length(r)), collect(x))

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.

Why can we not use vec(x) 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.

I guess I see. BLASVector includes StridedVector, and non-contiguous arrays would not be supported in level 2 BLAS methods. Can we maybe use another type alias for these methods, and restrict them so that we can actually leverage BLAS level 2, e.g.:

const BLAS1Vector{T <: BlasFloat} = Union{DenseArray{T}, StridedVector{T}}
const BLAS2Vector{T <: BlasFLoat} = DenseArray{T}

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.

Though that comment pertains more to the type of y. The x in unproject should really be a vector, not only after reshape/vec, and we should be able to use x directly, no xd or vec(x) necessary. But the type of x is harder to fix. It is not necessarily DenseVector{T}, as it could also be a contiguous view. So StridedVector{T} after all, but with guaranteed unit stride. Not sure how to express that.

Comment thread src/abstractarray.jl
mul!(yv, Q, xd, α, β)
return y
end
function unproject!!(y::AbstractArray, b, x::AbstractArray, α::Number = true, β::Number = false, r = Base.OneTo(length(b)))

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.

Suggested change
function unproject!!(y::AbstractArray, b, x::AbstractArray, α::Number = true, β::Number = false, r = Base.OneTo(length(b)))
function unproject!!(y::AbstractArray, b, x::AbstractVector, α::Number = true, β::Number = false, r = Base.OneTo(length(b)))

Comment thread src/abstractarray.jl
function unproject!!(y::AbstractArray, b, x::AbstractArray, α::Number = true, β::Number = false, r = Base.OneTo(length(b)))
length(x) == length(r) || throw(DimensionMismatch())
if get_num_threads() > 1
return unproject_linear_multithreaded!(y, b, x, α, β, r)

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.

Wasn't there some check for only calling this if the type of y exhibits efficient linear indexing in the original KrylovKit version?

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.

Actually, I think the check in KrylovKit now effectively boils down to y::Array{T}, which is even more restricted the case that would now go to the method above and immediately call BLAS level 2. The reason that this was not yet in KrylovKit, is that it was initially less restrictive, only checking whether y had efficient linear indexing using IndexStyle, but this then conflicted with GPUArrays.

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.

Conclusion, I think our own multithreaded linear-indexstyle implementations can go, which will simplify this greatly, and can probably remove the whole threading business (get_num_threads, enable_threading, ....) from VectorInterface and KrylovKit.

Comment thread src/abstractarray.jl
end
end
function rank1update!(
b::OrthonormalBasis{<:BLASVector{T}}, y::BLASVector{T}, x::BLASVector{T},

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.

Similar story, y::BLAS2Vector{T}, x::StridedVector{T} with guaranteed/checked unit stride on x.

Comment thread src/fallbacks.jl
#---------
function project!(y, b, x, α::Number = true, β::Number = false, r = Base.OneTo(length(b)))
length(y) == length(r) || throw(DimensionMismatch())
if get_num_threads() > 1

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.

So project! and basistransform! below are the only fallbacks where we could still use threading. The question is whether it's worth it. I don't know if this functionality was ever really used in KrylovKit.

Comment thread src/fallbacks.jl
end

# use an underscored fallback so that the single-threaded unproject!! in src/abstractarray.jl
# can use this as well.

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.

If we remove the linear_multithreaded stuff, this would no longer be necessary.

Comment thread src/fallbacks.jl
Comment on lines +251 to +258
if iszero(β)
y = scale!!(y, false) # should be hard zero
elseif !isone(β)
y = scale!!(y, β)
end
@inbounds for (i, ri) in enumerate(r)
y = add!!(y, b[ri], α * x[i])
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.

Suggested change
if iszero(β)
y = scale!!(y, false) # should be hard zero
elseif !isone(β)
y = scale!!(y, β)
end
@inbounds for (i, ri) in enumerate(r)
y = add!!(y, b[ri], α * x[i])
end
@inbounds for (i, ri) in enumerate(r)
y = i == 1 ? add!!(y, b[ri], α * x[i], β) : add!!(y, b[ri], α * x[i])
end

Comment thread src/fallbacks.jl
y = add!!(y, b[i], U[i, j])
end
return y
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.

Should we rewrite this as unproject!!(y, b, view(U, :, j)). Maybe we should even simply write

function basistransform!(b, U) # U should be unitary or isometric
    m, n = size(U)
    m == length(b) || throw(DimensionMismatch())
    b2 = [b * view(U, :, j) for j in 1:n)
    for j in 1:n
        b[j] = scale!!(b[j], b2[j], true)
    end
    return b
end

Comment thread src/splitrange.jl
various kernels. Can be iterated over to generate sub-ranges to hand to each
thread to work on. See also [`splitrange`](@ref).
"""
struct SplitRange

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.

All of this can also go if we don't do multithreading at this level. Let's ask Lander and Lukas.

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