ubuntu + conda 的 stable-diffusion-webui 安装笔记
前提:安装好 nvidia 显卡驱动。ubuntu 20.04, miniconda 环境
1. 下载仓库
# clone web ui and go into its directory
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
cd stable-diffusion-webui
2. 创建 conda 虚拟环境
conda create -n sd python=3.10
3. 安装所需的包
# install torch with CUDA support. See https://pytorch.org/get-started/locally/ for more instructions if this fails. 该命令会自动安装好 cuda 和 cudnn
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia
安装后 测试 pytorch 是否安装好。
# -c cmd : program passed in as string (terminates option list)
python -c "import torch; print(torch.cuda.is_available())"
安装其它包:
# check if torch supports GPU; this must output "True". You need CUDA 11. installed for this. You might be able to use
# a different version, but this is what I tested.
# clone web ui and go into its directory
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
cd stable-diffusion-webui
# clone repositories for Stable Diffusion and (optionally) CodeFormer
mkdir repositories
git clone https://github.com/CompVis/stable-diffusion.git stable-diffusion-stability-ai
git clone https://github.com/CompVis/taming-transformers.git repositories/taming-transformers
git clone https://github.com/sczhou/CodeFormer.git repositories/CodeFormer
git clone https://github.com/salesforce/BLIP.git repositories/BLIP
# install requirements of Stable Diffusion
pip install transformers==4.19.2 diffusers invisible-watermark --prefer-binary
# install k-diffusion
# pip install git+https://github.com/crowsonkb/k-diffusion.git --prefer-binary
# 估计是需要替换为:
# git clone https://github.com/crowsonkb/k-diffusion.git repositories/k-diffusion
# (optional) install GFPGAN (face restoration)
pip install git+https://github.com/TencentARC/GFPGAN.git --prefer-binary
# (optional) install requirements for CodeFormer (face restoration)
pip install -r repositories/CodeFormer/requirements.txt --prefer-binary
# install requirements of web ui
pip install -r requirements.txt --prefer-binary
# update numpy to latest version
pip install -U numpy --prefer-binary
下载 sd 1.5 模型(v1-5-pruned-emaonly.ckpt)放到:stable-diffusion-webui/models/Stable-diffusion/ 下。网址:https://huggingface.co/runwayml/stable-diffusion-v1-5
问题1:Warning: k_diffusion not found at path stable-diffusion-webui/repositories/k-diffusion/k_diffusion/sampling.py
解决:
https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/1272#issuecomment-1262244055
问题2:ImportError: cannot import name 'get_device' from 'basicsr.utils.misc'
解决:https://github.com/sczhou/CodeFormer/issues/193#issuecomment-1503410609
4. 启动程序
python webui.py
上述命令会报以下问题:
问题3:No module 'xformers'. Proceeding without it.
需要使用如下命令才能使用 xformers:
# 参数列表见:modules/cmd_args.py
python webui.py --xformers
安装 xformers:
坑:不同版本的 torch 需要不同版本的 xformers。以上我们安装的是 2.0.1 版本的 torch,参考:https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/10494#issuecomment-1552353339 ,因此:
pip install xformers==0.0.20
否则你会在生成图像的时候会报如下错误:
NotImplementedError: No operator found for `memory_efficient_attention_forward` with inputs:
query : shape=(2, 4096, 8, 40) (torch.float16)
key : shape=(2, 4096, 8, 40) (torch.float16)
value : shape=(2, 4096, 8, 40) (torch.float16)
attn_bias : <class 'NoneType'>
p : 0.0
`flshattF` is not supported because:
xFormers wasn't build with CUDA support
Operator wasn't built - see `python -m xformers.info` for more info
`tritonflashattF` is not supported because:
xFormers wasn't build with CUDA support
requires A100 GPU
`cutlassF` is not supported because:
xFormers wasn't build with CUDA support
`smallkF` is not supported because:
xFormers wasn't build with CUDA support
dtype=torch.float16 (supported: {torch.float32})
max(query.shape[-1] != value.shape[-1]) > 32
unsupported embed per head: 40
最后我们就可以愉快的使用了:
python webui.py --xformers
输入 pandas:

5. 一键启动
配置一个 sd_web_ui 脚本:
#!/bin/bash
source activate sd
cd /xx/stable-diffusion-webui
python webui.py --xformers
然后我们就可以在命令行启动了:
./sd_web_ui
参考:
【1】https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Install-and-Run-on-NVidia-GPUs :Manual Installation