MacOS 10.12编译安装 TensorFlow

文章目录

  • 使用 conda 安装
  • 编译安装
    • 本机型号
    • 说明
    • 环境检查
    • 开始安装
      • 选项
    • 测试
    • 其他
      • build_tf.sh
    • 常见问题
      • Compiling src/google/protobuf/compiler/main.cc [for host] failed: undeclared inclusion(s)
      • ERROR: tensorflow-2.8.0-cp39-cp39-macosx_12_0_x86_64.whl is not a supported wheel on this platform.

使用 conda 安装

  1. 安装的 TensorFlow 未使用高级 CPU 指令,存在警告,可通过环境变量忽略
conda create --name tf2 python=3.9
conda activate tf2
pip install tensorflow
  1. 忽略警告
cd $CONDA_PREFIX
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d

echo "export TF_CPP_MIN_LOG_LEVEL=2" >> ./etc/conda/activate.d/env_vars.sh
echo "unset TF_CPP_MIN_LOG_LEVEL" >> ./etc/conda/deactivate.d/env_vars.sh

编译安装

本机型号

MacOS 10.12编译安装 TensorFlow_第1张图片

说明

  • TensorFlow 2.8,python 3.9,且本机不支持 CUDA
  • 编译时间过长,如果环境和我的一样建议先尝试安装我编译后的文件,下载地址
  • 默认使用 pip 安装的 TensorFlow 未使用高级 CPU 指令,存在警告,警告如下:
2022-04-04 23:20:23.642820: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  SSE4.2 AVX AVX2 FMA
  • 想要更高的运算速率,需要手动编译安装 TensorFlow
  • 使用 conda 安装的 python 并不支持安装 MacOS 10.12 版本的 whell,提示如下:
ERROR: tensorflow-2.9.0rc0-cp39-cp39-macosx_12_0_x86_64.whl is not a supported wheel on this platform.
  • 即使 CPU 支持 AVX512F,编译安装后仍然会报错,错误如下:
The TensorFlow library was compiled to use AVX512F instructions, but these aren't available on your machine.

环境检查

# 查看 CPU 支持选项
sysctl machdep.cpu | egrep "SSE|AVX"

# 查看 -march=native 支持编译选项
gcc -march=native -dM -E - < /dev/null | egrep "SSE|AVX" | sort

# 查看 pip 支持 MacOS 版本
# cp39 表示 python3.9
pip debug --verbose | grep cp39-cp39-macosx_12

开始安装

  • 简介
    • FMA(Fused Multiply Accumulate) 是现代 CPU 支持的一种高级指令集, 中文叫"积和熔加运算"
    • AVX2 是现代 CPU 支持的"高级向量扩展指令集"
    • bazel 支持多种语言混编的项目并且可以根据不同的平台输出不同的构建结果
      • 会根据 TensorFlow 工程下的 .bazelrc 决定版本
    • ROCm 为 AMD 的运算平台
  • 必须安装 Xcode,只安装 xcode-select --install 测试失败
# 必须使用 brew 安装的 python,示例版本为 3.9.9
brew install python

# 卸载旧版本
pip uninstall tensorflow

# 安装依赖项
pip install -U pip numpy wheel six
pip install -U keras_preprocessing --no-deps

# 安装 bazel
brew unlink bazel
# bazel 管理工具
brew install bazelisk
bazel --version

# 安装 coreutils
brew install coreutils

# 必须完整安装 Xcode,可以从 App Store 下载或者从下面地址下载
# https://download.developer.apple.com/Developer_Tools/Xcode_13.3/Xcode_13.3.xip
sudo xcodebuild -license
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

# 安装 TensorFlow
git clone https://github.com/tensorflow/tensorflow.git
# 确定 bazel 版本符合要求
cat tensorflow/configure.py|grep "BAZEL_VERSION = '"
cd tensorflow
git checkout r2.8

# 构建 pip 软件包
export BAZEL_SH=/bin/bash
# 每次重新编译安装前需要清除上次构建缓存
bazel clean --expunge

# 配置选项,参考下文
./configure
# 构建选项参考 # https://gist.github.com/venik/9ba962c8b301b0e21f99884cbd35082f
# 构建时间大概需要 4 个小时
bazel build -c opt --copt=-mavx2 --copt=-mssse3 --copt=-mfma --copt=-mcx16 --copt=-msse4.1 --copt=-msse4.2 --copt=-mpopcnt --copt=-mavx -k //tensorflow/tools/pip_package:build_pip_package
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

# 安装软件包
pip install --upgrade /tmp/tensorflow_pkg/tensorflow-2.8.0-cp39-cp39-macosx_12_0_x86_64.whl

选项

  • 选择正确的 python Library 路径,其他的选择否
You have bazel 4.2.1 installed.
Please specify the location of python. [Default is /usr/local/anaconda3/envs/tf2/bin/python3]:


Found possible Python library paths:
  /Users/liuende/workspace/python/wswp/src
  /usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages
Please input the desired Python library path to use.  Default is [/Users/liuende/workspace/python/wswp/src]
/usr/local/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages
Do you wish to build TensorFlow with ROCm support? [y/N]: n
No ROCm support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]: n
No CUDA support will be enabled for TensorFlow.

Do you wish to download a fresh release of clang? (Experimental) [y/N]: n
Clang will not be downloaded.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]:


Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: n
Not configuring the WORKSPACE for Android builds.

Do you wish to build TensorFlow with iOS support? [y/N]: n
iOS support will be enabled for TensorFlow.

Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.
    --config=mkl            # Build with MKL support.
    --config=mkl_aarch64    # Build with oneDNN and Compute Library for the Arm Architecture (ACL).
    --config=monolithic     # Config for mostly static monolithic build.
    --config=numa           # Build with NUMA support.
    --config=dynamic_kernels    # (Experimental) Build kernels into separate shared objects.
    --config=v1             # Build with TensorFlow 1 API instead of TF 2 API.
Preconfigured Bazel build configs to DISABLE default on features:
    --config=nogcp          # Disable GCP support.
    --config=nonccl         # Disable NVIDIA NCCL support.
Configuration finished

测试

  • 如果没有警告信息,则安装成功
python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

其他

build_tf.sh

  • 不添加 --copt=-march=native, 添加后编译的 AVX512F 不支持, 原因不详
#!/bin/bash
 
# Author: Sasha Nikiforov
 
# source of inspiration
# https://stackoverflow.com/questions/41293077/how-to-compile-tensorflow-with-sse4-2-and-avx-instructions
 
raw_cpu_flags=`sysctl -a | grep machdep.cpu.features | cut -d ":" -f 2 | tr '[:upper:]' '[:lower:]'`
COPT="--copt=-march=native"
 
for cpu_feature in $raw_cpu_flags
do
    case "$cpu_feature" in
        "sse4.1" | "sse4.2" | "ssse3" | "fma" | "cx16" | "popcnt" | "maes")
            COPT+=" --copt=-m$cpu_feature"
        ;;
        "avx1.0")
            COPT+=" --copt=-mavx"
        ;;
        *)
            # noop
        ;;
    esac
done
 
mkdir /tmp/tensorflow_pkg
chmod 777 /tmp/tensorflow_pkg
 
bazel clean
./configure
bazel build -c opt $COPT -k //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
 
pip3 install --upgrade /tmp/tensorflow_pkg/`ls /tmp/tensorflow_pkg/ | grep tensorflow`

常见问题

Compiling src/google/protobuf/compiler/main.cc [for host] failed: undeclared inclusion(s)

https://github.com/bazelbuild/bazel/issues/2852#issuecomment-295747929

export BAZEL_SH=/bin/bash

ERROR: tensorflow-2.8.0-cp39-cp39-macosx_12_0_x86_64.whl is not a supported wheel on this platform.

tensorflow-2.8.0-cp39-cp39-macosx_12_0_x86_64.whl中的 cp39 表示 python3.9,anaconda 安装的 python 不支持安装 MacOS 10.12 版本的 whell,检查如下

pip debug --verbose | grep cp39-cp39-macosx_12

参考资料:
https://gist.github.com/venik/9ba962c8b301b0e21f99884cbd35082f
https://www.tensorflow.org/install/source?hl=zh-cn
https://knowm.org/compiling-tensorflow-from-source-on-macos/

你可能感兴趣的:(mac使用,人工智能,mac,tensorflow)