安装Mambaforge
首选推荐安装Mambaforge, 也可以安装miniforge3。
项目链接:https://github.com/conda-forge/miniforge/
创建conda虚拟环境
conda create -n tf270 -c apple tensorflow-deps
conda activate tf270
pip install tensorflow-macos tensorflow-metal
测试安装是否成功
import tensorflow as tf
print(tf.__version__)
print(tf.version.VERSION)
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
结果如下GPU即为安装成功:
(tf270) hzw@bogon ~ % python
Python 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:24:02)
[Clang 11.1.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from tensorflow.python.client import device_lib
>>> print(device_lib.list_local_devices())
Metal device set to: Apple M1
systemMemory: 8.00 GB
maxCacheSize: 2.67 GB
2021-12-15 16:38:14.922342: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2021-12-15 16:38:14.923134: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 16585735293412376744
xla_global_id: -1
, name: "/device:GPU:0"
device_type: "GPU"
locality {
bus_id: 1
}
incarnation: 17634086545751551048
physical_device_desc: "device: 0, name: METAL, pci bus id: <undefined>"
xla_global_id: -1
]
>>>