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

[Layout] Unify dense op input layout #8921

Merged
merged 4 commits into from
Sep 7, 2021
Merged

Conversation

masahi
Copy link
Member

@masahi masahi commented Sep 3, 2021

Fixes the issue reported in https://discuss.tvm.apache.org/t/pytorch-layout-cannot-convert-f-linear-x-f-linear-y-z/10866/

Currently, dense op's input layouts are NC and NK for data and weight respectively. This causes an issue when the output of dense is used as the weight for another dense. We get "Incompatible layouts: NC vs NK" error.

There is no need to distinguish the second dim of data and weight, since both must be the same value. So I replaced NK with NC.

@comaniac @yzhliu

@lazycal
Copy link
Contributor

lazycal commented Sep 3, 2021

@masahi Thanks for fixing this issue! Just one random thought out of this issue that may further improve TVM: is the layout annotation imposing too many unnecessary constraints (i.e., asserting semantics of each dimension)? Will it be better to not annotate layout itself, but instead annotate how the layout got transformed?

As in this case, previously TVM assumes NK for the second operand, which essentially constraints the 1st dimension to have the semantic of "batch" and the 2nd "units". This is why this issue happens: another operation assumes a semantic of NC which conflicts with NK.

However, I feel like what AlterOpLayout needs is only how the layouts are transformed before and after the pass (e.g., the 2nd dimension is split by a factor of xxx), and it does not care about the semantics at all. The current fix solves the issue for nn.dense, but it seems the fundamental issue still exists. For example, F.conv2d(x, F.conv2d(y, z)) will be broken too (though I guess this is a weird pattern that may not occur in practice?).

@masahi
Copy link
Member Author

masahi commented Sep 3, 2021

@lazycal That is an interesting suggestion. Layout annotation in TVM was introduced a long time ago, and I believe this is the simplest solution that works in most cases. Indeed, F.conv2d(x, F.conv2d(y, z)) would break in the current system. What you suggested makes a lot of sense and I like I0_I1_10i0 annotation you mentioned in the discuss post.

@masahi
Copy link
Member Author

masahi commented Sep 7, 2021

cc @comaniac @yzhliu should be an easy PR.

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

@comaniac comaniac merged commit 0fb840e into apache:main Sep 7, 2021
@comaniac
Copy link
Contributor

comaniac commented Sep 7, 2021

Thanks @masahi. The fix makes perfect sense to me.

ylc pushed a commit to ylc/tvm that referenced this pull request Sep 29, 2021
ylc pushed a commit to ylc/tvm that referenced this pull request Jan 13, 2022
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.

3 participants