
在Apple设备上实现高效稳定的AI图像生 成
ml-stable-diffusion是一个开源项目,旨在优化Stable Diffusion模型在Apple设备上的运行。它包含用于模型转换的Python工具和用于iOS/macOS应用集成的Swift包。通过权重压缩等技术,该项目显著提升了性能和内存效率,使开发者能够在Apple平台应用中实现高质量的AI图像生成。
Run Stable Diffusion on Apple Silicon with Core ML
This repository comprises:
python_coreml_stable_diffusion, a Python package for converting PyTorch models to Core ML format and performing image generation with Hugging Face diffusers in PythonStableDiffusion, a Swift package that developers can add to their Xcode projects as a dependency to deploy image generation capabilities in their apps. The Swift package relies on the Core ML model files generated by python_coreml_stable_diffusionIf you run into issues during installation or runtime, please refer to the FAQ section. Please refer to the System Requirements section before getting started.
<img src="assets/readme_reel.png">Model Conversion:
| macOS | Python | coremltools |
|---|---|---|
| 13.1 | 3.8 | 7.0 |
Project Build:
| macOS | Xcode | Swift |
|---|---|---|
| 13.1 | 14.3 | 5.8 |
Target Device Runtime:
| macOS | iPadOS, iOS |
|---|---|
| 13.1 | 16.2 |
Target Device Runtime (With Memory Improvements):
| macOS | iPadOS, iOS |
|---|---|
| 14.0 | 17.0 |
Target Device Hardware Generation:
| Mac | iPad | iPhone |
|---|---|---|
| M1 | M1 | A14 |
stabilityai/stable-diffusion-2-1-base (512x512)
| Device | --compute-unit | --attention-implementation | End-to-End Latency (s) | Diffusion Speed (iter/s) |
|---|---|---|---|---|
| iPhone 12 Mini | CPU_AND_NE | SPLIT_EINSUM_V2 | 18.5* | 1.44 |
| iPhone 12 Pro Max | CPU_AND_NE | SPLIT_EINSUM_V2 | 15.4 | 1.45 |
| iPhone 13 | CPU_AND_NE | SPLIT_EINSUM_V2 | 10.8* | 2.53 |
| iPhone 13 Pro Max | CPU_AND_NE | SPLIT_EINSUM_V2 | 10.4 | 2.55 |
| iPhone 14 | CPU_AND_NE | SPLIT_EINSUM_V2 | 8.6 | 2.57 |
| iPhone 14 Pro Max | CPU_AND_NE | SPLIT_EINSUM_V2 | 7.9 | 2.69 |
| iPad Pro (M1) | CPU_AND_NE | SPLIT_EINSUM_V2 | 11.2 | 2.19 |
| iPad Pro (M2) | CPU_AND_NE | SPLIT_EINSUM_V2 | 7.0 | 3.07 |
benchmark branch of the Diffusers apptokenizer.model_max_length) in the text token sequence regardless of the actual length of the input text.* indicates that the reduceMemory option was enabled which loads and unloads models just-in-time to avoid memory shortage. This added up to 2 seconds to the end-to-end latency.--compute-unit and --attention-implementation values per device. The former does not modify the Core ML model and can be applied during runtime. The latter modifies the Core ML model. Note that the best performing compute unit is model version and hardware-specific.--attention-implementation) are generally applicable to Transformers and not customized to Stable Diffusion. Better performance may be observed upon custom kernel tuning. Therefore, these numbers do not represent peak HW capability.stabilityai/stable-diffusion-xl-base-1.0-ios (768x768)
| Device | --compute-unit | --attention-implementation | End-to-End Latency (s) | Diffusion Speed (iter/s) |
|---|---|---|---|---|
| iPhone 12 Pro | CPU_AND_NE | SPLIT_EINSUM | 116* | 0.50 |
| iPhone 13 Pro Max | CPU_AND_NE | SPLIT_EINSUM | 86* | 0.68 |
| iPhone 14 Pro Max | CPU_AND_NE | SPLIT_EINSUM | 77* | 0.83 |
| iPhone 15 Pro Max | CPU_AND_NE | SPLIT_EINSUM | 31 | 0.85 |
| iPad Pro (M1) | CPU_AND_NE | SPLIT_EINSUM | 36 | 0.69 |
| iPad Pro (M2) | CPU_AND_NE | SPLIT_EINSUM | 27 | 0.98 |
benchmark branch of the Diffusers appUnet.mlmodelc is compressed to 4.04 bit precision following the Mixed-Bit Palettization algorithm recipe published hereUnet.mlmodelc are compressed to 16 bit precisionVAEDecoder.mlmodelc in order to enable float16 weight and activation quantization for the VAE model.--attention-implementation SPLIT_EINSUM is chosen in lieu of SPLIT_EINSUM_V2 due to the prohibitively long compilation time of the latter* indicates that the reduceMemory option was enabled which loads and unloads models just-in-time to avoid memory shortage. This added significant overhead to the end-to-end latency. Note that end-to-end latency difference between iPad Pro (M1) and iPhone 13 Pro Max despite identical diffusion speed.tokenizer.model_max_length) in the text token sequence regardless of the actual length of the input text.--compute-unit and --attention-implementation values per device. The former does not modify the Core ML model and can be applied during runtime. The latter modifies the Core ML model. Note that the best performing compute unit is model version and hardware-specific.--attention-implementation) are generally applicable to Transformers and not customized to Stable Diffusion. Better performance may be observed upon custom kernel tuning. Therefore, these numbers do not represent peak HW capability.stabilityai/stable-diffusion-xl-base-1.0 (1024x1024)
| Device | --compute-unit | --attention-implementation | End-to-End Latency (s) | Diffusion Speed (iter/s) |
|---|---|---|---|---|
| MacBook Pro (M1 Max) | CPU_AND_GPU | ORIGINAL | 46 | 0.46 |
| MacBook Pro (M2 Max) | CPU_AND_GPU | ORIGINAL | 37 | 0.57 |
| Mac Studio (M1 Ultra) | CPU_AND_GPU | ORIGINAL | 25 | 0.89 |
| Mac Studio (M2 Ultra) | CPU_AND_GPU | ORIGINAL | 20 | 1.11 |
StableDiffusion Swift pipeline.coremltools-7.0 supports advanced weight compression techniques for pruning, palettization and linear 8-bit quantization. For these techniques, coremltools.optimize.torch.* includes APIs that require fine-tuning to maintain accuracy at higher compression rates whereas coremltools.optimize.coreml.* includes APIs that are applied post-training and are data-free.
We demonstrate how data-free post-training palettization implemented in coremltools.optimize.coreml.palettize_weights enables us to achieve greatly improved performance for Stable Diffusion on mobile devices. This API implements the Fast Exact k-Means algorithm for optimal weight clustering which yields more accurate palettes. Using --quantize-nbits {2,4,6,8} during conversion is going to apply this compression to the unet and text_encoder models.
For best results, we recommend training-time palettization: coremltools.optimize.torch.palettization.DKMPalettizer if fine-tuning your model is feasible. This API implements the Differentiable k-Means (DKM) learned palettization algorithm. In this exercise, we stick to post-training palettization for the sake of simplicity and ease of reproducibility.
The Neural Engine is capable of accelerating models with low-bit palettization: 1, 2, 4, 6 or 8 bits. With iOS 17 and macOS 14, compressed weights for Core ML models can be just-in-time decompressed during runtime (as opposed to ahead-of-time decompression upon load) to match the precision of activation tensors. This yields significant memory savings and enables models to run on devices with smaller RAM (e.g. iPhone 12 Mini). In addition, compressed weights are faster to fetch from memory which reduces the latency of memory bandwidth-bound layers. The just-in-time decompression behavior depends on the compute unit, layer type and hardware generation.
| Weight Precision | --compute-unit | stabilityai/stable-diffusion-2-1-base generating "a high quality photo of a surfing dog" |
|---|---|---|
| 6-bit | cpuAndNeuralEngine | <img src="assets/palette6_cpuandne_readmereel.png"> |
| 16-bit | cpuAndNeuralEngine | <img src="assets/float16_cpuandne_readmereel.png"> |
| 16-bit | cpuAndGPU | <img src="assets/float16_gpu_readmereel.png"> |
Note that there are minor differences across 16-bit (float16) and 6-bit results. These differences are comparable to the differences across float16 and float32 or differences across compute units as exemplified above. We recommend a minimum of 6 bits for palettizing Stable Diffusion. Smaller number of bits (1, 2 and 4) will require either fine-tuning or advanced palettization techniques such as MBP.
Resources:
This section describes an advanced compression algorithm called Mixed-Bit Palettization (MBP) built on top of the Post-Training Weight Palettization tools and using the Weights Metadata API from coremltools.
MBP builds a per-layer "palettization


免费创建高清无水印Sora视频
Vora是一个免费创建高清无水印Sora视频的AI工具


最适合小白的AI自动化工作流平台
无需编码,轻松生成可复用、可变现的AI自动化工作流

大模型驱动的Excel数据处理工具
基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。


AI论文写作指导平台
AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。


AI一键生成PPT,就用博思AIPPT!
博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。


AI赋能电商视觉革命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商 家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无水印视频
最新版Sora2模型免费使用,一键生成无水印视频
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号