Repository navigation
Description
Describe the issue:
np.linalg.svd never returns when given a square float64 matrix of order >= 3 containing at least one inf alongside finite entries. It spins at 100% CPU inside dgesdd indefinitely(observed > 1 hour on a 3x3).
It is a hang, not slowness. A Python-level timeout (signal.SIGALRM) cannot
interrupt it, because control never returns to the interpreter. only an OS-level kill stops the process.
The same numpy version (2.4.2) built against linux arm64 / OpenBLAS returns in ~3 ms for
the case below.
Related behaviour is already handled correctly: a matrix containing nan, or
one that is entirely 'inf', raises "LinAlgError: SVD did not converge" on
both backends. It is the mix of 'inf' and finite values that hangs.
Reproduce the code example:
python
import numpy as np
A = np.array([[np.inf, 1.0, 1.0],
[1.0, 1.0, 1.0],
[1.0, 1.0, 1.0]])
np.linalg.svd(A) Error message:
No error and no traceback, the process never returns.Python and NumPy Versions:
numpy 2.4.2
3.12.10 (main, Apr 9 2025, 03:49:38) [Clang 20.1.0]
Runtime Environment:
[{'numpy_version': '2.4.2',
'python': '3.12.10 (main, Apr 9 2025, 03:49:38) [Clang 20.1.0 ]',
'uname': uname_result(system='Darwin', release='25.6.0',
machine='arm64')},
{'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
'found': ['ASIMDHP', 'ASIMDDP'],
'not_found': ['ASIMDFHM']}},
{'ignore_floating_point_errors_in_matmul': True}]
np.show_config() build dependencies:
blas: name: accelerate, detection method: system
lapack: name: accelerate, detection method: system
compilers: clang 15.0.0
macOS 26.6.2 (build 25G83), Apple silicon (arm64).
Linux control: python:3.12-slim container, pip install numpy==2.4.2
(scipy-openblas wheel), same script, every case returns or raises in ms.
How does this issue affect you or how did you find it:
Found via statsmodels, where an ARIMA fit whose optimizer diverges builds a
non-finite approximate Hessian and passes it to np.linalg.pinv. On OpenBLAS
that surfaces as a ConvergenceWarning and a usable result; on Accelerate the
process hangs forever.
The practical impact is that any library computing a pseudo-inverse or SVD on
optimizer output can hang unkillably on macOS, with no warning, no traceback,
and no way to time out from Python. Callers cannot defend against it without
checking np.isfinite on every matrix before every call.
Activity
ikrommyd commentedon Sep 11, 2026
Can reproduce on x86 as well with AMD Ryzen 7 7800X3D with a fresh pypi installation
seberg commentedon Sep 11, 2026
Can reproduce, on linux as well with pypi (my local isntall seems fine, different OpenBLAS presumably).
Here is the traceback:
I'll suspect that in the end the same place for the initial report?
ikrommyd commentedon Sep 11, 2026
This is what I get for apple silicon, x86 with openblas, and x86 with scipy openblas. All on numpy main.
apple silicon:
linux openblas:
linux scipy openblas:
seberg commentedon Sep 11, 2026
Interesting that accelerate/openblas kinda agree! Is that an upstream issue from lapack or so?! (sorry really a random guess)
ikrommyd commentedon Sep 11, 2026
Yes it is and reproduces against reference lapack. Got my claude to write the following reproducer. Build instructions on the docstring:
with backtrace
and check this out. It works with
compute_uv=Falsecause in that cases it passesjobz='N'ikrommyd commentedon Sep 11, 2026
@OttoJHTX
I'm curious how you built numpy to get it to work with openblas on macos though cause you said
but I'm reproducing the problem on every system here and every blas version.
Also @seberg you said "my local isntall seems fine", so I'm curious what golden openblas works 🤣
mattip commentedon Sep 11, 2026
Perhaps it is an earlier version of LAPACK?
OttoJHTX commentedon Sep 11, 2026
You're right, i ran openblas on a linux container, not on mac. I'll edit the title
It returned fine on linux arm64 and openblas. I tried these things
[[inf,1,1],[1,1,1],[1,1,1]]inf[[nan,1,1],[1,1,1],[1,1,1]]infinf[-]BUG: np.linalg.svd hangs forever on a square >=3x3 matrix containing 'inf' (Apple Accelerate)[/-][+]BUG: np.linalg.svd hangs forever on a square >=3x3 matrix containing 'inf'[/+]ikrommyd commentedon Sep 11, 2026
I don't have time right now to manually dig through lapack source code but my agent says what's written bellow (did not validate by reading the code). In any case, if it's a lapack issue, I don't think there's anything we can do right? I'd guess that the right solution is jto just check the input if it's all finite and if not, never call into lapack routines and just raise a
LinAlgErrorearly.🤖 AI text below 🤖"
The hang is a bug in LAPACK itself. It reproduces with reference LAPACK 3.12.1, and the same code is still on master. Line numbers are from Reference-LAPACK
SRC/.dgesddreturns an error for NaN input (DISNAN(ANRM),dgesdd.f:600), but letsinfthrough. It then rescales A byBIGNUM/inf = 0, so theinfbecomes NaN and every other entry becomes 0.dgebrdturns that into the bidiagonald = [nan, 0, 0],e = [0, 0], which is passed on throughdbdsdc -> dlasdq -> dbdsqr.dbdsqr,SMINOA = |d(1)| = NaN, soTHRESH = MAX(TOL*SMINOA, ...)(dbdsqr.f:416). Fortran leavesMAXwith a NaN argument up to the compiler.THRESHcomes out NaN, the split testABSE.LE.THRESH(dbdsqr.f:465) is never true, even thougheis exactly 0. The relative convergence test (dbdsqr.f:580-582) then sees|e(2)| = 0 <= TOL*|d(3)| = 0. It setse(2)to 0 again and jumps back to the top of the loop without changing anything.dbdsqr.f:623), which is never reached. So the max-iterations check (dbdsqr.f:447-451) never fires, and the loop runs forever.Only some platforms hang because of step 2. On arm64, gfortran compiles
MAXtofmaxnm, which returns the non-NaN value, soTHRESHstays finite and it returns NaNs instead. x86-64 gfortran builds usemaxsd, which returns the NaN here, and Accelerate also ends up with a NaN.That's also why the
infhas to be in the first column: that's what puts the NaN ind(1). Forcompute_uv=False,dbdsqrgoes throughdlasq1instead of this loop, and for 2x2 andmin(M, N) > 25other code paths are used, so none of those hang.ikrommyd commentedon Sep 11, 2026
@OttoJHTX - I assume though that in the cases you say it "returns", the result is just garbage. AFAIK, svd on a matrix with infinity is not mathematically valid.
OttoJHTX commentedon Sep 11, 2026
Yea haha it's all nan
ikrommyd commentedon Sep 11, 2026
I opened Reference-LAPACK/lapack#1409
AbhijeetKushwaha1213 commentedon Sep 12, 2026