Hi,
I failed to export Pytorch model contain var operation to ONNX :(
"RuntimeError: Exporting the operator var to ONNX opset version 9 is not supported. Please open a bug to request ONNX export support for the missing operator."
Thank you!
Hi @RanyaJumah,
Currently pytorch to ONNX supports the operators as listed here: https://pytorch.org/docs/stable/onnx.html#supported-operators
Unfortunately torch.var is not one of them. Please open an issue under pytorch repo to request a operator support from the converter team (torch.onnx.export). Thank you.
Hi @jcwchen,
Thank you very much for your quick replay, actually I add "_var function" to my local "symbolic_opset9.py" as bellow, and I successfully create ONNX model:
@parse_args('v', 'is', 'b', 'i')
def _var(g, input, dim, unbiased, keepdim):
if input.isCompleteTensor():
if dim is None:
mean = g.op("ReduceMean", input, keepdims_i=0)
t_mean = mean
num_elements = numel(g, input)
else:
mean = g.op("ReduceMean", input, axes_i=dim, keepdims_i=keepdim)
t_mean = g.op("ReduceMean", input, axes_i=dim, keepdims_i=1)
redudced_dims = g.op("Shape", input)
# dim could contain one or multiple dimensions
redudced_dims = g.op("Gather", redudced_dims, g.op("Constant", value_t=torch.tensor(dim)), axis_i=0)
num_elements = g.op("ReduceProd", redudced_dims, keepdims_i=0)
sub_v = g.op("Sub", input, t_mean)
sqr_sub = g.op("Mul", sub_v, sub_v)
keepdim_mean = 0 if dim is None else keepdim
var = g.op("ReduceMean", sqr_sub, axes_i=dim, keepdims_i=keepdim_mean)
# Correct bias in calculating variance, by dividing it over (N - 1) instead on N
if unbiased:
num_elements = g.op("Cast", num_elements, to_i=sym_help.cast_pytorch_to_onnx['Float'])
one = g.op("Constant", value_t=torch.tensor(1, dtype=torch.float))
mul = g.op("Mul", var, num_elements)
var = g.op("Div", mul, g.op("Sub", num_elements, one))
return var
else:
_unimplemented("var", "Unknown input rank. Cannot compute std along dimensions.")
def var(g, input, *args):
if len(args) == 3:
return _var(g, input, *args)
else:
return _var(g, input, None, args[0], None)
Thanks again!
Best,
Thank you for your contribution! Feel free to send a PR under pytorch to improve the converter. Close this issue now. Please reopen it if you have other ONNX issue.