Skip to content

BUG: np.linalg.svd hangs forever on a square >=3x3 matrix containing 'inf' #32591

Closed

Description

@OttoJHTX

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

  1. ikrommyd commented on Sep 11, 2026

    @ikrommyd
    Member

    Can reproduce on x86 as well with AMD Ryzen 7 7800X3D with a fresh pypi installation

    iason in  tinybox in ~
    ❯ uv venv -p 3.14.7
    Using CPython 3.14.7
    Creating virtual environment at: .venv
    Activate with: source .venv/bin/activate
    
    iason in  tinybox in ~
    ❯ source .venv/bin/activate
    
    iason in  tinybox in ~ via  pyenv system (iason)
    ❯ uv pip install numpy
    Resolved 1 package in 1ms
    Installed 1 package in 11ms
     + numpy==2.5.3
    
    iason in  tinybox in ~ via  pyenv system (iason)
    ❯ python
    Python 3.14.7 (main, Aug 25 2026, 14:02:56) [Clang 22.1.3 ] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 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)
    ...
    ^C^C^C^C^C^C^C^C^C^Z
    [1]  + 87917 suspended  python
    
    iason in  tinybox in ~ via  pyenv system (iason) took 1m11s
    ✦ ❯ kill -9 87917
    [1]  + 87917 killed     python
    
  2. seberg commented on Sep 11, 2026

    @seberg
    Member

    Can reproduce, on linux as well with pypi (my local isntall seems fine, different OpenBLAS presumably).

    Here is the traceback:

    #0  0x00007fffe7d88bec in scipy_dbdsqr_64_ ()
       from <snip>/numpy/_core/../../numpy.libs/libscipy_openblas64_-f48b354e.so
    #1  0x00007fffe7da4fb5 in scipy_dlasdq_64_ ()
       from <snip>/numpy/_core/../../numpy.libs/libscipy_openblas64_-f48b354e.so
    #2  0x00007fffe7d8823c in scipy_dbdsdc_64_ ()
       from <snip>/numpy/_core/../../numpy.libs/libscipy_openblas64_-f48b354e.so
    #3  0x00007fffe78e7194 in scipy_dgesdd_64_ ()
       from <snip>/numpy/_core/../../numpy.libs/libscipy_openblas64_-f48b354e.so
    #4  0x00007fffe8e1eb07 in void svd_wrapper<double>(char, char**, long const*, long const*) [clone .constprop.0] ()
       from <snip>/numpy/linalg/_umath_linalg.cpython-314-x86_64-linux-gnu.so
    #5  0x00007fffe86e9a75 in generic_wrapped_legacy_loop ()
       from<snip>/numpy/_core/_multiarray_umath.cpython-314-x86_64-linux-gnu.so
    

    I'll suspect that in the end the same place for the initial report?

  3. ikrommyd commented on Sep 11, 2026

    @ikrommyd
    Member

    This is what I get for apple silicon, x86 with openblas, and x86 with scipy openblas. All on numpy main.

    apple silicon:

    (lldb) bt
    * thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
      * frame #0: 0x00000001973d1600 libLAPACK.dylib`___lldb_unnamed_symbol_190f44c5c + 2468
        frame #1: 0x0000000196f52240 libLAPACK.dylib`___lldb_unnamed_symbol_190ac59c4 + 2172
        frame #2: 0x0000000197371a68 libLAPACK.dylib`___lldb_unnamed_symbol_190ee49d0 + 4248
        frame #3: 0x0000000196cc6508 libLAPACK.dylib`___lldb_unnamed_symbol_190835e7c + 18060
        frame #4: 0x00000001972edb84 libLAPACK.dylib`dgesdd$NEWLAPACK + 128
        frame #5: 0x00000001007ee5b8 _umath_linalg.cpython-314-darwin.so`call_gesdd(params=0x000000016fdf6330) at umath_linalg.cpp:2739:5 [opt] [inlined]
        frame #6: 0x00000001007ee5ac _umath_linalg.cpython-314-darwin.so`void svd_wrapper<double>(JOBZ=<unavailable>, args=0x00000009df2c43b0, dimensions=<unavailable>, steps=<unavailable>) at umath_linalg.cpp:3064:22 [opt]
        frame #7: 0x0000000105b6b4a4 _multiarray_umath.cpython-314-darwin.so`call_cached_loop(context=<unavailable>, data=<unavailable>, dimensions=<unavailable>, strides=<unavailable>, __NPY_UNUSED_TAGGEDauxdata=<unavailable>) at legacy_array_method.c:40:5 [opt]
        frame #8: 0x0000000105b7401c _multiarray_umath.cpython-314-darwin.so`PyUFunc_GeneralizedFunctionInternal(ufunc=0x0000000105816240, ufuncimpl=0x0000000105840e00, operation_descrs=0x000000016fdfb240, op=<unavailable>, casting=<unavailable>, order=NPY_KEEPORDER, axis=<unavailable>, axes=<unavailable>, keepdims=<unavailable>) at ufunc_object.c:2186:22 [opt]
        frame #9: 0x0000000105b700bc _multiarray_umath.cpython-314-darwin.so`ufunc_generic_fastcall(ufunc=<unavailable>, args=<unavailable>, len_args=<unavailable>, kwnames=<unavailable>, outer=<unavailable>) at ufunc_object.c:5183:18 [opt]
    

    linux openblas:

    (gdb) bt
    #0  0x00007ffff4011e47 in dbdsqr_ () from /usr/lib/x86_64-linux-gnu/libopenblas.so.0
    #1  0x00007ffff40dadd2 in dlasdq_ () from /usr/lib/x86_64-linux-gnu/libopenblas.so.0
    #2  0x00007ffff4010ef6 in dbdsdc_ () from /usr/lib/x86_64-linux-gnu/libopenblas.so.0
    #3  0x00007ffff4048128 in dgesdd_ () from /usr/lib/x86_64-linux-gnu/libopenblas.so.0
    #4  0x00007ffff7684df5 in call_gesdd (params=0x7fffffff6fb0) at ../numpy/linalg/umath_linalg.cpp:2739
    #5  svd_wrapper<double> (JOBZ=<optimized out>, args=<optimized out>, dimensions=<optimized out>, steps=<optimized out>) at ../numpy/linalg/umath_linalg.cpp:3064
    #6  0x00007ffff650dc12 in call_cached_loop (context=<optimized out>, data=<optimized out>, dimensions=<optimized out>, strides=<optimized out>, __NPY_UNUSED_TAGGEDauxdata=<optimized out>)
        at ../numpy/_core/src/umath/legacy_array_method.c:40
    #7  0x00007ffff651b3a0 in PyUFunc_GeneralizedFunctionInternal (ufunc=ufunc@entry=0x7fff6b9ade40, ufuncimpl=ufuncimpl@entry=0x7fff6b9bbad0, operation_descrs=operation_descrs@entry=0x7fffffffbd68,
        op=op@entry=0x7fffffffbd20, casting=casting@entry=NPY_SAME_KIND_CASTING, order=order@entry=NPY_KEEPORDER, axis=<optimized out>, axes=<optimized out>, keepdims=<optimized out>)
        at ../numpy/_core/src/umath/ufunc_object.c:2186
    #8  0x00007ffff651cffc in ufunc_generic_fastcall (ufunc=<optimized out>, args=<optimized out>, len_args=<optimized out>, kwnames=<optimized out>, outer=<optimized out>)
        at ../numpy/_core/src/umath/ufunc_object.c:5183
    

    linux scipy openblas:

    (gdb) bt
    #0  0x00007ffff6189118 in scipy_dbdsqr_64_ () from /home/iason/.pyenv/versions/3.14-dev/lib/python3.14/site-packages/scipy_openblas64/lib/libscipy_openblas64_.so
    #1  0x00007ffff61a4fb5 in scipy_dlasdq_64_ () from /home/iason/.pyenv/versions/3.14-dev/lib/python3.14/site-packages/scipy_openblas64/lib/libscipy_openblas64_.so
    #2  0x00007ffff618823c in scipy_dbdsdc_64_ () from /home/iason/.pyenv/versions/3.14-dev/lib/python3.14/site-packages/scipy_openblas64/lib/libscipy_openblas64_.so
    #3  0x00007ffff5ce7194 in scipy_dgesdd_64_ () from /home/iason/.pyenv/versions/3.14-dev/lib/python3.14/site-packages/scipy_openblas64/lib/libscipy_openblas64_.so
    #4  0x00007ffff6666a3a in call_gesdd (params=0x7fffffff6fa0) at ../numpy/linalg/umath_linalg.cpp:2739
    #5  svd_wrapper<double> (JOBZ=<optimized out>, args=<optimized out>, dimensions=<optimized out>, steps=<optimized out>) at ../numpy/linalg/umath_linalg.cpp:3064
    #6  0x00007fffcc70cd92 in call_cached_loop (context=<optimized out>, data=<optimized out>, dimensions=<optimized out>, strides=<optimized out>, __NPY_UNUSED_TAGGEDauxdata=<optimized out>)
        at ../numpy/_core/src/umath/legacy_array_method.c:40
    #7  0x00007fffcc71a520 in PyUFunc_GeneralizedFunctionInternal (ufunc=ufunc@entry=0x7fffcc30da40, ufuncimpl=ufuncimpl@entry=0x7fffcc383210, operation_descrs=operation_descrs@entry=0x7fffffffbd68,
        op=op@entry=0x7fffffffbd20, casting=casting@entry=NPY_SAME_KIND_CASTING, order=order@entry=NPY_KEEPORDER, axis=<optimized out>, axes=<optimized out>, keepdims=<optimized out>)
        at ../numpy/_core/src/umath/ufunc_object.c:2186
    #8  0x00007fffcc71c17c in ufunc_generic_fastcall (ufunc=<optimized out>, args=<optimized out>, len_args=<optimized out>, kwnames=<optimized out>, outer=<optimized out>)
        at ../numpy/_core/src/umath/ufunc_object.c:5183
    
  4. seberg commented on Sep 11, 2026

    @seberg
    Member

    Interesting that accelerate/openblas kinda agree! Is that an upstream issue from lapack or so?! (sorry really a random guess)

  5. ikrommyd commented on Sep 11, 2026

    @ikrommyd
    Member

    Yes it is and reproduces against reference lapack. Got my claude to write the following reproducer. Build instructions on the docstring:

    /*
     * Build against reference LAPACK (static, so every LAPACK routine is the
     * reference one; only BLAS comes from the system library):
     *   gcc repro_gesdd.c -o repro_gesdd /usr/lib/x86_64-linux-gnu/lapack/liblapack.a -lblas -lgfortran -lm
     * Run:
     *   ./repro_gesdd N   # singular values only: returns NaNs
     *   ./repro_gesdd A   # with singular vectors: hangs in dbdsqr
     */
    #include <math.h>
    #include <stdio.h>
    
    void dgesdd_(char *jobz, int *m, int *n, double *a, int *lda, double *s,
                 double *u, int *ldu, double *vt, int *ldvt, double *work,
                 int *lwork, int *iwork, int *info);
    
    int main(int argc, char **argv)
    {
        char jobz = argc > 1 ? argv[1][0] : 'A';
        int n = 3, lwork = 1000, iwork[8 * 3], info;
        double a[9] = {INFINITY, 1, 1,
                       1,        1, 1,
                       1,        1, 1};
        double s[3], u[9], vt[9], work[1000];
    
        dgesdd_(&jobz, &n, &n, a, &n, s, u, &n, vt, &n, work, &lwork, iwork, &info);
        printf("jobz=%c info=%d s=[%g %g %g]\n", jobz, info, s[0], s[1], s[2]);
        return 0;
    }

    with backtrace

    (gdb) bt
    #0  0x0000555555572ad1 in dbdsqr_ ()
    #1  0x0000555555567472 in dlasdq_ ()
    #2  0x000055555555e7b6 in dbdsdc_ ()
    #3  0x0000555555559398 in dgesdd_ ()
    #4  0x0000555555556629 in main ()
    

    and check this out. It works with compute_uv=False cause in that cases it passes jobz='N'

    In [1]: 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, compute_uv=False)
    Out[1]: array([nan, nan, nan])
  6. ikrommyd commented on Sep 11, 2026

    @ikrommyd
    Member

    @OttoJHTX
    I'm curious how you built numpy to get it to work with openblas on macos though cause you said

    The same numpy version (2.4.2) built against OpenBLAS returns in ~3 ms for
    the case below, so this looks specific to the Accelerate LAPACK.

    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 🤣

  7. mattip commented on Sep 11, 2026

    @mattip
    Member

    Perhaps it is an earlier version of LAPACK?

  8. OttoJHTX commented on Sep 11, 2026

    @OttoJHTX
    Author

    @OttoJHTX I'm curious how you built numpy to get it to work with openblas on macos though cause you said

    The same numpy version (2.4.2) built against OpenBLAS returns in ~3 ms for
    the case below, so this looks specific to the Accelerate LAPACK.

    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 🤣

    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

    case macOS arm64 / Accelerate linux arm64 / OpenBLAS linux x86_64 / OpenBLAS
    [[inf,1,1],[1,1,1],[1,1,1]] hangs returns, 5 ms hangs
    4x4, one inf hangs returns, 3 ms hangs
    [[nan,1,1],[1,1,1],[1,1,1]] LinAlgError LinAlgError LinAlgError
    3x3 all inf LinAlgError LinAlgError LinAlgError
    3x2 / 2x3 / 2x2, one inf returns returns returns
  9. changed the title [-]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'[/+] on Sep 11, 2026
  10. ikrommyd commented on Sep 11, 2026

    @ikrommyd
    Member

    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 LinAlgError early.

    🤖 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/.

    1. dgesdd returns an error for NaN input (DISNAN(ANRM), dgesdd.f:600), but lets inf through. It then rescales A by BIGNUM/inf = 0, so the inf becomes NaN and every other entry becomes 0. dgebrd turns that into the bidiagonal d = [nan, 0, 0], e = [0, 0], which is passed on through dbdsdc -> dlasdq -> dbdsqr.
    2. In dbdsqr, SMINOA = |d(1)| = NaN, so THRESH = MAX(TOL*SMINOA, ...) (dbdsqr.f:416). Fortran leaves MAX with a NaN argument up to the compiler.
    3. If THRESH comes out NaN, the split test ABSE.LE.THRESH (dbdsqr.f:465) is never true, even though e is exactly 0. The relative convergence test (dbdsqr.f:580-582) then sees |e(2)| = 0 <= TOL*|d(3)| = 0. It sets e(2) to 0 again and jumps back to the top of the loop without changing anything.
    4. The iteration counter only increases after a QR sweep (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 MAX to fmaxnm, which returns the non-NaN value, so THRESH stays finite and it returns NaNs instead. x86-64 gfortran builds use maxsd, which returns the NaN here, and Accelerate also ends up with a NaN.

    That's also why the inf has to be in the first column: that's what puts the NaN in d(1). For compute_uv=False, dbdsqr goes through dlasq1 instead of this loop, and for 2x2 and min(M, N) > 25 other code paths are used, so none of those hang.

  11. ikrommyd commented on Sep 11, 2026

    @ikrommyd
    Member

    @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.

  12. OttoJHTX commented on Sep 11, 2026

    @OttoJHTX
    Author

    @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.

    Yea haha it's all nan

  13. ikrommyd commented on Sep 11, 2026

    @ikrommyd
    Member
  14. AbhijeetKushwaha1213 commented on Sep 12, 2026

    @AbhijeetKushwaha1213
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions