From 8fb2dce9e6bb3d9d5424d397c689cea47dd85cbd Mon Sep 17 00:00:00 2001 From: LoneRanger <836253168@qq.com> Date: Mon, 6 Feb 2023 16:47:12 +0800 Subject: [PATCH] Fix Python IndexError of Case12: paddle.static.nn.bilinear_tensor_product (#50008) --- .../unittests/test_bilinear_tensor_product_op.py | 11 +++++++++++ python/paddle/static/nn/common.py | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_bilinear_tensor_product_op.py b/python/paddle/fluid/tests/unittests/test_bilinear_tensor_product_op.py index 77416dd982..350b6ca72f 100644 --- a/python/paddle/fluid/tests/unittests/test_bilinear_tensor_product_op.py +++ b/python/paddle/fluid/tests/unittests/test_bilinear_tensor_product_op.py @@ -34,6 +34,17 @@ class TestDygraphBilinearTensorProductAPIError(unittest.TestCase): x1 = fluid.data(name='x1', shape=[-1, 5], dtype="float16") x2 = fluid.data(name='x2', shape=[-1, 4], dtype="float32") self.assertRaises(TypeError, layer, x1, x2) + # the dimensions of x and y must be 2 + paddle.enable_static() + x3 = paddle.static.data("", shape=[0], dtype="float32") + x4 = paddle.static.data("", shape=[0], dtype="float32") + self.assertRaises( + ValueError, + paddle.static.nn.bilinear_tensor_product, + x3, + x4, + 1000, + ) class TestBilinearTensorProductOp(OpTest): diff --git a/python/paddle/static/nn/common.py b/python/paddle/static/nn/common.py index f70a19e65f..41a2d944d2 100644 --- a/python/paddle/static/nn/common.py +++ b/python/paddle/static/nn/common.py @@ -2574,7 +2574,12 @@ def bilinear_tensor_product( """ helper = LayerHelper('bilinear_tensor_product', **locals()) dtype = helper.input_dtype('x') - + if len(x.shape) != 2 or len(y.shape) != 2: + raise ValueError( + "Input x and y should be 2D tensor, but received x with the shape of {}, y with the shape of {}".format( + x.shape, y.shape + ) + ) param_shape = [size, x.shape[1], y.shape[1]] w = helper.create_parameter( -- GitLab