-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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] Apply constant folding when converting layout #13216
Conversation
This commit ensures that constant folding is applied when a desired layout is selected during compilation. It ensures that `layout_transform` operations are removed where possible so that pattern matching for BYOC backends can work effectively. A test has been added to check this regression. Change-Id: Ie7646a2c8fdb5ec17dcbeb0876f969f8b8218b6e
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 |
from tvm.driver.tvmc.transform import convert_graph_layout | ||
|
||
|
||
def test_layout_transform(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be re-testing the existing passes? Instead we could just test that it's using them? I don't know if there's any off the shelf infrastructure to get a list of passes but we could do something like:
mock_sequence = MagicMock()
mock_pass_runner = MagicMock()
mock_sequence.return_value = mock_pass_runner
monkeypatch.setattr(transform, 'Sequential', mock_sequence);
convert_graph_layout(mod, "NHWC")
# Might need to mock out a bunch of passes here with some
mock_sequence.assert_called_once_with([
relay.transform.RemoveUnusedFunctions(),
relay.transform.ConvertLayout({
"nn.conv2d": ["NHWC", "default"],
"nn.conv2d_transpose": ["NHWC", "default"],
"qnn.conv2d": ["NHWC", "default"],
}),
relay.transform.FoldConstant(),
])
mock_pass_runner.assert_called_once_with(mod);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test in the patch makes sense to me, it checks that the constant has been folded which wouldn't happen unless we have run the FoldConstant
pass. I suppose mocking would work as well, but don't we then have the problem of having to keep the dict of ops and preferred layouts in the codebase manually in sync with the same dict in the test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the issue with the current test is that it checks more than it really needs to and might become a maintenance burden going forwards. Ideally I'd like to make use of pass instruments to check that ConvertLayout
and FoldConstant
are run in the correct order, which means that we aren't tied to the implementation details of the passes. However, the use of PassContext
here overwrites any context set by the user (and therefore by the test that adds the instrumentation) so I think we should remove this and set the context from the callee(s) - given the test works for now, could we do this in a follow up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree, that testing the implementation details of the passes themselves in not ideal. I'd be fine with merging the current patch as it is and improving the test in a follow up, once we have cleared up the nested PassContexts if @Mousius agrees?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, this will fail if we disable constant folding so that'll work for now 😸
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! :)
from tvm.driver.tvmc.transform import convert_graph_layout | ||
|
||
|
||
def test_layout_transform(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test in the patch makes sense to me, it checks that the constant has been folded which wouldn't happen unless we have run the FoldConstant
pass. I suppose mocking would work as well, but don't we then have the problem of having to keep the dict of ops and preferred layouts in the codebase manually in sync with the same dict in the test?
Comes as a followup from conversations in apache#13216. By making the pass context a global value for both `compile` and `tune` commands, we can ensure the pass context is exactly as the user expected and also test components such as `convert_graph_layout` under a pass context suitable for testing (e.g. add instruments). With this change, it becomes the users responsibility to ensure the PassContext they select is suitable for the passes that will be run. By default, `opt_level` remains as 3 so current workflows that do not alter the pass context from the command line / TVMC Python API should not be affected. Change-Id: I7a601daf6fbe664f77bce1b45efeb7ca29f621b3
Comes as a followup from conversations in apache#13216. By making the pass context a global value for both `compile` and `tune` commands, we can ensure the pass context is exactly as the user expected and also test components such as `convert_graph_layout` under a pass context suitable for testing (e.g. add instruments). With this change, it becomes the users responsibility to ensure the PassContext they select is suitable for the passes that will be run. By default, `opt_level` remains as 3 so current workflows that do not alter the pass context from the command line / TVMC Python API should not be affected. Change-Id: I7a601daf6fbe664f77bce1b45efeb7ca29f621b3
This commit ensures that constant folding is applied when a desired layout is selected during compilation. It ensures that `layout_transform` operations are removed where possible so that pattern matching for BYOC backends can work effectively. A test has been added to check this regression.
* [TVMC] Global pass context for compile and tune Comes as a followup from conversations in #13216. By making the pass context a global value for both `compile` and `tune` commands, we can ensure the pass context is exactly as the user expected and also test components such as `convert_graph_layout` under a pass context suitable for testing (e.g. add instruments). With this change, it becomes the users responsibility to ensure the PassContext they select is suitable for the passes that will be run. By default, `opt_level` remains as 3 so current workflows that do not alter the pass context from the command line / TVMC Python API should not be affected. Change-Id: I7a601daf6fbe664f77bce1b45efeb7ca29f621b3 * fix vitis-ai test and typo Change-Id: I04f5bd031ae4717825f42e373bcb0e1e2c1c9d90
This commit ensures that constant folding is applied when a desired layout is selected during compilation. It ensures that `layout_transform` operations are removed where possible so that pattern matching for BYOC backends can work effectively. A test has been added to check this regression.
* [TVMC] Global pass context for compile and tune Comes as a followup from conversations in apache#13216. By making the pass context a global value for both `compile` and `tune` commands, we can ensure the pass context is exactly as the user expected and also test components such as `convert_graph_layout` under a pass context suitable for testing (e.g. add instruments). With this change, it becomes the users responsibility to ensure the PassContext they select is suitable for the passes that will be run. By default, `opt_level` remains as 3 so current workflows that do not alter the pass context from the command line / TVMC Python API should not be affected. Change-Id: I7a601daf6fbe664f77bce1b45efeb7ca29f621b3 * fix vitis-ai test and typo Change-Id: I04f5bd031ae4717825f42e373bcb0e1e2c1c9d90
This commit ensures that constant folding is applied when a desired layout is selected during compilation. It ensures that
layout_transform
operations are removed where possible so that pattern matching for BYOC backends can work effectively. A test has been added.