14

I am using a MacBook Pro with M1 processor, macOS version 11.0.1, Python 3.8 in PyCharm, Tensorflow version 2.4.0rc4 (also tried 2.3.0, 2.3.1, 2.4.0rc0). I am trying to run the following code:

import tensorflow

This causes the error message:

Process finished with exit code 132 (interrupted by signal 4: SIGILL)

The code runs fine on my Windows and Linux machines. What does the error message mean and how can I fix it?

4
  • 2
    SIGILL means Illegal Instruction, at the assembly level, so I don't think this is something we can fix, you should file a bug with the provider of your TensorFlow version (is it Apple's or the official TF wheels?)
    – Dr. Snoopy
    Dec 11, 2020 at 0:14
  • @Dr.Snoopy I download Tensorflow using pip from pypi.python.org. I don't know if that means I'm using Apple's or the official TF wheels.
    – user505117
    Dec 11, 2020 at 10:07
  • I also tried the TF version that I get from following the instructions in the "tensorflow_macos" repository on github: github.com/apple/tensorflow_macos . Maybe that is what you mean by Apple's version. The installation was fine, but the error remains the same.
    – user505117
    Dec 11, 2020 at 10:20
  • 1
    This solution worked for me: github.com/apple/tensorflow_macos/issues/65
    – user505117
    Dec 17, 2020 at 9:32

6 Answers 6

6

Seems that this problem happens when you have multiple python interpreters installed, and some of them are for differente architectuers (x86_64 vs arm64). You need to make sure that the correct python interpreter is being used, if you installed Apple's version of tensorflow, then that probably requires an arm64 interpreter.

If you use rosetta (Apple's x86_64 emulator) then you need to use a x86_64 python interpreter, if you somehow load the arm64 python interpreter, you will get the illegal instruction error (which totally makes sense).

If you use any script that installs new python interpreters, then you need to make sure the correct interpreter for the architecture is installed (most likely arm64).

Overalll I think this problem happens because the python environment setup is not made for systems that can run multiple instruction sets/architectures, pip does check the architecture of packages and the host system but seems you can run a x86_64 interpreter to load a package meant for arm64 and this produces the problem.

For reference there is an issue in tensorflow_macos that people can check.

3
  • Thank you. The problem was that I used a Rosetta terminal to try to run my arm64 tensorflow code. How do I check whether my Python interpreter is x86_64 or arm64? And how can I tell pip to download x86_64 packages? I would like to go all x86_64, but I don't know how. Should I create this as a new question?
    – user505117
    Dec 25, 2020 at 16:51
  • @user505117 That is complicated because as my answer says, the python interpreter is not aware of both architectures, only one. So be very careful about using rosetta and native python interpreters.
    – Dr. Snoopy
    Jan 22, 2021 at 0:03
  • @user505117 you can check the architecture through this code. >> import platform >> platform.platform() 'macOS-10.16-x86_64-i386-64bit'
    – Qazi Ammar
    Jul 13, 2021 at 12:24
3

Installing Tensorflow version 1.15 fixed this for me.

$ conda install tensorflow==1.15
3

For M1 Macs, From Apple developer page the following worked:

First, download Conda Env from here and then follow these instructions (assuming the script is downloaded to ~/Downloads folder)

chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh
sh ~/Downloads/Miniforge3-MacOSX-arm64.sh
source ~/miniforge3/bin/activate

reload the shell and do

python -m pip uninstall tensorflow-macos
python -m pip uninstall tensorflow-metal

conda install -c apple tensorflow-deps

python -m pip install tensorflow-macos
python -m pip install tensorflow-metal

If the above doesn't work for some reason, there are some edge cases and additional information provided at the Apple developer page

1
  • tensorflow-deps has been removed :/
    – duhaime
    Mar 17 at 17:59
2

In my case, the error was

zsh: illegal hardware instruction  python

Putting my answer here, since the question with the mentioned error ("zsh: illegal hardware instruction python" when installing Tensorflow on macbook pro M1) is marked a duplicate of this post.

So, in my case installing tensorflow-macos and tensorflow-metal was the resolution. Now, they can be installed via pip. But running pip install tensorflow-macos gave me the following error

ERROR: Could not find a version that satisfies the requirement tensorflow-macos (from versions: none) 
ERROR: No matching distribution found for tensorflow-macos

So, following the post on the apple forum here, I had to do the following

SYSTEM_VERSION_COMPAT=0 pip install tensorflow-macos tensorflow-metal
0
1

I have been able to resolve this issue by using Miniforge instead of Anaconda as the Python environment. Anaconda doesn't support the arm64 architecture, yet.

0

This is because of M1 chip. Now there is a pre-release that delivers hardware-accelerated TensorFlow and TensorFlow Addons for macOS 11.0+. Native hardware acceleration is supported on M1 Macs and Intel-based Macs through Apple’s ML Compute framework.

You need to install the TensorFlow that supports M1 chip Simply pull this tensorflow macos repository and run the ./scripts/download_and_install.sh

Not the answer you're looking for? Browse other questions tagged or ask your own question.