安装深度学习框架

若用户仅进行离线推理,请跳过此章节。

本文档以TensorFlow为例介绍安装深度学习框架的步骤,如果用户要使用MindSpore框架,请登录https://www.mindspore.cn/install获取安装MindSpore框架的方法。如果用户要使用PyTorch框架,请参见PyTorch安装指南

安装前准备

  1. 编译安装HDF5。

    1. 使用wget下载HDF5源码包,可以下载到安装环境的任意目录,命令为:
      wget https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-1.10/hdf5-1.10.5/src/hdf5-1.10.5.tar.gz --no-check-certificate
    2. 进入下载后的目录,解压源码包,命令为:
      tar -zxvf hdf5-1.10.5.tar.gz
      进入解压后的文件夹,执行配置、编译和安装命令:
      cd hdf5-1.10.5/
      ./configure --prefix=/usr/include/hdf5
      make
      make install   

  2. 配置环境变量并建立动态链接库软连接。

    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

  3. 安装h5py。

    root用户下安装h5py依赖包命令如下。
    pip3.7 install Cython

    安装h5py命令如下:

    pip3.7 install h5py==2.8.0

  4. 安装grpcio。

    root用户下安装grpcio命令如下。

    pip3.7 install grpcio==1.32.0

安装Tensorflow

需要安装Tensorflow才可以进行算子开发验证、训练业务开发。

  1. 下载“nsync-1.22.0.tar.gz”源码包。

    1. 进入tensorflow tag v1.15.0源码目录,打开“tensorflow/workspace.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 /tmp/nsync-1.22.0.tar.gz

    执行如上命令后得到sha256sum校验码(一串数字和字母的组合)

  5. 修改sha256sum校验码和urls。

    进入tensorflow tag v1.15.0源码目录,打开“tensorflow/workspace.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 = [
                "file:///tmp/nsync-1.22.0.tar.gz ",            "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",
            ],
        )

  6. 继续从官方的“配置build”(https://www.tensorflow.org/install/source)执行编译。

    执行完./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. 继续执行官方的编译指导步骤(https://www.tensorflow.org/install/source)即可。
  8. 安装编译好的Tensorflow。

    以上步骤执行完后会打包Tensorflow到指定目录,进入指定目录后root用户执行如下命令安装Tensorflow1.15:

    pip3.7 install tensorflow-1.15.0-cp37-cp37m-linux_aarch64.whl

    非root用户执行如下命令:

    pip3.7 install tensorflow-1.15.0-cp37-cp37m-linux_aarch64.whl --user