← Back to Pull requests

Check for Infs and NaNs before LAPACK's SVD routines #1798

Closed
unknown closed this pull request without merging on Oct 22, 2026 11:03 UTC
View on GitHub
unknown unknown commented on Oct 21, 2026 18:58 UTC
Author

LAPACK's SVD routines don't check their input for Infs and NaNs, and with such input they can fail badly:

  • Hang with an Inf: svd never returns, looping in dbdsqr:

  •   julia> svd([1.0 2 0; Inf 2 2; 0 1 3])   # hangs, also with alg = QRIteration() and for ComplexF64
    
      julia> svd(Bidiagonal([1.0, Inf, 3.0], [1.0, 2.0], :U))   # hangs in bdsdc!
      

    Whether it hangs depends on where the Inf is: svd([1.0 Inf; 0 1]) returns NaNs instead. Tridiagonal and SymTridiagonal go through the dense routine, so their svd hangs as well.
  • Misleading error with a NaN: svd([0. NaN; 1. 2.]) throws ArgumentError: invalid argument #4 to LAPACK call (#1058), and svd(::Bidiagonal) throws LAPACKException(2).

  • Wrong results with an Inf: svdvals returns all NaNs, and so do cond and opnorm(A, 2), which use it. But rank returns 0, since no NaN singular value exceeds the tolerance, and nullspace and pinv hang or return wrong results, see below. The generalized svdvals(A, B) returns zeros for an Inf or a NaN, e.g. [0.0, 0.0, 0.0] for A = [1.0 2 0; Inf 2 2; 0 1 3].
  • The eigensolvers already reject such input with chkfinite, with ArgumentError("matrix contains Infs or NaNs"). This PR adds the same check to the SVD wrappers gesdd!, gesvd!, ggsvd!, ggsvd3!, bdsqr! and bdsdc!. All the cases above now throw that ArgumentError. The check is O(mn), which is negligible next to the SVD.

    chkfinite now accepts vectors, for the bidiagonal d and e. #1788 makes the same one-line change, so the two PRs merge cleanly in either order.

    Behavior change: for matrices with an Inf, svdvals, cond and opnorm(A, 2) used to return NaN, and rank returned 0. They now throw, like eigvals does.

    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. On master before 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 with 1e12 instead of the Inf:
  • rank returned 0 for all 96, where the result for 1e12 is between 1 and n;

  • nullspace hung for 24, returned NaNs for 32, and returned a full n×n basis for the other 40, where the null space for 1e12 has dimension 0 to n - 1;

  • pinv hung for 24, returned NaNs for 52, and a finite matrix for 20;

  • cond and opnorm(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 | LAPACKException 150, NaN 30 | NaN 230, hangs 70 |
    | bdsdc, values | NaN 68, wrong (finite) 2 | NaN 136, Inf 4 |
    | bdsdc, vectors | LAPACKException 42, NaN 6 | hangs with an Inf on the diagonal, otherwise the limits or LAPACKException |
    | ggsvd3 | NaN 690, wrong (finite) 22, LAPACKException 8 | NaN 1144, finite 40, Inf 16 |

    Tests


  • test/svd.jl: svd with both algorithms, svdvals, and the generalized svd/svdvals, for Float64 and ComplexF64. Also svd/svdvals of upper and lower Bidiagonal, and ggsvd! directly, which svd(A, B) only calls for LAPACK older than 3.6. Each with NaN and Inf.

  • test/lapack.jl: bdsqr! and bdsdc! called directly. bdsqr! has no caller in the package.
  • The cases that hung on master now 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, qr and ambiguous_exec.

    Related


  • #1788: tridiagonal eigensolvers, which check for NaNs only;

  • #1804: Schur factorization, which checks for NaNs only, since it returns NaNs for Infs, as the testset for issue #40141 requires;

  • #1805: generalized eigenvalue and Schur routines, which check for both.

  • #1809: a script to check how LAPACK behaves for Infs and NaNs, on other platforms and with other LAPACK implementations.
  • Fixes #1058.

    ---
    _Generated by Claude Code_

    Diff: 580 lines (+435 / -145) across 7 files Reviewer: mikmoore Review duration: 965m
    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
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 49.9% (MEDIUM) · Higher = larger blast radius
    49.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.7% · Safe to merge
    19.7%