在华为云鲲鹏服务器上的部署tensorflow c++ api
本文介绍鲲鹏服务器安装Tensorflow的c++库。
1 安装准备
华为云购买一台鲲鹏服务器
本文以云服务器KC1实例搭建,云服务器配置如表1-1所示。
表1-1 云服务器配置
项目 |
说明 |
规格 |
kc1.large.2 | 8vCPUs | 16GB |
磁盘 |
系统盘:高IO(40GB) |
操作系统要求如表1-2所示。
表1-2 操作系统要求
项目 |
说明 |
下载地址 |
CentOS |
18.04 |
在公共镜像中已提供。 |
Kernel |
4.15.0 |
在公共镜像中已提供。 |
2 配置编译环境
1) 安装依赖包。
apt-get install autoconf automake libtool curl make g++ unzip # Protobuf Dependencies apt-get install zlib1g-dev zlib1g python3-numpy python3-pip zip python-numpy swig python-dev python-wheel python3-h5py #TensorFlow Dependencies
2) JDK
apt-get --purge remove openjdk-8-jdk openjdk-8-jdk-headless openjdk-8-jre openjdk-8-jre-headless -y
cd /usr/local/src
wget https://repo.huaweicloud.cn/java/jdk/8u151-b12/jdk-8u151-linux-arm64-vfp-hflt.tar.gz
tar -zxvf jdk-8u151-linux-arm64-vfp-hflt.tar.gz
vim /etc/profile
source /etc/profile
3) 安装Bazel
bazel是Google开源的一套编译构建工具,广泛应用于Google内部,包括TensorFlow项目。通过源码安装Tnesorflow需要使用bazel来编译。bazel主要有构建快、支持增量编译。对依赖关系进行优化支持并发执行。
a) 下载Bazel源码包
cd /home/ wget https://github.com/bazelbuild/bazel/releases/download/0.21.0/bazel-0.21.0-dist.zip
b) 编译和安装Bazel
mkdir bazel cd bazel unzip ../bazel-0.21.0-dist.zip ./compile.sh
c) 配置Bazel 环境变量
vi /etc/profile
增加如下命令,/home/bazel/output即Bazel可执行程序所在目录
export PATH=$PATH:/home/bazel/output
使得环境变量生效
source /etc/profile
d) 检验Bazel是否安装成功
bazel info
3 获取源码
cd /home/ wget https://github.com/tensorflow/tensorflow/archive/v1.13.1.tar.gz
4 编译Tensorflow
4) 解压软件包。
tar -zxvf tensorflow-1.13.1.tar.gz cd tensorflow-1.13.1/
5) 配置Tensorflow
root@ecs-tensorflow:/home/tensorflow-1.13.1# ./configure
Extracting Bazel installation...
WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown"
INFO: Invocation ID: 85323fa4-fb98-4bfb-b72f-9819dd1c7bb2
You have bazel 0.21.0- (@non-git) installed.
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3
Found possible Python library paths:
/usr/lib/python3/dist-packages
/usr/local/lib/python3.6/dist-packages
Please input the desired Python library path to use. Default is [/usr/lib/python3/dist-packages]
Do you wish to build TensorFlow with XLA JIT support? [Y/n]: n
No XLA JIT support will be enabled for TensorFlow.
Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: N
No OpenCL SYCL support will be enabled for TensorFlow.
Do you wish to build TensorFlow with ROCm support? [y/N]:
No ROCm support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]: N
No CUDA support will be enabled for TensorFlow.
Do you wish to download a fresh release of clang? (Experimental) [y/N]: N
Clang will not be downloaded.
Do you wish to build TensorFlow with MPI support? [y/N]: N
No MPI support will be enabled for TensorFlow.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=
native -Wno-sign-compare]:
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: N
Not configuring the WORKSPACE for Android builds.
6) 安装clang
cd /home/tensorflow-1.13.1/ apt-get install clang-8 clang-8++
由于gcc 自身与Tnesorflow兼容性问题,导致gcc无法编译tensorflow 1.13.1,所以考虑采用clang来编译。
备注:Clang 采用的是 BSD 协议的许可证,而 GCC 采用的是 GPL 协议,前者更为宽松;Clang 是一个高度模块化开发的轻量级编译器,编译速度快、占用内存小、有着友好的出错提示。
7) 编译Tensorflow
设置Clang环境变量
export CXX=clang++-8 export CC=clang-8
编译Tensorflow命令如下:
bazel build --config=noaws //tensorflow/tools/pip_package:build_pip_package --verbose_failures --copt=-funsafe-math-optimizations --copt=-march=armv8-a --copt=-mtune=cortex-a53
5 编译Tensorflow c++ api
1) 安装依赖
./tensorflow/contrib/makefile/build_all_linux.sh
2) 编译生成tensorflow:libtensorflow_cc.so
--如果会使用到OpenCV,编译命令需要加入--config=monolithic,本例加入此参数命令。
--加入--config=noaws以解决如下此类错误
undefined reference to 'Aws::Environment::GetEnv[abi:cxx11](char const*)'
bazel build tensorflow:libtensorflow_cc.so --config=monolithic --config=noaws
3) 拷贝安装
a) 拷贝库
cp bazel-bin/tensorflow/libtensorflow_cc.so /usr/local/lib
cp bazel-bin/tensorflow/libtensorflow_framework.so /usr/local/lib
b) 复制源文件到 /usr/local/include/google
mkdir -p /usr/local/include/google/tensorflow
cp -r tensorflow /usr/local/include/google/tensorflow/
find /usr/local/include/google/tensorflow/tensorflow -type f ! -name "*.h" -delete
c) 复制bazel-genfiles文件夹中所有生成的文件:
cp bazel-genfiles/tensorflow/core/framework/*.h /usr/local/include/google/tensorflow/tensorflow/core/framework
cp bazel-genfiles/tensorflow/core/lib/core/*.h /usr/local/include/google/tensorflow/tensorflow/core/lib/core
cp bazel-genfiles/tensorflow/core/protobuf/*.h /usr/local/include/google/tensorflow/tensorflow/core/protobuf
cp bazel-genfiles/tensorflow/core/util/*.h /usr/local/include/google/tensorflow/tensorflow/core/util
cp bazel-genfiles/tensorflow/cc/ops/*.h /usr/local/include/google/tensorflow/tensorflow/cc/ops
d) 复制 third_party 文件夹:
cp -r third_party /usr/local/include/google/tensorflow/
rm -r /usr/local/include/google/tensorflow/third_party/py
4) 配置环境变量
考虑到安装过程中,即上一步骤复制相应的头文件到安装目录不全,将/home/tensorflow-1.13.1也加入环境变量中。
vim /etc/profile
source /etc/profile
6 运行和验证
1) 编辑例子。
Cd /home
vi test.c
内容如下:
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"
int main()
{
using namespace tensorflow;
using namespace tensorflow::ops;
Scope root = Scope::NewRootScope();
// Matrix A = [3 2; -1 0]
auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
// Vector b = [3 5]
auto b = Const(root, { {3.f, 5.f} });
// v = Ab^T
auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
std::vector<Tensor> outputs;
ClientSession session(root);
// Run and fetch v
TF_CHECK_OK(session.Run({v}, &outputs));
// Expect outputs[0] == [19; -3]
LOG(INFO) << outputs[0].matrix<float>();
return 0;
}
2) 编译运行
g++ test.c –ltensorflow_cc -ltensorflow_framework
./a.out
3) 如果编译报如下错误,则参考此步骤解决。
错误:fatal error: unsupported/Eigen/CXX11/Tensor: No such file or directory
解决:安装eigen
cd /usr/local/src wget https://gitlab.com/libeigen/eigen/-/archive/3.3.7/eigen-3.3.7.tar.gz tar -zxvf eigen-3.3.7.tar.gz cd eigen-3.3.7 mkdir build cd build cmake .. make -j12 && make install
- 点赞
- 收藏
- 关注作者
评论(0)