Wondering how to use GPU in Macbook with M1/M2 chip to work with deep learning model. PyTorch has released one version to configure Macbook M1/M2 computers to use GPUs. Following steps will get you done with setting up your python environment for using GPU in deep learning model building in PyTorch.
Download Anaconda Python distribution: Visit https://www.anaconda.com/download to download the anaconda by choosing Download for Mac (M1/M2) under Download button.
Install Anaconda just by clicking the downloaded package.
Open the Terminal.
Create a virtual environment using Conda: conda create -n yourenvname python=x.x anaconda
Open Terminal and activate the new environment: conda activate yourenvname
As you enter into new environment, lets install jupyter notebook inside it. pip install notebook
Being in the environment, install ipython kernel: python -m ipykernel install --user --name=yourenvironment
Just execute this command under
# MPS acceleration is available on MacOS 12.3+
conda install pytorch-nightly::pytorch torchvision torchaudio -c pytorch-nightly
This should install the PyTorch with support for accelerated computation on M1/M2 integrated GPU.
open notebook by writing: jupyter notebook being in the environment.
Write following codes in the Jupyter notebook cell.
import torch
import math
print(torch.backends.mps.is_available()) #the MacOS is higher than 12.3+
print(torch.backends.mps.is_built()) #MPS is activated
The outcome should True in both of the cases. Instead of CUDA, for M1/M2 it is MPS.
Use MPS as follows with tensors to accelerate the processing.
import torch
torch.tensor([1,2,3], device="mps") #that's it!