GeezerVison is showing github.com — BUG: wrong errors e.g. "divide by zero encountered in matmul" on MacOS M4 · Issue #28687 · numpy/numpy
BUG: wrong errors e.g. "divide by zero encountered in matmul" on MacOS M4 #28687DescriptionCurrent status
Apple is aware of this issue, see this comment below. Hopefully an OS update will have a fix soon. If you are experiencing this issue on a Mac with an M4 CPU it is not necessary to comment on this thread about it.
Describe the issue:
I guess its self explanatory. Following code:
np.identity(n=784) @ np.identity(n=784)
produces many wrong runtime warnings.
For small matrices is works as expected, for eg. np.identity(14) @ np.identity(14) doesn't produce warning but np.identity(15) @ np.identity(15) and bigger does.
Reproduce the code example:
import numpy as np
np.identity(n=15) @ np.identity(n=15) # produces warning
np.identity(n=14) @ np.identity(n=14) # no warning
# same result for
import numpy as np
np.ones(shape=(1, 2)) @ np.ones(shape=(2, 784))
Error message:
../test.py:3: RuntimeWarning: divide by zero encountered in matmul
np.identity(n=784) @ np.identity(n=784)
../test.py:3: RuntimeWarning: overflow encountered in matmul
np.identity(n=784) @ np.identity(n=784)
../test.py:3: RuntimeWarning: invalid value encountered in matmul
np.identity(n=784) @ np.identity(n=784)
Python and NumPy Versions:
2.2.4
3.13.2 | packaged by Anaconda, Inc. | (main, Feb 6 2025, 12:55:35) [Clang 14.0.6 ]
I've checked also 3.11 & 3.9 but same result.
Runtime Environment:
No response
Context for the issue:
No response Reacted by Nick ODell, Kenshi Takayama, Kelly Buchanan, Glenn Jocher, Anton, Qingyao Sun, gMatas, Lukas Bühler, Daniel Kofler, Dan Raviv and 9 more Reacted by Gavin Wiggins, Kenshi Takayama, Glenn Jocher, gMatas, Filippo Miatto and Kazuki Fujii Activitychanged the title [-]BUG: wrong errors "divide by zero encountered in matmul" on MacOS[/-] [+]BUG: wrong errors e.g. "divide by zero encountered in matmul" on MacOS[/+] on Apr 10, 2025tylerjereddy commented on Apr 10, 2025 FWIW, I don't see the issue on latest main on MacOS with accelerate backend nor latest main on x86_64 Linux with OpenBLAS backend:
--- a/numpy/linalg/tests/test_linalg.py
+++ b/numpy/linalg/tests/test_linalg.py
@@ -2406,3 +2406,12 @@ def test_vector_norm_empty():
assert_equal(np.linalg.vector_norm(x, ord=1), 0)
assert_equal(np.linalg.vector_norm(x, ord=2), 0)
assert_equal(np.linalg.vector_norm(x, ord=np.inf), 0)
+
+
+
+@pytest.mark.parametrize("arr1, arr2", [
+ (np.identity(n=784), np.identity(n=784)),
+ (np.identity(n=14), np.identity(n=14)),
+])
+def test_gh_28687(arr1, arr2):
+ arr1 @ arr2
Incantation: spin test -t numpy/linalg/tests/test_linalg.py::test_gh_28687 -- -s
Maybe someone with a conda setup on Mac will reproduce more easily if this is just for a standard conda install. Sharing the contents of np.show_config() may also help. malciin commented on Apr 10, 2025 Result of np.show_config() run in miniconda env
Build Dependencies:
blas:
detection method: system
found: true
include directory: unknown
lib directory: unknown
name: accelerate
openblas configuration: unknown
pc file directory: unknown
version: unknown
lapack:
detection method: system
found: true
include directory: unknown
lib directory: unknown
name: accelerate
openblas configuration: unknown
pc file directory: unknown
version: unknown
Compilers:
c:
commands: cc
linker: ld64
name: clang
version: 15.0.0
c++:
commands: c++
linker: ld64
name: clang
version: 15.0.0
cython:
commands: cython
linker: cython
name: cython
version: 3.0.12
Machine Information:
build:
cpu: aarch64
endian: little
family: aarch64
system: darwin
host:
cpu: aarch64
endian: little
family: aarch64
system: darwin
Python Information:
path: /private/var/folders/0j/bwqcs4y508s2n4ck4dhf3rpc0000gn/T/build-env-310vy4bc/bin/python
version: '3.13'
SIMD Extensions:
baseline:
- NEON
- NEON_FP16
- NEON_VFPV4
- ASIMD
found:
- ASIMDHP
not found:
- ASIMDFHM
But I believe its not related to conda. I can also reproduce it on default python3 installation on my mac:

Result of np.show_config() for default python3 instalation:
{
"Compilers": {
"c": {
"name": "clang",
"linker": "ld64",
"version": "15.0.0",
"commands": "cc"
},
"cython": {
"name": "cython",
"linker": "cython",
"version": "3.0.11",
"commands": "cython"
},
"c++": {
"name": "clang",
"linker": "ld64",
"version": "15.0.0",
"commands": "c++"
}
},
"Machine Information": {
"host": {
"cpu": "aarch64",
"family": "aarch64",
"endian": "little",
"system": "darwin"
},
"build": {
"cpu": "aarch64",
"family": "aarch64",
"endian": "little",
"system": "darwin"
}
},
"Build Dependencies": {
"blas": {
"name": "accelerate",
"found": true,
"version": "unknown",
"detection method": "system",
"include directory": "unknown",
"lib directory": "unknown",
"openblas configuration": "unknown",
"pc file directory": "unknown"
},
"lapack": {
"name": "accelerate",
"found": true,
"version": "unknown",
"detection method": "system",
"include directory": "unknown",
"lib directory": "unknown",
"openblas configuration": "unknown",
"pc file directory": "unknown"
}
},
"Python Information": {
"path": "/private/var/folders/4d/0gnh84wj53j7wyk695q0tc_80000gn/T/build-env-kq4kuj35/bin/python",
"version": "3.9"
},
"SIMD Extensions": {
"baseline": [
"NEON",
"NEON_FP16",
"NEON_VFPV4",
"ASIMD"
],
"found": [
"ASIMDHP"
],
"not found": [
"ASIMDFHM"
]
}
}
>>> import sys, numpy; print(numpy.__version__); print(sys.version)
2.0.2
3.9.6 (default, Mar 12 2025, 20:22:46)
[Clang 17.0.0 (clang-1700.0.13.3)]
Environment: Macbook air M4 with currently newest macos (sequoia 15.4) ngoldbaum commented on Apr 11, 2025 I don't see any warnings on my M3 Macbook Pro using a pyenv python compiled from source and numpy compiled from source, or on homebrew's python and numpy builds.
Maybe this is specific to the M4 CPU? changed the title [-]BUG: wrong errors e.g. "divide by zero encountered in matmul" on MacOS[/-] [+]BUG: wrong errors e.g. "divide by zero encountered in matmul" on MacOS M4[/+] on Apr 11, 2025t-muser commented on Apr 14, 2025 I can confirm that I also ran into this problem on an M4 Mac with numpy==2.2.4 using uv as package manager (which I think gets it's packages from PyPI. No warnings are produced if I do any of the following:
- downgrade to numpy
1.26.4
- use
np.dot instead of matmul
- smaller matrices
Reacted by malciin, Glenn Jocher, Mark Andersen, WLMike, Florian Lalande, sashless, asvdv, Joseph Powell Ph.D., Poeticphysicist and PENGUINLIONG seberg commented on Apr 14, 2025 Ping @Developer-Ecosystem-Engineering, since this seems to probably originate in Accelerate and I don't think we can do anything about it.
use np.dot instead of matmul
Dot didn't report these, but we just changed that it should in 2.3, although if this is a common issue by then, I guess we may want to reconsider. Reacted by Developer-Ecosystem-Engineering kenshi84 commented on Apr 18, 2025 My computer is MacBook Pro (16-inch, Nov 2024) with Apple M4 Max running macOS 15.4.1, and I was able to reproduce the same issue (using both system and pyenv installed Python 3.9 & 3.12, NumPy 2.2.4). Furthermore, on the same machine I used UTM.app to create a virtual machine with macOS 15.3.2 and confirmed that it is not reproduced. So I suspect this issue is caused by something that was newly intreoduced in the latest macOS Sequoia 15.4.x. Reacted by malciin and WLMike ngoldbaum commented on Apr 18, 2025 Maybe filing an official apple radar bug to Apple would help?
If someone with an M4 chip could verify that the spurious floating point exception is coming from accelerate that would also help. Developer-Ecosystem-Engineering commented on Apr 18, 2025 Hi @ngoldbaum, and @seberg,
We are tracking this issue and are working through understanding exactly what is going on, if it's expected, what we can do about it short term (for NumPy) and long term. I apologize that the internal discussion & ideation isn't a part of the public discourse and how that seems like we aren't engaged on the issue.
We will circle back next week with an update. Reacted by t-muser, Nathan Goldbaum, Sebastian Berg, Kelly Buchanan, Glenn Jocher, Ralf Gommers, Lauri Kovanen, terranceye, Anton, Davide Marchini and 6 more glenn-jocher commented on Apr 22, 2025 Also seeing this on an M4 Macbook with latest 2.2.5 installed with pip in Python 3.12. Reacted by malciin, Anton, Davide Marchini, gMatas, ryn-mrgn, David Marttila, Lukas Kiss, WLMike and Vipin Sharma zertez commented on Apr 29, 2025 Well this is annoying. I am currently enrolled in a machine learning course at uni and I just got a warning from the teacher on an assignment I delivered. It ended up being a 397 page pdf, where 389 of those pages were just this printout warning. It seems that the warning supression that I used in python doesn't work when used in a jupyter notebook. I can see that Apple is looking int to this, but I can't really wait for a fix, so I have been doing a lot of testing to find a workaround for this issue, since exams starts in two weeks.
I am using a macbook pro 16 with the M4 max, on 15.4.1. I am not using anaconda, I am using uv with a venv setup through uv. Using python 3.10.8.
This is what I have found:
The bug or error only affects the @ operator when working with float values. There are no errors with integers. This makes sense since NumPy delegates floating-point matrix operations to the BLAS library, which uses Apple's Accelerate framework. Both np.dot() and the @ operator should theoretically use BLAS for floating-point operations, but they appear to use different code paths or interfaces to the BLAS libraries. The np.dot() function works correctly for both floats and integers, suggesting it might be accessing underlying libraries differently or handling errors more robustly than the @ operator implementation. This suggests the issue is specific to how NumPy's @ operator interfaces with Apple's implementation of BLAS on Apple Silicon hardware, but only on the M4 chip it would seem...
The workaround:
So by I having narrowed it down to BLAS (which I can see others have also done here), I wanted to setup OpenBLAS, to check if this resolved the issue. After using brew to install OpenBLAS, I ran this code in terminal window:
OPENBLAS=/opt/homebrew/opt/openblas uv pip install numpy --no-binary numpy --config-setting setup-args=-Dblas=openblas --config-setting setup-args=-Dlapack=openblas
After restarting the kernel, numpy is now using OpenBLAS and I am no longer getting these warnings. Hope this might help some others as a temporary fix until Apple has resolved the issue. I am certain there are other ways of changing to OpenBLAS, but this is the method that worked for me in the end. Reacted by Sebastian Berg, malciin, mlescobarm, Alistair Reid and Zeqing Wang seberg commented on Apr 29, 2025 FWIW, if this is just a warning in @ for you (and not elsewhere), just setting np.errstate(all="ignore") should work.
(It will hide all similar warnings, but if you are not working interactively, you are unlikely to care.) Reacted by Marcus Dalaker Figenschou and ryn-mrgn nalzok commented on Apr 30, 2025 I can reproduce the bug on M4 Pro. Perhaps it's due to the latest macOS 15.4.1 system update?
> sw_vers
ProductName: macOS
ProductVersion: 15.4.1
BuildVersion: 24E263
> sysctl -a | grep machdep.cpu
machdep.cpu.cores_per_package: 14
machdep.cpu.core_count: 14
machdep.cpu.logical_per_package: 14
machdep.cpu.thread_count: 14
machdep.cpu.brand_string: Apple M4 Pro
> python3 -c 'import numpy as np; np.identity(n=14) @ np.identity(n=14)'
> python3 -c 'import numpy as np; np.identity(n=15) @ np.identity(n=15)'
<string>:1: RuntimeWarning: divide by zero encountered in matmul
<string>:1: RuntimeWarning: overflow encountered in matmul
<string>:1: RuntimeWarning: invalid value encountered in matmul
Here is the output from np.show_config()
Build Dependencies:
blas:
detection method: system
found: true
include directory: unknown
lib directory: unknown
name: accelerate
openblas configuration: unknown
pc file directory: unknown
version: unknown
lapack:
detection method: system
found: true
include directory: unknown
lib directory: unknown
name: accelerate
openblas configuration: unknown
pc file directory: unknown
version: unknown
Compilers:
c:
commands: cc
linker: ld64
name: clang
version: 15.0.0
c++:
commands: c++
linker: ld64
name: clang
version: 15.0.0
cython:
commands: cython
linker: cython
name: cython
version: 3.0.12
Machine Information:
build:
cpu: aarch64
endian: little
family: aarch64
system: darwin
host:
cpu: aarch64
endian: little
family: aarch64
system: darwin
Python Information:
path: /private/var/folders/0j/bwqcs4y508s2n4ck4dhf3rpc0000gn/T/build-env-mgxqnezl/bin/python
version: '3.12'
SIMD Extensions:
baseline:
- NEON
- NEON_FP16
- NEON_VFPV4
- ASIMD
found:
- ASIMDHP
not found:
- ASIMDFHM
ziofil commented on Jul 25, 2025 From what I can read on this thread (and here) this issue should be resolved? But I am still experiencing it on an M4 MBA (Sequoia 15.5), numpy 2.0.2. Am I on the wrong version? ngoldbaum commented on Jul 25, 2025 I think you'll need NumPy 2.3 or newer. Reacted by zarkitapple Reacted by Anthony4Work and zarkitapple Anthony4Work commented on Sep 3, 2025 live saving moment, updating the version to 2.3.1 fixed the issue. Thanks! maresb commented on Sep 26, 2025 We are still seeing this with NumPy v2.3.3 on M4 with Sequoia 15.6.1 (24G90). details Reacted by Jared Jeyaretnam mattip commented on Sep 27, 2025
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|