とうとうRyzen + RADEONのAMD環境にもWindowsで動くStable Diffusionがきた
所要時間:14 分, 29 秒
Views:22

とうとうRyzen + RADEONのAMD環境にもWindowsで動くStable Diffusionがきた

やっと来たAMD最初の一歩。

DirectMLでAMD GPUを動かす

定期的にAMDxSD x Winのことを調べていますが、今回この記事に行き当たりました。

Running Stable Diffusion on Windows with an AMD GPU

Unfortunately, in its current state, it relies on Nvidia’s CUDA framework, which means that it only works out of the box if you’ve got an Nvidia GPU.

Fear not, however. Because Stable Diffusion is both a) open source and b) good, it has seen an absolute flurry of activity, and some enterprising folks have done the legwork to make it usable for AMD GPUs, even for Windows users.

記事によるとWindows上にPythonの環境を作るところまでは一緒。
AMD版のWindows対応が進まないPytorchではなく、DirectML (Windows) – onnxruntimeを使う所にカギがあるようです。

という訳でまた見様見真似で試してみました。
今回使っているWindowsはWindows 11 Pro 21H2です。

環境構築

Pythonのインストール

Python Releases for Windowsから、Python 3.10.7 – Sept. 6, 2022をインストールしました。
最新の3.11.系列じゃないのは後でダウンロードするOnnxパッケージが3.10止まりだから。

PS > python --version
Python 3.10.7

Windowsターミナル(管理者)を起動します。
設定を変えていなければPowerShell(PS)のプロンプトが表示されます。

Pyhtonの仮想環境を作成

PS C:\temp> python -m venv ./virtualenv

Activateするスクリプトを実行する前に、まずPowerShellスクリプトの実行を許可しておきます。
これを忘れると「このシステムではスクリプトの実行が無効になっている」とエラーになります。

PS C:\temp> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process

仮想環境を有効化。

PS C:\temp> ./virtualenv/Scripts/Activate.ps1
(virtualenv) PS C:\temp>

Pythonパッケージインストール

元記事に従い、pipでパッケージを追加していきます。

  • pip install diffusers==0.3.0
  • pip install transformers
  • pip install onnxruntime

長いですが参考として実行ログをそのまま貼っておきます。

(virtualenv) PS C:\temp> pip install diffusers==0.3.0
Collecting diffusers==0.3.0
Downloading diffusers-0.3.0-py3-none-any.whl (153 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 153.9/153.9 kB 3.1 MB/s eta 0:00:00
Collecting regex!=2019.12.17
Downloading regex-2022.9.13-cp310-cp310-win_amd64.whl (267 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 267.7/267.7 kB 8.3 MB/s eta 0:00:00
Collecting numpy
Downloading numpy-1.23.3-cp310-cp310-win_amd64.whl (14.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.6/14.6 MB 46.7 MB/s eta 0:00:00
Collecting torch>=1.4
Downloading torch-1.12.1-cp310-cp310-win_amd64.whl (162.2 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 162.2/162.2 MB 13.1 MB/s eta 0:00:00
Collecting huggingface-hub>=0.8.1
Downloading huggingface_hub-0.10.0-py3-none-any.whl (163 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 163.5/163.5 kB 10.2 MB/s eta 0:00:00
Collecting filelock
Downloading filelock-3.8.0-py3-none-any.whl (10 kB)
Collecting requests
Downloading requests-2.28.1-py3-none-any.whl (62 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.8/62.8 kB 3.3 MB/s eta 0:00:00
Collecting importlib-metadata
Downloading importlib_metadata-5.0.0-py3-none-any.whl (21 kB)
Collecting Pillow
Using cached Pillow-9.2.0-cp310-cp310-win_amd64.whl (3.3 MB)
Collecting tqdm
Downloading tqdm-4.64.1-py2.py3-none-any.whl (78 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 78.5/78.5 kB ? eta 0:00:00
Collecting typing-extensions>=3.7.4.3
Downloading typing_extensions-4.3.0-py3-none-any.whl (25 kB)
Collecting pyyaml>=5.1
Downloading PyYAML-6.0-cp310-cp310-win_amd64.whl (151 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 151.7/151.7 kB ? eta 0:00:00
Collecting packaging>=20.9
Downloading packaging-21.3-py3-none-any.whl (40 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 40.8/40.8 kB 984.2 kB/s eta 0:00:00
Collecting zipp>=0.5
Downloading zipp-3.8.1-py3-none-any.whl (5.6 kB)
Collecting idna<4,>=2.5
Downloading idna-3.4-py3-none-any.whl (61 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.5/61.5 kB ? eta 0:00:00
Collecting charset-normalizer<3,>=2
Downloading charset_normalizer-2.1.1-py3-none-any.whl (39 kB)
Collecting certifi>=2017.4.17
Downloading certifi-2022.9.24-py3-none-any.whl (161 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 161.1/161.1 kB 9.4 MB/s eta 0:00:00
Collecting urllib3<1.27,>=1.21.1
Downloading urllib3-1.26.12-py2.py3-none-any.whl (140 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 140.4/140.4 kB ? eta 0:00:00
Collecting pyparsing!=3.0.5,>=2.0.2
Downloading pyparsing-3.0.9-py3-none-any.whl (98 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 98.3/98.3 kB ? eta 0:00:00
Collecting colorama
Downloading colorama-0.4.5-py2.py3-none-any.whl (16 kB)
Installing collected packages: zipp, urllib3, typing-extensions, regex, pyyaml, pyparsing, Pillow, numpy, idna, filelock, colorama, charset-normalizer, certifi, tqdm, torch, requests, packaging, importlib-metadata, huggingface-hub, diffusers
Successfully installed Pillow-9.2.0 certifi-2022.9.24 charset-normalizer-2.1.1 colorama-0.4.5 diffusers-0.3.0 filelock-3.8.0 huggingface-hub-0.10.0 idna-3.4 importlib-metadata-5.0.0 numpy-1.23.3 packaging-21.3 pyparsing-3.0.9 pyyaml-6.0 regex-2022.9.13 requests-2.28.1 torch-1.12.1 tqdm-4.64.1 typing-extensions-4.3.0 urllib3-1.26.12 zipp-3.8.1
(virtualenv) PS C:\temp> pip install transformers
Collecting transformers
Downloading transformers-4.22.2-py3-none-any.whl (4.9 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.9/4.9 MB 17.3 MB/s eta 0:00:00
Requirement already satisfied: numpy>=1.17 in c:\temp\virtualenv\lib\site-packages (from transformers) (1.23.3)
Requirement already satisfied: regex!=2019.12.17 in c:\temp\virtualenv\lib\site-packages (from transformers) (2022.9.13)
Requirement already satisfied: huggingface-hub<1.0,>=0.9.0 in c:\temp\virtualenv\lib\site-packages (from transformers) (0.10.0)
Requirement already satisfied: tqdm>=4.27 in c:\temp\virtualenv\lib\site-packages (from transformers) (4.64.1)
Requirement already satisfied: requests in c:\temp\virtualenv\lib\site-packages (from transformers) (2.28.1)
Requirement already satisfied: packaging>=20.0 in c:\temp\virtualenv\lib\site-packages (from transformers) (21.3)
Requirement already satisfied: pyyaml>=5.1 in c:\temp\virtualenv\lib\site-packages (from transformers) (6.0)
Collecting tokenizers!=0.11.3,<0.13,>=0.11.1
Downloading tokenizers-0.12.1-cp310-cp310-win_amd64.whl (3.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.3/3.3 MB 51.9 MB/s eta 0:00:00
Requirement already satisfied: filelock in c:\temp\virtualenv\lib\site-packages (from transformers) (3.8.0)
Requirement already satisfied: typing-extensions>=3.7.4.3 in c:\temp\virtualenv\lib\site-packages (from huggingface-hub<1.0,>=0.9.0->transformers) (4.3.0)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:\temp\virtualenv\lib\site-packages (from packaging>=20.0->transformers) (3.0.9)
Requirement already satisfied: colorama in c:\temp\virtualenv\lib\site-packages (from tqdm>=4.27->transformers) (0.4.5)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\temp\virtualenv\lib\site-packages (from requests->transformers) (1.26.12)
Requirement already satisfied: idna<4,>=2.5 in c:\temp\virtualenv\lib\site-packages (from requests->transformers) (3.4)
Requirement already satisfied: certifi>=2017.4.17 in c:\temp\virtualenv\lib\site-packages (from requests->transformers) (2022.9.24)
Requirement already satisfied: charset-normalizer<3,>=2 in c:\temp\virtualenv\lib\site-packages (from requests->transformers) (2.1.1)
Installing collected packages: tokenizers, transformers
Successfully installed tokenizers-0.12.1 transformers-4.22.2
(virtualenv) PS C:\temp> pip install onnxruntime
Collecting onnxruntime
Downloading onnxruntime-1.12.1-cp310-cp310-win_amd64.whl (5.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.8/5.8 MB 17.7 MB/s eta 0:00:00
Requirement already satisfied: packaging in c:\temp\virtualenv\lib\site-packages (from onnxruntime) (21.3)
Collecting protobuf
Downloading protobuf-4.21.7-cp310-abi3-win_amd64.whl (525 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 525.5/525.5 kB ? eta 0:00:00
Collecting sympy
Downloading sympy-1.11.1-py3-none-any.whl (6.5 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.5/6.5 MB 41.2 MB/s eta 0:00:00
Collecting coloredlogs
Downloading coloredlogs-15.0.1-py2.py3-none-any.whl (46 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 46.0/46.0 kB ? eta 0:00:00
Collecting flatbuffers
Downloading flatbuffers-22.9.24-py2.py3-none-any.whl (26 kB)
Requirement already satisfied: numpy>=1.21.0 in c:\temp\virtualenv\lib\site-packages (from onnxruntime) (1.23.3)
Collecting humanfriendly>=9.1
Downloading humanfriendly-10.0-py2.py3-none-any.whl (86 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 86.8/86.8 kB ? eta 0:00:00
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:\temp\virtualenv\lib\site-packages (from packaging->onnxruntime) (3.0.9)
Collecting mpmath>=0.19
Downloading mpmath-1.2.1-py3-none-any.whl (532 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 532.6/532.6 kB ? eta 0:00:00
Collecting pyreadline3
Downloading pyreadline3-3.4.1-py3-none-any.whl (95 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 95.2/95.2 kB ? eta 0:00:00
Installing collected packages: pyreadline3, mpmath, flatbuffers, sympy, protobuf, humanfriendly, coloredlogs, onnxruntime
Successfully installed coloredlogs-15.0.1 flatbuffers-22.9.24 humanfriendly-10.0 mpmath-1.2.1 onnxruntime-1.12.1 protobuf-4.21.7 pyreadline3-3.4.1 sympy-1.11.1
(virtualenv) PS C:\temp>

Onnxのダウンロード

最後にインストールした”onnxruntime”には不足があるらしく、別途ビルド済みのパッケージで上書きインストールします。
ORT-Nightly – Azure Artifactsにアクセスし、Python3.10用である”ort_nightly_directml-1.13.0.dev20221003004-cp310-cp310-win_amd64”をダウンロード。
ファイルは作成したvirtualenvディレクトリに保存しておきます。

“–force-reinstall”オプションを忘れずにpip installします。

(virtualenv) PS C:\temp> pip install .\virtualenv\ort_nightly_directml-1.13.0.dev20220908001-cp310-cp310-win_amd64.whl --force-reinstall
Processing c:\temp\virtualenv\ort_nightly_directml-1.13.0.dev20220908001-cp310-cp310-win_amd64.whl
Collecting protobuf
Using cached protobuf-4.21.7-cp310-abi3-win_amd64.whl (525 kB)
Collecting sympy
Using cached sympy-1.11.1-py3-none-any.whl (6.5 MB)
Collecting numpy>=1.21.6
Using cached numpy-1.23.3-cp310-cp310-win_amd64.whl (14.6 MB)
Collecting packaging
Using cached packaging-21.3-py3-none-any.whl (40 kB)
Collecting coloredlogs
Using cached coloredlogs-15.0.1-py2.py3-none-any.whl (46 kB)
Collecting flatbuffers
Using cached flatbuffers-22.9.24-py2.py3-none-any.whl (26 kB)
Collecting humanfriendly>=9.1
Using cached humanfriendly-10.0-py2.py3-none-any.whl (86 kB)
Collecting pyparsing!=3.0.5,>=2.0.2
Using cached pyparsing-3.0.9-py3-none-any.whl (98 kB)
Collecting mpmath>=0.19
Using cached mpmath-1.2.1-py3-none-any.whl (532 kB)
Collecting pyreadline3
Using cached pyreadline3-3.4.1-py3-none-any.whl (95 kB)
Installing collected packages: pyreadline3, mpmath, flatbuffers, sympy, pyparsing, protobuf, numpy, humanfriendly, packaging, coloredlogs, ort-nightly-directml
Attempting uninstall: pyreadline3
Found existing installation: pyreadline3 3.4.1
Uninstalling pyreadline3-3.4.1:
Successfully uninstalled pyreadline3-3.4.1
Attempting uninstall: mpmath
Found existing installation: mpmath 1.2.1
Uninstalling mpmath-1.2.1:
Successfully uninstalled mpmath-1.2.1
Attempting uninstall: flatbuffers
Found existing installation: flatbuffers 22.9.24
Uninstalling flatbuffers-22.9.24:
Successfully uninstalled flatbuffers-22.9.24
Attempting uninstall: sympy
Found existing installation: sympy 1.11.1
Uninstalling sympy-1.11.1:
Successfully uninstalled sympy-1.11.1
Attempting uninstall: pyparsing
Found existing installation: pyparsing 3.0.9
Uninstalling pyparsing-3.0.9:
Successfully uninstalled pyparsing-3.0.9
Attempting uninstall: protobuf
Found existing installation: protobuf 4.21.7
Uninstalling protobuf-4.21.7:
Successfully uninstalled protobuf-4.21.7
Attempting uninstall: numpy
Found existing installation: numpy 1.23.3
Uninstalling numpy-1.23.3:
Successfully uninstalled numpy-1.23.3
Attempting uninstall: humanfriendly
Found existing installation: humanfriendly 10.0
Uninstalling humanfriendly-10.0:
Successfully uninstalled humanfriendly-10.0
Attempting uninstall: packaging
Found existing installation: packaging 21.3
Uninstalling packaging-21.3:
Successfully uninstalled packaging-21.3
Attempting uninstall: coloredlogs
Found existing installation: coloredlogs 15.0.1
Uninstalling coloredlogs-15.0.1:
Successfully uninstalled coloredlogs-15.0.1
Successfully installed coloredlogs-15.0.1 flatbuffers-22.9.24 humanfriendly-10.0 mpmath-1.2.1 numpy-1.23.3 ort-nightly-directml-1.13.0.dev20220908001 packaging-21.3 protobuf-4.21.7 pyparsing-3.0.9 pyreadline3-3.4.1 sympy-1.11.1
(virtualenv) PS C:\temp>

HuggingfaceにCLIログイン

まずブラウザでHuggingfaceにログインし、自分のアカウントのSetting>Access Tokensを開きます。
そこでTokenを作成しておき、クリップボードにコピー。

次に”huggingface-cli.exe login”コマンドを実行し、Token:で止まったらトークンをペーストします。

(virtualenv) PS C:\temp\virtualenv> huggingface-cli.exe login
_|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|
_|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|
_|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|
_|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|
_|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|
To login, `huggingface_hub` now requires a token generated from https://huggingface.co/settings/tokens .
Token:
Login successful
Your token has been saved to C:\Users\XXXXX\.huggingface\token
Authenticated through git-credential store but this isn't the helper defined on your machine.
You might have to re-authenticate when pushing to the Hugging Face Hub. Run the following command in your terminal in case you want to set this credential helper as the default
git config --global credential.helper store
(virtualenv) PS C:\temp\virtualenv>

なにやら赤い警告文字が出ますが無視してOK。
(このトークンは読み取り用であり、アップロードはできないやつだよ、という内容)

なお手順にはありませんでしたが、このままだと”onnx moduleがない”と言われスクリプトが止まります。
よって個別にonnxを追加インストールしました。

(virtualenv) PS C:\temp\virtualenv> pip install onnx
Collecting onnx
Downloading onnx-1.12.0-cp310-cp310-win_amd64.whl (11.5 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 11.5/11.5 MB 36.3 MB/s eta 0:00:00
Requirement already satisfied: numpy>=1.16.6 in c:\temp\virtualenv\lib\site-packages (from onnx) (1.23.3)
Collecting protobuf<=3.20.1,>=3.12.2
Downloading protobuf-3.20.1-cp310-cp310-win_amd64.whl (903 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 903.8/903.8 kB 55.9 MB/s eta 0:00:00
Requirement already satisfied: typing-extensions>=3.6.2.1 in c:\temp\virtualenv\lib\site-packages (from onnx) (4.3.0)
Installing collected packages: protobuf, onnx
Attempting uninstall: protobuf
Found existing installation: protobuf 4.21.7
Uninstalling protobuf-4.21.7:
Successfully uninstalled protobuf-4.21.7
Successfully installed onnx-1.12.0 protobuf-3.20.1
(virtualenv) PS C:\temp\virtualenv>

学習データのダウンロードとOnnxへの変換

StableDiffusionの公開モデルがそのまま使える訳ではなく、Onnxの形式に変換するそうです。
そのままは使えないのかー・・・。

DL&変換のためのスクリプト、を下記よりダウンロードします。

ファイルは”convert_stable_diffusion_checkpoint_to_onnx.py”としてvirtualenvディレクトリに保存します。
準備ができたらスクリプト実行。

(virtualenv) PS C:\temp\virtualenv> python convert_stable_diffusion_checkpoint_to_onnx.py --model_path="CompVis/stable-diffusion-v1-4" --output_path="./stable_diffusion_onnx"
Downloading: 100%|████████████████████████████████████████████████████████████████| 1.34k/1.34k [00:00<00:00, 1.34MB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████| 14.9k/14.9k [00:00<00:00, 78.1kB/s]
(中略)
WARNING: The shape inference of prim::Constant type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function.
WARNING: The shape inference of prim::Constant type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function.
ONNX pipeline saved to stable_diffusion_onnx
ftfy or spacy is not installed using BERT BasicTokenizer instead of ftfy.
ONNX pipeline is loadable
(virtualenv) PS C:\temp\virtualenv>

5GBくらいのダウンロードが終わった後、変換が行われます。
色々Warningが出てるけど見なかった事にしました。

完了すると、virtualenvディレクトリの下にoutput_pathで指定した”./stable_diffusion_onnx”が出来ています。

いよいよ実行

実行スクリプト

元記事を参考に、一番シンプルな実行スクリプトを用意します。
”txt2img.py”として同様のディレクトリに保存。

from diffusers import StableDiffusionOnnxPipeline
pipe = StableDiffusionOnnxPipeline.from_pretrained("./stable_diffusion_onnx", provider="DmlExecutionProvider")
prompt = "A happy celebrating robot on a mountaintop, happy, landscape, dramatic lighting, art by artgerm greg rutkowski alphonse mucha, 4k uhd'"
image = pipe(prompt).images[0] 
image.save("output.png")

CompVis/stable-diffusion-v1-4の公式スクリプトとほぼ一緒。
provider=”DmlExecutionProvider”という部分が今回のキモだそうです。

実行結果

(virtualenv) PS C:\temp\virtualenv> python.exe .\txt2img.py
2022-10-05 00:42:04.1301750 [W:onnxruntime:, inference_session.cc:491 onnxruntime::InferenceSession::RegisterExecutionProvider] Having memory pattern enabled is not supported while using the DML Execution Provider. So disabling it for this session since it uses the DML Execution Provider.
2022-10-05 00:42:04.2752279 [W:onnxruntime:, session_state.cc:1030 onnxruntime::VerifyEachNodeIsAssignedToAnEp] Some nodes were not assigned to the preferred execution providers which may or may not have an negative impact on performance. e.g. ORT explicitly assigns shape related ops to CPU to improve perf.
2022-10-05 00:42:04.2786332 [W:onnxruntime:, session_state.cc:1032 onnxruntime::VerifyEachNodeIsAssignedToAnEp] Rerunning with verbose output on a non-minimal build will show node assignments.
(中略)
100%|██████████████████████████████████████████████████████████████████████████████████| 51/51 [01:13<00:00,  1.44s/it]
(virtualenv) PS C:\temp\virtualenv>

512×512の画像1枚に1分13秒。
Ubuntu環境より大分遅い推論の後、無事に謎の画像が出力されました。
Prompt=山頂で喜ぶロボット, 幸せ, 風景, ドラマチックな照明, アートギャルド・グレッグ・ラトコウスキー・アルフォンス・ミュシャ, 4k uhd

greg rutkowskiとalphonse muchaはどんな画像でもコレを入れるとデザインが安定するという偉大な巨匠のフレーズです。

変換時の負荷を確認。
メモリ使用量も温度も、それほどリソースを使い切ってはいなさそう。

なおWaifu-Diffusionはダメでした→できました(10/8)

これ、Waifu-Diffusionに入れ替えれば動くんじゃない?
と思って実行しました。

(virtualenv) PS C:\temp\virtualenv> python convert_stable_diffusion_checkpoint_to_onnx.py --model_path="hakurei/waifu-diffusion" --output_path="./waifu_diffusion_onnx"

それぞれのパスをWaifu-Diffusionに置き換えてonnx形式に変換。
エラーはSDより出るものの無事に終わりました。

実行する場合はdiffusionの一部ファイルを直接修正が必要。

\virtualenv\Lib\site-packages\diffusers\schedulers\scheduling_ddim.pyの210行目を修正

変更前)pred_original_sample = (sample - beta_prod_t ** (0.5) * model_output) / alpha_prod_t ** (0.5)
変更後)pred_original_sample = (torch.FloatTensor(sample) - beta_prod_t ** (0.5) * torch.FloatTensor(model_output)) / alpha_prod_t ** (0.5)

\virtualenv\Lib\site-packages\diffusers\pipelines\stable_diffusion\pipeline_stable_diffusion_onnx.pyの133行目

変更前)sample=latent_model_input, timestep=np.array([t]), encoder_hidden_states=text_embeddings
変更後)sample=latent_model_input, timestep=np.array([t], dtype=np.int64), encoder_hidden_states=text_embeddings

コレで動きました。

あとはWaifu使うと大抵の画像がNSFWとなり真っ黒画像になります。
なのでそれを外す場合、実行するtxt2img.pyに1行書き加えるそうです。

pipe.safety_checker = lambda images, **kwargs: (images, [False] * len(images))

なおこのチェックを外すと推論の時間が2,3割短縮されます。
そんなチェックに時間かけてたんですかね。

fantastic,AMD,SD,Winの第一歩

ようやくといいますが、やっとローカルAMD環境でStableDiffusionが動きました。
もうOSを切り替えなくてもいいんだ!
まだPromptを一度ファイルに書いては保存して実行する不便な環境ではあるけれど。

速度もいまひとつでUbuntu上で3.5s/it位出ていたのにWindowsでは1.5s/itほど。半分以下です。

 

でも今のこのジャンルは日進月歩。
2週間後にはガラリと変わった動きがあるかもしれません。AMD自身が環境を作ったりとかね。

今後もチェックしていこうと思います。
AUTOMATIC1111版のAMD対応が着てくれたらもうそれでゴールなのだけど。

コメントを残す