如何在CentOS 7上编译TensorFlow v1.15.0源码

释放双眼,带上耳机,听听看~!
本教程将指导您如何在CentOS 7上使用Bazel编译TensorFlow v1.15.0的源码,让您轻松开始使用最新的TensorFlow版本。

本文正在参加极智开发 | centos7源码编译bazel》已经为这篇 tensorflow 的源码编译铺平了道路,所以这里的前提是你已经安装好了 bazel。

这里咱们以源码编译 tensorflow v1.15.0 为例,下面开始。

  • (1) 下载源码
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow

需要注意的是,这样的方式下载的 tensorflow 源码是默认master分支的,也就是默认是最新的,而在咱们的需求中,往往是需要编译指定版本的 tensorflow,所以你可以这样:

# clone好后进入tensorflow目录,执行如下
git checkout branch_name  # r2.2, r2.3, etc.

当然也可以直接到release里下载对应版本的源码包,地址:release

  • (2) 下载 nsync-1.22.0.tar.gz 源码包

进入 tensorflow tag v1.15.0 源码目录,打开 tensorflow/workspace.bzl 文件,找到其中 name 为 nsynctf_http_archive 定义,如下:

如何在CentOS 7上编译TensorFlow v1.15.0源码

从上面的 urls 中随便选一个链接下载,并将下载好的 nsync-1.22.0.tar.gz 放到任意目录,比如我放到 /workspace/env 下。

  • (3) 修改 nsync-1.22.0.tar.gz 源码包
cd /workspace/env 
tar -zxvf nsync-1.22.0.tar.gz

cd nsync-1.22.0/platform/c++11

然后打开atomic.h,在 NSYNC_CPP_START_替换/添加 如下内容:

#include "nsync_cpp.h"
#include "nsync_atomic.h"

NSYNC_CPP_START_

#define ATM_CB_() __sync_synchronize()

static INLINE int atm_cas_nomb_u32_ (nsync_atomic_uint32_ *p, uint32_t o, uint32_t n) {
    int result = (std::atomic_compare_exchange_strong_explicit (NSYNC_ATOMIC_UINT32_PTR_ (p), &o, n, std::memory_order_relaxed, std::memory_order_relaxed));
    ATM_CB_();
    return result;
}
static INLINE int atm_cas_acq_u32_ (nsync_atomic_uint32_ *p, uint32_t o, uint32_t n) {
    int result = (std::atomic_compare_exchange_strong_explicit (NSYNC_ATOMIC_UINT32_PTR_ (p), &o, n, std::memory_order_acquire, std::memory_order_relaxed));
    ATM_CB_();
    return result;
}
static INLINE int atm_cas_rel_u32_ (nsync_atomic_uint32_ *p, uint32_t o, uint32_t n) {
    int result = (std::atomic_compare_exchange_strong_explicit (NSYNC_ATOMIC_UINT32_PTR_ (p), &o, n, std::memory_order_release, std::memory_order_relaxed));
    ATM_CB_();
    return result;
}
static INLINE int atm_cas_relacq_u32_ (nsync_atomic_uint32_ *p, uint32_t o, uint32_t n) {
    int result = (std::atomic_compare_exchange_strong_explicit (NSYNC_ATOMIC_UINT32_PTR_ (p), &o, n, std::memory_order_acq_rel, std::memory_order_relaxed));
    ATM_CB_();
    return result;
}
  • (4) 重新生成 nsync-1.22.0.tar.gz 源码包

上面对 nsync-1.22.0 源码进行了修改,重新压缩生成 nsync-1.22.0.tar.gz,替换原来的 nsync-1.22.0.tar.gz,因此包的路径依旧还是 /workspace/env/nsync-1.22.0.tar.gz

  • (5) sha256sum 校验 nsync-1.22.0.tar.gz
sha256sum /workspace/env/nsync-1.22.0.tar.gz

# 生成一串sha256sum校验码,记住它
  • (6) 修改tensorflow workspace.bzl

回到第(2)步中的 ~/tensorflow-1.15.0/tensorflow/workspace.bzl,修改 sha256 = 为第(5)步生成的校验码,在 urls = [] 中新增 nsync-1.22.0.tar.gz的绝对路径,如下:

如何在CentOS 7上编译TensorFlow v1.15.0源码

  • (7) 配置系统build
cd tensorflow-1.15.0
# 直接执行
./configure
# 会跳出来很多配置选项,按自己的需要进行选择
# 如果只是编译最基础的CPU版本,一路N到底就可以了

执行完 ./configure 之后,需要修改 .tf_configure.bazelrc 配置文件:

# 添加如下一行 build编译选项
build:opt --cxxopt=-D_GLIBCXX_USE_CXX11_ABI=0
# 删除以下两行
build:opt --copt=-march=native 
build:opt --host_copt=-march=native
  • (8) 编译

使用 bazel 开始编译:

bazel build [--config=option] /path/tensorflow/tools/pip_package:build_pip_package

编译成功后会在对应目录中生成 .whl 软件包

  • (9) 安装.whl
pip install path/tensorflow-version-tags.whl

这样就大功告成了。

好了,以上分享了 centos7 源码编译 tensorflow 的方法。希望我的分享能对你的学习有一点帮助。

 【公众号传送】

《极智AI | centos7源码编译tensorflow》

如何在CentOS 7上编译TensorFlow v1.15.0源码

本网站的内容主要来自互联网上的各种资源,仅供参考和信息分享之用,不代表本网站拥有相关版权或知识产权。如您认为内容侵犯您的权益,请联系我们,我们将尽快采取行动,包括删除或更正。
AI教程

机器学习的两大应用场景——回归与分类

2023-12-21 5:28:14

AI教程

数字图像处理中的灰度化处理方法比较

2023-12-21 7:18:14

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索