extract_features.py 16.6 KB
Newer Older
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
# Copyright (c) 2021  PaddlePaddle Authors. All Rights Reserved.
#
# 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 re
import numpy as np
from .parse_ops import get_key_from_op
__all__ = ["get_data_from_tables", "get_features_from_paramkey"]


def cal_flops_params(op_type, cin, cout, kernel=1, h=1, w=1):
    # cin: weight[1]
    if 'conv' in op_type:
        params = cout * (kernel * kernel * cin + 1)
        flops = 2 * kernel * kernel * h * w * cin * cout
        return flops, params
    elif "fc" in op_type:
        flops = 2 * cin * cout
        params = (cin + 1) * cout
        return flops, params


def get_data_from_tables(table_dict, op_type, data_type='fp32'):
    data = []
    for param_key in table_dict:
        cur_type = param_key.split()[0]
        if op_type == cur_type:
            features = get_features_from_paramkey(param_key, op_type, data_type)
            if features == None:
                continue
41 42 43
            # only support bs=1 now
            if features[0] != 1:
                continue
44 45 46 47 48 49 50 51
            features.append(table_dict[param_key])
            data.append(features)
    return np.array(data)


def get_features_from_paramkey(param_key, op_type, data_type):
    """Get op's parameters according to the key of latency table
    """
52
    features = None
53 54 55 56 57 58 59 60 61 62

    if 'conv2d' in op_type:
        flag_quant = 'quant=None' if data_type == 'fp32' else 'quant=True'
        if flag_quant not in param_key:
            return None

        weight = re.search(r'weight=(\(\d*, \d*, \d*, \d*\))',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(', ')
63
        outputs = re.search(r'out=(\(-*\d*, \d*, -?\d*, -?\d*\))',
64 65 66
                            param_key).group().split('=')[-1].strip(
                                '('
                                ')').split(', ')
67 68
        batchsize = int(outputs[0])
        cout = int(outputs[1])
69 70 71 72 73 74 75 76 77 78 79
        cin = int(weight[1])
        kernel = int(weight[2])
        out_h = int(outputs[2])
        out_w = int(outputs[3])
        stride = int(re.search(r'stride=\d*', param_key).group().split('=')[1])
        group = int(re.search(r'group=\d*', param_key).group().split('=')[1])
        pad = int(re.search(r'pad=\d', param_key).group().split('=')[1])
        flops, params = cal_flops_params('conv', cin, cout, kernel, out_h,
                                         out_w)

        if data_type == 'fp32':
80
            inputs = re.search(r'in=(\(-*\d*, \d*, -?\d*, -?\d*\))',
81 82 83 84 85 86 87 88
                               param_key).group().split('=')[-1].strip(
                                   '('
                                   ')').split(', ')
            in_c = int(inputs[1])
            in_h = int(inputs[2])
            in_w = int(inputs[3])

            features = [
89
                batchsize, in_c, cout, kernel, group, stride, pad, in_h * in_w,
90 91 92 93
                out_h * out_w
            ]
        else:
            features = [
94 95
                batchsize, cin, cout, kernel, group, stride, pad, out_h * out_w,
                flops, params
96 97 98
            ]

    elif 'matmul' in op_type:
99
        X = re.search(r'X=(\((-?\d+,* *)+\))',
100 101
                      param_key).group().split('=')[-1].strip('('
                                                              ')').split(', ')
102
        Y = re.search(r'Y=(\((-?\d+,* *)+\))',
103 104 105 106 107 108 109 110
                      param_key).group().split('=')[-1].strip('('
                                                              ')').split(', ')

        a = int(X[0])
        b = int(Y[0])
        c = int(Y[1])
        flops, params = cal_flops_params('fc', b, c)

111
        features = [a, b, c, flops, params]
112 113 114 115 116 117 118

    elif ('batch_norm' in op_type or 'layer_norm' in op_type):
        inputs = re.search(r'in=(\((-?\d+,* *)+\))',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(', ')

119 120
        features = [0, 0, 0, 0]
        for i in range(len(inputs)):
121 122
            if inputs[i] == '':
                continue
123
            features[i] = int(inputs[i])
124 125 126 127 128 129 130 131 132 133 134

    elif 'pool2d' in op_type:

        inputs = re.search(r'in=(\(-*\d*, \d*, \d*, \d*\))',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(', ')
        outputs = re.search(r'out=(\(-*\d*, \d*, \d*, \d*\))',
                            param_key).group().split('=')[-1].strip(
                                '('
                                ')').split(', ')
135
        batchsize = int(inputs[0])
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        cin = int(inputs[1])
        in_h = int(inputs[2])
        in_w = int(inputs[3])
        out_h = int(outputs[2])
        out_w = int(outputs[3])
        kernel = int(
            re.search(r'kernel=\d*x*\d*', param_key).group().split('x')[-1])
        flag_global = int(
            re.search(r'flag_global=\d', param_key).group().split('=')[-1])
        if flag_global:
            kernel = in_h
        stride = int(re.search(r'stride=\d', param_key).group().split('=')[-1])
        pad = int(re.search(r'pad=\d', param_key).group().split('=')[-1])
        flag_type = 1 if 'type=avg' in param_key else 0

        features = [
152 153
            batchsize, cin, kernel, stride, pad, in_h * in_w, out_h * out_w,
            flag_type
154 155 156 157 158 159 160 161 162 163 164 165
        ]

    elif ('reshape' in op_type or 'scale' in op_type):
        inputs = re.search(r'in=(\((-?\d+,* *)+\))',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(',')
        outputs = re.search(r'out=(\((-?\d+,* *)+\))',
                            param_key).group().split('=')[-1].strip(
                                '('
                                ')').split(',')

166 167
        # inputs[4], ouputs[4]/[5]
        features = [0, 0, 0, 0, 0, 0, 0, 0, 0]
168 169 170 171 172 173 174 175 176 177 178 179 180
        for i in range(len(inputs)):
            if inputs[i] == '':
                continue
            features[i] = int(inputs[i])
        for i in range(len(outputs)):
            if outputs[i] == '':
                continue
            features[i + 4] = int(outputs[i])

    elif ('hard_swish' in op_type or 'relu' in op_type or
          'leaky_relu' in op_type or 'tanh' in op_type or 'swish' in op_type or
          'softmax' in op_type or 'hard_sigmoid' in op_type or
          'sigmoid' in op_type or 'gelu' in op_type or 'clip' in op_type or
181
          'shape' in op_type or 'interp_v2' in op_type):
182 183 184 185 186 187

        inputs = re.search(r'in=(\((-?\d+,* *)+\))',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(', ')

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        # N, C, H, W
        features = [0, 0, 0, 0]
        for i in range(len(inputs)):
            features[i] = int(inputs[i])

    elif 'transpose' in op_type:
        inputs = re.search(r'in=(\((-?\d+,* *)+\))',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(', ')

        # inputs[4]/[5]
        features = [0, 0, 0, 0, 0]
        for i in range(len(inputs)):
            features[i] = int(inputs[i])
203 204 205 206 207 208 209 210 211

    elif 'elementwise' in op_type:
        X = re.search(r'X=\((-?\d+,* *)+\)',
                      param_key).group().split('=')[-1].strip('('
                                                              ')').split(',')

        Y = re.search(r'Y=\((-?\d+,* *)+\)',
                      param_key).group().split('=')[-1].strip('('
                                                              ')').split(',')
212 213 214
        # X[0] X[1] X[2] X[3] Y[1] Y[2] Y[3]
        features = [0, 0, 0, 0, 0, 0, 0]
        for i in range(len(X)):
215 216
            if X[i] == '':
                continue
Z
zhouzj 已提交
217
            features[i] = int(X[i])
218 219 220 221 222
        for i in range(0, len(Y)):
            if len(Y) == 4 and i == 0:
                continue
            if Y[i] == '':
                continue
223
            features[i + 3] = int(Y[i])
224 225 226 227 228 229 230 231 232 233

    elif 'concat' in op_type:
        inputs = re.search(r'in=(\((-?\d+,* *)+\))+',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(')(')

        channels = []
        for ins in inputs:
            channels.append(int(ins.split(', ')[1]))
234 235
        # bs, hw, c1,c2,c3,c4,c5,c6,c7,c8,c9
        features = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
236
        input1 = inputs[0].split(', ')
237
        features[0] = int(input1[0])
238
        if len(input1) == 3:
239
            features[1] = int(input1[2])
240
        else:
241
            features[1] = int(input1[2]) * int(input1[3])
242 243

        for i in range(len(channels)):
244
            features[i + 2] = channels[i]
245 246 247 248 249 250 251 252 253 254

    elif 'yolo_box' in op_type:
        outputs = re.search(r'out=(\(-?\d*, \d*, \d*\))',
                            param_key).group().split('=')[-1].strip(
                                '('
                                ')').split(', ')
        inputs = re.search(r'in=(\(-?\d*, \d*, \d*, \d*\))',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(', ')
255
        batchsize = int(inputs[0])
256 257 258 259 260 261 262
        cin = int(inputs[1])
        h = int(inputs[2])
        w = int(inputs[3])
        cout = int(outputs[1])
        class_num = int(
            re.search(r'class_num=\d*', param_key).group().split('=')[-1])

263
        features = [batchsize, cin, h * w, cout, class_num]
264 265 266 267 268 269

    elif 'prior_box' in op_type:
        inputs = re.search(r'in=\((-?\d+,* *)+\)',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(',')
270
        batchsize = int(inputs[0])
271 272 273 274
        cin = int(inputs[1])
        h = int(inputs[2])
        w = int(inputs[3])

275
        features = [batchsize, cin, h, w]
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

    elif 'slice' in op_type:
        inputs = re.search(r'in=\((-?\d+,* *)+\)',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(',')
        features = [0, 0, 0, 0]
        for i in range(len(inputs)):
            if inputs[i] == '':
                continue
            features[i] = int(inputs[i])

    elif 'exp' in op_type:
        inputs = re.search(r'in=\((-?\d+,* *)+\)',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(',')
        features = [0, 0, 0, 0]
        for i in range(len(inputs)):
            if inputs[i] == '':
                continue
            features[i] = int(inputs[i])

    elif 'dropout' in param_key:
        inputs = re.search(r'in=\((-?\d+,* *)+\)',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(',')

        features = [0, 0, 0, 0]
        for i in range(len(inputs)):
            if inputs[i] == '':
                continue
            features[i] = int(inputs[i])

    elif 'shuffle_channel' in op_type:
        inputs = re.search(r'in=(\(-*\d*, \d*, \d*, \d*\))',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(', ')
316
        batchsize = int(inputs[0])
317 318 319 320 321
        cin = int(inputs[1])
        in_h = int(inputs[2])
        in_w = int(inputs[3])
        group = int(re.search(r'group=\d*', param_key).group().split('=')[1])

322
        features = [batchsize, cin, in_h, in_w, group]
323 324

    elif 'split' in op_type:
325
        inputs = re.search(r'in=(\(-*\d*, \d*, -?\d*, -?\d*\))',
326 327 328
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(', ')
329
        batchsize = int(inputs[0])
330 331 332 333
        cin = int(inputs[1])
        in_h = int(inputs[2])
        in_w = int(inputs[2])

334
        features = [batchsize, cin, in_h, in_w]
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

    elif 'squeeze' in op_type:
        inputs = re.search(r'in=\((-?\d+,* *)+\)',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(',')
        features = [0, 0, 0, 0]
        for i in range(len(inputs)):
            if inputs[i] == '':
                continue
            features[i] = int(inputs[i])

    elif 'flatten_contiguous_range' in op_type:
        inputs = re.search(r'in=(\(-?\d*, \d*, \d*, \d*\))',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(', ')

353 354 355
        features = [
            int(inputs[0]), int(inputs[1]), int(inputs[2]), int(inputs[3])
        ]
356 357 358 359 360 361 362 363 364 365

    elif ('calib' in op_type or 'floor' in op_type):
        inputs = re.search(r'in=\((-?\d+,* *)+\)',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(',')
        outputs = re.search(r'out=\((-?\d+,* *)+\)',
                            param_key).group().split('=')[-1].strip(
                                '('
                                ')').split(',')
366 367 368 369 370 371
        # inputs[4] outputs[4]
        features = [0, 0, 0, 0, 0, 0, 0, 0]
        for i in range(len(inputs)):
            features[i] = int(inputs[i])
        for i in range(len(outputs)):
            features[i + 4] = int(outputs[i])
372 373 374 375 376 377 378 379 380 381 382 383

    elif 'uniform_random' in op_type:
        shape = re.search(r'shape=\[(-?\d+,* *)+\]',
                          param_key).group().split('=')[-1].strip(
                              '['
                              ']').split(',')
        features = [0, 0, 0, 0]
        for i in range(len(shape)):
            if shape[i] == '':
                continue
            features[i] = int(shape[i])

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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
    elif 'arg_max' in op_type:
        inputs = re.search(r'in=\((-?\d+,* *)+\)',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(',')
        outputs = re.search(r'out=\((-?\d+,* *)+\)',
                            param_key).group().split('=')[-1].strip(
                                '('
                                ')').split(',')

        # inputs[4], outputs[4]
        features = [0, 0, 0, 0, 0, 0, 0, 0]
        for i in range(len(inputs)):
            if inputs[i] == '':
                continue
            features[i] = int(inputs[i])
        for i in range(len(outputs)):
            if outputs[i] == '':
                continue
            features[i + 4] = int(outputs[i])

    elif 'fill_constant_batch_size_like' in op_type:
        inputs = re.search(r'in=\((-?\d+,* *)+\)',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(',')
        outputs = re.search(r'out=\((-?\d+,* *)+\)',
                            param_key).group().split('=')[-1].strip(
                                '('
                                ')').split(',')

        # inputs[4], outputs[4]
        features = [0, 0, 0, 0, 0, 0, 0, 0]
        for i in range(len(inputs)):
            if inputs[i] == '':
                continue
            features[i] = int(inputs[i])
        for i in range(len(outputs)):
            if outputs[i] == '':
                continue
            features[i + 4] = int(outputs[i])

    elif op_type == 'rnn':
        inputs = re.search(r'in=\((-?\d+,* *)+\)',
                           param_key).group().split('=')[-1].strip(
                               '('
                               ')').split(',')
        inputs[0], inputs[1] = inputs[1], inputs[0]
        outputs = re.search(r'out=\((-?\d+,* *)+\)',
                            param_key).group().split('=')[-1].strip(
                                '('
                                ')').split(',')
        outputs[0], outputs[1] = outputs[1], outputs[0]

        # inputs[3], outputs[3]
        features = [0, 0, 0, 0, 0, 0]
        for i in range(len(inputs)):
            if inputs[i] == '':
                continue
            features[i] = int(inputs[i])
        for i in range(len(outputs)):
            if outputs[i] == '':
                continue
            features[i + 3] = int(outputs[i])

449
    return features