(原)ubuntu中C++调用libotrch

转载请注明出处:html

http://www.javashuo.com/article/p-sjyqerka-eb.htmlpython

参考网址:linux

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

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

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

cuda 10.0ubuntu

python环境:anaconda,python3.6.8app

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

CMakeLists.txt和example-app.cppspa

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的这些库不在默认的路径下,致使编译失败。。。)

相关文章
相关标签/搜索