スポンサーリンク

Stable Diffusion WebUI の PyTorch を 2.0 にアップデートする、他

Hydrangea_in_the_rain
記事内に広告が含まれています。
スポンサーリンク

Stable Diffusion WebUI を最新版に更新したい

WSL2 含む Ubuntu に NVIDIA CUDA をインストールする方法、アンインストールする方法については「こちらのページ 」で紹介しています。複数の CUDA のバージョンが共存できます。
WSL2 含む Ubuntu に NVIDIA cuDNN ライブラリをインストールする方法について「こちらのページ 」で紹介しています。cuDNN ライブラリのダウンロードは要ユーザー登録です。
2024年5月2日現在の最新の Stable Diffusion WebUI の最新バージョンは、4月23日に公開された v1.9.3 です。v1.7.0 を SDXL 1.0 を含め WSL2 Ubuntu 22.04 LTS にゼロからインストールする方法は「こちらのページ 」、v1.8.0 へバージョンアップする記事は「こちらのページ 」、v1.9.0 へバージョンアップする記事は「こちらのページ 」に記載しています。

私は Stable Diffusion WebUI を WSL2 Ubuntu 22.04 LTS にインストールして、主に WordPress のアイキャッチ画像の生成に使用しています。

これまでは特にアップデートしないで使用していましたが、進歩の速いソフトウェアですので、最新の状態に追従したいという気持ちは感じていました。とはいえ、生成する環境が使えなくなるのも困ります。

PyTorch 2.0 へ更新すると、画像の生成速度が向上するようですので、この週末に Stable Diffusion WebUI の更新と、それに伴うトラブル対処をしましたので、記事としてまとめました。

また、細かなトラブルもありましたので、解消する方法について記載しました。

20230822 追記 :

アップデートではなく、ゼロから WSL2 Ubuntu 上に Stable Diffusion WebUI を構築する方法についてまとめました。Stable Diffusion XL 1.0 (SDXL 1.0) のモデルを使用する方法です。PyTorch-2.0.1 & CUDA 11.8 です。

スポンサーリンク

ディレクトリを開くアイコンを押すと wsl-open が無いとエラー

Stable Diffusion WebUI を WSL2 で動作させて、生成した画像が保存されているフォルダを開こうと、下図のフォルダアイコン (Open images output directory) を選択すると、wsl-open が見つからないというエラーが表示されます。

Open images output directory
FileNotFoundError: [Errno 2] No such file or directory: 'wsl-open'
GitHub - 4U6U57/wsl-open: Open files with xdg-open on Bash for Windows in Windows applications. Read only mirror from GitLab, see link :point_right:
Open files with xdg-open on Bash for Windows in Windows applications. Read only mirror from GitLab, see link :point_righ...

npm で wsl-open を導入すると改善できます。

$ sudo apt install -y npm
$ sudo npm install -g wsl-open

画像が保存されているディレクトリを Windows11 の Explorer で開く事が出来るようになります。

上記はグローバルで wsl-open をインストールしていますが、ローカル環境で npm を既にインストールしている場合には、以下の記事を参照ください。

Stable Diffusion WebUI のアップデート方法

Stable Diffusion WebUI は頻繁に更新されていますので、新しい機能を使用したい場合にはアップデートする必要があります。アップデートの方法については以下のページを参考にしました。

Stable Diffusion WebUI をインストールしたディレクトリに移動して、以下のコマンドを実行します。

$ git pull

ただ、私の環境ではそのままでは更新できませんでした。以下のメッセージが表示されます。

$ git pull
remote: Enumerating objects: 919, done.
remote: Counting objects: 100% (830/830), done.
remote: Compressing objects: 100% (261/261), done.
remote: Total 919 (delta 566), reused 735 (delta 548), pack-reused 89
Receiving objects: 100% (919/919), 630.77 KiB | 8.76 MiB/s, done.
Resolving deltas: 100% (578/578), completed with 60 local objects.
From https://github.com/AUTOMATIC1111/stable-diffusion-webui
   72cd27a1..b08500ce  master                            -> origin/master
 * [new branch]        LoraFix                           -> origin/LoraFix
   696c338e..2b3fc246  dev                               -> origin/dev
 * [new branch]        fix-COMMANDLINE_ARGS--data-dir    -> origin/fix-COMMANDLINE_ARGS--data-dir
 * [new branch]        readme-simple-installation-method -> origin/readme-simple-installation-method
   6fbd85dd..231562ea  release_candidate                 -> origin/release_candidate
 * [new tag]           v1.2.0                            -> v1.2.0
 * [new tag]           1.1.1                             -> 1.1.1
Updating 22bcc7be..b08500ce
error: Your local changes to the following files would be overwritten by merge:
        webui-user.sh
Please commit your changes or stash them before you merge.
Aborting

今回は COMMANDLINE_ARGS に --xformers を設定していましたので、webui-user.sh を書き換えている為に同期が出来ずに error が表示されていました。一旦、webui-user.sh を別名に変更して (例えば webui-user.sh.orig) から git pull すれば OK です。その後で変更箇所を改めて適用させます。

Stable Diffusion WebUI アップデート後のメッセージと対処

アップデート後に webui.sh を実行すると、幾つかメッセージが表示されますので対処します。

$ ./webui.sh

################################################################
Install script for stable-diffusion + Web UI
Tested on Debian 11 (Bullseye)
################################################################

################################################################
Running on hiro user
################################################################

################################################################
Repo already cloned, using it as install directory
################################################################

################################################################
Create and activate python venv
################################################################

################################################################
Launching launch.py...
################################################################
Cannot locate TCMalloc (improves CPU memory usage)
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0]
Version: v1.2.0
Commit hash: b08500cec8a791ef20082628b49b17df833f5dda
Installing requirements
Launching Web UI with arguments: --xformers
==============================================================================
You are running torch 1.13.1+cu117.
The program is tested to work with torch 2.0.0.
To reinstall the desired version, run with commandline flag --reinstall-torch.
Beware that this will cause a lot of large files to be downloaded, as well as
there are reports of issues with training tab on the latest version.

Use --skip-version-check commandline argument to disable this check.
==============================================================================
=================================================================================
You are running xformers 0.0.16rc425.
The program is tested to work with xformers 0.0.17.
To reinstall the desired version, run with commandline flag --reinstall-xformers.

Use --skip-version-check commandline argument to disable this check.
=================================================================================
Loading weights [dcd690123c] from /home/hiro/stable-diffusion-webui/models/Stable-diffusion/v2-1_768-ema-pruned.safetensors
Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
Startup time: 3.3s (import torch: 0.7s, import gradio: 0.7s, import ldm: 0.5s, other imports: 0.7s, load scripts: 0.3s, create ui: 0.3s).
Creating model from config: /home/hiro/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/configs/stable-diffusion/v2-inference-v.yaml
LatentDiffusion: Running in v-prediction mode
DiffusionWrapper has 865.91 M params.
Applying xformers cross attention optimization.
Textual inversion embeddings loaded(0):
Model loaded in 4.4s (load weights from disk: 0.5s, find config: 0.9s, create model: 0.3s, apply weights to model: 0.7s, apply half(): 0.4s, load VAE: 1.0s, move model to device: 0.6s).

この状態でも画像は生成できますが、幾つかの項目についてインストールやバージョンアップを行います。

xformers を不使用、PyTorch 2.0 の高速化機能を使用

You are running xformers 0.0.16rc425.
The program is tested to work with xformers 0.0.17.
To reinstall the desired version, run with commandline flag --reinstall-xformers.

Use --skip-version-check commandline argument to disable this check.

xformers をアップデートするように、とのメッセージです。--reinstall-xformers を webui.sh に指定して実行するとアップデートされます。

ただし、PyTorch 2.0 以降を使用する場合には、xformers と同等の高速化を --opt-sdp-attention と --opt-sdp-no-mem-attention で実現できます。この後で PyTorch 2.0 以降にアップデートしますので、webui-user.sh の COMMANDLINE_ARGS から --xformers を除外し、二つのオプションを指定するように変更します。

# Commandline arguments for webui.py, for example: export COMMANDLINE_ARGS="--medvram --opt-split-attention"
#export COMMANDLINE_ARGS="--xformers"
export COMMANDLINE_ARGS="--opt-sdp-attention --opt-sdp-no-mem-attention"

TCMalloc のインストール

TCMalloc がインストールされていない場合に以下のメッセージが表示されます。

Cannot locate TCMalloc (improves CPU memory usage)

こちらを参考に TCMalloc をインストールします。

$ sudo apt install --no-install-recommends google-perftools

PyTorch のバージョンアップ

PyTorch を 1.13.1 から 2.0.1 にバージョンアップします。cuda 11.7 を使用していますので、cuda のバージョンを指定してインストールします。webui-user.sh の TORCH_COMMAND を有効にして、以下の内容を追記します。

export TORCH_COMMAND="pip install torch==2.0.1+cu117 torchvision==0.15.2+cu117 --extra-index-url https://download.pytorch.org/whl/cu117"

その後、PyTorch を再インストールします。

$ ./webui.sh --reinstall-torch

torch-1.13.1+cu117 から torch-2.0.1+cu117 に更新されました。

Stable Diffusion WebUI & 各種ライブラリ更新後のメッセージ

Stable Diffusion WebUI と各種ライブラリを更新後の起動時のメッセージは次の様になります。

$ ./webui.sh

################################################################
Install script for stable-diffusion + Web UI
Tested on Debian 11 (Bullseye)
################################################################

################################################################
Running on hiro user
################################################################

################################################################
Repo already cloned, using it as install directory
################################################################

################################################################
Create and activate python venv
################################################################

################################################################
Launching launch.py...
################################################################
Using TCMalloc: libtcmalloc.so.4
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0]
Version: v1.2.1
Commit hash: 89f9faa63388756314e8a1d96cf86bf5e0663045
Installing requirements
Launching Web UI with arguments: --opt-sdp-attention --opt-sdp-no-mem-attention
No module 'xformers'. Proceeding without it.
Loading weights [dcd690123c] from /home/hiro/stable-diffusion-webui/models/Stable-diffusion/v2-1_768-ema-pruned.safetensors
Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
Startup time: 7.3s (import torch: 2.3s, import gradio: 1.3s, import ldm: 0.5s, other imports: 1.6s, load scripts: 0.6s, create ui: 0.8s, gradio launch: 0.2s).
Creating model from config: /home/hiro/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/configs/stable-diffusion/v2-inference-v.yaml
LatentDiffusion: Running in v-prediction mode
DiffusionWrapper has 865.91 M params.
Applying scaled dot product cross attention optimization (without memory efficient attention).
Textual inversion embeddings loaded(0):
Model loaded in 8.3s (load weights from disk: 1.1s, find config: 2.2s, create model: 0.2s, apply weights to model: 1.3s, apply half(): 0.5s, load VAE: 1.4s, move model to device: 1.5s).

PyTorch 2.0 以降に更新しても、Stable Diffusion WebUI の画像生成は正常に動作しています。また xformers を使用しない場合でも生成時間はほぼ変わりません。

まとめ

Stable Diffusion WebUI をアップデートしました。それに伴い PyTorch 2.0 も推奨されていましたので、torch-2.0.1+cu117 に更新しました。PyTorch 2.0 以降では、xformers と同等の速度で画像を生成できるオプションがありますので、--xformers を不使用とし、代わりに --opt-sdp-attention --opt-sdp-no-mem-attention の二つのオプションを使用するように変更しました。

xformers を使わなくても、アイキャッチ用の画像 (1200 x 632) を4枚生成するのに約30秒と、画像の生成速度はほぼ変わりませんでした。

Stable Studio も公開されましたので、ローカルの GPU が使えるようになったら試してみたいです。

今回のアイキャッチ画像

雨の中の紫陽花の花を生成しました。花が平面的なのが少し不満です。

コメント

タイトルとURLをコピーしました