[RFC] Block thick-restart Lanczos for `eigsh`/`svds`: structural fix for degenerate spectra + integer-factor GPU speedups
cc @anaruse (eigsh/svds and the thick restart), @kmaehashi (scope).
Summary
#10098 closed seven long-standing eigsh/svds correctness issues in
one go — #5000, #6446, #6769, #7168, #7495, #8009 and #8486, the oldest open
since March 2021 — by detecting Lanczos breakdown at the sweep boundary and
repairing it. That is a good outcome, and it is worth saying why it took one
patch to fix seven reports: they were never seven bugs. They were one
weakness, reported seven times over four years.
The weakness is structural. eigsh's Lanczos engine is a scalar recurrence:
one matvec, one reorthogonalization pass, one normalization per Krylov
vector — and a single starting vector cannot observe an eigenvalue of
multiplicity greater than one, so every mechanism built on top of it must
reconstruct that information after the fact. #10098's guard is the right
repair, and the repair is now load-bearing.
Profiling the same engine per phase (CUDA events, A100) while doing that
work turned up a second thing: on sparse operators the per-vector costs —
not the matvecs — dominate, and the hardware offers block operations at a
marginal cost the scalar algorithm cannot use. The two findings point the
same way. A block Lanczos engine (block size b ≈ min(k, 16)) would:
- make that entire seven-issue pathology class structurally unreachable for multiplicity ≤ b, rather than repaired after the fact;
- convert the two dominant measured costs (BLAS2 reorthogonalization, per-vector SpMV) into BLAS3/SpMM at ~1/b the traffic;
- divide the launch/latency overhead — which bounds the sweep below n ≈ 1e5 — by b per Krylov vector.
I'd like feedback on scope and API before building it; a prototype offer is at the end. One number up front: on pwtk the scalar solver's run time spans 0.6 s to 273 s depending on the start vector (456×); the block prototype spans 202–220 ms across 20 random start blocks (1.09×).
Neither cupy nor any other GPU array library has a block Krylov eigensolver today, so there is room to be first rather than to catch up; the downstream users who asked for one (#2359, still open since 2019) have since written a randomized solver of their own — see Landscape below.
Measured cost profile of the current sweep
Per-phase CUDA-event decomposition of _lanczos_fast (A100-SXM4-40GB,
float64, k=6, synthetic symmetric CSR; percentages of total sweep time):
| n | nnz/row | ncv | reorth GEMVs | SpMV | nrm2+dotc | rest |
|---|---|---|---|---|---|---|
| 1e6 | 11 | 64 | 62.8% | 23.0% | 6.6% | 7.6% |
| 1e5 | 11 | 64 | 42.3% | 19.1% | 19.0% | 19.5% |
| 1e5 | 51 | 64 | 24.9% | 43.2% | 19.0% | 12.8% |
| 1e5 | 201 | 64 | 12.9% | 70.6% | 9.6% | 6.8% |
Two further facts shape the argument:
- The sweep is host-launch-bound below n ≈ 1e5: fitting t(n) = c + b·n across sizes gives ~120 µs of fixed cost per Lanczos step (~11 dependent launches), i.e. ~65% of the step at n=1e5. (This is also why a fused three-term-update kernel alone is worth 1.05–1.22× end-to-end — separate follow-up PR.)
- The SpMV runs at ~355 GB/s at nnz/row=11 vs ~1230 GB/s for the GEMVs: gather-bound on the column indices, re-reading matrix + indices per vector.
Why block: the cost of a block-apply
Per CSR row at nnz/row = d (float64): matrix+index traffic ≈ 12d bytes, vector traffic ≈ 16 bytes. SpMM with b right-hand sides reads the matrix once for all b columns:
cost(SpMM_b) / cost(SpMV) ≈ (12d + 16b) / (12d + 16)Measured, with a correction the synthetic model missed. On five SuiteSparse matrices (ecology2, thermal2, 2cubes_sphere, pwtk, consph; d = 5…72, n = 83k…1.6M, float64, A100), γ(8) is 2.1–7.1 with row-major dense and 1.9–5.6 with column-major dense (the layout is worth 20–30% on cupy's current SpMM path — a block implementation should use F-order). That is weaker than the traffic model predicts, because well-ordered real matrices stream SpMV at ~900 GB/s — near peak — so the matrix-reuse advantage is smaller than on scattered patterns. On an H200 the effect strengthens: SpMV gets 2× faster with HBM3e and γ rises further. The per-vector SpMM gain on real matrices is therefore ~1.1–3.6×, pattern- and generation-dependent — real, but not the main event.
The main event is the reorthogonalization term (42–65% of the sweep): blocked CGS2 against a 128-vector basis measures 6–12× faster than per-vector GEMV pairs (b = 8–16, A100 and H200 agree), and the launch count per Krylov vector divides by b on top. Notably, the newer the silicon, the more the block case rests on the BLAS3 and launch terms and the less on SpMM matrix reuse — the opposite emphasis from the classic traffic argument.
The right accounting unit is time per polynomial degree, not matvecs
per eigenvalue. Block Lanczos is not economical per matvec — it converges
more slowly per polynomial degree — and the case for it on a GPU is precisely
that SpMM makes the extra matvecs nearly free; where it does not (dense rows,
FP64-weak parts) the b = 1 fallback applies. Convergence of an extreme eigenpair is governed by the
degree of the Krylov polynomial, and a block step advances the degree by
one for all b directions at once. Counted in matvecs, block therefore
looks b× more expensive; counted in wall-clock, a block step costs
γ(b) ≈ 1.9–5.6 SpMV-equivalents (and less on dense rows), so the degree
advances at a fraction of b× the scalar rate while carrying b coupled
directions — which is what buys multiplicity capture, deflation, and
block warm starts essentially for free. Two boundary conditions keep
this honest. First, the b directions stay distinct only because the
orthogonalization couples them every step (b independent scalar runs
from b random starts would all converge to the same dominant
eigenvector); the marginal cost of width is therefore exactly the BLAS3
reorthogonalization above — the starting block itself is free (cuRAND
fills b vectors at memory bandwidth, once; it has never registered in a
profile). Second, width buys structure — clusters, multiplicity,
deflation, warm subspaces — not degree: for a single well-isolated
eigenvalue on a cheap-SpMV operator, scalar remains matvec-optimal,
which is why b = 1 stays the automatic fallback rather than width being
treated as universally free.
Why block: correctness (the #10098 connection)
The sharpest instance arrived during #10098's own review (2026-08-30).
On a spectrum with two distinct eigenvalues of multiplicity 100, the
thick restart set V[:k] = (V.T @ s).T from a Ritz basis that is
degenerate by construction -- several Ritz vectors collapse onto one
physical direction -- and locked duplicate rows into the basis. The
Gram matrix showed an off-diagonal of exactly 1.000 immediately after
the first restart, after which reseeds projected against a
rank-deficient basis and the recurrence amplified ~1e50 per restart to
ghost eigenvalues of 1e307. It is repairable (and is repaired), but it
is the scalar method's defining weakness stated as plainly as it can
be: a single starting vector cannot observe an eigenvalue of
multiplicity m > 1, so every mechanism built on top of it -- restart,
deflation, locking -- has to reconstruct that information after the
fact. With b >= multiplicity the cluster is captured in one block step
and the failure mode does not exist.
The underlying fact is exact: an unreduced tridiagonal matrix has simple eigenvalues, so single-vector Lanczos cannot represent a multiplicity at all — when it appears to, it is round-off that produced the copy. A block recurrence produces a block-tridiagonal matrix, which can.
Every pathology #10098 guards against — lucky breakdown on degenerate spectra (#6446, #7495, #5000), clustered eigenvalues exhausting the Krylov space (#7168, #6769), rank-deficient operators (#8009, #8486) — is scalar Lanczos failing to observe eigenvalue multiplicity through a single vector. The deterministic-reseed machinery is the correct repair; a block method with b ≥ multiplicity never enters the failure state: a cluster of multiplicity m ≤ b is captured in one block step. Grimes–Lewis–Simon's block shift-invert Lanczos became the industrial standard for exactly this reason. The breakdown guard stays (for multiplicity > b and genuine exhaustion), but it becomes a rare path instead of a load-bearing one.
Why block: warm starts (a capability, not just a speedup)
A Krylov engine accepts exactly one starting vector — ARPACK's
reverse-communication interface is defined that way, so scipy's v0 is a
single vector and structurally can never be more. But callers who solve a
sequence of slowly varying eigenproblems (outer optimization loops,
self-consistent iterations, tracking the spectrum of a parameterized
operator) hold a full k-dimensional subspace from the previous solve.
Today the best they can do is collapse it to one vector
(v0 = Σ cᵢ xᵢ): in exact arithmetic the Krylov space then recovers the
span within ~k steps, but small components wash out in floating point and
clustered eigenvalues degrade it.
A block engine takes the previous Ritz block as its initial block V₀ —
the warm subspace enters intact, no collapse and re-extraction. And
because cupy's Lanczos is thick-restart rather than an ARPACK wrapper,
a cheaper variant exists even independent of the block engine: accept a
2-D v0 of shape (n, j), orthonormalize it, Rayleigh–Ritz project, and
enter the sweep exactly as if resuming from a restart with j retained
vectors — the code path already exists. Either form is a genuine
cupy-over-scipy capability, not parity work: ARPACK's interface cannot
offer it.
Measured (A100, k=8, the prototype extended to accept a V0, on a
sequence where the operator is perturbed by ~1e-4·‖A‖ between solves):
| matrix | step | cold | single-vector v0 |
block V0 |
|---|---|---|---|---|
| 2cubes_sphere | 1 | 38.4 ms / 256 mv | 78.8 ms | 18.5 ms / 120 mv |
| 2cubes_sphere | 2 | 47.7 ms / 256 mv | 76.8 ms | 17.1 ms / 120 mv |
| pwtk | 1 | 110.3 ms / 416 mv | 573.7 ms | 92.0 ms / 352 mv |
| pwtk | 2 | 111.6 ms / 416 mv | 265.0 ms | 75.0 ms / 288 mv |
The block warm start halves the matvec count on 2cubes_sphere and runs
1.2–2.8× faster than a cold solve. The result worth pausing on is the
middle column: collapsing the previous Ritz vectors into a single v0
is 2–5× SLOWER than simply starting cold. A sum of near-eigenvectors is
a poor Krylov seed precisely because it is close to invariant — it spans
few new directions per step — so the one form of warm start that ARPACK's
interface permits is actively harmful here, while the form it cannot
express is the one that pays.
Prototype evidence (block thick-restart Lanczos vs eigsh, real matrices)
A ~90-line block TRLan prototype (b=8, kk=24 locked Ritz vectors, ncv=128,
blocked CGS2, CholQR panel factorization, host Ritz solves only at stride-4
check points and restart edges, column-major Krylov basis) racing
eigsh to the same 1e-6 residual, k=8 LA, float64, A100; eigenvalues
agree to 1e-9 or better everywhere; identical seeded start across
variants:
| matrix | n | nnz/row | eigsh | block | speedup | block matvecs |
|---|---|---|---|---|---|---|
| pwtk | 218k | 53 | 53.1 s (321.6 s worst) | 0.110 s | ~480× | 416 |
| 2cubes_sphere | 101k | 16 | 98 ms | 37 ms | 2.6× | 256 |
| consph | 83k | 72 | 27 ms | 35 ms | 0.78× | 216 |
| thermal2 | 1.2M | 7 | 0.71 s | 0.87 s | 0.82× | 1024 |
| ecology2 | 1.0M | 5 | 29.3 s | 41.3 s | 0.71× | 61k |
(A wider convergence-check stride lifts the small/easy cases a bit more: consph 0.92×, 2cubes 2.96× at stride 16.)
Read honestly:
pwtk is the pathology this RFC targets: a clustered top spectrum (structural FEM). Scalar restarts thrash, and how badly is decided by the start vector: 7.4 s to 321.6 s across identical calls, and 0.47 s from the particular fixed
v0used for the table above — three orders of magnitude of start-vector luck on the same matrix. Fixing the seed picks one ticket in that lottery rather than leaving it, which is why the row below is quoted with the worst case beside it; see Start-vector variance for the same spread measured across seeded starts. The block method captures the cluster structurally: 416 matvecs, ~480×. (An earlier prototype revision reproduced ~308× on an L40S.)The basis layout is load-bearing — measured per-phase, on two architectures. An earlier prototype kept the basis row-major; a per-phase CUDA-event profile showed the reorthogonalization phase then costing 21.0 ms/step at n=1.2M — 88% of the step — versus 3.2 ms column-major (A100; H200 reproduces the same mechanism at its own scale, 7.0 → 1.4 ms/step, 81% → 46% of the step). The cause: GEMM against a non-contiguous column slice of the basis silently copies it (~4 basis-sized copies, n·dim·8 B each, per block step). That artifact alone made the n ≥ 1M rows lose 4–4.5×; the one-line layout fix recovers them to the table above. A production implementation must keep both the dense multiplicand and the Krylov basis column-major. (This corrects an earlier "Python-loop overhead" attribution: the profile shows the cost was device-side copy traffic, and per-step host overhead is minor.)
Cross-architecture, and the FP64 caveat that decides the heuristic. Same prototype, same seeds, each raced against
eigshmeasured on the same GPU:matrix A100 H200 L40S pwtk 482× 878× 309× 2cubes_sphere 2.96× 2.34× 0.41× consph 0.92× 0.85× 0.36× thermal2 0.84× 0.93× 0.29× ecology2 0.72× 1.02× 0.29× On the datacenter parts the block engine is at parity or better everywhere except the easy/dense cases, and H200 lifts both n ≥ 1M meshes past 1.0× — the newer the memory system, the better it looks. The L40S column is the important one for scope. Ada Lovelace runs FP64 at 1/64 rate, and the block method's advantage lives in FP64 BLAS3: the reorthogonalization phase costs 24.1 ms/step there versus 3.2 ms on A100 for the same work. So on FP64-weak GPUs the block engine loses on ordinary spectra even though the layout fix still helps it (C→F halves that phase: 54.7 → 24.1 ms/step). What survives everywhere is the pathology case — pwtk is still 309×, because that win comes from capturing a cluster structurally rather than from arithmetic throughput. Two design consequences, both already in the proposal: the automatic
b = 1fallback is not a nicety but a requirement, and the selection heuristic should read FP64 capability (and float32/complex64 inputs, which Ada runs at full rate) rather than density alone.What remains at n ≥ 1M is honest and algorithmic: the reorth GEMMs are now genuine traffic-bound BLAS3 (~2–3 ms/step against a 128-vector basis at n=1.2M), and ecology2's 61k matvecs on the flattest spectrum in the set reflect the b× matvec-work-per-Krylov-dimension cost stated under "Costs and risks" — these are the cases the automatic b=1 fallback exists for. consph stays the easy case (eigsh converges almost immediately).
CholQR mattered: cuSOLVER
geqrfon (n, 8) tall-skinny panels was the single largest prototype cost before it (2cubes went 0.6× → 2.2× on that change alone). A production implementation should panel-factorize via CholQR (safe after CGS2) rather thangeqrf.Two alternatives for the easy/dense regime were measured and set aside. (i) Basis-width tuning: an ncv sweep (32–128) shows the 128-vector basis is near-optimal — smaller ncv is monotonically worse wherever convergence is nontrivial (thermal2 0.82×→0.29× by ncv=48, ecology2 0.71×→0.30× by 64, pwtk 485×→388× at 64: restart churn and extra matvecs swamp the reorth savings), and buys only ~0.92× on consph at best (ncv 96, check stride 16). (ii) Monomial power-filter subspace iteration ((A−cI)^s, s=2–4, CholQR2, Rayleigh–Ritz against A each round — the simplest ChASE-flavored alternative): converges correctly, and its per-matvec cost is indeed ~3× below block TRLan's, but it needs ~12× the matvecs on consph even with a good shift (2640 vs 216; 0.20× vs 0.92× wall-clock; 2cubes at best reaches 0.97× parity where block gets 2.9×) — and the sharp lower spectral bound it wants is itself expensive (a capped GPU Lanczos estimate cost ~31× the entire eigsh solve; host ARPACK
which='SA'did not converge it in 28 minutes). The coupled-orthogonalization Krylov basis is what carries the information-per-matvec advantage, which is why the easy cases get the b=1 fallback rather than a different iteration family. (Proper Chebyshev filtering on estimated intervals — ChASE/EVSL, cited below — remains interesting for interior spectra and multi-GPU, not contested by this data.)
Start-vector variance, measured (pwtk, A100, same prototype)
The scalar method's run time on pwtk depends on the start vector because a
single vector's weight on the clustered top eigenspace is one random number.
The block method starts from b random vectors and keeps a b-dimensional
coupling to every locked Ritz vector, so the same dependence should
concentrate away. Measured on the same A100, k=8 LA, tol=1e-6:
| starts | min | median | max | spread | |
|---|---|---|---|---|---|
| block TRLan (b=8, kk=24, ncv=128) | 20 random start blocks | 202 ms | 206 ms | 220 ms | 1.09× |
scalar eigsh |
5 seeded v0 |
0.60 s | 78 s | 273 s | 456× |
Block matvec counts stayed within 408–448 and the eigenvalues agreed across all 20 starts to 4e-12.
For reference, ARPACK (implicitly restarted Arnoldi/Lanczos; Lehoucq,
Sorensen & Yang 1998) on the same problem and the same
five seeded starts. Its run time varies far less than cupy's thick restart
(1.0–7×), but its answer does: the top of pwtk's spectrum contains a
triple within 7e-5 relative (1.00589219e8, 1.00589283e8, 1.00589290e8), and
at its default ncv=20 every ARPACK run — Fortran arpack-ng in scipy
1.13.1 and the C rewrite in scipy 1.18 alike — reported convergence while
returning a set that omits the third member and includes the 10th-largest
eigenvalue instead (max|Δλ|/λ = 6.7e-3). At ncv=40 (cupy's default for
k=8) scipy ≤ 1.13 recovers the full set from all five starts, while
scipy ≥ 1.16 misses it from two of the five, identically on two machines:
since scipy#21110 the start vector is preprocessed as A @ v0 (following
arpack-ng#79), and with that start ARPACK's residual test is satisfied at
tol=1e-6 before the third member has emerged; tol ≤ 1e-8 or ncv ≥ 60
recovers it (scipy/scipy#26112, where the scipy side reproduced this and
prefers a documentation note to a change). The reference set is the one cupy's eigsh, the block
prototype from 20 starts, a block k=16 run and the Fortran ncv=40 runs
all agree on to 1e-11. Missing a cluster member is the classic single-vector
failure on clustered spectra; cupy's thick restart (post-#10098) returned
the full set from every start it was given, at the price of the 456×
run-time spread, and the block method returned it from every start in
~0.2 s. The block method does not remove start-vector
dependence in principle — a start block nearly orthogonal to the target
subspace is still possible, just exponentially less likely with b
independent draws — but on the matrix that motivated this section it turns
a three-orders-of-magnitude lottery into a 9% spread. (This prototype
revision runs pwtk in ~0.2 s; the 0.110 s in the table above was the
sync-lean revision. Both are the same algorithm.)
Landscape: what exists elsewhere, and who asked
- The original ask is #2359 (2019): a sparse SVD for single-cell PCA.
svds(#4155) and the in-treelobpcgmet the literal request; the follow-ups in that thread — a block method, mean-centring through aLinearOperator—
Source: cupy/cupy