(原)ubuntu中C++调用libotrch

转载请注明出处:

https://www.cnblogs.com/darkknightzh/p/11479240.html

参考网址:

https://pytorch.org/tutorials/advanced/cpp_export.html

https://github.com/pytorch/pytorch/issues/15476#issuecomment-478293447

主要目的是在ubuntu中使用C++调用libotrch,从而调用pytorch训练得到的模型。

cuda 10.0

python环境:anaconda,python3.6.8

1. 按照官方教程https://pytorch.org/tutorials/advanced/cpp_export.html编写对应文件

CMakeLists.txt和example-app.cpp

CMakeLists.txt:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(custom_ops)

find_package(Torch REQUIRED)

add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 11)

example-app.cpp:

#include <torch/script.h> // One-stop header.

#include <iostream>
#include <memory>

int main(int argc, const char *argv[])
{
    if (argc != 2)
    {
        std::cerr << "usage: example-app <path-to-exported-script-module>\n";
        return -1;
    }

    torch::jit::script::Module module;
    try
    {
        // Deserialize the ScriptModule from a file using torch::jit::load().
        module = torch::jit::load(argv[1]);
    }
    catch (const c10::Error &e)
    {
        std::cerr << "error loading the model\n";
        return -1;
    }

    std::cout << "ok\n";
}

2. 生成makefile,编译:

mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=/path/to/libtorch ..
make

3. 运行程序:

./example-app <path_to_model>/traced_resnet_model.pt

输出ok,证明成功了。

 

理论上应该成功,然而实际上。。。

1. 修改后的cmake命令(修改2后,此处可以不修改)

cmake -DCMAKE_PREFIX_PATH=/home/xxx/libtorch/libtorch -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-10.0 -DCUDA_NVCC_EXECUTABLE=/usr/local/cuda-10.0/bin -DCUDA_INCLUDE_DIRS=/usr/local/cuda-10.0/include ..

2. 按照https://github.com/pytorch/pytorch/issues/15476#issuecomment-478293447,修改cuda库的相关路径,修改后的libtorch/libtorch/share/cmake/Caffe2/Caffe2Targets.cmake:

set_target_properties(torch PROPERTIES
  INTERFACE_COMPILE_DEFINITIONS "_THP_CORE;AT_PARALLEL_OPENMP=1"
  INTERFACE_COMPILE_OPTIONS "-Wall;-Wextra;-Wno-unused-parameter;-Wno-missing-field-initializers;-Wno-write-strings;-Wno-unknown-pragmas;-Wno-missing-braces;-fopenmp"
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include;${_IMPORT_PREFIX}/include"
  INTERFACE_LINK_LIBRARIES "protobuf::libprotobuf;c10;Threads::Threads;caffe2::mkl;caffe2::mkldnn;torch::cudart;c10_cuda;/usr/local/cuda-10.0/targets/x86_64-linux/lib/libnvToolsExt.so;/usr/local/cuda-10.0/targets/x86_64-linux/lib/libcudart.so;caffe2::cufft;caffe2::curand;caffe2::cudnn;/usr/local/cuda-10.0/targets/x86_64-linux/lib/libculibos.a;dl;/usr/local/cuda-10.0/targets/x86_64-linux/lib/libculibos.a;caffe2::cublas"
)

说明:实际上只需要修改2就可以了。修改之后,可以编译成功。如果修改了1,不修改2,编译时还是找不到cuda相关的那些库(原因是,cuda10的这些库不在默认的路径下,导致编译失败。。。)

 

posted on 2019-09-07 09:35  darkknightzh  阅读(1263)  评论(0编辑  收藏  举报

导航