0
点赞
收藏
分享

微信扫一扫

极智AI | centos7源码编译tensorflow


大家好,我是极智视界,本文介绍一下 centos7 源码编译 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 为 ​​nsync​​​ 的 ​​tf_http_archive​​ 定义,如下:

极智AI | centos7源码编译tensorflow_tensorflow

从上面的 ​​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的绝对路径​​,如下:

极智AI | centos7源码编译tensorflow_bazel_02

  • (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_centos7_03

 


举报

相关推荐

0 条评论