WindowsでLoRAしようとしたらPyTorchとxformersあたりでエラーが出たので解消

この記事は最新更新日から、1年以上経過しています。

概要

最近流行りでなにかと話題のLoRAをやろうとしたら下記エラーに遭遇したので対応しました。

RuntimeError: No such operator xformers::efficient_attention_forward_cutlass – did you forget to build xformers with
`python setup.py develop`?

RuntimeError: “slow_conv2d_cpu” not implemented for ‘Half’

error: Microsoft Visual C++ 14.0 or greater is required.

環境

Windows11
NVIDIA GeForce RTX 3060
Cuda compilation tools, release 11.8, V11.8.89
Python 3.10.11

やったこと

どうやらWindowsだとxformersをコンパイルしないとうまく動かないようです。
下記投稿に従って作業してみました。

AUTOMATIC1111 xformers cross attention with on Windows
by u/Der_Doe in StableDiffusion

グラフィックカードの型番の確認

1.「Win」アイコンを右クリックして、「タスクマネージャー」をクリックします。
2.「パフォーマンス」タブを選択して、左部の「GPU」をクリックして、右上部でグラフィックカードの型番を確認できます。

私の場合は下記でした。
NVIDIA GeForce RTX 3060

GPU コンピューティング能力の確認

下記サイトへ行き、対応するGPUの「Compute Capability」の値を確認します。

CUDA GPUs - Compute Capability
Explore your GPU compute capability and CUDA-enabled products.

私の場合は下記にありました。
CUDA-Enabled GeForce and TITAN Products
Geforce RTX 3060 8.6

xformersのインストール

TORCH_CUDA_ARCH_LISTには先程調べた「Compute Capability」の値を記入します。
repositoriesはエラーが発生したリポジトリのものにしてください。

PS> .\venv\Scripts\activate
(venv)> cd repositories
(venv)> git clone https://github.com/facebookresearch/xformers.git
(venv)> cd xformers
(venv)> set TORCH_CUDA_ARCH_LIST=8.6
(venv)> pip install -r requirements.txt
(venv)> pip install -e .

pip install -e .をしたあと、うまくいっていると「Running setup.py develop for xformers」と表示されて20-30分ぐらい何も表示されません。(私は23分かかりました)
やがて「Successfully installed xformers-0.0.21+a205b24.d20230604」と出るので気長に待ってください。

これでインストールが終わったのでエラーが解消されると思います。

出てきたエラー

error: Microsoft Visual C++ 14.0 or greater is required. というエラー

下記のエラーが発生した場合、C++のビルドツールが必要なのでインストールします。

    C:\repositories\venv\lib\site-packages\torch\utils\cpp_extension.py:359: UserWarning: Error checking compiler version for cl: [WinError 2] 指定されたファイルが見つかりません。
      warnings.warn(f'Error checking compiler version for {compiler}: {error}')
    building 'xformers._C_flashattention' extension
    error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
    [end of output]

メッセージに従って下記サイトからビルドツールをダウンロードし、C++のビルドツールをインストールします。(C++のパッケージにチェック入れてインストールします、インストール対象はデフォルトのままで大丈夫でした)

Microsoft C++ Build Tools - Visual Studio

インストールが終わったら下記の部分を再度行います。setはPowershellから抜けると消えるのでもう一度行う必要があります。

PS> .\venv\Scripts\activate
(venv)> cd repositories
(venv)> cd xformers
(venv)> set TORCH_CUDA_ARCH_LIST=8.6
(venv)> pip install -r requirements.txt
(venv)> pip install -e .

RuntimeError: “slow_conv2d_cpu” not implemented for ‘Half’ というエラー

このエラーはGPUに対応していないPyTorchをインストールしている場合に起きるようです。
一旦アンインストールします。

(venv)>pip uninstall torch torchvision torchaudio

アーカイブの方から対応するCUDAをダウンロードしてインストールします。
(2023/06/04現在だと11.7か11.8でしょうか)

CUDA Toolkit Archive

コマンドプロンプトからバージョン確認できます

C>nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Sep_21_10:41:10_Pacific_Daylight_Time_2022
Cuda compilation tools, release 11.8, V11.8.89
Build cuda_11.8.r11.8/compiler.31833905_0

再掲ですが、コマンドは下記サイトで自分の環境にあったものを選択すると「Run this Command:」に出てきます。

Start Locally
Start Locally

私の場合は下記のコマンドになりました。これをインストールすれば解消されると思います。

(venv)> pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

おわり。