Raspberry Pi 4でPyTorch2.0が下記エラーを出す事象の解決策をメモします。
Illegal instruction
はじめに
最近、Raspberry PiとChatGPTを組み合わせて何か面白いことができないだろうか、とあれこれ実験しています。
そんな中、Raspberry PiでPyTorch2.0.0(記事執筆時の最新バージョン)をインストールしたところ、import torch
しただけでIllegal instruction
のエラーが出る事象に遭遇しました。
状況と取り急ぎの対処をメモします。
ChatGPTとRaspberry Piを組み合わせる.
— BioErrorLog (@bioerrorlog) March 30, 2023
現実世界に進出させる実験をあれこれ試行錯誤中. https://t.co/6hY6ndpxjA pic.twitter.com/3xKRF6Nvp6
Raspberry PiでPyTorchがIllegal instruction
起きた事象
PyTorch2.0.0をpip installしてimport torch
すると、Illegal instruction
のエラーが出る。
pip install torch==2.0.0 torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu python -c "import torch;print(torch.__version__)" # Illegal instruction
環境: Raspberry Pi 4 Model B 4GB RAM / 64bit Raspberry Pi OS(bullseye)
$ hostnamectl Static hostname: raspberrypi Icon name: computer Machine ID: 4f8b19cb3280458c99d89xxxxxx Boot ID: 275f81g1c1fd4249a49bbxxxxxx Operating System: Debian GNU/Linux 11 (bullseye) Kernel: Linux 6.1.19-v8+ Architecture: arm64
対処法
根本解決ではありませんが、PyTorchバージョンを1.13.1
に戻せば解消します。
pip install torch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 --index-url https://download.pytorch.org/whl/cpu python -c "import torch;print(torch.__version__)" # 1.13.1
原因はPyTorch最新バージョン2.0.0
とRaspberry PiのCPUアーキテクチャの相性の問題だと思いますが、下記のissueでも未だ議論は決着していません。
バージョンを戻せば解消する、という元も子もない話ですが、逆にその他もろもろの足掻きでは解消できなかった事例、と受け取ってもらえれば幸いです。
おわりに
バージョンを戻さないと解決しなかった、というただの懺悔のような話になってしまいました。
今後のversionで解決されるのを祈っています。
[関連記事]
参考
Illegal instruction (core dumped) on Raspberry Pi 4B · Issue #8 · KumaTea/pytorch-aarch64 · GitHub