test_wrappers.py 12.6 KB
Newer Older
1
# Copyright (c) OpenMMLab. All rights reserved.
Cao Yuhang's avatar
Cao Yuhang committed
2
3
from unittest.mock import patch

dreamerlin's avatar
dreamerlin committed
4
import pytest
Cao Yuhang's avatar
Cao Yuhang committed
5
6
import torch
import torch.nn as nn
7
8
from mmengine.utils import digit_version
from mmengine.utils.dl_utils import TORCH_VERSION
Cao Yuhang's avatar
Cao Yuhang committed
9

dreamerlin's avatar
dreamerlin committed
10
11
from mmcv.cnn.bricks import (Conv2d, Conv3d, ConvTranspose2d, ConvTranspose3d,
                             Linear, MaxPool2d, MaxPool3d)
Cao Yuhang's avatar
Cao Yuhang committed
12

13
14
15
16
if torch.__version__ != 'parrots':
    torch_version = '1.1'
else:
    torch_version = 'parrots'
Cao Yuhang's avatar
Cao Yuhang committed
17

18
19

@patch('torch.__version__', torch_version)
dreamerlin's avatar
dreamerlin committed
20
21
22
23
24
@pytest.mark.parametrize(
    'in_w,in_h,in_channel,out_channel,kernel_size,stride,padding,dilation',
    [(10, 10, 1, 1, 3, 1, 0, 1), (20, 20, 3, 3, 5, 2, 1, 2)])
def test_conv2d(in_w, in_h, in_channel, out_channel, kernel_size, stride,
                padding, dilation):
Cao Yuhang's avatar
Cao Yuhang committed
25
26
27
28
29
    """
    CommandLine:
        xdoctest -m tests/test_wrappers.py test_conv2d
    """
    # train mode
dreamerlin's avatar
dreamerlin committed
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
    # wrapper op with 0-dim input
    x_empty = torch.randn(0, in_channel, in_h, in_w)
    torch.manual_seed(0)
    wrapper = Conv2d(
        in_channel,
        out_channel,
        kernel_size,
        stride=stride,
        padding=padding,
        dilation=dilation)
    wrapper_out = wrapper(x_empty)

    # torch op with 3-dim input as shape reference
    x_normal = torch.randn(3, in_channel, in_h, in_w).requires_grad_(True)
    torch.manual_seed(0)
    ref = nn.Conv2d(
        in_channel,
        out_channel,
        kernel_size,
        stride=stride,
        padding=padding,
        dilation=dilation)
    ref_out = ref(x_normal)

    assert wrapper_out.shape[0] == 0
    assert wrapper_out.shape[1:] == ref_out.shape[1:]

    wrapper_out.sum().backward()
    assert wrapper.weight.grad is not None
    assert wrapper.weight.grad.shape == wrapper.weight.shape

    assert torch.equal(wrapper(x_normal), ref_out)
Cao Yuhang's avatar
Cao Yuhang committed
62
63

    # eval mode
dreamerlin's avatar
dreamerlin committed
64
65
66
67
68
69
70
71
    x_empty = torch.randn(0, in_channel, in_h, in_w)
    wrapper = Conv2d(
        in_channel,
        out_channel,
        kernel_size,
        stride=stride,
        padding=padding,
        dilation=dilation)
Cao Yuhang's avatar
Cao Yuhang committed
72
73
74
75
    wrapper.eval()
    wrapper(x_empty)


76
@patch('torch.__version__', torch_version)
dreamerlin's avatar
dreamerlin committed
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
@pytest.mark.parametrize(
    'in_w,in_h,in_t,in_channel,out_channel,kernel_size,stride,padding,dilation',  # noqa: E501
    [(10, 10, 10, 1, 1, 3, 1, 0, 1), (20, 20, 20, 3, 3, 5, 2, 1, 2)])
def test_conv3d(in_w, in_h, in_t, in_channel, out_channel, kernel_size, stride,
                padding, dilation):
    """
    CommandLine:
        xdoctest -m tests/test_wrappers.py test_conv3d
    """
    # train mode
    # wrapper op with 0-dim input
    x_empty = torch.randn(0, in_channel, in_t, in_h, in_w)
    torch.manual_seed(0)
    wrapper = Conv3d(
        in_channel,
        out_channel,
        kernel_size,
        stride=stride,
        padding=padding,
        dilation=dilation)
    wrapper_out = wrapper(x_empty)

    # torch op with 3-dim input as shape reference
    x_normal = torch.randn(3, in_channel, in_t, in_h,
                           in_w).requires_grad_(True)
    torch.manual_seed(0)
    ref = nn.Conv3d(
        in_channel,
        out_channel,
        kernel_size,
        stride=stride,
        padding=padding,
        dilation=dilation)
    ref_out = ref(x_normal)

    assert wrapper_out.shape[0] == 0
    assert wrapper_out.shape[1:] == ref_out.shape[1:]

    wrapper_out.sum().backward()
    assert wrapper.weight.grad is not None
    assert wrapper.weight.grad.shape == wrapper.weight.shape

    assert torch.equal(wrapper(x_normal), ref_out)

    # eval mode
    x_empty = torch.randn(0, in_channel, in_t, in_h, in_w)
    wrapper = Conv3d(
        in_channel,
        out_channel,
        kernel_size,
        stride=stride,
        padding=padding,
        dilation=dilation)
    wrapper.eval()
    wrapper(x_empty)


134
@patch('torch.__version__', torch_version)
dreamerlin's avatar
dreamerlin committed
135
136
137
138
139
140
141
142
143
@pytest.mark.parametrize(
    'in_w,in_h,in_channel,out_channel,kernel_size,stride,padding,dilation',
    [(10, 10, 1, 1, 3, 1, 0, 1), (20, 20, 3, 3, 5, 2, 1, 2)])
def test_conv_transposed_2d(in_w, in_h, in_channel, out_channel, kernel_size,
                            stride, padding, dilation):
    # wrapper op with 0-dim input
    x_empty = torch.randn(0, in_channel, in_h, in_w, requires_grad=True)
    # out padding must be smaller than either stride or dilation
    op = min(stride, dilation) - 1
144
145
    if torch.__version__ == 'parrots':
        op = 0
dreamerlin's avatar
dreamerlin committed
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
    torch.manual_seed(0)
    wrapper = ConvTranspose2d(
        in_channel,
        out_channel,
        kernel_size,
        stride=stride,
        padding=padding,
        dilation=dilation,
        output_padding=op)
    wrapper_out = wrapper(x_empty)

    # torch op with 3-dim input as shape reference
    x_normal = torch.randn(3, in_channel, in_h, in_w)
    torch.manual_seed(0)
    ref = nn.ConvTranspose2d(
        in_channel,
        out_channel,
        kernel_size,
        stride=stride,
        padding=padding,
        dilation=dilation,
        output_padding=op)
    ref_out = ref(x_normal)

    assert wrapper_out.shape[0] == 0
    assert wrapper_out.shape[1:] == ref_out.shape[1:]

    wrapper_out.sum().backward()
    assert wrapper.weight.grad is not None
    assert wrapper.weight.grad.shape == wrapper.weight.shape

    assert torch.equal(wrapper(x_normal), ref_out)
Cao Yuhang's avatar
Cao Yuhang committed
178
179

    # eval mode
dreamerlin's avatar
dreamerlin committed
180
    x_empty = torch.randn(0, in_channel, in_h, in_w)
Cao Yuhang's avatar
Cao Yuhang committed
181
    wrapper = ConvTranspose2d(
dreamerlin's avatar
dreamerlin committed
182
183
184
185
186
187
188
        in_channel,
        out_channel,
        kernel_size,
        stride=stride,
        padding=padding,
        dilation=dilation,
        output_padding=op)
Cao Yuhang's avatar
Cao Yuhang committed
189
190
191
192
    wrapper.eval()
    wrapper(x_empty)


193
@patch('torch.__version__', torch_version)
dreamerlin's avatar
dreamerlin committed
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
@pytest.mark.parametrize(
    'in_w,in_h,in_t,in_channel,out_channel,kernel_size,stride,padding,dilation',  # noqa: E501
    [(10, 10, 10, 1, 1, 3, 1, 0, 1), (20, 20, 20, 3, 3, 5, 2, 1, 2)])
def test_conv_transposed_3d(in_w, in_h, in_t, in_channel, out_channel,
                            kernel_size, stride, padding, dilation):
    # wrapper op with 0-dim input
    x_empty = torch.randn(0, in_channel, in_t, in_h, in_w, requires_grad=True)
    # out padding must be smaller than either stride or dilation
    op = min(stride, dilation) - 1
    torch.manual_seed(0)
    wrapper = ConvTranspose3d(
        in_channel,
        out_channel,
        kernel_size,
        stride=stride,
        padding=padding,
        dilation=dilation,
        output_padding=op)
    wrapper_out = wrapper(x_empty)

    # torch op with 3-dim input as shape reference
    x_normal = torch.randn(3, in_channel, in_t, in_h, in_w)
    torch.manual_seed(0)
    ref = nn.ConvTranspose3d(
        in_channel,
        out_channel,
        kernel_size,
        stride=stride,
        padding=padding,
        dilation=dilation,
        output_padding=op)
    ref_out = ref(x_normal)

    assert wrapper_out.shape[0] == 0
    assert wrapper_out.shape[1:] == ref_out.shape[1:]

    wrapper_out.sum().backward()
    assert wrapper.weight.grad is not None
    assert wrapper.weight.grad.shape == wrapper.weight.shape

    assert torch.equal(wrapper(x_normal), ref_out)
dreamerlin's avatar
dreamerlin committed
235
236

    # eval mode
dreamerlin's avatar
dreamerlin committed
237
    x_empty = torch.randn(0, in_channel, in_t, in_h, in_w)
dreamerlin's avatar
dreamerlin committed
238
    wrapper = ConvTranspose3d(
dreamerlin's avatar
dreamerlin committed
239
240
241
242
243
244
245
        in_channel,
        out_channel,
        kernel_size,
        stride=stride,
        padding=padding,
        dilation=dilation,
        output_padding=op)
dreamerlin's avatar
dreamerlin committed
246
247
248
249
    wrapper.eval()
    wrapper(x_empty)


250
@patch('torch.__version__', torch_version)
dreamerlin's avatar
dreamerlin committed
251
252
253
254
255
256
257
258
259
260
@pytest.mark.parametrize(
    'in_w,in_h,in_channel,out_channel,kernel_size,stride,padding,dilation',
    [(10, 10, 1, 1, 3, 1, 0, 1), (20, 20, 3, 3, 5, 2, 1, 2)])
def test_max_pool_2d(in_w, in_h, in_channel, out_channel, kernel_size, stride,
                     padding, dilation):
    # wrapper op with 0-dim input
    x_empty = torch.randn(0, in_channel, in_h, in_w, requires_grad=True)
    wrapper = MaxPool2d(
        kernel_size, stride=stride, padding=padding, dilation=dilation)
    wrapper_out = wrapper(x_empty)
Cao Yuhang's avatar
Cao Yuhang committed
261

dreamerlin's avatar
dreamerlin committed
262
263
264
265
266
    # torch op with 3-dim input as shape reference
    x_normal = torch.randn(3, in_channel, in_h, in_w)
    ref = nn.MaxPool2d(
        kernel_size, stride=stride, padding=padding, dilation=dilation)
    ref_out = ref(x_normal)
Cao Yuhang's avatar
Cao Yuhang committed
267

dreamerlin's avatar
dreamerlin committed
268
269
    assert wrapper_out.shape[0] == 0
    assert wrapper_out.shape[1:] == ref_out.shape[1:]
Cao Yuhang's avatar
Cao Yuhang committed
270

dreamerlin's avatar
dreamerlin committed
271
    assert torch.equal(wrapper(x_normal), ref_out)
Cao Yuhang's avatar
Cao Yuhang committed
272
273


274
@patch('torch.__version__', torch_version)
dreamerlin's avatar
dreamerlin committed
275
276
277
@pytest.mark.parametrize(
    'in_w,in_h,in_t,in_channel,out_channel,kernel_size,stride,padding,dilation',  # noqa: E501
    [(10, 10, 10, 1, 1, 3, 1, 0, 1), (20, 20, 20, 3, 3, 5, 2, 1, 2)])
278
279
280
@pytest.mark.skipif(
    torch.__version__ == 'parrots' and not torch.cuda.is_available(),
    reason='parrots requires CUDA support')
dreamerlin's avatar
dreamerlin committed
281
282
283
284
285
286
def test_max_pool_3d(in_w, in_h, in_t, in_channel, out_channel, kernel_size,
                     stride, padding, dilation):
    # wrapper op with 0-dim input
    x_empty = torch.randn(0, in_channel, in_t, in_h, in_w, requires_grad=True)
    wrapper = MaxPool3d(
        kernel_size, stride=stride, padding=padding, dilation=dilation)
287
288
    if torch.__version__ == 'parrots':
        x_empty = x_empty.cuda()
dreamerlin's avatar
dreamerlin committed
289
290
291
292
293
    wrapper_out = wrapper(x_empty)
    # torch op with 3-dim input as shape reference
    x_normal = torch.randn(3, in_channel, in_t, in_h, in_w)
    ref = nn.MaxPool3d(
        kernel_size, stride=stride, padding=padding, dilation=dilation)
294
295
    if torch.__version__ == 'parrots':
        x_normal = x_normal.cuda()
dreamerlin's avatar
dreamerlin committed
296
    ref_out = ref(x_normal)
dreamerlin's avatar
dreamerlin committed
297

dreamerlin's avatar
dreamerlin committed
298
299
    assert wrapper_out.shape[0] == 0
    assert wrapper_out.shape[1:] == ref_out.shape[1:]
dreamerlin's avatar
dreamerlin committed
300

dreamerlin's avatar
dreamerlin committed
301
    assert torch.equal(wrapper(x_normal), ref_out)
dreamerlin's avatar
dreamerlin committed
302
303


304
@patch('torch.__version__', torch_version)
dreamerlin's avatar
dreamerlin committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
@pytest.mark.parametrize('in_w,in_h,in_feature,out_feature', [(10, 10, 1, 1),
                                                              (20, 20, 3, 3)])
def test_linear(in_w, in_h, in_feature, out_feature):
    # wrapper op with 0-dim input
    x_empty = torch.randn(0, in_feature, requires_grad=True)
    torch.manual_seed(0)
    wrapper = Linear(in_feature, out_feature)
    wrapper_out = wrapper(x_empty)

    # torch op with 3-dim input as shape reference
    x_normal = torch.randn(3, in_feature)
    torch.manual_seed(0)
    ref = nn.Linear(in_feature, out_feature)
    ref_out = ref(x_normal)

    assert wrapper_out.shape[0] == 0
    assert wrapper_out.shape[1:] == ref_out.shape[1:]

    wrapper_out.sum().backward()
    assert wrapper.weight.grad is not None
    assert wrapper.weight.grad.shape == wrapper.weight.shape

    assert torch.equal(wrapper(x_normal), ref_out)
Cao Yuhang's avatar
Cao Yuhang committed
328
329
330
331
332
333
334
335

    # eval mode
    x_empty = torch.randn(0, in_feature)
    wrapper = Linear(in_feature, out_feature)
    wrapper.eval()
    wrapper(x_empty)


336
@patch('mmcv.cnn.bricks.wrappers.TORCH_VERSION', (1, 10))
Cao Yuhang's avatar
Cao Yuhang committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
def test_nn_op_forward_called():

    for m in ['Conv2d', 'ConvTranspose2d', 'MaxPool2d']:
        with patch(f'torch.nn.{m}.forward') as nn_module_forward:
            # randn input
            x_empty = torch.randn(0, 3, 10, 10)
            wrapper = eval(m)(3, 2, 1)
            wrapper(x_empty)
            nn_module_forward.assert_called_with(x_empty)

            # non-randn input
            x_normal = torch.randn(1, 3, 10, 10)
            wrapper = eval(m)(3, 2, 1)
            wrapper(x_normal)
            nn_module_forward.assert_called_with(x_normal)

353
354
355
356
357
358
359
360
361
362
363
364
365
366
    for m in ['Conv3d', 'ConvTranspose3d', 'MaxPool3d']:
        with patch(f'torch.nn.{m}.forward') as nn_module_forward:
            # randn input
            x_empty = torch.randn(0, 3, 10, 10, 10)
            wrapper = eval(m)(3, 2, 1)
            wrapper(x_empty)
            nn_module_forward.assert_called_with(x_empty)

            # non-randn input
            x_normal = torch.randn(1, 3, 10, 10, 10)
            wrapper = eval(m)(3, 2, 1)
            wrapper(x_normal)
            nn_module_forward.assert_called_with(x_normal)

Cao Yuhang's avatar
Cao Yuhang committed
367
368
369
370
371
    with patch('torch.nn.Linear.forward') as nn_module_forward:
        # randn input
        x_empty = torch.randn(0, 3)
        wrapper = Linear(3, 3)
        wrapper(x_empty)
372
        nn_module_forward.assert_called_with(x_empty)
Cao Yuhang's avatar
Cao Yuhang committed
373
374
375
376
377
378

        # non-randn input
        x_normal = torch.randn(1, 3)
        wrapper = Linear(3, 3)
        wrapper(x_normal)
        nn_module_forward.assert_called_with(x_normal)
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396


@pytest.mark.skipif(
    digit_version(TORCH_VERSION) < digit_version('1.10'),
    reason='MaxPool2d and MaxPool3d will fail fx for torch<=1.9')
def test_fx_compatibility():
    from torch import fx

    # ensure the fx trace can pass the network
    for Net in (MaxPool2d, MaxPool3d):
        net = Net(1)
        gm_module = fx.symbolic_trace(net)  # noqa: F841
    for Net in (Linear, ):
        net = Net(1, 1)
        gm_module = fx.symbolic_trace(net)  # noqa: F841
    for Net in (Conv2d, ConvTranspose2d, Conv3d, ConvTranspose3d):
        net = Net(1, 1, 1)
        gm_module = fx.symbolic_trace(net)  # noqa: F841