-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Fix][Frontend][TOPI] minor bugs #8622
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
Conversation
python/tvm/relay/frontend/pytorch.py
Outdated
| # 1 - weight | ||
| bias = inputs[2] | ||
| mm_out = self.matmul(inputs[:2], input_types[:2]) | ||
| mm_out = self.matmul([inputs[0], _op.transpose(inputs[1], axes=(1, 0))], input_types[:2]) |
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.
We can use _op.nn.dense without transposing its second input. Also see https://discuss.tvm.apache.org/t/pytorch-unable-to-import-a-simple-torch-nn-linear-to-relay/10383/4
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.
Maybe I missed something, does that mean the current linear layers converted from PyTorch are wrong?
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, see my comment in the discuss thread above. Until PT 1.8, nn.Linear converts to aten::addmm, so this code path never hit unless users explicitly call something that converts to aten::linear. Since PT 1.9, nn.Linear converts to aten::linear, so this becomes a major issue.
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.
Ah I see, so this bug doesn't explode until PT 1.9. Thanks for the explanation!
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.
@masahi My concern is that linear is supposed to support inputs with arbitrary ranks like 1d, 2d, 3d, 4d.... The different input dimensions are normalized in self.matmul. If _op.nn.dense is directly used, the usability of linear would be limited to inputs whose ranks are 2.
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.
How about we just make an exception: when the input rank is 2 then use dense; otherwise use matmul?
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.
Doesn't op.nn.dense also support ND input?
tvm/python/tvm/relay/op/nn/nn.py
Lines 1528 to 1530 in 850abb0
| data : tvm.relay.Expr | |
| The input data to the operator, | |
| of shape `(d_1, d_2, ..., d_n, units_in)`. |
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.
also if we want to support ND input, please add more tests.
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.
@masahi Tests with 3d input are added. For now I use dense when both inputs are 2d and fallback to matmul otherwise.
I tried op.nn.dense in ND scenario, but it fails at
tvm/python/tvm/topi/x86/dense_alter_op.py
Lines 29 to 37 in 4b9d43e
| @dense_alter_layout.register(["cpu", "arm_cpu"]) | |
| def _alter_dense_layout(attrs, inputs, tinfos, out_type): | |
| target = tvm.target.Target.current(allow_none=False) | |
| dispatch_ctx = autotvm.task.DispatchContext.current | |
| data_tensor, weight_tensor = tinfos | |
| out_dtype = out_type.dtype | |
| M, K = get_const_tuple(data_tensor.shape) | |
| N, _ = get_const_tuple(weight_tensor.shape) | |
where data_tensor.shape is unpacked into a tuple of size 2. I also check that in tests/python/relay/test_op_level1.py dense is tested numerically for 2d inputs only. Not sure if I miss anything.
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.
Yeah it looks like relay doc is wrong and topi dense indeed only supports 2D #8412
junrushao
left a comment
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 agree with Masa, and otherwise looks good to me :-)
|
Any idea why ci always gets aborted in Build:CPU? Did I miss anything? |
It was timeout (4hrs). Re-trigger CI may resolve this issue |
|
It should be fixed by #8658 |
* fix * fix * lint
* fix * fix * lint
* fix * fix * lint
Fix 2 minor bugs in:
cc @yzhliu @comaniac