Python アプリケーション実行時に "NotImplementedError: Could not run 'torchvision::nms' with arguments from the 'CUDA' backend." が発生する現象と対象方の紹介です。
Pythonアプリケーション実行時に以下のエラーが発生します。特に処理時に発生するケースが多いです。
日本語の意味では以下になります。
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/)にて確認できます。
実行しているアプリケーションによってはvenv切り替え後に実行します。
pip uninstall torch
pip cache purge
実行しているアプリケーションによってはvenv切り替え後に実行します。
pip install torch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2 --index-url https://download.pytorch.org/whl/cu121
pip install torch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 --index-url https://download.pytorch.org/whl/cu121