
WSL 上の Web UI で xFormers をインストールする
Stable Diffusion Web UI を Windows Subsystem for Linux (WSL) で動かす際の効率化に必要なモジュールの一つである xFormers のインストールに至るまでのコンパイル段階で手を焼いたので、メモがてらに記録に残すことにした。情報は2023年1月15日に得たものであるゆえ、内容を実践される際は気をつけてお試しください。
目次
動作確認
Windows Subsystem for Linux 上の Ubuntu 20.04.5 で動作確認済み。

下は Python のバージョン。
python3 --version
Python 3.8.10
手順
xFormers をソースからコンパイルするには、CUDA のバージョンが 11.3 でなければいけない。それ以上のバージョンでコンパイルする場合、大量のエラーを吐いて停止する。
python3-dev のインストール
コンパイル要件の1つである、 python3-dev をインストールする。これがないとコンパイル時にfatal error: Python.h: No such file or directory
みたいなエラーを吐いて死ぬので準備しておくこと。
apt install python3-dev
CUDA 11.3 のインストール
一部、CUDA のバージョンは 11.x であれば xFormers のコンパイルには差し障りないという旨の情報をよく見かけるが、筆者の場合はより新しいバージョンである 11.8 のインストール後にコンパイルを試行したところ、実行時にバージョンの不一致でエラーが出力された。そのため、本稿ではコンパイルの要件である古い 11.3 をインストールすることにする。
CUDA Toolkit 11.3 Downloads に進み、 cuda-repo-wsl-ubuntu-11-3-local_11.3.0-1_amd64.deb
をダウンロードする。ダウンロードしたら、 Installation Instructions の通りにインストールする。
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.3.0/lo…nstallers/cuda-repo-wsl-ubuntu-11-3-local_11.3.0-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-11-3-local_11.3.0-1_amd64.deb
sudo apt-key add /var/cuda-repo-wsl-ubuntu-11-3-local/7fa2af80.pub
sudo apt-get update
sudo apt-get -y install cuda
cuDNN のインストール
下のリンクを参考に、cuDNN をインストールする。deb をダウンロードするのにアカウントが必要だった。以前は必要なかったはずなので、少々遺憾に思う。
cd
sudo dpkg -i cudnn-local-repo-ubuntu2204-8.6.0.163_1.0-1_amd64.deb
sudo cp /var/cudnn-local-repo-ubuntu2204-8.6.0.163/cudnn-local-FAED14DD-keyring.gpg /usr/share/keyrings/
sudo apt -y update
sudo apt install libcudnn8
sudo apt install libcudnn8-dev
ninja (ビルドツール) のインストール
任意だが、 xFormers のコンパイルの高速化に必要。webui の仮想環境に入ってから、pip で ninja をインストールする。ninja って聞くと忍者ツールズを思い出すのは古のネット民としての性なのだろうか。
pip install ninja
コンパイル & インストールする
webui のディレクトリへ行き、下の Linux の場合の手順に従ってコンパイルする。
source ./venv/bin/activate
cd repositories
git clone https://github.com/facebookresearch/xformers.git
cd xformers
git submodule update --init --recursive
pip install -r requirements.txt
pip install -e .
https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Xformers
うまくいけばこんな感じに Successfully installed xformers
と表示される。
(venv) ubuntu@arithmetica ~/stable-diffusion-webui/repositories/xformers$ pip install -e .
Obtaining file:///home/ubuntu/stable-diffusion-webui/repositories/xformers
Requirement already satisfied: numpy in /home/ubuntu/stable-diffusion-webui/venv/lib/python3.8/site-packages (from xformers==0.0.16+5db2259.d20230115) (1.23.3)
Requirement already satisfied: pyre-extensions==0.0.23 in /home/ubuntu/stable-diffusion-webui/venv/lib/python3.8/site-packages (from xformers==0.0.16+5db2259.d20230115) (0.0.23)
Requirement already satisfied: torch>=1.12 in /home/ubuntu/stable-diffusion-webui/venv/lib/python3.8/site-packages (from xformers==0.0.16+5db2259.d20230115) (1.12.1+cu113)
Requirement already satisfied: typing-extensions in /home/ubuntu/stable-diffusion-webui/venv/lib/python3.8/site-packages (from pyre-extensions==0.0.23->xformers==0.0.16+5db2259.d20230115) (4.4.0)
Requirement already satisfied: typing-inspect in /home/ubuntu/stable-diffusion-webui/venv/lib/python3.8/site-packages (from pyre-extensions==0.0.23->xformers==0.0.16+5db2259.d20230115) (0.8.0)
Requirement already satisfied: mypy-extensions>=0.3.0 in /home/ubuntu/stable-diffusion-webui/venv/lib/python3.8/site-packages (from typing-inspect->pyre-extensions==0.0.23->xformers==0.0.16+5db2259.d20230115) (0.4.3)
Installing collected packages: xformers
Running setup.py develop for xformers
Successfully installed xformers
後処理
webui-user.sh
の起動オプションに --xformers
を追加したら、作業は以上になる。