Cannot access nn.Linear.in_features in ScriptModule

import torch.nn as nn
import torch.jit as jit

class TestModule(jit.ScriptModule):
  def __init__(self):
    self.linear = nn.Linear(16, 16)

m = TestModule()

The code above throws AttributeError

>>> m.linear.in_features
Traceback (most recent call last):
  File "/home/qbx2/anaconda3/lib/python3.7/site-packages/torch/jit/", line 1197, in __getattr__
    return ScriptModule.__getattr__(self, attr)
  File "/home/qbx2/anaconda3/lib/python3.7/site-packages/torch/jit/", line 1102, in __getattr__
    return Module.__getattr__(self, attr)
  File "/home/qbx2/anaconda3/lib/python3.7/site-packages/torch/nn/modules/", line 535, in __getattr__
    type(self).__name__, name))
AttributeError: 'WeakScriptModuleProxy' object has no attribute 'in_features'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/qbx2/anaconda3/lib/python3.7/site-packages/torch/jit/", line 1200, in __getattr__
    return getattr(self.__dict__["_original"](), attr)
AttributeError: 'NoneType' object has no attribute 'in_features'

Looking at the code (, I think WeakScriptModuleProxy should have copied in_features and other fields to self here.

I’m just posting this issue here to check if any workaround for this already exists.

Thank you.

When nn modules are used in a ScriptModule, they get wrapped in an internal class that only copies the nn module’s buffers, submodules, and parameters. So as a workaround you could do something like self.linear.weight.shape[1] to get the in_features.

Really we should be copying everything though, could you file an issue on GitHub with the same example code you posted here?

Yes, opened an issue:

1 Like