apple / tensorflow_macos

TensorFlow for macOS 11.0+ accelerated using Apple's ML Compute framework.
Other
3.66k stars 308 forks source link

zsh: illegal hardware instruction python when Installing TensorFlow for M1 #143

Open MalteSil opened 3 years ago

MalteSil commented 3 years ago

I've followed these steps :

I'm using Python 3.8 and I'm running my terminal not rosetta: (base) user@useros ~ % uname -m
arm64 When I activate my environment I still get: (venv) (base) user@useros ~ % uname -m arm64

If I run file $(which python) I get the following: On base: (base) user@useros ~ % file $(which python) /Users/user/opt/anaconda3/bin/python: Mach-O 64-bit executable x86_64

On venv: (venv) (base) user@useros ~ % file $(which python) /Users/user/venv/bin/python: Mach-O 64-bit executable x86_64

Why do I get 'Mach-O 64-bit executable x86_64' here even though I get 'arm64' before? This confuses me and is probably the reason why my code is not working.

andy-96 commented 3 years ago

So, I got it working yesterday after a long time of similar problems. What helped me was the following:

  1. Use the python 3.8 version that was preinstalled by Apple, which should be python 3.8.2.
  2. Deactivate conda conda deactivate
  3. Create new virtualenv by python3 -m venv .venv
  4. Change all files in tensorflow_macos/arm64 from *_11_0_*.whl to *_10_16_*.whl
  5. Change all filenames in install_venv.sh to *_10_16*.whl
  6. Run install_venv.sh

Then it should work! I hope that helps!


Edit: I just forked the repo and added my own instructions on how to properly install it for future reference. Maybe that will help you more: https://github.com/andy-96/tensorflow_macos

MalteSil commented 3 years ago

Thank you! My Conda environment was indeed active, will try it later. What is the reason behind changing the files and filenames?

andy-96 commented 3 years ago

Tbh, I don't know...

Alyetama commented 3 years ago

Python3 is shipped with 2 architectures in M1.

$ file $(which python3)
/usr/bin/python3: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64e:Mach-O 64-bit executable arm64e]
/usr/bin/python3 (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/python3 (for architecture arm64e): Mach-O 64-bit executable arm64e

Here, you might need to specify which one to use. You can do this by installing the script like this:

arch -arm64 bash install_venv.sh my_tf_env

if you have multiple python installations, use:

arch -arm64 bash install_venv.sh --python=/usr/bin/python3 my_tf_env

You can replace my_tf_env with any other name/path you choose.

chinitaberrio commented 3 years ago

@Alyetama is right, I got this issue fixed in my repo, just make sure you have installed Xcode (download the latest version and install it): https://developer.apple.com/download/more/?=command%20line%20tools

Then in a terminal paste /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/chinitaberrio/tensorflow_macos/master/scripts/download_and_install.sh)" That should fix the issue :)

the1gofer commented 3 years ago

uninstalling tensorflow worked for me

Prashant-Kesharwani commented 3 years ago

You can install all the following modules by creating a Virtual Environment, and then installing all these modules inside that Virtual Environment.

1. Install Matplotlib

1.1 Install Homebrew Type in your terminal (not in rosetta environment):

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" Then add homebrew's path to the variable path:

echo 'eval $(/opt/homebrew/bin/brew shellenv)' >> /Users/bcghostpc/.zprofile eval $(/opt/homebrew/bin/brew shellenv)

1.2 Install libjpeg

brew install libjpeg

1.3 Install Matplotlib

In the virtual environment, type:

pip install matplotlib

2. Install tensorflow

Type in terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/apple/tensorflow_macos/master/scripts/download_and_install.sh)" then name your virtual environment's folder

3. Install pandas

In your virtual environment, type:

pip install cython git clone https://github.com/pandas-dev/pandas.git cd pandas python3 setup.py install

4. Install jupyter notebook

In your virtual environment, type:

pip install notebook Then go to lib/python3.8/site-packages/ipykernel

Edit the eventloops.py file using nano eventloops.py

In the _use_appnope() function, edit the return line as follows:

return sys.platform == 'darwin' and V(platform.mac_ver()[0]) >= V('10.9') and platform.mac_ver()[2] != 'arm64'

Appendix

About Installing Different Modules You can install any module using pip inside Virtual Environment:

pip install <moduleName>

About Virtual Environment

You can create a virtual environment by:

python3 -m venv <name> After creating the environment, you can activate the environment by:

source <name>/bin/activate To exit the environment, type:

deactivate

the1gofer commented 3 years ago

I'll give that a shot.

There might also be a fix by upgrading to this tensorflow alpha, but I haven't trie dit because it requires that I down grade to 3.8 of python.

https://github.com/apple/tensorflow_macos