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] Apply constant folding when converting layout #13216

Merged
merged 1 commit into from
Nov 2, 2022

Conversation

lhutton1
Copy link
Contributor

@lhutton1 lhutton1 commented Oct 27, 2022

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.

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
@tvm-bot
Copy link
Collaborator

tvm-bot commented Oct 27, 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

@lhutton1
Copy link
Contributor Author

also cc @leandron @ekalda

from tvm.driver.tvmc.transform import convert_graph_layout


def test_layout_transform():
Copy link
Member

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);

Copy link
Contributor

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?

Copy link
Contributor Author

@lhutton1 lhutton1 Oct 28, 2022

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?

Copy link
Contributor

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?

Copy link
Member

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 😸

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.

LGTM! :)

from tvm.driver.tvmc.transform import convert_graph_layout


def test_layout_transform():
Copy link
Contributor

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?

@Mousius Mousius merged commit 4ecf303 into apache:main Nov 2, 2022
@lhutton1 lhutton1 deleted the fix-convert-layout-tvmc branch November 2, 2022 15:58
lhutton1 added a commit to lhutton1/tvm that referenced this pull request Nov 7, 2022
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
lhutton1 added a commit to lhutton1/tvm that referenced this pull request Nov 8, 2022
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
xinetzone pushed a commit to daobook/tvm that referenced this pull request Nov 10, 2022
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.
leandron pushed a commit that referenced this pull request Nov 10, 2022
* [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
xinetzone pushed a commit to daobook/tvm that referenced this pull request Nov 25, 2022
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.
xinetzone pushed a commit to daobook/tvm that referenced this pull request Nov 25, 2022
* [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
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.

4 participants