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

Fix Python IndexError of case17: paddle.nn.functional.interpolate #49992

Merged
merged 5 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions python/paddle/fluid/tests/unittests/test_bicubic_interp_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ def test_input_shape():
x, size=[12, 12], mode='BICUBIC', align_corners=False
)

def test_size_shape():
x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32")
out = interpolate(
x, size=[12], mode='BICUBIC', align_corners=False
)

def test_align_corcers():
x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32")
interpolate(x, size=[12, 12], mode='BICUBIC', align_corners=3)
Expand Down Expand Up @@ -481,6 +487,7 @@ def test_outshape_and_scale():

self.assertRaises(ValueError, test_mode_type)
self.assertRaises(ValueError, test_input_shape)
self.assertRaises(ValueError, test_size_shape)
self.assertRaises(TypeError, test_align_corcers)
self.assertRaises(ValueError, test_attr_data_format)
self.assertRaises(TypeError, test_actual_shape)
Expand Down
17 changes: 17 additions & 0 deletions python/paddle/fluid/tests/unittests/test_bicubic_interp_v2_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,20 @@ def test_size_type():
x, size={2, 2}, mode='bicubic', align_corners=False
)

def test_size_length():
x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32")
out = interpolate(x, size=[2], mode='bicubic', align_corners=False)

def test_size_tensor_ndim():
x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32")
size = paddle.to_tensor(np.array([[2, 2]]))
out = interpolate(x, size=size, mode='bicubic', align_corners=False)

def test_size_tensor_length():
x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32")
size = paddle.to_tensor(np.array([2]))
out = interpolate(x, size=size, mode='bicubic', align_corners=False)

def test_input_shape_1():
x = fluid.data(name="x", shape=[2, 1, 0, 0], dtype="float32")
out = interpolate(
Expand All @@ -633,6 +647,9 @@ def test_input_shape_1():
self.assertRaises(ValueError, test_size_and_scale)
self.assertRaises(ValueError, test_size_and_scale2)
self.assertRaises(TypeError, test_size_type)
self.assertRaises(ValueError, test_size_length)
self.assertRaises(ValueError, test_size_tensor_ndim)
self.assertRaises(ValueError, test_size_tensor_length)
self.assertRaises(ValueError, test_input_shape_1)

def test_errors(self):
Expand Down
17 changes: 17 additions & 0 deletions python/paddle/nn/functional/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,23 @@ def interpolate(
if size is None and scale_factor is None:
raise ValueError("One of size and scale_factor must not be None.")

if (isinstance(size, list) or isinstance(size, tuple)) and len(
size
) != x.ndim - 2:
raise ValueError(
'The x and size should satisfy rank(x) - 2 == len(size).'
)

if isinstance(size, Variable):
if size.ndim != 1:
raise ValueError(
f"If size is a tensor, it's rank must be 1, but received {size.ndim}."
)
if size.shape[0] != x.ndim - 2:
raise ValueError(
'The x and size should satisfy rank(x) - 2 == size.shape[0].'
)

if not isinstance(align_corners, bool):
raise TypeError("Attr align_corners should be a bool value")

Expand Down