leejet/stable-diffusion.cpp
GitHub: leejet/stable-diffusion.cpp
纯 C/C++ 实现的扩散模型推理引擎,无需 Python 依赖即可本地运行 SD、FLUX、Wan 等图像和视频生成模型。
Stars: 5575 | Forks: 550
# stable-diffusion.cpp
纯 C/C++ 实现的 Diffusion model(SD,Flux,Wan,...) 推理
***请注意,本项目正处于积极开发中。 \
API 和命令行选项可能会频繁更改。***
## 🔥重要新闻
* **2026/01/18** 🚀 stable-diffusion.cpp 现已支持 **FLUX.2-klein**
👉 详情:[PR #1193](https://github.com/leejet/stable-diffusion.cpp/pull/1193)
* **2025/12/01** 🚀 stable-diffusion.cpp 现已支持 **Z-Image**
👉 详情:[PR #1020](https://github.com/leejet/stable-diffusion.cpp/pull/1020)
* **2025/11/30** 🚀 stable-diffusion.cpp 现已支持 **FLUX.2-dev**
👉 详情:[PR #1016](https://github.com/leejet/stable-diffusion.cpp/pull/1016)
* **2025/10/13** 🚀 stable-diffusion.cpp 现已支持 **Qwen-Image-Edit / Qwen-Image-Edit 2509**
👉 详情:[PR #877](https://github.com/leejet/stable-diffusion.cpp/pull/877)
* **2025/10/12** 🚀 stable-diffusion.cpp 现已支持 **Qwen-Image**
👉 详情:[PR #851](https://github.com/leejet/stable-diffusion.cpp/pull/851)
* **2025/09/14** 🚀 stable-diffusion.cpp 现已支持 **Wan2.1 Vace**
👉 详情:[PR #819](https://github.com/leejet/stable-diffusion.cpp/pull/819)
* **2025/09/06** 🚀 stable-diffusion.cpp 现已支持 **Wan2.1 / Wan2.2**
👉 详情:[PR #778](https://github.com/leejet/stable-diffusion.cpp/pull/778)
## 功能特性
- 基于 [ggml](https://github.com/ggml-org/ggml) 的纯 C/C++ 实现,工作方式类似于 [llama.cpp](https://github.com/ggml-org/llama.cpp)
- 超轻量级,无外部依赖
- 支持的模型
- 图像模型
- SD1.x, SD2.x, [SD-Turbo](https://huggingface.co/stabilityai/sd-turbo)
- SDXL, [SDXL-Turbo](https://huggingface.co/stabilityai/sdxl-turbo)
- [部分 SD1.x 和 SDXL 蒸馏模型](./docs/distilled_sd.md)
- [SD3/SD3.5](./docs/sd3.md)
- [FLUX.1-dev/FLUX.1-schnell](./docs/flux.md)
- [FLUX.2-dev/FLUX.2-klein](./docs/flux2.md)
- [Chroma](./docs/chroma.md)
- [Chroma1-Radiance](./docs/chroma_radiance.md)
- [Qwen Image](./docs/qwen_image.md)
- [Z-Image](./docs/z_image.md)
- [Ovis-Image](./docs/ovis_image.md)
- [Anima](./docs/anima.md)
- 图像编辑模型
- [FLUX.1-Kontext-dev](./docs/kontext.md)
- [Qwen Image Edit 系列](./docs/qwen_image_edit.md)
- 视频模型
- [Wan2.1/Wan2.2](./docs/wan.md)
- 支持 [PhotoMaker](https://github.com/TencentARC/PhotoMaker)。
- 支持 SD 1.5 的 Control Net。
- 支持 LoRA,与 [stable-diffusion-webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#lora) 相同。
- 支持 Latent Consistency Models (LCM/LCM-LoRA)。
- 使用 [TAESD](https://github.com/madebyollin/taesd) 进行更快且更省内存的 latent 解码。
- 使用 [ESRGAN](https://github.com/xinntao/Real-ESRGAN) 对生成的图像进行放大。
- 支持的后端
- CPU (支持 x86 架构的 AVX, AVX2 和 AVX512)
- CUDA
- Vulkan
- Metal
- OpenCL
- SYCL
- 支持的权重格式
- Pytorch checkpoint (`.ckpt` 或 `.pth`)
- Safetensors (`.safetensors`)
- GGUF (`.gguf`)
- 支持的平台
- Linux
- Mac OS
- Windows
- Android (通过 Termux, [Local Diffusion](https://github.com/rmatif/Local-Diffusion))
- Flash Attention 用于优化内存使用。
- Negative prompt。
- [stable-diffusion-webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui) 风格的 tokenizer(并非所有功能,目前仅支持 token 加权)。
- VAE tiling 处理以减少内存使用。
- 采样方法
- `Euler A`
- `Euler`
- `Heun`
- `DPM2`
- `DPM++ 2M`
- [`DPM++ 2M v2`](https://github.com/AUTOMATIC1111/stable-diffusion-webui/discussions/8457)
- `DPM++ 2S a`
- [`LCM`](https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/13952)
- 跨平台可复现性
- `--rng cuda`,默认值,与 `stable-diffusion-webui GPU RNG` 一致。
- `--rng cpu`,与 `comfyui RNG` 一致。
- 将生成参数作为 webui 兼容的文本字符串嵌入到 png 输出中。
## 快速开始
### 获取 sd 可执行文件
- 从 [releases 页面](https://github.com/leejet/stable-diffusion.cpp/releases) 下载预构建的二进制文件。
- 或者按照[构建指南](./docs/build.md)从源代码构建。
### 下载模型权重
- 下载权重(.ckpt 或 .safetensors 或 .gguf)。例如
- 来自 https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5 的 Stable Diffusion v1.5
curl -L -O https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors
### 仅需一条命令即可生成图像
```
./bin/sd-cli -m ../models/v1-5-pruned-emaonly.safetensors -p "a lovely cat"
```
***有关详细的命令行参数,请查看 [cli 文档](./examples/cli/README.md)。***
## 性能
如果您想提高性能或减少 VRAM/RAM 使用量,请参阅[性能指南](./docs/performance.md)。
## 更多指南
- [SD1.x/SD2.x/SDXL](./docs/sd.md)
- [SD3/SD3.5](./docs/sd3.md)
- [FLUX.1-dev/FLUX.1-schnell](./docs/flux.md)
- [FLUX.2-dev/FLUX.2-klein](./docs/flux2.md)
- [FLUX.1-Kontext-dev](./docs/kontext.md)
- [Chroma](./docs/chroma.md)
- [🔥Qwen Image](./docs/qwen_image.md)
- [🔥Qwen Image Edit 系列](./docs/qwen_image_edit.md)
- [🔥Wan2.1/Wan2.2](./docs/wan.md)
- [🔥Z-Image](./docs/z_image.md)
- [Ovis-Image](./docs/ovis_image.md)
- [Anima](./docs/anima.md)
- [LoRA](./docs/lora.md)
- [LCM/LCM-LoRA](./docs/lcm.md)
- [使用 PhotoMaker 个性化图像生成](./docs/photo_maker.md)
- [使用 ESRGAN 放大结果](./docs/esrgan.md)
- [使用 TAESD 加速解码](./docs/taesd.md)
- [Docker](./docs/docker.md)
- [量化和 GGUF](./docs/quantization_and_gguf.md)
- [通过缓存加速推理](./docs/caching.md)
## 绑定
这些项目封装了 `stable-diffusion.cpp`,以便在其他语言/框架中更轻松地使用。
* Golang (non-cgo): [seasonjs/stable-diffusion](https://github.com/seasonjs/stable-diffusion)
* Golang (cgo): [Binozo/GoStableDiffusion](https://github.com/Binozo/GoStableDiffusion)
* C#: [DarthAffe/StableDiffusion.NET](https://github.com/DarthAffe/StableDiffusion.NET)
* Python: [william-murray1204/stable-diffusion-cpp-python](https://github.com/william-murray1204/stable-diffusion-cpp-python)
* Rust: [newfla/diffusion-rs](https://github.com/newfla/diffusion-rs)
* Flutter/Dart: [rmatif/Local-Diffusion](https://github.com/rmatif/Local-Diffusion)
## UI
这些项目使用 `stable-diffusion.cpp` 作为其图像生成的后端。
- [Jellybox](https://jellybox.com)
- [Stable Diffusion GUI](https://github.com/fszontagh/sd.cpp.gui.wx)
- [Stable Diffusion CLI-GUI](https://github.com/piallai/stable-diffusion.cpp)
- [Local Diffusion](https://github.com/rmatif/Local-Diffusion)
- [sd.cpp-webui](https://github.com/daniandtheweb/sd.cpp-webui)
- [LocalAI](https://github.com/mudler/LocalAI)
- [Neural-Pixel](https://github.com/Luiz-Alcantara/Neural-Pixel)
- [KoboldCpp](https://github.com/LostRuins/koboldcpp)
## Star 历史
[](https://star-history.com/#leejet/stable-diffusion.cpp&Date)
## 参考
- [ggml](https://github.com/ggml-org/ggml)
- [diffusers](https://github.com/huggingface/diffusers)
- [stable-diffusion](https://github.com/CompVis/stable-diffusion)
- [sd3-ref](https://github.com/Stability-AI/sd3-ref)
- [stable-diffusion-stability-ai](https://github.com/Stability-AI/stablediffusion)
- [stable-diffusion-webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui)
- [ComfyUI](https://github.com/comfyanonymous/ComfyUI)
- [k-diffusion](https://github.com/crowsonkb/k-diffusion)
- [latent-consistency-model](https://github.com/luosiallen/latent-consistency-model)
- [generative-models](https://github.com/Stability-AI/generative-models/)
- [PhotoMaker](https://github.com/TencentARC/PhotoMaker)
- [Wan2.1](https://github.com/Wan-Video/Wan2.1)
- [Wan2.2](https://github.com/Wan-Video/Wan2.2)
标签:AIGC, Apex, C++, Cpp, FLUX, ggml, Qwen-Image, SD-Turbo, SDXL, Stable Diffusion, UML, Vectored Exception Handling, Veh, Wan, Z-Image, 云安全, 人工智能, 图像生成, 图像编辑, 实时告警, 扩散模型, 推理引擎, 数据擦除, 文生图, 无依赖, 本地部署, 机器学习, 模型量化, 深度学习, 生成式AI, 用户模式Hook绕过, 索引, 计算机视觉, 高性能计算