The neatest script for converting Stable Diffusion 1.5 models to Core ML
Features • Usage • Prerequisites • Troubleshooting
- ControlNet-ready
- Works with both CKPT and SafeTensors files
- Displays a log message and a system notification with status updates
- If a conversion fails, it will attempt it every 30s until it's successful
- Resume your conversions at any time by entering the same model name
- Automatically deletes all unnecessary files once the conversion is done
- When converting to
All
, asks whether to zip folders (useful for sharing purposes) - If zipped, asks whether to delete the model and its folders
- Some easter eggs 👀
Before running: be sure to meet the prerequisites, place the script in the same folder as the model you want to convert, and open it with a code editor since there is two folder paths that need to be adjusted. You'll find both at the start of the script:
When done, the script is ready to be used.
You can run it by dragging it into the Terminal app and then pressing Enter, or set it to (always) open with the Terminal App: right-click on the file → "Get Info" → "Open with" → "Terminal".
Once started, it will prompt you to specify your model's name:
When set, the script will proceed to display all the possible conversion options:
- CKPT → All
- CKPT → Diffusers
- SafeTensors → All
- SafeTensors → Diffusers
- Diffusers → ORIGINAL
- Diffusers → ORIGINAL 512x768
- Diffusers → ORIGINAL 768x512
- Diffusers → SPLIT_EINSUM
- Change model name
The CKPT → All
and SafeTensors → All
options will convert your model to Diffusers
, then Diffusers
to ORIGINAL
, ORIGINAL 512x768
, ORIGINAL 768x512
, and SPLIT_EINSUM
— all in one go.
All the steps will show a success or failure log message, including a visual and auditory system notification. In case you don't like the latter, you can change its behavior by going to "System Settings..." → "Notifications" → "Script Editor"; or by deleting all the lines in the script starting with osascript
.
The script assumes you have Homebrew, Wget, Xcode, and Miniconda (with the ml-stable-diffusion
environment) already installed. If not, please proceed as follows:
-
Install Homebrew and remember to follow the instructions under "Next steps"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
Install Wget
brew install wget
-
Download and install Xcode
-
Download and Install Miniconda
-
Once done, run the commands below according to their display order
git clone https://github.com/apple/ml-stable-diffusion.git
conda create -n coreml_stable_diffusion python=3.8 -y
conda activate coreml_stable_diffusion
cd ml-stable-diffusion
pip install -e .
pip install omegaconf
pip install safetensors
-
Download this Python script and place it in the same folder as the model
sd-to-coreml.sh: permission denied
: typechmod +x
, drag the script into the Terminal window, and then press Enter ⏎
This package is incompatible with this version of macOS
: after the "Software Licence Agreement" step, click on "Change Install Location..." and select "Install for me only"
-
xcrun: error: unable to find utility "coremlcompiler", not a developer tool or in PATH
: open Xcode and go to "Settings..." → "Locations" then click on the "Command Line Tools" drop-down menu and reselect the Command Line Tools version -
ModuleNotFoundError: No module named 'pytorch_lightning'
: while the condacoreml_stable_diffusion
environment is active, runpip install pytorch_lightning
Every time you see a similar message, you can solve it by installing what is requested via
pip install <name>
-
zsh: killed python
: your Mac has run out of memory. Close some memory-hungry applications you may have open and do the process again. Still not working? Reboot. Still not working? Usenice -n 10
before running the script. Still not working? Well,SPLIT_EINSUM
conversions tend to be the more demanding, so while converting, I often close all the other apps and leave my MacBook melting alone
-
If you get any of these
TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
WARNING:__main__:Casted the `beta`(value=0.0) argument of `baddbmm` op from int32 to float32 dtype for conversion!
WARNING:coremltools:Tuple detected at graph output. This will be flattened in the converted model.
WARNING:coremltools:Saving value type of int64 into a builtin type of int32, might lose precision!
You're fine