Eigensolvers¶
eigh(A, tol=1e-10, *, method=None)¶
Symmetric eigendecomposition A = V Λ Vᵀ for real symmetric A. Returns (eigenvalues, eigenvectors) with eigenvalues in ascending order.
method=None reads TRNSOLVER_EIGH_METHOD (default auto).
auto: on the nativeneurondevice, n <TRNSOLVER_EIGH_NKI_MIN_N(default 512) usestorch.linalg.eigh; otherwise the NKI path (Householder tridiagonalization, host QR iteration). The NKI path is single-tile (n ≤ 128) and falls back totorch.linalg.eighabove that (it raises underTRNSOLVER_REQUIRE_NKI=1), so at the default threshold every size runs aten. Host tensors on a TorchNeuron host take the same cutover, i.e. host LAPACK (v0.20.0). Off-device,torch.linalg.eigh.jacobi(#48): batched Jacobi for(n, n)or(..., n, n)input. On the nativeneurondevice (FP32, n ≤ 128) it runs the SBUF-resident one-sided Jacobi kernel: one launch per sweep, each holding up to 128 tiles of 128 / n matrices (512 at n = 32) in SBUF. Elsewhere it runs a torch implementation (two-sided Jacobi as batched matmuls). Uses the lower triangle, liketorch.linalg.eigh. For 33 ≤ n ≤ 128 the kernel is block Jacobi: per step the Tensor Engine forms each matrix's Gram matrix and applies one orthogonal matmul per block pair, and the Vector Engine only rotates the 16 × 16 pair Gram blocks.- Multi-core (opt-in):
TRNSOLVER_EIGH_CORES=<k>|autosplits a Jacobi batch over logical NeuronCores, one host thread per core (#48). WithTRNSOLVER_EIGH_POOL=procas well, a host batch goes to persistent worker processes instead, one per core (TRNSOLVER_EIGH_POOL_CORES, elseNEURON_RT_VISIBLE_CORES), through shared memory (#29). The pool is the faster split (Trn2, n = 128, B = 4096, 8 host CPUs: 324 ms on 4 cores against 531 ms with threads and 655 ms for 8-thread batch-parallel LAPACK), but a NeuronCore belongs to one process: the calling process must not use those cores itself, so device-resident input keeps the thread split. Workers start on the first call (about 10 s, plus any kernel compile) and stay up; calltrnsolver.nki.eigh_pool.shutdown()to free the cores. Scripts that use the pool need anif __name__ == "__main__":guard (the workers are spawned).TRNSOLVER_JACOBI_SWEEPS_PER_LAUNCH=<k>runs the scalar kernel's (n ≤ 32) first sweeps k per launch: 5–14% faster on one core (Trn2). autoalso runs the Jacobi kernel for FP32 batches on the nativeneurondevice inside a measured region (n ≤ 32 with B ≥ 512, 33 ≤ n ≤ 64 with B ≥ 256, 65 ≤ n ≤ 128 with B ≥ 1024, plus B ≥ 256 for n ≥ 96 and B ≥ 64 at n = 128), where it beats the aten cutover.TRNSOLVER_EIGH_JACOBI_AUTOoverrides the region.
eigenvalues, eigenvectors = trnsolver.eigh(A)
w, V = trnsolver.eigh(A_batch.to("neuron"), method="jacobi") # (B, n), (B, n, n)
eigh_generalized(A, B)¶
Generalized symmetric eigenproblem A x = λ B x with B SPD — the form that arises in the SCF iteration as F C = S C ε.
Reduces to standard form via Cholesky of B:
B = L LᵀA' = L⁻¹ A L⁻ᵀeigh(A') → (Λ, V')- Back-transform
V = L⁻ᵀ V'
eigenvalues, eigenvectors = trnsolver.eigh_generalized(F, S)
The Cholesky fails (LinAlgError) when B is exactly singular and loses accuracy when it is nearly so, as the overlaps of large or diffuse basis sets are. For those, and to skip the per-call Cholesky in an SCF loop, pass an orthogonalizer:
eigh_generalized(A, B, tol=1e-10, *, X=None)¶
With X (n × m) from orthogonalizer, it computes A' = Xᴴ A X (two GEMMs), eigh(A') (m × m) and V = X V', and returns m eigenpairs; B is not used and may be None. X is cast to A's dtype and device, so an FP32 A on the device keeps the GEMMs there (trnblas.gemm for real 2-D input) while X itself is built once, in float64 on the host.
orth = trnsolver.orthogonalizer(S, "canonical", lindep=1e-7) # once
w, C = trnsolver.eigh_generalized(F, S, X=orth.X) # every iteration; orth.rank pairs
Orthogonalizers: a tolerance for near-singular S¶
orthogonalizer(S, method="symmetric", lindep=1e-7, *, dtype=None, device=None)¶
Returns Orthogonalizer(X, rank, eigenvalues, pivots), with Xᴴ S X = I_m on the numerically independent subspace of S, where m = rank ≤ n. S is real symmetric or complex Hermitian, (n, n) or a batch (..., n, n). The work is always float64 (complex128) on the host. dtype and device set only the returned X, which is float64 on the host by default.
method |
X | Kept when | Cost |
|---|---|---|---|
"symmetric" (default) |
Löwdin S⁻¹ᐟ² (n × n), when nothing is dropped; otherwise the canonical X |
— | one eigh |
"canonical" |
U_keep diag(s_keep⁻¹ᐟ²) (n × m) |
s_i > lindep · max(s) |
one eigh |
"cholesky" |
X[P, :] = L⁻ᴴ with S_PP = L Lᴴ, zero on the other rows (n × m) |
pivoted Cholesky continues while the largest remaining Schur-complement diagonal is > lindep · max(diag S) |
O(n m²), no eigensolve |
- Threshold.
lindepis relative. A kept problem has condition number at most1 / lindepfor the eigenvalue methods, soXamplifies rounding by at most1/√lindep. For a normalised AO basismax(s) ≥ 1, so the relative threshold is at least as strict as an absolute one of the same value (Psi4's default is 1e-7, PySCF's 1e-8, both absolute).lindep=0keeps every positive eigenvalue (or pivot). - Symmetric with drops. When eigenvalues are dropped, no n × n
XsatisfiesXᴴ S X = I, so"symmetric"returns the canonical X. This is PySCF's convention. With nothing dropped it isinv_sqrt_spd(S), so existing results do not change. - Cholesky threshold. The Cholesky test is on Schur pivots, which are never smaller than
λ_min(S), so at the samelindepit keeps more functions than"canonical", and the kept block is worse conditioned. For an FP32 SCF, use aboutlindep=1e-5with it (see below).pivotslists the kept basis functions in pivot order. The pivots come from LAPACK?pstrfwhen SciPy is installed, and from a torch loop otherwise. - Batches. For a batch,
rankis a LongTensor andXis(..., n, max rank). For a matrix with a lower rank, the trailing columns are zero (andpivotsis padded with −1). - Autograd. It is differentiable through
S."symmetric"without drops, and"cholesky"for a fixed pivot set, giveXitself. CanonicalXis defined only up to the sign (phase) of each column, so only gauge-invariant functions of it, such asX Xᴴ, have a well-defined gradient.
Measured (#72), DF-RHF, examples/scf_df_rhf.py, CPU. benzene/aug-cc-pVTZ (n = 414, smallest eigenvalue of S 3.4e-7). E − E_ref is against PySCF; E(C) is the float64 energy at the converged orbitals.
| run | rank | FP64 E − E_ref | FP32 |
|---|---|---|---|
symmetric / canonical, lindep 1e-7 |
411 | 1.4e-6 Eh (3 functions dropped) | converged, 23 iterations, E(C) − E_ref 4.7e-6 |
symmetric, lindep 1e-9 (no drop, plain S⁻¹ᐟ²) |
414 | −5.7e-8 | not converged in 80 iterations |
generalized (Cholesky of S) |
414 | −5.7e-8 | not converged in 80 iterations |
cholesky, lindep 1e-7 |
414 | −5.7e-8 | not converged in 80 iterations |
cholesky, lindep 1e-5 |
408 | — | converged, 14 iterations, E(C) − E_ref 8.7e-6 |
In float64 nothing has to be dropped at this size. In FP32 the dropped directions decide convergence.
C20H42/aug-cc-pVDZ (n = 838, smallest eigenvalue 4.1e-7) is milder. At lindep 1e-7 two functions are dropped, and every variant converges in FP32 (26–35 iterations). In float64 the two dropped functions raise the energy by 2.8e-5 Eh against the full basis (canonical −781.9162346 against −781.9162624 Eh for S⁻¹ᐟ², the Cholesky of S, and cholesky). The threshold trades that basis-set truncation against FP32 conditioning. Lower it, or use float64, when the truncation matters more.
Backend notes¶
Householder tridiagonalization is n − 2 dependent BLAS-2 steps and leaves the Tensor Engine idle (#48). A parallel-ordered Jacobi step rotates n/2 disjoint pairs at once, so a batch of matrices advances in lockstep with no long dependency chain. In the kernel the Tensor Engine applies the step's partner permutation (an exact matmul shared by every tile) and the Vector Engine applies the rotations. For measurements see #48; the fair CPU baseline splits the batch across the same threads, which is 1.6–5.8× faster than one batched torch.linalg.eigh call (#54, v0.19.0).
occupied_subspace(F, k, *, guess=None, buffer=None, passes=4, tol="auto", rtol=0.1, degree=None, upper="gershgorin", refresh=None)¶
Lowest k eigenpairs of a symmetric F for SCF, where F changes little between calls. Returns (eigenvalues (k,), eigenvectors (n, k), state); pass state as guess on the next call.
guess=Noneis a cold start: one full float64 hosteigh, returned exactly.- With a warm start, each pass runs a degree-8 scaled Chebyshev filter
Y = p(F) Xon a block ofkb = k + buffervectors (bufferdefaults to about 15%, at least 8), splits off the correctionE = Y - X XᵀY, and does a float64 Rayleigh-Ritz onspan[X, E]on the host. - The Rayleigh-Ritz keeps only the lowest
kbof the2 kbRitz pairs. When the unit-diagonal Gram of[X, E]is well conditioned (its smallest eigenvalue, bounded below by1 / ‖L⁻¹‖²_F, is above the drop threshold), one Cholesky reduces the pencil to a standard problem, which needs oneeighinstead of two eigendecompositions. Otherwise it takes the rank-revealing path, which drops dependent correction columns. - For FP32
Fon the nativeneurondevice, one pass is one NKI launch (trnsolver/nki/subspace.py, n·kb ≤ about 1024 × 512).Fstays resident in SBUF, the filter is Tensor Engine GEMMs, and only the six kb × kb Gram blocks reach the host. - Each launch after a call's first one starts by applying the previous pass's block update,
X ← [X, E] Q, on chip (subspace_step_kernel).Qarrives in the same host-to-device copy as the filter's scalars. - Only the last pass's update runs as device ops.
TRNSOLVER_SUBSPACE_FOLD=0turns the folding off. - Elsewhere the same arithmetic runs as PyTorch (
subspace.filter_reference,subspace.step_reference). - The number of passes adapts to how far
Fmoved: rtol: stop once the wanted residuals of the block entering a pass have dropped tortoltimes those the call started with. Early SCF iterations take up topassespasses, and late ones take one Rayleigh-Ritz.tol: stop once they are belowtolrelative to the spectral scale."auto"is 30 units ofF's rounding (3.6e-6 for FP32), the residual's noise floor. A converged SCF then gets the same orbitals back, so an FP32 energy test can see two identical energies in a row.refresh(default off): return a fulleighwhen the first residual is aboverefresh.upper: the spectral upper bound for the filter: Gershgorin (default),"cached", or a number.
The correction form matters in FP32. The filter grows core levels far below the HOMO much faster than the HOMO itself, and a Rayleigh-Ritz on YᵀY loses the HOMO. Degrees above about 12 lose accuracy even in the correction form, so the default is 8.
examples/scf_df_rhf.py --solver subspace uses it on XᵀFX.
Measured on Trn2 (one logical NeuronCore, host pinned to 2 CPUs, GOMP_SPINCOUNT=10000; #78, #85, #86):
- Per call, fixed passes, n = 250 / 490 / 970 (
scripts/bench_occupied_subspace.py,tol=None, rtol=None): - 2 passes: 5.6 / 9.3 / 34 ms, against 3.8 / 11.4 / 67 ms for aten
eighon the device tensor (7.8 / 12 / 40 ms before #86); - 1 pass: 3.6 / 5.4 / 18 ms.
- Where the time goes: the kernel takes 0.4–0.7 ms per pass. The host Rayleigh-Ritz takes 0.9 / 2.9 / 14 ms per pass, against 1.2 / 3.6 / 16 ms for the two-eigendecomposition path. At n = 970 it is one float64
eighof the 372-wide reduced matrix. SciPy's subset driver (syevr) was 3.5× slower than the full MKLeighon these threads, so it is not used. - Accuracy is unchanged: after 2 passes the projector error is 1.4e-5 / 2.5e-5 / 6.0e-5, eigenvalues are within 1.2e-7 relative, and orthogonality is within 2.5e-7.
- Host threads: when the host threads share CPUs with the Neuron runtime, libgomp's default spin-wait after each host LAPACK call delays the next device operations by up to about 4 ms.
GOMP_SPINCOUNT=10000removes that delay without slowing LAPACK.OMP_WAIT_POLICY=PASSIVEalso removes it, but slows LAPACK. - DF-RHF time to convergence (
bench_scf.py,neuron-nosync, with the defaults above):
| system | n_ao | full eigh: iterations, ms |
occupied_subspace: iterations, ms |
|---|---|---|---|
| benzene | 114 | 11, 106 | 11, 128 |
| C10H22 | 250 | 14, 191 | 14, 220 |
| C20H42 | 490 | 16, 573 | 15, 520 |
| C40H82 | 970 | 17, 3495 | 18, 3616 |
- The eigh stage at n_ao = 490 / 970 drops from 12.1 / 62.5 to 8.8 / 42.2 ms.
- No call after the cold start is a full
eigh. The earlierpasses=2, refresh=3e-2took 22 iterations and 5.6 s at n_ao = 970. - Below n_ao ≈ 300 the fixed per-call cost loses to aten
eigh. - The FP32 energy test is at its noise floor at n_ao = 970: successive energies differ by 4 ulp (4.9e-4 Eh), against a threshold of 3.1e-4 Eh. Iteration counts there vary by ±3 between nearby settings.