I am using libtorch in c++.
In python, torch.jit.trace function exists and works fine.
However, torch::jit::trace does not work in libtorch.
It said there is no "trace" member in namespace torch::jit.
Here is my simple example code.
#include <torch/torch.h>
#include <torch/script.h>
#include <iostream>
struct MyNetImpl : torch::nn::Module {
torch::nn::Linear fc{ nullptr };
MyNetImpl(int in_features, int out_features) {
fc = register_module("fc", torch::nn::Linear(in_features, out_features));
}
torch::Tensor forward(torch::Tensor x) {
return torch::relu(fc->forward(x));
}
};
TORCH_MODULE(MyNet);
int main() {
MyNet model(10, 2);
model->eval();
// Create an example input
auto example_input = torch::randn({ 1, 10 });
// NOTE: torch::jit::trace is a function:
auto traced_model = torch::jit::trace(model, example_input);
// Save TorchScript module
traced_model.save("model_traced.pt");
std::cout << "Traced model saved successfully!\n";
return 0;
}
My code is wrong or torch::jit::trace is not available in libtorch?
What i was trying to do is to save model in c++ and load it in python.