Failed to export an ONNX attribute 'onnx::Gather', since it's not constant, please try to make things (e.g., kernel size) static if possible

malemo: <p>Trying to convert <a href="https://github.com/zllrunning/face-parsing.PyTorch" rel="noopener nofollow ugc">this</a> pytorch model with ONNX gives me this error. I’ve searched github and this error came up before in version 1.1.0 but was apparently rectified. Now I’m on torch 1.4.0. (python 3.6.9) and I see this error.</p>
<blockquote>
<p>File “/usr/local/lib/python3.6/dist-packages/torch/onnx/ <strong>init</strong> .py”, line 148, in export<br>
strip_doc_string, dynamic_axes, keep_initializers_as_inputs)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py”, line 66, in export<br>
dynamic_axes=dynamic_axes, keep_initializers_as_inputs=keep_initializers_as_inputs)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py”, line 416, in _export<br>
fixed_batch_size=fixed_batch_size)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py”, line 296, in _model_to_graph<br>
fixed_batch_size=fixed_batch_size, params_dict=params_dict)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py”, line 135, in _optimize_graph<br>
graph = torch._C._jit_pass_onnx(graph, operator_export_type)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/onnx/ <strong>init</strong> .py”, line 179, in _run_symbolic_function<br>
return utils._run_symbolic_function(*args, **kwargs)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py”, line 657, in _run_symbolic_function<br>
return op_fn(g, *inputs, **attrs)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/onnx/symbolic_helper.py”, line 128, in wrapper<br>
args = [_parse_arg(arg, arg_desc) for arg, arg_desc in zip(args, arg_descriptors)]<br>
File “/usr/local/lib/python3.6/dist-packages/torch/onnx/symbolic_helper.py”, line 128, in<br>
args = [_parse_arg(arg, arg_desc) for arg, arg_desc in zip(args, arg_descriptors)]<br>
File “/usr/local/lib/python3.6/dist-packages/torch/onnx/symbolic_helper.py”, line 81, in _parse_arg<br>
"', since it’s not constant, please try to make "<br>
RuntimeError: Failed to export an ONNX attribute ‘onnx::Gather’, since it’s not constant, please try to make things (e.g., kernel size) static if possible</p>
</blockquote>
<p>How to fix it? I’ve also tried latest nightly build, same error comes up.</p>
<p>My code:</p>
<blockquote>
<p><code>from model import BiSeNet</code><br>
<code>import torch.onnx</code><br>
<code>import torch</code></p>
</blockquote>
<blockquote>
<p><code>net = BiSeNet(19)</code><br>
<code>net.cuda()</code><br>
<code>net.load_state_dict(torch.load('/content/drive/My Drive/Collab/fp/res/cp/79999_iter.pth'))</code><br>
<code>net.eval()</code></p>
</blockquote>
<blockquote>
<p><code>dummy = torch.rand(1,3,512,512).cuda()</code><br>
<code>torch.onnx.export(net, dummy, "Model.onnx", input_names=["image"], output_names=["output"])</code><br>
`</p>
</blockquote>
<p>I also tried downgrading to torch 1.0.0 and the error is still there “RuntimeError: ONNX symbolic expected a constant value in the trace”</p>
<p>I added  <code>print (v.node ())</code>  to symbolic_helper.py just before the runtime error is raised to see what’s causing the error.</p>
<p>This is the output:  <code>%595 : Long() = onnx::Gather[axis=0](%592, %594) # /content/drive/My Drive/Collab/fp/model.py:111:0</code></p>
<p>And that line in 111 in model.py is:  <code>avg = F.avg_pool2d(feat32, feat32.size()[2:])</code></p>
<p>Based on my further research I found <a href="https://blog.csdn.net/weixin_43902449/article/details/90515528" rel="noopener nofollow ugc">this</a> source stating that:</p>
<blockquote>
<p>Both resNet50 and 32 are fine, but to resNet18, ONNX model cannot be exported.</p>
</blockquote>
<p>The model I’m using is indeed based on resnet18.</p>
<p>The source suggests the following changes:</p>
<p>From this:</p>
<blockquote>
<p>import torch.nn.functional as F<br>
def forward(self, x):<br>
feat = self.base(x)<br>
feat = F.avg_pool2d(feat, feat.size()[2:])</p>
</blockquote>
<p>To this:</p>
<blockquote>
<p>class Model(nn.Module):<br>
def <strong>init</strong>():<br>
…<br>
self.avg_pool2d = nn.AvgPool2d(kernel_size=k_s, ceil_mode=False)<br>
…<br>
def forward(self, x):<br>
…<br>
feat = self.avg_pool2d(feat, feat.size()[2:])<br>
…</p>
</blockquote>
<p>However after this change I have another error:</p>
<blockquote>
<p>forward() takes 2 positional arguments but 3 were given</p>
</blockquote>

Mate_Nagy: <p>I got a very similar error while trying to export my resnet model. The only difference is that the problem is ‘onnx::Sub’ instead of ‘onnx::Gather’ as in your case. I have not found the solution yet. I also have pytorch 1.4.0.<br>
Let me know if you have the solution.</p>

akshay_sharma1: <p><a class="mention" href="/u/malemo">@malemo</a> <a class="mention" href="/u/mate_nagy">@Mate_Nagy</a> have you guys figure out the solution.<br>
Any suggestion?<br>
Thanks.</p>

Jim_Yan: <p>same problem，but <code> %147 : Long() = onnx::Div(%145, %146)</code>,<br>
can’t traceback where the error occur, someone can help?<br>
pytorch 1.3.0</p>

Vasil_Popov: <p>The same as <a class="mention" href="/u/mate_nagy">@Mate_Nagy</a> ‘onnx::Sub’ any ideas?</p>