Ubuntu 20.04下编译Tensorflow 1.15 C++

TensorFlow的1.15版本已经不受官方支持了,因此编译C++版本的TensorFlow十分困难。并且网上相关资料很少,本人在做了多次尝试后走了很多弯路。在成功完成编译后,特记录一下:

编译NVIDIA维护的TensorFlow 1.15

目前由NVIDIA维护的TensorFlow 1.15是目前唯一长期支持的版本,能够在Ubuntu20.04下支持CUDA11.0。因此编译的目标不是官方的TensorFlow,而是NVIDIA维护的版本。

github地址:https://github.com/NVIDIA/tensorflow,本文编译的仓库分支为 r1.15.5+nv22.04

然后完成以下步骤:

  1. 特别提醒:编译需要 CUDA, CUDNN 和 NCCL 库,这部分的按照请参考NVIDIA的教程,这里不再赘述。
  2. 按照github的说明完成 Build From Source 标签下的 Fetch sources and install build dependencies,安装依赖项。
  3. 继续完成 Configure TensorFLow 部分,注意这部分的配置要根据自己电脑的实际情况来。
  4. 不要执行 Build and install TensorFlow 部分,因为这部分是编译Python程序的命令,C++并不需要。用以下命令代替,该命令会编译C++的.so库,其中:local_cpu_resources是编译使用的CPU数量,因为编译需要很长时间,因此请结合自己电脑情况考虑。编译大概需要7G存储空间以及1
bazel build -c opt --config=cuda --config=monolithic  //tensorflow:libtensorflow_cc.so

完成编译后看到的现象

在这里插入图片描述
编译完成后,可以看到在TensorFlow源文件夹下的bazel-bin/tensorflow中出现libtensorflow_cc.so即为编译成功。

编译测试

现在,写一个简单的Demo来测试,注意修改CMakeList.txt中的 set(Tensorflow_Root “/home/llm/src/tensorflow”) 的地址为你电脑上的实际地址


example.cpp

#include <tensorflow/core/platform/env.h>
#include <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";
}

CMakeList.txt

set(PROJECT_NAME tf_tutorial)
cmake_minimum_required (VERSION 2.8.8)
project (${PROJECT_NAME})

set(CMAKE_CXX_FLAGS "-std=c++11")

set(Tensorflow_Root "/home/llm/src/tensorflow")
set(Tensorflow_INCLUDE_DIRS
    ${Tensorflow_Root}
    ${Tensorflow_Root}/bazel-genfiles
    ${Tensorflow_Root}/bazel-bin/tensorflow
    ${Tensorflow_Root}/bazel-tensorflow/external/eigen_archive
    ${Tensorflow_Root}/bazel-tensorflow/external/com_google_protobuf/src
    ${Tensorflow_Root}/bazel-tensorflow/external/com_google_absl
    ${Protobuf_INCLUDE_DIRS}
)
set(Tensorflow_LIBS
    ${Tensorflow_Root}/bazel-bin/tensorflow/libtensorflow_cc.so)

include_directories(
    ${Tensorflow_INCLUDE_DIRS})

add_executable(example example.cpp)
# Link the Tensorflow library.
target_link_libraries(example ${Tensorflow_LIBS})


执行结果:

llm@llm-ubuntu-20:~/tensorflow-test/build$ ./example 
2022-05-27 19:16:40.382173: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
2022-05-27 19:16:40.409403: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcuda.so.1
2022-05-27 19:16:40.442120: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1082] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-05-27 19:16:40.442470: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1666] Found device 0 with properties: 
name: NVIDIA GeForce RTX 2070 with Max-Q Design major: 7 minor: 5 memoryClockRate(GHz): 1.125
pciBusID: 0000:01:00.0
2022-05-27 19:16:40.442485: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
2022-05-27 19:16:40.445263: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcublas.so.11
2022-05-27 19:16:40.446646: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcufft.so.10
2022-05-27 19:16:40.446817: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcurand.so.10
2022-05-27 19:16:40.447169: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcusolver.so.11
2022-05-27 19:16:40.447800: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcusparse.so.11
2022-05-27 19:16:40.447914: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudnn.so.8
2022-05-27 19:16:40.447970: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1082] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-05-27 19:16:40.448329: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1082] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-05-27 19:16:40.448632: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1794] Adding visible gpu devices: 0
2022-05-27 19:16:40.767612: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1206] Device interconnect StreamExecutor with strength 1 edge matrix:
2022-05-27 19:16:40.767637: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1212]      0 
2022-05-27 19:16:40.767642: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1225] 0:   N 
2022-05-27 19:16:40.767739: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1082] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-05-27 19:16:40.768070: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1082] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-05-27 19:16:40.768380: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1082] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-05-27 19:16:40.768681: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1351] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6250 MB memory) -> physical GPU (device: 0, name: NVIDIA GeForce RTX 2070 with Max-Q Design, pci bus id: 0000:01:00.0, compute capability: 7.5)
2022-05-27 19:16:40.781083: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55dc7ad205c0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2022-05-27 19:16:40.781102: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): NVIDIA GeForce RTX 2070 with Max-Q Design, Compute Capability 7.5
2022-05-27 19:16:40.799479: I tensorflow/core/platform/profile_utils/cpu_utils.cc:109] CPU Frequency: 2599990000 Hz
2022-05-27 19:16:40.800181: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55dc7ad20250 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2022-05-27 19:16:40.800211: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
Session successfully created.
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值