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 apple #523

Closed
zyazdani-92 opened this issue Jun 4, 2022 · 13 comments
Closed

M1 apple #523

zyazdani-92 opened this issue Jun 4, 2022 · 13 comments
Labels
enhancement New feature or request

Comments

@zyazdani-92
Copy link

Hello,

I trained a model in GUI (Cellpose-V2) and I'd like to use it on a pipeline in the Jupyter notebook.
I'm facing this error: TORCH CUDA version not installed/working.

I used conda install pytorch cudatoolkit=10.1 -c pytorch but it didn't work.

Would you please help me to fix this issue?
I have a MacBook air M1

@zhanglab2008
Copy link

It’s because apple is not using nvidia gpu.

@psobolewskiPhD
Copy link

This recently came up on the image.sc forums.
First off, CUDA is nVidia technology Apple doesn't support. It's not available on M1 at all.
You can run cellpose on M1 Apple Silicon on CPU only at the moment.
Here's some instructions I wrote up:
https://forum.image.sc/t/cellpose-on-macm1pro/68018/4?u=psobolewskiphd
PyTorch is adding a Metal backend which will enable GPU support on M1:
https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/
I've played with the nightlies and the backend is much faster, but cellpose will require some patches to support it and last I tried, some of the ops were still buggy.

@carsen-stringer
Copy link
Member

thanks @psobolewskiPhD , we'll be happy to take a pull request on this. I don't have an M1 mac to test myself

@carsen-stringer carsen-stringer added the enhancement New feature or request label Jul 3, 2022
carsen-stringer added a commit that referenced this issue Jul 3, 2022
…forcing tiff saving for masks > 2**16 (#524), and new suggestion mode
@tdincer
Copy link

tdincer commented Aug 1, 2022

Hi @carsen-stringer,

I'm able to make PyTorch utilize the M1 apple chip. The trick is to install the PyTorch from nightly builds:
conda install pytorch torchvision torchaudio -c pytorch-nightly
(see https://pytorch.org/get-started/locally/)

However, this is not enough for cellpose to utilize the M1 chip. As far as I see from the cellpose's core.py file, _use_gpu_torch function searches only for the cuda drivers. For a general solution, the try-except logic can be expanded to look for the M1 chip.

The lazy solution for the Apple M1 users is to do the following change in all relevant places:
device = torch.device(f'cuda:{device}') -> device = torch.device('mps').

@zyazdani-92
Copy link
Author

zyazdani-92 commented Oct 11, 2022 via email

@carsen-stringer
Copy link
Member

this is now possible from the command line with --gpu_device mps

please let me know if this works and I'll add it to the GUI

@lancetdenes
Copy link

I was able to get the '--gpu_device mps' option to work locally off the command line on an macbook air M2 (!)

@lancetdenes
Copy link

lancetdenes commented Nov 26, 2022

Upon closer inspection this does not appear to be working for me.

My torch installation is working and able to access the mps through torch.device('mps'), however I am getting a couple of errors with cellpose.

First, when I run on the command line it appears to load the GPU but then revert back to CPU and I am not getting any performance increase upon using --gpu_device mps.
here is my command:
python -m cellpose --gpu_device mps --dir _path_ --train --n_epochs 100 --mask_filter _seg.npy --verbose
and the output I get before it runs is:

2022-11-26 17:59:59,541 [INFO] >>>> using GPU
2022-11-26 17:59:59,541 [INFO] >>>> using CPU
2022-11-26 17:59:59,698 [INFO] 4 / 4 images in path folder have labels
2022-11-26 17:59:59,698 [INFO] >>>> during training rescaling images to fixed diameter of 30.0 pixels
2022-11-26 17:59:59,698 [INFO] >> cyto << model set to be used
2022-11-26 17:59:59,698 [INFO] WARNING: MKL version on torch not working/installed - CPU version will be slightly slower.
2022-11-26 17:59:59,698 [INFO] see https://pytorch.org/docs/stable/backends.html?highlight=mkl

Then, when I try to run from a python script I get an error while trying to load a model:
model = cellpose.models.CellposeModel(gpu=True, device=torch.device('mps'), model_type='cyto')
this is the last call:

--> 369 self.net.load_model(self.pretrained_model[0], cpu=(not self.gpu))

and here is the error:

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, mps:0 and cpu!

but if I make the same call like this:
model = cellpose.models.CellposeModel(device=torch.device('cpu'), model_type='cyto')
it will work fine and model.device returns:

device(type='cpu')

Not sure if I am making some silly mistake or not but would be great if anyone has insight on this.

@delnatan
Copy link

delnatan commented Dec 3, 2022

I ran into the same problem here (running Cellpose 2.1.1 on a M1 Pro).
Looking at these lines in your log file:
2022-11-26 17:59:59,541 [INFO] >>>> using GPU
2022-11-26 17:59:59,541 [INFO] >>>> using CPU

I think this is because the function assign_device() in cellpose/core.py is missing an extra line which should set cpu=False after the following line?

gpu=True

This way if 'mps' is passed as 'device' parameter, we can avoid executing the code block in the if statement below (the if cpu: bit).

As for the runtime error you get when using cellpose from a script, I think it's an issue with the load_model() method in CPnet (

def load_model(self, filename, cpu=False):
)

I think now you should be able to pass the argument map_location=torch.device("mps") to have the model weights loaded in the M1 gpu, and I don't see a way to specify that with the current implementation (looks like it only goes to cpu or cuda?). I haven't looked at instances where this method gets called in the program.

Any one else got cellpose to run on M1 gpu?
-Daniel

@delnatan
Copy link

delnatan commented Dec 3, 2022

forgot to mention that I'm running pytorch 1.13.0

@psobolewskiPhD
Copy link

@delnatan If you want to try, I have a branch where I hacked in MPS support.
https://github.com/psobolewskiPhD/cellpose/tree/feature/add_MPS_device
I get about a 60% speedup for a set of the example images using this code based on one of the examples/notebooks:
https://gist.github.com/psobolewskiPhD/80d4b18ba78b49eec6a2398153347a83

carsen-stringer added a commit that referenced this issue Jan 12, 2023
@carsen-stringer
Copy link
Member

I think this should be working on the main branch now, please feel free to make a pull request if I'm missing something in the implementation

@FiReTiTi
Copy link

FiReTiTi commented Apr 2, 2024

this is now possible from the command line with --gpu_device mps

please let me know if this works and I'll add it to the GUI

What about from a notebook/script?
I have an Apple M2 and I use CellPose models from "models.CellPose". How could I specify to use mps?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants