TorchScript segfault when using recursive custom classes

Hi,

Following the comments in this question, I was trying to create a class that has attributes of the same class type as shown below:

import torch
from typing import Dict, List, Optional


@torch.jit.script
class JSONValue(object):
    def __init__(self):
        self.value = torch.jit.annotate(Optional[JSONValue], None)

class TestModule(torch.jit.ScriptModule):
    @torch.jit.script_method
    def forward(self, input):
        # type: (JSONValue) -> JSONValue
        return input

m = TestModule()

m.save("test.pt")

But this code results in a segfault with message Segmentation fault: 11. Any thoughts on what could be wrong?

Thanks!

We do not actually currently support recursive class definitions. I didn’t remember that in the previous thread (will comment there as well). https://github.com/pytorch/pytorch/pull/21842 improves the error message in this case.

We will likely support it soon (before the next release) but for now it will not work.

1 Like