Check for Infs and NaNs before LAPACK's SVD routines #1798
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 580 lines diff across 7 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.381
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1798"
general
weight: 0.35
mikmoore
LAPACK's SVD routines don't check their input for Infs and NaNs, and with such input they can fail badly:
svdnever returns, looping indbdsqr:Whether it hangs depends on where the Inf is:
svd([1.0 Inf; 0 1])returns NaNs instead.TridiagonalandSymTridiagonalgo through the dense routine, so theirsvdhangs as well.svd([0. NaN; 1. 2.])throwsArgumentError: invalid argument #4 to LAPACK call(#1058), andsvd(::Bidiagonal)throwsLAPACKException(2).svdvalsreturns all NaNs, and so docondandopnorm(A, 2), which use it. Butrankreturns0, since no NaN singular value exceeds the tolerance, andnullspaceandpinvhang or return wrong results, see below. The generalizedsvdvals(A, B)returns zeros for an Inf or a NaN, e.g.[0.0, 0.0, 0.0]forA = [1.0 2 0; Inf 2 2; 0 1 3].The eigensolvers already reject such input with
chkfinite, withArgumentError("matrix contains Infs or NaNs"). This PR adds the same check to the SVD wrappersgesdd!,gesvd!,ggsvd!,ggsvd3!,bdsqr!andbdsdc!. All the cases above now throw thatArgumentError. The check is O(mn), which is negligible next to the SVD.chkfinitenow accepts vectors, for the bidiagonaldande. #1788 makes the same one-line change, so the two PRs merge cleanly in either order.Behavior change: for matrices with an Inf,
svdvals,condandopnorm(A, 2)used to return NaN, andrankreturned0. They now throw, likeeigvalsdoes.Why Infs are rejected even where the SVD returns NaNs
Unlike #1788 and #1804, which only reject NaNs, this PR rejects Infs too, also for
svdvals, which returns NaNs for them. The reason is what the functions built on the SVD make of these NaNs. Onmasterbefore this PR, for 96 matrices with one Inf or -Inf (n= 2, 3, 5 and 10, real and complex, random or rank 1), compared with the same matrix with1e12instead of the Inf:rankreturned0for all 96, where the result for1e12is between 1 andn;nullspacehung for 24, returned NaNs for 32, and returned a fulln×nbasis for the other 40, where the null space for1e12has dimension 0 ton - 1;pinvhung for 24, returned NaNs for 52, and a finite matrix for 20;condandopnorm(A, 2)returned NaN, which is fine.And whether the SVD itself returns NaNs or hangs for an Inf depends on whether it computes singular vectors. For all positions of a NaN, Inf or -Inf for
n= 1 to 4, real and complex, with LAPACK 3.12.0 from OpenBLAS 0.3.34:| Routine | NaN | Inf |
|---|---|---|
|
gesdd, values |ArgumentError(LAPACK's own check) 180 | NaN 300 ||
gesdd, vectors |ArgumentError(LAPACK's own check) 180 | NaN 230, hangs 70 ||
gesvd, values | NaN 178, wrong (finite) 2 | NaN 300 ||
gesvd, vectors |LAPACKException150, NaN 30 | NaN 230, hangs 70 ||
bdsdc, values | NaN 68, wrong (finite) 2 | NaN 136, Inf 4 ||
bdsdc, vectors |LAPACKException42, NaN 6 | hangs with an Inf on the diagonal, otherwise the limits orLAPACKException||
ggsvd3| NaN 690, wrong (finite) 22,LAPACKException8 | NaN 1144, finite 40, Inf 16 |Tests
test/svd.jl:svdwith both algorithms,svdvals, and the generalizedsvd/svdvals, forFloat64andComplexF64. Alsosvd/svdvalsof upper and lowerBidiagonal, andggsvd!directly, whichsvd(A, B)only calls for LAPACK older than 3.6. Each with NaN and Inf.test/lapack.jl:bdsqr!andbdsdc!called directly.bdsqr!has no caller in the package.The cases that hung on
masternow throw. I ran each in its own process with a timeout. These suites pass locally:svd,lapack,bidiag,dense,schur,triangular,generic,symmetric,diagonal,tridiag,pinv,hessenberg,special,eigen,uniformscaling,qrandambiguous_exec.Related
Fixes #1058.
---
_Generated by Claude Code_