GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #28687

New issue
Copy link
New issue
Copy link
Closed
#29223
Closed
BUG: wrong errors e.g. "divide by zero encountered in matmul" on MacOS M4#28687
#29223
Copy link
Labels
00 - Bug

Description

@malciin
malciin
opened on Apr 10, 2025
Issue body actions

Current 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

Activity

Next
  1. malciin
    added
    00 - Bug
    on Apr 10, 2025
  2. malciin
    changed 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, 2025
  3. tylerjereddy commented on Apr 10, 2025

    @tylerjereddy
    tylerjereddy
    on Apr 10, 2025
    Contributor
    More actions

    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.

  4. malciin commented on Apr 10, 2025

    @malciin
    malciin
    on Apr 10, 2025
    Author
    More actions

    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:

    Image

    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)

  5. ngoldbaum commented on Apr 11, 2025

    @ngoldbaum
    ngoldbaum
    on Apr 11, 2025
    Member
    More actions

    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?

  6. malciin
    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, 2025
  7. t-muser commented on Apr 14, 2025

    @t-muser
    t-muser
    on Apr 14, 2025
    More actions

    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
  8. seberg commented on Apr 14, 2025

    @seberg
    seberg
    on Apr 14, 2025
    Member
    More actions

    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
  9. kenshi84 commented on Apr 18, 2025

    @kenshi84
    kenshi84
    on Apr 18, 2025
    More actions

    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
  10. ngoldbaum commented on Apr 18, 2025

    @ngoldbaum
    ngoldbaum
    on Apr 18, 2025
    Member
    More actions

    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.

  11. Developer-Ecosystem-Engineering commented on Apr 18, 2025

    @Developer-Ecosystem-Engineering
    Developer-Ecosystem-Engineering
    on Apr 18, 2025
    Contributor
    More actions

    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
  12. glenn-jocher commented on Apr 22, 2025

    @glenn-jocher
    glenn-jocher
    on Apr 22, 2025
    More actions

    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
  13. zertez commented on Apr 29, 2025

    @zertez
    zertez
    on Apr 29, 2025
    More actions

    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
  14. seberg commented on Apr 29, 2025

    @seberg
    seberg
    on Apr 29, 2025
    Member
    More actions

    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
  15. nalzok commented on Apr 30, 2025

    @nalzok
    nalzok
    on Apr 30, 2025
    More actions

    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
    
    Reacted by malciin
  16. 17 remaining items

  17. cournape
    added a commit that references this issue on Jun 24, 2025

    MAINT: update min version of python to 3.11.

    389090a
  18. joy2myself
    added a commit that references this issue on Jul 21, 2025

    ENH: Convert unary from C universal intrinsics to C++ using Highway (#19

    3f5dccf
  19. ziofil commented on Jul 25, 2025

    @ziofil
    ziofil
    on Jul 25, 2025
    More actions

    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?

  20. ngoldbaum commented on Jul 25, 2025

    @ngoldbaum
    ngoldbaum
    on Jul 25, 2025
    Member
    More actions

    I think you'll need NumPy 2.3 or newer.

    Reacted by zarkitapple
    Reacted by Anthony4Work and zarkitapple
  21. Anthony4Work commented on Sep 3, 2025

    @Anthony4Work
    Anthony4Work
    on Sep 3, 2025
    More actions

    live saving moment, updating the version to 2.3.1 fixed the issue. Thanks!

  22. maresb commented on Sep 26, 2025

    @maresb
    maresb
    on Sep 26, 2025
    More actions

    We are still seeing this with NumPy v2.3.3 on M4 with Sequoia 15.6.1 (24G90). details

    Reacted by Jared Jeyaretnam
  23. mattip commented on Sep 27, 2025

    @mattip
    mattip
    on Sep 27, 2025
    Member
    More actions

    @maresb please open a new issue

    Reacted by Ben Mares
  24. jsdodge
    added a commit that references this issue on Dec 24, 2025

    Add conftest.py to filter out erroneous warnings when using numpy < 2…

    144aaa8
  25. arcondello
    added a commit that references this issue on Jan 8, 2026

    Skip sklearn tests with NumPy < 2.3.0

    e331607
  26. nosracd
    added 2 commits that reference this issue on Feb 13, 2026

    refactor numpy to be a range

    a165c10

    refactor numpy to be a range

    d70da88
  27. rgeronimi
    added a commit that references this issue on May 28, 2026

    sigma: silence spurious matmul RuntimeWarnings (numpy/numpy#28687) in…

    c4c9d60
  28. LudoRey
    added 2 commits that reference this issue on Aug 9, 2026

    build: upgrade numpy to 2.3.1

    28c9f42

    build: upgrade numpy to 2.3.1

    ee6d047
  29. gianwario
    added a commit that references this issue on Sep 30, 2026

    Second round revision: explicit floating-point policy and undefined f…

    a5bc642
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    00 - Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.