MacOS 10.12编译安装 TensorFlow

2023-12-03 13:32

本文主要是介绍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.decho "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

编译安装

本机型号

image-20220405133635208

说明

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

测试

  • 如果没有警告信息,则安装成功
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-instructionsraw_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
docase "$cpu_feature" in"sse4.1" | "sse4.2" | "ssse3" | "fma" | "cx16" | "popcnt" | "maes")COPT+=" --copt=-m$cpu_feature";;"avx1.0")COPT+=" --copt=-mavx";;*)# noop;;esac
donemkdir /tmp/tensorflow_pkg
chmod 777 /tmp/tensorflow_pkgbazel 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_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/

这篇关于MacOS 10.12编译安装 TensorFlow的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/449631

相关文章

Win安装MySQL8全过程

《Win安装MySQL8全过程》:本文主要介绍Win安装MySQL8全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Win安装mysql81、下载MySQL2、解压文件3、新建文件夹data,用于保存数据库数据文件4、在mysql根目录下新建文件my.ini

最详细安装 PostgreSQL方法及常见问题解决

《最详细安装PostgreSQL方法及常见问题解决》:本文主要介绍最详细安装PostgreSQL方法及常见问题解决,介绍了在Windows系统上安装PostgreSQL及Linux系统上安装Po... 目录一、在 Windows 系统上安装 PostgreSQL1. 下载 PostgreSQL 安装包2.

Maven如何手动安装依赖到本地仓库

《Maven如何手动安装依赖到本地仓库》:本文主要介绍Maven如何手动安装依赖到本地仓库问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、下载依赖二、安装 JAR 文件到本地仓库三、验证安装四、在项目中使用该依赖1、注意事项2、额外提示总结一、下载依赖登

如何在Mac上安装并配置JDK环境变量详细步骤

《如何在Mac上安装并配置JDK环境变量详细步骤》:本文主要介绍如何在Mac上安装并配置JDK环境变量详细步骤,包括下载JDK、安装JDK、配置环境变量、验证JDK配置以及可选地设置PowerSh... 目录步骤 1:下载JDK步骤 2:安装JDK步骤 3:配置环境变量1. 编辑~/.zshrc(对于zsh

如何在pycharm安装torch包

《如何在pycharm安装torch包》:本文主要介绍如何在pycharm安装torch包方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录在pycharm安装torch包适http://www.chinasem.cn配于我电脑的指令为适用的torch包为总结在p

在PyCharm中安装PyTorch、torchvision和OpenCV详解

《在PyCharm中安装PyTorch、torchvision和OpenCV详解》:本文主要介绍在PyCharm中安装PyTorch、torchvision和OpenCV方式,具有很好的参考价值,... 目录PyCharm安装PyTorch、torchvision和OpenCV安装python安装PyTor

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi

idea maven编译报错Java heap space的解决方法

《ideamaven编译报错Javaheapspace的解决方法》这篇文章主要为大家详细介绍了ideamaven编译报错Javaheapspace的相关解决方法,文中的示例代码讲解详细,感兴趣的... 目录1.增加 Maven 编译的堆内存2. 增加 IntelliJ IDEA 的堆内存3. 优化 Mave

如何解决mmcv无法安装或安装之后报错问题

《如何解决mmcv无法安装或安装之后报错问题》:本文主要介绍如何解决mmcv无法安装或安装之后报错问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mmcv无法安装或安装之后报错问题1.当我们运行YOwww.chinasem.cnLO时遇到2.找到下图所示这里3.

Python 安装和配置flask, flask_cors的图文教程

《Python安装和配置flask,flask_cors的图文教程》:本文主要介绍Python安装和配置flask,flask_cors的图文教程,本文通过图文并茂的形式给大家介绍的非常详细,... 目录一.python安装:二,配置环境变量,三:检查Python安装和环境变量,四:安装flask和flas