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 pad outliers err #34979

Merged
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 paddle/fluid/operators/pad3d_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,13 @@ class Pad3dCPUKernel : public framework::OpKernel<T> {
in_width, pads[1]));
}

if (mode == "circular") {
PADDLE_ENFORCE_NE(
in_depth * in_height * in_width, 0,
platform::errors::InvalidArgument(
"The input tensor size can not be 0 for circular padding mode."));
}

const int pad_left = pads[0];
const int pad_top = pads[2];
const int pad_front = pads[4];
Expand Down
7 changes: 7 additions & 0 deletions paddle/fluid/operators/pad3d_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,13 @@ class Pad3dCUDAKernel : public framework::OpKernel<T> {
in_width, pads[1]));
}

if (mode == "circular") {
PADDLE_ENFORCE_NE(
in_depth * in_height * in_width, 0,
platform::errors::InvalidArgument(
"The input tensor size can not be 0 for circular padding mode."));
}

const int pad_left = pads[0];
const int pad_top = pads[2];
const int pad_front = pads[4];
Expand Down
64 changes: 41 additions & 23 deletions python/paddle/fluid/tests/unittests/test_pad3d_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,46 +682,64 @@ def test_class(self):


class TestPad3dOpError(unittest.TestCase):
def setUp(self):
self.places = [paddle.CPUPlace()]
if core.is_compiled_with_cuda():
self.places.append(paddle.CUDAPlace(0))

def test_errors(self):
def test_variable():
input_shape = (1, 2, 3, 4, 5)
data = np.random.rand(*input_shape).astype(np.float32)
F.pad(x=data, paddings=[1, 1, 1, 1, 1, 1])
y = F.pad(x=data, pad=[1, 1, 1, 1, 1, 1], data_format="NCDHW")

def test_reflect_1():
input_shape = (1, 2, 3, 4, 5)
data = np.random.rand(*input_shape).astype(np.float32)
x = paddle.fluid.data(name="x", shape=input_shape)
y = F.pad(x, pad=[5, 6, 1, 1, 1, 1], value=1, mode='reflect')
place = paddle.CPUPlace()
exe = Executor(place)
outputs = exe.run(feed={'x': data}, fetch_list=[y.name])
x = paddle.to_tensor(data)
y = F.pad(x,
pad=[5, 6, 1, 1, 1, 1],
value=1,
mode='reflect',
data_format="NCDHW")

def test_reflect_2():
input_shape = (1, 2, 3, 4, 5)
data = np.random.rand(*input_shape).astype(np.float32)
x = paddle.fluid.data(name="x", shape=input_shape)
y = F.pad(x, pad=[1, 1, 4, 3, 1, 1], value=1, mode='reflect')
place = paddle.CPUPlace()
exe = Executor(place)
outputs = exe.run(feed={'x': data}, fetch_list=[y.name])
x = paddle.to_tensor(data)
y = F.pad(x,
pad=[1, 1, 4, 3, 1, 1],
value=1,
mode='reflect',
data_format="NCDHW")

def test_reflect_3():
input_shape = (1, 2, 3, 4, 5)
data = np.random.rand(*input_shape).astype(np.float32)
x = paddle.fluid.data(name="x", shape=input_shape)
y = F.pad(x, pad=[1, 1, 1, 1, 2, 3], value=1, mode='reflect')
place = paddle.CPUPlace()
exe = Executor(place)
outputs = exe.run(feed={'x': data}, fetch_list=[y.name])

self.assertRaises(TypeError, test_variable)

self.assertRaises(Exception, test_reflect_1)

self.assertRaises(Exception, test_reflect_2)
x = paddle.to_tensor(data)
y = F.pad(x,
pad=[1, 1, 1, 1, 2, 3],
value=1,
mode='reflect',
data_format="NCDHW")

def test_circular_1():
input_shape = (1, 2, 0, 4, 5)
data = np.random.rand(*input_shape).astype(np.float32)
x = paddle.to_tensor(data)
y = F.pad(x,
pad=[1, 1, 1, 1, 2, 3],
mode='circular',
data_format="NCDHW")

self.assertRaises(Exception, test_reflect_3)
paddle.disable_static()
for place in self.places:
self.assertRaises(ValueError, test_variable)
self.assertRaises(Exception, test_reflect_1)
self.assertRaises(Exception, test_reflect_2)
self.assertRaises(Exception, test_reflect_3)
self.assertRaises(Exception, test_circular_1)
paddle.enable_static()


class TestPadDataformatError(unittest.TestCase):
Expand Down
32 changes: 25 additions & 7 deletions python/paddle/nn/functional/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,12 +1160,13 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None):

Parameters:
x (Tensor): The input tensor with data type float32/double/int32/int64_t.
pad (Tensor | List[int32]): The padding size with data type int32. [len(padding)/2] dimensions
of input will be padded. 1. If input dimension is 3, then the pad has the form (pad_left,
pad (Tensor | List[int] | Tuple[int]): The padding size with data type int.
If mode is 'constant' and length of pad is twice as length of x dimension, then x will
be padded from the first dimension to the last dimension.
Else: 1. If input dimension is 3, then the pad has the form (pad_left,
pad_right). 2. If the input dimension is 4, then the pad has the form (pad_left, pad_right,
pad_top, pad_bottom). 3. If the input dimension is 5, then the pad has the form
(pad_left, pad_right, pad_top, pad_bottom, pad_front, pad_back).

mode (str): Four modes: 'constant' (default), 'reflect', 'replicate', 'circular'.
When in 'constant' mode, this op uses a constant value to pad the input tensor.
When in 'reflect' mode, uses reflection of the input boundaries to pad the input tensor.
Expand All @@ -1189,6 +1190,15 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None):
[4., 5., 6.]]]]]

Case 0:
pad = [0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
mode = 'constant'
value = 0
Out = [[[[[0., 0., 0.],
[1., 2., 3.],
[4., 5., 6.],
[0., 0., 0.]]]]]

Case 1:
pad = [2, 2, 1, 1, 0, 0],
mode = 'constant'
value = 0
Expand All @@ -1197,23 +1207,23 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None):
[0. 0. 4. 5. 6. 0. 0.]
[0. 0. 0. 0. 0. 0. 0.]]]]]

Case 1:
Case 2:
pad = [2, 2, 1, 1, 0, 0],
mode = 'reflect'
Out = [[[[[6. 5. 4. 5. 6. 5. 4.]
[3. 2. 1. 2. 3. 2. 1.]
[6. 5. 4. 5. 6. 5. 4.]
[3. 2. 1. 2. 3. 2. 1.]]]]]

Case 2:
Case 3:
pad = [2, 2, 1, 1, 0, 0],
mode = 'replicate'
Out = [[[[[1. 1. 1. 2. 3. 3. 3.]
[1. 1. 1. 2. 3. 3. 3.]
[4. 4. 4. 5. 6. 6. 6.]
[4. 4. 4. 5. 6. 6. 6.]]]]]

Case 3:
Case 4:
pad = [2, 2, 1, 1, 0, 0],
mode = 'circular'
Out = [[[[[5. 6. 4. 5. 6. 4. 5.]
Expand All @@ -1231,11 +1241,18 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None):
# example 1
x_shape = (1, 1, 3)
x = paddle.arange(np.prod(x_shape), dtype="float32").reshape(x_shape) + 1
y = F.pad(x, [2, 3], value=1, mode='constant', data_format="NCL")
y = F.pad(x, [0, 0, 0, 0, 2, 3], value=1, mode='constant', data_format="NCL")
print(y)
# [[[1. 1. 1. 2. 3. 1. 1. 1.]]]

# example 2
x_shape = (1, 1, 3)
x = paddle.arange(np.prod(x_shape), dtype="float32").reshape(x_shape) + 1
y = F.pad(x, [2, 3], value=1, mode='constant', data_format="NCL")
print(y)
# [[[1. 1. 1. 2. 3. 1. 1. 1.]]]

# example 3
x_shape = (1, 1, 2, 3)
x = paddle.arange(np.prod(x_shape), dtype="float32").reshape(x_shape) + 1
y = F.pad(x, [1, 2, 1, 1], value=1, mode='circular')
Expand Down Expand Up @@ -1295,6 +1312,7 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None):
unsqueezed_dim = [1]
x = unsqueeze(x, axis=unsqueezed_dim)
else:
pad = list(pad)
if data_format in ["NCL", "NCHW", "NCDHW"]:
data_format = "NCDHW"
if x_dim == 3:
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/nn/layer/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


def _npairs(x, n):
if isinstance(x, (paddle.Tensor, list)):
if isinstance(x, (paddle.Tensor, list, tuple)):
return x
x = [x] * (n * 2)
return x
Expand Down