Python アプリケーション実行時に "NotImplementedError: Could not run 'torchvision::nms' with arguments from the 'CUDA' backend." が発生する - Python

Python アプリケーション実行時に "NotImplementedError: Could not run 'torchvision::nms' with arguments from the 'CUDA' backend." が発生する現象と対象方の紹介です。

現象

Pythonアプリケーション実行時に以下のエラーが発生します。特に処理時に発生するケースが多いです。
エラーメッセージ
NotImplementedError: Could not run 'torchvision::nms' with arguments from the 'CUDA' backend. This could be because the operator doesn't exist for this backend, or was omitted during the selective/custom build process (if using custom build). If you are a Facebook employee using PyTorch on mobile, please visit https://fburl.com/ptmfixes for possible resolutions. 'torchvision::nms' is only available for these backends: [CPU, Meta, QuantizedCPU, BackendSelect, Python, FuncTorchDynamicLayerBackMode, Functionalize, Named, Conjugate, Negative, ZeroTensor, ADInplaceOrView, AutogradOther, AutogradCPU, AutogradCUDA, AutogradXLA, AutogradMPS, AutogradXPU, AutogradHPU, AutogradLazy, AutogradMeta, Tracer, AutocastCPU, AutocastCUDA, FuncTorchBatched, BatchedNestedTensor, FuncTorchVmapMode, Batched, VmapMode, FuncTorchGradWrapper, PythonTLSSnapshot, FuncTorchDynamicLayerFrontMode, PreDispatch, PythonDispatcher].

日本語の意味では以下になります。

NotImplementedError: 'CUDA' バックエンドからの引数を使用して 'torchvision::nms' を実行できませんでした。これは、このバックエンドに演算子が存在しないか、選択/カスタム ビルド プロセス中に省略されたことが考えられます (カスタム ビルドを使用している場合)。モバイルで PyTorch を使用している Facebook 従業員の場合は、https://fburl.com/ptmfixes にアクセスして考えられる解決策を確認してください。 「torchvision::nms」は次のバックエンドでのみ使用できます: [CPU、Meta、QuantizedCPU、BackendSelect、Python、FuncTorchDynamicLayerBackMode、Functionalize、Named、Conjugate、Negative、ZeroTensor、ADInplaceOrView、AutogradOther、AutogradCPU、AutogradCUDA、AutogradXLA、AutogradMPS、AutogradXPU、 AutogradHPU、AutogradLazy、AutogradMeta、Tracer、AutocastCPU、AutocastCUDA、FuncTorchBatched、BatchedNestedTensor、FuncTorchVmapMode、Batched、VmapMode、FuncTorchGradWrapper、PythonTLSSnapshot、FuncTorchDynamicLayerFrontMode、PreDispatch、PythonDispatcher]

原因

いくつか可能性が考えられますが、よくある原因として、PyTorch が新しすぎる可能性があります。

対処法

PyTorchをアンインストールし、その後、PyTorchのバージョンを下げたバージョンをインストールします。

PyTorchをバージョン指定してインストールするコマンドは(https://pytorch.org/get-started/previous-versions/)にて確認できます。

PyTochのアンインストール

実行しているアプリケーションによってはvenv切り替え後に実行します。
PyTochのアンインストールコマンド
pip uninstall torch
pip cache purge

PyToch のインストール

実行しているアプリケーションによってはvenv切り替え後に実行します。
PyToch 2.2系 CUDA 12.1
pip install torch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2 --index-url https://download.pytorch.org/whl/cu121
PyToch 2.1系 CUDA 12.1
pip install torch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 --index-url https://download.pytorch.org/whl/cu121
著者
iPentecのプログラマー、最近はAIの積極的な活用にも取り組み中。
とっても恥ずかしがり。
掲載日: 2024-05-17
iPentec all rights reserverd.