# Jit for tolist()

**URL:** <https://discuss.pytorch.org/t/jit-for-tolist/60926>\
**Category:** jit\
**Created:** [November 14, 2019, 3:37am UTC](https://discuss.pytorch.org/t/jit-for-tolist/60926 "2019-11-14T03:37:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![huoge](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/h/c77e96/32.png) [@huoge](https://discuss.pytorch.org/u/huoge)\
**Post date:** [November 14, 2019, 3:37am UTC](https://discuss.pytorch.org/t/jit-for-tolist/60926/1 "2019-11-14T03:37:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![driazati](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/driazati/32/15393_2.png) [@driazati](https://discuss.pytorch.org/u/driazati)\
**Post date:** [November 14, 2019, 9:59pm UTC](https://discuss.pytorch.org/t/jit-for-tolist/60926/2 "2019-11-14T21:59:29Z")

</div>

This is a known bug [https://github.com/pytorch/pytorch/issues/26752](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):

```python
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))

```

---

<div class="post-metadata">

**Author:** ![huoge](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/h/c77e96/32.png) [@huoge](https://discuss.pytorch.org/u/huoge)\
**Post date:** [November 15, 2019, 2:42am UTC](https://discuss.pytorch.org/t/jit-for-tolist/60926/3 "2019-11-15T02:42:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![driazati](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/driazati/32/15393_2.png) [@driazati](https://discuss.pytorch.org/u/driazati)\
**Post date:** [November 15, 2019, 9:05pm UTC](https://discuss.pytorch.org/t/jit-for-tolist/60926/4 "2019-11-15T21:05:32Z")

</div>

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

```python
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`

```cpp
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.

---

<div class="post-metadata">

**Author:** ![huoge](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/h/c77e96/32.png) [@huoge](https://discuss.pytorch.org/u/huoge)\
**Post date:** [November 18, 2019, 1:49am UTC](https://discuss.pytorch.org/t/jit-for-tolist/60926/5 "2019-11-18T01:49:25Z")

</div>

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