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

相关文章

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

Python中Tensorflow无法调用GPU问题的解决方法

《Python中Tensorflow无法调用GPU问题的解决方法》文章详解如何解决TensorFlow在Windows无法识别GPU的问题,需降级至2.10版本,安装匹配CUDA11.2和cuDNN... 当用以下代码查看GPU数量时,gpuspython返回的是一个空列表,说明tensorflow没有找到

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

Nexus安装和启动的实现教程

《Nexus安装和启动的实现教程》:本文主要介绍Nexus安装和启动的实现教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Nexus下载二、Nexus安装和启动三、关闭Nexus总结一、Nexus下载官方下载链接:DownloadWindows系统根