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

[microTVM] Improve code reuse in Corstone300 conv2d tests #13051

Merged
merged 11 commits into from
Oct 21, 2022

Conversation

guberti
Copy link
Member

@guberti guberti commented Oct 12, 2022

This pull request contains two changes:

  1. Adds support for specifying an out_layout to the Tensordot-based convolution schedules for Arm Cortex-M. This is useful, as when compiling models like MobileNetV1, we will want to alternate between NHWC and NCHW layers (see this comment for more).

  2. Re-organizes the Corstone300 tests for regular, grouped, and depthwise conv2d to use a shared base class. This removes a lot of code re-use that used to exist in tests/python/relay/strategy/arm_cpu.

I chose to make these changes as one PR instead of two, since I had to change some of the testing logic to support the out_layout parameter (and copying that into four places would be kinda gross). Would love a look from @mkatanbaf!

@tvm-bot
Copy link
Collaborator

tvm-bot commented Oct 12, 2022

Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.

Generated by tvm-bot

@guberti
Copy link
Member Author

guberti commented Oct 12, 2022

When reorganizing the tests, I noticed there was duplicate code in tests besides the conv2d ones. Each file has its own calls to relay.op.nn.[operator], which are constructed the same way and then passed to compile_and_run.

  • Would it make sense to create an even-more-abstract class that handled graph creation and running for all the tests in this directory, not just the conv2d variants?
  • I would prefer to test our schedules by comparing them to external operator implementations - say, the Tensorflow ones - rather than by comparing them to our x86 schedules. It would be much simpler to do this as well.

Copy link
Contributor

@mkatanbaf mkatanbaf left a comment

Choose a reason for hiding this comment

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

Thanks @guberti. LGTM. I added a few minor comments

kernel_layout = tvm.testing.parameter("HWOI")
schedule_name = tvm.testing.parameter("depthwise_conv2d_nhwc_dsp.arm_cpu")
dilation = parameter(1)
data_layout = parameter("NHWC")
Copy link
Contributor

Choose a reason for hiding this comment

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

the data_shape is always in NHWC layout. should we rename the data_layout to in_layout to avoid any confusion?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd prefer to keep the name data_shape, as it's the name used by relay.op.nn.conv2d. However, I added a comment clarifying this to test_generalized_conv2d.py.

kernel_layout = tvm.testing.parameter("OIHW")
schedule_name = tvm.testing.parameter("depthwise_conv2d_nchw_oihw_dsp.arm_cpu")
dilation = parameter(1)

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: remove new line. there are a couple more in other files too.

Copy link
Member Author

Choose a reason for hiding this comment

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

I prefer the newline to separate "computation" parameters from "layout" parameters, and IMO it helps with readability. I could be convinced otherwise, though.

@mkatanbaf
Copy link
Contributor

* Would it make sense to create an even-more-abstract class that handled graph creation and running for all the tests in this directory, not just the `conv2d` variants?

When I was working on adding corstone to the platforms supported by the project API, we talked about migrating from this test structure and use Project API for these tests too. But then the fvp transport layer was not as stable as we have hoped for, and we didn't continue that work at that time. One argument in favor of creating a more abstract class is that it makes it easier to change the test structure.

@guberti
Copy link
Member Author

guberti commented Oct 13, 2022

Thanks for the review @mkatanbaf! I'm a big fan of the current test runner and of the AOTTestModule and compile_and_run helper functions - they've proven to be stable, and when running the tests locally they're relatively fast too.

It is possible we change the test structure in the future though - your argument makes sense. Creating a more abstract class is probably OOS for this PR, but I'd be open to it in the future.

@mehrdadh
Copy link
Member

mehrdadh commented Oct 14, 2022

@guberti the problem with current test setup is that we cannot test it on hardware out of the box. So ideally we want to move to project API for corstone so we have the same standard API for testing Zephyr/Arduino/Corstone300 or any other microtvm platform with ARM chip.
This is however out of the scope of this PR. But I suggest we don't create another abstract test class until we figure out the issue with corstone project API

@guberti
Copy link
Member Author

guberti commented Oct 14, 2022

@mehrdadh thanks for explaining, that makes sense. Tests should pass now, would you mind reviewing and merging?

@guberti guberti force-pushed the micro/improve-corstone-tests branch from ee6096c to 2ffad17 Compare October 17, 2022 16:37
Copy link
Contributor

@mkatanbaf mkatanbaf left a comment

Choose a reason for hiding this comment

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

Thanks @guberti, LGTM

python/tvm/topi/utils.py Outdated Show resolved Hide resolved
# Our x86 depthwise implementation only supports HWOI with NHWC, so we need to change our
# kernel layout to work around this. We can't just change the whole thing to HWIO or
# something else, as then group conv2d would not work. Eventually, we should switch to using
# TensorFlow to create the reference output so we can ensure our implementation is right.
Copy link
Contributor

Choose a reason for hiding this comment

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

can you file a bug for this and link to it from this comment?

Copy link
Member Author

@guberti guberti Oct 19, 2022

Choose a reason for hiding this comment

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

Done, see #13137. I also link to it from the comment.

tests/python/relay/strategy/arm_cpu/test_group_conv2d.py Outdated Show resolved Hide resolved
@areusch areusch added needs-triage PRs or issues that need to be investigated by maintainers to find the right assignees to address it and removed needs-triage PRs or issues that need to be investigated by maintainers to find the right assignees to address it labels Oct 19, 2022
@guberti guberti force-pushed the micro/improve-corstone-tests branch from 51c96eb to 6a12eca Compare October 19, 2022 15:30
@guberti guberti force-pushed the micro/improve-corstone-tests branch from 6a12eca to fed35c2 Compare October 19, 2022 15:32
@guberti
Copy link
Member Author

guberti commented Oct 19, 2022

Thanks for the review @areusch! Your comments should be addressed by fed35c2.

Copy link
Member

@mehrdadh mehrdadh left a comment

Choose a reason for hiding this comment

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

@guberti @mkatanbaf @areusch thanks!
This PR is merged now.

@mehrdadh mehrdadh merged commit e7a72af into apache:main Oct 21, 2022
janetsc pushed a commit to janetsc/tvm that referenced this pull request Oct 21, 2022
* Add support for out_layout to tensordot schedules and tests

* Move shared conv2d test logic into new file

* Rework depthwise and grouped convolutions to use common logic

* Linting and bugfixes

* Fix tests

* Fix depthwise and grouped tests

* More linting fixes

* Address code review comments

* Fix unit tests

* Address code review comments from Andrew

* Fix imports
xinetzone pushed a commit to daobook/tvm that referenced this pull request Nov 10, 2022
* Add support for out_layout to tensordot schedules and tests

* Move shared conv2d test logic into new file

* Rework depthwise and grouped convolutions to use common logic

* Linting and bugfixes

* Fix tests

* Fix depthwise and grouped tests

* More linting fixes

* Address code review comments

* Fix unit tests

* Address code review comments from Andrew

* Fix imports
xinetzone pushed a commit to daobook/tvm that referenced this pull request Nov 25, 2022
* Add support for out_layout to tensordot schedules and tests

* Move shared conv2d test logic into new file

* Rework depthwise and grouped convolutions to use common logic

* Linting and bugfixes

* Fix tests

* Fix depthwise and grouped tests

* More linting fixes

* Address code review comments

* Fix unit tests

* Address code review comments from Andrew

* Fix imports
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.

5 participants