Jit for tolist()

Hello everyone. I attempt to use torch.jit.script on the something like hyp_ctc_state_prev.tolist(), but it doesn’t work。I build the pytorch from source and the torch version is 1.4.0a0+2e7dd54。
I’d appreciate if anybody can help me! Or if there is a workable implementation, please let me know! Thanks in advance!
here is the log:

logs

RuntimeError: │ # will be (2 x beam) hyps at most
Tried to access nonexistent attribute or method ‘tolist’ of type ‘Tensor’.:
hyps_score = [hyp_score]
hyps_ctc_state_prev = [hyp_ctc_state_prev.tolist()]
~~~~~~~~~~~~~~~~~~~~~~~~~ <— HERE
hyps_ctc_score_prev = [hyp_ctc_score_prev]
ended_hyps_score = []

This is a known bug https://github.com/pytorch/pytorch/issues/26752

You can workaround by implementing it yourself, but it can be cumbersome due to the reasons explained in the bug report (plus the fact that TorchScript does not support generic functions or recursion):

def my_1d_tolist(x):
    result: List[float] = []
    for i in x:
        result.append(i.item())
    return result

@torch.jit.script
def my_2d_tolist(x):
    result: List[List[float]] = []
    for i in x:
        result.append(my_1d_tolist(i))
    return result

x = torch.ones(2, 2)

# These calls should be the same
print(x.tolist())
print(my_2d_tolist(x))

Thanks a lot! I implemented it by myself yesterday and I find that use c++ to load torchscript model only support the function name ‘forward’. If I change the function name ‘forward’ to ‘infer’,and use the decorator @torch.jit.export , transfer the model to torchscript.however, when i use c++ to load the model like torch::jit::script::Module module = torch::jit::load(‘model.pt’); and then use module.infer(inputs), the cpp file won’t be built. Do you know why it happen?

For methods other than forward you have to explicitly get the method and run it. For this Module

class M(nn.Module):
    @torch.jit.export
    def infer(self, x):
        return x + 10

torch.jit.script(M()).save("m.pt")

You can run it in C++ with script::Module::get_method

int main() {
  auto module = torch::jit::load("m.pt");
  auto result = module.get_method("infer")({torch::ones({2, 2})});
  std::cout << result << "\n";
}

We have an open issue to improve our C++ documentation to make things like this more clear in the future.

1 Like

Thanks a lot! it’s very helpful to me!