I’m using the SSD-ResNet34
model from mlcommons inference repo, the model can be downloaded here.
And I wrote a python script to jit.script the model:
import torch
m = torch.load('/path/to/resnet34-ssd1200.pytorch')
scripted = torch.jit.script(m)
However, the code won’t work, complaining Module 'Conv2d' has no attribute '_reversed_padding_repeated_twice'
.
Here’s the full stack trace:
>>> import torch
>>> m = torch.load('/root/mlcommons/model/resnet34-ssd1200.pytorch' ,map_location=lambda storage, loc: storage)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:671: SourceChangeWarning: source code of class 'models.ssd_r34.SSD_R34' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:671: SourceChangeWarning: source code of class 'torch.nn.modules.batchnorm.BatchNorm2d' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:671: SourceChangeWarning: source code of class 'torch.nn.modules.activation.ReLU' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:671: SourceChangeWarning: source code of class 'torch.nn.modules.pooling.MaxPool2d' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
warnings.warn(msg, SourceChangeWarning)
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:671: SourceChangeWarning: source code of class 'torchvision.models.resnet.BasicBlock' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
warnings.warn(msg, SourceChangeWarning)
>>> traced = torch.jit.script(m)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_script.py", line 1097, in script
obj, torch.jit._recursive.infer_methods_to_compile
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_recursive.py", line 412, in create_script_module
return create_script_module_impl(nn_module, concrete_type, stubs_fn)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_recursive.py", line 474, in create_script_module_impl
script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_script.py", line 497, in _construct
init_fn(script_module)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_recursive.py", line 452, in init_fn
scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_recursive.py", line 474, in create_script_module_impl
script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_script.py", line 497, in _construct
init_fn(script_module)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_recursive.py", line 452, in init_fn
scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_recursive.py", line 474, in create_script_module_impl
script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_script.py", line 497, in _construct
init_fn(script_module)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_recursive.py", line 452, in init_fn
scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_recursive.py", line 478, in create_script_module_impl
create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_recursive.py", line 355, in create_methods_and_properties_from_stubs
concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_recursive.py", line 820, in compile_unbound_method
create_methods_and_properties_from_stubs(concrete_type, (stub,), ())
File "/usr/local/lib/python3.6/dist-packages/torch/jit/_recursive.py", line 355, in create_methods_and_properties_from_stubs
concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
RuntimeError:
Module 'Conv2d' has no attribute '_reversed_padding_repeated_twice' :
File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py", line 436
def _conv_forward(self, input: Tensor, weight: Tensor, bias: Optional[Tensor]):
if self.padding_mode != 'zeros':
return F.conv2d(F.pad(input, self._reversed_padding_repeated_twice, mode=self.padding_mode),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
weight, bias, self.stride,
_pair(0), self.dilation, self.groups)
'Conv2d._conv_forward' is being compiled since it was called from 'Conv2d.forward'
File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py", line 443
def forward(self, input: Tensor) -> Tensor:
return self._conv_forward(input, self.weight, self.bias)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
I also noticed there’s some warning about the source code change:
/usr/local/lib/python3.6/dist-packages/torch/serialization.py:671: SourceChangeWarning: source code of class 'models.ssd_r34.SSD_R34' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
And I guess maybe it’s related to pytorch version. So I tried to re-save the model with the code: torch.save(m, '/new/path/new.pytorch)
, and load it back again to script it, the source code warning goes away, however, the save error occurs.
I’m using torch==1.10.1
, is it related to any API changes? How could I get the model scripted / traced? Thanks!