Skip to content

Commit 669c0df

Browse files
author
Yibing Liu
committed
Add groups for conv transpose
1 parent 8b1b756 commit 669c0df

File tree

3 files changed

+103
-42
lines changed

3 files changed

+103
-42
lines changed

paddle/fluid/operators/conv_transpose_op.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void ConvTransposeOp::InferShape(framework::InferShapeContext* ctx) const {
3232
std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides");
3333
std::vector<int> paddings = ctx->Attrs().Get<std::vector<int>>("paddings");
3434
std::vector<int> dilations = ctx->Attrs().Get<std::vector<int>>("dilations");
35+
int groups = ctx->Attrs().Get<int>("groups");
3536

3637
PADDLE_ENFORCE(in_dims.size() == 4 || in_dims.size() == 5,
3738
"ConvTransposeOp intput should be 4-D or 5-D tensor.");
@@ -48,10 +49,10 @@ void ConvTransposeOp::InferShape(framework::InferShapeContext* ctx) const {
4849
"ConvTransposeOp paddings dimension and dilations "
4950
"dimension should be the same.");
5051
PADDLE_ENFORCE_EQ(in_dims[1], filter_dims[0],
51-
"In ConvTransposeOp, The input channel should be the same "
52-
"as the number of filters.");
52+
"In ConvTransposeOp, The number of input channels should "
53+
"be equal to the number of filter' channels.");
5354

54-
std::vector<int64_t> output_shape({in_dims[0], filter_dims[1]});
55+
std::vector<int64_t> output_shape({in_dims[0], filter_dims[1] * groups});
5556
for (size_t i = 0; i < strides.size(); ++i) {
5657
auto filter_extent = dilations[i] * (filter_dims[i + 2] - 1) + 1;
5758
output_shape.push_back((in_dims[i + 2] - 1) * strides[i] - 2 * paddings[i] +
@@ -102,7 +103,10 @@ void Conv2DTransposeOpMaker::Make() {
102103
AddOutput("Output",
103104
"(Tensor) The output tensor of convolution transpose operator. "
104105
"The format of output tensor is also NCHW.");
105-
106+
AddAttr<int>("groups",
107+
"(int default:1), the groups number of the convolution "
108+
"transpose operator. ")
109+
.SetDefault(1);
106110
AddAttr<std::vector<int>>("dilations",
107111
"(vector<int> default:{1, 1}), the "
108112
"dilations(h_dilation, w_dilation) of convolution "

paddle/fluid/operators/conv_transpose_op.h

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class GemmConvTransposeKernel : public framework::OpKernel<T> {
7070
std::vector<int> strides = context.Attr<std::vector<int>>("strides");
7171
std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
7272
std::vector<int> dilations = context.Attr<std::vector<int>>("dilations");
73-
// groups will alway be disabled in conv2dtranspose.
73+
int groups = context.Attr<int>("groups");
7474

7575
const int batch_size = static_cast<int>(input->dims()[0]);
7676

@@ -81,18 +81,18 @@ class GemmConvTransposeKernel : public framework::OpKernel<T> {
8181

8282
// use col_shape in the im2col and col2im (or vol2col and col2vol)
8383
// calculation
84-
// col_shape_vec: {c, k_h, k_w, h, w} or {c, k_d, k_h, k_w, d, h, w}
84+
// col_shape_vec: {c/g, k_h, k_w, h, w} or {c/g, k_d, k_h, k_w, d, h, w}
8585
size_t data_dim = filter_shape_vec.size() - 2;
8686
std::vector<int64_t> col_shape_vec(1 + 2 * data_dim);
87-
col_shape_vec[0] = output->dims()[1];
87+
col_shape_vec[0] = output->dims()[1] / groups;
8888
for (size_t j = 0; j < data_dim; ++j) {
8989
col_shape_vec[j + 1] = filter_shape_vec[j + 2];
9090
col_shape_vec[j + 1 + data_dim] = input_shape_vec[j + 2];
9191
}
9292
DDim col_shape(framework::make_ddim(col_shape_vec));
9393

9494
// use col_matrix_shape in the gemm calculation
95-
// size: (c * k_h * k_w, h * w) or (c * k_d * k_h * k_w, d * h * w)
95+
// size: (c/g * k_h * k_w, h * w) or (c/g * k_d * k_h * k_w, d * h * w)
9696
DDim col_matrix_shape = framework::flatten_to_2d(col_shape, data_dim + 1);
9797

9898
Tensor col;
@@ -111,7 +111,7 @@ class GemmConvTransposeKernel : public framework::OpKernel<T> {
111111
// input matrix size: (m, h * w) or (m, d * h * w)
112112
DDim input_matrix_shape = {input->dims()[1], col_matrix_shape[1]};
113113

114-
// filter size: (m, c * k_h * k_w) or (m, c * k_d * k_h * k_w)
114+
// filter size: (m, c/g * k_h * k_w) or (m, c/g * k_d * k_h * k_w)
115115
DDim filter_matrix_shape = {input->dims()[1], col_matrix_shape[0]};
116116
filter.Resize(filter_matrix_shape);
117117

@@ -121,6 +121,8 @@ class GemmConvTransposeKernel : public framework::OpKernel<T> {
121121
auto blas = math::GetBlas<DeviceContext, T>(dev_ctx);
122122
set_zero(dev_ctx, output, static_cast<T>(0));
123123

124+
int in_step = static_cast<int>(input->dims()[1]) / groups;
125+
int out_step = static_cast<int>(output->dims()[1]) / groups;
124126
math::Col2ImFunctor<math::ColFormat::kCFO, DeviceContext, T> col2im;
125127
math::Col2VolFunctor<DeviceContext, T> col2vol;
126128

@@ -133,22 +135,29 @@ class GemmConvTransposeKernel : public framework::OpKernel<T> {
133135
// output size: (c, o_h, o_w) or (c, o_d, o_h, o_w)
134136
Tensor output_batch = output->Slice(i, i + 1).Resize(output_shape);
135137

136-
// col_matrix = filter * input_batch
137-
// of shape (c * k_h * k_w, h * w) or (c * k_d * k_h * k_w, d * h * w)
138-
blas.MatMul(filter, true, input_batch, false, static_cast<T>(1.0),
139-
&col_matrix, static_cast<T>(0.0));
140-
141-
if (data_dim == 2U) {
142-
// col2im: col_matrix -> dy
143-
// from (c * k_h * k_w, h * w) to (c, o_h, o_w)
144-
col2im(dev_ctx, col, dilations, strides,
145-
std::vector<int>{paddings[0], paddings[1], paddings[0],
146-
paddings[1]},
147-
&output_batch);
148-
} else if (data_dim == 3U) {
149-
// col2vol: col_matrix -> dy
150-
// from (c * k_d * k_h * k_w, d * h * w) to (c, o_d, o_h, o_w)
151-
col2vol(dev_ctx, col, dilations, strides, paddings, &output_batch);
138+
for (int g = 0; g < groups; g++) {
139+
Tensor in_slice = input_batch.Slice(g * in_step, (g + 1) * in_step);
140+
Tensor filter_slice = filter.Slice(g * in_step, (g + 1) * in_step);
141+
Tensor out_slice = output_batch.Slice(g * out_step, (g + 1) * out_step);
142+
143+
// col_matrix = filter_slice * input_slice
144+
// of shape (c/g * k_h * k_w, h * w)
145+
// or (c/g * k_d * k_h * k_w, d * h * w)
146+
blas.MatMul(filter_slice, true, in_slice, false, static_cast<T>(1.0),
147+
&col_matrix, static_cast<T>(0.0));
148+
149+
if (data_dim == 2U) {
150+
// col2im: col_matrix -> dy
151+
// from (c/g * k_h * k_w, h * w) to (c/g, o_h, o_w)
152+
col2im(dev_ctx, col, dilations, strides,
153+
std::vector<int>{paddings[0], paddings[1], paddings[0],
154+
paddings[1]},
155+
&out_slice);
156+
} else if (data_dim == 3U) {
157+
// col2vol: col_matrix -> dy
158+
// from (c/g * k_d * k_h * k_w, d * h * w) to (c/g, o_d, o_h, o_w)
159+
col2vol(dev_ctx, col, dilations, strides, paddings, &out_slice);
160+
}
152161
}
153162
}
154163
}
@@ -174,6 +183,7 @@ class GemmConvTransposeGradKernel : public framework::OpKernel<T> {
174183
std::vector<int> strides = context.Attr<std::vector<int>>("strides");
175184
std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
176185
std::vector<int> dilations = context.Attr<std::vector<int>>("dilations");
186+
int groups = context.Attr<int>("groups");
177187

178188
const int batch_size = static_cast<int>(input->dims()[0]);
179189

@@ -205,9 +215,11 @@ class GemmConvTransposeGradKernel : public framework::OpKernel<T> {
205215
// input matrix size: (m, h * w) or (m, d * h * w)
206216
DDim input_matrix_shape = {input->dims()[1], col_matrix_shape[1]};
207217

208-
// filter size: (m, c * k_h * k_w) or (m, c * k_d * k_h * k_w)
209-
DDim filter_matrix_shape = {input->dims()[1], col_matrix_shape[0]};
218+
// filter size: (m, c/g * k_h * k_w) or (m, c/g * k_d * k_h * k_w)
219+
DDim filter_matrix_shape = {input->dims()[1], col_matrix_shape[0] / groups};
210220
filter.Resize(filter_matrix_shape);
221+
int in_step = static_cast<int>(input->dims()[1]) / groups;
222+
int col_step = static_cast<int>(col_matrix_shape[0]) / groups;
211223

212224
// convolution transpose grad on input:
213225
// im2col + gemm (similar to conv-forward)
@@ -233,7 +245,7 @@ class GemmConvTransposeGradKernel : public framework::OpKernel<T> {
233245
if (input_grad) {
234246
input_grad->mutable_data<T>(context.GetPlace());
235247
}
236-
if (filter_grad) { // filter size (m, c, k_h, k_w)
248+
if (filter_grad) { // filter size (m, c/g, k_h, k_w)
237249
filter_grad->mutable_data<T>(context.GetPlace());
238250
set_zero(dev_ctx, filter_grad, static_cast<T>(0));
239251
filter_grad_ = *filter_grad;
@@ -268,8 +280,17 @@ class GemmConvTransposeGradKernel : public framework::OpKernel<T> {
268280
// or
269281
// (m, c * k_d * k_h * k_w) * (c * k_d * k_h * k_w, d * h * w) -> (m,
270282
// d, h, w)
271-
blas.MatMul(filter, false, col_matrix, false, static_cast<T>(1.0),
272-
&input_grad_batch, static_cast<T>(0.0));
283+
for (int g = 0; g < groups; g++) {
284+
Tensor input_grad_slice =
285+
input_grad_batch.Slice(g * in_step, (g + 1) * in_step);
286+
Tensor filter_slice = filter.Slice(g * in_step, (g + 1) * in_step);
287+
Tensor col_matrix_slice =
288+
col_matrix.Slice(g * col_step, (g + 1) * col_step);
289+
290+
blas.MatMul(filter_slice, false, col_matrix_slice, false,
291+
static_cast<T>(1.0), &input_grad_slice,
292+
static_cast<T>(0.0));
293+
}
273294
}
274295
if (filter_grad) {
275296
// input batch
@@ -279,8 +300,17 @@ class GemmConvTransposeGradKernel : public framework::OpKernel<T> {
279300
// or
280301
// (m, d * h * w) * (d * h * w, c * k_d * k_h * k_w) -> (m, c * k_d *
281302
// k_h * k_w)
282-
blas.MatMul(in_batch, false, col_matrix, true, static_cast<T>(1.0),
283-
&filter_grad_, static_cast<T>(1.0));
303+
for (int g = 0; g < groups; g++) {
304+
Tensor in_batch_slice =
305+
in_batch.Slice(g * in_step, (g + 1) * in_step);
306+
Tensor filter_grad_slice =
307+
filter_grad_.Slice(g * in_step, (g + 1) * in_step);
308+
Tensor col_matrix_slice =
309+
col_matrix.Slice(g * col_step, (g + 1) * col_step);
310+
blas.MatMul(in_batch_slice, false, col_matrix_slice, true,
311+
static_cast<T>(1.0), &filter_grad_slice,
312+
static_cast<T>(1.0));
313+
}
284314
}
285315
}
286316
}

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

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121

2222
def conv2dtranspose_forward_naive(input_, filter_, attrs):
2323
in_n, in_c, in_h, in_w = input_.shape
24-
f_c, out_c, f_h, f_w = filter_.shape
24+
f_c, f_out_c, f_h, f_w = filter_.shape
25+
groups = attrs['groups']
2526
assert in_c == f_c
27+
out_c = f_out_c * groups
28+
sub_in_c = in_c / groups
2629

2730
stride, pad, dilations = attrs['strides'], attrs['paddings'], attrs[
2831
'dilations']
@@ -36,15 +39,21 @@ def conv2dtranspose_forward_naive(input_, filter_, attrs):
3639
for n in range(in_n):
3740
for i in range(in_h):
3841
for j in range(in_w):
39-
input_masked = input_[n, :, i, j] # (c)
40-
input_masked = np.reshape(input_masked, (in_c, 1, 1))
41-
input_masked = np.tile(input_masked, (1, f_h, f_w))
42-
43-
for k in range(out_c):
44-
tmp_out = np.sum(input_masked * filter_[:, k, :, :], axis=0)
45-
i1, i2 = i * stride[0], i * stride[0] + d_bolck_h
46-
j1, j2 = j * stride[0], j * stride[0] + d_bolck_h
47-
out[n, k, i1:i2:dilations[0], j1:j2:dilations[1]] += tmp_out
42+
for g in range(groups):
43+
input_masked = input_[n, g * sub_in_c:(g + 1) * sub_in_c, i,
44+
j] # (c)
45+
input_masked = np.reshape(input_masked, (sub_in_c, 1, 1))
46+
input_masked = np.tile(input_masked, (1, f_h, f_w))
47+
48+
for k in range(f_out_c):
49+
tmp_out = np.sum(
50+
input_masked *
51+
filter_[g * sub_in_c:(g + 1) * sub_in_c, k, :, :],
52+
axis=0)
53+
i1, i2 = i * stride[0], i * stride[0] + d_bolck_h
54+
j1, j2 = j * stride[0], j * stride[0] + d_bolck_h
55+
out[n, g * f_out_c + k, i1:i2:dilations[0], j1:j2:
56+
dilations[1]] += tmp_out
4857

4958
out = out[:, :, pad[0]:out_h - pad[0], pad[1]:out_w - pad[1]]
5059
return out
@@ -64,6 +73,7 @@ def setUp(self):
6473
self.attrs = {
6574
'strides': self.stride,
6675
'paddings': self.pad,
76+
'groups': self.groups,
6777
'dilations': self.dilations,
6878
'use_cudnn': self.use_cudnn,
6979
'data_format': 'AnyLayout' # TODO(dzhwinter) : should be fix latter
@@ -127,6 +137,7 @@ def init_test_case(self):
127137
self.pad = [0, 0]
128138
self.stride = [1, 1]
129139
self.dilations = [1, 1]
140+
self.groups = 1
130141
self.input_size = [2, 3, 5, 5] # NCHW
131142
f_c = self.input_size[1]
132143
self.filter_size = [f_c, 6, 3, 3]
@@ -140,16 +151,29 @@ def init_test_case(self):
140151
self.pad = [1, 1]
141152
self.stride = [1, 1]
142153
self.dilations = [1, 1]
154+
self.groups = 1
143155
self.input_size = [2, 3, 5, 5] # NCHW
144156
f_c = self.input_size[1]
145157
self.filter_size = [f_c, 6, 3, 3]
146158

147159

160+
class TestWithGroups(TestConv2dTransposeOp):
161+
def init_test_case(self):
162+
self.pad = [1, 1]
163+
self.stride = [1, 1]
164+
self.dilations = [1, 1]
165+
self.groups = 2
166+
self.input_size = [2, 4, 5, 5] # NCHW
167+
f_c = self.input_size[1]
168+
self.filter_size = [f_c, 3, 3, 3]
169+
170+
148171
class TestWithStride(TestConv2dTransposeOp):
149172
def init_test_case(self):
150173
self.pad = [1, 1]
151174
self.stride = [2, 2]
152175
self.dilations = [1, 1]
176+
self.groups = 1
153177
self.input_size = [2, 3, 5, 5] # NCHW
154178
f_c = self.input_size[1]
155179
self.filter_size = [f_c, 6, 3, 3]
@@ -159,6 +183,7 @@ class TestWithDilation(TestConv2dTransposeOp):
159183
def init_test_case(self):
160184
self.pad = [1, 1]
161185
self.stride = [1, 1]
186+
self.groups = 1
162187
self.dilations = [2, 2]
163188
self.input_size = [2, 3, 5, 5] # NCHW
164189
f_c = self.input_size[1]
@@ -176,6 +201,7 @@ class TestCUDNNWithPad(TestWithPad):
176201
def init_test_case(self):
177202
self.pad = [1, 1]
178203
self.stride = [1, 1]
204+
self.groups = 1
179205
self.dilations = [1, 1]
180206
self.input_size = [2, 3, 5, 5] # NCHW
181207
f_c = self.input_size[1]
@@ -190,6 +216,7 @@ class TestCUDNNWithStride(TestWithStride):
190216
def init_test_case(self):
191217
self.pad = [1, 1]
192218
self.stride = [2, 2]
219+
self.groups = 1
193220
self.dilations = [1, 1]
194221
self.input_size = [2, 3, 5, 5] # NCHW
195222
f_c = self.input_size[1]

0 commit comments

Comments
 (0)