若用户仅进行离线推理,请跳过此章节。
由于TensorFlow依赖h5py,而h5py依赖HDF5,需要先编译安装HDF5,否则使用pip安装h5py会报错,以下步骤以root用户操作。
tar -zxvf hdf5-1.10.5.tar.gz
cd hdf5-1.10.5/ ./configure --prefix=/usr/include/hdf5 make make install
export CPATH="/usr/include/hdf5/include/:/usr/include/hdf5/lib/"
ln -s /usr/include/hdf5/lib/libhdf5.so /usr/lib/libhdf5.so ln -s /usr/include/hdf5/lib/libhdf5_hl.so /usr/lib/libhdf5_hl.so
root用户下执行如下命令安装h5py,TensorFlow 1.15需安装h5py 2.8.0版本,TensorFlow 2.6.5需安装h5py 3.1.0版本,请所需版本修改如下命令。
pip3 install h5py==2.8.0
需要安装TensorFlow才可以进行算子开发验证、训练业务开发。
如下命令如果使用非root用户安装,需要在安装命令后加上--user,例如:pip3 install tensorflow-cpu==1.15 --user
pip3 install tensorflow-cpu==1.15
pip3 install tensorflow-cpu==2.6.5
在下载完tensorflow tag v1.15.0或tensorflow tag v2.6.5源码后需要执行如下步骤。
tf_http_archive( name = "nsync", sha256 = "caf32e6b3d478b78cff6c2ba009c3400f8251f646804bcb65465666a9cea93c4", strip_prefix = "nsync-1.22.0", system_build_file = clean_dep("//third_party/systemlibs:nsync.BUILD"), urls = [ "https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/nsync/archive/1.22.0.tar.gz", "https://github.com/google/nsync/archive/1.22.0.tar.gz", ], )
在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; }
将上个步骤中解压出的内容压缩为一个新的“nsync-1.22.0.tar.gz”源码包,保存(比如,保存在“/tmp/nsync-1.22.0.tar.gz”)。
sha256sum /tmp/nsync-1.22.0.tar.gz
tf_http_archive( name = "nsync", sha256 = "caf32e6b3d478b78cff6c2ba009c3400f8251f646804bcb65465666a9cea93c4", strip_prefix = "nsync-1.22.0", system_build_file = clean_dep("//third_party/systemlibs:nsync.BUILD"), urls = [ "https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/nsync/archive/1.22.0.tar.gz", "file:///tmp/nsync-1.22.0.tar.gz ", "https://github.com/google/nsync/archive/1.22.0.tar.gz", ], )
执行完./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
以上步骤执行完后会打包TensorFlow到指定目录,进入指定目录后执行如下命令安装:
如下命令如果使用非root用户安装,需要在安装命令后加上--user,例如:pip3 install tensorflow-1.15.0-*.whl --user
pip3 install tensorflow-1.15.0-*.whl
pip3 install tensorflow-2.6.5-*.whl
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
如果返回了张量则表示安装成功。