Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Relay][ONNX][Fix] Flatten in OnnxConverter #10593

Merged
merged 3 commits into from
Mar 16, 2022
Merged

Conversation

ganler
Copy link
Contributor

@ganler ganler commented Mar 13, 2022

Thanks for contributing to TVM! Please refer to guideline https://tvm.apache.org/docs/contribute/ for useful information and tips. After the pull request is submitted, please request code reviews from Reviewers by @ them in the pull request thread.

Simple Flatten operator's shape cannot be evaluated when using ONNX. This is because the flatten op does not try to do constant folding thus creating unnecessary dynamic operator, causing graph executor unable to execute it.

To trigger this bug, simply use this script to generate a ONNX model and run it with graph executor.

import torch

N = 1 # can be something else

class Net(torch.nn.Module):
    def __init__(self) -> None:
        super().__init__()
    
    def forward(self, x):
        return x.flatten()

net = Net().eval()

o = net(torch.zeros((N), dtype=torch.float))
print(o.shape)

with torch.no_grad():
  torch.onnx.export(net, (
      torch.ones((N), dtype=torch.float)), "output.onnx", verbose=True, opset_version=14)

It will throw errors: the relay script is unnecessarily complicated.

def @main(%v0: Tensor[(1), float32]) -> Tensor[(?, ?), float32] {
  %0 = shape_of(%v0, dtype="int32") /* ty=Tensor[(1), int32] */;
  %1 = strided_slice(%0, begin=[0], end=[0], strides=[1], axes=None) /* ty=Tensor[(0), int32] */;
  %2 = strided_slice(%0, begin=[0], end=[1], strides=[1], axes=None) /* ty=Tensor[(1), int32] */;
  %3 = prod(%1, keepdims=True) /* ty=Tensor[(1), int32] */;
  %4 = prod(%2, keepdims=True) /* ty=Tensor[(1), int32] */;
  %5 = (%3, %4);
  %6 = concatenate(%5) /* ty=Tensor[(2), int32] */;
  dyn.reshape(%v0, %6, newshape=[]) /* ty=Tensor[(?, ?), float32] */
}

Traceback (most recent call last):
...
    executor = relay.build_module.create_executor(
  File "/home/ganler/Documents/tvm/python/tvm/relay/backend/interpreter.py", line 171, in evaluate
    return self._make_executor()
  File "/home/ganler/Documents/tvm/python/tvm/relay/build_module.py", line 621, in _make_executor
    raise ValueError(
ValueError: ('Graph Executor only supports static graphs, got output type', TensorType([?, ?], float32))

It looks much better after this fix.

def @main(%v0: Tensor[(1), float32]) -> Tensor[(1, 1), float32] {
  reshape(%v0, newshape=[1, 1]) /* ty=Tensor[(1, 1), float32] */
}

cc: @AndrewZhaoLuo @vinx13 @jwfromm @masahi

@ganler ganler changed the title [Fix] Flatten in OnnxConverter [Relay][ONNX][Fix] Flatten in OnnxConverter Mar 13, 2022
Copy link
Member

@masahi masahi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice!

@ganler
Copy link
Contributor Author

ganler commented Mar 15, 2022

It seems the CI is broken. I will push an empty commit when it's fixed.

@masahi
Copy link
Member

masahi commented Mar 15, 2022

Hopefully fixed by #10610

@masahi
Copy link
Member

masahi commented Mar 16, 2022

The CI issue has been fixed thanks to #10626

@ganler
Copy link
Contributor Author

ganler commented Mar 16, 2022

@masahi Thank you! The CI now passes!

@masahi masahi merged commit f700e72 into apache:main Mar 16, 2022
pfk-beta pushed a commit to pfk-beta/tvm that referenced this pull request Apr 11, 2022
* fix flatten

* fix: python tuple to list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants