Hi, I am trying to make python wrapper for model execution in C++
I defined a class which loads torchscript model in the constructor and run a model by calling its method. code is as follows
– scripttest.cpp –
#include <torch/script.h>
#include <pybind11/pybind11.h>
#include <iostream>
#include <vector>
#include <torch/extension.h>
using namespace std;
namespace py = pybind11;
class Model{
private:
torch::jit::script::Module module;
torch::Tensor t1;
public:
Model(){
t1 = torch::randn({1,3,11,11}, torch::dtype(at::kFloat).device(c10::kCUDA));
module = torch::jit::load("test.pt");
}
at::Tensor get(){
at::GradMode::set_enabled(false);
at::Tensor t2 = module.forward({t1}).toTensor();
return t2;
}
};
PYBIND11_MODULE(scripttest, m){
py::class_<Model>(m, "Model")
.def(py::init<>())
.def("get", &Model::get);
}
— CMakeLists.txt —
cmake_minimum_required(VERSION 3.4...3.18)
set(CMAKE_CXX_STANDARD 14)
project(scripttest LANGUAGES CXX)
#include_directories("<libtorch/to/path>")
find_package(Torch REQUIRED)
find_package(pybind11 REQUIRED)
#add_library(scripttest)
pybind11_add_module(scripttest scripttest.cpp)
//find_library(TORCH_PYTHON_LIBRARY torch_python PATHS "{TORCH_INSTALL_PREFIX}/lib")
target_link_libraries(scripttest PRIVATE "${TORCH_LIBRARIES}" )//${TORCH_PYTHON_LIBRARY})
– python script –
from scripttest import Model
m = Model()
out = m.get()
It gives "ImportError: undefined symbol: _Z16THPVariable_WrapN2at6TensorE"
When I removed <torch/extension.h> in the scripttest.cpp, it says "TypeError: Unable to convert function return value to a Python type! The signature was (self: build.scripttest.Model) → at::Tensor"
When I uncommented CMakeLists.txt to use TORCH_PYTHON_LIBRARY, this returns Segmentation fault (core dumped) at m.get().
I downloaded 1.9.1+cu11.1 libtorch for ubuntu and 1.7.1 torch in anaconda.
Actually if I change get function not to return anything, then it does not raise any problem. But, I have to return torch result and also I should add torch tensor argument for get method later to replace random input tensor defined in get method.
Does anyone know how to solve it?
I read this thread(Can libtorch return torch::Tensor to pytorch with pybind11? - #3 by tom) and this tutorial(Custom C++ and CUDA Extensions — PyTorch Tutorials 1.9.1+cu102 documentation) but I want to use C++ class.