RuntimeError: ONNX export failed: Couldn't export operator aten::norm

Hi, I ran into this issue while exporting to onnx model. Has anyone run into this issue before?

/home/zichengr/.local/lib/python3.5/site-packages/torch/onnx/utils.py:365: UserWarning: ONNX export failed on ATen operator norm because torch.onnx.symbolic.norm does not exist
.format(op_name, op_name))

RuntimeError: ONNX export failed: Couldn’t export operator aten::norm

I had the same problem, and couldn’t find any answer to it. So I checked out the torch.onnx docs, and saw that onnx already has an operator for L-normalization (LpNormalization). Then I edited the torch/onnx/symbolic.py file and added a mapping from tensor norm to that operator:

def norm(g, self, p, dim, keepdim):
    return g.op("LpNormalization", self, p_i=p, axis_i=dim)

Additionally, I had to replace this:
x = F.normalize(x, p=2, dim=1)

with this:
x = x / (x.norm(p=2, dim=1, keepdim=True) + 1e-12).expand_as(x)

to avoid having to export clamp_min as well.
That seemed to work for me, hope it helps!

3 Likes

Amazing job! It worked for me too. Thanks!

Hi,
Can you tell me the location of the LpNormalization ? I don’t seem to find it.

Thanks.

where is the torch/onxx/symbolic.py in torch python library

RuntimeError: ONNX export failed: Couldn’t export operator aten::softmax

Hi, @cfer8395 could share further info? Like, the version of pytorch? Thanks!