Member-only story
ML Notes
Hugging Face and Sentence Transformers on M1 Macs
Easy guide to Hugging Face and Sentence-Transformers on M1 with GPU-acceleration
Prerequisites
- MacOS 12.4+
- Anaconda distribution for M1 (find at bottom of page here)
Python Environment
We first setup a new Python environment that uses osx-arm64
and install the latest version(s) of PyTorch.
- Open terminal
- Create new environment with
conda create -n ml python=3.9 anaconda
- [Alternative]: If using the non-M1 distribution of Anaconda use
CONDA_SUBDIR=osx-arm64 conda create -n ml python=3.9 -c conda-forge
- Switch to new environment with
conda activate ml
- Set the sub-directory variable with
conda env config vars set CONDA_SUBDIR=osx-arm64
- Reactivate the environment with
conda activate
followed byconda activate ml
PyTorch Installation
GPU-acceleration on the new M1 chips is a new feature for PyTorch. For this, we need PyTorch v1.12 or later, as of 3rd June 2022 this is only available via the PyTorch nightly release.
pip install -U --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu

*Here’s a video on the new GPU-enabled PyTorch for M1*
Hugging Face Installation
The Transformers library uses another Hugging Face package called Tokenizers. Tokenizers requires Rust, because we’re using an ARM64 environment we will likely see ERROR: Failed building wheel for tokenizers during the install for these packages.
To avoid this, we install Rust.
curl — proto ‘=https’ — tlsv1.2 -sSf https://sh.rustup.rs | sh
Now we can pip install transformers
.
Sentence Transformers Installation
Our final step is installing the Sentence Transformers library, again there are some additional steps we must take to get this working on M1.
Sentence transformers has a sentencepiece
depency, if we try to install this package we will see ERROR: Failed building wheel for sentencepiece. To fix this we need:
brew install cmake
brew install pkg-config
*If you don’t have Homebrew installed click here*
Now we’re ready to pip install sentence-transformers
.
And that’s it, you’re good to go!
If you’d like to keep up to date with what I’m doing I post weekly on YouTube, and you get in touch directly via Discord. I hope to see you around!