I'm trying to create a python module using pybind11 to generate a torch tensor. I'm doing this in colaboratory. First, I get the libtorch library from wget https://download.pytorch.org/libtorch/cu102/libtorch-win-shared-with-deps-1.7.1.zip and unzip libtorch-win-shared-with-deps-1.7.1.zip. Also I do git clone https://github.com/pybind/pybind11.git.
I have two .cpp files. The first one is testLibtorch.cpp containing:
#include <torch/torch.h>
#include <torch/extension.h>
using torch::Tensor;
Tensor test(int num) {
Tensor tensor1 = torch::randn({4, 3});
return tensor1;
}
PYBIND11_MODULE(testLibtorch, m) {
m.doc() = "hello world"; // optional module docstring
m.def("test", &test, "A function giving torch tensor");
}
The second one is testExe.cpp containing:
#include <torch/torch.h>
#include <torch/extension.h>
#include <iostream>
int main(){
std::cout << torch::randn({4, 3}) << std::endl;
}
I'm writing my own CMakeLists.txt:
project(torchlibtest)
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 14)
include_directories(/content/libtorch)
set(CMAKE_PREFIX_PATH "/content/libtorch/share/cmake/Torch")
find_package(Torch REQUIRED)
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
add_subdirectory(pybind11)
pybind11_add_module(testLibtorch testLibtorch.cpp)
target_link_libraries(testLibtorch PRIVATE "${TORCH_LIBRARIES}")
target_link_libraries(testLibtorch ${MY_LIBRARIES})
set_property(TARGET testLibtorch PROPERTY CXX_STANDARD 14)
add_executable(testExe testExe.cpp)
target_link_libraries(testExe PRIVATE "${TORCH_LIBRARIES}")
if (MSVC)
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
add_custom_command(TARGET example-app
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${TORCH_DLLS}
$<TARGET_FILE_DIR:example-app>)
endif (MSVC)
Then I successfully cmake and make. However, when I try to import my module testLibtorch, I receive this error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-9-9855a0c0e93c> in <module>()
1 import torch
----> 2 import testLibtorch
3 print(testLibtorch.test(4))
ImportError: /content/build/testLibtorch.cpython-36m-x86_64-linux-gnu.so: undefined symbol: _Z16THPVariable_WrapN2at6TensorE
I doubt that I may fail to import class and functions from torch, but the executable from testExe.cpp seems to work fine. Will appreciate solutions to the problem, as well as recommendations and ideas for the task I'm trying to do.