Skip to content

Instantly share code, notes, and snippets.

@valosekj
Last active July 14, 2024 02:26
Show Gist options
  • Save valosekj/0dac687c8299b715482a98270fcca101 to your computer and use it in GitHub Desktop.
Save valosekj/0dac687c8299b715482a98270fcca101 to your computer and use it in GitHub Desktop.
Accelerate PyTorch using `MPS` on Apple silicon

Accelerate PyTorch using MPS on Apple silicon

  1. Download Xcode from the App Store
  2. Opan a new Terminal tab and run sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
  3. Accept the Xcode licence by running: sudo xcodebuild -license
  4. Try running /usr/bin/xcodebuild -version to ensure Xcode is correctly installed and configured
  5. Create a new conda env and install pytorch-nightly:
conda create -n nanogpt python=3.10
conda activate nanogpt
conda install pytorch -c pytorch-nightly
conda install numpy
  1. Start Python and run the following code:
import torch

device = "cpu"
if torch.cuda.is_available():
    device = "cuda"
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
    device = "mps"

print(f"using device: {device}")
using device: mps

Resources: https://developer.apple.com/metal/pytorch/, Let's reproduce GPT-2 (124M), ChatGPT4o

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment