Search code examples
python-decoratorstorchtorchscript

Why can't I use classes for type annotation of the arguments to a function under the `torch.jit.script` decorator?


This code compiles just fine:

import torch
import torch.nn             as nn

class Foo(nn.Module):
    def __init__(self):
        super(Foo, self).__init__()
        self.x = 0

    def forward(self, X):
        X      *= self.x
        self.x += 1
        return X

# @torch.jit.script
def bar(f: Foo):
    return f.x

But, if I uncomment the # @torch.jit.script line, I get this error:

Traceback (most recent call last):
  File "test1.py", line 18, in <module>
    def bar(f: Foo):
  File "/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/jit/__init__.py", line 1103, in script
    fn = torch._C._jit_script_compile(qualified_name, ast, _rcb, get_default_args(obj))
RuntimeError:
Unknown type name 'Foo':
at test1.py:18:12
@torch.jit.script
def bar(f: Foo):
           ~~~ <--- HERE
    return f.x

If I change the type annotation to int:

@torch.jit.script
# def bar(f: Foo):
#     return f.x
def bar(f: int):
    return f

then compilation works again.

Does anyone know what I need to do, to allow my custom class definitions to be used in type annotations to arguments of functions that lie under a torch.jit.script decorator?


Solution

  • Only the types list in the docs here can be used as arguments to functions:

    https://pytorch.org/docs/stable/jit_language_reference.html#supported-type

    nn.Modules have some special handling in TorchScript to make them work, but they aren't currently supported as arguments.