[C++ exception]: c10::Error at memory location

jb892: <p>Hi,</p>
<p>I’m trying to convert my network trained in Tensorflow to libtorch for inference. However, I run into this c++ exception:</p>
<pre><code class="lang-auto">Unhandled exception at 0x00007FFCA0F0A839 in Inference.exe: Microsoft C++ exception: c10::Error at memory location 0x000000E75FCFCFC0. 
</code></pre>
<p>Here is a piece of code for testing:</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;

using namespace torch;

struct ConvNet : nn::Module
{
    ConvNet()
        :conv1(nn::Conv2dOptions(2, 10, { 1 , 1 }).stride(1).padding(1).with_bias(true))
    {
        register_module("Conv1", conv1);
    }

    Tensor forward(Tensor input)
    {
        auto x = conv1-&gt;forward(input);  // &lt;--- where exception happens.
        return x;
    }

    nn::Conv2d conv1{nullptr};
};

int main()
{
    Tensor input = torch::randn({ 2, 3, 3, 2 });
    input = input.cuda();
    std::cout &lt;&lt; input &lt;&lt; std::endl;

    std::shared_ptr&lt;ConvNet&gt; mNet = std::make_shared&lt;ConvNet&gt;();
    std::cout &lt;&lt; mNet-&gt;forward(input) &lt;&lt; std::endl;
    
    return 0;
}
</code></pre>
<p>Why did this happen? Could anyone help? Thx in advance!</p>

jb892: <p>Additional info:<br>
OS: Windows 10<br>
Libtorch: 1.3.1<br>
CUDA: 10.1<br>
cudnn: 7.6.5<br>
VS: 2017</p>
<p>The memory layout in my code is NHWC. I just found that it only support NCHW format in libtorch. So, the updated code:</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;

using namespace torch;

struct ConvNet : nn::Module
{
    ConvNet()
        :conv1(nn::Conv2dOptions(2, 10, { 1 , 1 }).stride(1).padding(0).with_bias(true))
    {
        register_module("Conv1", conv1);
    }

    Tensor forward(Tensor input)
    {
        auto x = conv1-&gt;forward(input);  // &lt;--- where exception happens.
        return x;
    }

    nn::Conv2d conv1{nullptr};
};

int main()
{
    Tensor input = torch::randn({ 2, 2, 3, 3 });
    input = input.cuda();
    std::cout &lt;&lt; input &lt;&lt; std::endl;

    std::shared_ptr&lt;ConvNet&gt; mNet = std::make_shared&lt;ConvNet&gt;();
    std::cout &lt;&lt; mNet-&gt;forward(input) &lt;&lt; std::endl;
    
    return 0;
}
</code></pre>
<p>The exception still exists.</p>

jb892: <p>After some research and debugging work, I just figure out what’s wrong. Basically I forgot to make my network cuda enable. Just add one line of code and it works fine now. Source code:</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;

using namespace torch;

struct ConvNet : nn::Module
{
    ConvNet()
        :conv1(nn::Conv2dOptions(2, 10, { 1 , 1 }).stride(1).padding(0).with_bias(true))
    {
        register_module("Conv1", conv1);
    }

    Tensor forward(Tensor input)
    {
        auto x = conv1-&gt;forward(input);  // &lt;--- where exception happens.
        return x;
    }

    nn::Conv2d conv1{nullptr};
};

int main()
{
    Tensor input = torch::randn({ 2, 2, 3, 3 });
    input = input.cuda();
    std::cout &lt;&lt; input &lt;&lt; std::endl;

    std::shared_ptr&lt;ConvNet&gt; mNet = std::make_shared&lt;ConvNet&gt;();
    mNet-&gt;to(kCUDA);
    std::cout &lt;&lt; mNet-&gt;forward(input) &lt;&lt; std::endl;
    
    return 0;
}
</code></pre>