ArrayRef length issue when export pytorch model to onnx

weidezhang: <p>Hi</p>
<p>I have some problem converting my pytorch model into onnx. My input to the model is a list(since I define my own data loader collate_fn), where length of the list is the batch size and each element in the list is a custom tuple of elements. My output is a tensor with shape: batchsize x height x width</p>
<p>The code converting model to onnx:<br>
<code><br>
# Export the model<br>
torch.onnx.export(model,               # model being run<br>
cuda(X),                         # model input (or a tuple for multiple inputs)<br>
“final.onnx”,   # where to save the model (can be a file or file-like object)<br>
export_params=True,        # store the trained parameter weights inside the model file<br>
opset_version=9,          # the ONNX version to export the model to<br>
do_constant_folding=True,  # whether to execute constant folding for optimization<br>
input_names = [‘input’],   # the model’s input names<br>
output_names = [‘pred_traj_tensor’], # the model’s output names<br>
dynamic_axes={‘input’ : {0 : ‘batch_size’},    # variable lenght axes<br>
‘pred_traj_tensor’ : {0 : ‘batch_size’}}, verbose=True)<br>
</code></p>
<p>Runtime Error:<br>
<code><br>
File “/home/weide/anaconda3/envs/fuel-py36/lib/python3.6/site-packages/torch/onnx/utils.py”, line 144, in _optimize_graph<br>
torch._C._jit_pass_onnx_peephole(graph, _export_onnx_opset_version, fixed_batch_size)<br>
RuntimeError: ArrayRef: invalid index Index = 0; Length = 0 (at at /opt/conda/conda-bld/pytorch_1579022034529/work/c10/util/ArrayRef.h:197)<br>
</code></p>
<p>Really don’t know how to debug further. Any help is greatly appreciated !</p>
<p>Here is the detailed stack trace.</p>
<code>
Traceback (most recent call last):
  File "learning_algorithms/prediction/datasets/apollo_vehicle_trajectory_dataset/convert_onnx.py", line 113, in 
    'pred_traj_tensor' : {0 : 'batch_size'}})
  File "/home/weide/anaconda3/envs/fuel-py36/lib/python3.6/site-packages/torch/onnx/__init__.py", line 148, in export
    strip_doc_string, dynamic_axes, keep_initializers_as_inputs)
  File "/home/weide/anaconda3/envs/fuel-py36/lib/python3.6/site-packages/torch/onnx/utils.py", line 66, in export
    dynamic_axes=dynamic_axes, keep_initializers_as_inputs=keep_initializers_as_inputs)
  File "/home/weide/anaconda3/envs/fuel-py36/lib/python3.6/site-packages/torch/onnx/utils.py", line 418, in _export
    fixed_batch_size=fixed_batch_size)
  File "/home/weide/anaconda3/envs/fuel-py36/lib/python3.6/site-packages/torch/onnx/utils.py", line 298, in _model_to_graph
    fixed_batch_size=fixed_batch_size, params_dict=params_dict)
  File "/home/weide/anaconda3/envs/fuel-py36/lib/python3.6/site-packages/torch/onnx/utils.py", line 144, in _optimize_graph
    torch._C._jit_pass_onnx_peephole(graph, _export_onnx_opset_version, fixed_batch_size)
RuntimeError: ArrayRef: invalid index Index = 0; Length = 0 (at at /opt/conda/conda-bld/pytorch_1579022034529/work/c10/util/ArrayRef.h:197)
frame #0: c10::Error::Error(c10::SourceLocation, std::string const&amp;) + 0x47 (0x7fb2baa2c627 in /home/weide/anaconda3/envs/fuel-py36/lib/python3.6/site-packages/torch/lib/libc10.so)
frame #1:  + 0x6f484b (0x7fb2ed63184b in /home/weide/anaconda3/envs/fuel-py36/lib/python3.6/site-packages/torch/lib/libtorch_python.so)
frame #2:  + 0x7003ec (0x7fb2ed63d3ec in /home/weide/anaconda3/envs/fuel-py36/lib/python3.6/site-packages/torch/lib/libtorch_python.so)
frame #3: torch::jit::PeepholeOptimizeONNX(std::shared_ptr&amp;, int, bool) + 0x125 (0x7fb2ed642985 in /home/weide/anaconda3/envs/fuel-py36/lib/python3.6/site-packages/torch/lib/libtorch_python.so)
frame #4:  + 0x6b496c (0x7fb2ed5f196c in /home/weide/anaconda3/envs/fuel-py36/lib/python3.6/site-packages/torch/lib/libtorch_python.so)
frame #5:  + 0x28c076 (0x7fb2ed1c9076 in /home/weide/anaconda3/envs/fuel-py36/lib/python3.6/site-packages/torch/lib/libtorch_python.so)

frame #36: __libc_start_main + 0xe7 (0x7fb30ac9ab97 in /lib/x86_64-linux-gnu/libc.so.6)
</code>

eellison: <p>Hi <a class="mention" href="/u/weidezhang">@weidezhang</a>, seems like an ONNX bug. I would suggest raising an issue on the pytorch repo.</p>

weidezhang: <p>created: <a href="https://github.com/pytorch/pytorch/issues/39762" rel="nofollow noopener">https://github.com/pytorch/pytorch/issues/39762</a></p>

weidezhang: <p>Hi <a class="mention" href="/u/eellison">@eellison</a>, could you help me to see who is able to look at the issue on github ? I have updated the details(model and test script) on github.</p>