Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M1 Mac compatibility #579

Closed
naren-ramesh opened this issue Aug 16, 2021 · 15 comments
Closed

M1 Mac compatibility #579

naren-ramesh opened this issue Aug 16, 2021 · 15 comments

Comments

@naren-ramesh
Copy link

Hi! I followed the instructions on the SLEAP page for installing SLEAP, creating a new conda environment with Python 3.6 and installing sleap using pip. However, when I run sleap-label, I get the following error: "illegal hardware instruction sleap-label" I suspect this has something to do with the fact that I use an M1 MacBook Pro. Is anyone else running into this issue and if so, how were you able to resolve it?

Thanks for the help!

@talmo
Copy link
Collaborator

talmo commented Aug 16, 2021

@ariematsliah-princeton any tips?

@arie-matsliah
Copy link
Contributor

@ariematsliah-princeton any tips?

No unfortunately I was not able to run SLEAP on m1. Even the pyside libraries can't be installed on mine.

@BearConnorsParadiso
Copy link

I guess M1 supports python versions of 3.8 or higher only, so 3.6 package is not supported currently.

Would it be possible to use 3.8 or 3.9 for SLEAP?

@arie-matsliah
Copy link
Contributor

I wasn't able to make it work on 6 months old M1, but please give it a try and update if you've found a workaround.

@BearConnorsParadiso
Copy link

I tried my best to utilize M1 for the past few days with installation of SLEAP, yet still stuck with the python version. :(

@talmo
Copy link
Collaborator

talmo commented Sep 2, 2021

Hi @BearConnorsParadiso,

What happens when you try to pip install sleap in a Python 3.8 environment? Do you get any errors?

I've been able to run it in Python 3.7 on Linux and Windows, but I think it should still be ok on 3.8. If not, let me know what kinds of error messages you're getting and we can try some stuff out from there.

Cheers,

Talmo

@BearConnorsParadiso
Copy link

For python versions of 3.6 and 3.7, I get (personal guess is that in order to run anaconda on M1 it has to go through alternatives like miniforge, and it seems the miniforge channel owns python of 3.8 and higher only)

__PackagesNotFoundError: The following packages are not available from current channels:

  • python=3.6

Current channels:

To search for alternate channels that may provide the conda package you're
looking for, navigate to

https://anaconda.org

and use the search bar at the top of the page.__

In case of conda create -n sleap python=3.8 -y (I got hundreds of other error lines before the attached error but below seems to be the major one)

_ERROR: Cannot install sleap==1.0.0, sleap==1.0.1, sleap==1.0.10, sleap==1.0.2, sleap==1.0.3, sleap==1.0.4, sleap==1.0.5, sleap==1.0.6, sleap==1.0.7, sleap==1.0.8, sleap==1.0.9, sleap==1.1.0, sleap==1.1.1, sleap==1.1.2, sleap==1.1.3, sleap==1.1.4 and sleap==1.1.5 because these package versions have conflicting dependencies.

The conflict is caused by:
sleap 1.1.5 depends on PySide2==5.14.1
sleap 1.1.4 depends on PySide2==5.14.1
sleap 1.1.3 depends on PySide2==5.14.1
sleap 1.1.2 depends on PySide2==5.14.1
sleap 1.1.1 depends on PySide2==5.14.1
sleap 1.1.0 depends on PySide2==5.14.1
sleap 1.0.10 depends on tensorflow==2.1.2
sleap 1.0.9 depends on PySide2<=5.14.1 and >=5.12.0
sleap 1.0.8 depends on PySide2<=5.14.1 and >=5.12.0
sleap 1.0.7 depends on PySide2<=5.14.1 and >=5.12.0
sleap 1.0.6 depends on PySide2<=5.14.1 and >=5.12.0
sleap 1.0.5 depends on PySide2<=5.14.1 and >=5.12.0
sleap 1.0.4 depends on PySide2<=5.14.1 and >=5.12.0
sleap 1.0.3 depends on PySide2<=5.14.1 and >=5.12.0
sleap 1.0.2 depends on PySide2<=5.14.1 and >=5.12.0
sleap 1.0.1 depends on PySide2<=5.14.1 and >=5.12.0
sleap 1.0.0 depends on PySide2<=5.14.1 and >=5.12.0

To fix this you could try to:

  1. loosen the range of package versions you've specified
  2. remove package versions to allow pip attempt to solve the dependency conflict_

Thanks for your attention and interest.

Bests.

@talmo
Copy link
Collaborator

talmo commented Sep 3, 2021

Hi @BearConnorsParadiso,

Could you try the following?

In the Python 3.8 environment (conda create -n sleap python=3.8 -y), first just try to install PySide2:

pip install PySide2

Let me know if this doesn't work, or if it does, what version you end up getting (should be 5.15.2).

Then, try to install sleap again like this:

pip install --no-deps sleap

And let me know what you get. There might be other version conflicts even if that does work, but let's just see -- there's a lot of stuff missing for M1 still so it's hard to know for sure which packages will work.

@BearConnorsParadiso
Copy link

BearConnorsParadiso commented Sep 6, 2021

Here are the errors from your previous advice. Thanks for your continued interest! (FYI, after every "in", "<n(m)odule>" should follow but they get ignored when I upload the comment.)

anaconda3/envs/sleap/bin/sleap-label", line 5, in
from sleap.gui.app import main
anaconda3/envs/sleap/lib/python3.8/site-packages/sleap/init.py", line 9, in
from sleap.io.dataset import Labels, load_file
anaconda3/envs/sleap/lib/python3.8/site-packages/sleap/io/dataset.py", line 58, in
import cattr
anaconda3/envs/sleap/lib/python3.8/site-packages/cattr/init.py", line 2, in
from .converters import Converter, UnstructureStrategy
anaconda3/envs/sleap/lib/python3.8/site-packages/cattr/converters.py", line 15, in
from ._compat import (
anaconda3/envs/sleap/lib/python3.8/site-packages/cattr/_compat.py", line 86, in
from typing import _Union
ImportError: cannot import name '_Union' from 'typing' (anaconda3/envs/sleap/lib/python3.8/typing.py)

@talmo
Copy link
Collaborator

talmo commented Jan 14, 2022

Just posting a small update to this issue -- we still haven't resolved it, but here's some additional troubleshooting we've been able to do in case someone else would like to pick it up from here:

  1. Start from the talmo/m1 branch. This is based off of a pre-release of SLEAP v1.2.0.
  2. Make sure you're using the M1/arm64 version of Miniconda. Here's a one-liner:
wget -nc https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh && bash Miniconda3-latest-MacOSX-arm64.sh -b && ~/miniconda3/bin/conda init zsh
  1. Set up your environment like this:
git clone -b talmo/m1 --single-branch https://github.com/murthylab/sleap.git sleap_m1 && cd sleap_m1
conda create -y -n sleap_m1 python=3.8 pillow shapely=1.7.1 && conda activate sleap_m1
pip install -e .

The last line will probably break because many of our dependencies don't have binaries for M1.

To troubleshoot this, start by relaxing the version requirements in requirements.txt:

#numpy<=1.21.2,>=1.19.5
numpy
attrs==21.2.0
cattrs==1.1.1
jsonpickle==1.2
networkx
#tensorflow<=2.7.0,>=2.6.2  # this just won't work, see below
#h5py<=3.6.0,>=3.1.0
h5py
python-rapidjson
#opencv-python-headless==4.2.0.34
opencv-python-headless
pandas
psutil
PySide6==6.2.2.1
pyzmq
pyyaml
imgaug==0.4.0
#scipy<=1.7.1,>=1.4.1
scipy
scikit-image
scikit-learn
scikit-video
imgstore==0.2.9
qimage2ndarray @ git+https://github.com/hmeine/qimage2ndarray.git@238148cafbb6cfcf121febebf82eaacc8bf34b1e
jsmin
seaborn
pykalman==0.9.5
segmentation-models==1.0.1
rich==10.16.1

Things we've learned:

  • We need to be on Python 3.8.
  • We have to update to PySide6 to support M1 since PySide2 is no longer going to be developed to support new architectures. PySide6 >= 6.2 seem to work.
  • h5py might need to be built from scratch -- see this SO answer.
  • tensorflow, as per usual, is a headache.
    • The pypi wheels for tensorflow 2.6.3 or 2.7.0 do not work.
    • The apple/tensorflow_macos repo seems to have previously hosted a compatible version, but it is now archived.
    • Apple has a page for installing their new version of tensorflow (tensorflow-plugin? tensorflow-metal? maybe tensorflow-ipod, who knows?)
    • It uses the apple::tensorflow-deps conda package for some stuff, and it looks like there's a 2.7.0 version.

I'm still waiting on my M1 Mac to arrive so I can keep testing this, but I'd say it's safe to assume that there will not be an easy installation solution for M1 for a while.

@talmo talmo added the TODO Not a new feature/enhancement, but not a bug either label Jan 21, 2022
@talmo talmo pinned this issue Jan 26, 2022
@talmo
Copy link
Collaborator

talmo commented Feb 3, 2022

Big update: We can now run SLEAP on M1 Macs!

Caveat: Network training is still not working (see the bottom of this post).

Here are the steps if you want to use it today:

  1. In addition to being on an M1 mac, make sure you're on macOS Monterey, i.e., version 12+. I tested this on a MacBook Pro (14-inch, 2021) running macOS version 12.0.1.

  2. If you don't have it yet, install homebrew, a convenient package manager for Macs (skip this if you can run brew from the terminal):

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

This might take a little while since it'll also install Xcode (which we'll need later). Once it's finished, run this to enable the brew command in your shell, then close and re-open the terminal for it to take effect:

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile && eval "$(/opt/homebrew/bin/brew shellenv)"
  1. Install wget, a CLI downloading utility (also makes sure your homebrew setup worked):
brew install wget
  1. Install the M1 Mac version of Miniconda -- this is important, so make sure you don't have the regular Mac version! If you're not sure, type which conda and delete the containing directory to uninstall your existing conda. To install the correct Miniconda, just run:
wget -nc https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh && bash Miniconda3-latest-MacOSX-arm64.sh -b && rm Miniconda3-latest-MacOSX-arm64.sh && ~/miniconda3/bin/conda init zsh

Then close and re-open the terminal again.

  1. Download the SLEAP M1 branch:
cd ~ && git clone -b talmo/m1 --single-branch https://github.com/murthylab/sleap.git sleap_m1 && cd sleap_m1

Note: This installs SLEAP in development mode, so changes to the source code are immediately applied in case you wanted to mess around with it. You can also just do a git pull to update it (no need to re-do any of the previous steps).

  1. Install SLEAP in a conda environment:
conda env create -f environment_m1.yml

Your Mac will then automatically sign a devil's pact with Apple to install the correct versions of everything on your system. Once the blood sacrifice/installation process completes, SLEAP will be available in an environment called sleap_m1.

  1. Test it out by activating the environment and opening the GUI!
conda activate sleap_m1 && sleap-label

It takes a bit longer to start up than on Windows/Linux, but have faith :)
Also, at least in dark mode, the GUI looks awful, but at least this is just cosmetic.

Things that should work:

  • GUI-based operations, like labeling and inspecting data via sleap-label
  • Programmatic APIs, like sleap.load_file() (technically all of our tests pass)
  • Inference and tracking via sleap-track

Neural network training, however, will NOT work. See below for where we're at with that.


Currently, training on the GPU will not work (CPU works fine though, but slowly). Even in simple Unet models, we run into errors like this:

INFO:sleap.nn.training:Starting training loop...
Epoch 1/200
Traceback (most recent call last):
  File "/Users/talmo/miniconda3/envs/sleap_m1/bin/sleap-train", line 33, in <module>
    sys.exit(load_entry_point('sleap', 'console_scripts', 'sleap-train')())
  File "/Users/talmo/sleap_m1/sleap/nn/training.py", line 1618, in main
    trainer.train()
  File "/Users/talmo/sleap_m1/sleap/nn/training.py", line 885, in train
    self.keras_model.fit(
  File "/Users/talmo/miniconda3/envs/sleap_m1/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/Users/talmo/miniconda3/envs/sleap_m1/lib/python3.8/site-packages/tensorflow/python/eager/execute.py", line 58, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation model/stack0_enc0_conv0/Conv2D/ReadVariableOp: Could not satisfy explicit device specification '' because the node {{colocation_node model/stack0_enc0_conv0/Conv2D/ReadVariableOp}} was colocated with a group of nodes that required incompatible device '/job:localhost/replica:0/task:0/device:GPU:0'. All available devices [/job:localhost/replica:0/task:0/device:CPU:0, /job:localhost/replica:0/task:0/device:GPU:0]. 
Colocation Debug Info:
Colocation group had the following types and supported devices: 
Root Member(assigned_device_name_index_=2 requested_device_name_='/job:localhost/replica:0/task:0/device:GPU:0' assigned_device_name_='/job:localhost/replica:0/task:0/device:GPU:0' resource_device_name_='/job:localhost/replica:0/task:0/device:GPU:0' supported_device_types_=[CPU] possible_devices_=[]
ResourceApplyAdamWithAmsgrad: CPU 
ReadVariableOp: GPU CPU 
_Arg: GPU CPU 

Colocation members, user-requested devices, and framework assigned devices, if any:
  model_stack0_enc0_conv0_conv2d_readvariableop_resource (_Arg)  framework assigned device=/job:localhost/replica:0/task:0/device:GPU:0
  adam_adam_update_resourceapplyadamwithamsgrad_m (_Arg)  framework assigned device=/job:localhost/replica:0/task:0/device:GPU:0
  adam_adam_update_resourceapplyadamwithamsgrad_v (_Arg)  framework assigned device=/job:localhost/replica:0/task:0/device:GPU:0
  adam_adam_update_resourceapplyadamwithamsgrad_vhat (_Arg)  framework assigned device=/job:localhost/replica:0/task:0/device:GPU:0
  model/stack0_enc0_conv0/Conv2D/ReadVariableOp (ReadVariableOp) 
  Adam/Adam/update/ResourceApplyAdamWithAmsgrad (ResourceApplyAdamWithAmsgrad) /job:localhost/replica:0/task:0/device:GPU:0

	 [[{{node model/stack0_enc0_conv0/Conv2D/ReadVariableOp}}]] [Op:__inference_train_function_9229]

The important part is this:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation model/stack0_enc0_conv0/Conv2D/ReadVariableOp: Could not satisfy explicit device specification '' because the node {{colocation_node model/stack0_enc0_conv0/Conv2D/ReadVariableOp}} was colocated with a group of nodes that required incompatible device '/job:localhost/replica:0/task:0/device:GPU:0'. All available devices [/job:localhost/replica:0/task:0/device:CPU:0, /job:localhost/replica:0/task:0/device:GPU:0]. 

Basically, it seems like Apple's tensorflow-metal can't run seem to find a way to place an operation in the Conv2D layer on the GPU. We'll need to dig into it a bit more to figure out what exact part of our models aren't working (maybe some poorly defined shapes or our preprocessing pipeline?). In the meantime, here are some other reports of similar issues to follow up on from other brave souls attempting to run TensorFlow on M1s:

@talmo
Copy link
Collaborator

talmo commented Feb 18, 2022

Update: Training is now working on M1! This means that SLEAP is now fully compatible with M1 macs!

To use it, install it using the instructions listed in the post above. We'll try and streamline it for the 1.2.0 stable release.

@talmo talmo changed the title Issue with Downloading on M1 MacBook Pro M1 Mac compatibility Mar 16, 2022
@talmo
Copy link
Collaborator

talmo commented May 12, 2022

Status update: While both inference and training work on M1 now, the blocker is that we need to use PySide6 instead of PySide2 on M1 Macs.

PySide6 does not have a conda package yet though, so we can't switch over completely since it would break support on Windows and Linux.

Possible alternative: Support both PySide2 and PySide6 in the meantime, possibly without offering a conda-based installation route for PySide6.

@talmo
Copy link
Collaborator

talmo commented May 25, 2022

Just to keep track of the conda Qt6/PySide6 packaging, here's the relevant PR to follow: conda-forge/qt-main-feedstock#14

@talmo talmo mentioned this issue Aug 3, 2022
11 tasks
@talmo talmo added fixed in future release Fix or feature is merged into develop and will be available in future release. and removed TODO Not a new feature/enhancement, but not a bug either labels Aug 11, 2022
@roomrys roomrys unpinned this issue Aug 25, 2022
@roomrys roomrys pinned this issue Aug 25, 2022
@roomrys
Copy link
Collaborator

roomrys commented Sep 12, 2022

The m1 branch has been merged with main in the new release v1.2.7, but unfortunately the m1 conda package build has failed. We will follow up on the m1 conda build in a new issue.

@roomrys roomrys closed this as completed Sep 12, 2022
@roomrys roomrys removed the fixed in future release Fix or feature is merged into develop and will be available in future release. label Sep 12, 2022
@sean-afshar sean-afshar unpinned this issue Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants