You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ScriptModule is not able to have a tensor as an attribute
To Reproduce
Not Constant
import torch
class ConstantTensor(torch.jit.ScriptModule):
def __init__(self):
self.cx = torch.ones(100, 100, dtype=torch.float, device='cuda')
@torch.jit.script_method
def forward(self, x):
return x + self.cx
c = ConstantTensor()
print c.graph
attribute 'cx' of type 'Tensor' is not usable in a script method (did you forget to add it __constants__?):
Constant
import torch
class ConstantTensor(torch.jit.ScriptModule):
__constants__ = ['cx']
def __init__(self):
self.cx = torch.ones(100, 100, dtype=torch.float, device='cuda')
@torch.jit.script_method
def forward(self, x):
return x + self.cx
c = ConstantTensor()
print c.graph
TypeError: 'Tensor' object for attribute 'cx' is not a valid constant.
Valid constants are:
1. a nn.ModuleList
2. a value of type {bool, float, int, str, NoneType, function, device, layout, dtype}
3. a list or tuple of (2)
UPDATE:
Seems like the right way to use a tensor attribute is to declare it to be a buffer
sidazhang
changed the title
ScriptModule is unable to have a tensor as attribute (constant or not)
[JIT] ScriptModule is unable to have a tensor as attribute (constant or not)
Jan 23, 2019
In this case, could you improve the error messaging so it is explicit?
sidazhang
changed the title
[JIT] ScriptModule is unable to have a tensor as attribute (constant or not)
[JIT] Improve error messaging for using a tensor attribute in ScriptModule
Jan 24, 2019
馃悰 Bug
ScriptModule is not able to have a tensor as an attribute
To Reproduce
Not Constant
Constant
UPDATE:
Seems like the right way to use a tensor attribute is to declare it to be a buffer
self.register_buffer('cx', torch.ones(100, 100, dtype=torch.float, device='cuda'))
Please improve the error message when a user tries to use a tensor as an attribute
The text was updated successfully, but these errors were encountered: