Skip to content

Commit 8fb2dce

Browse files
authored
Fix Python IndexError of Case12: paddle.static.nn.bilinear_tensor_product (#50008)
1 parent b8e6ca9 commit 8fb2dce

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

python/paddle/fluid/tests/unittests/test_bilinear_tensor_product_op.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ def test_errors(self):
3434
x1 = fluid.data(name='x1', shape=[-1, 5], dtype="float16")
3535
x2 = fluid.data(name='x2', shape=[-1, 4], dtype="float32")
3636
self.assertRaises(TypeError, layer, x1, x2)
37+
# the dimensions of x and y must be 2
38+
paddle.enable_static()
39+
x3 = paddle.static.data("", shape=[0], dtype="float32")
40+
x4 = paddle.static.data("", shape=[0], dtype="float32")
41+
self.assertRaises(
42+
ValueError,
43+
paddle.static.nn.bilinear_tensor_product,
44+
x3,
45+
x4,
46+
1000,
47+
)
3748

3849

3950
class TestBilinearTensorProductOp(OpTest):

python/paddle/static/nn/common.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2574,7 +2574,12 @@ def bilinear_tensor_product(
25742574
"""
25752575
helper = LayerHelper('bilinear_tensor_product', **locals())
25762576
dtype = helper.input_dtype('x')
2577-
2577+
if len(x.shape) != 2 or len(y.shape) != 2:
2578+
raise ValueError(
2579+
"Input x and y should be 2D tensor, but received x with the shape of {}, y with the shape of {}".format(
2580+
x.shape, y.shape
2581+
)
2582+
)
25782583
param_shape = [size, x.shape[1], y.shape[1]]
25792584

25802585
w = helper.create_parameter(

0 commit comments

Comments
 (0)