
M1 MacにTensorFlow + coremltoolsの環境を構築する/TensorFlow PluggableDeviceとは何なのか
とあるTensorFlowのモデルを最新のcoremltools 6.0で変換したく、
手元のM1 MBPにTensorFlow + coremltoolsの環境を構築した際のメモ。
illegal hardware instructionエラー
condaでPython 3.10の環境を用意して、tensorflowをインストール。バージョンはcoremltools 6.0がサポートしている2.8を指定。
% pip install tensorflow==2.8
そのうえでPythonで `import tensorflow` してみると、
zsh: illegal hardware instruction
なるエラーが発生した。
エラーメッセージでググるとこんな記事が出てきた。
M1のMACのtensorflowでzsh illegal hardwareが出る話 2020/12 - Qiita
M1のMACでAppleが最適化したtensorflow環境を利用せず、Rosettaでターミナルを起動した状態でtensorflowを利用したい(普通にpip install tensorflowでインストールしたい)場合、import tensorflowを実行するとzsh illegal hardwareと怒られてインタプリンタが落ちます。
手元でやっている状況としては"Rosettaでターミナル起動"しているわけではないが、"M1のMACでAppleが最適化したtensorflow環境"なるものがあるらしいのはこの記事で初めて知った。
tensorflow_macos
リンクが貼られている記事を見に行き、そちらの記事でApple公式アカウントの `tensorflow_macos` なるリポジトリの存在を知る。
このリポジトリには
This repository has been archived by the owner. It is now read-only.
と既にメンテされない旨が表示されており、さらにREADMEのトップには
You can now leverage Apple’s tensorflow-metal PluggableDevice in TensorFlow v2.5 for accelerated training on Mac GPUs directly with Metal.
(TensorFlow v2.5でAppleのtensorflow-metal PluggableDeviceを活用し、Metalで直接Mac GPUのトレーニングを加速することができるようになりました。)
と"tensorflow-metal PluggableDevice"なるさらに次世代の実装がある旨が表示されている。
"Learn more here"には以下のURLがリンクされていた。
tensorflow-metal PluggableDeviceとは
"Getting Started with tensorflow-metal PluggableDevice" というページには次のように記載されていた。
Accelerate training of machine learning models with TensorFlow right on your Mac. Install TensorFlow and the tensorflow-metal PluggableDevice to accelerate training with Metal on Mac GPUs.
(TensorFlowを使った機械学習モデルのトレーニングをMac上で高速化する。TensorFlowとtensorflow-metal PluggableDeviceをインストールすると、MacのGPU上でMetalを使った学習を高速化することができます。)
tensorflow-metal PluggableDeviceを使用することで、TensorFlowの学習が、Mac上でMetalを利用して高速に行われるようになるらしい。
TensorFlow PluggableDevicesとは何なのか
最後まで読んでいただきありがとうございます!もし参考になる部分があれば、スキを押していただけると励みになります。 Twitterもフォローしていただけたら嬉しいです。 https://twitter.com/shu223/