Skip to content

[Frontend][ONNX]support Pool2D layout is CHW#11034

Merged
AndrewZhaoLuo merged 3 commits intoapache:mainfrom
chengven027:ven.dev
Apr 20, 2022
Merged

[Frontend][ONNX]support Pool2D layout is CHW#11034
AndrewZhaoLuo merged 3 commits intoapache:mainfrom
chengven027:ven.dev

Conversation

@chengven027
Copy link
Contributor

@chengven027 chengven027 commented Apr 18, 2022

Hi, my onnx model is superpoint. it's converted from a pytorch model.
When I import the model. I get the relay as follow:

...
%38 = transpose(%37, axes=[0, 1, 3, 2, 4]);
%39 = reshape(%38, newshape=[1, 480, 640]);
%40 = nn.max_pool2d(%39, pool_size=[9, 9], padding=[4, 4, 4, 4], layout="NCW");
%41 = equal(%39, %40);
cast(%41, dtype="float32")

The model layer in Netron is:
image
Then we can see the %39 reshape is a 3 dimensions. It is legal in pytorch model. But in ONNX, I will get a error.
Then I will add a expand_dims before pool2D and a squeeze after pool2D like the code I commited.

But i don`t know how to add a unit test case. because there will be error in onnxruntime if the pool2D shape is 3D.
image

cc: @MatthewARM @jwfromm @AndrewZhaoLuo

Copy link
Contributor

@AndrewZhaoLuo AndrewZhaoLuo left a comment

Choose a reason for hiding this comment

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

The central issue is that onnx expects a batch dimension: https://github.com/onnx/onnx/blob/main/docs/Operators.md#maxpool

So technically your onnx model is malformed (probably pytorch onnx converter is non spec compliant) since it does not have a batch dimension input to the pooling op. We have seen this before so I'm fine with the change in general. Just address comments and also document this fact.

return attr_cvt([data], attr, params)
out = attr_cvt([data], attr, params)

if ndim == 3 and len(attr["kernel_shape"]) == 2:
Copy link
Contributor

@AndrewZhaoLuo AndrewZhaoLuo Apr 19, 2022

Choose a reason for hiding this comment

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

Can you make this so if kernel_shape rank and ndim differ by 1 we assume it's missing a batch dimension and runs below code? (same thing for _run_calculation)

@chengven027
Copy link
Contributor Author

ok, thanks @AndrewZhaoLuo
I have made the modifications as you said, and run it successed again.

@AndrewZhaoLuo AndrewZhaoLuo merged commit 7612b22 into apache:main Apr 20, 2022
@chengven027 chengven027 deleted the ven.dev branch April 21, 2022 02:25
shtinsa pushed a commit to Deelvin/tvm that referenced this pull request May 17, 2022
* support  Pool layout is CHW

* fix lint test

* change the if condition
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