目錄
因為anaconda現在還沒完美支持M1,因此通用的步驟還是Xcode、miniforge、ATF2.4等。
TensorFlow 2.4 on Apple Silicon M1: installation under Conda environment
1. 你可能遇到的問題:
- Anaconda下安裝tensorflow出現zsh: illegal hardware instruction;
- conda install 過程中各種依賴庫缺少的錯誤提示,如
from absl import logging
ModuleNotFoundError: No module named 'absl'; - 使用miniforge創建虛擬環境下能正常import tensorflow,但是模型無法編譯。 例如使用keras,一個簡單的Sequential模型,到model.compile()就報錯了。具體如下:
I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2); W tensorflow/core/platform/profile_utils/cpu_utils.cc:126] Failed to get CPU frequency: 0 Hz; F tensorflow/core/grappler/costs/op_level_cost_estimator.cc:710] Check failed: 0 < gflops (0 vs. 0)type: "CPU"
- 。。。
具體的建議就是:重新安裝TensorFlow,按照官方提供的流程一步步走。既然是arm64架構,那就用支持arm64架構的TensorFlow,避免再生問題。
對於很多朋友提出,能import tensorflow,但是模型無法編譯:一到model.fit就出錯。
問題描述:無法在M1上使用keras進行模型編譯
報錯信息:
2021-09-29 12:04:50.205695: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
2021-09-29 12:04:50.205850: W tensorflow/core/platform/profile_utils/cpu_utils.cc:126] Failed to get CPU frequency: 0 Hz
2021-09-29 12:04:50.206537: F tensorflow/core/grappler/costs/op_level_cost_estimator.cc:710] Check failed: 0 < gflops (0 vs. 0)type: "CPU"
model: "0"
num_cores: 8
environment {
key: "cpu_instruction_set"
value: "ARM NEON"
}
environment {
key: "eigen"
value: "3.3.90"
}
l1_cache_size: 16384
l2_cache_size: 524288
l3_cache_size: 524288
memory_size: 268435456
zsh: abort /Users/dan/miniforge3/envs/pytorch_env/bin/python
原因分析:
還是TensorFlow版本的問題, 使用一個支持Mac M1芯片的arm64版本的TensorFlow。具體的操作可以參考官方。
2. 解決方案
Step 1:安裝Xcode Command Line Tools,Apple Developer下載安裝即可。
Step 2:安裝arm版本miniforge。
從miniforge github選擇最新的ARM64版本,一路yes就行。
之后終端conda --version
或 conda info -e
檢查是否成功。
Step 3: 從Mac-optimized TensorFlow2.4 and TensorFlow Addons下載ARM64版本的TensorFlow2.4,具體的安裝要求是macOS 11.0+, Python3.8.
進入releases選擇最新的版本tensorflow_macos-0.1alpha3.tar.gz:
tensorflow_macos-0.1alpha3.tar.gz下載后,先解壓會出現2個文件夾arm64和x86_64,需要cd進入arm64文件夾。
Step 4:路徑到.\arm64,創建conda虛擬環境。
創建一個新環境tf24:
conda create --name tf24
創建后利用conda info -e
查看。
激活環境tf24,安裝Python3.8.6和pandas等。
conda activate tf24
conda install -y python==3.8.6
conda install -y pandas matplotlib scikit-learn jupyterlab
Step 5:開始安裝Apple-TensorFlow2.4
Step 5.1查看arm64文件夾,強制安裝這些whl文件(注意:這里不安裝Tensorflow的包)
如下所示arm64文件夾內的whl文件:
先強制安裝除Tensorflow包以外的whl:
pip install --upgrade --no-dependencies --force numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl grpcio-1.33.2-cp38-cp38-macosx_11_0_arm64.whl h5py-2.10.0-cp38-cp38-macosx_11_0_arm64.whl
Step 5.2 安裝完成后,再安裝一些依賴庫:
因為安裝TensorFlow有很多依賴的其他包,先安裝這些依賴包。具體如下所示:
pip install absl-py astunparse flatbuffers gast google_pasta keras_preprocessing opt_einsum protobuf tensorflow_estimator termcolor typing_extensions wrapt wheel tensorboard typeguard
Step 5.3 終於可以安裝TensorFlow
繼續安裝來自arm64文件夾的whl文件:
pip install --upgrade --force --no-dependencies tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl
pip install --upgrade --force --no-dependencies tensorflow_addons_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl
至此tensorflow安裝完成。
Step 5.4 進入Python檢查TensorFlow版本
Step 5.5 測試代碼
import tensorflow as tf
import time
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.summary()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
start = time.time()
model.fit(x_train, y_train, epochs=5)
end = time.time()
model.evaluate(x_test, y_test)
print(end - start)
可以正常編譯: