首页
/ Stable Diffusion C++ 项目下载及安装教程

Stable Diffusion C++ 项目下载及安装教程

2024-12-09 22:51:36作者:郁楠烈Hubert

1. 项目介绍

Stable Diffusion C++ 是一个基于纯 C/C++ 实现的 Stable Diffusion 和 Flux 推理项目。该项目具有以下特点:

  • 基于 ggml 的纯 C/C++ 实现,类似于 llama.cpp。
  • 超轻量级,无外部依赖。
  • 支持 SD1.x、SD2.x、SDXL 和 SD3/SD3.5。
  • 支持 SD-Turbo 和 SDXL-Turbo。
  • 支持 PhotoMaker。
  • 支持 16-bit 和 32-bit 浮点数。
  • 支持 2-bit、3-bit、4-bit、5-bit 和 8-bit 整数量化。
  • 支持加速内存高效的 CPU 推理。
  • 支持 AVX、AVX2 和 AVX512。
  • 支持 CUDA、Metal、Vulkan 和 SYCL 后端。
  • 支持加载 ckpt、safetensors 和 diffusers 模型/检查点。
  • 支持 Flash Attention 以优化内存使用。

2. 项目下载位置

项目源代码托管在 GitHub 上,可以通过以下命令进行下载:

git clone --recursive https://github.com/leejet/stable-diffusion.cpp.git

3. 项目安装环境配置

3.1 环境要求

  • 操作系统:Linux、Mac OS、Windows、Android(通过 Termux)
  • 编译器:支持 C++11 的编译器(如 GCC、Clang)
  • 依赖库:OpenBLAS、CUBLAS、HipBLAS、Metal、Vulkan、SYCL(根据需要选择)

3.2 环境配置示例

3.2.1 Linux 环境配置

Linux 环境配置示例

3.2.2 Windows 环境配置

Windows 环境配置示例

4. 项目安装方式

4.1 从源码编译

4.1.1 下载源码

git clone --recursive https://github.com/leejet/stable-diffusion.cpp.git
cd stable-diffusion.cpp

4.1.2 更新子模块

如果已经克隆了仓库,可以使用以下命令更新子模块:

git pull origin master
git submodule init
git submodule update

4.1.3 下载模型权重

下载原始权重(ckpt 或 safetensors),例如:

curl -L -O https://huggingface.co/CompVis/stable-diffusion-v-1-4-original/resolve/main/sd-v1-4.ckpt

4.1.4 编译

mkdir build
cd build
cmake ..
cmake --build . --config Release

4.2 使用预编译的可执行文件

对于大多数用户,可以直接从项目的最新发布版本中下载预编译的可执行文件。

5. 项目处理脚本

5.1 文本生成图像

使用 SYCL 后端生成图像的示例:

./bin/sd -m /models/sd3_medium_incl_clips_t5xxlfp16.safetensors --cfg-scale 5 --steps 30 --sampling-method euler -H 1024 -W 1024 --seed 42 -p "fantasy medieval village world inside a glass sphere high detail fantasy realistic light effect hyper detail volumetric lighting cinematic macro depth of field blur red light and clouds from the back highly detailed epic cinematic concept art cg render made in maya blender and photoshop octane render excellent composition dynamic dramatic cinematic lighting aesthetic very inspirational world inside a glass sphere by james gurney by artgerm with james jean joe fenton and tristan eaton by ross tran fine details 4k resolution"

5.2 启用 Flash Attention

启用 Flash Attention 可以减少内存使用,例如:

./bin/sd --flash-attention

通过以上步骤,您可以成功下载并安装 Stable Diffusion C++ 项目,并使用其提供的功能进行图像生成和处理。

热门项目推荐
相关项目推荐