π Maple Diffusion
Maple Diffusion runs Stable Diffusion models locally on macOS / iOS devices, in Swift, using the MPSGraph framework (not Python).
Maple Diffusion should be capable of generating a reasonable image in a minute or two on a recent iPhone (I get around ~2.3s / step on an iPhone 13 Pro).
To attain usable performance without tripping over iOS's 4GB memory limit, Maple Diffusion relies internally on FP16 (NHWC) tensors, operator fusion from MPSGraph, and a truly pitiable degree of swapping models to device storage.
On macOS, Maple Diffusion uses slightly more memory (~6GB), to reach <1s / step.
Related Projects
- Core ML Stable Diffusion (repo) is Apple's recommended way of running Stable Diffusion in Swift, using CoreML instead of MPSGraph. CoreML was originally much slower than MPSGraph (I tried it back in August), but Apple has improved CoreML performance a lot on recent macOS / iOS versions.
- Native Diffusion (repo) is a Swift Package-ified version of this codebase with several improvements (including image-to-image)
- Waifu Art AI (announcement, App Store link) is an iOS / macOS app for (anime-style) Stable Diffusion based on this codebase
- Draw Things (announcement, App Store link) is an iOS app for Stable Diffusion (using an independent codebase with similar MPSGraph-based approach)
Device Requirements
Maple Diffusion should run on any Apple Silicon Mac (M1, M2, etc.). Intel Macs should also work now thanks to this PR.
Maple Diffusion should run on any iOS device with sufficient RAM (β₯6144MB RAM definitely works; 4096MB doesn't). That means recent iPads should work out of the box, and recent iPhones should work if you can get the Increase Memory Limit
capability working (to unlock 4GB of app-usable RAM). iPhone 14 variants reportedly didn't work until iOS 16.1 stable.
Maple Diffusion currently expects Xcode 14 and iOS 16; other versions may require changing build settings or just not work. iOS 16.1 (beta) was reportedly broken and always generating a gray image, but I think that's fixed
Usage
To build and run Maple Diffusion:
-
Download a Stable Diffusion PyTorch model checkpoint (
sd-v1-4.ckpt
, or some derivation thereof) -
Download this repo
git clone https://github.com/madebyollin/maple-diffusion.git && cd maple-diffusion
-
Setup & install Python with PyTorch, if you haven't already.
# may need to install conda first https://github.com/conda-forge/miniforge#homebrew conda deactivate conda remove -n maple-diffusion --all conda create -n maple-diffusion python=3.10 conda activate maple-diffusion pip install torch typing_extensions numpy Pillow requests pytorch_lightning
-
Convert the PyTorch model checkpoint into a bunch of fp16 binary blobs.
./maple-convert.py ~/Downloads/sd-v1-4.ckpt
-
Open the
maple-diffusion
Xcode project. Select the device you want to run on from theProduct > Destination
menu. -
Manually add the
Increased Memory Limit
capability to themaple-diffusion
target (this step might not be needed on iPads, but it's definitely needed on iPhones - the default limit is 3GB). -
Build & run the project on your device with the
Product > Run
menu.