Issue
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?
Solution
I eventually found I needed to use the Anaconda version of torchlib rather than my own, as well as using Torch's CppExtension. Here is my working setup.py:
from distutils.core import setup, Extension
from torch.utils.cpp_extension import BuildExtension, CppExtension
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.ext_modules.append(CppExtension(
name='reson8',
sources=['reson8.cpp'],
extra_info=info,
extra_compile_args=['-g', '-D_GLIBCXX_USE_CXX11_ABI=0'],
extra_ldflags=['-ltorch_python'],
include_dirs=["/home/ian/anaconda3/lib/python3.7/site-packages/pybind11/include",
"/home/ian/anaconda3/lib/python3.8/site-packages/pybind11/include",
"/home/ian/anaconda3/lib"
]
))
return config
if __name__ == "__main__":
from numpy.distutils.core import setup
setup(configuration=configuration)
Answered By - Omroth
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.