y_megane.log

日々の勉強や改善ネタの備忘。

kerasでfitしたら異常終了するようになった問題の解決方法(OMP: Error #15)

kerasで適当にモデル組んでfitしたら、fit完了と同時にkernelが死ぬようになった問題の状況と解決策の備忘。
(まだ)きっちり検証していないので、同じ状況に陥ったら原因候補の一つ程度に考えて頂ければよいかと。
限定的な状況での事象だと思いますが、それ故にいつか誰かの役に立てば幸いです。

概要

  • LightGBMを間違った手順でインストールする(conda install lightgbm)
  • kerasをインストールする
  • LightGBMを正しい手順でインストールし直す

とすると、kerasでfitして学習完了した時点で異常終了するようになる。
具体的には、JupyterLabだと何のメッセージも出ずkernel再起動される。
JupyterNotebookだと「kernel死にました」だけ出て、やはりkernelが死ぬ。
.pyファイルにしてVSCodeなりで実行すると、以下のメッセージが表示されるはず。

OMP: Error #15: Initializing libomp.dylib, but found libiomp5.dylib already initialized.  
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://openmp.llvm.org/  
Abort trap: 6  

解決策

conda uninstall keras
conda install keras

としてkerasをインストールし直す。以上。
見慣れないエラーだしググってもほとんど情報がありませんでしたが、これだけでした。
StackOverflowとかでは「ダブってるlibomp5.dylibを消せ」的なことが書いてありましたが、心当たりがありませんでしたし、実際不要でした。
最初の間違ったLightGBMインストールと正しいインストールをした結果、「multiple copies of the OpenMP runtime have been linked」されたのだろうか。全く確証ありませんが。

そもそもkerasではなくOpenMPとかLightGBMを疑っていたため、この簡単な解法にたどり着くまで随分かかってしまいました・・・

正しいLightGBMのインストール方法はこちら(Mac用)

ymegane88.hatenablog.com