georgeoshardo / SyMBac

Accurate segmentation of bacterial microscope images using synthetically generated image data.
https://doi.org/10.1186/s12915-022-01453-6
GNU General Public License v2.0
17 stars 9 forks source link

Apple Silicon port #44

Open henriquenunez opened 4 months ago

henriquenunez commented 4 months ago

Hello,

First of all, amazing project!

I am opening this issue to ask whether you think it is possible to port the code to the M1/2/3 GPUs. The hardware in our lab is not nvidia, and we would like to use the tool as well.

I would be able to do the porting myself, if some on the maintainers can offer me some guidance!

georgeoshardo commented 3 months ago

Hi, thanks for your interest in SyMBac @henriquenunez! Apologies for the late reply!

I think this code can work on Apple Silicon. The only GPU operation which is used in the main code is CuPy's convolution function. Other GPU code is just PyTorch or Tensorflow, which I believe are gaining support already so that in theory should be covered.

So all we need to do is find a convolution function which can be called from Python and runs on Apple Silicon. Have you any experience utilising the Apple Silicon GPUs using any Python libraries? E.g I think one could leverage Tensorflow's convolution operations if you have managed to get that running before (https://developer.apple.com/metal/tensorflow-plugin/). I do not have access to any Apple Silicon to test this.

In https://github.com/georgeoshardo/SyMBac/blob/main/SyMBac/renderer.py is where I define the convolution function, depending on whether or not CuPy is installed. All we would need to do is check for Apple Silicon and implement a version which uses tensorflow-metal.

This blog post looks like a nice starting point https://www.laurencegellert.com/2023/10/how-make-python-code-run-on-the-gpu/