Torch.jit.script

Hi, I’m trying to make a script for a model trained on cifar-10.

scripted_model = torch.jit.script(model, torch.rand(1,3,32,32))

I get the following error:
TypeError:

'numpy.int64' object in attribute 'Conv2d.in_channels' is not a valid constant.
Valid constants are:
1. a nn.ModuleList
2. a value of type {bool, float, int, str, NoneType, torch.device, torch.layout, torch.dtype}
3. a list or tuple of (2)

I also tried using torch.jit.ScriptModule instead of nn.Module in the class definition of the model but it didn’t work.

The error message claims you are using numpy.int64 to specify the in_channels argument in an nn.Conv2d layer as seen here:

# works
conv = nn.Conv2d(3, 3, 3, 1, 1)
torch.jit.script(conv)

# fails
conv = nn.Conv2d(np.int64(3), 3, 3, 1, 1)
torch.jit.script(conv)
> TypeError: 
 'numpy.int64' object in attribute 'Conv2d.in_channels' is not a valid constant.
  Valid constants are:
  1. a nn.ModuleList
  2. a value of type {bool, float, int, str, NoneType, torch.device, torch.layout, torch.dtype}
  3. a list or tuple of (2)

Pass the arguments as one of the mentioned supported types (e.g. as a Python int).

1 Like