MacOS 10.12编译安装 TensorFlow

文章目录

使用 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

; 说明

  • 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.

环境检查


sysctl machdep.cpu | egrep "SSE|AVX"

gcc -march=native -dM -E - < /dev/null | egrep "SSE|AVX" | sort

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 install python

pip uninstall tensorflow

pip install -U pip numpy wheel six
pip install -U keras_preprocessing --no-deps

brew unlink bazel

brew install bazelisk
bazel --version

brew install coreutils

sudo xcodebuild -license
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

git clone https://github.com/tensorflow/tensorflow.git

cat tensorflow/configure.py|grep "BAZEL_VERSION = '"
cd tensorflow
git checkout r2.8

export BAZEL_SH=/bin/bash

bazel clean --expunge

./configure

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/python@3.9/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/python@3.9/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

测试

  • 如果没有警告消息,则安装成功
    [En]

    if there is no warning message, the installation is successful*

python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

其他

build_tf.sh

  • 不添加 --copt=-march=native, 添加后编译的 AVX512F 不支持, 原因不详
#!/bin/bashraw_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_flagsdo    case "$cpu_feature" in        "sse4.1" | "sse4.2" | "ssse3" | "fma" | "cx16" | "popcnt" | "maes")            COPT+=" --copt=-m$cpu_feature"        ;;        "avx1.0")            COPT+=" --copt=-mavx"        ;;        *)        ;;    esacdonemkdir /tmp/tensorflow_pkgchmod 777 /tmp/tensorflow_pkgbazel clean./configurebazel build -c opt $COPT -k //tensorflow/tools/pip_package:build_pip_packagebazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkgpip3 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/

Original: https://blog.csdn.net/shichen501/article/details/123968324
Author: shichen501
Title: MacOS 10.12编译安装 TensorFlow

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/497326/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球