Core dumped when returning torch::Tensor from cpp_extension

username: haridas_n
cooked: <p>I’m facing this problem in my code, when returning the torch::Tensor type.</p>
<p>To reproduce the problem, I have setup the simple extension available as part of the pytorch test code available here - <a href="https://github.com/pytorch/pytorch/blob/master/test/cpp_extensions/extension.cpp" rel="nofollow noopener">https://github.com/pytorch/pytorch/blob/master/test/cpp_extensions/extension.cpp</a></p>
<h2>My environment</h2>
<p>Python 3.6.9<br>
OS: Ubuntu 18.04.3 LTS<br>
g++: 7.5.0<br>
torch: 1.6.0</p>
<p>Simple cpp extension, has the following code,</p>
<pre><code class="lang-cpp">#include &lt;torch/extension.h&gt;

// test include_dirs in setuptools.setup with relative path
// #include &lt;tmp.h&gt;

torch::Tensor sigmoid_add(torch::Tensor x, torch::Tensor y)
{
    return x.sigmoid() + y.sigmoid();
}

struct MatrixMultiplier
{
    MatrixMultiplier(int A, int B)
    {
        tensor_ =
            torch::ones({A, B}, torch::dtype(torch::kFloat64).requires_grad(true));
        test = "Hello";
    }
    torch::Tensor forward(torch::Tensor weights)
    {
        return tensor_.mm(weights);
    }
    torch::Tensor get() const
    {
        return tensor_;
    }

    std::string getstr() const
    {
        return test;
    }

private:
    torch::Tensor tensor_;
    std::string test;
};

bool function_taking_optional(c10::optional&lt;torch::Tensor&gt; tensor)
{
    return tensor.has_value();
}

PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)
{
    m.def("sigmoid_add", &amp;sigmoid_add, "sigmoid(x) + sigmoid(y)");
    m.def(
        "function_taking_optional",
        &amp;function_taking_optional,
        "function_taking_optional");
    py::class_&lt;MatrixMultiplier&gt;(m, "MatrixMultiplier")
        .def(py::init&lt;int, int&gt;())
        .def("forward", &amp;MatrixMultiplier::forward)
        .def("get", &amp;MatrixMultiplier::get)
        .def("getstr", &amp;MatrixMultiplier::getstr);
}
</code></pre>
<h2>setup.py file</h2>
<pre><code class="lang-python">from setuptools import setup, Extension
from torch.utils import cpp_extension

setup(name="test_ext",
    ext_modules=[cpp_extension.CppExtension(
        name='detr',
        sources=['test_module.cpp']
        )
    ],
    cmdclass={'build_ext': cpp_extension.BuildExtension}
)
</code></pre>
<h2>tests.py file:</h2>
<pre><code class="lang-python">
import test_ext
import unittest

class TestLib(unittest.TestCase):
    def test_matrix_mal(self):
        mm = test_ext.MatrixMultiplier(3, 4)
        mm.get()


traceback error:-

[New Thread 0x7fff943f2700 (LWP 1625)]
[New Thread 0x7fff93bf1700 (LWP 1626)]
[New Thread 0x7fff8d3f0700 (LWP 1628)]

Thread 1 "python" received signal SIGSEGV, Segmentation fault.
0x00007fffe550b76a in THPVariable_NewWithVar(_typeobject*, at::Tensor) () from /mnt1/projects/venv/lib/python3.6/site-packages/torch/lib/libtorch_python.so
</code></pre>
<p>Accessing the binding after building and installation step:</p>
<pre><code class="lang-bash">pip uninstall -y test_ext &amp;&amp; python setup.py install --verbose &amp;&amp; python -m unittest tests.py
</code></pre>
<p>Any idea on this ?, I was trying on the nightly version also, seems it’s not related to torch, may be python version or other thing.</p>

username: haridas_n
cooked: <p>The problem here was, I wasn’t importing the <code>pytorch</code> library before the extension. ie;</p>
<pre><code class="lang-python">import torch
import test_ext
import unittest

class TestLib(unittest.TestCase):
    def test_matrix_mal(self):
        mm = test_ext.MatrixMultiplier(3, 4)
        mm.get()
</code></pre>
<p><code>import torch</code> has to be done before importing the custom binding.</p>
<p>I was expecting to bind it without the dependency from pytorch, and use only the dynamic libraries that are linked with the my extension and standard python dependencies. Seems I was wrong here, pytorch has to be loaded into python interpreter.</p>