-
Notifications
You must be signed in to change notification settings - Fork 79
/
test_pac.py
423 lines (364 loc) · 20.7 KB
/
test_pac.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
"""
Copyright (C) 2019 NVIDIA Corporation. All rights reserved.
Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode).
"""
import unittest
from functools import wraps
import numpy as np
import torch as th
from torch import nn
from torch.autograd import gradcheck
import pac
def _allclose(x1, x2, rtol=1e-5, atol=1e-10):
return np.allclose(x1.cpu(), x2.cpu(), rtol=rtol, atol=atol)
def _gradcheck(f, x0, rtol=1e-3, atol=1e-8):
return gradcheck(f, x0, rtol=rtol, atol=atol)
# test both native autograd version and Function version
def repeat_impl_types(f):
@wraps(f)
def call_wrapped(self, *args):
f(self, *args, native_impl=True)
f(self, *args, native_impl=False)
return call_wrapped
# some features are not yet implemented using custom Function
def use_only_native_impl(f):
@wraps(f)
def call_wrapped(self, *args):
f(self, *args, native_impl=True)
return call_wrapped
# test only the version with custom Function
def use_only_custom_impl(f):
@wraps(f)
def call_wrapped(self, *args):
f(self, *args, native_impl=False)
return call_wrapped
class PacConvTest(unittest.TestCase):
def setUp(self):
self.device = th.device('cuda:0')
th.cuda.set_device(0)
@repeat_impl_types
def test_conv_forward_const_kernel(self, native_impl):
bs, sz, k_ch = 2, 111, 5
args = dict(in_channels=4, out_channels=3, kernel_size=5, stride=2, padding=4, dilation=2)
im = th.rand(bs, args['in_channels'], sz, sz).to(self.device)
im_th = im.clone()
im_k = th.ones(bs, k_ch, sz, sz).to(self.device)
conv_w = th.rand(args['out_channels'], args['in_channels'],
args['kernel_size'], args['kernel_size']).to(self.device)
conv_b = th.rand(args['out_channels']).to(self.device)
conv = pac.PacConv2d(native_impl=native_impl, **args).to(self.device)
conv_th = nn.Conv2d(**args).to(self.device)
conv.weight.data[:] = conv_th.weight.data[:] = conv_w
conv.bias.data[:] = conv_th.bias.data[:] = conv_b
_allclose(conv(im, im_k).detach(), conv_th(im_th).detach())
@repeat_impl_types
def test_conv_transpose_forward_const_kernel(self, native_impl):
bs, sz, k_ch = 4, 128, 5
args = dict(in_channels=4, out_channels=3, kernel_size=5, stride=2, padding=2, output_padding=1, dilation=1)
k_with_d = (args['kernel_size'] - 1) * args['dilation'] + 1
sz_out = (sz - 1) * args['stride'] - 2 * args['padding'] + k_with_d + args['output_padding']
im = th.rand(bs, args['in_channels'], sz, sz).to(self.device)
im_th = im.clone()
im_k = th.ones(bs, k_ch, sz_out, sz_out).to(self.device)
conv_w = th.rand(args['in_channels'], args['out_channels'],
args['kernel_size'], args['kernel_size']).to(self.device)
conv_b = th.rand(args['out_channels']).to(self.device)
conv = pac.PacConvTranspose2d(native_impl=native_impl, **args).to(self.device)
conv_th = nn.ConvTranspose2d(**args).to(self.device)
conv.weight.data[:] = conv_th.weight.data[:] = conv_w
conv.bias.data[:] = conv_th.bias.data[:] = conv_b
_allclose(conv(im, im_k).detach(), conv_th(im_th).detach())
@repeat_impl_types
def test_pool_forward_const_kernel(self, native_impl):
bs, sz, in_ch, k_ch = 2, 9, 4, 5
dilation = 1
args = dict(kernel_size=5, stride=2, padding=2)
im = th.rand(bs, in_ch, sz, sz).to(self.device)
im_th = im.clone()
im_k = th.ones(bs, k_ch, sz, sz).to(self.device)
pool = pac.PacPool2d(dilation=dilation, native_impl=native_impl, **args).to(self.device)
pool_th = nn.AvgPool2d(**args).to(self.device)
_allclose(pool(im, im_k).detach(), pool_th(im_th).detach())
@repeat_impl_types
def test_conv_input_grad(self, native_impl):
bs, sz, k_ch = 2, 8, 3
args = dict(in_channels=4, out_channels=2, kernel_size=3, stride=2, padding=1, dilation=1)
im = th.rand(bs, args['in_channels'], sz, sz).double().to(self.device)
im_k = th.rand(bs, k_ch, sz, sz).double().to(self.device)
im.requires_grad = im_k.requires_grad = True
conv = pac.PacConv2d(native_impl=native_impl, **args).double().to(self.device)
self.assertTrue(_gradcheck(conv, (im, im_k)))
@use_only_native_impl
def test_conv_inv_kernel_input_grad(self, native_impl):
bs, sz, k_ch = 2, 8, 3
args = dict(in_channels=4, out_channels=2, kernel_size=3, stride=2, padding=1, dilation=1,
kernel_type='inv_0.2_0.2_asym', smooth_kernel_type='average_5', normalize_kernel=True)
im = th.rand(bs, args['in_channels'], sz, sz).double().to(self.device)
im_k = th.rand(bs, k_ch, sz, sz).double().to(self.device)
im.requires_grad = im_k.requires_grad = True
conv = pac.PacConv2d(native_impl=native_impl, **args).double().to(self.device)
self.assertTrue(_gradcheck(conv, (im, im_k)))
@repeat_impl_types
def test_conv_all_grad(self, native_impl):
bs, sz, k_ch, f_sz, in_ch, out_ch = 2, 10, 3, 5, 2, 4
conv_args = dict(stride=1, padding=2, dilation=2)
kernel_args = dict(kernel_size=f_sz, smooth_kernel=None, inv_alpha=None, inv_lambda=None,
kernel_type='gaussian', smooth_kernel_type='none',
channel_wise=False, normalize_kernel=False, transposed=False,
**conv_args)
im = th.rand(bs, in_ch, sz, sz).double().to(self.device)
im_k = th.rand(bs, k_ch, sz, sz).double().to(self.device)
im.requires_grad = im_k.requires_grad = True
conv_w = th.rand(out_ch, in_ch, f_sz, f_sz).double().to(self.device)
conv_b = th.rand(out_ch).double().to(self.device)
self.assertTrue(_gradcheck(
lambda in0, in1, w, b: pac.pacconv2d(in0,
pac.packernel2d(in1, **kernel_args)[0],
w, b, native_impl=native_impl, **conv_args),
(im, im_k, conv_w, conv_b)))
@repeat_impl_types
def test_conv_transpose_input_grad(self, native_impl):
bs, sz, k_ch = 1, 4, 2
args = dict(in_channels=2, out_channels=3, kernel_size=3, stride=2, padding=1, output_padding=1, dilation=1)
k_with_d = (args['kernel_size'] - 1) * args['dilation'] + 1
sz_out = (sz - 1) * args['stride'] - 2 * args['padding'] + k_with_d + args['output_padding']
im = th.rand(bs, args['in_channels'], sz, sz).double().to(self.device)
im_k = th.rand(bs, k_ch, sz_out, sz_out).double().to(self.device)
im.requires_grad = im_k.requires_grad = True
conv = pac.PacConvTranspose2d(native_impl=native_impl, **args).double().to(self.device)
self.assertTrue(_gradcheck(conv, (im, im_k)))
@repeat_impl_types
def test_conv_transpose_all_grad(self, native_impl):
bs, sz, k_ch, f_sz, in_ch, out_ch = 2, 3, 3, 3, 2, 3
conv_args = dict(stride=2, padding=1, output_padding=1, dilation=1)
kernel_args = dict(kernel_size=f_sz, smooth_kernel=None, inv_alpha=None, inv_lambda=None,
kernel_type='gaussian', smooth_kernel_type='none',
channel_wise=False, normalize_kernel=False, transposed=True,
**conv_args)
k_with_d = (f_sz - 1) * conv_args['dilation'] + 1
sz_out = (sz - 1) * conv_args['stride'] - 2 * conv_args['padding'] + k_with_d + conv_args['output_padding']
im = th.rand(bs, in_ch, sz, sz).double().to(self.device)
im_k = th.rand(bs, k_ch, sz_out, sz_out).double().to(self.device)
im.requires_grad = im_k.requires_grad = True
conv_w = th.rand(in_ch, out_ch, f_sz, f_sz).double().to(self.device)
conv_b = th.rand(out_ch).double().to(self.device)
self.assertTrue(_gradcheck(
lambda in0, in1, w, b: pac.pacconv_transpose2d(in0,
pac.packernel2d(in1, **kernel_args)[0],
w, b, native_impl=native_impl, **conv_args),
(im, im_k, conv_w, conv_b)))
@repeat_impl_types
def test_pool_grad(self, native_impl):
bs, sz, ch, k_ch = 2, 8, 2, 3
args = dict(kernel_size=5, stride=2, padding=4, dilation=2)
im = th.rand(bs, ch, sz, sz).double().to(self.device)
im_k = th.rand(bs, k_ch, sz, sz).double().to(self.device)
im.requires_grad = im_k.requires_grad = True
pool = pac.PacPool2d(native_impl=native_impl, **args).double().to(self.device)
self.assertTrue(_gradcheck(pool, (im, im_k)))
def test_conv_two_impl_match(self):
bs, sz, k_ch = 24, 128, 3
args = dict(in_channels=4, out_channels=2, kernel_size=3, stride=2, padding=2, dilation=2)
im = th.rand(bs, args['in_channels'], sz, sz).double().to(self.device)
im_k = th.rand(bs, k_ch, sz, sz).double().to(self.device)
im0 = im.clone()
im0_k = im_k.clone()
im.requires_grad = im_k.requires_grad = True
im0.requires_grad = im0_k.requires_grad = True
conv = pac.PacConv2d(native_impl=False, **args).double().to(self.device)
conv0 = pac.PacConv2d(native_impl=True, **args).double().to(self.device)
conv_w = th.rand(args['out_channels'], args['in_channels'],
args['kernel_size'], args['kernel_size']).double().to(self.device)
conv_b = th.rand(args['out_channels']).double().to(self.device)
conv.weight.data[:] = conv0.weight.data[:] = conv_w
conv.bias.data[:] = conv0.bias.data[:] = conv_b
out = conv(im, im_k)
out0 = conv0(im0, im0_k)
out.sum().backward()
out0.sum().backward()
self.assertTrue(_allclose(out.detach(), out0.detach()))
self.assertTrue(_allclose(im.grad, im0.grad))
self.assertTrue(_allclose(im_k.grad, im0_k.grad))
self.assertTrue(_allclose(conv.weight.grad, conv0.weight.grad))
self.assertTrue(_allclose(conv.bias.grad, conv0.bias.grad))
def test_conv_with_kernel_input_two_impl_match(self):
bs, sz, k_ch = 24, 128, 3
args = dict(in_channels=4, out_channels=2, kernel_size=3, stride=2, padding=2, dilation=2)
im = th.rand(bs, args['in_channels'], sz, sz).double().to(self.device)
out_sz = int(np.floor(
(sz + 2 * args['padding'] - (args['kernel_size'] - 1) * args['dilation'] - 1) / args['stride'])) + 1
im_k = th.rand(bs, 1, args['kernel_size'], args['kernel_size'], out_sz, out_sz).double().to(self.device)
im0 = im.clone()
im0_k = im_k.clone()
im.requires_grad = im_k.requires_grad = True
im0.requires_grad = im0_k.requires_grad = True
conv = pac.PacConv2d(native_impl=False, **args).double().to(self.device)
conv0 = pac.PacConv2d(native_impl=True, **args).double().to(self.device)
conv_w = th.rand(args['out_channels'], args['in_channels'],
args['kernel_size'], args['kernel_size']).double().to(self.device)
conv_b = th.rand(args['out_channels']).double().to(self.device)
conv.weight.data[:] = conv0.weight.data[:] = conv_w
conv.bias.data[:] = conv0.bias.data[:] = conv_b
out = conv(im, None, im_k)
out0 = conv0(im0, None, im0_k)
out.sum().backward()
out0.sum().backward()
self.assertTrue(_allclose(out.detach(), out0.detach()))
self.assertTrue(_allclose(im.grad, im0.grad))
self.assertTrue(_allclose(im_k.grad, im0_k.grad))
self.assertTrue(_allclose(conv.weight.grad, conv0.weight.grad))
self.assertTrue(_allclose(conv.bias.grad, conv0.bias.grad))
def test_conv_transpose_two_impl_match(self):
bs, sz, k_ch = 3, 128, 3
args = dict(in_channels=2, out_channels=3, kernel_size=3, stride=2, padding=1, output_padding=1, dilation=1)
k_with_d = (args['kernel_size'] - 1) * args['dilation'] + 1
sz_out = (sz - 1) * args['stride'] - 2 * args['padding'] + k_with_d + args['output_padding']
im = th.rand(bs, args['in_channels'], sz, sz).double().to(self.device)
im_k = th.rand(bs, k_ch, sz_out, sz_out).double().to(self.device)
im0 = im.clone()
im0_k = im_k.clone()
im.requires_grad = im_k.requires_grad = True
im0.requires_grad = im0_k.requires_grad = True
conv = pac.PacConvTranspose2d(native_impl=False, **args).double().to(self.device)
conv0 = pac.PacConvTranspose2d(native_impl=True, **args).double().to(self.device)
conv_w = th.rand(args['in_channels'], args['out_channels'],
args['kernel_size'], args['kernel_size']).double().to(self.device)
conv_b = th.rand(args['out_channels']).double().to(self.device)
conv.weight.data[:] = conv0.weight.data[:] = conv_w
conv.bias.data[:] = conv0.bias.data[:] = conv_b
out = conv(im, im_k)
out0 = conv0(im0, im0_k)
out.sum().backward()
out0.sum().backward()
self.assertTrue(_allclose(out.detach(), out0.detach()))
self.assertTrue(_allclose(im.grad, im0.grad))
self.assertTrue(_allclose(im_k.grad, im0_k.grad))
self.assertTrue(_allclose(conv.weight.grad, conv0.weight.grad))
self.assertTrue(_allclose(conv.bias.grad, conv0.bias.grad))
def test_pool_two_impl_match(self):
bs, sz, ch, k_ch = 2, 128, 4, 3
args = dict(kernel_size=3, stride=2, padding=2, dilation=2)
im = th.rand(bs, ch, sz, sz).double().to(self.device)
im_k = th.rand(bs, k_ch, sz, sz).double().to(self.device)
im0 = im.clone()
im0_k = im_k.clone()
im.requires_grad = im_k.requires_grad = True
im0.requires_grad = im0_k.requires_grad = True
pool = pac.PacPool2d(native_impl=False, **args).to(self.device)
p00l0 = pac.PacPool2d(native_impl=True, **args).to(self.device)
out = pool(im, im_k)
out0 = p00l0(im0, im0_k)
out.sum().backward()
out0.sum().backward()
self.assertTrue(_allclose(out.detach(), out0.detach()))
self.assertTrue(_allclose(im.grad, im0.grad))
self.assertTrue(_allclose(im_k.grad, im0_k.grad))
def test_kernel_two_impl_match(self):
bs, sz, ch = 16, 256, 8
args = dict(kernel_size=3, stride=1, padding=1, dilation=1)
im = th.rand(bs, ch, sz, sz).double().to(self.device)
im0 = im.clone()
im.requires_grad = im0.requires_grad = True
out = pac.packernel2d(im, native_impl=False, **args)[0]
out0 = pac.packernel2d(im0, native_impl=True, **args)[0]
out.sum().backward()
out0.sum().backward()
self.assertTrue(_allclose(out.detach(), out0.detach()))
self.assertTrue(_allclose(im.grad, im0.grad))
# Tests below pass on small input sizes, but may fail on larger ones
@repeat_impl_types
def test_conv_sum_all_grad(self, native_impl):
bs, sz, k_ch, f_sz, in_ch, out_ch = 2, 10, 3, 5, 2, 4
conv_args = dict(stride=1, padding=2, dilation=2)
kernel_args = dict(kernel_size=f_sz, smooth_kernel=None, inv_alpha=None, inv_lambda=None,
kernel_type='gaussian', smooth_kernel_type='none',
channel_wise=False, normalize_kernel=False, transposed=False,
**conv_args)
im = th.rand(bs, in_ch, sz, sz).double().to(self.device)
im_k = th.rand(bs, k_ch, sz, sz).double().to(self.device)
im.requires_grad = im_k.requires_grad = True
conv_w = th.rand(out_ch, in_ch, f_sz, f_sz).double().to(self.device)
conv_b = th.rand(out_ch).double().to(self.device)
self.assertTrue(_gradcheck(
lambda in0, in1, w, b: pac.pacconv2d(in0,
pac.packernel2d(in1, **kernel_args)[0],
w, b, native_impl=native_impl, **conv_args).sum(),
(im, im_k, conv_w, conv_b), rtol=0.01))
@repeat_impl_types
def test_conv_transpose_sum_all_grad(self, native_impl):
bs, sz, k_ch, f_sz, in_ch, out_ch = 2, 3, 3, 3, 2, 3
conv_args = dict(stride=2, padding=1, output_padding=1, dilation=1)
kernel_args = dict(kernel_size=f_sz, smooth_kernel=None, inv_alpha=None, inv_lambda=None,
kernel_type='gaussian', smooth_kernel_type='none',
channel_wise=False, normalize_kernel=False, transposed=True,
**conv_args)
k_with_d = (f_sz - 1) * conv_args['dilation'] + 1
sz_out = (sz - 1) * conv_args['stride'] - 2 * conv_args['padding'] + k_with_d + conv_args['output_padding']
im = th.rand(bs, in_ch, sz, sz).double().to(self.device)
im_k = th.rand(bs, k_ch, sz_out, sz_out).double().to(self.device)
im.requires_grad = im_k.requires_grad = True
conv_w = th.rand(in_ch, out_ch, f_sz, f_sz).double().to(self.device)
conv_b = th.rand(out_ch).double().to(self.device)
self.assertTrue(_gradcheck(
lambda in0, in1, w, b: pac.pacconv_transpose2d(in0,
pac.packernel2d(in1, **kernel_args)[0],
w, b, native_impl=native_impl, **conv_args).sum(),
(im, im_k, conv_w, conv_b), rtol=0.01))
@repeat_impl_types
def test_pool_sum_grad(self, native_impl):
bs, sz, ch, k_ch = 2, 8, 2, 3
args = dict(kernel_size=5, stride=2, padding=4, dilation=2)
im = th.rand(bs, ch, sz, sz).double().to(self.device)
im_k = th.rand(bs, k_ch, sz, sz).double().to(self.device)
im.requires_grad = im_k.requires_grad = True
pool = pac.PacPool2d(native_impl=native_impl, **args).double().to(self.device)
self.assertTrue(_gradcheck(lambda x, y: pool(x, y).sum(), (im, im_k), rtol=0.01))
@repeat_impl_types
def test_kernel_sum_grad(self, native_impl):
bs, sz, ch = 2, 4, 4
args = dict(kernel_size=3, stride=2, padding=1, dilation=1)
im = th.rand(bs, ch, sz, sz).double().to(self.device)
im.requires_grad = True
self.assertTrue(_gradcheck(lambda x: pac.packernel2d(x, native_impl=native_impl, **args)[0].sum(),
(im,), rtol=0.01))
@repeat_impl_types
def test_conv_with_kernel_input_sum_all_grad(self, native_impl):
bs, sz, k_ch, f_sz, in_ch, out_ch = 2, 10, 3, 5, 2, 4
args = dict(stride=1, padding=2, dilation=2)
out_sz = int(np.floor((sz + 2 * args['padding'] - (f_sz - 1) * args['dilation'] - 1) / args['stride'])) + 1
im = th.rand(bs, in_ch, sz, sz).double().to(self.device)
im_k = th.rand(bs, 1, f_sz, f_sz, out_sz, out_sz).double().to(self.device)
im.requires_grad = im_k.requires_grad = True
conv_w = th.rand(out_ch, in_ch, f_sz, f_sz).double().to(self.device)
conv_b = th.rand(out_ch).double().to(self.device)
self.assertTrue(_gradcheck(
lambda in0, in1, w, b: pac.pacconv2d(in0, in1, w, b, native_impl=native_impl, **args).sum(),
(im, im_k, conv_w, conv_b), rtol=0.01))
@repeat_impl_types
def test_conv_transpose_with_kernel_input_sum_all_grad(self, native_impl):
bs, sz, k_ch, f_sz, in_ch, out_ch = 2, 3, 3, 3, 2, 3
args = dict(stride=2, padding=1, output_padding=1, dilation=1)
k_with_d = (f_sz - 1) * args['dilation'] + 1
sz_out = (sz - 1) * args['stride'] - 2 * args['padding'] + k_with_d + args['output_padding']
im = th.rand(bs, in_ch, sz, sz).double().to(self.device)
im_k = th.rand(bs, 1, f_sz, f_sz, sz_out, sz_out).double().to(self.device)
im.requires_grad = im_k.requires_grad = True
conv_w = th.rand(in_ch, out_ch, f_sz, f_sz).double().to(self.device)
conv_b = th.rand(out_ch).double().to(self.device)
self.assertTrue(_gradcheck(
lambda in0, in1, w, b: pac.pacconv_transpose2d(in0, in1, w, b, native_impl=native_impl, **args).sum(),
(im, im_k, conv_w, conv_b), rtol=0.01))
@repeat_impl_types
def test_pool_with_kernel_input_sum_grad(self, native_impl):
bs, sz, ch = 2, 8, 2
args = dict(kernel_size=3, stride=2, padding=2, dilation=2)
out_sz = int(np.floor(
(sz + 2 * args['padding'] - (args['kernel_size'] - 1) * args['dilation'] - 1) / args['stride'])) + 1
im = th.rand(bs, ch, sz, sz).double().to(self.device)
im_k = th.rand(bs, 1, args['kernel_size'], args['kernel_size'], out_sz, out_sz).double().to(self.device)
im.requires_grad = im_k.requires_grad = True
pool = pac.PacPool2d(native_impl=native_impl, **args).double().to(self.device)
self.assertTrue(_gradcheck(lambda x, y: pool(x, None, y).sum(),
(im, im_k), rtol=0.01))
if __name__ == '__main__':
unittest.main()