Skip to content

DGELSD and DGESDD (with vectors) report 'SVD did not converge' on a well-conditioned 167x35 matrix with the Haswell/Zen kernels, OpenBLAS 0.3.33 to 0.3.34.481 #6255

Closed

Description

@M-Colley

numpy.linalg.lstsq (DGELSD) raises LinAlgError: SVD did not converge in Linear Least Squares on a finite 167x35 double matrix with condition number 2.7e4. np.linalg.svd(A) with singular vectors (DGESDD, full or reduced) fails the same way; only the values-only np.linalg.svd(A, compute_uv=False) succeeds.

The matrix (A.txt, b.txt, written with %.17g, so they load back bit for bit) and a short repro.py are in this gist: https://gist.github.com/M-Colley/5c3163fdde75e4e8aa6bef2ec7132037

Output with NumPy 2.5.4 (Windows 11, Intel i7-14700K, which OpenBLAS runs as Haswell):

numpy 2.5.4 | scipy-openblas 0.3.34.106.0
A (167, 35) finite: True cond: 2.73e+04
numpy.linalg.LinAlgError: SVD did not converge in Linear Least Squares

and with OPENBLAS_CORETYPE=Sandybridge set:

numpy 2.5.4 | scipy-openblas 0.3.34.106.0
A (167, 35) finite: True cond: 2.73e+04
ok, |x| = 2.1560645989917813

This looks related to #5866, which was closed as numerical sensitivity on nearly singular matrices. This case is not nearly singular, and the result is a hard failure rather than a different rounding.

Versions (DGELSD through numpy.linalg.lstsq, same matrix and machine; OPENBLAS_NUM_THREADS=1; the thread count does not matter):

NumPy bundled scipy-openblas64 DGELSD
2.1.3 0.3.27 ok
2.3.5 0.3.30 ok
2.4.6 0.3.31.188.0 ok
2.5.0 0.3.33.112.0 fails
2.5.4 0.3.34.106.0 fails
2.6.0.dev (nightly, 2026-11-03) 0.3.34.237.2 fails
nightly with libscipy_openblas64_.dll swapped in 0.3.31.188.0 / 0.3.33.0.0 / 0.3.34.0.0 / 0.3.34.481.3 ok / fails / fails / fails

SciPy 1.18.1, which bundles OpenBLAS 0.3.31.dev, solves the same problem with DGELSD, DGELSS and DGELSY, and its DGESDD and DGESVD succeed.

Kernels (NumPy 2.5.4, OPENBLAS_CORETYPE):

  • Haswell (the default here) and Zen fail.
  • Sandybridge, Nehalem and Prescott pass.
  • SkylakeX could not run on this CPU.

So the first bad version is between 0.3.31 and 0.3.33.0, on the AVX2 kernel set. This matches the DLARF1F / DGEMV region discussed in #5866.

Activity

  1. martin-frbg commented on Nov 14, 2026

    @martin-frbg
    Collaborator

    0.3.33 imported DLARF1F changes from the development branch of Reference-LAPACK, which apparently made convergence more sensitive to numerical accuracy of AXPY and GEMV.
    So indeed this looks related to #5866 - I wonder how the reproducer behaves with OPENBLAS_CORETYPE=GENERIC (naive C code for DGEMV and DAXPY)

  2. martin-frbg commented on Nov 14, 2026

    @martin-frbg
    Collaborator

    Not reproduced with current develop under Linux (native is Zen5c supported through COOPERLAKE. the scipy-openblas version is bogus as I overwrote the library that conda installed):

    (numpyenv) (base) martin@xenopusw:~/numpyenv> python3 repro.py 
    numpy 2.5.0 | scipy-openblas 0.3.33.112.0
    A (167, 35) finite: True cond: 2.73e+04
    ok, |x| = 2.156064598992368
    (numpyenv) (base) martin@xenopusw:~/numpyenv> OPENBLAS_CORETYPE=NEHALEM python3 repro.py 
    numpy 2.5.0 | scipy-openblas 0.3.33.112.0
    A (167, 35) finite: True cond: 2.73e+04
    ok, |x| = 2.1560645989918266
    (numpyenv) (base) martin@xenopusw:~/numpyenv> OPENBLAS_CORETYPE=SANDYBRIDGE python3 repro.py 
    numpy 2.5.0 | scipy-openblas 0.3.33.112.0
    A (167, 35) finite: True cond: 2.73e+04
    ok, |x| = 2.156064598992141
    (numpyenv) (base) martin@xenopusw:~/numpyenv> OPENBLAS_CORETYPE=GENERIC python3 repro.py 
    numpy 2.5.0 | scipy-openblas 0.3.33.112.0
    A (167, 35) finite: True cond: 2.73e+04
    ok, |x| = 2.156064598992368
    (numpyenv) (base) martin@xenopusw:~/numpyenv> OPENBLAS_CORETYPE=HASWELL python3 repro.py 
    numpy 2.5.0 | scipy-openblas 0.3.33.112.0
    A (167, 35) finite: True cond: 2.73e+04
    ok, |x| = 2.156064598991765
    

    The HASWELL target uses a different set of blocking parameters in Windows builds to reduce stack size, which may have an effect on rounding, need to see if that makes any difference...

  3. martin-frbg commented on Nov 14, 2026

    @martin-frbg
    Collaborator

    Hmm, same (passing) result with the smaller GEMM_Q from the ifdef WINDOWS_ABI branch in param.h.
    I also cannot reproduce this with the actual scipy-openblas-0.3.33.122.0 a.k.a scipy-openblas-017048f4.so
    This is one elusive problem, I will see if I can test under Windows later (no code differences on the Haswell axpy or gemv code paths nor in the level2 BLAS driver code - the general multithreading environment is different of course)

  4. M-Colley commented on Nov 14, 2026

    @M-Colley
    ContributorAuthor

    Thanks, that fits the 0.3.33 boundary. NumPy's wheels are DYNAMIC_ARCH builds without a GENERIC core: OPENBLAS_CORETYPE=GENERIC prints Core not found: GENERIC and runs Haswell. So I built 0.3.33 myself and called DGELSD and DGESDD through ctypes the way NumPy does: a workspace query first, and rcond = eps * max(m, n).

    The surprise is that the failure reproduces only with NumPy's Windows wheel. Same i7-14700K, same A.txt and b.txt as in the gist, 1 or 8 threads:

    library OS core DGELSD DGESDD, jobz A / S DGESDD, jobz N
    NumPy 2.5.4 wheel: OpenBLAS 0.3.34.106.0, USE64BITINT DYNAMIC_ARCH Windows 11 Haswell (also when Zen is forced) INFO 1 INFO 1 0
    same DLL Windows 11 Sandybridge, Nehalem, Prescott 0 0 0
    NumPy 2.5.4 wheel: OpenBLAS 0.3.34.106.0 Linux (WSL2) Haswell (also when Zen is forced) 0 0 0
    0.3.33 built here, TARGET=GENERIC Linux (WSL2) generic 0 0 0
    0.3.33 built here, TARGET=HASWELL Linux (WSL2) HASWELL 0 0 0

    Every passing case gives the same solution, |x| = 2.156064599.

    My builds used gcc 15.2 with NOFORTRAN=1 C_LAPACK=1 USE_THREAD=0 (there is no gfortran here). Their LAPACK is therefore the C translation, but the Linux wheel, with compiled Fortran, passes too.

    So GENERIC passes, but here so does HASWELL, which means this does not isolate the kernels. On this machine the Haswell kernels fail only in the Windows build of the library. That points at how that DLL is compiled (toolchain or flags), not at the kernels' rounding alone. I can't build OpenBLAS on Windows here, but I'm happy to run any Windows binary you'd like tested.

  5. martin-frbg commented on Nov 14, 2026

    @martin-frbg
    Collaborator

    Hmm, 0.3.33 had the primary DLARF1F change, 0.3.34 added minor corrections to that - so indeed something about the Windows build seems most likely - either the compiler or the windows threading backend (as opposed to posix threads).

  6. martin-frbg commented on Nov 14, 2026

    @martin-frbg
    Collaborator

    As far as I remember, the scipy wheels use LLVM, but compiling with clang/flang-new instead of gcc/gfortran does not cause any failures on Linux. I'll see if I can test on Windows tomorrow...

  7. martin-frbg commented on Nov 15, 2026

    @martin-frbg
    Collaborator

    Curious - I can reproduce the problem with the scipy nightlies, but not when building from source using LLVM 22.1.7 as
    cmake -DCMAKE_C_COMPILER=clang-cl -DCMAKE_Fortran_COMPILER=flang -DCMAKE_MT=mt -DBUILD_SHARED_LIBS=ON -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DTARGET=CORE2 -DINTERFACE64=1 -DSYMBOLPREFIX=scipy_ -DSYMBOLSUFFIX=64_ -DDYNAMIC_ARCH=1 .. and copying the resulting openblas_64.dll on top of the one in site-packages/numpy.libs

    scipyopenblas_64.zip

  8. martin-frbg commented on Nov 15, 2026

    @martin-frbg
    Collaborator

    As far as I can tell, the main differences to the scipy-nightly (assuming it is still using the MacPython/openblas-libs repository) are (1)gcc vs. LLVM (2) make vs cmake (3) the smaller-than-default BUFFERSIZE=20 (4) forcing MAX_STACK_ALLOC=2048

  9. martin-frbg commented on Nov 16, 2026

    @martin-frbg
    Collaborator

    BUFFERSIZE and MAX_STACK_ALLOC are definitely irrelevant. A "make"-based build with mingw-w64-ucrt-gcc 16.2 also passes the test with the mingw numpy 2.5.5 (as does the stock mingw64 build of openblas-0.3.34, as an aside the mingw ones doesn't define INTERFACE64 nor symbol pre-and-suffixes)

  10. martin-frbg commented on Nov 16, 2026

    @martin-frbg
    Collaborator

    trying now with the non-standard -fno-asynchronous-unwind-tables from the MacPython build script that appears to be a workaround for a compiler bug affecting Cygwin that was fixed in GCC 8

  11. martin-frbg commented on Nov 16, 2026

    @martin-frbg
    Collaborator

    That gcc option proved to be harmless too... @mattip which version of gcc are you currently using for the scipy nightlies ?

  12. mattip commented on Nov 17, 2026

    @mattip
    Contributor

    On the manylinux builds we use gcc 14.2. On windows we use gcc 10.3 from rtools. On macos we use Xcode_16.4.

  13. martin-frbg commented on Nov 17, 2026

    @martin-frbg
    Collaborator

    That would be rtools 4.2 from four years ago ? I'll see if I can do builds with that and the gcc14-based rtools 4.5 from earlier this year

  14. mattip commented on Nov 17, 2026

    @mattip
    Contributor

    Yes, we are still using it to support 32-bit windows.

  15. martin-frbg commented on Nov 17, 2026

    @martin-frbg
    Collaborator

    Ah, so you're actually on the original rtools4 toolchain rather than 4.2 ? That's unfortunate, guess I'll need to look for optimization options to disable on the gemv sources then, in the hope of making this pass with the old gcc10

  16. mattip commented on Nov 17, 2026

    @mattip
    Contributor

    We can drop win32 builds and upgrade rtools, or move on to a more modern toolchain if that solves the problem.

  17. mattip commented on Nov 18, 2026

    @mattip
    Contributor

    The surprise is that the failure reproduces only with NumPy's Windows wheel.

    The original report does not mention windows only. @M-Colley can you confirm that you are reporting a windows problem or is this more general? If it is windows only, indeed the toolchain used for the windows build is suspect, see MacPython/openblas-libs#292.

  18. M-Colley commented on Nov 18, 2026

    @M-Colley
    ContributorAuthor

    Hi there, I found this problem on windows :) I have not tested on Linux.

  19. martin-frbg commented on Nov 19, 2026

    @martin-frbg
    Collaborator

    Unfortunately rtools4.5 i.e. x86_64-w64-mingw32.static.posix-gcc 14.3 doesn't appear to be new enough to fix this. I'll try playing with optimization flags tomorrow if there's time

  20. mattip commented on Nov 20, 2026

    @mattip
    Contributor

    Over at MacPython/openblas-libs#293 I did two things:

    • changed the gcc/gfortran Makefile build from rtools to msys2/setup-msys2@v2.33.0, which provides gcc16.2
    • added a x86_64 build to the existing llvm clang/flang CMake arm64 build and use LLVM-22.1.8.

    I did notice that the openblas-libs builds use -O2 for gfortran/flang builds, where the default is -O3. This is to reduce the dll size from 27 to 21MB. In any case, the artifacts are here and here if someone want to try to reproduce the problem using the DLLs in those artifacts.

  21. M-Colley commented on Nov 21, 2026

    @M-Colley
    ContributorAuthor

    I ran the reproducer on the two 64-bit-integer wheels from MacPython/openblas-libs#293 (i7-14700K, Windows 11, Python 3.13, NumPy 2.5.4). Each DLL was tested two ways: through ctypes (DGELSD, and DGESDD with jobz A/S/N), and through NumPy with the DLL copied over NumPy's bundled libscipy_openblas64_-ed4f167a5330424524f45258e7ca2c8d.dll.

    build Haswell kernel (default and forced; 1 and 8 threads) Sandybridge kernel np.linalg.lstsq
    NumPy 2.5.4's bundled DLL (0.3.34.106.0) DGELSD INFO 1; DGESDD A/S/N INFO 1/1/0 INFO 0 fails
    #293 msys2 gcc 16.2 (run 44691178759, wheels-x64-1, 0.3.34.481.4) DGELSD INFO 1; DGESDD A/S/N INFO 1/1/0 INFO 0 fails
    #293 LLVM 22.1.8 (run 44691178742, wheels-x86_64-1, 0.3.34.481.4) INFO 0 in every case INFO 0 passes, |x| = 2.156064598992073

    On this machine the LLVM build fixes the failure and the gcc 16.2 build does not. martin-frbg's mingw-w64-ucrt gcc 16.2 build passed, so the cause is probably one of the openblas-libs build options rather than the gcc version.

    A correction to my reply above: NumPy's Linux wheel was tested. It passes on the same CPU under WSL2, as reported in my earlier comment; only the Windows wheel fails.

  22. mattip commented on Nov 21, 2026

    @mattip
    Contributor

    Claude and I seem to have pinned down the difference between the builds. The x87 precision-control bits from the calling process affect the results of kernel/x86_64/nrm2.S on exactly the values reported. That kernel accumulates the whole sum of squares on the x87 stack (fld/fmul/faddp) and never sets the control word, so its result depends on whatever precision the caller happens to have selected. With mingw-built executables, they run at 64-bit mantissa, because the mingw CRT startup calls _fpreset
    (fninit); MSVC-built ones run at Windows' default 53-bit, and on x64 MSVC
    _controlfp cannot change it (it only reaches MXCSR used for SSE/AVX). CPython is generally MSVC-built, so numpy and scipy always call OpenBLAS at 53-bit — while OpenBLAS's own
    mingw-built Windows test programs always call it at 64-bit, which is why CI
    never sees this.

    With the reporter's 167x35 matrix

    x87 control word DGELSD
    0x037F (64-bit, mingw-built caller) info=0 rank=35, |x|=2.1560645989918
    0x027F (53-bit, MSVC-built caller) info=1 rank=0, |x|=8024.36364483834

    Why older versions were fine. ?LARF1F (used by dgebd2.f, first released in
    0.3.33) changed the last bits of the bidiagonal reduction just enough to make this
    important. The reported matrix sits right on the edge - perturbing any element by a
    single ULP makes it converge even with the 53-bit flag.

    Proposed fix — force extended precision in the x87 nrm2 kernels and restore
    the caller's control word on exit

    --- a/kernel/x86_64/nrm2.S
    +++ b/kernel/x86_64/nrm2.S
    @@ -54,6 +54,17 @@
     	emms
     #endif
     
    +/* The sum of squares is accumulated on the x87 stack, so its precision is
    + * whatever the caller's x87 control word selects.  Processes built with MSVC
    + * run with 53-bit precision, which silently defeats the extended-precision
    + * accumulation this kernel is built around, so force 64-bit precision here and
    + * restore the caller's control word on the way out. */
    +	subq	$8, %rsp
    +	fnstcw	(%rsp)
    +	fnstcw	4(%rsp)
    +	orw	$0x0300, 4(%rsp)
    +	fldcw	4(%rsp)
    +
     	fldz
     	testq	M, M
     	jle	.L999
    @@ -204,6 +215,8 @@
     	MOVSD	(%rsp), %xmm0
     	add	$2 * SIZE, %rsp
     #endif
    +	fldcw	(%rsp)
    +	addq	$8, %rsp
     	ret
     
     	EPILOGUE

    The same two hunks apply to kernel/x86_64/znrm2.S (ZNRM2KERNEL/XNRM2KERNEL);
    kernel/x86/nrm2.S and kernel/x86/znrm2.S have the same single-exit structure
    and need it too. The long-double kernels dot.S, asum.S, zdot.S and
    zasum.S are x87 as well, but they are only selected for the Q/X types, which
    are extended-precision by definition. Alternatively DNRM2KERNEL could point at
    a non-x87 implementation, but that gives up the accuracy the x87 kernel was
    written for.

    To reproduce from a mingw-built harness, no MSVC needed: issue
    fldcw with 0x027f before the call, e.g.

    unsigned short cw = 0x027f;
    __asm__ __volatile__("fldcw %0" : : "m"(cw));
    dgelsd_(&m, &n, &nrhs, a, &lda, b, &ldb, s, &rcond, &rank, work, &lwork, iwork, &info);

    Note that the fix has an edge case: if the routine returns in the middle (because of a runtime exception?), the flag will not be reset.

  23. mattip commented on Nov 21, 2026

    @mattip
    Contributor

    And as to why the gcc/gfortan build in MacPython/openblas-libs#293 hits the problem while the llvm/flang one doesn't: it is not because of the nrm2.S, the two compiler chains produce exactly the same assembly. It seems the routines above compile slightly differently so the kernel's reduced precision is not a problem for this particular matrix. Perhaps there are other cases where it would change the result.

  24. mattip commented on Nov 21, 2026

    @mattip
    Contributor

    The phenomenon reproduces on posix: setting the flag from the default 0x037f to 0x027f reproduces the problematic values.

  25. martin-frbg commented on Nov 21, 2026

    @martin-frbg
    Collaborator

    That's an interesting and annoying find, I'm worried that there may be more x86 kernels that rely on a 64bit "x87 fpu". And I guess I can't simply argue (over at Reference-LAPACK) that DLARF1F itself has become too fragile when it all appears to work with its Reference BLAS.

  26. mattip commented on Nov 22, 2026

    @mattip
    Contributor

    From what I can tell, nrm2.S/znrm2.S are the only kernels sensitive to the flag. The others fall into two groups: amax.S and iamax.S load 80-bit values and only compare them, and comparisons don't round; and the rest, like axpy.S and dot.S, are only ever selected for the Q*/X* slots, i.e. the 80-bit long double type, where x87 is the only possible implementation. nrm2.S is special in being instantiated for the double type too (DNRM2KERNEL, ZNRM2KERNEL), so it does x87 arithmetic on data the caller declared as double, and the control word then decides its last bit.

    One non-kernel case has the same problem: interface/rotg.c (built as drotg, and zrotg.c as zrotg) takes double arguments but computes internally in long double. LAPACK uses its own DLARTG rather than BLAS DROTG, so it isn't implicated in this bug, but anything calling drotg directly is exposed.

    Note MSVC callers, that set the "inaccurate" flag (like CPython), uses 64-bit long doubles, making direct access to the kernels "safe". The problem in this issue is that OpenBLAS is compiled by gcc, and nrm2.S is called internally with 80-bit long doubles.

  27. added a commit that references this issue on Nov 22, 2026
    69b5a23
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions