A No Nonsense Guide on how to use an M-Series Mac GPU with PyTorch

M-Series Macs is better than saying M1/M2 Macs

PyTorch
Squeezing out that extra performance.
Author

Salman Naqvi

Published

Thursday, 26 January 2023

This blog post was updated on Saturday, 28 January 2023.

A picture of a snake that has taken a bite out of an apple, and whose tail is a burning torch.

If you have one of those fancy Macs with an M-Series chip (M1/M2, etc.), here’s how to make use of its GPU in PyTorch for increased performance.

It’s a bit annoying and a little tedious, but here we go.

1 Requirements

  • Have an M-Series chip
  • Have at least PyTorch 1.12
  • Have at least macOS Monterey 12.3

2 Installing PyTorch

Install PyTorch as you usually would. Just make sure it’s PyTorch 1.12.

# Installing with Pip.
$ pip3 install torch torchvision torchaudio

# Installing using Conda.
$ conda install pytorch torchvision torchaudio -c pytorch

By using these commands, the latest version of the library is installed so there is no need to specify the version number.

However, if you have an existing installation, you can run the following Pip command instead.

$ pip3 install --upgrade torch torchvision torchaudio

3 Import PyTorch

import torch

4 Check Requirements are Met

Below is a convenient code snippet taken from the PyTorch documentation that checks whether requirements are met.

if not torch.backends.mps.is_available():
    if not torch.backends.mps.is_built():
        print("MPS not available because the current PyTorch install was not built with MPS enabled.")
    else:
        print("MPS not available because the current MacOS version is not 12.3+ and/or you do not have an MPS-enabled device on this machine.")

If neither of the two above messages print, you’re good to go!

5 The Annoying Part: Enabling the GPU

As far as I know, you must explicitly enable the use of the GPU for whatever model or tensor you wish to use the GPU for.

There are different ways you can do this.

Use a string.

t = torch.tensor([1, 2, 3], device='mps')

tensor([1, 2, 3], device='mps:0')

Store as a variable.

device='mps'
t = torch.tensor([1, 2, 3], device=device)

tensor([1, 2, 3], device='mps:0')

Convert existing objects.

t = torch.tensor([1, 2, 3])
t.to(device)

tensor([1, 2, 3], device='mps:0')

Note that converting existing objects creates a copy and does not modify the original.

t

tensor([1, 2, 3])

Though the above operations have been performed on tensors, they can also be performed on models.

6 Points to Note

  • GPU enabled means operations are done on the GPU.

  • A GPU enabled tensor can only perform operations with another GPU enabled tensor.

  • As of writing this, GPU support is still in its early stages. So certain features are unsupported and further optimizations await.

Closing Words

If you have any comments, questions, suggestions, feedback, criticisms, or corrections, please do post them down in the comment section below!