• Stars
    star
    356
  • Rank 119,446 (Top 3 %)
  • Language
    Jupyter Notebook
  • License
    MIT License
  • Created over 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Setup PyTorch on Mac/Apple Silicon plus a few benchmarks.

PyTorch on Apple Silicon

You: Have an Apple Silicon Mac (M1, M2, M1 Pro, M1 Max, M1 Ultra) and would like to set it up for data science and machine learning.

This repo: Helps you install various software tools such as Homebrew and Miniforge3 to use to install various data science and machine learning tools such as PyTorch. We'll also be getting PyTorch to run on the Apple Silicon GPU for (hopefully) faster computing.

Helpful links

Setup a machine learning environment with PyTorch on Mac (short version)

Note: As of March 2023, PyTorch 2.0 is out and that brings a bunch of updates to PyTorch for Apple Silicon (though still not perfect). As of June 30 2022, accelerated PyTorch for Mac (PyTorch using the Apple Silicon GPU) is still in beta, so expect some rough edges.

Requirements:

  • Apple Silicon Mac (M1, M2, M1 Pro, M1 Max, M1 Ultra, etc).
  • macOS 12.3+ (PyTorch will work on previous versions but the GPU on your Mac won't get used, this means slower code).

Steps

  1. Download and install Homebrew from https://brew.sh. Follow the steps it prompts you to go through after installation.
  2. Download Miniforge3 (Conda installer) for macOS arm64 chips (M1, M2, M1 Pro, M1 Max, M1 Ultra).
  3. Install Miniforge3 into home directory.
chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh
sh ~/Downloads/Miniforge3-MacOSX-arm64.sh
source ~/miniforge3/bin/activate
  1. Restart terminal.
  2. Create a directory to setup PyTorch environment.
mkdir pytorch-test
cd pytorch-test
  1. Make and activate Conda environment.

Note: Python 3.8 is the most stable for using the following setup.

conda create --prefix ./env python=3.8
conda activate ./env
  1. Install the PyTorch 1.12.0+ default version for Mac with pip from the PyTorch getting started page.
pip3 install torch torchvision torchaudio

This will install the following:

Installing collected packages: urllib3, typing-extensions, pillow, numpy, idna, charset-normalizer, certifi, torch, requests, torchvision, torchaudio

  1. Install common data science packages.
conda install jupyter pandas numpy matplotlib scikit-learn tqdm 
  1. Start Jupyter.
jupyter notebook
  1. Create a new notebook by "New" -> "Notebook: Python 3 (ipykernel)" and run the following code to verfiy all the dependencies are available and check PyTorch version/GPU access.
import torch
import numpy as np
import pandas as pd
import sklearn
import matplotlib.pyplot as plt

print(f"PyTorch version: {torch.__version__}")

# Check PyTorch has access to MPS (Metal Performance Shader, Apple's GPU architecture)
print(f"Is MPS (Metal Performance Shader) built? {torch.backends.mps.is_built()}")
print(f"Is MPS available? {torch.backends.mps.is_available()}")

# Set the device      
device = "mps" if torch.backends.mps.is_available() else "cpu"
print(f"Using device: {device}")

If it all worked, you should see something like:

PyTorch version: 1.12.0
Is MPS (Metal Performance Shader) built? True
Is MPS available? True
Using device: mps

Note: See more on running MPS as a backend in the PyTorch documentation.

  1. To run data/models on an Apple Silicon GPU, use the PyTorch device name "mps" with .to("mps"). MPS stands for Metal Performance Shaders, Metal is Apple's GPU framework.
import torch

# Set the device
device = "mps" if torch.backends.mps.is_available() else "cpu"

# Create data and send it to the device
x = torch.rand(size=(3, 4)).to(device)
x

Finally, you should get something like this:

tensor([[2.6020e-01, 9.6467e-01, 7.5282e-01, 1.8063e-01],
        [7.0760e-02, 9.8610e-01, 6.5195e-01, 7.5700e-01],
        [3.4065e-01, 1.8971e-01, 6.0876e-01, 9.3907e-01]], device='mps:0')

Congratulations! Your Apple Silicon device is now running PyTorch + a handful of other helpful data science and machine learning libraries.

Results

Last update: 23 May 2022

Benchmark results were gathered with the notebook 00_cifar10_tinyvgg.ipynb.

Running TinyVGG on CIFAR10 dataset with batch size 32 and image size 32*32:

results for running PyTorch on Apple M1 Pro with TinyVGG and CIFAR10 with image size 32x32

Running TinyVGG on CIFAR10 dataset with batch size 32 and image size 224*224:

results for running PyTorch on Apple M1 Pro with TinyVGG and CIFAR10 with image size 224x224

Looks like the "mps" device shines when the GPU is getting utilized more. However, since accelerated PyTorch for Mac is still in beta, I'm sure there's room for improvement.

How to setup a PyTorch environment on Apple Silicon using Miniforge (longer version)

If you're new to creating environments, using an Apple Silicon Mac (M1, M1 Pro, M1 Max, M1 Ultra) machine and would like to get started running PyTorch and other data science libraries, follow the below steps.

Note: You're going to see the term "package manager" a lot below. Think of it like this: a package manager is a piece of software that helps you install other pieces (packages) of software.

Installing package managers (Homebrew and Miniforge)

  1. Download and install Homebrew from https://brew.sh. Homebrew is a package manager that sets up a lot of useful things on your machine, including Command Line Tools for Xcode, you'll need this to run things like git. The command to install Homebrew will look something like:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

It will explain what it's doing and what you need to do as you go.

  1. Download the most compatible version of Miniforge (minimal installer for Conda specific to conda-forge, Conda is another package manager and conda-forge is a Conda channel) from GitHub.

If you're using an M1 variant Mac, it's "Miniforge3-MacOSX-arm64" <- click for direct download.

Clicking the link above will download a shell file called Miniforge3-MacOSX-arm64.sh to your Downloads folder (unless otherwise specified).

  1. Open Terminal.

  2. We've now got a shell file capable of installing Miniforge, but to do so we'll have to modify it's permissions to make it executable.

To do so, we'll run the command chmod -x FILE_NAME which stands for "change mode of FILE_NAME to -executable".

We'll then execute (run) the program using sh.

chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh
sh ~/Downloads/Miniforge3-MacOSX-arm64.sh
  1. This should install Miniforge3 into your home directory (~/ stands for "Home" on Mac).

To check this, we can try to activate the (base) environment, we can do so using the source command.

source ~/miniforge3/bin/activate

If it worked, you should see something like the following in your terminal window.

(base) daniel@Daniels-MBP ~ %
  1. We've just installed some new software and for it to fully work, we'll need to restart terminal.

Creating a PyTorch environment

Now we've got the package managers we need, it's time to install PyTorch.

Let's setup a folder called pytorch-test (you can call this anything you want) and install everything in there to make sure it's working.

Note: An environment is like a virtual room on your computer. For example, you use the kitchen in your house for cooking because it's got all the tools you need. It would be strange to have an oven in your bedroom. The same thing on your computer. If you're going to be working on specific software, you'll want it all in one place and not scattered everywhere else.

  1. Make a directory called pytorch-test. This is the directory we're going to be storing our environment. And inside the environment will be the software tools we need to run PyTorch, especially PyTorch on the Apple Silicon GPU.

We can do so with the mkdir command which stands for "make directory".

mkdir pytorch-test
  1. Change into pytorch-test. For the rest of the commands we'll be running them inside the directory pytorch-test so we need to change into it.

We can do this with the cd command which stands for "change directory".

cd pytorch-test
  1. Now we're inside the pyorch-test directory, let's create a new Conda environment using the conda command (this command was installed when we installed Miniforge above).

We do so using conda create --prefix ./env which stands for "conda create an environment with the name file/path/to/this/folder/env". The . stands for "everything before".

For example, if I didn't use the ./env, my filepath looks like: /Users/daniel/pytorch-test/env

conda create --prefix ./env
  1. Activate the environment. If conda created the environment correctly, you should be able to activate it using conda activate path/to/environment.

Short version:

conda activate ./env

Long version:

conda activate /Users/daniel/pytorch-test/env

Note: It's important to activate your environment every time you'd like to work on projects that use the software you install into that environment. For example, you might have one environment for every different project you work on. And all of the different tools for that specific project are stored in its specific environment.

If activating your environment went correctly, your terminal window prompt should look something like:

(/Users/daniel/pytorch-test/env) daniel@Daniels-MBP pytorch-test %
  1. Now we've got a Conda environment setup, it's time to install the software we need.

Let's start by installing the PyTorch 1.12.0+ (v1.12.0+ is required for GPU acceleration) for Mac from the PyTorch install page.

Note:

pip3 install torch torchvision torchaudio

If it worked, you should see a bunch of stuff being downloaded and installed for you.

  1. Install common data science packages.

If you'd like to work on other various data science and machine learning projects, you're likely going to need Jupyter Notebooks, pandas for data manipulation, NumPy for numeric computing, matplotlib for plotting and Scikit-Learn for traditional machine learning algorithms and processing functions.

To install those in the current environment run:

conda install jupyter pandas numpy matplotlib scikit-learn tqdm
  1. Test it out. To see if everything worked, try starting a Jupyter Notebook and importing the installed packages.
# Start a Jupyter notebook
jupyter notebook

Once the notebook is started, in the first cell:

import torch
import numpy as np
import pandas as pd
import sklearn
import matplotlib.pyplot as plt

print(f"PyTorch version: {torch.__version__}")

# Check PyTorch has access to MPS (Metal Performance Shader, Apple's GPU architecture)
print(f"Is MPS (Metal Performance Shader) built? {torch.backends.mps.is_built()}")
print(f"Is MPS available? {torch.backends.mps.is_available()}")

# Set the device      
device = "mps" if torch.backends.mps.is_available() else "cpu"
print(f"Using device: {device}")

If it all worked, you should see something like:

PyTorch version: 1.12.0
Is MPS (Metal Performance Shader) built? True
Is MPS available? True
Using device: mps

Note: See more on running MPS as a backend in the PyTorch documentation.

  1. To run data/models on an Apple Silicon GPU, use the PyTorch device name "mps" with .to("mps"). MPS stands for Metal Performance Shaders, Metal is Apple's GPU framework.
import torch

# Set the device
device = "mps" if torch.backends.mps.is_available() else "cpu"

# Create data and send it to the device
x = torch.rand(size=(3, 4)).to(device)
x

Finally, you should get something like this:

tensor([[2.6020e-01, 9.6467e-01, 7.5282e-01, 1.8063e-01],
        [7.0760e-02, 9.8610e-01, 6.5195e-01, 7.5700e-01],
        [3.4065e-01, 1.8971e-01, 6.0876e-01, 9.3907e-01]], device='mps:0')

Congratulations! Your Apple Silicon device is now running PyTorch + a handful of other helpful data science and machine learning libraries.

  1. To see if it really worked, try running one of the notebooks above end to end!

More Repositories

1

machine-learning-roadmap

A roadmap connecting many of the most important concepts in machine learning, how to learn them and what tools to use to perform them.
7,121
star
2

pytorch-deep-learning

Materials for the Learn PyTorch for Deep Learning: Zero to Mastery course.
Jupyter Notebook
6,701
star
3

tensorflow-deep-learning

All course materials for the Zero to Mastery Deep Learning with TensorFlow course.
Jupyter Notebook
4,709
star
4

zero-to-mastery-ml

All course materials for the Zero to Mastery Machine Learning and Data Science course.
Jupyter Notebook
2,449
star
5

cs329s-ml-deployment-tutorial

Code and files to go along with CS329s machine learning model deployment tutorial.
Jupyter Notebook
573
star
6

m1-machine-learning-test

Code for testing various M1 Chip benchmarks with TensorFlow.
Jupyter Notebook
482
star
7

your-first-kaggle-submission

How to perform an exploratory data analysis on the Kaggle Titanic dataset and make a submission to the leaderboard.
Jupyter Notebook
216
star
8

nutrify

Take a photo of food and learn about it.
Jupyter Notebook
156
star
9

airbnb-amenity-detection

Repo for 42 days project to replicate/improve Airbnb's amenity (object) detection pipeline.
Jupyter Notebook
156
star
10

mac-ml-speed-test

A few quick scripts focused on testing TensorFlow/PyTorch/Llama 2 on macOS.
Jupyter Notebook
86
star
11

food-not-food

Machine Learning powered app to decide whether a photo is food or not.
Jupyter Notebook
54
star
12

python-list-comprehensions-tutorial

This is the code to go along with the Python list comprehensions video by Daniel Bourke on YouTube.
Jupyter Notebook
43
star
13

mrdbourke

34
star
14

learn-transformers

Work in progress. Simple repository to learn Transformers (and transformers).
Jupyter Notebook
28
star
15

old_blog

My dev's blog
HTML
21
star
16

rag-resources

A collection of curated RAG (Retrieval Augmented Generation) resources.
21
star
17

charlie-walks

Website code for Charlie Walks: A Novel by Daniel Bourke
HTML
12
star
18

Udacity_DLND_Projects

All of my projects from the Udacity Deep Learning Foundations Nanodegree.
Jupyter Notebook
12
star
19

coursera_bioinformatics_and_genetic_algorithm_experiment

Code relating to the Coursera Bioinformatics Specialization as well as my own genetic algorithm experiment.
Jupyter Notebook
11
star
20

AIND-Machine-Translation

The code and other files related to the Udacity Artificial Intelligence Nanodegree Machine Translation project.
Jupyter Notebook
10
star
21

twitch-ml-deploy

Deploying a ML model in the browser using TensorFlow JS
JavaScript
8
star
22

Sentiment-Analysis-with-Keras

Materials and code relating to Learning Intelligence 25.
Jupyter Notebook
8
star
23

AIND-VUI-Capstone

Code and other files related to the Udacity Artificial Intelligence Nanodegree Deep Neural Network Speech Recognizer.
HTML
7
star
24

udacity-AIND

All of my files and projects associated with Term 1 of the Udacity Artificial Intelligence Nanodegree.
HTML
5
star
25

LearnPythonTheHardWay

All of the exercises from the Learn Python the Hard Way book from Zed Shaw.
Python
5
star
26

pytorch-resnet-twitch

PyTorch implementation(s) of various ResNet models from Twitch streams.
Jupyter Notebook
5
star
27

udacityDLfoundationsP2

Second project of the Udacity Deep Learning Foundations Nanodegree
HTML
5
star
28

nutrify-ai-grant-application

Nutrify application to aigrant.org
HTML
4
star
29

AIND-NLP

Materials relating to the Udacity AIND NLP classes.
Jupyter Notebook
3
star
30

modal-test

testing modal.com
Python
2
star
31

Treehouse

Projects from my Treehouse Coursework
2
star
32

hello-world

Github practice
2
star
33

food-data-central-database-creation-with-supabase

JavaScript
2
star
34

html-search-bar

Simple HTML search bar to search through a JavaScript array of JSON.
JavaScript
2
star
35

iOS-Retro-Calculator

Retro/Space
1
star
36

test-data-size-repo

1
star
37

ios-course-super-cool-app

Basic-iOS-app
1
star
38

blog-starter-test-1

🚀⚡️ Blazing fast blog built with Gatsby and Cosmic JS 🔥
JavaScript
1
star