The neatest script for converting Stable Diffusion 1.5 models to Core ML
Features β’ Usage β’ Prerequisites β’ Troubleshooting
All
, asks whether to zip folders (useful for sharing purposes)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:
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
: type chmod +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 conda coreml_stable_diffusion
environment is active, run
pip 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? Use nice -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