KeyError: 'prim_Uninitialized'

I got this error when exporting model to ONNX:

/home/dai/py36env/lib/python3.6/site-packages/torch/onnx/ UserWarning: ONNX export failed on primitive operator Uninitialized; please report a bug
  warnings.warn("ONNX export failed on primitive operator {}; please report a bug".format(op_name))
Traceback (most recent call last):
  File "", line 280, in <module>
  File "/home/dai/py36env/lib/python3.6/site-packages/torch/onnx/", line 143, in export
    strip_doc_string, dynamic_axes, keep_initializers_as_inputs)
  File "/home/dai/py36env/lib/python3.6/site-packages/torch/onnx/", line 66, in export
    dynamic_axes=dynamic_axes, keep_initializers_as_inputs=keep_initializers_as_inputs)
  File "/home/dai/py36env/lib/python3.6/site-packages/torch/onnx/", line 382, in _export
  File "/home/dai/py36env/lib/python3.6/site-packages/torch/onnx/", line 262, in _model_to_graph
  File "/home/dai/py36env/lib/python3.6/site-packages/torch/onnx/", line 132, in _optimize_graph
    graph = torch._C._jit_pass_onnx(graph, operator_export_type)
  File "/home/dai/py36env/lib/python3.6/site-packages/torch/onnx/", line 174, in _run_symbolic_function
    return utils._run_symbolic_function(*args, **kwargs)
  File "/home/dai/py36env/lib/python3.6/site-packages/torch/onnx/", line 652, in _run_symbolic_function
    symbolic_fn = sym_registry.get_registered_op(symbolic_name, '', opset_version)
  File "/home/dai/py36env/lib/python3.6/site-packages/torch/onnx/", line 91, in get_registered_op
    return _registry[(domain, version)][opname]
KeyError: 'prim_Uninitialized'

This error could come from these two lines:

if xy_text.size(0) == 0:
        return torch.tensor(0)

What’s the function of prim_Uninitialized? How can I define it? Looking forward to your replay.

prim::Uninitialized is used to represent values which the compiler can prove will never be used. As you correctly guessed, it’s introduced by the early return statement there. You can work around this by removing early returns, breaks, and continues from your scripted function.

Generally, you should expect ONNX to work with tracing and with anything scripted you will likely run into some issues.