ImportError: undefined symbol: _THError when cpp extention

I build a cpp extension of PyTorch and it is successfully compiled and installed.

sources = ['src/my_spatialConv.cu',  'src/my_im2col.cu']

if __name__ == '__main__':
    assert torch.cuda.is_available(), 'Please install CUDA for GPU support.'
    setup(
    name='my_conv',
    ext_modules=[CUDAExtension('my_conv', sources)],
    cmdclass={'build_ext': BuildExtension}
    )

However, when I import it via

python -c "import my_conv"

An error appears:

ImportError: /usr/local/lib/python3.6/dist-packages/my_conv-0.0.0-py3.6-linux-x86_64.egg/my_conv.cpython-36m-x86_64-linux-gnu.so: undefined symbol: _THError

The following headers are included in my sources:

#include <TH/TH.h>
#include <THC/THC.h>
#include <ATen/div_rtn.h>

I work on torch-1.1.0, cuda-10.0, and ubuntu-16.04.
What are the probable causes of this error?

Hi,

it would seem expected that

python -c "import my_conv"

does not work, as you need to load PyTorch itself and its symbols before you load the extension, so python -c "import torch, my_conv" would likely work much better.
If you wanted to avoid that for some reason, you could explicitly link against libtorch (extra_ldflags=['-ltorch'] or somesuch).

Best regards

Thomas

Thanks. import torch, my_conv works.
But when I run it

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: dynamic module does not define module export function (PyInit_my_conv)

It seems I need to write function warppers via Pybind11. But I don’t know how to warp a cuda function like this:

void f(THCTensor *input, THCTensor *output, THCTensor *weight, THCTensor *bias);

I have refered https://github.com/pytorch/extension-cpp. But it is not suitable for my case.
Is there solutions of my problem?

I think you need a rather to upgrade to from legacy THC to modern PyTorch (I think this has been looming for 2 years or so…). We don’t really do THCTensor* anymore, in fact there is an effort going on to migrate all these old bits internally as well.

Best regards

Thomas

Thanks.
Based on your advice, I am considering to replace THCTensor with at::Tensor.

torch::Tensor even. :slight_smile: But you’ll likely also need to adapt what is inside the functions. It is a bit of work, but your code will look much better afterwards.