There are several methods to achieve this, but I think you need to decide on one and then add the required interface declarations. (I think it’s a DLL on Python, not a .so btw.)


If you want to do C++ and Python, the custom operator approach you mention is a nice way.
For this, too you would want to add a the registration part, this can look like the one in the custom op tutorial 2:
TORCH_LIBRARY(my_ops, m) {
   m.def("warp_perspective", warp_perspective);
}

This can be loaded with torch.ops.load_library.


If you want to call from Python: Write a torch extension module.
In this case you need a declaration like this one from the tutorial 1:
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
 m.def("forward", &lltm_forward, "LLTM forward");
 m.def("backward", &lltm_backward, "LLTM backward");
}

This seems to be missing in method 2 and the error messages is a cryptic way of telling you “this isn’t a complete python module”.


As an aside:

If you want to call from C++ only: you can skip the PYBIND11_MODULE  likely need to declare your symbols as exported. This is particularly important on windows. (__declspec(dllexport) in the exporting library, __declspec(dllimport) in the programs using it). This seems to be the problem of of no symbols find in method 1. You don’t need a main for libraries (I think).  I’m labelling this “C++ only” because I don’t know how well passing tensors via ctypes would work. You could try to go through DLPack if you wanted. I do use DLPack to get tensors to get third party C code (e.g. Apache TVM-generated kernels) in my courses, but even there I do the conversion in a custom PyTorch C++ extension module.

Best regards
Thomas