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
Intermittent NaNs appearing in results from calling shared-library blis from Julia #12
CPU: Core2 Duo E8400 (old machine)
OS: Ubuntu 14.04, x86-64
Compiled BLIS reference configuration, setting BLIS_ENABLE_DYNAMIC_BUILD := yes. By itself, BLIS passes its own make test.
I'm calling into BLIS from Julia by the following steps:
git clone https://github.com/julialang/julia
cd julia
mkdir -p $PWD/usr/lib
cp /path/to/libblis.so $PWD/usr/lib
echo 'override USE_SYSTEM_BLAS = 1' > Make.user
echo 'override USE_BLAS64 = 0' >> Make.user
echo 'override LIBBLAS = -L$(build_libdir) -lblis -lm' >> Make.user
echo 'override LIBBLASNAME = libblis' >> Make.user
make -j8 # this will take quite a while - Julia has lots of big dependencies
make testall
I'll see if I can translate a few of these test cases into C to figure out whether the problem is reproducible outside of Julia. I'll also try setting the BLIS integer size to 64-bit and delete the USE_BLAS64 = 0 line, see whether that changes anything.
Aha. Well not exactly multiple threads, but the master julia process launches separate worker julia processes to run the tests in parallel. The subprocesses should have separate memory spaces in which they load dynamic libraries, I think. Even when I run the tests in a single julia process, I still get similar-looking non-reproducible results and intermittent NaNs though.
Is there a list of which configurations are or are not thread-safe anywhere? Or should I assume none of them are at this point?
Is there a list of which configurations are or are not thread-safe anywhere? Or should
I assume none of them are at this point?
Sorry, I should have been more descriptive. The reference configuration isn't thread-safe because it is single-threaded and does not enable OpenMP. (OpenMP is currently the only threading model we support, but POSIX threads could be supported in the future.) In order to achieve thread safety, a critical section in the BLIS memory allocator must be enabled. Currently, it's only enabled when OpenMP is enabled, via BLIS_ENABLE_OPENMP.
Now, I'm not sure if the critical section works when BLIS is called from two different application threads. Tyler thinks that it may not do what we want it to do in that situation.
This all may be unrelated, of course. But it would be interesting to enable OpenMP and see if that fixes some or all of the NaNs.
OK, I also had to uncomment -fopenmp from CMISCFLAGS and add it to LDFLAGS (and LIBBLAS in Julia's Make.user). This is giving a different set of errors - either index out of bounds, or LAPACKException(6597069766656), or SingularException(140011638882304), or determinants of the wrong signs.
Trying with 64-bit integers in BLIS resulted in a segfault, I'll see if I can get a backtrace.
git clone https://github.com/julialang/julia
cd julia
mkdir -p $PWD/usr/lib
cp /path/to/libblis.so $PWD/usr/lib
echo 'override USE_SYSTEM_BLAS = 1' > Make.user
echo 'override USE_BLAS64 = 0' >> Make.user
echo 'override LIBBLAS = -L$(build_libdir) -lblis' >> Make.user
echo 'override LIBBLASNAME = libblis' >> Make.user
make -j8 # this will take quite a while - Julia has lots of big dependencies
make testall
I'll see if I can translate a few of these test cases into C to figure out
whether the problem is reproducible outside of Julia. I'll also try setting
the BLIS integer size to 64-bit and delete the USE_BLAS64 = 0 line, see
whether that changes anything.
—
Reply to this email directly or view it on GitHub #12.
Regarding the 64-bit integer segfaults, something seemed to be calling lapack dpotrf at the very end of creating Julia's system image, which then went into BLIS with what looked to be garbage dimensions. I wouldn't expect anything to be doing a Cholesky factorization at that moment, so not sure what was happening there.
Regarding the 64-bit integer segfaults, something seemed to be calling
lapack dpotrf at the very end of creating Julia's system image, which
then went into BLIS with what looked to be garbage dimensions. I wouldn't
expect anything to be doing a Cholesky factorization at that moment, so not
sure what was happening there.
—
Reply to this email directly or view it on GitHub #12 (comment).
I think I've figured out what's happening. These first few tests in linalg are often going to lapack, which complicates matters. It was more useful to start at the tests that use blas directly.
Julia offers both in-place and allocating versions of blas calls. The allocating versions do not explicitly initialize the contents of the output array before calling blas, they just send in whatever garbage is in the memory and use beta = 0. BLIS appears to be following the mathematical statement of BLAS operations a bit more literally than the reference and other implementations I'm aware of, and is actually multiplying my input garbage by zero. When my input garbage has NaNs (or presumably +/- Inf as well, but that's not as likely to happen randomly), the output ends up with corresponding NaNs in the same locations.
Should BLIS be checking for beta == 0 and special-casing to an assign-only operation, instead of multiplying the input by 0? Many applications may have grown accustomed to relying on this behavior over the years without realizing it.
@tkelman I want to definitely try this out, but only after the 0.3 release. Would be great to have a BLIS.jl julia package for now. You could just replace all calls to Array with zeros on the julia side to get the initializations zeroed out.
@ViralBShah that's about what I thought you'd say right now, no worries. I'll see what I can do with Clang auto-wrapping and coming up with a package.
With this patch to Julia https://gist.github.com/a16ce380d174b67561cd to initialize the allocating BLAS calls to 0, I see BLAS tests passing reliably for Float32 and Float64. There appears to be a problem with the imaginary part of the result of cdotc, this is likely due to complex type convention. Julia had switched here JuliaLang/julia#5291 to use cblas for these calls due to some troubles with MKL, but BLIS does't provide a cblas compatibility API. Looks like no difference from setting BLIS_ENABLE_C99_COMPLEX.
The linalg tests still see a few nans, lapack might be making some similar beta == 0 initialization assumptions to what unpatched Julia was making.
Should BLIS be checking for beta == 0 and special-casing to an assign-only operation,
instead of multiplying the input by 0? Many applications may have grown accustomed to
relying on this behavior over the years without realizing it.
Generally speaking, BLIS should already be employing this write vs. update optimization for beta == 0 cases. Could you give me more details about how your are running BLIS? Are you configuring with the reference configuration? Which operation is being called that causes the NaNs to pop up?
Looking at the reference gemm micro-kernel, I can't see any reason it would allow uninitialized memory to get in the way of a beta = 0 invocation.
Maybe it's a different operation altogether (e.g. level-1 / level-2) that is causing the problem?
Are you configuring with the reference configuration?
Yes, only change here is BLIS_ENABLE_DYNAMIC_BUILD := yes.
Which operation is being called that cause the NaNs to pop up? Looking at the reference gemm micro-kernel
Not sure whether gemm was ever causing trouble, I'll see how many test failures I can cause from direct BLAS calls. syrk definitely was leaving input NaNs in place with beta = 0.
Great, thanks Field. This is now passing Julia's blas tests for single and double. I'll see if I can reproduce any issues with complex in a standalone example and open a new issue if so.
Okay I'm not sure what's at fault with the complex dot product, I don't think it's worth opening a new issue on. The problem only occurs with cdotu_ and cdotc_, they give the correct real part but wrong imaginary part. It might be Julia's C FFI getting slightly confused by the function returning a struct of 2 floats, though it also happens if I turn on BLIS_ENABLE_C99_COMPLEX.
@tkelman Hmm, If the problem were about return value conventions, you would think it would affect zdotc_ and zdotu_ too.
I did a quick test on my end and was not able to reproduce any issue using 4x1 vectors.
Would it be easy to check exactly how wrong the imaginary result is? (Perhaps this is subtle numerical issue, which of course single-precision floats are quite vulnerable to.)
So libblis and libblas (system reference blas from Ubuntu package) give the same wrong result from cdotu_, which leads me to believe it's a Julia C FFI quirk. Interestingly if I ask for a Complex64 (Julia's name for complex single, 32 bits each for real and imaginary parts) return value from cblas_cdotu_sub out of libblas, which should actually be returning void, I get the imaginary part of the correct dot product value returned in the real part, and the imaginary part of the incorrect dot product value returned in the imaginary part. I'm not sure where that value's coming from, but the connection is interesting.
The cdotc stuff is ABI related to returning values from fortran functions. I wouldn't worry too much about it for now. I can't find the relevant julia issue right now.
FWIW, I ran into this issue a lot back when Kazushige Goto maintained GotoBLAS. It basically boiled down to making sure that the Fortran components of both the BLAS and the application were compiled with either -ff2c or -fno-f2c. (The important thing was that they matched, though I preferred the more sane -fno-f2c option.)
Thanks Viral. I'll have to check, I think the Ubuntu libblas package is cblas, rather than Netlib Fortran blas. edit: nevermind, this is wrong
The Julia issue was JuliaLang/julia#5291 where we did switch to using cblas for these functions, the trouble is that BLIS doesn't recreate an exact copy of cblas. BLIS' C API is similar to cblas for this particular function, but more general.
We can't use f2c with Julia, as we use the C calling convention for calling gfortran compiled libraries.
That's probably for the best. f2c calling conventions are horrible. (I only mentioned it because the real/imaginary return value issue seemed familiar.)
Activity
fgvanzee commentedon Jul 3, 2014
Tyler took a look at your output and had an idea: is Julia calling BLIS via multiple threads? (The reference configuration is not thread-safe.)
tkelman commentedon Jul 3, 2014
Aha. Well not exactly multiple threads, but the master julia process launches separate worker julia processes to run the tests in parallel. The subprocesses should have separate memory spaces in which they load dynamic libraries, I think. Even when I run the tests in a single julia process, I still get similar-looking non-reproducible results and intermittent NaNs though.
Is there a list of which configurations are or are not thread-safe anywhere? Or should I assume none of them are at this point?
fgvanzee commentedon Jul 3, 2014
Sorry, I should have been more descriptive. The reference configuration isn't thread-safe because it is single-threaded and does not enable OpenMP. (OpenMP is currently the only threading model we support, but POSIX threads could be supported in the future.) In order to achieve thread safety, a critical section in the BLIS memory allocator must be enabled. Currently, it's only enabled when OpenMP is enabled, via BLIS_ENABLE_OPENMP.
Now, I'm not sure if the critical section works when BLIS is called from two different application threads. Tyler thinks that it may not do what we want it to do in that situation.
This all may be unrelated, of course. But it would be interesting to enable OpenMP and see if that fixes some or all of the NaNs.
tkelman commentedon Jul 3, 2014
Sure, happy to try. Where would that flag be located?
fgvanzee commentedon Jul 3, 2014
Just add
to bli_config.h.
tkelman commentedon Jul 4, 2014
OK, I also had to uncomment
-fopenmpfromCMISCFLAGSand add it toLDFLAGS(andLIBBLASin Julia'sMake.user). This is giving a different set of errors - either index out of bounds, orLAPACKException(6597069766656), orSingularException(140011638882304), or determinants of the wrong signs.Trying with 64-bit integers in BLIS resulted in a segfault, I'll see if I can get a backtrace.
Maratyszcza commentedon Jul 4, 2014
This kind of error might also result from reading uninitialized memory.
Could you try to run under valgrind memcheck to eliminate this possibility?
On Wednesday, July 2, 2014, Tony Kelman [email protected] wrote:
tkelman commentedon Jul 4, 2014
Not sure whether this is useful: https://gist.github.com/tkelman/f03fb2b9779b7adac894edit: whoops, definitely not useful, valgrind doesn't apply to processes run by makeRegarding the 64-bit integer segfaults, something seemed to be calling lapack
dpotrfat the very end of creating Julia's system image, which then went into BLIS with what looked to be garbage dimensions. I wouldn't expect anything to be doing a Cholesky factorization at that moment, so not sure what was happening there.Maratyszcza commentedon Jul 4, 2014
I think you'll need to run it as
valgrind --tool=memcheck --trace-children=yes -- make testall
By default it does not instrument child processes.
On Fri, Jul 4, 2014 at 2:59 PM, Tony Kelman [email protected]
wrote:
tkelman commentedon Jul 4, 2014
Thanks yeah I realized that a few minutes after initially posting.
So there's a lot going on here https://gist.github.com/tkelman/c55da37d849d01585739 that I don't quite know how to make sense of, though the calls to lapack
sgecon_anddgecon_look most relevant.@ViralBShah now might not be a good time, but in case you want to play with an alternate BLAS at all...
tkelman commentedon Jul 5, 2014
I think I've figured out what's happening. These first few tests in linalg are often going to lapack, which complicates matters. It was more useful to start at the tests that use blas directly.
Julia offers both in-place and allocating versions of blas calls. The allocating versions do not explicitly initialize the contents of the output array before calling blas, they just send in whatever garbage is in the memory and use beta = 0. BLIS appears to be following the mathematical statement of BLAS operations a bit more literally than the reference and other implementations I'm aware of, and is actually multiplying my input garbage by zero. When my input garbage has NaNs (or presumably +/- Inf as well, but that's not as likely to happen randomly), the output ends up with corresponding NaNs in the same locations.
Should BLIS be checking for beta == 0 and special-casing to an assign-only operation, instead of multiplying the input by 0? Many applications may have grown accustomed to relying on this behavior over the years without realizing it.
ViralBShah commentedon Jul 5, 2014
@tkelman I want to definitely try this out, but only after the 0.3 release. Would be great to have a BLIS.jl julia package for now. You could just replace all calls to
Arraywithzeroson the julia side to get the initializations zeroed out.tkelman commentedon Jul 5, 2014
@ViralBShah that's about what I thought you'd say right now, no worries. I'll see what I can do with Clang auto-wrapping and coming up with a package.
With this patch to Julia https://gist.github.com/a16ce380d174b67561cd to initialize the allocating BLAS calls to 0, I see BLAS tests passing reliably for Float32 and Float64. There appears to be a problem with the imaginary part of the result of
cdotc, this is likely due to complex type convention. Julia had switched here JuliaLang/julia#5291 to use cblas for these calls due to some troubles with MKL, but BLIS does't provide a cblas compatibility API. Looks like no difference from settingBLIS_ENABLE_C99_COMPLEX.The linalg tests still see a few nans, lapack might be making some similar beta == 0 initialization assumptions to what unpatched Julia was making.
fgvanzee commentedon Jul 5, 2014
Generally speaking, BLIS should already be employing this write vs. update optimization for beta == 0 cases. Could you give me more details about how your are running BLIS? Are you configuring with the reference configuration? Which operation is being called that causes the NaNs to pop up?
Looking at the reference gemm micro-kernel, I can't see any reason it would allow uninitialized memory to get in the way of a beta = 0 invocation.
Maybe it's a different operation altogether (e.g. level-1 / level-2) that is causing the problem?
tkelman commentedon Jul 5, 2014
Yes, only change here is
BLIS_ENABLE_DYNAMIC_BUILD := yes.Not sure whether gemm was ever causing trouble, I'll see how many test failures I can cause from direct BLAS calls. syrk definitely was leaving input NaNs in place with beta = 0.
6 remaining items
tkelman commentedon Jul 8, 2014
Great, thanks Field. This is now passing Julia's blas tests for single and double. I'll see if I can reproduce any issues with complex in a standalone example and open a new issue if so.
tkelman commentedon Jul 8, 2014
Okay I'm not sure what's at fault with the complex dot product, I don't think it's worth opening a new issue on. The problem only occurs with
cdotu_andcdotc_, they give the correct real part but wrong imaginary part. It might be Julia's C FFI getting slightly confused by the function returning a struct of 2 floats, though it also happens if I turn onBLIS_ENABLE_C99_COMPLEX.I can work around the issue by applying this patch to Julia http://gist.github.com/43cece304e7247307e47 to account for the extra inputs to
bli_cdotvrelative tocblas_cdotu_sub. The good news is, with this change Julia's entire test suite passes using Blis in place of OpenBLAS.fgvanzee commentedon Jul 8, 2014
@tkelman Hmm, If the problem were about return value conventions, you would think it would affect
zdotc_andzdotu_too.I did a quick test on my end and was not able to reproduce any issue using 4x1 vectors.
Would it be easy to check exactly how wrong the imaginary result is? (Perhaps this is subtle numerical issue, which of course single-precision floats are quite vulnerable to.)
tkelman commentedon Jul 8, 2014
It's far enough off that this isn't a matter of numerical differences.
So
libblisandlibblas(system reference blas from Ubuntu package) give the same wrong result fromcdotu_, which leads me to believe it's a Julia C FFI quirk. Interestingly if I ask for aComplex64(Julia's name for complex single, 32 bits each for real and imaginary parts) return value fromcblas_cdotu_subout oflibblas, which should actually be returning void, I get the imaginary part of the correct dot product value returned in the real part, and the imaginary part of the incorrect dot product value returned in the imaginary part. I'm not sure where that value's coming from, but the connection is interesting.tkelman commentedon Jul 8, 2014
cc @andreasnoackjensen does this stuff with complex return types from
ccalllook familiar to you?fgvanzee commentedon Jul 8, 2014
@tkelman If netlib BLAS exhibits the problem too, then it sounds like it's on Julia's side. I'll let you take it from here! :)
ViralBShah commentedon Jul 8, 2014
The
cdotcstuff is ABI related to returning values from fortran functions. I wouldn't worry too much about it for now. I can't find the relevant julia issue right now.ViralBShah commentedon Jul 8, 2014
One possibility is to just use the CBLAS wrapper instead of calling it directly.
fgvanzee commentedon Jul 8, 2014
FWIW, I ran into this issue a lot back when Kazushige Goto maintained GotoBLAS. It basically boiled down to making sure that the Fortran components of both the BLAS and the application were compiled with either -ff2c or -fno-f2c. (The important thing was that they matched, though I preferred the more sane -fno-f2c option.)
https://gcc.gnu.org/onlinedocs/gfortran/Code-Gen-Options.html
I assume the issue with Julia is something similar.
tkelman commentedon Jul 8, 2014
Thanks Viral.
I'll have to check, I think the Ubuntu libblas package is cblas, rather than Netlib Fortran blas.edit: nevermind, this is wrongThe Julia issue was JuliaLang/julia#5291 where we did switch to using cblas for these functions, the trouble is that BLIS doesn't recreate an exact copy of cblas. BLIS' C API is similar to cblas for this particular function, but more general.
ViralBShah commentedon Jul 8, 2014
We can't use f2c with Julia, as we use the C calling convention for calling gfortran compiled libraries.
fgvanzee commentedon Jul 8, 2014
That's probably for the best. f2c calling conventions are horrible. (I only mentioned it because the real/imaginary return value issue seemed familiar.)
Add invscalv, invertv (flame#12)