ImportError: undefined symbol: _THError when cpp extention

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

sources = ['src/',  'src/']

if __name__ == '__main__':
    assert torch.cuda.is_available(), 'Please install CUDA for GPU support.'
    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/ 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?


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


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 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


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.