Macbook M1避坑指南:安装Apple-TensorFlow(arm64)

目录
因为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 --versionconda info -e检查是否成功。

Step 3: 从Mac-optimized TensorFlow2.4 and TensorFlow Addons下载ARM64版本的TensorFlow2.4,具体的安装要求是macOS 11.0+, Python3.8.

Macbook M1避坑指南:安装Apple-TensorFlow(arm64)

进入releases选择最新的版本tensorflow_macos-0.1alpha3.tar.gz

Macbook M1避坑指南:安装Apple-TensorFlow(arm64)

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文件:

Macbook M1避坑指南:安装Apple-TensorFlow(arm64)

先强制安装除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

Macbook M1避坑指南:安装Apple-TensorFlow(arm64)

Step 5.2 安装完成后,再安装一些依赖库:

因为安装TensorFlow有很多依赖的其他包,先安装这些依赖包。具体如下所示:

Macbook M1避坑指南:安装Apple-TensorFlow(arm64)

pip install absl-py astunparse flatbuffers gast google_pasta keras_preprocessing opt_einsum protobuf tensorflow_estimator termcolor typing_extensions wrapt wheel tensorboard typeguard

Macbook M1避坑指南:安装Apple-TensorFlow(arm64)

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

Macbook M1避坑指南:安装Apple-TensorFlow(arm64)

至此tensorflow安装完成。

Step 5.4 进入Python检查TensorFlow版本

Macbook M1避坑指南:安装Apple-TensorFlow(arm64)

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)


可以正常编译:

Macbook M1避坑指南:安装Apple-TensorFlow(arm64)

上一篇:Windows 10 on ARM, version 21H2 (released Nov 2021) ARM64 简体中文版、英文版(企业版)下载


下一篇:搭建在线VSCode——code-server