-
Notifications
You must be signed in to change notification settings - Fork 0
/
grouped_conv.py
299 lines (252 loc) · 9.68 KB
/
grouped_conv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import torch
def pytorch_conv2d(input, weight, bias, padding, stride, kernel, groups):
# perform grouped convolution using pytorch
output = torch.nn.functional.conv2d(
input, weight, bias, stride=stride, padding=padding, groups=groups
)
return output
def naive_grouped_conv2d_nchw(input, weight, bias, padding, stride, kernel, groups):
# input is of shape (batch_size, in_channels, in_height, in_width)
# output is of shape (batch_size, out_channels, out_height, out_width)
# bias is of shape (out_channels,)
# padding is of shape (padding_height, padding_width)
# stride is of shape (stride_height, stride_width)
# kernel is of shape (kernel_height, kernel_width)
# groups is an integer
batch_size, in_channels, in_height, in_width = input.shape
out_channels, _, kernel_height, kernel_width = weight.shape
padding_height, padding_width = padding
stride_height, stride_width = stride
# compute output height and width
out_height = (in_height + 2 * padding_height - kernel_height) // stride_height + 1
out_width = (in_width + 2 * padding_width - kernel_width) // stride_width + 1
# split input and weight into groups
input_groups = torch.split(input, in_channels // groups, dim=1)
weight_groups = torch.split(weight, out_channels // groups, dim=0)
# perform grouped convolution
output = torch.zeros(batch_size, out_channels, out_height, out_width)
for i in range(groups):
input_group = input_groups[i]
weight_group = weight_groups[i]
bias_group = bias[out_channels // groups * i: out_channels // groups * (i + 1)]
output_group = torch.nn.functional.conv2d(
input_group, weight_group, bias_group, stride=stride, padding=padding
)
output[:, out_channels // groups * i: out_channels // groups * (i + 1), :, :] = output_group
return output
def naive_grouped_conv2d_scalar_nchw(input, weight, bias, padding, stride, kernel, groups):
# input is of shape (batch_size, in_channels, in_height, in_width)
# output is of shape (batch_size, out_channels, out_height, out_width)
# filter is of shape (out_channels, in_channels, kernel_height, kernel_width)
# bias is of shape (out_channels,)
# padding is of shape (padding_height, padding_width)
# stride is of shape (stride_height, stride_width)
# kernel is of shape (kernel_height, kernel_width)
# groups is an integer
batch_size, in_channels, in_height, in_width = input.shape
out_channels, _, kernel_height, kernel_width = weight.shape
padding_height, padding_width = padding
stride_height, stride_width = stride
# compute output height and width
out_height = (in_height + 2 * padding_height - kernel_height) // stride_height + 1
out_width = (in_width + 2 * padding_width - kernel_width) // stride_width + 1
# split input and weight into groups
input_groups = torch.split(input, in_channels // groups, dim=1)
weight_groups = torch.split(weight, out_channels // groups, dim=0)
# perform grouped convolution
output = torch.zeros(batch_size, out_channels, out_height, out_width)
for i in range(groups):
input_group = input_groups[i]
weight_group = weight_groups[i]
bias_group = bias[out_channels // groups * i: out_channels // groups * (i + 1)]
for b in range(batch_size):
for c_out in range(out_channels // groups):
for h_out in range(out_height):
for w_out in range(out_width):
h_in = h_out * stride_height - padding_height
w_in = w_out * stride_width - padding_width
dot_product = 0
for c_in in range(in_channels // groups):
for h_k in range(kernel_height):
for w_k in range(kernel_width):
h = h_in + h_k
w = w_in + w_k
if h >= 0 and h < in_height and w >= 0 and w < in_width:
dot_product += input_group[b, c_in, h, w] * weight_group[c_out, c_in, h_k, w_k]
output[b, out_channels // groups * i + c_out, h_out, w_out] += dot_product + bias_group[c_out]
return output
def naive_grouped_conv2d_nhwc(input, weight, bias, padding, stride, kernel, groups):
# input is of shape (batch_size, in_height, in_width, in_channels)
# output is of shape (batch_size, out_height, out_width, out_channels)
# weight is of shape (out_channels, in_channels, kernel_height, kernel_width)
# bias is of shape (out_channels,)
# padding is of shape (padding_height, padding_width)
# stride is of shape (stride_height, stride_width)
# kernel is of shape (kernel_height, kernel_width)
# groups is an integer
batch_size, in_height, in_width, in_channels = input.shape
out_channels, _, kernel_height, kernel_width = weight.shape
padding_height, padding_width = padding
stride_height, stride_width = stride
# compute output height and width
out_height = (in_height + 2 * padding_height - kernel_height) // stride_height + 1
out_width = (in_width + 2 * padding_width - kernel_width) // stride_width + 1
# split input and weight into groups
input_groups = torch.split(input, in_channels // groups, dim=3)
weight_groups = torch.split(weight, out_channels // groups, dim=0)
# perform grouped convolution
output = torch.zeros(batch_size, out_height, out_width, out_channels)
for i in range(groups):
input_group = input_groups[i]
weight_group = weight_groups[i]
bias_group = bias[out_channels // groups * i: out_channels // groups * (i + 1)]
for b in range(batch_size):
for h_out in range(out_height):
for w_out in range(out_width):
dot_product = 0
for c_in in range(in_channels // groups):
for h_k in range(kernel_height):
for w_k in range(kernel_width):
h_in = h_out * stride_height - padding_height + h_k
w_in = w_out * stride_width - padding_width + w_k
if h_in >= 0 and h_in < in_height and w_in >= 0 and w_in < in_width:
dot_product += input_group[b, h_in, w_in, c_in] * weight_group[out_channels // groups * i: out_channels // groups * (i + 1), c_in, h_k, w_k]
output[b, h_out, w_out, out_channels // groups * i: out_channels // groups * (i + 1)] += dot_product + bias_group
return output
def test_correctness(
input_shape, weight_shape, bias_shape, padding, stride, kernel, groups
):
input = torch.randn(input_shape)
weight = torch.randn(weight_shape)
bias = torch.randn(bias_shape)
input = torch.abs(input) * 1000
weight = torch.abs(weight) * 1000
bias = torch.abs(bias) * 1000
pytorch_output = pytorch_conv2d(
input, weight, bias, padding, stride, kernel, groups
)
naive_output_nchw = naive_grouped_conv2d_nchw(
input, weight, bias, padding, stride, kernel, groups
)
assert torch.allclose(pytorch_output, naive_output_nchw)
# convert input to NHWC
input = input.permute(0, 2, 3, 1)
naive_output_nhwc = naive_grouped_conv2d_nhwc(
input, weight, bias, padding, stride, kernel, groups
)
# transpose output back to NCHW
naive_output_nhwc = naive_output_nhwc.permute(0, 3, 1, 2)
assert torch.allclose(pytorch_output, naive_output_nhwc)
print("Correctness test passed!")
if __name__ == "__main__":
input_shapes = [
(1, 24, 112, 112),
(1, 56, 56, 56),
(1, 152, 28, 28),
(1, 152, 14, 14),
(1, 152, 14, 14),
(1, 152, 14, 14),
(1, 368, 14, 14),
(1, 368, 7, 7),
(1, 368, 7, 7),
(1, 368, 7, 7),
(1, 368, 7, 7),
(1, 368, 7, 7),
(1, 368, 7, 7),
]
weight_shapes = [
(24, 8, 3, 3),
(56, 8, 3, 3),
(152, 8, 3, 3),
(152, 8, 3, 3),
(152, 8, 3, 3),
(152, 8, 3, 3),
(368, 8, 3, 3),
(368, 8, 3, 3),
(368, 8, 3, 3),
(368, 8, 3, 3),
(368, 8, 3, 3),
(368, 8, 3, 3),
(368, 8, 3, 3),
]
bias_shapes = [
24,
56,
152,
152,
152,
152,
368,
368,
368,
368,
368,
368,
368,
]
strides = [
(2, 2),
(2, 2),
(2, 2),
(1, 1),
(1, 1),
(1, 1),
(2, 2),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
]
padding = [
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
]
kernels = [
(3, 3),
(3, 3),
(3, 3),
(3, 3),
(3, 3),
(3, 3),
(3, 3),
(3, 3),
(3, 3),
(3, 3),
(3, 3),
(3, 3),
(3, 3),
]
groups = [
3,
7,
19,
19,
19,
19,
46,
46,
46,
46,
46,
46,
46,
]
for input_shape, weight_shape, bias_shape, stride, padding, kernel, group in zip(
input_shapes, weight_shapes, bias_shapes, strides, padding, kernels, groups
):
test_correctness(
input_shape, weight_shape, bias_shape, padding, stride, kernel, group
)