File size: 9,529 Bytes
d7e58f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) OpenMMLab. All rights reserved.
import logging

import numpy as np
import pytest
import torch
import torch.nn as nn

from mmcv.runner.fp16_utils import auto_fp16
from mmcv.utils import IS_IPU_AVAILABLE

if IS_IPU_AVAILABLE:
    from mmcv.device.ipu import cfg2options, ipu_model_wrapper
    from mmcv.device.ipu.utils import compare_ndarray

skip_no_ipu = pytest.mark.skipif(
    not IS_IPU_AVAILABLE, reason='test case under ipu environment')


class MyBN(nn.BatchNorm2d):

    def forward(self, *args, **kwargs):
        result = super().forward(*args, **kwargs)
        return result, self.running_mean


# TODO Once the model training and inference interfaces
# of MMCLS and MMDET are unified,
# construct the model according to the unified standards
class ToyModel(nn.Module):

    def __init__(self):
        super().__init__()
        self.conv = nn.Conv2d(3, 3, 1)
        self.bn = MyBN(3)
        self.relu = nn.ReLU6()
        self.fp16_enabled = False

    @auto_fp16(apply_to=('img', ))
    def forward(self, img, return_loss=True, **kwargs):
        x = self.conv(img)
        x, running_mean = self.bn(x)
        x = self.relu(x)
        if return_loss:
            loss = ((x - kwargs['gt_label'])**2).sum()
            return {
                'loss': loss,
                'loss_list': [loss, loss],
                'loss_dict': {
                    'loss1': loss
                }
            }
        return x

    def _parse_losses(self, losses):
        return losses['loss'], losses['loss']

    def train_step(self, data, optimizer=None, **kwargs):
        losses = self(**data)
        loss, log_vars = self._parse_losses(losses)
        outputs = dict(
            loss=loss, log_vars=log_vars, num_samples=len(data['img'].data))
        return outputs


@skip_no_ipu
def test_build_model():
    for execution_strategy in \
            ['SameAsIpu', 'ShardedExecution', 'error_strategy']:
        if execution_strategy == 'error_strategy':

            def maybe_catch_error(_error):
                return pytest.raises(_error)
        else:

            class NullContextManager:

                def __enter__(self, ):
                    pass

                def __exit__(self, exc_type, exc_value, exc_traceback):
                    pass

            def maybe_catch_error(_error):
                return NullContextManager()

        with maybe_catch_error(NotImplementedError):
            options_cfg = dict(
                randomSeed=888,
                enableExecutableCaching='cache_engine',
                train_cfg=dict(
                    executionStrategy=execution_strategy,
                    Training=dict(gradientAccumulation=8),
                    availableMemoryProportion=[0.3, 0.3, 0.3, 0.3]),
                eval_cfg=dict(deviceIterations=1, ),
                partialsType='half')

            ipu_options = cfg2options(options_cfg)
            model = ToyModel()
            optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
            logger = logging.getLogger()
            modules_to_record = None
            ipu_model_cfg = dict(
                train_split_edges=[dict(layer_to_call='conv', ipu_id=0)],
                train_ckpt_nodes=['bn', 'conv'])
            fp16_cfg = {'loss_scale': 0.5}
            ipu_model = ipu_model_wrapper(
                model,
                ipu_options,
                optimizer,
                logger,
                modules_to_record=modules_to_record,
                ipu_model_cfg=ipu_model_cfg,
                fp16_cfg=fp16_cfg)

            ipu_model.train()
            ipu_model.eval()
            ipu_model.train()


def run_model(ipu_options,
              fp16_cfg,
              modules_to_record,
              ipu_model_wrapper_func,
              only_eval=False):
    model = ToyModel()
    optimizer = torch.optim.SGD(model.parameters(), lr=0.1)\
        if not only_eval else None
    logger = logging.getLogger()
    ipu_model_cfg = dict(
        train_split_edges=[dict(layer_to_call='conv', ipu_id=0)],
        train_ckpt_nodes=['bn', 'conv'])
    ipu_model = ipu_model_wrapper_func(
        model,
        ipu_options,
        optimizer,
        logger,
        modules_to_record=modules_to_record,
        ipu_model_cfg=ipu_model_cfg,
        fp16_cfg=fp16_cfg)

    def get_dummy_input(training):
        if training:
            return {
                'data': {
                    'img': torch.rand((16, 3, 10, 10)),
                    'gt_label': torch.rand((16, 3, 10, 10))
                }
            }
        else:
            return {
                'img': torch.rand((16, 3, 10, 10)),
                'img_metas': {
                    'img': torch.rand((16, 3, 10, 10))
                },
                'return_loss': False
            }

    if not only_eval:
        training = True
        ipu_model.train()
        for _ in range(3):
            dummy_input = get_dummy_input(training)
            output = ipu_model.train_step(**dummy_input)
    training = False
    ipu_model.eval()
    for _ in range(3):
        dummy_input = get_dummy_input(training)
        output = ipu_model(**dummy_input)
    return output, ipu_model


@skip_no_ipu
def test_run_model():

    # test feature alignment not support gradientAccumulation mode
    options_cfg = dict(
        randomSeed=888,
        enableExecutableCaching='cache_engine',
        train_cfg=dict(
            executionStrategy='SameAsIpu',
            Training=dict(gradientAccumulation=8),
            availableMemoryProportion=[0.3, 0.3, 0.3, 0.3],
        ),
        eval_cfg=dict(deviceIterations=1, ),
        partialsType='half')
    ipu_options = cfg2options(options_cfg)
    modules_to_record = ['bn']
    with pytest.raises(AssertionError, match='Feature alignment'):
        run_model(ipu_options, None, modules_to_record, ipu_model_wrapper)

    # test feature alignment not support multi-replica mode
    options_cfg = dict(
        randomSeed=888,
        replicationFactor=2,
        enableExecutableCaching='cache_engine',
        train_cfg=dict(
            executionStrategy='SameAsIpu',
            availableMemoryProportion=[0.3, 0.3, 0.3, 0.3],
        ),
        eval_cfg=dict(deviceIterations=1, ),
        partialsType='half')
    ipu_options = cfg2options(options_cfg)
    modules_to_record = ['bn']
    with pytest.raises(AssertionError, match='Feature alignment'):
        run_model(ipu_options, None, modules_to_record, ipu_model_wrapper)

    # test feature alignment not support fp16 mode
    options_cfg = dict(
        randomSeed=888,
        enableExecutableCaching='cache_engine',
        train_cfg=dict(
            executionStrategy='SameAsIpu',
            availableMemoryProportion=[0.3, 0.3, 0.3, 0.3],
        ),
        eval_cfg=dict(deviceIterations=1, ),
        partialsType='half')
    ipu_options = cfg2options(options_cfg)
    fp16_cfg = {
        'loss_scale': 0.5,
        'velocity_accum_type': 'half',
        'accum_type': 'half'
    }
    modules_to_record = ['bn']
    with pytest.raises(NotImplementedError):
        run_model(ipu_options, fp16_cfg, modules_to_record, ipu_model_wrapper)

    # test velocity_accum_type and accum_type
    fp16_cfg = {
        'loss_scale': 0.5,
        'velocity_accum_type': 'float',
        'accum_type': 'float'
    }
    run_model(ipu_options, fp16_cfg, None, ipu_model_wrapper)

    # test compile and run
    options_cfg = dict(
        randomSeed=888,
        enableExecutableCaching='cache_engine',
        train_cfg=dict(
            executionStrategy='SameAsIpu',
            availableMemoryProportion=[0.3, 0.3, 0.3, 0.3],
        ),
        eval_cfg=dict(deviceIterations=1, ),
        partialsType='half')
    ipu_options = cfg2options(options_cfg)
    modules_to_record = ['bn']
    run_model(ipu_options, None, modules_to_record, ipu_model_wrapper)

    # test feature alignment
    options_cfg = dict(
        randomSeed=888,
        enableExecutableCaching='cache_engine',
        train_cfg=dict(
            executionStrategy='SameAsIpu',
            availableMemoryProportion=[0.3, 0.3, 0.3, 0.3],
        ),
        eval_cfg=dict(deviceIterations=1, ))
    ipu_options = cfg2options(options_cfg)
    modules_to_record = None
    run_model(ipu_options, None, modules_to_record, ipu_model_wrapper)

    # test inference mode
    options_cfg = dict(
        randomSeed=888,
        enableExecutableCaching='cache_engine',
        train_cfg=dict(
            executionStrategy='SameAsIpu',
            availableMemoryProportion=[0.3, 0.3, 0.3, 0.3],
        ),
        eval_cfg=dict(deviceIterations=1, ),
        partialsType='half')
    ipu_options = cfg2options(options_cfg)
    fp16_cfg = {'loss_scale': 0.5}
    modules_to_record = None
    _, ipu_model = run_model(
        ipu_options,
        fp16_cfg,
        modules_to_record,
        ipu_model_wrapper,
        only_eval=True)
    with pytest.raises(RuntimeError):
        ipu_model.train()
    with pytest.raises(ValueError):
        ipu_model.train(123)
    _, ipu_model = run_model(ipu_options, None, modules_to_record,
                             ipu_model_wrapper)

    # test NotImplementedError in __call__
    ipu_model.train()
    with pytest.raises(NotImplementedError):
        ipu_model()

    # test parse_losses
    with pytest.raises(TypeError):
        ipu_model._model.model._parse_losses({'loss': None})


@skip_no_ipu
def test_compare_tensor():
    compare_ndarray(np.random.rand(3, 4), np.random.rand(3, 4))