How to Install PyTorch on Apple Silicon/Mac M1/M2 | [Easiest Guide]

Varun Kumethi
4 min readMay 11, 2023

--

Machine-Learning & Deep Learning on Mac M1/M2? What? Yes!

The following steps are designed for absolute beginners using Conda. If you are a programming wizard, visit this link here → 🔗 PyTroch Website: & follow the steps on your own.

Morden LLMs mostly have a cut-off of 2021, and Pytorch on mps is a recent update for Apple Silicon. I’m sure in a few months, you might not need this guide. Meanwhile, follow these steps, and by the end, you will have PyTorch on Apple Silicon.

Also, the following guide is an extension to Alpha-Co-Vision, a real-time, Video-to-Text bot that allows Cohere AI’s Command and GPT-3/ChatGPT-like LLM models to see the world.

What is Metal Acceleration?

PyTorch and TensorFlow can now use the new Metal Performance Shaders (MPS) backend for GPU training acceleration. This MPS backend enhances the PyTorch framework by offering Mac-specific setup, operation scripts, and tools. The MPS framework uses kernels that are specifically tailored for the distinctive features of each Metal GPU family to optimize computing performance. The new MPS device maps computational graphs and primitives from machine learning onto the MPS Graph framework and tuned kernels offered by MPS.

Enough of the technical jargon; here is the crux:

Your Mac-M1 Apple Silicon device has a mighty GPU; it is just built on a different architecture and requires additional steps to activate. Apple’s M1/M2 Arm architecture currently does not supports external GPUs. Therefore, leveraging maximum performance in your ML/DL tasks and running PT on Apple Silicon would significantly enhance your workflow.

This should also work for Mac AMD GPUs, but we’ll focus on M1 GPUs.

[Requirements] You will need:

  • Mac computers with Apple Silicon.
  • macOS 12.3 or later.
  • Python 3.7 or later.
  • Xcode command-line tools.

Here are the steps:

Step 1: Open your Terminal, either OG macOS or Terminal, on your IDE.

  • Copy → Paste this in your terminal: Xcode-select --install
  • Let it install!

Step 2: Go here and download Anaconda for M1.

  • Download Anaconda from the link above.
  • The file should say: “…MacOSX-arm64.pkg” Hit Install
  • Let it install!

Step 3: Run this in your terminal: [You can use your IDE’s terminal as well]

  • Copy → Paste this in your Terminal:
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
sh Miniconda3-latest-MacOSX-arm64.sh
  • Let it install!

Step 4: Install PyTorch

  • Copy → Paste this in your terminal to install PyTorch:

conda install pytorch torchvision torchaudio -c pytorch-nightly

Step 5: Test & Verify if PyTorch Was Correctly Installed [Don’t skip]

If you are using macOS Terminal, then type:

python

Python 3. x should be running on your Terminal. Then paste this:

import torch
if torch.backends.mps.is_available():
mps_device = torch.device("mps")
x = torch.ones(1, device=mps_device)
print (x)
else:
print ("MPS device not found.")
  • Let it run for a few seconds.

The output should now show the following:

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

Step 6: Using The Right Environment:

  • [Very Important] Make sure you are using the right environment.

If all you did was install and are not using the right environment, then PyTorch is not using GPU, which would cause errors later in programs while running.

It should say Python 3.xx | packaged by conda-forge | (main, Mar 24 2023, 20:12:31) [Clang 14.0.6 ] on darwin” etc

Look for conda-forge to be 100% sure.

  • [Not Necessary] Restart M1 & redo the test to be sure.

Step 7: Select the environment on your IDE, for example, PyCharm or Visual Code.

  • Make sure that the interpreter is selected namely: miniconda3
Example showing how to add miniconda3 as your interpreter.

🥳 Congratulations, you have now successfully installed PyTorch.

References:

🔗 PyTroch Website:

🔗 Apple Website:

Alpha-Co-Vision:

GitHub: https://github.com/xISSAx/Alpha-Co-Vision

Here is a Silly-Cone, haha, get it? Me neither XD

Happy Coding :)

--

--