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

jaolan: <p>Hi all,</p>
<p>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.</p>
<p>The issue lies when torch.onnx.export is called, with the following trace:</p>
<blockquote>
<p>/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</p>
<p>Traceback (most recent call last):<br>
File “onnx_conversion_script.py”, line 74, in <br>
torch.onnx.export(model, dummy_input, model_name + “.onnx”, verbose=True, input_names = input_name, output_names=output_name)<br>
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/<strong>init</strong>.py”, line 158, in export<br>
custom_opsets, enable_onnx_checker)<br>
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/utils.py”, line 68, in export<br>
custom_opsets=custom_opsets, enable_onnx_checker=enable_onnx_checker)<br>
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/utils.py”, line 469, in _export<br>
fixed_batch_size=fixed_batch_size)<br>
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/utils.py”, line 338, in _model_to_graph<br>
fixed_batch_size=fixed_batch_size, params_dict=params_dict)<br>
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/utils.py”, line 153, in _optimize_graph<br>
graph = torch._C._jit_pass_onnx(graph, operator_export_type)<br>
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/<strong>init</strong>.py”, line 189, in _run_symbolic_function<br>
return utils._run_symbolic_function(*args, **kwargs)<br>
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/utils.py”, line 716, in _run_symbolic_function<br>
op_fn = sym_registry.get_registered_op(op_name, ‘’, opset_version)<br>
File “/opt/anaconda3/lib/python3.7/site-packages/torch/onnx/symbolic_registry.py”, line 94, in get_registered_op<br>
return _registry[(domain, version)][opname]<br>
KeyError: ‘var’</p>
</blockquote>
<p>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.</p>
<p>Thanks.</p>

Jeremy_Cochoy: <p>Variance doesn’t seems to be an ONNX operator. But hopefully, it can be expressed as a combination of simpler operations.</p>
<ul>
<li>One way to “bypass” it is to remove your call to “var” and replace by its formula.</li>
<li>A better approach is to add to torch.onnx the var export (I could do it if I get some free time this evening).</li>
<li>Ideally, if its usage is common, it would be interesting to open an issue on the ONNX Repo to ask for a <code>Variance</code> opcode that could be added to the standard.</li>
</ul>
<p>Do you know how many models use this operator? I never had to use it before personally. <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=9" title=":sweat_smile:" class="emoji" alt=":sweat_smile:"></p>
<hr>
<p>PS: The variance can be expressed as <a href="https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance" rel="nofollow noopener"><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/eebbe65be3901ea71934a4f9361c4084b77e6ab4" alt="{\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}}.!}" width="44" height="6"></a></p>

vadimkantorov: <p><a class="mention" href="/u/jaolan">@jaolan</a> I think you should open a feature request on <a href="https://github.com/pytorch/pytorch/issues" rel="nofollow noopener">https://github.com/pytorch/pytorch/issues</a></p>

jaolan: <p>Thanks for your reply!</p>
<p>Adding var to the export is a good idea <img src="https://discuss.pytorch.org/images/emoji/apple/grin.png?v=9" title=":grin:" class="emoji" alt=":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.</p>

jaolan: <p>Good idea! I will open an issue in PyTorch requesting the Var operator.</p>

blindneo: <p><a class="mention" href="/u/jeremy_cochoy">@Jeremy_Cochoy</a>  <a class="mention" href="/u/jaolan">@jaolan</a><br>
Hi Both, I’m using Pytorch 1.4 and got the save issue when convert pytorch model to onnx.<br>
I’m new to Pytorch, I want to know how to “Adding var to the export”?<br>
Thanks.</p>

blindneo: <p>Oh I got this instruction as following, I’ll try first. Sorry for post before searching the web.</p><aside class="onebox githubblob">
  <header class="source">

      <a href="https://github.com/onnx/tutorials/blob/master/tutorials/PytorchAddExportSupport.md" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/onnx/tutorials/blob/master/tutorials/PytorchAddExportSupport.md" target="_blank" rel="noopener nofollow ugc">onnx/tutorials/blob/master/tutorials/PytorchAddExportSupport.md</a></h4>


    <pre><code class="lang-md">&lt;!--- SPDX-License-Identifier: Apache-2.0 --&gt;

## Fail to export the model in PyTorch
When you try to export a model, you may receive a message similar to the following:
```
UserWarning: ONNX export failed on elu because torch.onnx.symbolic.elu does not exist
RuntimeError: ONNX export failed: Couldn't export operator elu
```
The export fails because PyTorch does not support exporting the `elu` operator. If you've already reached out to the ONNX team but haven't received a response, you can add support for this yourself. The difficulty of doing this depends on your answers to the following questions:

### Determine how difficult it is to add support for the operator
#### Question 1: Is the operator you want standardized in ONNX?
Answer:
- **Yes.** Great! It will be straightforward to add support for the missing operator.
- **No.** In this case, it may be difficult to do the work by yourself.
Check the [Standardization Section](#standardize_op).

#### Question 2: Can the ONNX operator be imported by the backend framework, such as Caffe2?
Answer:
- **Yes.** Terrific. We are able to run the exported ONNX model.
</code></pre>


  This file has been truncated. <a href="https://github.com/onnx/tutorials/blob/master/tutorials/PytorchAddExportSupport.md" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>