Setting up Mac M1/M2  for using GPU with PyTorch

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. 

Installing Anaconda

Create  a Virtual Environment

Activate Virtual Environment

Install PyTorch for Enabling GPU


# 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.  

Checking if it is Working

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!