Skip to main content
On an Apple Silicon Mac, Docker cannot use the GPU. It runs containers inside a Linux virtual machine, where Apple’s Metal framework is not available. That means PyTorch runs on the CPU inside the container, even on an M-series Mac. For the AI and computer vision courses, you can install the project directly on macOS instead. This gives PyTorch access to the GPU through Apple’s Metal Performance Shaders backend, which appears in PyTorch as the mps device.
This does not apply to the robotics course. ROS 2 still needs the container on macOS. See ROS on Mac.

Native macOS or Docker?

Both options work, but each has a tradeoff. A native installation uses your Mac’s GPU, so training and other compute-heavy tasks are usually much faster. The downside is that you manage the dependencies yourself, and your installed versions may gradually differ from the course environment. The Docker container gives you the same pinned environment used to run the notebooks on this site. This makes it easier to reproduce the published results, but the container can only use your Mac’s CPU. A practical approach is to work natively most of the time, then use the container or Colab if a result looks suspicious and you want to rule out your local setup. Every assignment includes a Colab badge for this reason.

Setup

You will need Python and uv. If you do not have them yet, follow the Python and uv guide first. The commands below assume that uv is available in your terminal.
That is all you need. The standard PyTorch package for Apple Silicon already includes Metal support, so there is no separate GPU package to install. CUDA is only for NVIDIA GPUs and is not used here.

Selecting the device

Let PyTorch choose from the devices that are actually available instead of hard-coding one. With the following check, the same code can run on your Mac, a CUDA machine, or Colab:
On an M-series Mac, you should see mps. If the output says cpu, you are probably running inside a container or using a PyTorch build without Metal support.

When an operation is not supported

PyTorch’s Metal backend does not support every operation yet. If your model reaches an unsupported one, PyTorch stops with a runtime error that names the operator. You can tell it to run unsupported operations on the CPU instead:
If you need this regularly, add the variable to your shell profile. Keep in mind that frequent fallbacks can slow a model down because tensors have to move between the GPU and CPU. The MPS backend documentation has more detail about current support.

A note about course notebooks

Some course notebooks only check for cuda and otherwise default to cpu. Those notebooks will still use the CPU on a Mac, even after you complete this setup. If you come across one, replace its device-selection code with the example above; that is usually the only change required.