linux编译tensorflow C++

环境

  1. linux版本: Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-48-generic x86_64) (GPU服务器)
  2. 编译的tensorflow版本:r2.3

编译过程

  1. Anaconda创建虚拟环境
  2. github下载tensorflow的r2.3分支
  3. 安装bazel,bazel的版本有限制,太高太低都不行,我的是3.1.0
  4. cd tensorflow-r2.3
    ./configure
    安装CPU版本全选N
  5. 编译C++动态库: bazel build --config=opt //tensorflow:libtensorflow_cc.so 这个过程耗时很长,可以通过-j 16限制任务数
  6. 上一步完成后,在tensorflowr2.3目录下生成文件夹bazel-binbazel-bin/tensorflow/下生成动态链接库libtensorflow_cc.so、libtensorflow_cc.so.2、libtensorflow_cc.so.2.3.4 libtensorflow_cc.so.2.3.4-2.params、libtensorflow_framework.so.2、libtensorflow_framework.so.2.3.4、libtensorflow_framework.so.2.3.4-2.params
  7. 将这些编译结果放在/usr/local/lib文件夹下,再vim ~/.bashrc添加动态链接库的路径 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib 再执行source .bashrc
  8. 编译头文件,在tensorflowr2.3/目录下执行 bazel build --config=opt //tensorflow:install_headers
  9. 上一步完成后,在tensorflowr2.3/bazel-bin/tensorflow下出现include文件夹,将文件夹复制到usr/local/include

测试1

  1. 测试程序hello_tf.c
    #include <stdio.h>
    #include <tensorflow/c/c_api.h>
    
    int main() {
        printf("Hello from TensorFlow C library version %s\n", TF_Version());
        return 0;
    }
    
  2. 执行gcc hello_tf.c -o hello_tf -ltensorflow, 再./hello_tf,程序打印 Hello from TensorFlow C library version 2.3.0

测试2

  1. 使用cmake测试,编写源码文件main.cpp,如下
#include <iostream>
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"

using namespace tensorflow;
using namespace tensorflow::ops;

int main()
{
    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));
    std::cout << "tensorflow session run ok" << std::endl;
    // Expect outputs[0] == [19; -3]
    std::cout << outputs[0].matrix<float>();

    return 0;
}
  1. 编写规则CMakeLists.txt
project(libtf)
cmake_minimum_required(VERSION 3.0)

add_definitions(-std=c++11)

set(TENSORFLOW_ROOT_DIR /home/mhb/tensorflow-r2.3)

include_directories(
        ${TENSORFLOW_ROOT_DIR}/bazel-bin/tensorflow/include
)

aux_source_directory(./ DIR_SRCS)

link_directories(/home/mhb/tensorflow-r2.3/bazel-bin/tensorflow)

add_executable(libtf ${DIR_SRCS})
#target_link_libraries(libtf
#        tensorflow_cc
#        tensorflow_framework
#        )

target_link_libraries(libtf /home/mhb/tensorflow-r2.3/bazel-bin/tensorflow/libtensorflow_cc.so /home/mhb/tensorflow-r2.3/bazel-bin/tensorflow/libtensorflow_framework.so.2)
  1. 编译运行
mkdir build
cd build
cmake ..
make
./libtf
  1. 结果
2021-11-23 21:40:31.997535: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 2594120000 Hz
2021-11-23 21:40:31.999506: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55f7dd554200 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2021-11-23 21:40:31.999706: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
tensorflow session run ok
19

重要参考

https://blog.csdn.net/djstavaV/article/details/106290915 https://blog.csdn.net/guotianqing/article/details/114943892 https://blog.csdn.net/hh_2018/article/details/81185198


本文章使用limfx的vscode插件快速发布