ImportError: undefined symbol: _THError when cpp extention

username: zzd1992
cooked: <p>I build a cpp extension of PyTorch and it is successfully compiled and installed.</p>
<pre><code class="lang-auto">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}
    )
</code></pre>
<p>However, when I import it via</p>
<pre><code class="lang-auto">python -c "import my_conv"
</code></pre>
<p>An error appears:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>The following headers are included in my sources:</p>
<pre><code class="lang-auto">#include &lt;TH/TH.h&gt;
#include &lt;THC/THC.h&gt;
#include &lt;ATen/div_rtn.h&gt;
</code></pre>
<p>I work on torch-1.1.0, cuda-10.0, and ubuntu-16.04.<br>
What are the probable causes of this error?</p>

username: tom
cooked: <p>Hi,</p>
<p>it would seem expected that</p>
<pre><code class="lang-auto">python -c "import my_conv"
</code></pre>
<p>does not work, as you need to load PyTorch itself and its symbols before you load the extension, so <code>python -c "import torch, my_conv"</code> would likely work much better.<br>
If you wanted to avoid that for some reason, you could explicitly link against libtorch (<code>extra_ldflags=['-ltorch']</code> or somesuch).</p>
<p>Best regards</p>
<p>Thomas</p>

username: zzd1992
cooked: <p>Thanks. <code>import torch, my_conv</code> works.<br>
But when I run it</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "&lt;string&gt;", line 1, in &lt;module&gt;
ImportError: dynamic module does not define module export function (PyInit_my_conv)
</code></pre>
<p>It seems I need to write function warppers via Pybind11. But I don’t know how to warp a cuda function like this:</p>
<pre><code class="lang-auto">void f(THCTensor *input, THCTensor *output, THCTensor *weight, THCTensor *bias);
</code></pre>
<p>I have refered <a href="https://github.com/pytorch/extension-cpp" rel="nofollow noopener">https://github.com/pytorch/extension-cpp</a>. But it is not suitable for my case.<br>
Is there solutions of my problem?</p>

username: tom
cooked: <p>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.</p>
<p>Best regards</p>
<p>Thomas</p>

username: zzd1992
cooked: <p>Thanks.<br>
Based on your advice, I am considering to replace <code>THCTensor</code> with <code>at::Tensor</code>.</p>

username: tom
cooked: <p>torch::Tensor even. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":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.</p>