Install tensorflow_macos on Macbook M1 (Apple silicon)
Preface
This article in written in 2021.10.27, and current version is
0.1a3
. In the future, Apple may provide a better machine
learning structure on TensorFlow, so please always remember to check here.
My feeling: 🐂🍺 but lots of bugs
Article reference.
X-code
- Install:
xcode-select --install
- Check:
which xcrun
, you should get the path toxcrun
Miniforge
Miniforge is just a substitude of Anaconda.
Follow the instruction here.
Create virtual environment
Create
environment.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28name: apple_tensorflow
channels:
- conda-forge
- nodefaults
dependencies:
- absl-py
- astunparse
- gast
- google-pasta
- grpcio
- h5py
- ipython
- keras-preprocessing
- numpy
- opt_einsum
- pip=20.2.4
- protobuf
- python-flatbuffers
- python=3.8
- scipy
- tensorboard
- tensorflow-estimator
- termcolor
- typeguard
- typing_extensions
- wheel
- wrapt
Create new environment:
conda env create --file <PATH_TO_ENVIRONMENT.yaml> --name=<YOUR_ENV_NAME_HERE>
Activate environment:
conda activate <YOUR_ENV_NAME_HERE>
Install Tensorflow
Directly install whl
1
pip install --upgrade --force --no-dependencies https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_addons_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl
check here for the latest released version
Check:
1
2$ python
>>> import tensorflow as tfIf there is no error, you have already install tensorflow.
Here is a demo for TensorFlow.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28import 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.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
start = time.time()
model.fit(x_train, y_train, epochs=10)
end = time.time()
model.evaluate(x_test, y_test)
print("time consumed: ", end - start)
Congratulations! You have done everything. Now it is time to enjoy (be tortured) tensorflow!
Something more you should note
Why not GPU
If you check your activity monitor, you will find out that GPU is not used by tensorflow.
Apple implemented machine learning acceleration on their own, and the eager version of machine learning acceleration is currently only available on CPU.
However, you can still ask tensorflow to use GPU via:
1 | # Import mlcompute module to use the optional set_mlc_device API for device selection with ML Compute. |
If you set mlcompute.set_mlc_device(device_name='any')
,
it will automatically select the best -- generally CPU.
With GPU, M1 will report
WARNING:tensorflow:Eager mode uses the CPU. Switching to the CPU.
You can test GPU on your will, but GPU is not recomended (it is much less powerful).
Debug
If you encounter something like this
AutoGraph could not transform
, it may be caused bygast
.pip install gast==0.3.3
will help.I also found an error
Failed to get CPU frequency: 0 Hz
. However I still cannot solved it. And the repo of tensorflow_macos is archived, so I cannot report this issue to Apple. But it seems several people have this problem too. May be this is not a big issue just for learning. Let's wait the beta version of tensorflow_macos!