GeezerVison is showing github.com — BUG: Lapack lite is not thread-safe (need to guard) · Issue #22509 · numpy/numpy
BUG: Lapack lite is not thread-safe (need to guard) #22509Descriptionskimage found an issue probably related to eigvalsh or generally threaded calls to linalg_umath.cpp.
Unfortunately, I have not yet narrowed it down further, and the test is slightly flaky (on my machine). For example, it seems to easier reproduce running the full file, rather than just the test.
To reproduce, I have to explicitly use NPY_BLAS_ORDER= (disable blas use), then run current main of skimage tests:
python3 -m pytest skimage/feature/tests/test_basic_features.py::test_multiscale_basic_features_deprecated_multichannel
I have a gdb traceback of a crash (the actual crash fluctuates a bit, so it may well be elsewhere):
#5 0x00007fffb7ef3f7f in release_evd<double> (params=0x7fffda26e940) at numpy/linalg/umath_linalg.cpp:1482
#6 eigh_wrapper<double> (JOBZ=<optimized out>, UPLO=<optimized out>, args=<optimized out>, dimensions=<optimized out>,
steps=<optimized out>) at numpy/linalg/umath_linalg.cpp:1555
#7 0x00007ffff639eed5 in generic_wrapped_legacy_loop (__NPY_UNUSED_TAGGEDcontext=<optimized out>, data=<optimized out>,
dimensions=<optimized out>, strides=<optimized out>, auxdata=0x7fffd19e9b30)
at numpy/core/src/umath/legacy_array_method.c:87
So it seems eigvalsh and threading related. I need to dig further eventually, but maybe someone else has an idea what to look into (the umath code, the blas/lapack fallback code?). Activityseberg commented on Nov 2, 2022 Ah... should have checked the code first. https://github.com/numpy/numpy/blob/main/numpy/linalg/lapack_lite/f2c_d_lapack.c includes lines such as:
/* Local variables */
static integer i__, j, k;
static doublereal p, r__;
static integer z__, ic, ii, kk;
static doublereal cs;
static integer is, iu;
static doublereal sn;
static integer nm1;
static doublereal eps;
static integer ivt, difl, difr, ierr, perm, mlvl, sqre;
which is clearly not threadable. I suspect that some of the functions probably need state because they are called repeatedly or recurse (not sure).
Nobody should use lapack-lite of course, so locking seems OK to me, but I am unsure at which level we should introduce the lock.
We could tell the ufunc to never release the GIL if compiled without external BLAS. Or add some custom lock to all entry-points to lapack lite?
Or can we update lapack lite or our C wrappers to be safe? changed the title [-]BUG: Threading related issue in linalg (probably `eigvalsh`)[/-] [+]BUG: Lapack lite is not thread-safe (need to guard)[/+] on Nov 2, 2022ngoldbaum commented on Jun 12, 2024
We could tell the ufunc to never release the GIL if compiled without external BLAS. Or add some custom lock to all entry-points to lapack lite?
Or can we update lapack lite or our C wrappers to be safe?
Given how pervasive the use of global state in lapack_lite is, I don't think it's tenable to update it to be thread-safe. I think we could initialize a global lock in the lapack lite module initialization code and then acquire the global lock every time we call into the low-level fortran-translated code. Looking at the code, I doubt it's even reentrant, so we'd need to lock around every library call.
Obviously this is terrible for the free-threaded build, so I think I'll detect the free-threaded build in meson.build and then force users in that build to use a real BLAS.
Does that sound reasonable to you @seberg? seberg commented on Jun 13, 2024 Locking sounds fine to me. Forcing blas with freethreaded builds seems OK, no strong opinion. We could also force you to configure BLAS=none and print an error explaining that it is terrible (and maybe more so in free-threaded builds, although I am not sure it even is worse since the GIL was never sufficient to begin with).
We had discussed sunsetting lapacklite before, so we could even consider forcing explicitly choosing it for all builds, but I am happy either way. (We even discussed sunsetting it completely, since I think wasm figured out how to build a proper BLAS.)
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|