Unable to do very much with libTorch in MSVC

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.

I downloaded and now building against the 1.10.0 LibTorch package (CPU).

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

"Exception thrown: read access violation.
this->_Vec._Mypair._Myval2._Myfirst was 0x111011101110111."

This is also the case with sample code from the pytorch example repository.

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

/// Calls the forward() method of the contained module.
template <typename… Args>
auto operator()(Args&&… args)
→ torch::detail::return_type_of_forward_t<Contained, Args…> {
// This will not compile if the module does not have a forward() method
// (as expected).
// NOTE: std::forward is qualified to prevent VS2017 emitting
// error C2872: ‘std’: ambiguous symbol
return impl_->forward(::std::forward(args)…);
}

MSVC Error:

“Unhandled exception at 0x00007FF8563D4F69 in Torch.exe: Microsoft C++ exception: c10::Error at memory location 0x000000252078DDA0.”

At this point I’m not sure what to do. Any help would be of great assistance. Thanks.

Having the same error struct Net : torch::nn::Module {
Net(int64_t input_size, int64_t hidden_size, int64_t hidden_size2, int64_t hidden_size3, int64_t output_size);

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

};

#include “net.h”

Net::Net(int64_t input_size, int64_t hidden_size, int64_t hidden_size2, int64_t hidden_size3, int64_t output_size) {
i2h = register_module(“i2h”, torch::nn::Linear(input_size, hidden_size));
h2h = register_module(“h2h”, torch::nn::Linear(hidden_size, hidden_size2));
h2h2 = register_module(“h2h2”, torch::nn::Linear(hidden_size2, hidden_size3));
h2h3 = register_module(“h2h3”, torch::nn::Linear(hidden_size3, hidden_size3));
h2o = register_module(“h2o”, torch::nn::Linear(hidden_size3, output_size));
}

torch::Tensor Net::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->forward(xi));
torch::Tensor xm2 = torch::relu(h2h->forward(xm));
torch::Tensor xm3 = torch::relu(h2h2->forward(xm2));
torch::Tensor xo = torch::relu(h2h3->forward(xm3));
torch::Tensor x = torch::relu(h2o->forward(xo));

return x;

}

The moment it hits register_module the same error comes up . This is basically build after Frontend Doc

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

Okay tried it differently :
Version libtorch-win-shared-with-deps-2.0.1+cu117
struct MyNetImpl : torch::nn::Module {
MyNetImpl(int64_t input_size, int64_t hidden_size, int64_t hidden_size2, int64_t hidden_size3, int64_t output_size):
i2h(register_module(“i2h”, torch::nn::Linear(input_size, hidden_size))),
h2h(register_module(“h2h”, torch::nn::Linear(hidden_size, hidden_size2))),
h2h2(register_module(“h2h2”, torch::nn::Linear(hidden_size2, hidden_size3))),
h2h3(register_module(“h2h3”, torch::nn::Linear(hidden_size3, hidden_size3))),
h2o(register_module(“h2o”, torch::nn::Linear(hidden_size3, output_size))) {}

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->forward(xi));
    torch::Tensor xm2 = torch::relu(h2h->forward(xm));
    torch::Tensor xm3 = torch::relu(h2h2->forward(xm2));
    torch::Tensor xo = torch::relu(h2h3->forward(xm3));
    torch::Tensor x = torch::relu(h2o->forward(xo));

    return x;
}


torch::nn::Linear i2h,h2h, h2h2, h2h3, h2o;

};
TORCH_MODULE(MyNet);

I load with:
//before
// auto net = std::make_shared(3,50,50,50,1);
auto net = MyNet(3,50,50,50,1);

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->release either . I have release-> release yet to test because my project settings got all mushy.

If this intended somebody should overwork the cmake files so that only valid project settings are generated and not like default all