Installing PyTorch (on Apple Silicon Mac)
To install PyTorch on ny Apple Silicon Mac, e.g. M5, we don’t need a special version. PyTorch natively supports Apple’s GPU acceleration out of the box using Metal Performance Shaders (MPS).
Metal Performance ShadersMetal Performance Shaders (MPS) is an Apple framework providing highly optimized, GPU-accelerated compute and graphics functions for iOS, iPadOS, and macOS. It lets developers run intensive tasks like machine learning, image processing, and ray tracing efficiently without writing custom code for every GPU architecture.
Follow these straightforward steps to set it up inside a clean Python environment:
-
Ensure prerequisites are met: Open terminal and make sure command-line tools are installed:
Terminal window xcode-select --install -
Create and activate virtual environment: It is best practice to use a virtual environment so our packages stay organized. We can use Python’s built-in
venv:Terminal window # Create a project folder and environmentpython3 -m venv pytorch-envsource pytorch-env/bin/activate -
Install PyTorch: Install the stable, native Apple Silicon version of PyTorch, along with
torchvisionandtorchaudioviapipTerminal window pip install --upgrade pippip install torch torchvision torchaudio -
Verify GPU (MPS) Acceleration: To confirm that PyTorch can successfully talk to our Mac’s GPU, run a quick python check:
Terminal window python3 -c "import torchif torch.backends.mps.is_available():device = torch.device('mps')x = torch.ones(1, device=device)print('Success! PyTorch is using the GPU:', x)else:print('MPS device not found. Running on CPU.')"
If it outputs tensor([1.], device='mps:0'), our setup is fully ready for high-performance machine learning tasks.