当前位置:   article > 正文

ubuntu tensorflow源码编译C++库_tensorflow c++ 编译

tensorflow c++ 编译

vcpkg

C++中用frugally-deep调用keras的模型并进行预测
添加链接描述

CUDA10.0支持的最高版本为ubuntu18.04,CUDA11.1.0开始有WSL-Ubuntu

查询版本
在这里插入图片描述
我的版本:
ubuntu 18.04
cuda 10.0
https://developer.nvidia.com/cuda-toolkit-archive
cudnn 7.4.2
https://developer.nvidia.com/rdp/cudnn-archive
bazel 0.19.2
https://github.com/bazelbuild/bazel
tensorflow 1.13.1
https://github.com/tensorflow/tensorflow
protobuf 3.6
https://github.com/protocolbuffers/protobuf

wsl2编译c++
Tensorflow c++源码编译(踩坑版)

前提

1.下载驱动
https://www.nvidia.cn/Download/index.aspx?lang=cn#
测试:nvidia-smi
2.下载cuda
https://developer.nvidia.com/cuda-toolkit-archive
测试:nvcc -V
3.下载cudnn
https://developer.nvidia.com/rdp/cudnn-archive
测试:cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
或 cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

一、bazel

安装bazel

https://bazel.build/install/compile-source?hl=zh-cn

chmod +x bazel--installer-linux-x86_64.sh
./bazel--installer-linux-x86_64.sh --user
vim ~/.bashrc
export PATH=“ P A T H : PATH: PATH:HOME/bin”
source ~/.bashrc
测试:bazel --version

bazel删除

sudo rm -rf /usr/bin/bazel
sudo rm -rf ~/bin
sudo rm -rf /usr/bin/bazel

二、protobuf

安装protobuf

需要先安装 sudo apt-get install build-essential
./autogen.sh
./configure
make -j8
make check -j8
sudo make install
sudo vim /etc/profile
export LD_LIBRARY_PATH=/usr/local/lib
source /etc/profile
sudo ldconfig # 执行动态链接库管理命令,让可共享的动态链接库被找到
测试:protoc --version

错误

./autogen.sh: 38: ./autogen.sh: autoreconf: not found
解决方案:
sudo apt-get install autoconf automake libtool
sudo apt-get install automake
sudo apt-get install libtool

卸载protobuf

cd protobuf-3.6.1.2/
make uninstall

删除protobuf

1.删除执行文件
sudo rm -rf /usr/bin/protoc
2.删除所有protobuf 的链接库和头文件
sudo rm -rf /usr/include/google/protobuf #头文件
sudo rm -rf /usr/local/include/google/protobuf #头文件
sudo rm -rf /usr/lib/libproto* #库文件
sudo rm -rf /usr/local/lib/libproto* #库文件

三、编译tensorflow

./configure

错误

nvcc fatal : Unsupported gpu architecture ‘compute_89’
不要默认,CUDA 10.0对应的显卡算力范围是从3.0到7.5
Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 8.9]: 7.5
选项
在这里插入图片描述

2.bazel编译
如果不需要cuda版,则输入 bazel build --config=opt //tensorflow:libtensorflow_cc.so
如果需要cuda版,则输入 bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so
这个过程需要全程保持联网(科学上网),会在编译的过程中联网下载很多依赖,时间较长,耐心等待
/tensorflow-1.13.0/bazel-bin/tensorflow/
会出现libtensorflow_cc.so以及libtensorflow_framework.so等文件
编译成功后,tensorflow-1.13.1目录下会出现 bazel-xxx 的几个文件,在tensorflow-1.13.1/bazel-bin/tensorflow文件下会出现 libtensorflow_cc.so 和 libtensorflow_framework.so 动态库文件。

重新编译
bazel clean
bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so

编译过程中出现的错误
错误1

x86_64-linux-gnu-gcc-11: fatal error: cannot execute ‘cc1plus’: execvp: No such file or directory
解决方法:
sudo apt-get update
sudo apt-get install --reinstall build-essential

错误2

Server terminated abruptly (error code: 14, error message: ‘Socket closed’, log file: ‘/home/cw/.cache/bazel/_bazel_cw/adb056c93effb4e59f521b0ce0ed0b80/server/jvm.out’)在这里插入图片描述
https://www.cnblogs.com/HelloCodeWrold/p/12967920.html
https://blog.51cto.com/zhangsz0516/6103347
https://blog.csdn.net/weixin_44958188/article/details/126893265
https://zhuanlan.zhihu.com/p/573352835

解决方法:
1.查看swap的大小
sudo swapon --show 或 sudo free -h
2.暂停swap
sudo swapoff -a
3.修改swap大小
sudo dd if=/dev/zero of=/swapfile bs=1G count=32 或 sudo fallocate -l 20G /swapfile
4.修改文件的权限
sudo chmod 600 /swapfile
5.将该交换文件更改为交换空间
sudo mkswap /swapfile
6.开机挂载 swap
sudo vim /etc/fstab
‘/swapfile none swap sw 0 0’
7.启动Swap
sudo swapon /swapfile 或 sudo swapon -a

错误3

gcc版本
安装gcc-9
./configure 选择 /usr/bin/gcc-9
sudo apt install gcc-9 g+±9
在这里插入图片描述

错误4

/home/cw/tensorflow-1.13.1/tensorflow/core/kernels/BUILD:4680:1: no such package ‘@icu//’: java.io.IOException: Error downloading [https://mirror.bazel.build/github.com/unicode-org/icu/archive/release-62-1.tar.gz, https://github.com/unicode-org/icu/archive/release-62-1.tar.gz] to /home/cw/.cache/bazel/_bazel_cw/f878ebe08e74da0c4869e3f2293c00b8/external/icu/release-62-1.tar.gz: Checksum was 86b85fbf1b251d7a658de86ce5a0c8f34151027cc60b01e1b76f167379acf181 but wanted e15ffd84606323cbad5515bf9ecdf8061cc3bf80fb883b9e6aa162e485aa9761 and referenced by ‘//tensorflow/core/kernels:string_util’

解决方案:
本地下载:https://link.zhihu.com/?target=https%3A//codeload.github.com/unicode-org/icu/tar.gz/release-62-1
安装apache2:sudo apt-get install apache2
安装apache2-dev sudo apt-get install apache2-dev
安装完成后 查看是否安装成功:sudo systemctl status apache2
sudo cp release-62-1.tar.gz /var/www/html
“http://localhost/release-62-1.tar.gz”
sha256 = “86b85fbf1b251d7a658de86ce5a0c8f34151027cc60b01e1b76f167379acf181”,

错误5

/usr/bin/env: ‘python’: No such file or directory
解决方案:sudo ln -s /usr/bin/python3 /usr/bin/python

错误6

添加链接描述
添加链接描述
error: #error This file was generated by a newer version of protoc which is~~~
编译的protobuf版本不对,重新安装protobuf,再bazel clean 后重新编译tensorflow。

四、编译其他依赖

1.cd ~/tensorflow-1.13.1/tensorflow/contrib/makefile/
2…/build_all_linux.sh
执行成功后,在tensorflow-1.13.1/tensorflow/contrib/makefile目录下:downloads文件夹下存放第三方依赖的一些头文件和静态库,比如absl cub double_conversion eigen fft2d gemmlowp googletest nsync re2。这些第三方库是可以自己安装的

错误:

eigen下载路径失效
https://bitbucket.org/eigen/eigen/get/9f48e814419e.tar.gz
修改download_dependencies.sh,注释下载eigen的命令
解决方法:手动下载并编译,下载链接:https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.tar.gz
cd eigen
mkdir build
cd build
cmake …
sudo make install 默认路径为/usr/local/include

需要的头文件的路径
~/tensorflow-1.13.1/tensorflow/cc/ops
~/tensorflow-1.13.1/tensorflow/core/framework
~/tensorflow-1.13.1/tensorflow/core/graph
~/tensorflow-1.13.1/tensorflow/core/platform
~/tensorflow-1.13.1/tensorflow/core/public
.so文件
~/tensorflow-1.13.1/bazel-bin/tensorflow
其他库
~/tensorflow-1.13.1/tensorflow/contrib/makefile/downloads

五、测试

test.cpp

#include <tf/tensorflow/core/platform/env.h>
#include <tf/tensorflow/core/public/session.h>
#include <iostream>
using namespace std;
using namespace tensorflow;
int main()
{
    Session* session;
    Status status = NewSession(SessionOptions(), &session);
    if (!status.ok()) {
        cout << status.ToString() << "\n";
        return 1;
    }
    cout << "Session successfully created.\n";
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

CMakeLists.txt
#指定 cmake 的最小版本
cmake_minimum_required(VERSION 2.8.8)
#项目名称/工程名
project(demo)
#设置c++编译器
#Set C++14 as standard for the whole project
set(CMAKE_CXX_STANDARD 14)
#设置TENSORFLOW_DIR变量,变量内容为安装的tensorflow文件夹路径
set(TENSORFLOW_DIR /home/cw/tensorflow-1.13.1)
#将源码目录保存到变量中
aux_source_directory(./src DIR_SRCS) # 搜索当前目录下的所有.cpp文件
#add_library(demo ${SRC_LIST}) #明确指定包含哪些源文件
#设置包含的目录,项目中的include路径,换成自己的路径即可
include_directories(
KaTeX parse error: Expected 'EOF', got '#' at position 297: …e/eigen3 ) #̲设置链接库搜索目录,项目中li…{TENSORFLOW_DIR}/tensorflow/contrib/makefile/downloads/nsync/builds/default.linux.c++11)
link_directories(${TENSORFLOW_DIR}~/tensorflow-1.13.1/bazel-bin/tensorflow) #动态链接库目录
#添加要编译的可执行文件
#add_executable(demo test.cpp)
add_executable(demo ${DIR_SRCS}) ## 生成可执行文件
#设置 target 需要链接的库
#添加可执行文件所需要的库,连接libtensorflow_cc.so和libtensorflow_framework库,链接动态链接库
#target_link_libraries(demo tensorflow_cc tensorflow_framework)
target_link_libraries(demo ~/tensorflow-1.13.1/bazel-bin/tensorflow/libtensorflow_cc.so ~/tensorflow-1.13.1/bazel-bin/tensorflow/libtensorflow_framework.so)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/899819
推荐阅读
相关标签
  

闽ICP备14008679号