You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
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
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.
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
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):
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)
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):
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...
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)
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:
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.
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).
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...
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
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
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)
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
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
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.
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
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.
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)
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.
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.
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.
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.
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.
Activity
martin-frbg commentedon Nov 14, 2026
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)
martin-frbg commentedon Nov 14, 2026
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):
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...
martin-frbg commentedon Nov 14, 2026
Hmm, same (passing) result with the smaller GEMM_Q from the
ifdef WINDOWS_ABIbranch 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)
M-Colley commentedon Nov 14, 2026
Thanks, that fits the 0.3.33 boundary. NumPy's wheels are DYNAMIC_ARCH builds without a GENERIC core:
OPENBLAS_CORETYPE=GENERICprintsCore not found: GENERICand runs Haswell. So I built 0.3.33 myself and called DGELSD and DGESDD through ctypes the way NumPy does: a workspace query first, andrcond = eps * max(m, n).The surprise is that the failure reproduces only with NumPy's Windows wheel. Same i7-14700K, same
A.txtandb.txtas in the gist, 1 or 8 threads:A/SNUSE64BITINT DYNAMIC_ARCHZenis forced)Zenis forced)TARGET=GENERICTARGET=HASWELLEvery 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.
martin-frbg commentedon Nov 14, 2026
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).
martin-frbg commentedon Nov 14, 2026
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...
martin-frbg commentedon Nov 15, 2026
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.libsscipyopenblas_64.zip
martin-frbg commentedon Nov 15, 2026
As far as I can tell, the main differences to the scipy-nightly (assuming it is still using the
MacPython/openblas-libsrepository) are (1)gcc vs. LLVM (2) make vs cmake (3) the smaller-than-default BUFFERSIZE=20 (4) forcing MAX_STACK_ALLOC=2048martin-frbg commentedon Nov 16, 2026
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)
martin-frbg commentedon Nov 16, 2026
trying now with the non-standard
-fno-asynchronous-unwind-tablesfrom the MacPython build script that appears to be a workaround for a compiler bug affecting Cygwin that was fixed in GCC 8martin-frbg commentedon Nov 16, 2026
That gcc option proved to be harmless too... @mattip which version of gcc are you currently using for the scipy nightlies ?
mattip commentedon Nov 17, 2026
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.
martin-frbg commentedon Nov 17, 2026
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
mattip commentedon Nov 17, 2026
Yes, we are still using it to support 32-bit windows.
martin-frbg commentedon Nov 17, 2026
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
mattip commentedon Nov 17, 2026
We can drop win32 builds and upgrade rtools, or move on to a more modern toolchain if that solves the problem.
mattip commentedon Nov 18, 2026
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.
M-Colley commentedon Nov 18, 2026
Hi there, I found this problem on windows :) I have not tested on Linux.
martin-frbg commentedon Nov 19, 2026
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
mattip commentedon Nov 20, 2026
Over at MacPython/openblas-libs#293 I did two things:
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.
M-Colley commentedon Nov 21, 2026
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.np.linalg.lstsqwheels-x64-1, 0.3.34.481.4)wheels-x86_64-1, 0.3.34.481.4)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.
mattip commentedon Nov 21, 2026
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.Son 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_controlfpcannot 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 ownmingw-built Windows test programs always call it at 64-bit, which is why CI
never sees this.
With the reporter's 167x35 matrix
0x037F(64-bit, mingw-built caller)info=0 rank=35,|x|=2.15606459899180x027F(53-bit, MSVC-built caller)info=1 rank=0,|x|=8024.36364483834Why older versions were fine.
?LARF1F(used bydgebd2.f, first released in0.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
The same two hunks apply to
kernel/x86_64/znrm2.S(ZNRM2KERNEL/XNRM2KERNEL);kernel/x86/nrm2.Sandkernel/x86/znrm2.Shave the same single-exit structureand need it too. The long-double kernels
dot.S,asum.S,zdot.Sandzasum.Sare x87 as well, but they are only selected for the Q/X types, whichare extended-precision by definition. Alternatively
DNRM2KERNELcould point ata 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
fldcwwith0x027fbefore the call, e.g.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.
mattip commentedon Nov 21, 2026
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.mattip commentedon Nov 21, 2026
The phenomenon reproduces on posix: setting the flag from the default
0x037fto0x027freproduces the problematic values.martin-frbg commentedon Nov 21, 2026
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.
mattip commentedon Nov 22, 2026
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.
x86/x86_64: force 64-bit x87 precision in nrm2 and znrm2 kernels