Open sumny opened 3 years ago
Thanks for the bug report and detailed investigation. My guess is that the right fix here is to ensure that R and Python are both using the same BLAS library; presumedly if using Python from Miniconda standalone then there's a bundled version of OpenBLAS that is used instead.
@kevinushey would it be possible for reticulate to compare the BLAS libraries used in numpy vs R and warn or error on this?
I know this isn't really a reticulate issue per se, numpy should be throwing a helpful error instead of a segfault when linked against another version of BLAS. But reticulate users will feel the issue most keenly since it's particularly difficult or impossible for them to debug this and be able to re-install numpy from source (e.g. I'm not sure if that's possible with conda-based envs?)
meanwhile if anyone stumbles on this, this can be resolved in a pip-based virtualenv by installing numpy from source, e.g. with
reticulate::py_install("numpy", pip_options="--no-binary='numpy'", ignore_installed=TRUE)
as @jwalton3141 notes above.
Good question... we could probably introspect the BLAS library that numpy was configured to use when it is loaded, e.g.
> np$show_config()
blas_info:
libraries = ['cblas', 'blas', 'cblas', 'blas']
library_dirs = ['/Users/kevin/Library/r-miniconda-arm64/envs/r-reticulate/lib']
include_dirs = ['/Users/kevin/Library/r-miniconda-arm64/envs/r-reticulate/include']
language = c
define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)]
libraries = ['cblas', 'blas', 'cblas', 'blas']
library_dirs = ['/Users/kevin/Library/r-miniconda-arm64/envs/r-reticulate/lib']
include_dirs = ['/Users/kevin/Library/r-miniconda-arm64/envs/r-reticulate/include']
language = c
lapack_info:
libraries = ['lapack', 'blas', 'lapack', 'blas']
library_dirs = ['/Users/kevin/Library/r-miniconda-arm64/envs/r-reticulate/lib']
language = f77
lapack_opt_info:
libraries = ['lapack', 'blas', 'lapack', 'blas', 'cblas', 'blas', 'cblas', 'blas']
library_dirs = ['/Users/kevin/Library/r-miniconda-arm64/envs/r-reticulate/lib']
language = c
define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)]
include_dirs = ['/Users/kevin/Library/r-miniconda-arm64/envs/r-reticulate/include']
Supported SIMD extensions in this NumPy install:
baseline = NEON,NEON_FP16,NEON_VFPV4,ASIMD
found = ASIMDHP
not found = ASIMDDP
And then cross-check that against the BLAS libraries actually loaded in the process:
> system(paste("lsof -p", Sys.getpid(), "| grep blas"))
rsession- 45809 kevin txt REG 1,18 193440 56767533 /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
rsession- 45809 kevin txt REG 1,18 10570608 6977756 /Users/kevin/Library/r-miniconda-arm64/envs/r-reticulate/lib/libopenblas_vortexp-r0.3.18.dylib
Might be fixed https://github.com/numpy/numpy/pull/21717
I recently stumbled across a BLAS compatiblity issue. Although this issue may only occur seldomly (depending on your system's and
numpy
's BLAS version), I wanted to report/discuss it nevertheless.First some information about my setup:
Now, suppose I want to invert a matrix in
R
usingnumpy
viareticulate
:This gives me a segfault:
Debugging via
R -d gdb --vanilla
yields:Note that
/usr/lib/libblas.so.3
is a symlink toNotably
drot_k_SANDYBRIDGE ()
actually should not be called here. I believe this is a result ofnumpy
being built against a different openblas version, namelylibopenblasp-r0-ae94cfde.3.9.dev.so
whereasR
is built against the newerlibopenblasp-r0.3.13.so
. Therefore, I believe some table lookup or something related fails, causing the segfault.In
python
(/home/lps/.local/share/r-miniconda/envs/r-reticulate/bin/python
) everything works as expected:If I downgrade my systems openblas version to e.g.,
0.3.10
or0.3.9
the matrix inversion now also works inR
usingnumpy
viareticulate
.I am not sure how a good solution to this problem could look like. I guess you cannot expect different BLAS versions to be fully compatible with each other.