Code for testing various M1 Chip benchmarks with TensorFlow.
MIT License
This repo contains some sample code to benchmark the new M1 MacBooks (M1 Pro and M1 Max) against various other pieces of hardware.
It also has steps below to setup your M1, M1 Pro, M1 Max, M1 Ultra or M2 Mac to run the code.
You: have a new M1, M1 Pro, M1 Max, M1 Ultra or M2 Mac and would like to get started doing machine learning and data science on it.
This repo: teaches you how to install the most common machine learning and data science packages (software) on your machine and make sure they run using sample code.
All experiments were run with the same code. For Apple devices, TensorFlow environments were created with the steps below.
Notebook Number | Experiment |
---|---|
00 | TinyVGG model trained on CIFAR10 dataset with TensorFlow code. |
01 | EfficientNetB0 Feature Extractor on Food101 dataset with TensorFlow code. |
02 |
RandomForestClassifier from Scikit-Learn trained with random search cross-validation on California Housing dataset. |
See the results directory.
If you're experienced with making environments and using the command line, follow this version. If not, see the longer version below.
chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh
sh ~/Downloads/Miniforge3-MacOSX-arm64.sh
source ~/miniforge3/bin/activate
mkdir tensorflow-test
cd tensorflow-test
conda create --prefix ./env python=3.8
conda activate ./env
conda install -c apple tensorflow-deps
tensorflow-macos
).python -m pip install tensorflow-macos
tensorflow-metal
to leverage Apple Metal (Apple's GPU framework) for M1, M1 Pro, M1 Max GPU acceleration.python -m pip install tensorflow-metal
python -m pip install tensorflow-datasets
conda install jupyter pandas numpy matplotlib scikit-learn
jupyter notebook
import numpy as np
import pandas as pd
import sklearn
import tensorflow as tf
import matplotlib.pyplot as plt
# Check for TensorFlow GPU access
print(f"TensorFlow has access to the following devices:\n{tf.config.list_physical_devices()}")
# See TensorFlow version
print(f"TensorFlow version: {tf.__version__}")
If it all worked, you should see something like:
TensorFlow has access to the following devices:
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),
PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
TensorFlow version: 2.8.0
If you're new to creating environments, using a new M1, M1 Pro, M1 Max machine and would like to get started running TensorFlow 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.
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.
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).
Open Terminal.
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
~/
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 ~ %
Now we've got the package managers we need, it's time to install TensorFlow.
Let's setup a folder called tensorflow-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.
tensorflow-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 TensorFlow.We can do so with the mkdir
command which stands for "make directory".
mkdir tensorflow-test
tensorflow-test
. For the rest of the commands we'll be running them inside the directory tensorflow-test
so we need to change into it.We can do this with the cd
command which stands for "change directory".
cd tensorflow-test
tensorflow-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/tensorflow-test/env
conda create --prefix ./env
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/tensorflow-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/tensorflow-test/env) daniel@Daniels-MBP tensorflow-test %
Let's start by installing various TensorFlow dependencies (TensorFlow is a large piece of software and depends on many other pieces of software).
Rather than list these all out, Apple have setup a quick command so you can install almost everything TensorFlow needs in one line.
conda install -c apple tensorflow-deps
The above stands for "hey conda install all of the TensorFlow dependencies from the Apple Conda channel" (-c
stands for channel).
If it worked, you should see a bunch of stuff being downloaded and installed for you.
Apple have created a fork (copy) of TensorFlow specifically for Apple Macs. It has all the features of TensorFlow with some extra functionality to make it work on Apple hardware.
This Apple fork of TensorFlow is called tensorflow-macos
and is the version we'll be installing:
python -m pip install tensorflow-macos
Depending on your internet connection the above may take a few minutes since TensorFlow is quite a large piece of software.
tensorflow-metal
.Why?
Machine learning models often benefit from GPU acceleration. And the M1, M1 Pro and M1 Max chips have quite powerful GPUs.
TensorFlow allows for automatic GPU acceleration if the right software is installed.
And Metal is Apple's framework for GPU computing.
So Apple have created a plugin for TensorFlow (also referred to as a TensorFlow PluggableDevice) called tensorflow-metal
to run TensorFlow on Mac GPUs.
We can install it using:
python -m pip install tensorflow-metal
If the above works, we should now be able to leverage our Mac's GPU cores to speed up model training with TensorFlow.
TensorFlow Datasets provides a collection of common machine learning datasets to test out various machine learning code.
python -m pip install tensorflow-datasets
To install those in the current environment run:
conda install jupyter pandas numpy matplotlib scikit-learn
# Start a Jupyter notebook
jupyter notebook
Once the notebook is started, in the first cell:
import numpy as np
import pandas as pd
import sklearn
import tensorflow as tf
import matplotlib.pyplot as plt
# Check for TensorFlow GPU access
print(tf.config.list_physical_devices())
# See TensorFlow version
print(tf.__version__)
If it all worked, you should see something like:
TensorFlow has access to the following devices:
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),
PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
TensorFlow version: 2.5.0
And then compare your results to the benchmarks above.