Problem using libtorch to bind C++ modules to Python


I needed to bind C++ torch modules to python, but could not find any detailed documentation anywhere.
So I turned to docstring in code, specifically in torch/python.h, and followed minimal example provided in the comments for simple identity module.

Minimal example below


cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(example VERSION 1.0.0 LANGUAGES C CXX CUDA)


find_package(Python COMPONENTS Interpreter Development REQUIRED)
find_package(pybind11 CONFIG REQUIRED)
find_package(Torch REQUIRED)

pybind11_add_module(pybind_example tmp.cpp)

target_link_libraries(pybind_example PRIVATE ${TORCH_LIBRARIES})


#include <torch/extension.h>
#include <pybind11/pybind11.h>

namespace py = pybind11;

struct Net : torch::nn::Module {
    Net(int in, int out) { }
    torch::Tensor forward(torch::Tensor x) { return x; }

PYBIND11_MODULE(my_module, m)
    torch::python::bind_module<Net>(m, "Net")
        .def(py::init<int, int>())
        .def("forward", &Net::forward);

Code compiles and creates shared library
Problem occurs on import of created module with error

ImportError: /home/user/tmp/build/ undefined symbol: THPDtypeType

Am I missing a linking step? THPDtypeType is defined in torch/csrc/Dtype.h, but I don’t see where it is invoked.

Thank you in advance for any help / ideas.