ONNX Export failed on ATen operator var (KeyError: 'var')

Hi all,

I’m trying to convert a PyTorch model to ONNX with torch.onnx.export, but the operation fails upon trying the ‘var’ operator (symbolic_opset9). I have done some reading and found mean variance normalization (mvn) is supported, but I could not find anything about var alone.

The issue lies when torch.onnx.export is called, with the following trace:

/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/utils.py:715: UserWarning: ONNX export failed on ATen operator var because torch.onnx.symbolic_opset9.var does not exist

Traceback (most recent call last):
File “onnx_conversion_script.py”, line 74, in
torch.onnx.export(model, dummy_input, model_name + “.onnx”, verbose=True, input_names = input_name, output_names=output_name)
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/init.py”, line 158, in export
custom_opsets, enable_onnx_checker)
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/utils.py”, line 68, in export
custom_opsets=custom_opsets, enable_onnx_checker=enable_onnx_checker)
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/utils.py”, line 469, in _export
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/utils.py”, line 338, in _model_to_graph
fixed_batch_size=fixed_batch_size, params_dict=params_dict)
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/utils.py”, line 153, in _optimize_graph
graph = torch._C._jit_pass_onnx(graph, operator_export_type)
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/init.py”, line 189, in _run_symbolic_function
return utils._run_symbolic_function(*args, **kwargs)
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/utils.py”, line 716, in _run_symbolic_function
op_fn = sym_registry.get_registered_op(op_name, ‘’, opset_version)
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/symbolic_registry.py”, line 94, in get_registered_op
return _registry[(domain, version)][opname]
KeyError: ‘var’

Is a custom operator necessary? Has anyone else run into this? I have tried both the latest PyTorch as well as the nightly build with no success.


Variance doesn’t seems to be an ONNX operator. But hopefully, it can be expressed as a combination of simpler operations.

  • One way to “bypass” it is to remove your call to “var” and replace by its formula.
  • A better approach is to add to torch.onnx the var export (I could do it if I get some free time this evening).
  • Ideally, if its usage is common, it would be interesting to open an issue on the ONNX Repo to ask for a Variance opcode that could be added to the standard.

Do you know how many models use this operator? I never had to use it before personally. :sweat_smile:

PS: The variance can be expressed as {\displaystyle \sigma ^{2}={\overline {(x^{2})}}-{\bar {x}}^{2}=\displaystyle {\frac {\sum {i=1}^{N}x{i}^{2}-(\sum {i=1}^{N}x{i})^{2}/N}{N}}.!}

1 Like

@jaolan I think you should open a feature request on https://github.com/pytorch/pytorch/issues

Thanks for your reply!

Adding var to the export is a good idea :grin:. This is the first time I’ve seen it in a model I am exporting as well, but it seems like an operator that could be useful to others - I will open a feature request in PyTorch.

Good idea! I will open an issue in PyTorch requesting the Var operator.

@Jeremy_Cochoy @jaolan
Hi Both, I’m using Pytorch 1.4 and got the save issue when convert pytorch model to onnx.
I’m new to Pytorch, I want to know how to “Adding var to the export”?

Oh I got this instruction as following, I’ll try first. Sorry for post before searching the web.