convert_super.py 31.7 KB
Newer Older
C
ceci3 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import inspect
import decorator
import logging
C
ceci3 已提交
18
import numbers
C
ceci3 已提交
19
import paddle
C
ceci3 已提交
20
from ...common import get_logger
C
ceci3 已提交
21 22 23 24 25
from .utils.utils import get_paddle_version
pd_ver = get_paddle_version()
if pd_ver == 185:
    import paddle.fluid.dygraph.nn as nn
    from paddle.fluid.dygraph.nn import Conv2D, Conv2DTranspose, Linear, LayerNorm, Embedding
C
ceci3 已提交
26
    from paddle.fluid import ParamAttr
C
ceci3 已提交
27 28
    from .layers_old import *
    from . import layers_old as layers
C
ceci3 已提交
29
    Layer = paddle.fluid.dygraph.Layer
C
ceci3 已提交
30 31 32
else:
    import paddle.nn as nn
    from paddle.nn import Conv2D, Conv2DTranspose, Linear, LayerNorm, Embedding
C
ceci3 已提交
33
    from paddle import ParamAttr
C
ceci3 已提交
34 35
    from .layers import *
    from . import layers
C
ceci3 已提交
36
    Layer = paddle.nn.Layer
37
from .layers_base import Block
C
ceci3 已提交
38 39 40

_logger = get_logger(__name__, level=logging.INFO)

C
ceci3 已提交
41
__all__ = ['supernet', 'Convert']
C
ceci3 已提交
42

C
ceci3 已提交
43
WEIGHT_LAYER = ['conv', 'linear', 'embedding']
C
ceci3 已提交
44 45 46


class Convert:
C
ceci3 已提交
47 48 49 50 51 52 53 54 55 56 57
    """
    Convert network to the supernet according to the search space.
    Parameters:
        context(paddleslim.nas.ofa.supernet): search space defined by the user.
    Examples:
        .. code-block:: python
          from paddleslim.nas.ofa import supernet, Convert
          sp_net_config = supernet(kernel_size=(3, 5, 7), expand_ratio=[1, 2, 4])
          convert = Convert(sp_net_config)
    """

C
ceci3 已提交
58 59 60
    def __init__(self, context):
        self.context = context

C
ceci3 已提交
61 62 63 64 65 66 67
    def _change_name(self, layer, pd_ver, has_bias=True, conv=False):
        if conv:
            w_attr = layer._param_attr
        else:
            w_attr = layer._param_attr if pd_ver == 185 else layer._weight_attr

        if isinstance(w_attr, ParamAttr):
C
ceci3 已提交
68 69
            if w_attr != None and not isinstance(w_attr,
                                                 bool) and w_attr.name != None:
C
ceci3 已提交
70 71 72 73
                w_attr.name = 'super_' + w_attr.name

        if has_bias:
            if isinstance(layer._bias_attr, ParamAttr):
C
ceci3 已提交
74 75 76
                if layer._bias_attr != None and not isinstance(
                        layer._bias_attr,
                        bool) and layer._bias_attr.name != None:
C
ceci3 已提交
77 78
                    layer._bias_attr.name = 'super_' + layer._bias_attr.name

C
ceci3 已提交
79
    def convert(self, network):
C
ceci3 已提交
80 81 82 83 84 85 86 87 88 89 90
        """
        The function to convert the network to a supernet.
        Parameters:
            network(paddle.nn.Layer|list(paddle.nn.Layer)): instance of the model or list of instance of layers.
        Examples:
            .. code-block:: python
              from paddle.vision.models import mobilenet_v1
              from paddleslim.nas.ofa import supernet, Convert
              sp_net_config = supernet(kernel_size=(3, 5, 7), expand_ratio=[1, 2, 4])
              convert = Convert(sp_net_config).convert(mobilenet_v1())
        """
C
ceci3 已提交
91 92
        # search the first and last weight layer, don't change out channel of the last weight layer
        # don't change in channel of the first weight layer
C
ceci3 已提交
93 94 95 96 97 98 99
        model = []
        if isinstance(network, Layer):
            for name, sublayer in network.named_sublayers():
                model.append(sublayer)
        else:
            model = network

C
ceci3 已提交
100 101 102 103
        first_weight_layer_idx = -1
        last_weight_layer_idx = -1
        weight_layer_count = 0
        # NOTE: pre_channel store for shortcut module
C
ceci3 已提交
104
        pre_channel = None
C
ceci3 已提交
105 106 107
        cur_channel = None
        for idx, layer in enumerate(model):
            cls_name = layer.__class__.__name__.lower()
C
ceci3 已提交
108
            if 'conv' in cls_name or 'linear' in cls_name or 'embedding' in cls_name:
C
ceci3 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
                weight_layer_count += 1
                last_weight_layer_idx = idx
                if first_weight_layer_idx == -1:
                    first_weight_layer_idx = idx

        if getattr(self.context, 'channel', None) != None:
            assert len(
                self.context.channel
            ) == weight_layer_count, "length of channel must same as weight layer."

        for idx, layer in enumerate(model):
            if isinstance(layer, Conv2D):
                attr_dict = layer.__dict__
                key = attr_dict['_full_name']

                new_attr_name = [
C
ceci3 已提交
125
                    'stride', 'padding', 'dilation', 'groups', 'bias_attr'
C
ceci3 已提交
126
                ]
C
ceci3 已提交
127 128 129 130 131 132
                if pd_ver == 185:
                    new_attr_name += ['param_attr', 'use_cudnn', 'act', 'dtype']
                else:
                    new_attr_name += [
                        'weight_attr', 'data_format', 'padding_mode'
                    ]
C
ceci3 已提交
133

C
ceci3 已提交
134
                self._change_name(layer, pd_ver, conv=True)
C
ceci3 已提交
135
                new_attr_dict = dict.fromkeys(new_attr_name, None)
C
ceci3 已提交
136
                new_attr_dict['candidate_config'] = dict()
C
ceci3 已提交
137 138 139 140 141 142 143 144
                if pd_ver == 185:
                    new_attr_dict['num_channels'] = None
                    new_attr_dict['num_filters'] = None
                    new_attr_dict['filter_size'] = None
                else:
                    new_attr_dict['in_channels'] = None
                    new_attr_dict['out_channels'] = None
                    new_attr_dict['kernel_size'] = None
C
ceci3 已提交
145 146 147
                self.kernel_size = getattr(self.context, 'kernel_size', None)

                # if the kernel_size of conv is 1, don't change it.
C
ceci3 已提交
148 149 150
                fks = '_filter_size' if '_filter_size' in attr_dict.keys(
                ) else '_kernel_size'

C
ceci3 已提交
151
                ks = [attr_dict[fks]] if isinstance(
C
ceci3 已提交
152 153 154 155 156
                    attr_dict[fks], numbers.Integral) else attr_dict[fks]

                if self.kernel_size and int(ks[0]) != 1:
                    new_attr_dict['transform_kernel'] = True
                    new_attr_dict[fks[1:]] = max(self.kernel_size)
C
ceci3 已提交
157 158 159 160
                    new_attr_dict['candidate_config'].update({
                        'kernel_size': self.kernel_size
                    })
                else:
C
ceci3 已提交
161
                    new_attr_dict[fks[1:]] = attr_dict[fks]
C
ceci3 已提交
162

C
ceci3 已提交
163 164 165 166
                in_key = '_num_channels' if '_num_channels' in attr_dict.keys(
                ) else '_in_channels'
                out_key = '_num_filters' if '_num_filters' in attr_dict.keys(
                ) else '_out_channels'
C
ceci3 已提交
167 168 169
                if self.context.expand:
                    ### first super convolution
                    if idx == first_weight_layer_idx:
C
ceci3 已提交
170
                        new_attr_dict[in_key[1:]] = attr_dict[in_key]
C
ceci3 已提交
171
                    else:
C
ceci3 已提交
172 173 174
                        new_attr_dict[in_key[1:]] = int(self.context.expand *
                                                        attr_dict[in_key])

C
ceci3 已提交
175 176
                    ### last super convolution
                    if idx == last_weight_layer_idx:
C
ceci3 已提交
177
                        new_attr_dict[out_key[1:]] = attr_dict[out_key]
C
ceci3 已提交
178
                    else:
C
ceci3 已提交
179 180
                        new_attr_dict[out_key[1:]] = int(self.context.expand *
                                                         attr_dict[out_key])
C
ceci3 已提交
181 182 183 184 185
                        new_attr_dict['candidate_config'].update({
                            'expand_ratio': self.context.expand_ratio
                        })
                elif self.context.channel:
                    if attr_dict['_groups'] != None and (
C
ceci3 已提交
186 187
                            int(attr_dict['_groups']) == int(attr_dict[in_key])
                    ):
C
ceci3 已提交
188 189 190 191 192 193 194 195 196 197
                        ### depthwise conv, if conv is depthwise, use pre channel as cur_channel
                        _logger.warn(
                        "If convolution is a depthwise conv, output channel change" \
                        " to the same channel with input, output channel in search is not used."
                        )
                        cur_channel = pre_channel
                    else:
                        cur_channel = self.context.channel[0]
                    self.context.channel = self.context.channel[1:]
                    if idx == first_weight_layer_idx:
C
ceci3 已提交
198
                        new_attr_dict[in_key[1:]] = attr_dict[in_key]
C
ceci3 已提交
199
                    else:
C
ceci3 已提交
200
                        new_attr_dict[in_key[1:]] = max(pre_channel)
C
ceci3 已提交
201 202

                    if idx == last_weight_layer_idx:
C
ceci3 已提交
203
                        new_attr_dict[out_key[1:]] = attr_dict[out_key]
C
ceci3 已提交
204
                    else:
C
ceci3 已提交
205
                        new_attr_dict[out_key[1:]] = max(cur_channel)
C
ceci3 已提交
206 207 208 209 210
                        new_attr_dict['candidate_config'].update({
                            'channel': cur_channel
                        })
                        pre_channel = cur_channel
                else:
C
ceci3 已提交
211 212
                    new_attr_dict[in_key[1:]] = attr_dict[in_key]
                    new_attr_dict[out_key[1:]] = attr_dict[out_key]
C
ceci3 已提交
213 214

                for attr in new_attr_name:
C
ceci3 已提交
215 216 217 218
                    if attr == 'weight_attr':
                        new_attr_dict[attr] = attr_dict['_param_attr']
                    else:
                        new_attr_dict[attr] = attr_dict['_' + attr]
C
ceci3 已提交
219 220 221 222 223 224 225

                del layer

                if attr_dict['_groups'] == None or int(attr_dict[
                        '_groups']) == 1:
                    ### standard conv
                    layer = Block(SuperConv2D(**new_attr_dict), key=key)
C
ceci3 已提交
226
                elif int(attr_dict['_groups']) == int(attr_dict[in_key]):
C
ceci3 已提交
227 228 229
                    # if conv is depthwise conv, groups = in_channel, out_channel = in_channel,
                    # channel in candidate_config = in_channel_list
                    if 'channel' in new_attr_dict['candidate_config']:
C
ceci3 已提交
230 231
                        new_attr_dict[in_key[1:]] = max(cur_channel)
                        new_attr_dict[out_key[1:]] = new_attr_dict[in_key[1:]]
C
ceci3 已提交
232 233
                        new_attr_dict['candidate_config'][
                            'channel'] = cur_channel
C
ceci3 已提交
234
                    new_attr_dict['groups'] = new_attr_dict[in_key[1:]]
C
ceci3 已提交
235 236 237 238 239 240 241
                    layer = Block(
                        SuperDepthwiseConv2D(**new_attr_dict), key=key)
                else:
                    ### group conv
                    layer = Block(SuperGroupConv2D(**new_attr_dict), key=key)
                model[idx] = layer

C
ceci3 已提交
242 243 244 245
            elif isinstance(layer,
                            getattr(nn, 'BatchNorm2D', nn.BatchNorm)) and (
                                getattr(self.context, 'expand', None) != None or
                                getattr(self.context, 'channel', None) != None):
C
ceci3 已提交
246 247 248 249 250
                # num_features in BatchNorm don't change after last weight operators
                if idx > last_weight_layer_idx:
                    continue

                attr_dict = layer.__dict__
C
ceci3 已提交
251 252 253 254 255 256 257 258 259 260
                new_attr_name = ['momentum', 'epsilon', 'bias_attr']

                if pd_ver == 185:
                    new_attr_name += [
                        'param_attr', 'act', 'dtype', 'in_place', 'data_layout',
                        'is_test', 'use_global_stats', 'trainable_statistics'
                    ]
                else:
                    new_attr_name += ['weight_attr', 'data_format', 'name']

C
ceci3 已提交
261
                self._change_name(layer, pd_ver)
C
ceci3 已提交
262 263 264 265 266 267 268
                new_attr_dict = dict.fromkeys(new_attr_name, None)
                if pd_ver == 185:
                    new_attr_dict['num_channels'] = None
                else:
                    new_attr_dict['num_features'] = None
                new_key = 'num_channels' if 'num_channels' in new_attr_dict.keys(
                ) else 'num_features'
C
ceci3 已提交
269
                if self.context.expand:
C
ceci3 已提交
270 271
                    new_attr_dict[new_key] = int(
                        self.context.expand *
C
ceci3 已提交
272 273
                        layer._parameters['weight'].shape[0])
                elif self.context.channel:
C
ceci3 已提交
274
                    new_attr_dict[new_key] = max(cur_channel)
C
ceci3 已提交
275
                else:
C
ceci3 已提交
276 277 278
                    new_attr_dict[new_key] = attr_dict[
                        '_num_channels'] if '_num_channels' in attr_dict.keys(
                        ) else attr_dict['_num_features']
C
ceci3 已提交
279 280

                for attr in new_attr_name:
C
ceci3 已提交
281
                    new_attr_dict[attr] = attr_dict['_' + attr]
C
ceci3 已提交
282 283 284

                del layer, attr_dict

C
ceci3 已提交
285 286 287
                layer = layers.SuperBatchNorm(
                    **new_attr_dict
                ) if pd_ver == 185 else layers.SuperBatchNorm2D(**new_attr_dict)
C
ceci3 已提交
288 289 290 291 292 293 294 295 296
                model[idx] = layer

            ### assume output_size = None, filter_size != None
            ### NOTE: output_size != None may raise error, solve when it happend. 
            elif isinstance(layer, Conv2DTranspose):
                attr_dict = layer.__dict__
                key = attr_dict['_full_name']

                new_attr_name = [
C
ceci3 已提交
297
                    'stride', 'padding', 'dilation', 'groups', 'bias_attr'
C
ceci3 已提交
298
                ]
C
ceci3 已提交
299 300 301
                assert getattr(
                    attr_dict, '_filter_size', '_kernel_size'
                ) != None, "Conv2DTranspose only support kernel size != None now"
C
ceci3 已提交
302

C
ceci3 已提交
303 304 305 306 307 308 309 310 311 312
                if pd_ver == 185:
                    new_attr_name += [
                        'output_size', 'param_attr', 'use_cudnn', 'act', 'dtype'
                    ]
                else:
                    new_attr_name += [
                        'output_padding', 'weight_attr', 'data_format'
                    ]

                new_attr_dict = dict.fromkeys(new_attr_name, None)
C
ceci3 已提交
313
                new_attr_dict['candidate_config'] = dict()
C
ceci3 已提交
314 315 316 317 318 319 320 321
                if pd_ver == 185:
                    new_attr_dict['num_channels'] = None
                    new_attr_dict['num_filters'] = None
                    new_attr_dict['filter_size'] = None
                else:
                    new_attr_dict['in_channels'] = None
                    new_attr_dict['out_channels'] = None
                    new_attr_dict['kernel_size'] = None
C
ceci3 已提交
322 323

                self._change_name(layer, pd_ver, conv=True)
C
ceci3 已提交
324 325 326
                self.kernel_size = getattr(self.context, 'kernel_size', None)

                # if the kernel_size of conv transpose is 1, don't change it.
C
ceci3 已提交
327 328
                fks = '_filter_size' if '_filter_size' in attr_dict.keys(
                ) else '_kernel_size'
C
ceci3 已提交
329
                ks = [attr_dict[fks]] if isinstance(
C
ceci3 已提交
330 331 332 333 334
                    attr_dict[fks], numbers.Integral) else attr_dict[fks]

                if self.kernel_size and int(ks[0]) != 1:
                    new_attr_dict['transform_kernel'] = True
                    new_attr_dict[fks[1:]] = max(self.kernel_size)
C
ceci3 已提交
335 336 337 338
                    new_attr_dict['candidate_config'].update({
                        'kernel_size': self.kernel_size
                    })
                else:
C
ceci3 已提交
339
                    new_attr_dict[fks[1:]] = attr_dict[fks]
C
ceci3 已提交
340

C
ceci3 已提交
341 342 343 344
                in_key = '_num_channels' if '_num_channels' in attr_dict.keys(
                ) else '_in_channels'
                out_key = '_num_filters' if '_num_filters' in attr_dict.keys(
                ) else '_out_channels'
C
ceci3 已提交
345 346 347
                if self.context.expand:
                    ### first super convolution transpose
                    if idx == first_weight_layer_idx:
C
ceci3 已提交
348
                        new_attr_dict[in_key[1:]] = attr_dict[in_key]
C
ceci3 已提交
349
                    else:
C
ceci3 已提交
350 351
                        new_attr_dict[in_key[1:]] = int(self.context.expand *
                                                        attr_dict[in_key])
C
ceci3 已提交
352 353
                    ### last super convolution transpose
                    if idx == last_weight_layer_idx:
C
ceci3 已提交
354
                        new_attr_dict[out_key[1:]] = attr_dict[out_key]
C
ceci3 已提交
355
                    else:
C
ceci3 已提交
356 357
                        new_attr_dict[out_key[1:]] = int(self.context.expand *
                                                         attr_dict[out_key])
C
ceci3 已提交
358 359 360 361 362
                        new_attr_dict['candidate_config'].update({
                            'expand_ratio': self.context.expand_ratio
                        })
                elif self.context.channel:
                    if attr_dict['_groups'] != None and (
C
ceci3 已提交
363 364
                            int(attr_dict['_groups']) == int(attr_dict[in_key])
                    ):
C
ceci3 已提交
365 366 367 368 369 370 371 372 373 374
                        ### depthwise conv_transpose
                        _logger.warn(
                        "If convolution is a depthwise conv_transpose, output channel " \
                        "change to the same channel with input, output channel in search is not used."
                        )
                        cur_channel = pre_channel
                    else:
                        cur_channel = self.context.channel[0]
                    self.context.channel = self.context.channel[1:]
                    if idx == first_weight_layer_idx:
C
ceci3 已提交
375
                        new_attr_dict[in_key[1:]] = attr_dict[in_key]
C
ceci3 已提交
376
                    else:
C
ceci3 已提交
377
                        new_attr_dict[in_key[1:]] = max(pre_channel)
C
ceci3 已提交
378 379

                    if idx == last_weight_layer_idx:
C
ceci3 已提交
380
                        new_attr_dict[out_key[1:]] = attr_dict[out_key]
C
ceci3 已提交
381
                    else:
C
ceci3 已提交
382
                        new_attr_dict[out_key[1:]] = max(cur_channel)
C
ceci3 已提交
383 384 385 386 387
                        new_attr_dict['candidate_config'].update({
                            'channel': cur_channel
                        })
                        pre_channel = cur_channel
                else:
C
ceci3 已提交
388 389
                    new_attr_dict[in_key[1:]] = attr_dict[in_key]
                    new_attr_dict[out_key[1:]] = attr_dict[out_key]
C
ceci3 已提交
390 391

                for attr in new_attr_name:
C
ceci3 已提交
392 393 394 395 396 397
                    if attr == 'weight_attr':
                        new_attr_dict[attr] = attr_dict['_param_attr']
                    elif attr == 'output_padding':
                        new_attr_dict[attr] = attr_dict[attr]
                    else:
                        new_attr_dict[attr] = attr_dict['_' + attr]
C
ceci3 已提交
398 399 400

                del layer

C
ceci3 已提交
401
                if getattr(new_attr_dict, 'output_size', None) == []:
C
ceci3 已提交
402 403 404 405 406 407 408
                    new_attr_dict['output_size'] = None

                if attr_dict['_groups'] == None or int(attr_dict[
                        '_groups']) == 1:
                    ### standard conv_transpose
                    layer = Block(
                        SuperConv2DTranspose(**new_attr_dict), key=key)
C
ceci3 已提交
409
                elif int(attr_dict['_groups']) == int(attr_dict[in_key]):
C
ceci3 已提交
410 411 412
                    # if conv is depthwise conv, groups = in_channel, out_channel = in_channel,
                    # channel in candidate_config = in_channel_list
                    if 'channel' in new_attr_dict['candidate_config']:
C
ceci3 已提交
413 414
                        new_attr_dict[in_key[1:]] = max(cur_channel)
                        new_attr_dict[out_key[1:]] = new_attr_dict[in_key[1:]]
C
ceci3 已提交
415 416
                        new_attr_dict['candidate_config'][
                            'channel'] = cur_channel
C
ceci3 已提交
417
                    new_attr_dict['groups'] = new_attr_dict[in_key[1:]]
C
ceci3 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430
                    layer = Block(
                        SuperDepthwiseConv2DTranspose(**new_attr_dict), key=key)
                else:
                    ### group conv_transpose
                    layer = Block(
                        SuperGroupConv2DTranspose(**new_attr_dict), key=key)
                model[idx] = layer

            elif isinstance(layer, Linear) and (
                    getattr(self.context, 'expand', None) != None or
                    getattr(self.context, 'channel', None) != None):
                attr_dict = layer.__dict__
                key = attr_dict['_full_name']
C
ceci3 已提交
431
                if pd_ver == 185:
C
ceci3 已提交
432
                    new_attr_name = ['act', 'dtype']
C
ceci3 已提交
433 434
                else:
                    new_attr_name = ['weight_attr', 'bias_attr']
C
ceci3 已提交
435
                self._change_name(layer, pd_ver) if pd_ver != 185 else None
C
ceci3 已提交
436 437
                in_nc, out_nc = layer._parameters['weight'].shape

C
ceci3 已提交
438
                new_attr_dict = dict.fromkeys(new_attr_name, None)
C
ceci3 已提交
439
                new_attr_dict['candidate_config'] = dict()
C
ceci3 已提交
440 441 442 443 444 445 446
                if pd_ver == 185:
                    new_attr_dict['input_dim'] = None
                    new_attr_dict['output_dim'] = None
                else:
                    new_attr_dict['in_features'] = None
                    new_attr_dict['out_features'] = None

C
ceci3 已提交
447 448
                in_key = '_input_dim' if pd_ver == 185 else '_in_features'
                out_key = '_output_dim' if pd_ver == 185 else '_out_features'
C
ceci3 已提交
449 450
                attr_dict[in_key] = in_nc
                attr_dict[out_key] = out_nc
C
ceci3 已提交
451 452
                if self.context.expand:
                    if idx == first_weight_layer_idx:
C
ceci3 已提交
453
                        new_attr_dict[in_key[1:]] = int(attr_dict[in_key])
C
ceci3 已提交
454
                    else:
C
ceci3 已提交
455 456
                        new_attr_dict[in_key[1:]] = int(self.context.expand *
                                                        attr_dict[in_key])
C
ceci3 已提交
457 458

                    if idx == last_weight_layer_idx:
C
ceci3 已提交
459
                        new_attr_dict[out_key[1:]] = int(attr_dict[out_key])
C
ceci3 已提交
460
                    else:
C
ceci3 已提交
461 462
                        new_attr_dict[out_key[1:]] = int(self.context.expand *
                                                         attr_dict[out_key])
C
ceci3 已提交
463 464 465 466 467 468 469
                        new_attr_dict['candidate_config'].update({
                            'expand_ratio': self.context.expand_ratio
                        })
                elif self.context.channel:
                    cur_channel = self.context.channel[0]
                    self.context.channel = self.context.channel[1:]
                    if idx == first_weight_layer_idx:
C
ceci3 已提交
470
                        new_attr_dict[in_key[1:]] = int(attr_dict[in_key])
C
ceci3 已提交
471
                    else:
C
ceci3 已提交
472
                        new_attr_dict[in_key[1:]] = max(pre_channel)
C
ceci3 已提交
473 474

                    if idx == last_weight_layer_idx:
C
ceci3 已提交
475
                        new_attr_dict[out_key[1:]] = int(attr_dict[out_key])
C
ceci3 已提交
476
                    else:
C
ceci3 已提交
477
                        new_attr_dict[out_key[1:]] = max(cur_channel)
C
ceci3 已提交
478 479 480 481 482
                        new_attr_dict['candidate_config'].update({
                            'channel': cur_channel
                        })
                        pre_channel = cur_channel
                else:
C
ceci3 已提交
483 484
                    new_attr_dict[in_key[1:]] = int(attr_dict[in_key])
                    new_attr_dict[out_key[1:]] = int(attr_dict[out_key])
C
ceci3 已提交
485 486

                for attr in new_attr_name:
C
ceci3 已提交
487
                    new_attr_dict[attr] = attr_dict['_' + attr]
C
ceci3 已提交
488 489 490 491 492 493

                del layer, attr_dict

                layer = Block(SuperLinear(**new_attr_dict), key=key)
                model[idx] = layer

C
ceci3 已提交
494 495 496 497 498 499
            elif isinstance(
                    layer,
                    getattr(nn, 'InstanceNorm2D',
                            paddle.fluid.dygraph.nn.InstanceNorm)) and (
                                getattr(self.context, 'expand', None) != None or
                                getattr(self.context, 'channel', None) != None):
C
ceci3 已提交
500 501 502 503 504
                # num_features in InstanceNorm don't change after last weight operators
                if idx > last_weight_layer_idx:
                    continue

                attr_dict = layer.__dict__
C
ceci3 已提交
505 506 507 508 509 510
                if pd_ver == 185:
                    new_attr_name = [
                        'bias_attr', 'epsilon', 'param_attr', 'dtype'
                    ]
                else:
                    new_attr_name = ['bias_attr', 'epsilon', 'weight_attr']
C
ceci3 已提交
511 512

                self._change_name(layer, pd_ver)
C
ceci3 已提交
513 514 515 516 517 518 519 520 521 522 523
                new_attr_dict = dict.fromkeys(new_attr_name, None)
                if pd_ver == 185:
                    new_attr_dict['num_channels'] = None
                else:
                    new_attr_dict['num_features'] = None
                new_key = '_num_channels' if '_num_channels' in new_attr_dict.keys(
                ) else '_num_features'
                ### 10 is a default channel in the case of weight_attr=False, in this condition, num of channels if useless, so give it arbitrarily.
                attr_dict[new_key] = layer._parameters['scale'].shape[0] if len(
                    layer._parameters) != 0 else 10

C
ceci3 已提交
524
                if self.context.expand:
C
ceci3 已提交
525 526
                    new_attr_dict[new_key[1:]] = int(self.context.expand *
                                                     attr_dict[new_key])
C
ceci3 已提交
527
                elif self.context.channel:
C
ceci3 已提交
528
                    new_attr_dict[new_key[1:]] = max(cur_channel)
C
ceci3 已提交
529
                else:
C
ceci3 已提交
530
                    new_attr_dict[new_key[1:]] = attr_dict[new_key]
C
ceci3 已提交
531 532

                for attr in new_attr_name:
C
ceci3 已提交
533
                    new_attr_dict[attr] = attr_dict['_' + attr]
C
ceci3 已提交
534 535 536

                del layer, attr_dict

C
ceci3 已提交
537 538 539 540
                layer = layers.SuperInstanceNorm(
                    **new_attr_dict
                ) if pd_ver == 185 else layers.SuperInstanceNorm2D(
                    **new_attr_dict)
C
ceci3 已提交
541 542
                model[idx] = layer

C
ceci3 已提交
543 544 545 546 547 548 549 550
            elif isinstance(layer, LayerNorm) and (
                    getattr(self.context, 'expand', None) != None or
                    getattr(self.context, 'channel', None) != None):
                ### TODO(ceci3): fix when normalized_shape != last_dim_of_input
                if idx > last_weight_layer_idx:
                    continue

                attr_dict = layer.__dict__
C
ceci3 已提交
551 552 553 554 555 556 557 558
                new_attr_name = ['epsilon', 'bias_attr']
                if pd_ver == 185:
                    new_attr_name += [
                        'scale', 'shift', 'param_attr', 'act', 'dtype'
                    ]
                else:
                    new_attr_name += ['weight_attr']

C
ceci3 已提交
559
                self._change_name(layer, pd_ver)
C
ceci3 已提交
560 561
                new_attr_dict = dict.fromkeys(new_attr_name, None)
                new_attr_dict['normalized_shape'] = None
C
ceci3 已提交
562
                if self.context.expand:
C
ceci3 已提交
563 564
                    new_attr_dict['normalized_shape'] = int(
                        self.context.expand * attr_dict['_normalized_shape'][0])
C
ceci3 已提交
565 566 567 568 569 570 571
                elif self.context.channel:
                    new_attr_dict['normalized_shape'] = max(cur_channel)
                else:
                    new_attr_dict['normalized_shape'] = attr_dict[
                        '_normalized_shape']

                for attr in new_attr_name:
C
ceci3 已提交
572
                    new_attr_dict[attr] = attr_dict['_' + attr]
C
ceci3 已提交
573 574 575 576 577 578 579 580 581 582

                del layer, attr_dict
                layer = SuperLayerNorm(**new_attr_dict)
                model[idx] = layer

            elif isinstance(layer, Embedding) and (
                    getattr(self.context, 'expand', None) != None or
                    getattr(self.context, 'channel', None) != None):
                attr_dict = layer.__dict__
                key = attr_dict['_full_name']
C
ceci3 已提交
583
                new_attr_name = []
C
ceci3 已提交
584 585
                if pd_ver == 185:
                    new_attr_name += [
C
ceci3 已提交
586
                        'is_sparse', 'is_distributed', 'param_attr', 'dtype'
C
ceci3 已提交
587 588
                    ]
                else:
C
ceci3 已提交
589
                    new_attr_name += ['sparse', 'weight_attr', 'name']
C
ceci3 已提交
590

C
ceci3 已提交
591 592
                self._change_name(layer, pd_ver, has_bias=False)

C
ceci3 已提交
593
                new_attr_dict = dict.fromkeys(new_attr_name, None)
C
ceci3 已提交
594 595 596
                new_attr_dict['candidate_config'] = dict()
                bef_size = attr_dict['_size']
                if self.context.expand:
C
ceci3 已提交
597 598 599 600 601 602 603 604 605 606
                    if pd_ver == 185:
                        new_attr_dict['size'] = [
                            bef_size[0], int(self.context.expand * bef_size[1])
                        ]
                    else:
                        new_attr_dict['num_embeddings'] = attr_dict[
                            '_num_embeddings']
                        new_attr_dict['embedding_dim'] = int(
                            self.context.expand * attr_dict['_embedding_dim'])

C
ceci3 已提交
607 608 609 610 611 612 613
                    new_attr_dict['candidate_config'].update({
                        'expand_ratio': self.context.expand_ratio
                    })

                elif self.context.channel:
                    cur_channel = self.context.channel[0]
                    self.context.channel = self.context.channel[1:]
C
ceci3 已提交
614 615 616 617 618 619 620
                    if pd_ver == 185:
                        new_attr_dict['size'] = [bef_size[0], max(cur_channel)]
                    else:
                        new_attr_dict['num_embeddings'] = attr_dict[
                            '_num_embeddings']
                        new_attr_dict['embedding_dim'] = max(cur_channel)

C
ceci3 已提交
621 622 623 624 625
                    new_attr_dict['candidate_config'].update({
                        'channel': cur_channel
                    })
                    pre_channel = cur_channel
                else:
C
ceci3 已提交
626 627 628 629 630 631 632
                    if pf_ver == 185:
                        new_attr_dict['size'] = bef_size
                    else:
                        new_attr_dict['num_embeddings'] = attr_dict[
                            '_num_embeddings']
                        new_attr_dict['embedding_dim'] = attr_dict[
                            '_embedding_dim']
C
ceci3 已提交
633 634

                for attr in new_attr_name:
C
ceci3 已提交
635
                    new_attr_dict[attr] = attr_dict['_' + attr]
C
ceci3 已提交
636

C
ceci3 已提交
637 638 639
                new_attr_dict['padding_idx'] = None if attr_dict[
                    '_padding_idx'] == -1 else attr_dict['_padding_idx']

C
ceci3 已提交
640 641 642 643 644
                del layer, attr_dict

                layer = Block(SuperEmbedding(**new_attr_dict), key=key)
                model[idx] = layer

C
ceci3 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
        def split_prefix(net, name_list):
            if len(name_list) > 1:
                net = split_prefix(getattr(net, name_list[0]), name_list[1:])
            elif len(name_list) == 1:
                net = getattr(net, name_list[0])
            else:
                raise NotImplementedError("name error")
            return net

        if isinstance(network, Layer):
            for idx, (name, sublayer) in enumerate(network.named_sublayers()):
                if len(name.split('.')) > 1:
                    net = split_prefix(network, name.split('.')[:-1])
                else:
                    net = network
                setattr(net, name.split('.')[-1], model[idx])

        return network
C
ceci3 已提交
663 664 665


class supernet:
C
ceci3 已提交
666 667 668 669 670 671 672 673
    """
    Search space of the network.
    Parameters:
        kernel_size(list|tuple, optional): search space for the kernel size of the Conv2D.
        expand_ratio(list|tuple, optional): the search space for the expand ratio of the number of channels of Conv2D, the expand ratio of the output dimensions of the Embedding or Linear, which means this parameter get the number of channels of each OP in the converted super network based on the the channels of each OP in the original model, so this parameter The length is 1. Just set one between this parameter and ``channel``.
        channel(list|tuple, optional): the search space for the number of channels of Conv2D, the output dimensions of the Embedding or Linear, this parameter directly sets the number of channels of each OP in the super network, so the length of this parameter needs to be the same as the total number that of Conv2D, Embedding, and Linear included in the network. Just set one between this parameter and ``expand_ratio``.
    """

C
ceci3 已提交
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

        assert (
            getattr(self, 'expand_ratio', None) == None or
            getattr(self, 'channel', None) == None
        ), "expand_ratio and channel CANNOT be NOT None at the same time."

        self.expand = None
        if 'expand_ratio' in kwargs.keys():
            if isinstance(self.expand_ratio, list) or isinstance(
                    self.expand_ratio, tuple):
                self.expand = max(self.expand_ratio)
            elif isinstance(self.expand_ratio, int):
                self.expand = self.expand_ratio
C
ceci3 已提交
690 691
        if 'channel' not in kwargs.keys():
            self.channel = None
C
ceci3 已提交
692 693 694 695 696

    def __enter__(self):
        return Convert(self)

    def __exit__(self, exc_type, exc_val, exc_tb):
C
ceci3 已提交
697 698 699
        self.expand = None
        self.channel = None
        self.kernel_size = None
C
ceci3 已提交
700 701 702 703 704 705 706 707 708


#def ofa_supernet(kernel_size, expand_ratio):
#    def _ofa_supernet(func):
#        @functools.wraps(func)
#        def convert(*args, **kwargs):
#            supernet_convert(*args, **kwargs)
#        return convert
#    return _ofa_supernet