Saturday, 23 January 2021

How do I link in all of PyTorch with a pybind11 .so?

I have a pybind11 c++ project which uses the pytorch c++ api:

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <math.h>
#include <torch/torch.h>

...

void f()
{
...
   torch::Tensor dynamic_parameters = torch::full({1}, /*value=*/0.5, torch::dtype(torch::kFloat64).requires_grad(true));
   torch::optim::SGD optimizer({dynamic_parameters}, /*lr=*/0.01);
...
}

PYBIND11_MODULE(reson8, m)
{
    m.def("my_function", &my_function, "");
}

I use distutils to compile this into a .so that can be imported in Python:

from distutils.core import setup, Extension

def configuration(parent_package='', top_path=None):
      import numpy
      from numpy.distutils.misc_util import Configuration
      from numpy.distutils.misc_util import get_info

      #Necessary for the half-float d-type.
      info = get_info('npymath')

      config = Configuration('',
                             parent_package,
                             top_path)
      config.add_extension('reson8',
                           ['reson8.cpp'],
                           extra_info=info,
                           include_dirs=["/home/ian/anaconda3/lib/python3.7/site-packages/pybind11/include",
                                          "/home/ian/anaconda3/lib/python3.8/site-packages/pybind11/include",
                                          "/home/ian/dev/hedgey/Engine/lib/libtorch/include",
                                          "/home/ian/dev/hedgey/Engine/lib/libtorch/include/torch/csrc/api/include"])

      return config


if __name__ == "__main__":
      from numpy.distutils.core import setup
      setup(configuration=configuration)

It compiles with no error, but on running "import reson8" in python I get this error:

importerror: undefined symbol: _ZTVN5torch5optim9OptimizerE

I'm unsure if it's whether pytorch hasn't been linked into my so (although the .so is 10mb which is rather large if it doesn't include pytorch, but maybe all pybind11 .so files are large.)

How can I solve this issue?



from How do I link in all of PyTorch with a pybind11 .so?

No comments:

Post a Comment