Run Stable Diffusion locally on your M1 Macbook Pro

Apple's awesome update enables us to use Stable Diffusion on an M1 Macbook Pro. Here are the step-by-step instructions.

Step 0 - Install Homebrew package manager

Simple instructions.

Step 1 - Install Rust

Install rustup packager for rust.

brew install rustup

Use rustup to install the Rust compiler (rustc) and the Rust package manager (cargo).

rustup-init

Select Option 1

Restart Terminal to reload your PATH environment variable to include Cargo package manager

The Homebrew package manager is the easiest way to install developer tools on a Mac.

Run this, just in case.

source "$HOME/.cargo/env"

Step 2 - Install Python3.8 via pyenv

Install pyenv. pyenv is the package manager used for installing multiple versions of python.

brew install pyenv

Install python3.8.

pyenv install 3.8.16

Set python3.8 as your global language.

pyenv global 3.8.16

Verify your work

pyenv versions

Step 1 - Install Anaconda via Homebrew

Anaconda is a Python-based toolbelt for data scientists. You can install it using a distribution file or via Homebrew. I always pick Homebrew when possible. It will help you better understand where your libraries live.

brew install --cask anaconda

Configuring Paths in Terminal

Anaconda no longer suggests modifying the path.

/opt/homebrew/anaconda3/bin/conda init zsh

Old Way

Add this path to your environment.

echo 'export PATH=/usr/local/anaconda3/bin:$PATH' >> ~/.zshrc

Add this other path to your environment.

echo 'export PATH=/opt/homebrew/anaconda3/bin:$PATH' >> ~/.zshrc

Either restart the Terminal or run this command.

source ~/.zshrc

Verify

Verify that you can access Anaconda from your Terminal.

conda

Step 2 - Create a Python environment and install dependencies

conda create -n coreml_stable_diffusion python=3.8 -y

Activate your python environment.

conda activate coreml_stable_diffusion

Step 3 - Clone Repo from Github

Clone Apple's repo from Github.

I will download the git repo on my desktop to make things obvious.

Change the directory to your desktop.

mkdir ~/Desktop/

Clone from GitHub.

git clone https://github.com/apple/ml-stable-diffusion.git

Change directory into repo.

cd ml-stable-diffusion

pip is the package manager for this git repo. Install all the dependencies.

pip install -e .

Step 4 - Get API Token from Hugging Face

Register an account on Hugging Face and generate an API Access Token.

Log into Hugging Face via Terminal

huggingface-cli login

Step 5 - Convert Stable Diffusion Model to Apple's CoreML

Create a folder to place AI models.

mkdir ~/Desktop/AI_MODELS

Pick the version of Stable Diffusion you want to run on Hugging Face via Hugging Face Hub.

5.1 Converting Default Model: CompVis/Default Model is v1.5.

Generate Apple's CoreML model files from Hugging Face and store them in AI_MODELS.

python -m python_coreml_stable_diffusion.torch2coreml --convert-unet --convert-text-encoder --model-version runwayml/stable-diffusion-v1-5 --convert-vae-decoder --convert-safety-checker -o ~/Desktop/AI_MODELS

5.2 Converting Different Model: dreamlike-artdreamlike-photoreal-2.0

I will use the non-default dreamlike-photoreal-2.0 for this example.

python -m python_coreml_stable_diffusion.torch2coreml --convert-unet --convert-text-encoder --convert-safety-checker -o ~/Desktop/AI_MODELS --model-version m-a-p/music2vec-v1

Wait 20+ minutes!

Step 6 - Generate an Image!

Create a directory for your upcoming images

mkdir ~/Desktop/my-images

Change the directory into your Git repo.

cd ~/Desktop/ml-stable-diffusion

Run this command. Please refer to the Github Docs to better understand how --compute-unit, --prompt. I suggest reading this article to better understand --seed.

python -m python_coreml_stable_diffusion.pipeline \
          -i ~/Documents/AI_MODELS \
          -o ~/Desktop/my-images \
          --compute-unit CPU_AND_NE \
          --seed <enter a 3-4 digit number> \
          --prompt <enter your prompt here>