Torch::save and torch::load compilation errors - no overloaded operator definition

I’ve successfully created a model using libTorch and visual studio 2019 (compiler version: 19.29.30141).

However, when I try to save it via torch::save(model, “model.pt”):

torch::save(model, “model.pt”);

model is of type Net:

struct Net : torch::nn::Module {}

I get the following visual studio compilation error:

|Error|C2679|binary ‘<<’: no operator found which takes a right-hand operand of type ‘const Value’ (or there is no acceptable conversion)

When I dig into this I find the error is actually in serialise.h:

template <typename Value, typename… SaveToArgs>
void save(const Value& value, SaveToArgs&&… args) {
serialize::OutputArchive archive(
std::make_sharedjit::CompilationUnit());
archive << value;
archive.save_to(std::forward(args)…);
}*

The issue is the overloading of the “<<” operator (archive << value).

When I look further I find OutputArchive (which is a closed class) has no overload definition for “<<” so this will never build. The documentation suggests that the overload can be defined elsewhere but this is a final class (closed).

Is this a bug in libTorch or am I missing something - which is the usual case (sorry).

Any help much appreciated.

The solution is here from mhubi (builds but I haven’t tested it):

The key here is inclusion of TORCH_MODULE(Net). See example.