Stable Diffusion setup for Apple Silicon-based Macs

If you're one of the few people that have not yet got a local version of Stable Diffusion up and running – and you have a computer with an Apple Silicon based processor – I've got something for you...

The following bash script will set up a local conda-environment ("m1sd"), along with any and all dependencies – including Homebrew and Conda, if they are not already installed on your system.

It'll pause once and ask you to perform the one necessary manual step necessary (the download-portion of which can take a little time) before continuing at your request.

It'll also remove the built-in NSFW-filter (which is a little trigger-happy, IMO), so you won't get rickrolled by the machine learning model.

Run this script in a folder you'd like to set up Stable Diffusion in:

#!/bin/env bash

if ! brew &> /dev/null
then
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi

if ! which conda &> /dev/null
then
    brew install conda
fi

git clone -b apple-silicon-mps-support https://github.com/bfirsh/stable-diffusion.git m1-stable-diffusion
m1-stable-diffusion
cd m1-stable-diffusion

brew install Cmake protobuf rust

conda create --name m1sd python==3.10
conda activate m1sd
pip install -r requirements.txt

mkdir -p models/ldm/stable-diffusion-v1/

echo ""
echo "Now you need to do the following:"
echo "    - Set up an account on https://huggingface.co/"
echo "    - Go to https://huggingface.co/CompVis/stable-diffusion-v-1-4-original and agree to the license."
FOLDER=`pwd`/models/ldm/stable-diffusion-v1/
echo "    - Download the file named \"sd-v1-4.ckpt\"; save it to $FOLDER as \"model.ckpt\""
read -p "Press enter to continue"
echo ""

#remove NSFW filter
cp scripts/txt2img.py scripts/txt2img.py.BAK
sed -i ''  's/x_checked_image, has_nsfw_concept = check_safety(x_samples_ddim)/x_checked_image = x_samples_ddim/g' scripts/txt2img.py
awk '/# load safety model/ {suppress=1} /^\s*$/ {suppress=0} !suppress' scripts/txt2img.py > scripts/tmp.py && mv -f scripts/tmp.py scripts/txt2img.py
cd -
cd ..

I'll probably host a proper installation script later; For now, just run the commands above, and you should be up and running in no time!

Newer post
PiP-ify All the Things!
Older post
AppSec Village DEF CON 29 CTF Writeup