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):
    super().__init__()
    self.linear = nn.Linear(16, 16)


m = TestModule()
print(m.linear.in_features)

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/__init__.py", line 1197, in __getattr__
    return ScriptModule.__getattr__(self, attr)
  File "/home/qbx2/anaconda3/lib/python3.7/site-packages/torch/jit/__init__.py", line 1102, in __getattr__
    return Module.__getattr__(self, attr)
  File "/home/qbx2/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", 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/__init__.py", line 1200, in __getattr__
    return getattr(self.__dict__["_original"](), attr)
AttributeError: 'NoneType' object has no attribute 'in_features'

Looking at the code (https://github.com/pytorch/pytorch/blob/master/torch/jit/init.py#L1226), 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: https://github.com/pytorch/pytorch/issues/19363

1 Like