Docker——The TensorFlow library wasn't compiled to use SSE/AVX/FMA instructions

搭建docker环境之tensorflow-gpu(指定版本+从源码编译)

背景

两个问题:

  1. 版本问题:英伟达官网上的tensorflow镜像没有我想要的版本。
  2. CPU问题:通过pip安装的tensorflow在运行时会发出警告:浪费了CPU的性能。这是因为tensorflow并不是通过源码编译造成的。
2020-01-19 08:03:05.879415: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2020-01-19 08:03:05.879452: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2020-01-19 08:03:05.879477: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2020-01-19 08:03:05.879484: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2020-01-19 08:03:05.879492: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX512F instructions, but these are available on your machine and could speed up CPU computations.
2020-01-19 08:03:05.879499: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.

步骤

安装步骤整体参照tensorflow官网,结合实际情况,一些地方略有修改。此外,各类安装包的版本严格按照该官网上的建议。

  1. 英伟达官网上下载对应版本镜像
  2. apt-get 更新
apt-get update
  1. 安装 python 及 tensorflow pip 软件包依赖项。其中 --user 选项表示安装在 $HOME/bin,如果没有则安装在 $HOME/local/bin。后者适用于安装在虚拟环境中。
apt-get install python-dev python-pip
pip install -U --user pip six numpy wheel setuptools mock future>=0.17.1
pip install -U --user keras_applications==1.0.6 --no-deps
pip install -U --user keras_preprocessing==1.0.5 --no-deps
  1. 安装 bazel 。先下载对应版本的安装包(.sh结尾)。其中版本号以实际情况为准
apt-get install zip upzip
apt-get install gcc-4.8 g++-4.8

gcc -v # 查看当前 gcc/g++ 版本
g++ -v

update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50 # 设置 gcc/g++ 优先级,优先使用4.8
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 20
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 20

chmod +x bazel-<version>-installer-linux-x86_64.sh # 给安装器添加执行权限
./bazel-<version>-installer-linux-x86_64.sh --user # 执行安装器

执行完毕后将环境变量添加上。例如可以将 export PATH=$PATH:/usr/bin 添加到 ~/.bashrc 文件的末尾,然后执行 suorce ~/.bashrc。由于并不需要编译 java 代码,就不继续安装 JDK8。

  1. 进入 tensorflow 源代码树的根目录,开始安装tensorflow。
cd tensorflow

# 配置
./configure

# 构建软件包编译器
# CPU (二选一)
bazel build --config=opt --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mavx512f --copt=-mfma //tensorflow/tools/pip_package:build_pip_package
# GPU
bazel build --config=opt --config=cuda --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mavx512f --copt=-mfma //tensorflow/tools/pip_package:build_pip_package

# 编译
# 从某个版本分支编译 (二选一)
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
# 从 master 编译
./bazel-bin/tensorflow/tools/pip_package/build_pip_package --nightly_flag /tmp/tensorflow_pkg

# 安装
pip install /tmp/tensorflow_pkg/tensorflow-[version-tags].whl

如果在 bazel build 步骤遇到raw.githubusercontent.com访问受限的问题,在docker内部配置好ssr就行了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值