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;

Hey I’m trying to check my torchscript model from the python REPL. When I load a jit model with torch.jit.load(), that model doesn’t seem to have a get_method function.

If I’m using python, what is the alternative to get_method

I seem to have answered my own question.

I’m supposed to use model.__getattr__(func_name)(args)

I think you can also access it like any other attribute of a Python object.