Getting Unknown type annotation error when JIT saving pytorch model
When JIT saving “model.pt” of a complex pytorch model with many custom classes, I am encountering the error that pytorch doesn’t know the type annotation of one of those custom classes. In other words, the following code (drastically summarized from original) fails on the seventh line:
import torch
from gan import Generator
from gan.blocks import SpadeBlock

generator = Generator()
generator.load_weights(&quot;path/to/weigts&quot;)
jitted = torch.jit.script(generator)
torch.jit.save(jitted, &quot;model.pt&quot;)

Error:
Traceback (most recent call last):
  File &quot;pth2onnx.py&quot;, line 72, in &lt;module&gt;
    to_torch_jit(generator)
  File &quot;pth2onnx.py&quot;, line 24, in to_torch_jit
    jitted = torch.jit.script(generator)
  File &quot;/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/__init__.py&quot;, line 1516, in script
    return torch.jit._recursive.create_script_module(obj, torch.jit._recursive.infer_methods_to_compile)
  File &quot;/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py&quot;, line 310, in create_script_module
    concrete_type = concrete_type_store.get_or_create_concrete_type(nn_module)
  File &quot;/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py&quot;, line 269, in get_or_create_concrete_type
    concrete_type_builder = infer_concrete_type_builder(nn_module)
  File &quot;/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py&quot;, line 138, in infer_concrete_type_builder
    sub_concrete_type = concrete_type_store.get_or_create_concrete_type(item)
  File &quot;/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py&quot;, line 269, in get_or_create_concrete_type
    concrete_type_builder = infer_concrete_type_builder(nn_module)
  File &quot;/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py&quot;, line 138, in infer_concrete_type_builder
    sub_concrete_type = concrete_type_store.get_or_create_concrete_type(item)
  File &quot;/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py&quot;, line 269, in get_or_create_concrete_type
    concrete_type_builder = infer_concrete_type_builder(nn_module)
  File &quot;/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py&quot;, line 126, in infer_concrete_type_builder
    attr_type = infer_type(name, item)
  File &quot;/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py&quot;, line 99, in infer_type
    attr_type = torch.jit.annotations.ann_to_type(class_annotations[name], _jit_internal.fake_range())
  File &quot;/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/annotations.py&quot;, line 303, in ann_to_type
    raise ValueError(&quot;Unknown type annotation: '{}'&quot;.format(ann))
ValueError: Unknown type annotation: '&lt;class 'gan.blocks.SpadeBlock'&gt;'

The type it complains about is indeed a class we ourselves have programmed and used in the loaded Generator. I would appreciate pointers on what could cause this or how to investigate this!
I tried the following:

explicitly importing SpadeBlock in the script that calls torch.jit.script
ensured it inherits from nn.Module (as does Generator)
ensured the gan package is installed, using pip install --user -e 

Any ideas? Thanks in advance!

The issue turned out to be the that I was using class variable names which got mangled. Example:
class Generator(nn.Module):
    __main: nn.Module

The two leading underscores are the cause. Changing them to a single or no underscore. resolves the issue.
class Generator(nn.Module):
    main: nn.Module

