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 replicate pad when input size is 0 #36510

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
12 changes: 5 additions & 7 deletions paddle/fluid/operators/pad3d_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -565,13 +565,11 @@ class Pad3dCPUKernel : public framework::OpKernel<T> {
" in reflect mode"
", but received depth(%d) and pad_right(%d).",
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."));
} else if (mode == "circular" || mode == "replicate") {
PADDLE_ENFORCE_NE(in_depth * in_height * in_width, 0,
platform::errors::InvalidArgument(
"The input tensor size can not be 0 for circular "
"or replicate padding mode."));
}

const int pad_left = pads[0];
Expand Down
12 changes: 5 additions & 7 deletions paddle/fluid/operators/pad3d_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -618,13 +618,11 @@ class Pad3dCUDAKernel : public framework::OpKernel<T> {
" in reflect mode"
", but received depth(%d) and pad_right(%d).",
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."));
} else if (mode == "circular" || mode == "replicate") {
PADDLE_ENFORCE_NE(in_depth * in_height * in_width, 0,
platform::errors::InvalidArgument(
"The input tensor size can not be 0 for circular "
"or replicate padding mode."));
}

const int pad_left = pads[0];
Expand Down
10 changes: 10 additions & 0 deletions python/paddle/fluid/tests/unittests/test_pad3d_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,23 @@ def test_circular_1():
mode='circular',
data_format="NCDHW")

def test_replicate_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='replicate',
data_format="NCDHW")

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)
self.assertRaises(Exception, test_replicate_1)
paddle.enable_static()


Expand Down