LoginSignup
0
1

More than 3 years have passed since last update.

(超ラフ版)M1チップ な Macbook でMacPorts経由でTensorflow_macos を導入する

Last updated at Posted at 2021-02-14

2020年11月より、M1 CPU (Apple Silicon)が搭載された、 Macbook Air/Macbook Pro/Mac Mini がリリースされました。アルファ版とはいえ、Apple からM1チップ対応のTensorflow も出てます。これを、MacPorts で導入するためにいろいろ試行錯誤したので、備忘録代わりに。

MacPorts について&その設定方法。

@tenomoto さん記事MacPortsの紹介を見てください。M1 CPU でもやり方は変わりません。ファイルの置き場所は素直に

/opt/local 

に置きましょう。

ローカルリポジトリ設定

はてなブログ記事MacPorts で自家製 ports を使う方法を参考に、レポジトリ設定ファイルに、ローカルリポジトリの URL を設定します(ここでは自分のホームディレクトリにディレクトリ "ports" を作成してます。)。

 % mkdir ~/ports
 % cat /opt/local/etc/macports/sources.conf
....
....

file:///Users/XXX/ports ### <=== 新しく追加 ###
rsync://rsync.macports.org/macports/release/tarballs/ports.tar [default]

ローカルリポジトリに設定ファイル追加

適当なフォルダで以下コマンドを実行します。

 % git clone https://github.com/gdaisukesuzuki/Macports_tensorflow_macos

すると、ports というフォルダが作成され以下にフォルダやファイルがぶら下がってます。
今回は以下コマンドを実行すればよいでしょう。

(ディレクトリ移動しない)
 % mkdir ~/ports/python
 % cp -pr ports/python/* ~/ports/python/
 % cd ~/ports
 % portindex

最後のportindexを実行することで、コマンド port 実行時に、ローカルリポジトリを参照するようになります。また、本家 macports リポジトリに登録されているコンポーネントも、この操作でローカルに置かれた方を優先することに。

Python3.8, などのインストールは、自分の前記事ですが、
『M1 Macへの Scipy, Scikit-learnインストール記事』を見てください。

Tensorflowなどインストール

 % sudo port install py38-tensorflow_macos pu38-tensorflow_addons

とすると、Tensorflowを使うのに必要なコンポーネントはインストールされるが、Tensorflowなどは"ファイルがない!"と怒られます。そのため、github の apple のリポジトリから直接ファイルを取ってくる(そして適切な場所に手動コピー)する必要があります。

 % cd (適当なフォルダに移動)
# ファイルを取得
 % curl -OL https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha2/tensorflow_macos-0.1alpha2.tar.gz
# ファイルを展開し、適当な場所にコピー
 % tar zxvf tensorflow_macos-0.1alpha2.tar.gz
 % sudo cp tensorflow_macos/arm64/tensorflow_macos-0.1a2-cp38-cp38-macosx_11_0_arm64.whl /opt/local/var/macports/distfiles/py-tensorflow_macos/     
 % sudo cp tensorflow_macos/arm64/tensorflow_addons_macos-0.1a2-cp38-cp38-macosx_11_0_arm64.whl /opt/local/var/macports/distfiles/py-tensorflow_addons 

でもう一度、tensorflow などをインストールしてみます。(だめなら -f オプションで強制インスコ)。

 % sudo port install py38-tensorflow_macos pu38-tensorflow_addons

で、import が成功すれば一応OK。

 % python  
Python 3.8.7 (default, Dec 25 2020, 07:51:03) 
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import tensorflow as tf
>>> import tensorflow_addons as tfa
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow_addons/utils/ensure_tf_install.py:54: UserWarning: Tensorflow Addons supports using Python ops for all Tensorflow versions above or equal to 2.2.0 and strictly below 2.4.0 (nightly versions are not supported). 
 The versions of TensorFlow you are currently using is 2.4.0-rc0 and is not supported. 
Some things might work, some things might not.
If you were to encounter a bug, do not file an issue.
If you want to make sure you're using a tested and supported configuration, either change the TensorFlow version or the TensorFlow Addons's version. 
You can find the compatibility matrix in TensorFlow Addon's readme:
https://github.com/tensorflow/addons
  warnings.warn(
>>> 

今回紹介したリンク

https://github.com/gdaisukesuzuki/Macports_tensorflow_macos ... 自家製Portfile について。
https://github.com/apple/tensorflow_macos ... Apple による、M1向け Tensorflow の実装(バイナリのみ)

記録

  1. (2021.2.14 19:40 JST)...とりあえず初版
  2. (2021.2.14 21:25 JST)...リンクとタイトル訂正
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1