Apple Silicon 上的 Stable Diffusion Core ML – 如何运行和优化

TL;DR

Hugging Face 发布了 Stable Diffusion 的 Core ML 版本(v1.4、v1.5、v2‑base、v2.1‑base),可在 Apple Silicon 本地运行,并提供了 Python 和 Swift 推理脚本以及可直接使用的 Mac App Store 应用。


可用的 Core ML 检查点

  • Stable Diffusion v1.4 – 转换后的模型位于 apple/coreml-stable-diffusion-v1-4
  • Stable Diffusion v1.5 – 转换后的模型位于 apple/coreml-stable-diffusion-v1-5
  • Stable Diffusion v2 base – 转换后的模型位于 apple/coreml-stable-diffusion-2-base
  • Stable Diffusion v2.1 base – 转换后的模型位于 apple/coreml-stable-diffusion-2-1-base

所有检查点均托管在 Hugging Face Hub 上,可在任何 Apple Silicon 设备上使用 CPU、GPU 或 Apple Neural Engine(NE)运行。提供了不同的变体(注意力实现方式和打包方式),以满足硬件和语言特定的需求。


性能变体与推荐

  • Attention implementationsoriginal(仅 CPU/GPU,有时更快) vs. split_einsum(兼容 CPU、GPU 和 NE)。根据设备能力进行选择。
  • Packagingpackages 用于 Python 推理;compiled 用于 Swift(将大型 UNet 拆分为多个 .mlmodelc 文件,以兼容 iOS/iPadOS)。
  • Best‑case benchmark – 在配备 macOS Ventura 13.1 Beta 4 的 MacBook Pro(M1 Max,32 GPU 核心,64 GB RAM)上,使用 original 注意力和全部计算单元时,Stable Diffusion v1.4 可在 18 秒 内生成图像。

⚠️ 注意 macOS Ventura 13.1 引入了这些模型所需的 Core ML 改进;较早的 macOS 版本可能会生成黑图像或运行速度更慢。


Python 推理工作流

前置条件

pip install huggingface_hub
pip install git+https://github.com/apple/ml-stable-diffusion

下载检查点

from huggingface_hub import snapshot_download
from pathlib import Path

repo_id = "apple/coreml-stable-diffusion-v1-4"
variant = "original/packages"
model_path = Path("./models") / (repo_id.split('/')[-1] + "_" + variant.replace('/', '_'))
snapshot_download(repo_id, allow_patterns=f"{variant}/*", local_dir=model_path, local_dir_use_symlinks=False)
print(f"Model downloaded at {model_path}")

运行推理

python -m python_coreml_stable_diffusion.pipeline \
  --prompt "a photo of an astronaut riding a horse on mars" \
  -i models/coreml-stable-diffusion-v1-4_original_packages \
  -o ./output.png \
  --compute-unit ALL \
  --seed 93
  • --compute-unit 选项:ALLCPU_AND_GPUCPU_ONLYCPU_AND_NE
  • 若要使用不同的检查点,添加 --model-version <hub-id>(例如 runwayml/stable-diffusion-v1-5)。

Swift 推理工作流

下载已编译的检查点

from huggingface_hub import snapshot_download
from pathlib import Path

repo_id = "apple/coreml-stable-diffusion-v1-4"
variant = "original/compiled"
model_path = Path("./models") / (repo_id.split('/')[-1] + "_" + variant.replace('/', '_'))
snapshot_download(repo_id, allow_patterns=f"{variant}/*", local_dir=model_path, local_dir_use_symlinks=False)
print(f"Model downloaded at {model_path}")

运行推理

git clone https://github.com/apple/ml-stable-diffusion
cd ml-stable-diffusion
swift run StableDiffusionSample \
  --resource-path models/coreml-stable-diffusion-v1-4_original_compiled \
  --compute-units all \
  "a photo of an astronaut riding a horse on mars"
  • --compute-units 可选值:allcpuOnlycpuAndGPUcpuAndNeuralEngine
  • 已编译的模型在应用启动时加载更快,从而在后续生成时略微降低延迟。

转换自定义模型(自带模型)

如果您对 Stable Diffusion 模型进行过微调或其他定制(例如 DreamBooth、Textual Inversion),则必须自行运行 Apple 的转换脚本。请遵循 Apple 仓库中的官方说明:https://github.com/apple/ml-stable-diffusion#converting-models-to-coreml


后续步骤与社区机会

  • 构建在 macOS、iPhone 和 iPad 上本地运行、完全在设备上生成图像的应用。
  • 在 Swift 中集成更多调度器,以实现更快的采样。
  • 将流水线扩展到其他生成任务(例如修复、放大)。
  • 探索量化及其他优化,以进一步降低延迟和内存占用。

发布的 Core ML 检查点和工具降低了开发者在 Apple Silicon 上使用 Stable Diffusion 的门槛,为隐私保护的离线生成式 AI 体验打开了大门。

Sources