# ac 平台部署 Stable Diffusion 记录

# 官方版本(无 webui)

sd 官方仓库 https://github.com/Stability-AI/StableDiffusion

# 环境配置

进入 Eshell,加载 pytorch dtk23.04 环境

1
2
3
module rm compiler/rocm/2.9
module load compiler/rocm/dtk-23.04
module load apps/DeepLearning/PyTorch/1.13.1/pytorch-1.13.1-py3.9-dtk23.04

在登陆节点下载依赖

1
pip install transformers==4.19.2 diffusers invisible-watermark

下载官方仓库

1
2
# 超算服务器无法正常访问github,该链接为官方仓库同步镜像
git clone https://gitee.com/Cerber2ol8/StableDiffusion

安装依赖

首先修改 requirements.txt,否则会由于找不到合适版本的包无限循环

1
2
3
#gradio==3.11
# 改成
gradio>=3.11

然后执行安装

1
2
3
cd StableDiffusion.
pip install -r requirements.txt

不出意外的话会成功安装,失败的话可能是网络原因,多尝试几次即可

+++

# Xformers 加速注意力模块(可选)

(暂未实现)

+++

下载模型文件

1
2
3
mkdir ckpt && cd ckpt
wget v2-1_512-ema-pruned.ckpt # 这里为权重下载地址,也可以自己上传
cd ..

包含的模型

1
2
3
4
5
6
7
8
9
10
11
12
model:
base_learning_rate: 1.0e-4
target: ldm.models.diffusion.ddpm.LatentDiffusion
unet_config:
target: ldm.modules.diffusionmodules.openaimodel.UNetModel

first_stage_config:
target: ldm.models.autoencoder.AutoencoderKL

cond_stage_config:
target: ldm.modules.encoders.modules.FrozenOpenCLIPEmbedder

官方 ckpt 模型不包含 vit 和 clip 权重,正常情况下 pretrain 模型权重会自动从 huggingface 中进行缓存,但是超算计算节点是无法访问外部资源的,因此需要自己从 huggingface 下载,我选择的方式为在本地缓存之后上传。

在本机中 clone 仓库装完依赖,然后执行测试脚本,

1
python scripts/txt2img.py --prompt "a professional photograph of an astronaut riding a horse" --ckpt ckpt/v2-1_512-ema-pruned.ckpt --config configs/stable-diffusion/v2-inference.yaml --H 256 --W 256

等待 huggingface 的模型缓存完毕,然后将本机 ~/.cache/huggingface/hubmodels--laion--CLIP-ViT-H-14-laion2B-s32B-b79K 目录上传到超算的 ~/.cache/huggingface/hub 目录中去。

# 运行 sd

在用户目录新建一个作业脚本 sd.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
#SBATCH -o sd.%j.out
#SBATCH --partition=normal
#SBATCH -J SD_Job
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=4
#SBATCH --gres=dcu:1
#SBATCH --chdir=StableDiffusion
module purge
module rm compiler/rocm/2.9
module load compiler/devtoolset/7.3.1
module load mpi/hpcx/2.7.4/gcc-7.3.1
module load compiler/rocm/dtk-23.04
module load apps/DeepLearning/PyTorch/1.13.1/pytorch-1.13.1-py3.9-dtk23.04

python scripts/txt2img.py --prompt "a professional photograph of an astronaut riding a horse" --ckpt ckpt/v2-1_512-ema-pruned.ckpt --config configs/stable-diffusion/v2-inference.yaml --H 256 --W 256

执行该脚本

1
sbatch sd.sh

在 Stable Diffusion 目录下查看输出日志

image-20230918090244838

image-20230918090401493

输出的结果位于 Stable Diffusion/outpus/txt2img-samples

image-20230918100433578

# 已知问题

・模型版本不能为 v 版,只能使用 base 版本的模型配置

・待补充