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

[TVMC] Allow manual shape specification in tvmc #7366

Merged
merged 13 commits into from
Feb 9, 2021

Conversation

CircleSpin
Copy link
Contributor

Currently tvmc does not allow users to overriding specific shapes (such as batch sizes) and instead uses what is defined within the model file. In most cases this is most practical, but in there are some cases that would benefit from additional flexibility i.e. undefined shape sizes, fiddling with batch sizes. This PR adds support for manually specifying input shapes. :)

@CircleSpin
Copy link
Contributor Author

@leandron @jwfromm @mbrookhart

Could you take a look at this PR and let me know what you think?

Copy link
Contributor

@leandron leandron left a comment

Choose a reason for hiding this comment

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

Hi @CircleSpin, thanks for the PR - I think shape definition from the outside is something we want to have in tvmc. So much so, that we have discussions going on in #7359.

I think we can use some ideas from the ongoing discussion in #7359, like the syntax to define shapes, and apply shape definition to more frontends, as you have covered here.

I suggest the first thing, we organise which PR takes care of which change.

cc @ekalda @u99127

@comaniac
Copy link
Contributor

Agree with @leandron. We can actually just keep one PR for both purposes, because PyTorch fix is more like a corner case of supporting custom shape.

@jwfromm
Copy link
Contributor

jwfromm commented Jan 29, 2021

Funny that these two very similar PRs landed so close to one another. After reading both, I would argue in favor of choosing this implementation. I think its important to be able to specify a mapping between input name and shape. Not only is this important for constructing a shape dictionary for all non-pytorch frontends, but it also allows a subset of shapes to be overwritten. If a bert model for example had 4 default shapes, this approach would allow us to specify seq_len=[256] while leaving the other 3 shapes alone. This would be much less clear without names.

@comaniac
Copy link
Contributor

@CircleSpin @ekalda could you folks organize a plan to work on the one PR and close the other?

@ekalda
Copy link
Contributor

ekalda commented Jan 29, 2021

From what I can tell, the functionality this PR implements is a superset of the one in my PR, so I'm happy to close mine.

Copy link
Contributor

@ekalda ekalda left a comment

Choose a reason for hiding this comment

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

Some suggestions (mostly around PyTorch), but I agree with others that it is good to have --shapes as a general parameter :)

Another suggestion (I didn't know where was a good place to add it):
I think it would make sense to add --shapes parameter to tvmc tune as well since tvmc tune also calls frontend.load_model. In fact, since PyTorch models rely on that parameter having a value, we wouldn't be able to tune them without that parameter being part of tvmc tune.

python/tvm/driver/tvmc/compiler.py Outdated Show resolved Hide resolved
python/tvm/driver/tvmc/frontends.py Outdated Show resolved Hide resolved
tests/python/driver/tvmc/test_compiler.py Outdated Show resolved Hide resolved
@CircleSpin
Copy link
Contributor Author

Thank you @ekalda for your super thorough feedback! 🏆 We've tried to incorporate it, and also included a test in test_frontends.py that imports a torchvision resnet18 using the new shape specification. I've also added shape inputs to tuning as you had mentioned. Could you take another look and let me know if you have any additional thoughts or feedback? 😄

python/tvm/driver/tvmc/common.py Outdated Show resolved Hide resolved
python/tvm/driver/tvmc/common.py Outdated Show resolved Hide resolved
python/tvm/driver/tvmc/common.py Outdated Show resolved Hide resolved
python/tvm/driver/tvmc/common.py Outdated Show resolved Hide resolved
python/tvm/driver/tvmc/common.py Outdated Show resolved Hide resolved
python/tvm/driver/tvmc/compiler.py Outdated Show resolved Hide resolved
python/tvm/driver/tvmc/frontends.py Outdated Show resolved Hide resolved
python/tvm/driver/tvmc/frontends.py Outdated Show resolved Hide resolved
python/tvm/driver/tvmc/frontends.py Outdated Show resolved Hide resolved
tests/python/driver/tvmc/test_common.py Show resolved Hide resolved
@comaniac
Copy link
Contributor

Also cc @masahi to review PyTorch shape handling.

Copy link
Contributor

@leandron leandron left a comment

Choose a reason for hiding this comment

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

Some comments, trying not to overlap with other reviews.

python/tvm/driver/tvmc/common.py Outdated Show resolved Hide resolved
tests/python/driver/tvmc/test_common.py Outdated Show resolved Hide resolved
tests/python/driver/tvmc/test_compiler.py Outdated Show resolved Hide resolved
python/tvm/driver/tvmc/autotuner.py Outdated Show resolved Hide resolved
@CircleSpin
Copy link
Contributor Author

@hogepodge

@CircleSpin
Copy link
Contributor Author

@comaniac @leandron @jwfromm

The code has been refactored. The input syntax has been updated so that "data:[32,3,224,224] data2:[1,4,4,4]" will work, and the previous one no longer will. Could you take another pass and let me know your thoughts on this new version?

Copy link
Contributor

@comaniac comaniac left a comment

Choose a reason for hiding this comment

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

LGTM. Just a nit.

python/tvm/driver/tvmc/common.py Outdated Show resolved Hide resolved
Copy link
Contributor

@leandron leandron left a comment

Choose a reason for hiding this comment

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

LGTM, with just a small comment.

python/tvm/driver/tvmc/frontends.py Show resolved Hide resolved
@CircleSpin
Copy link
Contributor Author

Thanks for the catches you both found, @comaniac and @leandron 😃

Copy link
Contributor

@jwfromm jwfromm left a comment

Choose a reason for hiding this comment

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

This LGTM. Thanks for making this change @CircleSpin, it will definitely be helpful to TVMC users!

Copy link
Contributor

@leandron leandron left a comment

Choose a reason for hiding this comment

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

LGTM - thanks for the effort in implementing this feature @CircleSpin.

@leandron
Copy link
Contributor

leandron commented Feb 5, 2021

I think this one is ready for merge, right? Just need another CI run...

@comaniac
Copy link
Contributor

comaniac commented Feb 5, 2021

I think this one is ready for merge, right? Just need another CI run...

@CircleSpin please fix or re-trigger the CI.

@jwfromm
Copy link
Contributor

jwfromm commented Feb 5, 2021

@masahi the previous build error seems like a torch failure unrelated to TVM but is not at all descriptive. Have you run into that one before?

@comaniac
Copy link
Contributor

comaniac commented Feb 5, 2021

@masahi the previous build error seems like a torch failure unrelated to TVM but is not at all descriptive. Have you run into that one before?

It's not related to torch failure. It was because of the 2-hour timeout on CI and has been worked around.

@jwfromm
Copy link
Contributor

jwfromm commented Feb 6, 2021

Looks like the new commit failed again in the same way, python aborting during construction of the torchvision model.

@masahi
Copy link
Member

masahi commented Feb 6, 2021

Yeah looks wierd. Does it reproduce locally? Maybe CPU node has older torch and torchvision

@jwfromm
Copy link
Contributor

jwfromm commented Feb 6, 2021

this works fine locally with the same version of torch and torchvision as CI. Not sure what the deal is.

@masahi
Copy link
Member

masahi commented Feb 6, 2021

I don't think doing frontend tests during CPU unittest is a good idea. I suggest moving test_frontends.py to test/python/frontend.

@leandron
Copy link
Contributor

leandron commented Feb 6, 2021

I don't think doing frontend tests during CPU unittest is a good idea. I suggest moving test_frontends.py to test/python/frontend.

Some test needs to be done to exercise the integration between TVMC and the underlying APIs - we want to keep this to a minimum, and will be reducing the number of tests that actually do this as per previous discussions in #6831 (cc @d-smirnov).

In #7304 I have the first test within TVMC that mocks all the APIs and just checks for arguments being passed - I intend to use that one as an example to motivate some refactoring in the TVMC tests to make them easier/faster to run.

@leandron
Copy link
Contributor

leandron commented Feb 9, 2021

@comaniac it looks like now the CI issues are gone, can we merge this one when you have a moment?

@comaniac comaniac merged commit 2b8d113 into apache:main Feb 9, 2021
@comaniac
Copy link
Contributor

comaniac commented Feb 9, 2021

Thanks @CircleSpin @ekalda @leandron @jwfromm @masahi

alexwong pushed a commit to alexwong/tvm that referenced this pull request Feb 11, 2021
* add ability to optionally overide tvm shapes

* add help documentation for --shapes

* improve documentation

* reformat test_compiler using black

* Incorporate feedback from ekalda for better pytorch support and testing.

* address feedback

* switch input shape syntax to be more pythonic

* add commentary

* reformat common.py

* fix lint issue

* format common.py with black

* torch/pytorch test hiccup

* add -s to setup-pytest-env.sh for clearer error msgs

Co-authored-by: Jocelyn <jocelyn@pop-os.localdomain>
@apivovarov
Copy link
Contributor

apivovarov commented Feb 18, 2021

Quite often I see the following failure in CI builds

test_frontends.py::test_load_model__pth munmap_chunk(): invalid pointer
Fatal Python error: Aborted

test_load_model__pth was added in this PR

Issue: #7471

Lokiiiiii pushed a commit to Lokiiiiii/tvm that referenced this pull request Mar 2, 2021
* add ability to optionally overide tvm shapes

* add help documentation for --shapes

* improve documentation

* reformat test_compiler using black

* Incorporate feedback from ekalda for better pytorch support and testing.

* address feedback

* switch input shape syntax to be more pythonic

* add commentary

* reformat common.py

* fix lint issue

* format common.py with black

* torch/pytorch test hiccup

* add -s to setup-pytest-env.sh for clearer error msgs

Co-authored-by: Jocelyn <jocelyn@pop-os.localdomain>
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Mar 2, 2021
* add ability to optionally overide tvm shapes

* add help documentation for --shapes

* improve documentation

* reformat test_compiler using black

* Incorporate feedback from ekalda for better pytorch support and testing.

* address feedback

* switch input shape syntax to be more pythonic

* add commentary

* reformat common.py

* fix lint issue

* format common.py with black

* torch/pytorch test hiccup

* add -s to setup-pytest-env.sh for clearer error msgs

Co-authored-by: Jocelyn <jocelyn@pop-os.localdomain>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants