Problem loading C++ torch script oeprator from python

Hi folks,

following the tutorial at 1, I am trying to implement a simple C++ torch script operator. I’ve installed libtorch 2 and managed to compile my module. However, when I am trying to load the operator via

import torch
torch.ops.load_library("/home/jviereck/dev/tools/torch_pinocchio/build/libtorch_pinocchio.so")

this is the error I am getting:

$ python2 ../test/basic.py 
Traceback (most recent call last):
  File "../test/basic.py", line 2, in <module>
    torch.ops.load_library("/home/jviereck/dev/tools/torch_pinocchio/build/libtorch_pinocchio.so")
  File "/home/jviereck/.local/lib/python2.7/site-packages/torch/_ops.py", line 102, in load_library
    ctypes.CDLL(path)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 362, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /home/jviereck/dev/tools/torch_pinocchio/build/libtorch_pinocchio.so: undefined symbol: _ZN6caffe26detail37_typeMetaDataInstance_preallocated_29E

Note that I’ve installed the torch via pip install torch.

My best guess is that the python torch is not aware about the libraries in the libtorch folder. Does someone has an idea how to resolve this?

Best,
Julian

Not sure if that’s related to your problem, but similar issues originated in multiple PyTorch installatons (or some old, unused installs).
Could you check, if your current environment has some old PyTorch installs (in conda, pip, and build from source)?

My guess is that either

  • Your compiler version is not the same as the one that compiled the caffe2 library, in particular differing in its name mangling (mapping C++ namespace+symbol+signature to a unique name in an address table)
  • The function you were using somewhere in your extension (caffe2::typeMetaDataInstance_preallocated) is not exported by the libraries you linked against.

What you can do is check whether you are getting the same error when compiling with the same compiler as was used to compile Caffe2.