Skip to content

Commit cf883d9

Browse files
author
chengduo
authored
Merge pull request #8613 from chengduoZH/feature/fix_bug_conv_op
Fix conv_op bug
2 parents f449180 + a779b42 commit cf883d9

File tree

7 files changed

+60
-10
lines changed

7 files changed

+60
-10
lines changed

paddle/fluid/operators/conv_op.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ void ConvOp::InferShape(framework::InferShapeContext* ctx) const {
5454

5555
std::vector<int64_t> output_shape({in_dims[0], filter_dims[0]});
5656
for (size_t i = 0; i < strides.size(); ++i) {
57-
PADDLE_ENFORCE(in_dims[i + 2] + 2 * paddings[i] -
58-
(dilations[i] * (filter_dims[i + 2] - 1) + 1) >
59-
0,
60-
"Due to the settings of paddings, filter_dims and "
61-
"dilations, the output size is less than 0, please check "
62-
"again.");
6357
output_shape.push_back(ConvOutputSize(in_dims[i + 2], filter_dims[i + 2],
6458
dilations[i], paddings[i],
6559
strides[i]));

paddle/fluid/operators/conv_op.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ using Tensor = framework::Tensor;
3131
inline int ConvOutputSize(int input_size, int filter_size, int dilation,
3232
int padding, int stride) {
3333
const int dkernel = dilation * (filter_size - 1) + 1;
34-
const int output_size = (input_size + 2 * padding - dkernel) / stride + 1;
34+
int output_size = (input_size + 2 * padding - dkernel) / stride + 1;
35+
PADDLE_ENFORCE(
36+
output_size > 0,
37+
"Due to the settings of padding(%d), filter_size(%d), dilation(%d) and "
38+
"stride(%d), the output size is less than 0, please check "
39+
"again. Input_size:%d",
40+
padding, filter_size, dilation, stride, input_size);
41+
3542
return output_size;
3643
}
3744
inline bool IsExpand(std::vector<int64_t>& filter_dim,

paddle/fluid/operators/pool_op.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ namespace operators {
1919

2020
int PoolOutputSize(int input_size, int filter_size, int padding, int stride) {
2121
int output_size = (input_size - filter_size + 2 * padding) / stride + 1;
22+
PADDLE_ENFORCE(output_size > 0,
23+
"Due to the settings of padding(%d), filter_size(%d) and "
24+
"stride(%d), the output size is less than 0, please check "
25+
"again. Input_size:%d",
26+
padding, filter_size, stride, input_size);
2227
return output_size;
2328
}
2429

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ def init_group(self):
210210
self.groups = 3
211211

212212

213+
class TestWithInput1x1Filter1x1(TestConv2dOp):
214+
def init_test_case(self):
215+
self.pad = [0, 0]
216+
self.stride = [1, 1]
217+
self.input_size = [2, 3, 1, 1] # NCHW
218+
assert np.mod(self.input_size[1], self.groups) == 0
219+
f_c = self.input_size[1] / self.groups
220+
self.filter_size = [6, f_c, 1, 1]
221+
222+
def init_group(self):
223+
self.groups = 3
224+
225+
213226
#----------------Conv2dCUDNN----------------
214227
class TestCUDNN(TestConv2dOp):
215228
def init_op_type(self):
@@ -241,6 +254,12 @@ def init_op_type(self):
241254
self.op_type = "conv2d"
242255

243256

257+
class TestCUDNNWithInput1x1Filter1x1(TestWithInput1x1Filter1x1):
258+
def init_op_type(self):
259+
self.use_cudnn = True
260+
self.op_type = "conv2d"
261+
262+
244263
class TestDepthwiseConv(TestConv2dOp):
245264
def init_test_case(self):
246265
self.pad = [1, 1]
@@ -265,7 +284,8 @@ def init_test_case(self):
265284
self.op_type = "depthwise_conv2d"
266285

267286

268-
# cudnn v5 does not support dilation conv.
287+
# Please Don't remove the following code.
288+
# Currently, CI use cudnn V5.0 which not support dilation conv.
269289
# class TestCUDNNWithDilation(TestWithDilation):
270290
# def init_op_type(self):
271291
# self.op_type = "conv_cudnn"

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ def init_op_type(self):
200200
self.op_type = "conv2d_transpose"
201201

202202

203-
# #cudnn v5 does not support dilation conv.
203+
# Please Don't remove the following code.
204+
# Currently, CI use cudnn V5.0 which not support dilation conv.
204205
# class TestCUDNNWithDilation(TestWithDilation):
205206
# def init_test_case(self):
206207
# self.pad = [1, 1]

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,22 @@ def init_group(self):
200200
self.groups = 3
201201

202202

203+
class TestWithInput1x1Filter1x1(TestConv3dOp):
204+
def init_test_case(self):
205+
self.pad = [0, 0, 0]
206+
self.stride = [1, 1, 1]
207+
self.input_size = [2, 3, 1, 1, 1] # NCHW
208+
assert np.mod(self.input_size[1], self.groups) == 0
209+
f_c = self.input_size[1] / self.groups
210+
self.filter_size = [6, f_c, 1, 1, 1]
211+
212+
def init_dilation(self):
213+
self.dilations = [1, 1, 1]
214+
215+
def init_group(self):
216+
self.groups = 3
217+
218+
203219
class TestWithDilation(TestConv3dOp):
204220
def init_test_case(self):
205221
self.pad = [0, 0, 0]
@@ -240,6 +256,12 @@ def init_op_type(self):
240256
self.op_type = "conv3d"
241257

242258

259+
class TestWithInput1x1Filter1x1CUDNN(TestWithInput1x1Filter1x1):
260+
def init_op_type(self):
261+
self.use_cudnn = True
262+
self.op_type = "conv3d"
263+
264+
243265
# FIXME(typhoonzero): find a way to determine if
244266
# using cudnn > 6 in python
245267
# class TestWithDilationCUDNN(TestWithDilation):

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ def init_op_type(self):
207207
self.op_type = "conv3d_transpose"
208208

209209

210-
# #cudnn v5 does not support dilation conv.
210+
# Please Don't remove the following code.
211+
# Currently, CI use cudnn V5.0 which not support dilation conv.
211212
# class TestCUDNNWithDilation(TestWithDilation):
212213
# def init_test_case(self):
213214
# self.pad = [1, 1, 1]

0 commit comments

Comments
 (0)