From ecdd87255f3cc8770121f43d2fbcec2cb07e9169 Mon Sep 17 00:00:00 2001 From: Haozheng Fan Date: Mon, 2 Aug 2021 19:32:47 +0000 Subject: [PATCH 1/3] fix --- include/tvm/topi/detail/ravel_unravel.h | 4 +++- python/tvm/relay/frontend/pytorch.py | 2 +- tests/python/frontend/pytorch/test_forward.py | 3 +++ tests/python/relay/test_op_level3.py | 1 + 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/tvm/topi/detail/ravel_unravel.h b/include/tvm/topi/detail/ravel_unravel.h index dd7bcac09a04..e91d6afb666a 100644 --- a/include/tvm/topi/detail/ravel_unravel.h +++ b/include/tvm/topi/detail/ravel_unravel.h @@ -44,7 +44,9 @@ using namespace tvm::te; */ inline PrimExpr RavelIndex(Array indices, Array shape) { ICHECK_EQ(indices.size(), shape.size()) << "indices and shape must have equal size"; - ICHECK_GT(indices.size(), 0) << "indices must not be empty"; + if (indices.size() == 0U) { + return 0; + } PrimExpr idx; for (size_t i = 0; i < indices.size(); ++i) { if (i == 0) { diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index 33cb83b883bc..b528fff8d455 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -1445,7 +1445,7 @@ def linear(self, inputs, input_types): # 0 - input # 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]) if isinstance(bias, _expr.Expr): bias_ndims = len(self.infer_shape_with_prelude(bias)) if bias_ndims == 1: diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index f76ea9a5d324..c981c0552cd7 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -1571,6 +1571,7 @@ def forward(self, input, weight): input2d = torch.rand([2, 2]).float() weight1d = torch.rand([2]).float() weight2d = torch.rand([2, 2]).float() + weight3x2 = torch.rand([3, 2]).float() bias1d = torch.rand([2]).float() bias2d = torch.rand([2, 2]).float() # 2D input, 2D weight, 1D bias @@ -1579,6 +1580,7 @@ def forward(self, input, weight): verify_model(Linear(), input_data=[input2d, weight2d, bias2d]) # 2D input, 2D weight, no bias verify_model(LinearNoBias(), input_data=[input2d, weight2d]) + verify_model(LinearNoBias(), input_data=[input2d, weight3x2]) # 2D input, 1D weight, 1D bias is not supported by torch.linear() # 2D input, 1D weight, no bias verify_model(LinearNoBias(), input_data=[input2d, weight1d]) @@ -3981,6 +3983,7 @@ def forward(self, x): test_forward_logsoftmax() test_forward_sigmoid() test_forward_dense() + test_forward_linear() test_forward_avgpool1d() test_forward_avgpool2d() test_forward_avgpool3d() diff --git a/tests/python/relay/test_op_level3.py b/tests/python/relay/test_op_level3.py index 95b0dfe96304..03e7101e8354 100644 --- a/tests/python/relay/test_op_level3.py +++ b/tests/python/relay/test_op_level3.py @@ -293,6 +293,7 @@ def verify_reshape(shape, newshape, oshape): verify_reshape((2, 3, 4), (-3, -2), (6, 4)) verify_reshape((2, 3, 4), (-4, 1, 2, -2), (1, 2, 3, 4)) verify_reshape((2, 3, 4), (2, -4, -1, 3, -2), (2, 1, 3, 4)) + verify_reshape((1,), (), ()) def test_reshape_fail(): From 15e2d135348b06e78614e48baa1474901b015621 Mon Sep 17 00:00:00 2001 From: Haozheng Fan Date: Tue, 3 Aug 2021 19:06:33 +0000 Subject: [PATCH 2/3] fix --- python/tvm/relay/frontend/pytorch.py | 9 ++++++++- tests/python/frontend/pytorch/test_forward.py | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index b528fff8d455..73c3caffbb6f 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -1445,7 +1445,14 @@ def linear(self, inputs, input_types): # 0 - input # 1 - weight bias = inputs[2] - mm_out = self.matmul([inputs[0], _op.transpose(inputs[1], axes=(1, 0))], input_types[:2]) + a_shape = self.infer_shape_with_prelude(inputs[0]) + b_shape = self.infer_shape_with_prelude(inputs[1]) + if len(a_shape) == 2 and len(b_shape) == 2: + mm_out = _op.nn.dense(inputs[0], inputs[1]) + elif len(b_shape) == 1: + mm_out = self.matmul([inputs[0], inputs[1]], input_types[:2]) + else: + mm_out = self.matmul([inputs[0], _op.transpose(inputs[1], axes=(1, 0))], input_types[:2]) if isinstance(bias, _expr.Expr): bias_ndims = len(self.infer_shape_with_prelude(bias)) if bias_ndims == 1: diff --git a/tests/python/frontend/pytorch/test_forward.py b/tests/python/frontend/pytorch/test_forward.py index c981c0552cd7..e58575266414 100644 --- a/tests/python/frontend/pytorch/test_forward.py +++ b/tests/python/frontend/pytorch/test_forward.py @@ -1569,6 +1569,7 @@ def forward(self, input, weight): return F.linear(input, weight) input2d = torch.rand([2, 2]).float() + input3d = torch.rand([4, 3, 2]).float() weight1d = torch.rand([2]).float() weight2d = torch.rand([2, 2]).float() weight3x2 = torch.rand([3, 2]).float() @@ -1584,6 +1585,8 @@ def forward(self, input, weight): # 2D input, 1D weight, 1D bias is not supported by torch.linear() # 2D input, 1D weight, no bias verify_model(LinearNoBias(), input_data=[input2d, weight1d]) + # 3D input, 2D weight, no bias + verify_model(LinearNoBias(), input_data=[input3d, weight3x2]) # TODO: Add the following cases when matmul(1D, _) is supported by TVM # 1D input, 2D weight, 1D bias # 1D input, 2D weight, no bias From d3a3ae1c6ef868f8439dedeb890720d8127e6ce5 Mon Sep 17 00:00:00 2001 From: Haozheng Fan Date: Tue, 3 Aug 2021 19:17:13 +0000 Subject: [PATCH 3/3] lint --- python/tvm/relay/frontend/pytorch.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/pytorch.py b/python/tvm/relay/frontend/pytorch.py index 73c3caffbb6f..40b95d1d1f71 100644 --- a/python/tvm/relay/frontend/pytorch.py +++ b/python/tvm/relay/frontend/pytorch.py @@ -1452,7 +1452,9 @@ def linear(self, inputs, input_types): elif len(b_shape) == 1: mm_out = self.matmul([inputs[0], inputs[1]], input_types[:2]) else: - mm_out = self.matmul([inputs[0], _op.transpose(inputs[1], axes=(1, 0))], input_types[:2]) + mm_out = self.matmul( + [inputs[0], _op.transpose(inputs[1], axes=(1, 0))], input_types[:2] + ) if isinstance(bias, _expr.Expr): bias_ndims = len(self.infer_shape_with_prelude(bias)) if bias_ndims == 1: