Unable to do very much with libTorch in MSVC

cdcdcd: <p>I am trying to build a simple network in Visual Studio but hitting run time issues. My VS version is 2019 Comminuty, compiling with v14.2 on Windows 10. I am currently working with a simple VS CMake project.</p>
<p>I downloaded and now building against the 1.10.0 LibTorch package (CPU).</p>
<p>If I try to instantiate an object (either struct or class) that inherits from torch::nn::Module and register modules I get the following error when I try to run my code (it builds ok):</p>
<p><em>"Exception thrown: read access violation.</em><br>
<em>this-&gt;_Vec._Mypair._Myval2._Myfirst was 0x111011101110111."</em></p>
<p>This is also the case with sample code from the pytorch example repository.</p>
<p>If I try to initialise objects of type Conv2d without registering them I get a different error (torch tries to capture an issue for MSVC):</p>
<blockquote>
<p>/// Calls the <code>forward()</code> method of the contained module.<br>
template &lt;typename… Args&gt;<br>
auto operator()(Args&amp;&amp;… args)<br>
 → torch::detail::return_type_of_forward_t&lt;Contained, Args…&gt; {<br>
// This will not compile if the module does not have a <code>forward()</code> method<br>
// (as expected).<br>
// NOTE: <code>std::forward</code> is qualified to prevent VS2017 emitting<br>
// error C2872: ‘std’: ambiguous symbol<br>
return impl_-&gt;forward(::std::forward(args)…);<br>
}</p>
</blockquote>
<p>MSVC Error:</p>
<p><em>“Unhandled exception at 0x00007FF8563D4F69 in Torch.exe: Microsoft C++ exception: c10::Error at memory location 0x000000252078DDA0.”</em></p>
<p>At this point I’m not sure what to do. Any help would be of great assistance. Thanks.</p>

QFuser: <p>Having the same error struct Net : torch::nn::Module {<br>
Net(int64_t input_size, int64_t hidden_size, int64_t hidden_size2, int64_t hidden_size3, int64_t output_size);</p>
<pre><code>torch::Tensor forward(torch::Tensor x1, torch::Tensor x2, torch::Tensor x3);

torch::nn::Linear i2h{ nullptr };
torch::nn::Linear h2h{ nullptr };
torch::nn::Linear h2h2{ nullptr };
torch::nn::Linear h2h3{ nullptr };
torch::nn::Linear h2o{ nullptr };
</code></pre>
<p>};</p>
<p><span class="hashtag-raw">#include</span> “net.h”</p>
<p>Net::Net(int64_t input_size, int64_t hidden_size, int64_t hidden_size2, int64_t hidden_size3, int64_t output_size) {<br>
i2h = register_module(“i2h”, torch::nn::Linear(input_size, hidden_size));<br>
h2h = register_module(“h2h”, torch::nn::Linear(hidden_size, hidden_size2));<br>
h2h2 = register_module(“h2h2”, torch::nn::Linear(hidden_size2, hidden_size3));<br>
h2h3 = register_module(“h2h3”, torch::nn::Linear(hidden_size3, hidden_size3));<br>
h2o = register_module(“h2o”, torch::nn::Linear(hidden_size3, output_size));<br>
}</p>
<p>torch::Tensor Net::forward(torch::Tensor x1, torch::Tensor x2, torch::Tensor x3) {<br>
torch::Tensor xi = torch::cat({ x1, x2, x3 }, 1);</p>
<pre><code>torch::Tensor xm = torch::relu(i2h-&gt;forward(xi));
torch::Tensor xm2 = torch::relu(h2h-&gt;forward(xm));
torch::Tensor xm3 = torch::relu(h2h2-&gt;forward(xm2));
torch::Tensor xo = torch::relu(h2h3-&gt;forward(xm3));
torch::Tensor x = torch::relu(h2o-&gt;forward(xo));

return x;
</code></pre>
<p>}</p>
<p>The moment it hits register_module the same error comes up . This is basically build after <a href="https://pytorch.org/cppdocs/frontend.html" rel="noopener nofollow ugc">Frontend Doc</a></p>
<p>I know there is also another way to do this I remember seeing something like register the whole struct or something in the GitHub examples. […]Okay it just too different</p>

QFuser: <p>Okay tried it differently :<br>
Version libtorch-win-shared-with-deps-2.0.1+cu117<br>
struct MyNetImpl : torch::nn::Module {<br>
MyNetImpl(int64_t input_size, int64_t hidden_size, int64_t hidden_size2, int64_t hidden_size3, int64_t output_size):<br>
i2h(register_module(“i2h”, torch::nn::Linear(input_size, hidden_size))),<br>
h2h(register_module(“h2h”, torch::nn::Linear(hidden_size, hidden_size2))),<br>
h2h2(register_module(“h2h2”, torch::nn::Linear(hidden_size2, hidden_size3))),<br>
h2h3(register_module(“h2h3”, torch::nn::Linear(hidden_size3, hidden_size3))),<br>
h2o(register_module(“h2o”, torch::nn::Linear(hidden_size3, output_size))) {}</p>
<pre><code>torch::Tensor forward(torch::Tensor x1, torch::Tensor x2, torch::Tensor x3) {
    torch::Tensor xi = torch::cat({ x1, x2, x3 }, 1);

    torch::Tensor xm = torch::relu(i2h-&gt;forward(xi));
    torch::Tensor xm2 = torch::relu(h2h-&gt;forward(xm));
    torch::Tensor xm3 = torch::relu(h2h2-&gt;forward(xm2));
    torch::Tensor xo = torch::relu(h2h3-&gt;forward(xm3));
    torch::Tensor x = torch::relu(h2o-&gt;forward(xo));

    return x;
}


torch::nn::Linear i2h,h2h, h2h2, h2h3, h2o;
</code></pre>
<p>};<br>
TORCH_MODULE(MyNet);</p>
<p>I load with:<br>
//before<br>
// auto net = std::make_shared(3,50,50,50,1);<br>
auto net = MyNet(3,50,50,50,1);</p>

QFuser: <p>I was busy but the problem is your can’t use debug settings in VS with the release version of the PyTorch c++ frontend . From what I read in an even older post you can’t to do debug-&gt;release either . I have release-&gt; release yet to test because my project settings got all mushy.</p>
<p>If this intended somebody should overwork the cmake files so that only valid project settings are generated and not like default all</p>