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 = []
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?