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
BUG: <input>:1: RuntimeWarning: invalid value encountered in matmul #24067
Getting an error with numpy version 1.25.0 when doing matrix multiplication with operator '@'.
When an array surpasses a certain size limit. Multiplying the two matrices produces a RuntimeWarning.
However, the warning is gone when downgrading numpy to 1.24.4. It also is removed when making a copy of the array and multiplying the original matrix with the copy. (X.T @ X_copy)
importnumpyasnp# Loading the first filenew=np.load('file1.zip')['file1']
# concatenating the other files forfilein ['file2', 'file3']:
x=np.load(f'{file}.zip')[file ]
new=np.concatenate([new, x])
# performing matrix multiplicationnew.T @ new
Error message:
<input>:1: RuntimeWarning: invalid value encountered in matmul
Runtime information:
import sys, numpy; print(numpy.version); print(sys.version)
1.25.0
3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)]
@maciejskorski the Colab notebook gives the same warning <ipython-input-4-fa27106da462>:10: RuntimeWarning: invalid value encountered in matmul new.T @ new when run with numpy version 1.25.0
Thanks, this must come somewhere from the OpenBLAS internals. Probably while deciding things like how to chunk up the job (in theory it could be uninitialized values).
The annoying thing, it would help to recompile openblas with debugging info (and maybe not super high optimization). That is unfortunately only the first step, the second one is to go write a small C program and compile with trapping math (or enable it), or run NumPy but then you have to enable the trapping math after importing NumPy (which is possible, by hacking it in).
Is anyone interested in diving into that? I can share some hack for the second part.
(I have done this type of thing once, but without recompiling openblas so it was a bit hard to really figure out where the issue happens exactly.)
Actually, maybe enabling trapping math (while possible) isn't that helpful... We know approximately where this happens, so stepping though until a NaN pops up somewhere may well be good enough.
Why this has not been detected by mat-mul tests? Don't they check for warnings 🤔
The matrix size that triggers problems depends probably on the architecture, memory and so on? Playing with my notebook, I can see that (29000,270) works fine but (30000,270) triggers errors. @seberg this is what you referred to as "chunking up the job"?
We do see it randomly, but only "reliably" (still randomly) on the win32 CI (if this is the same thing). So CI might well hit it, but maybe only on your machine or so.
It is not completely impossible that this comes from uninintialized values even, or the compiler doing unsafe optimizations...
Or, our test suite just doesn't run the magic size that triggers it realiably here... In either case, we need to find out where it happens, ideally probably in a debugger where you can step through the OpenBLAS code to figure it out precisely. Once that is known, the Openblas fixup is probably easy.
chunking up the job
The BLAS implementation always has a lot of calculations to figure out how to distribute the kernel to its worker threads (and maybe just finding the size of scratch-space needed). The most probably issue is that one of those calculations causes a NaN to be created where it just doesn't matter. For example, by taking the root of a negative number or (or by uising uninitialized memory that happens to be an infinite, or...
EDIT: That said, it could of course also be the kernel itself that uses uninitialized values due to vectorization.
We do see it randomly, but only "reliably" (still randomly) on the win32 CI (if this is the same thing). So CI might well hit it, but maybe only on your machine or so. It is not completely impossible that this comes from uninintialized values even, or the compiler doing unsafe optimizations...
The example I shared is reproducible under Colab's container so this happens on Linux too.
Or, our test suite just doesn't run the magic size that triggers it realiably here... In either case, we need to find out where it happens, ideally probably in a debugger where you can step through the OpenBLAS code to figure it out precisely. Once that is known, the Openblas fixup is probably easy.
chunking up the job
The BLAS implementation always has a lot of calculations to figure out how to distribute the kernel to its worker threads (and maybe just finding the size of scratch-space needed). The most probably issue is that one of those calculations causes a NaN to be created where it just doesn't matter. For example, by taking the root of a negative number or (or by uising uninitialized memory that happens to be an infinite, or...
In this specific case, mat-mul, it is likely chunking the matrix to make it into L2 cache or so?
Would it make sense to test the problematic mat-mul under the "native" OpenBLAS following their docs?
EDIT: That said, it could of course also be the kernel itself that uses uninitialized values due to vectorization.
Why this has not been detected by mat-mul tests? Don't they check for warnings
The easy answer is "it hits a code path we do not hit in tests". The harder question is "what codepath is triggered". I would imagine it is something like #16744, which turned out to be caused by registers not being properly restored. Maybe there is some race condition when chunking the large array.
Can you try limiting OpenBLAS to one thread with threadpoolctl
from threadpoolctl import threadpool_limits
import numpy as np
with threadpool_limits(limits=1, user_api='blas'):
M = np.random.random((30000, 270))
M.T @ M
Darn, I was hoping for a fix of the long time occasional warnings on Windows :) But now I wonder if it is related to Windows having 32 bit default integers?
Just for reference, the Windows problem manifested after the @ operator was directly implemented instead of using einsum.
Here we were able to locate the problem because of reproducing and narrowing the scope in Colab, that others could access independently and comment. How do we debug and reproduce on Windows?
@maciejskorski The problem is that the warning is only occasional, but always in the same test. There is another occasional warning involving complex numbers. Both of those only occur on Windows and don't seem to affect the correctness of the results, they just warn of invalid values.
@maciejskorski The problem is that the warning is only occasional, but always in the same test. There is another occasional warning involving complex numbers. Both of those only occur on Windows and don't seem to affect the correctness of the results, they just warn of invalid values.
Is this a numpy test on CI/CD? Occasionally, because the data is randomized or because the algorithm is not fully deterministic (for various technical reasons, like parallelization)? Could we track and eventually (once spotted) share the data triggering the error?
@martin-frbg I don't know. I've been shrugging them off as probably a platform/compiler problem. Going forward, I am going to start tracking them in an issue. They only occur a couple of times a month, and seem less frequent than they used to be, but that is very subjective.
I think so, I'm currently trying to coordinate with xianyi after he made one of his ninja commits but I expect any immediate action will be limited to risc-v
Activity
charris commentedon Jun 28, 2023
We see that occasionally on Windows, it doesn't seem repeatable. Are you using a 32 bit version of NumPy?
hbombDTU commentedon Jun 29, 2023
Hi Charris,
I believe we are using 64 bit version of NumPy.
maciejskorski commentedon Jun 29, 2023
I can't reproduce either, under Colab is fine. See / try on this notebook.
StefRe commentedon Jun 29, 2023
@maciejskorski the Colab notebook gives the same warning
<ipython-input-4-fa27106da462>:10: RuntimeWarning: invalid value encountered in matmul new.T @ newwhen run with numpy version1.25.0maciejskorski commentedon Jun 29, 2023
@StefRe thanks for testing the breaking version.
I also tested that the matrix content is irrelevant, the error is trigerred by this:
So we have our minimal-not-working-example! (MNWE) 👍
seberg commentedon Jun 29, 2023
Thanks, this must come somewhere from the OpenBLAS internals. Probably while deciding things like how to chunk up the job (in theory it could be uninitialized values).
The annoying thing, it would help to recompile openblas with debugging info (and maybe not super high optimization). That is unfortunately only the first step, the second one is to go write a small C program and compile with trapping math (or enable it), or run NumPy but then you have to enable the trapping math after importing NumPy (which is possible, by hacking it in).
Is anyone interested in diving into that? I can share some hack for the second part.
(I have done this type of thing once, but without recompiling openblas so it was a bit hard to really figure out where the issue happens exactly.)
seberg commentedon Jun 29, 2023
Actually, maybe enabling trapping math (while possible) isn't that helpful... We know approximately where this happens, so stepping though until a
NaNpops up somewhere may well be good enough.maciejskorski commentedon Jun 29, 2023
Why this has not been detected by mat-mul tests? Don't they check for warnings 🤔
The matrix size that triggers problems depends probably on the architecture, memory and so on? Playing with my notebook, I can see that (29000,270) works fine but (30000,270) triggers errors. @seberg this is what you referred to as "chunking up the job"?
seberg commentedon Jun 29, 2023
We do see it randomly, but only "reliably" (still randomly) on the win32 CI (if this is the same thing). So CI might well hit it, but maybe only on your machine or so.
It is not completely impossible that this comes from uninintialized values even, or the compiler doing unsafe optimizations...
Or, our test suite just doesn't run the magic size that triggers it realiably here... In either case, we need to find out where it happens, ideally probably in a debugger where you can step through the OpenBLAS code to figure it out precisely. Once that is known, the Openblas fixup is probably easy.
The BLAS implementation always has a lot of calculations to figure out how to distribute the kernel to its worker threads (and maybe just finding the size of scratch-space needed). The most probably issue is that one of those calculations causes a NaN to be created where it just doesn't matter. For example, by taking the root of a negative number or (or by uising uninitialized memory that happens to be an infinite, or...
EDIT: That said, it could of course also be the kernel itself that uses uninitialized values due to vectorization.
maciejskorski commentedon Jun 29, 2023
The example I shared is reproducible under Colab's container so this happens on Linux too.
In this specific case, mat-mul, it is likely chunking the matrix to make it into L2 cache or so?
Would it make sense to test the problematic mat-mul under the "native" OpenBLAS following their docs?
mattip commentedon Jun 29, 2023
The easy answer is "it hits a code path we do not hit in tests". The harder question is "what codepath is triggered". I would imagine it is something like #16744, which turned out to be caused by registers not being properly restored. Maybe there is some race condition when chunking the large array.
mattip commentedon Jun 29, 2023
Can you try limiting OpenBLAS to one thread with
threadpoolctlmattip commentedon Jun 29, 2023
... answering my own question:
mattip commentedon Jun 29, 2023
17 remaining items
charris commentedon Jun 29, 2023
Darn, I was hoping for a fix of the long time occasional warnings on Windows :) But now I wonder if it is related to Windows having 32 bit default integers?
charris commentedon Jun 29, 2023
Just for reference, the Windows problem manifested after the
@operator was directly implemented instead of using einsum.maciejskorski commentedon Jun 29, 2023
Here we were able to locate the problem because of reproducing and narrowing the scope in Colab, that others could access independently and comment. How do we debug and reproduce on Windows?
charris commentedon Jun 29, 2023
@maciejskorski The problem is that the warning is only occasional, but always in the same test. There is another occasional warning involving complex numbers. Both of those only occur on Windows and don't seem to affect the correctness of the results, they just warn of invalid values.
maciejskorski commentedon Jun 29, 2023
Is this a numpy test on CI/CD? Occasionally, because the data is randomized or because the algorithm is not fully deterministic (for various technical reasons, like parallelization)? Could we track and eventually (once spotted) share the data triggering the error?
martin-frbg commentedon Jun 29, 2023
Is it reasonable to assume the other warning is related to something in OpenBLAS as well ?
charris commentedon Jun 29, 2023
@martin-frbg I don't know. I've been shrugging them off as probably a platform/compiler problem. Going forward, I am going to start tracking them in an issue. They only occur a couple of times a month, and seem less frequent than they used to be, but that is very subjective.
mattip commentedon Jun 30, 2023
Is the OpenBLAS develop branch stable enough to update openblas-libs to fix this issue?
martin-frbg commentedon Jun 30, 2023
I think so, I'm currently trying to coordinate with xianyi after he made one of his ninja commits but I expect any immediate action will be limited to risc-v
hbombDTU commentedon Jul 11, 2023
Could I get the current status for this bug?
mattip commentedon Jul 11, 2023
I tried building the OpenBLAS version in MacPython/openblas-libs#101 but the macos arm64 build failed to recognize the new SVE intrinsics.
mattip commentedon Jul 17, 2023
#24199 updates OpenBLAS to a version with a fix for this.
fix: use np.dot to avoid spurious numpy 2.0.x matmul warnings
fix: use np.dot to avoid spurious numpy 2.0.x matmul warnings