Can't script / trace MLCommons SSD-ResNet34 model

username: voidmain1313113
cooked: <p>I’m using the <code>SSD-ResNet34</code> model from <a href="https://github.com/mlcommons/inference/tree/r1.1/vision/classification_and_detection" rel="noopener nofollow ugc">mlcommons inference repo</a>, the model can be downloaded <a href="https://zenodo.org/record/3236545/files/resnet34-ssd1200.pytorch" rel="noopener nofollow ugc">here</a>.</p>
<p>And I wrote a python script to jit.script the model:</p>
<pre><code class="lang-auto">import torch
m = torch.load('/path/to/resnet34-ssd1200.pytorch')
scripted = torch.jit.script(m)
</code></pre>
<p>However, the code won’t work, complaining <code>Module 'Conv2d' has no attribute '_reversed_padding_repeated_twice'</code>.<br>
Here’s the full stack trace:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
&gt;&gt;&gt; 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)
&gt;&gt;&gt; traced = torch.jit.script(m)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  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),
                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;--- 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) -&gt; Tensor:
        return self._conv_forward(input, self.weight, self.bias)
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
</code></pre>
<p>I also noticed there’s some warning about the source code change:</p>
<pre><code class="lang-auto">/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.
</code></pre>
<p>And I guess maybe it’s related to pytorch version. So I tried to re-save the model with the code: <code>torch.save(m, '/new/path/new.pytorch)</code>, and load it back again to script it, the source code warning goes away, however, the save error occurs.</p>
<p>I’m using <code>torch==1.10.1</code>, is it related to any API changes? How could I get the model scripted / traced? Thanks!</p>