文档
注册

源码编译安装TensorFlow

安装前准备

由于TensorFlow依赖h5py,而h5py依赖HDF5,需要先编译安装HDF5,否则使用pip安装h5py会报错,以下步骤以root用户操作。

  1. 编译安装HDF5。
    1. 访问下载链接下载HDF5源码包,并上传到安装环境的任意目录。
    2. 进入源码包所在目录,执行如下命令解压源码包。
      tar -zxvf hdf5-1.10.5.tar.gz
    3. 进入解压后的文件夹,执行配置、编译和安装命令:
      cd hdf5-1.10.5/
      ./configure --prefix=/usr/include/hdf5
      make
      make install
    4. 配置环境变量并建立动态链接库软连接。
      1. 配置环境变量。
        export CPATH="/usr/include/hdf5/include/:/usr/include/hdf5/lib/"
      2. root用户建立动态链接库软连接命令如下,非root用户需要在以下命令前添加sudo。
        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
  2. 安装h5py。
    1. 执行如下命令安装h5py依赖包。
      pip3 install Cython
    2. 执行如下命令安装h5py。
      pip3 install h5py==2.8.0

安装TensorFlow

  • TensorFlow1.15配套的Python版本是:Python3.7.x(3.7.5~3.7.11)。
  • TensorFlow2.6.5配套的Python版本是:Python3.7.x(3.7.5~3.7.11)、Python3.8.x(3.8.0~3.8.11)、Python3.9.x(3.9.0~3.9.2)。
  • TensorFlow2.6.5存在漏洞,请参考相关漏洞及其修复方案处理。

由于pip源未提供对应的版本,所以需要用户使用官网要求的linux_gcc7.3.0编译器编译tensorflow1.15.0或tensorflow2.6.5,编译步骤参考官网TensorFlow官网。特别注意点如下。

在下载完tensorflow tag v1.15.0或tensorflow tag v2.6.5源码后需要执行如下步骤。

  1. 下载“nsync-1.22.0.tar.gz”源码包。
    1. 进入源码目录,TensorFlow1.15场景下打开“tensorflow/workspace.bzl”文件,TensorFlow2.6.5场景下打开“tensorflow/workspace2.bzl”文件,找到其中name为nsync的“tf_http_archive”定义。
          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",
              ],
          )
    2. 从urls中的任一路径下载nsync-1.22.0.tar.gz的源码包,保存到任意路径。
  2. 修改“nsync-1.22.0.tar.gz”源码包。
    1. 切换到nsync-1.22.0.tar.gz所在路径,解压缩该源码包。解压缩后存在“nsync-1.22.0”文件夹。
    2. 编辑“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;
      }
  3. 重新压缩“nsync-1.22.0.tar.gz”源码包。

    将上个步骤中解压出的内容压缩为一个新的“nsync-1.22.0.tar.gz”源码包,保存(比如,保存在“/tmp/nsync-1.22.0.tar.gz”)。

  4. 重新生成“nsync-1.22.0.tar.gz”源码包的sha256sum校验码。
    执行如下命令后得到sha256sum校验码(一串数字和字母的组合)。
    sha256sum /tmp/nsync-1.22.0.tar.gz
  5. 修改sha256sum校验码和urls。
    进入tensorflow tag源码目录,TensorFlow1.15场景下打开“tensorflow/workspace.bzl”文件,TensorFlow2.6.5场景下打开“tensorflow/workspace2.bzl”文件,找到其中name为nsync的“tf_http_archive”定义,其中“sha256=”后面的数字填写4得到的校验码,“urls=”后面的列表第二行,填写存放“nsync-1.22.0.tar.gz”的file://索引。
        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",
            ],
        )
  6. 继续参考官网的“配置build”(TensorFlow官网)章节执行编译。

    执行完./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
  7. 继续执行官方的编译指导步骤(TensorFlow官网)即可。
  8. 安装编译好的TensorFlow。

    以上步骤执行完后会打包TensorFlow到指定目录,进入指定目录后执行如下命令安装:

    • TensorFlow1.15:
      pip3 install tensorflow-1.15.0-*.whl
    • TensorFlow2.6.5:
      pip3 install tensorflow-2.6.5-*.whl
  9. 执行如下命令验证安装效果。
    python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

    如果返回了张量则表示安装成功。

搜索结果
找到“0”个结果

当前产品无相关内容

未找到相关内容,请尝试其他搜索词