Macbook M1 pit avoidance Guide: installing apple tensorflow (arm64)


Because anaconda does not support M1 perfectly, the common steps are Xcode, miniforge, ATF2.4, etc.
TensorFlow 2.4 on Apple Silicon M1: installation under Conda environment

1. Problems you may encounter:

  • When installing tensorflow in Anaconda, Zsh: illustral hardware instruction appears;
  • Error prompts missing from various dependent libraries during conda install, such as from absl import logging ModuleNotFoundError: No module named 'absl';
  • Using miniforge to create a virtual environment can normally import tensorflow, but the model cannot be compiled. For example, when using keras, a simple Sequential model, an error is reported in model.compile(). The details are as follows: 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"
  • . . .

The specific suggestion is to reinstall TensorFlow and follow the official process step by step. Since it is an arm64 architecture, use TensorFlow that supports the arm64 architecture to avoid the regeneration problem.

For many friends, you can import tensorflow, but the model cannot be compiled: an error occurs when you get to model.fit.

Problem Description: unable to use keras for model compilation on M1

Error message:

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 

Cause analysis:
There is still a problem with TensorFlow version. Use an arm64 version of TensorFlow that supports Mac M1 chip. For specific operations, please refer to official.

2. Solutions

Step 1: install Xcode Command Line Tools, which can be downloaded and installed by Apple Developer.

Step 2: install arm version miniforge.

from miniforge github Select the latest ARM64 version and yes all the way.

After that, the terminal conda --version or conda info -e checks whether it is successful.

Step 3: from Mac-optimized TensorFlow2.4 and TensorFlow Addons Download tensorflow 2.4 of ARM64. The specific installation requirements are macOS 11.0 + and python 3.8

get into releases Select the latest version of tensorflow_macos-0.1alpha3.tar.gz:

tensorflow_ After downloading macos-0.1alpha3.tar.gz, unzip it first, and two folders arm64 and x86 will appear_ 64. You need to enter the cd into the arm64 folder.

Step 4: go to. \ arm64 to create conda virtual environment.

Create a new environment tf24:

conda create --name tf24

After creation, use conda info -e to view.

Activate the environment tf24 and install Python 3.8.6 and pandas.

conda activate tf24
conda install -y python==3.8.6
conda install -y pandas matplotlib scikit-learn jupyterlab

Step 5: start installing apple tensorflow 2.4

Step 5.1 view the arm64 folder and force the installation of these whl files (Note: Tensorflow packages are not installed here)

The whl file in the arm64 folder is as follows:

Force the installation of whl other than Tensorflow package first:

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

After Step 5.2 is installed, install some dependent libraries:

Because there are many dependent packages for installing TensorFlow, install these dependent packages first. The details are as follows:

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 can finally be installed

Continue installing the whl file from the arm64 folder:

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

The tensorflow installation is now complete.

Step 5.4 enter Python to check TensorFlow version

Step 5.5 test code

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)


It can be compiled normally:

Posted on Fri, 03 Dec 2021 10:19:01 -0500 by monk.e.boy