TorchScript: how can I access functions that were exported using @torch.jit.export?

When converting my model to TorchScript, I am using the decorator @torch.jit.export to mark some functions besides forward() to be exported by torch.jit.script().

When loading the TorchScript model in Python, I can indeed access these functions.

My question is regarding C++: since these functions are not included in the standard module interface, I need them to appear in a header file somewhere. Is such a header generated, and if not, how does one call the exported functions from C++?

Thank you!

You can see in torch/csrc/jit/api/module.h that torch::jit::Module::forward is implemented like so:

  IValue forward(std::vector<IValue> inputs) {
    return get_method("forward")(std::move(inputs));

You should be able to call exported functions like so:

auto mod = torch::jit::load(...);
auto exported_method = mod.get_method(<exported_method_name>)
auto result = exported_method(...);

Thanks. Yes, I just found an earlier question that was asked on this (it seems to be missing from docs at the moment). My function does not take any arguments, but it looks like you still need to pass in an empty vector of IValues. For future reference, here’s what I ended up doing:

    std::vector<torch::jit::IValue> stack;