BioErrorLog Tech Blog

試行錯誤の記録

Raspberry PiでPyTorchがIllegal instructionエラーを起こす事象の対処法

Raspberry Pi 4でPyTorch2.0が下記エラーを出す事象の解決策をメモします。

Illegal instruction

はじめに

最近、Raspberry PiとChatGPTを組み合わせて何か面白いことができないだろうか、とあれこれ実験しています。

そんな中、Raspberry PiでPyTorch2.0.0(記事執筆時の最新バージョン)をインストールしたところ、import torchしただけでIllegal instructionのエラーが出る事象に遭遇しました。

状況と取り急ぎの対処をメモします。

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でも未だ議論は決着していません。

Illegal instruction (core dumped) : PyTorch 2.0 on Raspberry Pi 4.0 8gb · Issue #97226 · pytorch/pytorch · GitHub

バージョンを戻せば解消する、という元も子もない話ですが、逆にその他もろもろの足掻きでは解消できなかった事例、と受け取ってもらえれば幸いです。

おわりに

バージョンを戻さないと解決しなかった、というただの懺悔のような話になってしまいました。

今後のversionで解決されるのを祈っています。

[関連記事]

www.bioerrorlog.work

www.bioerrorlog.work

www.bioerrorlog.work

参考

Illegal instruction (core dumped) : PyTorch 2.0 on Raspberry Pi 4.0 8gb · Issue #97226 · pytorch/pytorch · GitHub

Illegal instruction (core dumped) on Raspberry Pi 4B · Issue #8 · KumaTea/pytorch-aarch64 · GitHub