I want to pass the traced module to C++ and get its input tensors’ shape. To do this, I use the following code :
# test.py
import torch
import custom_cpp_func
import torchvision.models as models

rn18 = models.resnet18()
traced_rn18 = torch.jit.trace(rn18, (torch.randn(5, 3, 224, 224),))
torch._C._jit_pass_inline(traced_rn18.graph)
custom_cpp_func.print_shape(traced_rn18._c)

// custom_op.cpp
int64_t print_shape(const torch::jit::Module &mod)
{
  mod.eval(); // Error here
  auto forward_method = mod.get_method("forward");
  assert(forward_method.graph());
  //...
  std::vector<torch::jit::IValue> new_inputs;
  new_inputs.push_back(torch::randn({5, 3, 224, 224}));

  const c10::IValue result = mod.forward(new_inputs).toTensor(); //Error here
}

The above code could not compile, it has the following error:
error: 'this' argument to member function 'eval' has type 'const torch::jit::Module', but function is not marked const
  mod.eval();
  ^~~
.../torch/csrc/jit/api/module.h:182:8: note: 'eval' declared here
  void eval() {

 error: 'this' argument to member function 'forward' has type 'const torch::jit::Module', but function is not marked const
  const c10::IValue result = mod.forward(new_inputs).toTensor();

torch/csrc/jit/api/module.h:111:10: note: 'forward' declared here
  IValue forward(std::vector<IValue> inputs) {

Could someone tell me what’s wrong with this? Why can’t I just pass the module as an argument ? Must I store the module and re-load it into the C++?
Secondly , how could I run the module to make sure the incomplete tensor could get correct shape information?