transformer.py 109.6 KB
Newer Older
L
Liangliang He 已提交
1
# Copyright 2018 The MACE Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# 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.


Y
yejianwu 已提交
16
import re
17

18 19 20
import numpy as np
import six

L
liyin 已提交
21 22
from py_proto import mace_pb2
from transform import base_converter
B
Bin Li 已提交
23
from transform.base_converter import ActivationType
L
liyin 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37
from transform.base_converter import ConverterUtil
from transform.base_converter import DataFormat
from transform.base_converter import DeviceType
from transform.base_converter import EltwiseType
from transform.base_converter import FrameworkType
from transform.base_converter import MaceKeyword
from transform.base_converter import MaceOp
from transform.base_converter import MaceFixedDataFormatOps  # noqa
from transform.base_converter import MaceTransposableDataFormatOps  # noqa
from transform.base_converter import PaddingMode
from transform.base_converter import ReduceType
from transform.base_converter import TransformerRule
from quantize import quantize_util
from utils.util import mace_check
38 39 40 41 42 43 44 45 46


class Transformer(base_converter.ConverterInterface):
    """A class for transform naive mace model to optimized model.
    This Transformer should be platform irrelevant. So, do not assume
    tensor name has suffix like ':0".
    """

    def __init__(self, option, model):
47 48
        # Dependencies
        # (TRANSFORM_MATMUL_TO_FC, TRANSFORM_GLOBAL_CONV_TO_FC) -> RESHAPE_FC_WEIGHT  # noqa
49
        self._registered_transformers = {
李寅 已提交
50 51
            TransformerRule.TRANSFORM_FAKE_QUANTIZE:
                self.transform_fake_quantize,
52
            TransformerRule.REMOVE_USELESS_OP: self.remove_useless_op,
53 54
            TransformerRule.TRANSFORM_GLOBAL_POOLING:
                self.transform_global_pooling,
Y
yejianwu 已提交
55 56 57 58
            TransformerRule.TRANSFORM_LSTMCELL_ZEROSTATE:
                self.transform_lstmcell_zerostate,
            TransformerRule.TRANSFORM_BASIC_LSTMCELL:
                self.transform_basic_lstmcell,
李寅 已提交
59 60 61
            TransformerRule.FOLD_RESHAPE: self.fold_reshape,
            TransformerRule.TRANSFORM_MATMUL_TO_FC:
                self.transform_matmul_to_fc,
62
            TransformerRule.FOLD_BATCHNORM: self.fold_batchnorm,
63
            TransformerRule.FOLD_BIASADD: self.fold_biasadd,
64 65
            TransformerRule.FOLD_CONV_AND_BN:
                self.fold_conv_and_bn,  # data_format related
L
liutuo 已提交
66 67
            TransformerRule.FOLD_DECONV_AND_BN:
                self.fold_deconv_and_bn,  # data_format related
68 69 70 71
            TransformerRule.FOLD_DEPTHWISE_CONV_AND_BN:
                self.fold_depthwise_conv_and_bn,  # data_format related
            TransformerRule.TRANSFORM_ADD_TO_BIASADD:
                self.transform_add_to_biasadd,
B
Bin Li 已提交
72 73
            TransformerRule.REARRANGE_BATCH_TO_SPACE:
                self.rearrange_batch_to_space,
74
            TransformerRule.FLATTEN_ATROUS_CONV: self.flatten_atrous_conv,
75
            TransformerRule.FOLD_ACTIVATION: self.fold_activation,
76
            TransformerRule.FOLD_SQRDIFF_MEAN: self.fold_squared_diff_mean,
李寅 已提交
77
            TransformerRule.FOLD_EMBEDDING_LOOKUP: self.fold_embedding_lookup,
78
            TransformerRule.TRANSPOSE_FILTERS: self.transpose_filters,
79 80
            TransformerRule.TRANSPOSE_MATMUL_WEIGHT:
                self.transpose_matmul_weight,
Y
yejianwu 已提交
81 82
            TransformerRule.FOLD_FC_RESHAPE:
                self.fold_fc_reshape,
L
liuqi 已提交
83 84
            TransformerRule.ADD_IN_OUT_TENSOR_INFO:
                self.add_in_out_tensor_info,
85
            TransformerRule.ADD_WINOGRAD_ARG: self.add_winograd_arg,
86 87
            TransformerRule.TRANSFORM_GLOBAL_CONV_TO_FC:
                self.transform_global_conv_to_fc,
L
liuqi 已提交
88
            TransformerRule.RESHAPE_FC_WEIGHT: self.reshape_fc_weight,
李寅 已提交
89 90 91 92 93 94
            TransformerRule.QUANTIZE_NODES:
                self.quantize_nodes,
            TransformerRule.ADD_QUANTIZE_TENSOR_RANGE:
                self.add_quantize_tensor_range,
            TransformerRule.QUANTIZE_WEIGHTS:
                self.quantize_weights,
95 96
            TransformerRule.UPDATE_FLOAT_OP_DATA_TYPE:
                self.update_float_op_data_type,
97 98
            TransformerRule.ADD_OPENCL_INFORMATIONS:
                self.add_opencl_informations,
99
            TransformerRule.SORT_BY_EXECUTION: self.sort_by_execution,
100
            TransformerRule.UPDATE_DATA_FORMAT: self.update_data_format,
L
luxuhui 已提交
101 102
            TransformerRule.TRANSPOSE_RESHAPE_AND_FLATTEN:
                self.transform_reshape_and_flatten,
103 104
            TransformerRule.TRANSPOSE_SHAPE_TENSOR_TO_PARAM:
                self.transform_shape_tensor_to_param,
105
            TransformerRule.TRANSPOSE_DATA_FORMAT: self.transpose_data_format,
李寅 已提交
106 107
            TransformerRule.CHECK_QUANTIZE_INFO:
                self.check_quantize_info,
李寅 已提交
108 109
            TransformerRule.TRANSFORM_CHANNEL_SHUFFLE:
                self.transform_channel_shuffle,
110 111
            TransformerRule.QUANTIZE_SPECIFIC_OPS_ONLY:
                self.quantize_specific_ops_only,
112 113
            TransformerRule.FP16_MATMUL_WEIGHT:
                self.fp16_matmul_weight,
Y
yulianfei 已提交
114 115
            TransformerRule.FP16_GATHER_WEIGHT:
                self.fp16_gather_weight,
B
Bin Li 已提交
116 117
            TransformerRule.QUANTIZE_LARGE_WEIGHTS:
                self.quantize_large_weights,
118
        }
119 120 121

        self._option = option
        self._model = model
122
        self._wino_arg = self._option.winograd
123 124 125 126 127

        self._ops = {}
        self._consts = {}
        self._consumers = {}
        self._producer = {}
李寅 已提交
128 129
        self._quantize_activation_info = {}
        self._quantized_tensor = set()
130

B
Bin Li 已提交
131 132 133 134
        self.input_name_map = {}
        self.output_name_map = {}
        self.initialize_name_map()

135
    def run(self):
李寅 已提交
136 137 138
        for key in self._option.transformer_option:
            transformer = self._registered_transformers[key]
            while True:
139
                self.construct_ops_and_consumers(key)
李寅 已提交
140 141
                changed = transformer()
                if not changed:
142
                    break
B
Bin Li 已提交
143
        return self._model, self._quantize_activation_info
144

B
Bin Li 已提交
145 146 147 148 149 150 151 152 153 154 155 156
    def initialize_name_map(self):
        for input_node in self._option.input_nodes.values():
            new_input_name = MaceKeyword.mace_input_node_name \
                             + '_' + input_node.name
            self.input_name_map[input_node.name] = new_input_name

        output_nodes = self._option.check_nodes.values()
        for output_node in output_nodes:
            new_output_name = MaceKeyword.mace_output_node_name \
                              + '_' + output_node.name
            self.output_name_map[output_node.name] = new_output_name

157 158 159 160
    def filter_format(self):
        filter_format_value = ConverterUtil.get_arg(self._model,
                                                    MaceKeyword.mace_filter_format_str).i  # noqa
        filter_format = None
161 162 163 164 165 166
        if filter_format_value == DataFormat.HWIO.value:
            filter_format = DataFormat.HWIO
        elif filter_format_value == DataFormat.OIHW.value:
            filter_format = DataFormat.OIHW
        elif filter_format_value == DataFormat.HWOI.value:
            filter_format = DataFormat.HWOI
167 168 169 170 171 172 173 174 175 176 177
        else:
            mace_check(False, "filter format %d not supported" %
                       filter_format_value)

        return filter_format

    def set_filter_format(self, filter_format):
        arg = ConverterUtil.get_arg(self._model,
                                    MaceKeyword.mace_filter_format_str)
        arg.i = filter_format.value

178
    def construct_ops_and_consumers(self, key):
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
        self._ops.clear()
        self._consumers.clear()
        self._producer.clear()
        for op in self._model.op:
            self._ops[op.name] = op
        for tensor in self._model.tensors:
            self._consts[tensor.name] = tensor
        for op in self._ops.values():
            for input_tensor in op.input:
                if input_tensor not in self._consumers:
                    self._consumers[input_tensor] = []
                self._consumers[input_tensor].append(op)

            for output_tensor in op.output:
                self._producer[output_tensor] = op
194 195 196 197 198 199 200 201 202 203 204
        if key != TransformerRule.SORT_BY_EXECUTION:
            for input_node in self._option.input_nodes.values():
                input_node_existed = False
                for op in self._model.op:
                    if input_node.name in op.output:
                        input_node_existed = True
                        break
                if not input_node_existed:
                    op = mace_pb2.OperatorDef()
                    op.name = self.normalize_op_name(input_node.name)
                    op.type = "Input"
205 206
                    data_type_arg = op.arg.add()
                    data_type_arg.name = MaceKeyword.mace_op_data_type_str
207
                    data_type_arg.i = input_node.data_type
208 209 210
                    op.output.extend([input_node.name])
                    output_shape = op.output_shape.add()
                    output_shape.dims.extend(input_node.shape)
211
                    if input_node.data_format != DataFormat.NONE:
212
                        if input_node.data_format == DataFormat.NCHW:
213 214
                            self.transpose_shape(output_shape.dims,
                                                 [0, 3, 1, 2])
215
                        ConverterUtil.add_data_format_arg(op,
216
                                                          DataFormat.AUTO)
217 218
                    else:
                        ConverterUtil.add_data_format_arg(op,
219
                                                          DataFormat.NONE)
220
                    self._producer[op.output[0]] = op
221 222 223

    @staticmethod
    def replace(obj_list, source, target):
224
        for i in six.moves.range(len(obj_list)):
225 226 227 228 229 230
            if obj_list[i] == source:
                obj_list[i] = target

    @staticmethod
    def transpose_shape(shape, order):
        transposed_shape = []
231
        for i in six.moves.range(len(order)):
232 233 234 235 236 237 238
            transposed_shape.append(shape[order[i]])
        shape[:] = transposed_shape[:]

    @staticmethod
    def normalize_op_name(name):
        return name.replace(':', '_')

李寅 已提交
239
    def get_tensor_shape(self, tensor):
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
        if tensor in self._consts:
            return list(self._consts[tensor].dims)
        elif tensor in self._producer:
            producer = self._producer[tensor]
            for i in six.moves.range(len(producer.output)):
                if producer.output[i] == tensor:
                    return list(producer.output_shape[i].dims)
        else:
            return None

    def get_tensor_data_type(self, tensor):
        if tensor in self._consts:
            return self._consts[tensor].data_type
        elif tensor in self._producer:
            producer = self._producer[tensor]
            for i in six.moves.range(len(producer.output)):
                if producer.output[i] == tensor:
                    if i < len(producer.output_type):
                        return producer.output_type[i]
                    elif ConverterUtil.get_arg(producer, "T") is not None:
                        return ConverterUtil.get_arg(producer, "T").i
                    else:
                        print("No data type filled: ", producer)
                        return None
        else:
            return None
李寅 已提交
266

267 268 269 270 271
    def get_tensor_data_format(self, tensor):
        if tensor in self._producer:
            producer = self._producer[tensor]
            return ConverterUtil.data_format(producer)
        else:
272
            return DataFormat.NONE
273

274 275 276 277 278 279 280 281 282 283 284 285
    def consumer_count(self, tensor_name):
        return len(self._consumers.get(tensor_name, []))

    def is_op_output_node(self, op):
        output_node_tensor_names = [out for out in
                                    self._option.output_nodes]
        for output in op.output:
            if output in output_node_tensor_names:
                return True

        return False

L
liuqi 已提交
286
    def safe_remove_node(self, op, replace_op, remove_input_tensor=False):
李寅 已提交
287 288 289 290 291 292 293 294 295 296
        """remove op.
        1. change the inputs of its consumers to the outputs of replace_op
        2. if the op is output node, change output node to replace op"""

        if replace_op is None:
            # When no replace op specified, we change the inputs of
            # its consumers to the input of the op. This handles the case
            # that the op is identity op and its input is a tensor.
            mace_check(len(op.output) == 1 and len(op.input) == 1,
                       "cannot remove op that w/o replace op specified"
李寅 已提交
297
                       " and input/output length > 1\n" + str(op))
李寅 已提交
298 299 300 301 302 303 304 305 306 307 308

            for consumer_op in self._consumers.get(op.output[0], []):
                self.replace(consumer_op.input, op.output[0], op.input[0])

            mace_check(op.output[0] not in self._option.output_nodes,
                       "cannot remove op that is output node")
        else:
            mace_check(len(op.output) == len(replace_op.output),
                       "cannot remove op since len(op.output) "
                       "!= len(replace_op.output)")

309
            for i in six.moves.range(len(op.output)):
B
Bin Li 已提交
310 311
                # if the op is output node, change replace_op output name
                # to the op output name
李寅 已提交
312 313 314 315 316 317 318
                if op.output[i] in self._option.output_nodes:
                    for consumer in self._consumers.get(
                            replace_op.output[i], []):
                        self.replace(consumer.input,
                                     replace_op.output[i],
                                     op.output[i])
                    replace_op.output[i] = op.output[i]
B
Bin Li 已提交
319 320 321 322 323
                else:
                    for consumer_op in self._consumers.get(op.output[i], []):
                        self.replace(consumer_op.input,
                                     op.output[i],
                                     replace_op.output[i])
李寅 已提交
324

L
liuqi 已提交
325 326 327 328 329 330
        if remove_input_tensor:
            for input_name in op.input:
                if input_name in self._consts:
                    const_tensor = self._consts[input_name]
                    self._model.tensors.remove(const_tensor)

李寅 已提交
331 332
        self._model.op.remove(op)

333 334 335 336 337
    def add_in_out_tensor_info(self):
        net = self._model
        for input_node in self._option.input_nodes.values():
            input_info = net.input_info.add()
            input_info.name = input_node.name
338
            input_info.data_format = input_node.data_format.value
339
            input_info.dims.extend(input_node.shape)
L
liuqi 已提交
340
            input_info.data_type = input_node.data_type
341

B
Bin Li 已提交
342 343
        output_nodes = self._option.check_nodes.values()
        for output_node in output_nodes:
344 345
            output_info = net.output_info.add()
            output_info.name = output_node.name
346
            output_info.data_format = output_node.data_format.value
347 348
            output_info.dims.extend(
                self._producer[output_node.name].output_shape[0].dims)
L
liuqi 已提交
349
            output_info.data_type = output_node.data_type
350 351 352

        return False

353
    def remove_useless_op(self):
354 355 356
        net = self._model
        for op in net.op:
            if op.type == 'Identity':
357
                print("Remove useless op: %s(%s)" % (op.name, op.type))
李寅 已提交
358 359
                self.safe_remove_node(op,
                                      self._producer.get(op.input[0], None))
360
                return True
361
            elif op.type == 'Reshape' and \
叶剑武 已提交
362 363 364 365 366 367
                    op.output_shape[0].dims == \
                    self.get_tensor_shape(op.input[0]):
                print("Remove useless reshape: %s(%s)" % (op.name, op.type))
                self.safe_remove_node(op,
                                      self._producer.get(op.input[0], None))
                return True
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

        return False

    def transform_global_pooling(self):
        net = self._model
        for op in net.op:
            if op.type == MaceOp.Pooling.name and \
                            ConverterUtil.get_arg(op,
                                                  MaceKeyword.mace_global_pooling_str) is not None:  # noqa
                print("Transform global pooling: %s(%s)" % (op.name, op.type))
                input_shape = self._producer[op.input[0]].output_shape[0].dims
                if ConverterUtil.data_format(op) == DataFormat.NHWC:
                    kernel_shape = input_shape[1:3]
                else:
                    kernel_shape = input_shape[2:4]
                ConverterUtil.get_arg(op,
                                      MaceKeyword.mace_kernel_str).ints[:] \
                    = kernel_shape[:]

        return False

    def fold_batchnorm(self):
        net = self._model
        for op in net.op:
            if (op.type == MaceOp.Eltwise.name
                    and ConverterUtil.get_arg(
                        op, MaceKeyword.mace_element_type_str).i
                    == EltwiseType.PROD.value) \
                    and len(op.input) == 2 \
                    and op.input[1] in self._consts \
398 399
                    and op.output_shape[0].dims[-1:] == \
                    self._consts[op.input[1]].dims \
400 401 402 403 404
                    and self.consumer_count(op.output[0]) == 1 \
                    and not self.is_op_output_node(op):
                consumer_op = self._consumers[op.output[0]][0]
                if (consumer_op.type == MaceOp.Eltwise.name
                    and ConverterUtil.get_arg(
405
                        consumer_op, MaceKeyword.mace_element_type_str).i
406 407 408 409 410 411
                        == EltwiseType.SUM.value
                    or consumer_op.type == MaceOp.BiasAdd.name) \
                        and len(consumer_op.input) == 2 \
                        and consumer_op.input[1] in self._consts \
                        and len(self._consts[consumer_op.input[1]].dims) == 1:
                    print("Fold batchnorm: %s(%s)" % (op.name, op.type))
412
                    consumer_op.type = MaceOp.BatchNorm.name
李寅 已提交
413 414
                    consumer_op.input[:] = [op.input[0], op.input[1],
                                            consumer_op.input[1]]
415
                    net.op.remove(op)
416 417 418
                    return True
        return False

419 420 421 422
    def fold_squared_diff_mean(self):
        net = self._model
        for op in net.op:
            if op.type == MaceOp.Eltwise.name and len(op.input) == 2:
L
liutuo 已提交
423
                elt_type = ConverterUtil.get_arg(
424 425
                    op,
                    MaceKeyword.mace_element_type_str).i
L
liutuo 已提交
426
                if elt_type == EltwiseType.SQR_DIFF.value and\
427 428
                        self.consumer_count(op.output[0]) == 1:
                    consumer_op = self._consumers[op.output[0]][0]
L
liutuo 已提交
429 430 431 432 433 434 435 436 437 438
                    if consumer_op.type == MaceOp.Reduce.name:
                        axis = ConverterUtil.get_arg(
                            consumer_op,
                            MaceKeyword.mace_axis_str).ints
                        keep_dims = ConverterUtil.get_arg(
                            consumer_op,
                            MaceKeyword.mace_keepdims_str).i
                        reduce_type = ConverterUtil.get_arg(
                            consumer_op,
                            MaceKeyword.mace_reduce_type_str).i
L
liutuo 已提交
439
                        if reduce_type == ReduceType.MEAN.value and\
L
liutuo 已提交
440 441 442 443 444 445 446 447 448
                                len(consumer_op.input) == 1 and\
                                axis[0] == 1 and axis[1] == 2 and\
                                keep_dims > 0:
                            print("Fold SquaredDiff Reduce: %s" % op.name)
                            op.type = MaceOp.SqrDiffMean.name
                            op.output[0] = consumer_op.output[0]
                            self.replace_quantize_info(op, consumer_op)
                            self.safe_remove_node(consumer_op, op)
                            return True
449 450 451

        return False

李寅 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
    def fold_embedding_lookup(self):
        net = self._model
        for op in net.op:
            # gather -> mul
            if (op.type == MaceOp.Gather.name and
                    self.consumer_count(op.output[0]) == 1):
                consumer_op = self._consumers[op.output[0]][0]
                if (consumer_op.type == MaceOp.Eltwise.name and
                    ConverterUtil.get_arg(consumer_op,
                                          MaceKeyword.mace_element_type_str).i == EltwiseType.PROD.value and  # noqa
                            len(consumer_op.input) == 1 and
                            op.input[0] in self._consts and
                            self.consumer_count(op.input[0]) == 1):
                    print("Fold Gather and Mul: %s" % op.name)
                    gather_weights = self._consts[op.input[0]]
                    mul_weight = ConverterUtil.get_arg(consumer_op,
                                                       MaceKeyword.mace_scalar_input_str).f  # noqa
L
liukai6 已提交
469
                    gather_weights.float_data[:] = [float_data * mul_weight for float_data in gather_weights.float_data]  # noqa
李寅 已提交
470 471 472
                    self.safe_remove_node(consumer_op, None,
                                          remove_input_tensor=True)

Y
yejianwu 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
    def transform_lstmcell_zerostate(self):
        net = self._model

        zero_state_pattern = \
                re.compile(r'^.*BasicLSTMCellZeroState_?[0-9]*/[a-zA-Z]+_?[0-9]*')  # noqa
        for op in net.op:
            if op.type == MaceOp.Fill.name and \
                    zero_state_pattern.match(op.name):
                print("Transform lstm zerostate")
                concat_op = self._producer[op.input[0]]
                consumer_op = self._consumers[op.output[0]][0]

                dims = [self._consts[concat_op.input[0]].int32_data[0],
                        self._consts[concat_op.input[1]].int32_data[0]]
                tensor_def = net.tensors.add()
                tensor_def.name = op.output[0].replace('/zeros', '/init_const')
                tensor_def.dims.extend(dims)
                tensor_def.data_type = self._consts[op.input[1]].data_type
                tensor_def.float_data.extend(
                        [self._consts[op.input[1]].float_data[0]] *
                        (dims[0] * dims[1]))

                for i in range(len(consumer_op.input)):
                    if zero_state_pattern.match(consumer_op.input[i][:-2]):
                        consumer_op.input[i] = tensor_def.name

                net.tensors.remove(self._consts[op.input[1]])
                net.tensors.remove(self._consts[concat_op.input[0]])
                net.tensors.remove(self._consts[concat_op.input[1]])

                net.op.remove(concat_op)
                net.op.remove(op)

                return True

    def transform_basic_lstmcell(self):
        if self._option.device != DeviceType.GPU.value:
            return False

        net = self._model
        basic_lstm_concat_pattern = \
            re.compile(r'^.*basic_lstm_cell_?[0-9]*/concat_?[0-9]*')
        for op in net.op:
            if op.type == MaceOp.Concat.name and \
                    basic_lstm_concat_pattern.match(op.name):
                print("Transform basic lstmcell")
                ops_to_delete = []
                ops_to_delete.extend([op])

                op_def = net.op.add()
                op_def.name = op.name.replace('/concat', '/folded_lstmcell')
                op_def.type = MaceOp.LSTMCell.name
                op_def.arg.extend(op.arg[:-1])

                # Concat pre output and cur input
                # extend concat inputs
                op_def.input.extend([op_input for op_input in op.input])

                # lstm MatMul in FC of [pre_output, cur_input]
                matmul_op = self._consumers[op.output[0]][0]
                ops_to_delete.extend([matmul_op])
                # extend MatMul weight input
                op_def.input.extend([matmul_op.input[1]])

                # lstm BiasAdd in FC of [pre_output, cur_input]
                biasadd_op = self._consumers[matmul_op.output[0]][0]
                ops_to_delete.extend([biasadd_op])
                # extend BiasAdd bias input
                op_def.input.extend([biasadd_op.input[1]])

                # Split FC output into i, j, f, o
                # i = input_gate, j = new_input, f = forget_gate, o = output_gate  # noqa
                split_op = self._consumers[biasadd_op.output[0]][0]
                ops_to_delete.extend([split_op])

                # input gate activation
                input_gate_op = self._consumers[split_op.output[0]][0]
                ops_to_delete.extend([input_gate_op])
                # new input gate
                new_input_tanh_op = self._consumers[split_op.output[1]][0]
                ops_to_delete.extend([new_input_tanh_op])
                # forget gate add
                forget_add_op = self._consumers[split_op.output[2]][0]
                ops_to_delete.extend([forget_add_op])
                # output gate activation
                output_gate_op = self._consumers[split_op.output[3]][0]
                ops_to_delete.extend([output_gate_op])

                # extend forget add
                mace_check(len(forget_add_op.input) == 1,
                           'Wrong LSTM format in forget gate inputs')
                for arg in forget_add_op.arg:
                    if arg.name == MaceKeyword.mace_scalar_input_str:
                        op_def.arg.extend([arg])

                # state remember
                remember_mul_op = self._consumers[input_gate_op.output[0]][0]
                ops_to_delete.extend([remember_mul_op])
                mace_check(remember_mul_op.name == self._consumers[
                               new_input_tanh_op.output[0]][0].name,
                           'Wrong LSTM format in input sig & input tanh mul')

                # forget gate activation
                forget_gate_op = self._consumers[forget_add_op.output[0]][0]
                ops_to_delete.extend([forget_gate_op])

                # Mul `forget` & `pre cell state`
                forget_mul_op = self._consumers[forget_gate_op.output[0]][0]
                ops_to_delete.extend([forget_mul_op])

                # extend pre cell state input
                op_def.input.extend([forget_mul_op.input[0]])

                # get cur cell state
                # Add `forget gate output` & `remember mul output`
                remember_forget_add_op = \
                    self._consumers[remember_mul_op.output[0]][0]
                ops_to_delete.extend([remember_forget_add_op])
                mace_check(remember_forget_add_op.name ==
                           self._consumers[forget_mul_op.output[0]][0].name,
                           'Wrong LSTM format in add forget gate & remember mul')  # noqa
                op_def.output.extend([remember_forget_add_op.output[0]])
                op_def.output_shape.extend(remember_forget_add_op.output_shape)

                # cell state output tanh
                for consumer in \
                        self._consumers[remember_forget_add_op.output[0]]:
                    if consumer.type == MaceOp.Activation.name and \
                            consumer.name.find('basic_lstm_cell') > 0:
                        cell_tanh_op = consumer
                ops_to_delete.extend([cell_tanh_op])

                # final mul, get output
                final_mul_op = self._consumers[cell_tanh_op.output[0]][0]
                ops_to_delete.extend([final_mul_op])
                mace_check(final_mul_op.name ==
                           self._consumers[output_gate_op.output[0]][0].name,
                           'Wrong LSTM format in final mul')
                op_def.output.extend([final_mul_op.output[0]])
                op_def.output_shape.extend(final_mul_op.output_shape)

                for op_to_del in ops_to_delete:
                    net.op.remove(op_to_del)

                return True

        return False

621 622 623
    def fold_conv_and_bn(self):
        net = self._model
        for op in net.op:
L
liutuo 已提交
624
            if (op.type == MaceOp.Conv2D.name) \
625 626
                    and self.consumer_count(op.output[0]) == 1:
                consumer_op = self._consumers[op.output[0]][0]
627
                input_len = len(op.input)
628 629 630
                if (consumer_op.type == MaceOp.BatchNorm.name
                        and (input_len == 2 or (input_len == 3 and op.input[-1] in self._consts))  # noqa
                        and len(self._consumers[op.input[1]]) == 1):
631 632 633
                    print("Fold conv and bn: %s(%s)" % (op.name, op.type))
                    filter = self._consts[op.input[1]]
                    scale = self._consts[consumer_op.input[1]]
634
                    offset = self._consts[consumer_op.input[2]]
635 636
                    idx = 0
                    filter_format = self.filter_format()
637
                    if filter_format == DataFormat.HWIO:
638 639 640 641
                        for hwi in six.moves.range(filter.dims[0]
                                                   * filter.dims[1]
                                                   * filter.dims[2]):
                            for o in six.moves.range(filter.dims[3]):
642 643
                                filter.float_data[idx] *= scale.float_data[o]
                                idx += 1
644
                    elif filter_format == DataFormat.OIHW:
645 646 647 648
                        for o in six.moves.range(filter.dims[0]):
                            for hwi in six.moves.range(filter.dims[1]
                                                       * filter.dims[2]
                                                       * filter.dims[3]):
649 650 651 652 653 654
                                filter.float_data[idx] *= scale.float_data[o]
                                idx += 1
                    else:
                        mace_check(False, "filter format %s not supported" %
                                   filter_format)

655 656 657 658 659 660 661 662
                    if len(op.input) == 3:
                        conv_bias = self._consts[op.input[2]]
                        for c in six.moves.range(conv_bias.dims[0]):
                            conv_bias.float_data[c] *= scale.float_data[c]
                            conv_bias.float_data[c] += offset.float_data[c]
                        net.tensors.remove(offset)
                    else:
                        op.input.extend([consumer_op.input[2]])
663

664 665
                    # remove bn
                    del consumer_op.input[:]
666
                    net.tensors.remove(scale)
667
                    self.replace_quantize_info(op, consumer_op)
668 669
                    self.safe_remove_node(consumer_op, op)

670 671 672 673
                    return True

        return False

L
liutuo 已提交
674 675 676
    def fold_deconv_and_bn(self):
        net = self._model
        for op in net.op:
L
liutuo 已提交
677
            if (op.type in [MaceOp.Deconv2D.name, MaceOp.DepthwiseDeconv2d]) \
L
liutuo 已提交
678 679
                    and self.consumer_count(op.output[0]) == 1:
                consumer_op = self._consumers[op.output[0]][0]
680 681 682
                framework = ConverterUtil.get_arg(
                        op, MaceKeyword.mace_framework_type_str).i
                input_len = len(op.input)
683
                if (consumer_op.type == MaceOp.BatchNorm.name and (
684
                        (framework == FrameworkType.CAFFE.value and
L
liutuo 已提交
685 686
                         (input_len == 2 or (input_len == 3 and
                                             op.input[-1] in self._consts))) or
687 688
                        (framework == FrameworkType.TENSORFLOW.value and
                         (input_len == 3 or (input_len == 4 and
689 690
                                             op.input[-1] in self._consts))))
                        and len(self._consumers[op.input[1]]) == 1):
L
liutuo 已提交
691 692 693
                    print("Fold deconv and bn: %s(%s)" % (op.name, op.type))
                    filter = self._consts[op.input[1]]
                    scale = self._consts[consumer_op.input[1]]
694
                    offset = self._consts[consumer_op.input[2]]
L
liutuo 已提交
695 696 697
                    idx = 0
                    filter_format = self.filter_format()
                    # in deconv op O and I channel is switched
698
                    if filter_format == DataFormat.HWIO:
L
liutuo 已提交
699 700 701 702 703 704 705
                        for hw in six.moves.range(filter.dims[0]
                                                  * filter.dims[1]):
                            for o in six.moves.range(filter.dims[2]):
                                for i in six.moves.range(filter.dims[3]):
                                    filter.float_data[idx] *=\
                                        scale.float_data[o]
                                    idx += 1
706
                    elif filter_format == DataFormat.OIHW:
L
liutuo 已提交
707 708 709 710 711 712 713 714 715 716 717
                        for i in six.moves.range(filter.dims[0]):
                            for o in six.moves.range(filter.dims[1]):
                                for hw in six.moves.range(filter.dims[2]
                                                          * filter.dims[3]):
                                    filter.float_data[idx] *=\
                                        scale.float_data[o]
                                    idx += 1
                    else:
                        mace_check(False, "filter format %s not supported" %
                                   filter_format)

718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
                    bias_dim = -1
                    if framework == FrameworkType.CAFFE.value \
                            and len(op.input) == 3:
                        bias_dim = 2
                    if framework == FrameworkType.TENSORFLOW.value \
                            and len(op.input) == 4:
                        bias_dim = 3

                    if bias_dim != -1:
                        conv_bias = self._consts[op.input[bias_dim]]
                        for c in six.moves.range(conv_bias.dims[0]):
                            conv_bias.float_data[c] *= scale.float_data[c]
                            conv_bias.float_data[c] += offset.float_data[c]
                        net.tensors.remove(offset)
                    else:
                        op.input.extend([consumer_op.input[2]])
L
liutuo 已提交
734

735
                    del consumer_op.input[:]
L
liutuo 已提交
736
                    net.tensors.remove(scale)
737
                    self.replace_quantize_info(op, consumer_op)
738 739
                    self.safe_remove_node(consumer_op, op)

L
liutuo 已提交
740 741 742 743
                    return True

        return False

744 745 746 747 748 749
    def fold_depthwise_conv_and_bn(self):
        net = self._model
        for op in net.op:
            if op.type == MaceOp.DepthwiseConv2d.name \
                    and self.consumer_count(op.output[0]) == 1:
                consumer_op = self._consumers[op.output[0]][0]
750
                input_len = len(op.input)
751 752 753
                if (consumer_op.type == MaceOp.BatchNorm.name
                        and (input_len == 2 or (input_len == 3 and op.input[-1] in self._consts))  # noqa
                        and len(self._consumers[op.input[1]]) == 1):
754 755 756 757
                    print("Fold depthwise conv and bn: %s(%s)"
                          % (op.name, op.type))
                    filter = self._consts[op.input[1]]
                    scale = self._consts[consumer_op.input[1]]
758
                    offset = self._consts[consumer_op.input[2]]
759 760 761
                    idx = 0

                    filter_format = self.filter_format()
762
                    if filter_format == DataFormat.HWIO:
763 764 765 766
                        for hw in six.moves.range(filter.dims[0]
                                                  * filter.dims[1]):
                            for i in six.moves.range(filter.dims[2]):
                                for o in six.moves.range(filter.dims[3]):
767
                                    filter.float_data[idx] *= scale.float_data[
768
                                                        i * filter.dims[3] + o]
769
                                    idx += 1
770
                    elif filter_format == DataFormat.OIHW:
771 772 773 774
                        for o in six.moves.range(filter.dims[0]):
                            for i in six.moves.range(filter.dims[1]):
                                for hw in six.moves.range(filter.dims[2]
                                                          * filter.dims[3]):
775 776 777 778 779 780 781
                                    filter.float_data[idx] *= scale.float_data[
                                        i * filter.dims[0] + o]
                                    idx += 1
                    else:
                        mace_check(False, "filter format %s not supported" %
                                   filter_format)

782 783 784 785 786 787 788 789
                    if len(op.input) == 3:
                        conv_bias = self._consts[op.input[2]]
                        for c in six.moves.range(conv_bias.dims[0]):
                            conv_bias.float_data[c] *= scale.float_data[c]
                            conv_bias.float_data[c] += offset.float_data[c]
                        net.tensors.remove(offset)
                    else:
                        op.input.extend([consumer_op.input[2]])
790

791 792
                    # remove bn
                    del consumer_op.input[:]
793
                    net.tensors.remove(scale)
794
                    self.replace_quantize_info(op, consumer_op)
795 796
                    self.safe_remove_node(consumer_op, op)

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
                    return True

        return False

    @staticmethod
    def sort_feature_map_shape(shape, data_format):
        """Return shape in NHWC order"""
        batch = shape[0]
        if data_format == DataFormat.NHWC:
            height = shape[1]
            width = shape[2]
            channels = shape[3]
        else:
            height = shape[2]
            width = shape[3]
            channels = shape[1]
        return batch, height, width, channels

    @staticmethod
    def sort_filter_shape(filter_shape, filter_format):
        """Return filter shape in HWIO order"""
818
        if filter_format == DataFormat.HWIO:
819 820 821 822
            filter_height = filter_shape[0]
            filter_width = filter_shape[1]
            in_channels = filter_shape[2]
            out_channels = filter_shape[3]
823
        elif filter_format == DataFormat.OIHW:
824 825 826 827
            filter_height = filter_shape[2]
            filter_width = filter_shape[3]
            in_channels = filter_shape[1]
            out_channels = filter_shape[0]
828
        elif filter_format == DataFormat.HWOI:
829 830 831 832 833 834 835 836 837 838 839
            filter_height = filter_shape[0]
            filter_width = filter_shape[1]
            in_channels = filter_shape[3]
            out_channels = filter_shape[2]
        else:
            mace_check(False, "filter format %s not supported" % filter_format)
        return filter_height, filter_width, in_channels, out_channels

    def transform_add_to_biasadd(self):
        net = self._model
        for op in net.op:
李寅 已提交
840 841 842 843 844
            if (op.type == 'Eltwise'
                    and ConverterUtil.get_arg(op, MaceKeyword.mace_element_type_str).i == EltwiseType.SUM.value  # noqa
                    and len(op.input) == 2
                    and op.input[1] in self._consts
                    and len(self._consts[op.input[1]].dims) == 1):
845 846 847 848 849 850
                print("Transform add to biasadd: %s(%s)" % (op.name, op.type))
                op.type = MaceOp.BiasAdd.name
                return True

        return False

李寅 已提交
851 852 853 854
    def replace_quantize_info(self, op, replace_op):
        if len(replace_op.quantize_info) > 0:
            del op.quantize_info[:]
            op.quantize_info.extend(replace_op.quantize_info)
B
Bin Li 已提交
855 856 857
            for i in range(len(op.quantize_info)):
                self._quantize_activation_info[op.output[i]] = \
                    op.quantize_info[i]
李寅 已提交
858

859 860 861
    def fold_biasadd(self):
        net = self._model
        for op in net.op:
862 863 864 865
            if (((op.type == MaceOp.Conv2D.name
                  or op.type == MaceOp.DepthwiseConv2d.name
                  or op.type == MaceOp.FullyConnected.name)
                 and len(op.input) == 2)
L
liutuo 已提交
866 867 868 869 870 871 872 873 874 875 876
                or (op.type == MaceOp.Deconv2D.name
                    and ((ConverterUtil.get_arg(
                                op,
                                MaceKeyword.mace_framework_type_str).i ==
                          FrameworkType.CAFFE.value
                          and len(op.input) == 2)
                         or (ConverterUtil.get_arg(
                                        op,
                                        MaceKeyword.mace_framework_type_str).i
                             == FrameworkType.TENSORFLOW.value
                             and len(op.input) == 3)))) \
877 878 879 880
                    and len(self._consumers.get(op.output[0], [])) == 1:
                consumer_op = self._consumers[op.output[0]][0]
                if consumer_op.type == MaceOp.BiasAdd.name:
                    print("Fold biasadd: %s(%s)" % (op.name, op.type))
B
Bin Li 已提交
881 882
                    op.name = consumer_op.name
                    op.output[0] = consumer_op.output[0]
883
                    op.input.append(consumer_op.input[1])
李寅 已提交
884
                    self.replace_quantize_info(op, consumer_op)
李寅 已提交
885
                    self.safe_remove_node(consumer_op, op)
886 887 888 889
                    return True

        return False

890
    def flatten_atrous_conv(self):
891
        if self._option.device != DeviceType.GPU.value \
892 893
               and self._option.device != DeviceType.APU.value \
               and self._option.device != DeviceType.HTA.value:
894 895 896 897 898 899 900 901 902 903 904 905
            return

        net = self._model
        for op in net.op:
            if (op.type == MaceOp.SpaceToBatchND.name
                    and len(self._consumers.get(op.output[0], [])) == 1):
                conv_op = self._consumers.get(op.output[0])[0]
                if (conv_op.type == MaceOp.Conv2D.name
                        or conv_op.type == MaceOp.DepthwiseConv2d.name) \
                        and len(self._consumers.get(conv_op.output[0], [])) == 1:  # noqa
                    b2s_op = self._consumers.get(conv_op.output[0])[0]
                    if b2s_op.type == MaceOp.BatchToSpaceND.name:
906
                        six.print_("Flatten atrous convolution")
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
                        # Add args.
                        padding_arg_values = ConverterUtil.get_arg(
                            op,
                            MaceKeyword.mace_paddings_str).ints
                        blocks_arg_values = ConverterUtil.get_arg(
                            b2s_op,
                            MaceKeyword.mace_space_batch_block_shape_str).ints
                        dilation_arg = ConverterUtil.get_arg(
                            conv_op,
                            MaceKeyword.mace_dilations_str)
                        if dilation_arg is None:
                            dilation_arg = conv_op.arg.add()
                        dilation_arg.name = MaceKeyword.mace_dilations_str
                        dilation_arg.ints[:] = blocks_arg_values

                        padding_arg = ConverterUtil.get_arg(
                            conv_op,
                            MaceKeyword.mace_padding_str)
                        if padding_arg is None:
                            padding_arg = conv_op.arg.add()
                        padding_arg.name = MaceKeyword.mace_padding_str
                        if len(padding_arg_values) > 0 \
                                and padding_arg_values[0] > 0:
                            padding_arg.i = PaddingMode.SAME.value
                        else:
                            padding_arg.i = PaddingMode.VALID.value

                        strides_arg = ConverterUtil.get_arg(
                            conv_op,
                            MaceKeyword.mace_strides_str)
                        if strides_arg is None:
                            strides_arg = conv_op.arg.add()
                        strides_arg.name = MaceKeyword.mace_strides_str
                        strides_arg.ints[:] = [1, 1]

                        # update output shape
                        conv_op.output_shape[0].dims[:] = \
                            b2s_op.output_shape[0].dims[:]
B
Bin Li 已提交
945 946
                        conv_op.output[0] = b2s_op.output[0]
                        conv_op.name = b2s_op.name
947 948

                        self.safe_remove_node(op, None)
B
Bin Li 已提交
949
                        self.replace_quantize_info(b2s_op, conv_op)
950 951 952 953
                        self.safe_remove_node(b2s_op, conv_op)
                        return True
        return False

954 955 956 957 958 959 960
    def fold_activation(self):
        net = self._model
        for op in net.op:
            if (op.type == MaceOp.Conv2D.name
                or op.type == MaceOp.Deconv2D.name
                or op.type == MaceOp.DepthwiseConv2d.name
                or op.type == MaceOp.FullyConnected.name
961
                or op.type == MaceOp.BatchNorm.name) \
962 963
                    and len(self._consumers.get(op.output[0], [])) == 1:
                consumer_op = self._consumers[op.output[0]][0]
B
Bin Li 已提交
964 965 966 967 968 969 970 971 972 973 974
                if consumer_op.type == MaceOp.Activation.name:
                    act_type_arg = ConverterUtil.get_arg(
                        consumer_op, MaceKeyword.mace_activation_type_str)
                    act_type = act_type_arg.s.decode()
                    if act_type == ActivationType.PRELU.name:
                        continue
                    # during quantization, only fold relu/relux
                    if (self._option.quantize_stat or self._option.quantize) \
                            and act_type not in [ActivationType.RELU.name,
                                                 ActivationType.RELUX.name]:
                        continue
975 976 977 978 979
                    print("Fold activation: %s(%s)" % (op.name, op.type))
                    op.name = consumer_op.name
                    op.output[0] = consumer_op.output[0]
                    for arg in consumer_op.arg:
                        if arg.name == MaceKeyword.mace_activation_type_str \
Y
yejianwu 已提交
980 981 982
                                or arg.name == \
                                    MaceKeyword.mace_activation_max_limit_str \
                                or arg.name == MaceKeyword.mace_activation_leakyrelu_coefficient_str:  # noqa
983 984
                            op.arg.extend([arg])

李寅 已提交
985
                    self.replace_quantize_info(op, consumer_op)
李寅 已提交
986
                    self.safe_remove_node(consumer_op, op)
987 988 989 990
                    return True

        return False

991 992 993 994 995 996 997 998 999
    def transform_global_conv_to_fc(self):
        """Transform global conv to fc should be placed after transposing
        input/output and filter"""

        if self._option.quantize:
            return

        net = self._model
        for op in net.op:
1000 1001 1002
            if op.type == MaceOp.Conv2D.name \
                    and len(op.input) >= 2 \
                    and op.input[1] in self._consts:
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
                producer = self._producer[op.input[0]]
                input_shape = producer.output_shape[0].dims
                batch, height, width, channels = self.sort_feature_map_shape(
                    input_shape, ConverterUtil.data_format(producer))
                filter = self._consts[op.input[1]]
                filter_shape = filter.dims
                filter_height, filter_width, in_channels, out_channels = \
                    self.sort_filter_shape(filter_shape, self.filter_format())
                zero_padding = True
                padding_arg = ConverterUtil.get_arg(op,
                                                    MaceKeyword.mace_padding_str)  # noqa
                if padding_arg is not None:
                    if padding_arg.i != PaddingMode.VALID.value:
                        zero_padding = False
                else:
                    padding_value_arg = ConverterUtil.get_arg(op,
                                                              MaceKeyword.mace_padding_values_str)  # noqa
                    if padding_value_arg is not None:
                        if not all(v == 0 for v in padding_value_arg.ints):
                            zero_padding = False

                if height == filter_height and width == filter_width \
1025 1026
                        and zero_padding \
                        and len(self._consumers[op.input[1]]) == 1:
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
                    print("transform global conv to fc %s(%s)"
                          % (op.name, op.type))
                    op.type = MaceOp.FullyConnected.name

        return False

    def reshape_fc_weight(self):
        net = self._model
        filter_format = self.filter_format()
        for op in net.op:
            if op.type == MaceOp.FullyConnected.name:
                weight = self._consts[op.input[1]]
                if len(weight.dims) == 2:
L
liutuo 已提交
1040
                    print("Reshape fully connected weight shape")
1041 1042 1043 1044
                    input_op = self._producer[op.input[0]]
                    input_shape = list(input_op.output_shape[0].dims)
                    weight.dims[:] = [weight.dims[0]] + input_shape[1:]
                    if len(input_shape) == 2:
1045
                        if filter_format == DataFormat.HWIO:
1046
                            weight.dims[:] = [1, 1] + weight.dims[:]
1047
                        elif filter_format == DataFormat.OIHW:
1048 1049
                            weight.dims[:] = weight.dims[:] + [1, 1]
                        else:
L
liuqi 已提交
1050 1051
                            mace_check(False,
                                       "FC does not support filter format %s" %
1052 1053 1054
                                       filter_format.name)
        return False

1055 1056 1057 1058
    def add_winograd_arg(self):
        if self._wino_arg == 0:
            return False
        net = self._model
李寅 已提交
1059

1060 1061 1062 1063 1064
        for op in net.op:
            if op.type == MaceOp.Conv2D.name:
                winograd_arg = op.arg.add()
                winograd_arg.name = MaceKeyword.mace_wino_arg_str
                winograd_arg.i = self._wino_arg
李寅 已提交
1065

1066 1067
        return False

1068 1069 1070 1071
    def transpose_matmul_weight(self):
        if self._option.device != DeviceType.CPU.value:
            return False
        net = self._model
1072
        transposed_weights = []
1073 1074
        for op in net.op:
            if op.type == MaceOp.MatMul.name:  # noqa
1075 1076 1077
                rhs = op.input[1]
                if rhs in self._consts and len(self._consts[rhs].dims) == 2:
                    arg = ConverterUtil.get_arg(op, MaceKeyword.mace_transpose_b_str)  # noqa
L
liutuo 已提交
1078
                    # six.print_("Transpose matmul weight %s" % rhs)
1079 1080 1081 1082 1083 1084
                    if arg is None:
                        arg = op.arg.add()
                        arg.name = MaceKeyword.mace_transpose_b_str
                        arg.i = 0
                    if arg.i == 0:
                        arg.i = 1
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
                        if rhs not in transposed_weights:
                            filter = self._consts[rhs]
                            filter_data = np.array(filter.float_data).reshape(
                                filter.dims)
                            filter_data = filter_data.transpose(1, 0)
                            filter.float_data[:] = filter_data.flat
                            filter.dims[:] = filter_data.shape
                            transposed_weights.append(rhs)
                            six.print_('Transpose matmul weight to shape:',
                                       filter.dims)
1095

1096 1097 1098
    def transpose_filters(self):
        net = self._model
        filter_format = self.filter_format()
1099
        transposed_filter = set()
L
liutuo 已提交
1100
        transposed_deconv_filter = set()
1101

李寅 已提交
1102
        if self._option.quantize and \
1103 1104
                (self._option.device == DeviceType.CPU.value or
                 self._option.device == DeviceType.APU.value):
1105
            print("Transpose filters to OHWI")
1106
            if filter_format == DataFormat.HWIO:
1107
                transpose_order = [3, 0, 1, 2]
1108
            elif filter_format == DataFormat.OIHW:
1109 1110
                transpose_order = [0, 2, 3, 1]
            else:
L
liuqi 已提交
1111
                mace_check(False, "Quantize model does not support conv "
1112 1113
                           "filter format: %s" % filter_format.name)

1114
            for op in net.op:
L
liutuo 已提交
1115
                if (op.type == MaceOp.Conv2D.name or
1116 1117 1118
                    op.type == MaceOp.Deconv2D.name or
                    (op.type == MaceOp.DepthwiseConv2d.name and
                     self._option.device == DeviceType.APU.value)) and\
L
liutuo 已提交
1119
                        op.input[1] not in transposed_filter:
1120 1121 1122
                    filter = self._consts[op.input[1]]
                    filter_data = np.array(filter.float_data).reshape(
                        filter.dims)
1123
                    filter_data = filter_data.transpose(transpose_order)
1124 1125
                    filter.float_data[:] = filter_data.flat
                    filter.dims[:] = filter_data.shape
1126
                    transposed_filter.add(op.input[1])
L
liutuo 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
            # deconv's filter's output channel and input channel is reversed
            for op in net.op:
                if op.type == MaceOp.Deconv2D.name and \
                        op.input[1] not in transposed_deconv_filter:
                    filter = self._consts[op.input[1]]
                    filter_data = np.array(filter.float_data).reshape(
                        filter.dims)
                    filter_data = filter_data.transpose(3, 1, 2, 0)
                    filter.float_data[:] = filter_data.flat
                    filter.dims[:] = filter_data.shape
                    transposed_deconv_filter.add(op.input[1])
1138

1139
            self.set_filter_format(DataFormat.OHWI)
B
Bin Li 已提交
1140
        elif self._option.quantize and \
B
Bin Li 已提交
1141 1142
                (self._option.device == DeviceType.HEXAGON.value or
                 self._option.device == DeviceType.HTA.value):
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
            for op in net.op:
                # from HWOI to OHWI, deconv is unique
                if op.type == MaceOp.Deconv2D.name \
                        and op.input[1] in self._consts \
                        and op.input[1] not in transposed_deconv_filter:
                    filter = self._consts[op.input[1]]
                    filter_data = np.array(filter.float_data).reshape(
                        filter.dims)
                    filter_data = filter_data.transpose(2, 0, 1, 3)
                    filter.float_data[:] = filter_data.flat
                    filter.dims[:] = filter_data.shape
                    transposed_deconv_filter.add(op.input[1])

B
Bin Li 已提交
1156
            print("Transpose filters to HWIO/HWIM")
1157
            mace_check(filter_format == DataFormat.HWIO,
B
Bin Li 已提交
1158
                       "HEXAGON only support HWIO/HWIM filter format.")
1159 1160
        else:
            # transpose filter to OIHW/MIHW for tensorflow (HWIO/HWIM)
1161
            if filter_format == DataFormat.HWIO:
1162
                for op in net.op:
1163 1164 1165
                    if (op.type == MaceOp.Conv2D.name
                            or op.type == MaceOp.Deconv2D.name
                            or op.type == MaceOp.DepthwiseConv2d.name) \
1166
                            and op.input[1] in self._consts \
1167
                            and op.input[1] not in transposed_filter:
L
liutuo 已提交
1168
                        print("Transpose Conv2D/Deconv2D filters to OIHW/MIHW")
1169 1170 1171 1172 1173 1174
                        filter = self._consts[op.input[1]]
                        filter_data = np.array(filter.float_data).reshape(
                            filter.dims)
                        filter_data = filter_data.transpose(3, 2, 0, 1)
                        filter.float_data[:] = filter_data.flat
                        filter.dims[:] = filter_data.shape
1175
                        transposed_filter.add(op.input[1])
L
liutuo 已提交
1176 1177 1178 1179 1180
                    if (op.type == MaceOp.MatMul.name and
                            (ConverterUtil.get_arg(
                                op,
                                MaceKeyword.mace_winograd_filter_transformed)
                                 is not None)  # noqa
1181
                            and op.input[1] not in transposed_filter):
L
liutuo 已提交
1182
                        print("Transpose Winograd filters to OIHW/MIHW")
1183 1184 1185 1186 1187 1188
                        filter = self._consts[op.input[0]]
                        filter_data = np.array(filter.float_data).reshape(
                            filter.dims)
                        filter_data = filter_data.transpose(3, 2, 0, 1)
                        filter.float_data[:] = filter_data.flat
                        filter.dims[:] = filter_data.shape
1189 1190 1191
                        transposed_filter.add(op.input[0])
                    if op.type == MaceOp.FullyConnected.name \
                            and op.input[1] not in transposed_filter:
1192 1193
                        weight = self._consts[op.input[1]]
                        if len(weight.dims) == 4:
L
liutuo 已提交
1194 1195
                            print("Transpose FullyConnected filters to"
                                  " OIHW/MIHW")
1196 1197 1198 1199 1200
                            weight_data = np.array(weight.float_data).reshape(
                                weight.dims)
                            weight_data = weight_data.transpose(3, 2, 0, 1)
                            weight.float_data[:] = weight_data.flat
                            weight.dims[:] = weight_data.shape
1201
                            transposed_filter.add(op.input[1])
1202

1203
                self.set_filter_format(DataFormat.OIHW)
L
liutuo 已提交
1204
            # deconv's filter's output channel and input channel is reversed
1205
            for op in net.op:
L
liutuo 已提交
1206 1207
                if op.type in [MaceOp.Deconv2D.name,
                               MaceOp.DepthwiseDeconv2d] \
L
liutuo 已提交
1208
                        and op.input[1] not in transposed_deconv_filter:
1209
                    filter = self._consts[op.input[1]]
1210 1211
                    filter_data = np.array(filter.float_data).reshape(
                        filter.dims)
1212
                    filter_data = filter_data.transpose(1, 0, 2, 3)
1213 1214
                    filter.float_data[:] = filter_data.flat
                    filter.dims[:] = filter_data.shape
L
liutuo 已提交
1215
                    transposed_deconv_filter.add(op.input[1])
1216 1217 1218

        return False

李寅 已提交
1219
    def fold_reshape(self):
1220 1221
        net = self._model
        for op in net.op:
1222 1223
            if op.type == MaceOp.Softmax.name:
                # see if possible to fold
1224
                # Reshape(xd->2d) + Softmax(2d) [+ Reshape(xd)] to Softmax(xd)
1225 1226 1227 1228
                should_fold = False
                if op.input[0] in self._producer \
                        and self._producer[op.input[0]].type \
                        == MaceOp.Reshape.name \
1229
                        and len(op.output_shape[0].dims) == 2:
L
liutuo 已提交
1230 1231 1232 1233 1234
                    producer = self._producer[op.input[0]]
                    reshape_input_rank = len(self.get_tensor_shape(
                        producer.input[0]))
                    if reshape_input_rank == 4:
                        should_fold = True
1235 1236 1237 1238 1239

                if should_fold:
                    print(
                        "Fold reshape and softmax: %s(%s)"
                        % (op.name, op.type))
1240
                    producer = self._producer[op.input[0]]
1241 1242 1243
                    op.output_shape[0].dims[:] = self.get_tensor_shape(
                        producer.input[0])

1244 1245 1246
                    if op.output[0] in self._consumers:
                        consumer = self._consumers[op.output[0]][0]
                        # if there is a shape op, remove it too
L
liutuo 已提交
1247 1248 1249 1250 1251 1252 1253
                        if len(consumer.input) > 1:
                            if (consumer.input[1] in self._producer
                                and self._producer[consumer.input[1]].type
                                    == 'Shape'):
                                self.safe_remove_node(
                                    self._producer[consumer.input[1]], None,
                                    remove_input_tensor=True)
1254 1255 1256
                        # remove consumer reshape
                        self.safe_remove_node(consumer, op,
                                              remove_input_tensor=True)
1257 1258 1259
                    # remove producer reshape
                    self.safe_remove_node(producer,
                                          self._producer.get(producer.input[0],
L
liuqi 已提交
1260 1261
                                                             None),
                                          remove_input_tensor=True)
1262

1263
                    return True
1264 1265
        return False

Y
yejianwu 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
    def is_after_fc(self, op):
        while op.input[0] in self._producer:
            producer = self._producer[op.input[0]]
            if producer.type in [MaceOp.Activation.name, MaceOp.BiasAdd.name]:
                op = producer
                continue
            elif producer.type == MaceOp.FullyConnected.name:
                return True
            else:
                return False
        return False

李寅 已提交
1278 1279
    def transform_matmul_to_fc(self):
        net = self._model
L
liuqi 已提交
1280
        filter_format = self.filter_format()
李寅 已提交
1281
        for op in net.op:
Y
yejianwu 已提交
1282 1283
            # transform `input(4D) -> reshape(2D) -> matmul` to `fc(2D)`
            # fc output is 2D in transformer, using as 4D in op kernel
L
liuqi 已提交
1284
            # work for TensorFlow
L
liuqi 已提交
1285
            if op.type == MaceOp.Reshape.name and \
L
lichao18 已提交
1286
                    len(op.input) == 2 and \
L
liuqi 已提交
1287 1288
                    op.input[1] in self._consts and \
                    len(op.output_shape[0].dims) == 2 and \
1289
                    filter_format == DataFormat.HWIO and \
李寅 已提交
1290
                    op.input[0] in self._producer:
L
liuqi 已提交
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
                input_op = self._producer[op.input[0]]
                input_shape = input_op.output_shape[0].dims
                # check input op
                if len(input_shape) == 4 and \
                        np.prod(input_shape[1:]) == op.output_shape[0].dims[1]:
                    is_fc = True
                    consumers = self._consumers[op.output[0]]
                    # check matmul op
                    for matmul_op in consumers:
                        if matmul_op.type != MaceOp.MatMul.name:
                            is_fc = False
                        else:
                            weight = self._consts[matmul_op.input[1]]
                            if len(weight.dims) != 2 or \
                               weight.dims[0] != op.output_shape[0].dims[1]:
                                is_fc = False
                    if is_fc:
1308
                        print('convert reshape and matmul to fc')
L
liuqi 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
                        self.safe_remove_node(op, input_op,
                                              remove_input_tensor=True)
                        for matmul_op in consumers:
                            weight = self._consts[matmul_op.input[1]]
                            matmul_op.type = MaceOp.FullyConnected.name
                            weight_data = np.array(weight.float_data).reshape(
                                weight.dims)
                            weight.dims[:] = input_shape[1:] + \
                                [weight_data.shape[1]]
                        return True

Y
yejianwu 已提交
1320 1321
            # transform `fc1(2D) -> matmul` to `fc1(2D) -> fc1(2D)`
            if op.type == MaceOp.MatMul.name and \
1322
                    filter_format == DataFormat.HWIO and \
Y
yejianwu 已提交
1323
                    op.input[1] in self._consts:
Y
yejianwu 已提交
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
                producer = self._producer[op.input[0]]
                weight = self._consts[op.input[1]]
                if len(weight.dims) == 2 and self.is_after_fc(op) and \
                        len(producer.output_shape[0].dims) == 2 and \
                        weight.dims[0] == producer.output_shape[0].dims[1]:
                    six.print_('convert matmul to fc')
                    op.type = MaceOp.FullyConnected.name
                    weight_data = np.array(weight.float_data).reshape(
                        weight.dims)
                    weight.dims[:] = [1, 1] + list(weight_data.shape)
                    return True

李寅 已提交
1336 1337
        return False

1338 1339 1340
    def update_float_op_data_type(self):
        print("update op with float data type")
        net = self._model
1341
        data_type = self._option.data_type
1342 1343 1344 1345 1346
        net.data_type = data_type

        if self._option.quantize:
            return

1347 1348 1349 1350 1351 1352
        for op in net.op:
            data_type_arg = ConverterUtil.get_arg(
                op, MaceKeyword.mace_op_data_type_str)
            if not data_type_arg:
                data_type_arg = op.arg.add()
                data_type_arg.name = MaceKeyword.mace_op_data_type_str
1353 1354
                data_type_arg.i = data_type
            elif data_type_arg.i != data_type \
L
liuqi 已提交
1355
                    and data_type_arg.i == mace_pb2.DT_FLOAT:
1356
                data_type_arg.i = data_type
1357 1358 1359

        return False

1360
    def sort_dfs(self, op, visited, sorted_nodes):
1361 1362
        if op.name in visited:
            return
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
        visited.update([op.name])
        if len(op.input) > 0:
            for input_tensor in op.input:
                producer_op = self._producer.get(input_tensor, None)
                if producer_op is None:
                    pass
                elif producer_op.name not in visited:
                    self.sort_dfs(producer_op, visited, sorted_nodes)
        sorted_nodes.append(op)

    def sort_by_execution(self):
        print("Sort by execution")
        net = self._model
        visited = set()
        sorted_nodes = []

L
luxuhui 已提交
1379
        output_nodes = list(self._option.check_nodes.keys())
B
Bin Li 已提交
1380
        if not self._quantize_activation_info:
B
Bin Li 已提交
1381
            output_nodes.extend(self._option.output_nodes)
B
Bin Li 已提交
1382
        for output_node in output_nodes:
1383 1384 1385
            mace_check(output_node in self._producer,
                       "output_tensor %s not existed in model" % output_node)
            self.sort_dfs(self._producer[output_node], visited, sorted_nodes)
1386 1387 1388

        del net.op[:]
        net.op.extend(sorted_nodes)
李寅 已提交
1389 1390

        print("Final ops:")
B
Bin Li 已提交
1391
        index = 0
李寅 已提交
1392
        for op in net.op:
B
Bin Li 已提交
1393 1394 1395 1396 1397 1398
            if op.type not in [MaceOp.Quantize.name, MaceOp.Dequantize.name]:
                index_str = str(index)
                index += 1
            else:
                index_str = ''
            print("%s (%s, index:%s): %s" % (op.name, op.type, index_str, [
李寅 已提交
1399
                out_shape.dims for out_shape in op.output_shape]))
1400
        return False
李寅 已提交
1401

1402 1403 1404 1405
    def is_transposable_data_format_ops(self, op):
        if op.type == MaceOp.Reshape:
            input_op = self._producer[op.input[0]]
            out_dims_len = len(op.output_shape[0].dims)
1406 1407
            if len(input_op.output_shape) != 1 or \
                len(input_op.output_shape[0].dims) != 4 \
1408 1409 1410 1411 1412
                    or (out_dims_len != 4 and out_dims_len != 2):
                print("In this model, reshape is not transposable op.")
                return False
        return op.type in MaceTransposableDataFormatOps

1413
    def update_data_format(self):
1414
        print("update data format")
1415 1416
        net = self._model
        for op in net.op:
1417
            df_arg = ConverterUtil.get_arg(
1418
                op, MaceKeyword.mace_data_format_str)
1419 1420 1421
            if not df_arg:
                df_arg = op.arg.add()
                df_arg.name = MaceKeyword.mace_data_format_str
1422
            if op.type in MaceFixedDataFormatOps:
1423
                df_arg.i = DataFormat.AUTO.value
1424
            elif self.is_transposable_data_format_ops(op):
1425
                input_df = DataFormat.AUTO.value
1426 1427 1428
                for input_tensor in op.input:
                    if input_tensor in self._consts:
                        continue
1429 1430 1431
                    mace_check(
                        input_tensor in self._producer,
                        "Input tensor %s not in producer" % input_tensor)
1432 1433 1434
                    father_op = self._producer[input_tensor]
                    temp_input_df = ConverterUtil.get_arg(
                        father_op, MaceKeyword.mace_data_format_str)
1435
                    if temp_input_df.i != DataFormat.AUTO.value:
1436
                        input_df = temp_input_df.i
1437
                if input_df == DataFormat.AUTO.value:
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
                    df_arg.i = input_df
                    # add flag to mark the ops may has data format
                    has_data_format_arg = op.arg.add()
                    has_data_format_arg.name = \
                        MaceKeyword.mace_has_data_format_str
                    has_data_format_arg.i = 1
        return False

    def transpose_data_format(self):
        print("Transpose arguments based on data format")
        net = self._model

        src_data_format = ConverterUtil.data_format(net)
        for op in net.op:
            has_data_format = ConverterUtil.data_format(op) == \
1453
                              DataFormat.AUTO
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
            # transpose args
            if op.type == MaceOp.Pad.name:
                for arg in op.arg:
                    if arg.name == MaceKeyword.mace_paddings_str:
                        mace_check(len(arg.ints) == 8,
                                   "pad dim rank should be 8.")
                        if src_data_format == DataFormat.NCHW and \
                                has_data_format:
                            print("Transpose pad args: %s(%s)"
                                  % (op.name, op.type))
                            self.transpose_shape(arg.ints,
                                                 [0, 1, 4, 5, 6, 7, 2, 3])
            elif op.type == MaceOp.Concat.name or op.type == MaceOp.Split.name:
                for arg in op.arg:
                    if arg.name == MaceKeyword.mace_axis_str:
                        if (src_data_format == DataFormat.NCHW
                                and has_data_format
                                and len(op.output_shape[0].dims) == 4):
                            print("Transpose concat/split args: %s(%s)"
                                  % (op.name, op.type))
                            if arg.i == 1:
                                arg.i = 3
                            elif arg.i == 2:
                                arg.i = 1
                            elif arg.i == 3:
                                arg.i = 2
L
liutuo 已提交
1480 1481 1482 1483 1484 1485 1486 1487 1488
                        if op.input[0] in self._producer:
                            producer = self._producer[op.input[0]]
                            input_shape = producer.output_shape[0].dims
                            if (producer.type == MaceOp.FullyConnected.name
                                    and len(input_shape) == 2):
                                axis_arg = ConverterUtil.get_arg(
                                        op, MaceKeyword.mace_axis_str)
                                if axis_arg.i == 1:
                                    axis_arg.i = 3
1489

Y
yejianwu 已提交
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
            elif op.type == MaceOp.Squeeze.name:
                for arg in op.arg:
                    if arg.name == MaceKeyword.mace_axis_str:
                        if (src_data_format == DataFormat.NCHW
                                and has_data_format
                                and len(self._producer[op.input[0]].output_shape[0].dims) == 4  # noqa
                                and len(op.output_shape[0].dims) == 2
                                and arg.ints == [2, 3]):
                            print("Transpose squeeze args: %s(%s)"
                                  % (op.name, op.type))
                            arg.ints[:] = [1, 2]

1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
            elif op.type == MaceOp.Reduce.name:
                for arg in op.arg:
                    if arg.name == MaceKeyword.mace_axis_str:
                        if src_data_format == DataFormat.NCHW and \
                                has_data_format:
                            print("Transpose reduce args: %s(%s)"
                                  % (op.name, op.type))
                            reduce_axises = list(arg.ints)
                            new_axises = []
                            for i in range(len(reduce_axises)):
                                idx = reduce_axises[i]
                                if idx == 2 or idx == 3:
                                    new_axises.append(idx - 1)
                                elif idx == 1:
                                    new_axises.append(3)
                                else:
                                    new_axises.append(idx)
                            new_axises.sort()
                            arg.ints[:] = []
                            arg.ints.extend(new_axises)
            elif op.type == MaceOp.Crop.name:
                offset_arg = ConverterUtil.get_arg(op,
                                                   MaceKeyword.mace_offset_str)
                mace_check(offset_arg and
                           src_data_format == DataFormat.NCHW
                           and has_data_format
                           and len(op.output_shape[0].dims) == 4,
                           "MACE only support crop with NCHW format")
                print("Transpose crop args: %s(%s)"
                      % (op.name, op.type))
                self.transpose_shape(offset_arg.ints, [0, 2, 3, 1])
L
luxuhui 已提交
1533 1534 1535 1536 1537 1538 1539
            elif op.type == MaceOp.Reshape.name:
                for arg in op.arg:
                    if arg.name == MaceKeyword.mace_dim_str and \
                            len(arg.ints) == 4 and \
                            src_data_format == DataFormat.NCHW and \
                            has_data_format:
                        self.transpose_shape(arg.ints, [0, 2, 3, 1])
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549

            # transpose op output shape
            if src_data_format == DataFormat.NCHW and \
                    has_data_format:
                print("Transpose output shapes: %s(%s)" % (op.name, op.type))
                for output_shape in op.output_shape:
                    if len(output_shape.dims) == 4:
                        self.transpose_shape(output_shape.dims,
                                             [0, 2, 3, 1])

1550 1551
        return False

李寅 已提交
1552
    def quantize_nodes(self):
李寅 已提交
1553 1554 1555
        if not self._option.quantize:
            return False

李寅 已提交
1556 1557 1558
        print("Add mace quantize and dequantize nodes")

        for op in self._model.op:
1559
            for i in range(len(op.input)):
B
Bin Li 已提交
1560 1561
                if op.input[i] in self.input_name_map:
                    op.input[i] = self.input_name_map[op.input[i]]
1562
            for i in range(len(op.output)):
B
Bin Li 已提交
1563
                if op.output[i] in self.output_name_map:
B
Bin Li 已提交
1564 1565
                    op.name = MaceKeyword.mace_output_node_name \
                              + '_' + op.name
B
Bin Li 已提交
1566
                    new_output_name = self.output_name_map[op.output[i]]
B
Bin Li 已提交
1567 1568
                    self._quantize_activation_info[new_output_name] = \
                        self._quantize_activation_info[op.output[i]]
1569 1570 1571 1572 1573
                    if op.output[i] in self._consumers:
                        for consumer_op in self._consumers[op.output[i]]:
                            self.replace(consumer_op.input,
                                         op.output[i],
                                         new_output_name)
B
Bin Li 已提交
1574
                    op.output[i] = new_output_name
1575

李寅 已提交
1576 1577 1578 1579 1580 1581
            data_type_arg = ConverterUtil.get_arg(
                op, MaceKeyword.mace_op_data_type_str)
            mace_check(data_type_arg, "Data type does not exist for %s(%s)"
                       % (op.name, op.type))
            if data_type_arg.i == mace_pb2.DT_FLOAT:
                data_type_arg.i = mace_pb2.DT_UINT8
1582 1583 1584 1585 1586
            elif data_type_arg.i == mace_pb2.DT_UINT8:
                mace_check(op.type == MaceOp.Quantize.name
                           or op.type == MaceOp.Dequantize.name,
                           "Only Quantization ops support uint8, "
                           "but got %s(%s)" % (op.name, op.type))
李寅 已提交
1587
            else:
1588
                mace_check(op.type == MaceOp.Quantize.name,
李寅 已提交
1589
                           "Quantization only support float ops, "
B
Bin Li 已提交
1590 1591 1592
                           "but get %s(%s, %s)"
                           % (op.name, op.type,
                              mace_pb2.DataType.Name(data_type_arg.i)))
李寅 已提交
1593

B
Bin Li 已提交
1594
        for i, input_node in enumerate(self._option.input_nodes.values()):
B
Bin Li 已提交
1595
            new_input_name = self.input_name_map[input_node.name]
李寅 已提交
1596
            op_def = self._model.op.add()
B
Bin Li 已提交
1597
            op_def.name = self.normalize_op_name(new_input_name)
李寅 已提交
1598
            op_def.type = MaceOp.Quantize.name
1599
            op_def.input.extend([input_node.name])
B
Bin Li 已提交
1600
            op_def.output.extend([new_input_name])
李寅 已提交
1601 1602
            output_shape = op_def.output_shape.add()
            output_shape.dims.extend(input_node.shape)
B
Bin Li 已提交
1603 1604 1605 1606
            quantize_info = self._quantize_activation_info[new_input_name]
            self.copy_quantize_info(op_def, quantize_info)
            self._model.input_info[i].scale = quantize_info.scale
            self._model.input_info[i].zero_point = quantize_info.zero_point
李寅 已提交
1607 1608

            ConverterUtil.add_data_type_arg(op_def, mace_pb2.DT_UINT8)
1609
            ConverterUtil.add_data_format_arg(op_def, input_node.data_format)
B
Bin Li 已提交
1610 1611 1612 1613 1614
            # use actual ranges for model input quantize
            find_range_every_time_arg = op_def.arg.add()
            find_range_every_time_arg.name = \
                MaceKeyword.mace_find_range_every_time
            find_range_every_time_arg.i = 1
李寅 已提交
1615

B
Bin Li 已提交
1616
        output_nodes = self._option.check_nodes.values()
B
Bin Li 已提交
1617
        for i, output_node in enumerate(output_nodes):
李寅 已提交
1618
            op_def = self._model.op.add()
B
Bin Li 已提交
1619
            op_def.name = self.normalize_op_name(output_node.name)
李寅 已提交
1620
            op_def.type = MaceOp.Dequantize.name
B
Bin Li 已提交
1621
            op_def.input.extend([self.output_name_map[output_node.name]])
1622
            op_def.output.extend([output_node.name])
李寅 已提交
1623
            output_shape = op_def.output_shape.add()
B
Bin Li 已提交
1624 1625
            producer_op = self._producer[output_node.name]
            output_shape.dims.extend(producer_op.output_shape[0].dims)
李寅 已提交
1626
            op_def.output_type.extend([mace_pb2.DT_FLOAT])
B
Bin Li 已提交
1627 1628 1629
            quantize_info = producer_op.quantize_info[0]
            self._model.output_info[i].scale = quantize_info.scale
            self._model.output_info[i].zero_point = quantize_info.zero_point
李寅 已提交
1630 1631

            ConverterUtil.add_data_type_arg(op_def, mace_pb2.DT_UINT8)
1632
            ConverterUtil.add_data_format_arg(op_def, output_node.data_format)
李寅 已提交
1633

1634 1635 1636
        quantize_flag_arg = self._model.arg.add()
        quantize_flag_arg.name = MaceKeyword.mace_quantize_flag_arg_str
        quantize_flag_arg.i = 1
李寅 已提交
1637

李寅 已提交
1638
        return False
李寅 已提交
1639 1640 1641 1642 1643

    def quantize_tensor(self, tensor):
        """Assume biasadd has been already folded with convolution and fc"""
        if tensor.data_type == mace_pb2.DT_FLOAT:
            ops = self._consumers.get(tensor.name, None)
L
liutuo 已提交
1644 1645 1646
            check_conv = False
            check_deconv = False
            if ops is not None and len(ops) == 1:
L
liutuo 已提交
1647 1648 1649 1650 1651 1652
                if len(ops[0].input) >= 3:
                    check_conv =\
                        ops[0].type in [MaceOp.Conv2D.name,
                                        MaceOp.DepthwiseConv2d.name,
                                        MaceOp.FullyConnected.name]\
                        and ops[0].input[2] == tensor.name
L
liutuo 已提交
1653
                # in tensorflow deconv's bias is the forth input
L
liutuo 已提交
1654 1655
                if ops[0].type in [MaceOp.Deconv2D.name,
                                   MaceOp.DepthwiseDeconv2d]:
L
liutuo 已提交
1656 1657 1658 1659
                    from_caffe = ConverterUtil.get_arg(
                        ops[0],
                        MaceKeyword.mace_framework_type_str).i ==\
                                 FrameworkType.CAFFE.value
L
liutuo 已提交
1660 1661
                    if from_caffe and len(ops[0].input) >= 3:
                        check_deconv = ops[0].input[2] == tensor.name
L
liutuo 已提交
1662
                    else:
L
liutuo 已提交
1663 1664
                        if len(ops[0].input) >= 4:
                            check_deconv = ops[0].input[3] == tensor.name
L
liutuo 已提交
1665
            if check_conv or check_deconv:
1666 1667
                if self._option.device == DeviceType.CPU.value \
                       or self._option.device == DeviceType.APU.value:
B
Bin Li 已提交
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
                    conv_op = ops[0]
                    scale_input = self._quantize_activation_info[
                        conv_op.input[0]].scale
                    if conv_op.input[1] not in self._quantized_tensor:
                        self.quantize_tensor(self._consts[conv_op.input[1]])
                    scale_filter = self._consts[conv_op.input[1]].scale
                    scale = scale_input * scale_filter
                    quantized_tensor = \
                        quantize_util.quantize_with_scale_and_zero(
                            tensor.float_data, scale, 0)
B
Bin Li 已提交
1678 1679
                elif self._option.device == DeviceType.HEXAGON.value or \
                        self._option.device == DeviceType.HTA.value:
B
Bin Li 已提交
1680 1681 1682 1683 1684
                    quantized_tensor = \
                        quantize_util.quantize_bias_for_hexagon(
                            tensor.float_data)
                else:
                    mace_check(False, "wrong device.")
李寅 已提交
1685 1686
                tensor.data_type = mace_pb2.DT_INT32
            else:
1687 1688
                non_zero = self._option.device == DeviceType.CPU.value
                quantized_tensor = quantize_util.quantize(tensor.float_data,
B
Bin Li 已提交
1689
                                                          self._option.device,
1690
                                                          non_zero)
李寅 已提交
1691 1692 1693 1694 1695 1696
                tensor.data_type = mace_pb2.DT_UINT8

            del tensor.float_data[:]
            tensor.int32_data.extend(quantized_tensor.data)
            tensor.scale = quantized_tensor.scale
            tensor.zero_point = quantized_tensor.zero
B
Bin Li 已提交
1697 1698
            tensor.minval = quantized_tensor.minval
            tensor.maxval = quantized_tensor.maxval
李寅 已提交
1699
            tensor.quantized = True
李寅 已提交
1700 1701
            self._quantized_tensor.update([tensor.name])

李寅 已提交
1702 1703
        return False

李寅 已提交
1704 1705 1706 1707 1708
    def quantize_weights(self):
        print("Quantize weights")
        net = self._model
        for tensor in net.tensors:
            self.quantize_tensor(tensor)
李寅 已提交
1709 1710 1711

        return False

B
Bin Li 已提交
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
    def quantize_large_tensor(self, tensor):
        if tensor.data_type == mace_pb2.DT_FLOAT:
            ops = self._consumers.get(tensor.name, None)
            if ops is not None and len(ops) == 1:
                if ops[0].type in [MaceOp.Conv2D.name,
                                   MaceOp.FullyConnected.name]:
                    quantized_tensor = \
                        quantize_util.quantize(tensor.float_data,
                                               self._option.device,
                                               False)
                    tensor.data_type = mace_pb2.DT_UINT8

                    del tensor.float_data[:]
                    tensor.int32_data.extend(quantized_tensor.data)
                    tensor.scale = quantized_tensor.scale
                    tensor.zero_point = quantized_tensor.zero
                    tensor.minval = quantized_tensor.minval
                    tensor.maxval = quantized_tensor.maxval
                    tensor.quantized = True
                    self._quantized_tensor.update([tensor.name])

    def quantize_large_weights(self):
        print("Quantize large weights")
        net = self._model
        for tensor in net.tensors:
            self.quantize_large_tensor(tensor)

        return False

李寅 已提交
1741
    def add_quantize_info(self, op, minval, maxval):
B
Bin Li 已提交
1742
        scale, zero, minval, maxval = \
B
Bin Li 已提交
1743 1744
            quantize_util.adjust_range(minval, maxval, self._option.device,
                                       non_zero=False)
李寅 已提交
1745 1746 1747 1748 1749 1750 1751 1752
        quantize_info = op.quantize_info.add()
        quantize_info.minval = minval
        quantize_info.maxval = maxval
        quantize_info.scale = scale
        quantize_info.zero_point = zero

        return quantize_info

1753 1754 1755 1756 1757 1758 1759
    def copy_quantize_info(self, op, info):
        quantize_info = op.quantize_info.add()
        quantize_info.minval = info.minval
        quantize_info.maxval = info.maxval
        quantize_info.scale = info.scale
        quantize_info.zero_point = info.zero_point

李寅 已提交
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
    def transform_fake_quantize(self):
        if not self._option.quantize:
            return False

        # Quantize info from fixpoint fine tune
        print("Transform fake quantize")
        range_file = self._option.quantize_range_file
        if range_file:
            return

        net = self._model
        for op in net.op:
1772 1773
            if op.type == 'FakeQuantWithMinMaxVars' or \
                   op.type == 'FakeQuantWithMinMaxArgs':
B
Bin Li 已提交
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
                if op.input[0] not in self._consts:
                    producer_op = self._producer[op.input[0]]
                    minval = ConverterUtil.get_arg(op, 'min').f
                    maxval = ConverterUtil.get_arg(op, 'max').f
                    quantize_info = \
                        self.add_quantize_info(producer_op, minval, maxval)
                    self._quantize_activation_info[op.input[0]] = quantize_info
                    # for add -> fakequant pattern
                    self._quantize_activation_info[op.output[0]] = \
                        quantize_info

                    print(op.input[0], op.output[0])
李寅 已提交
1786 1787 1788 1789
                op.type = MaceOp.Identity.name

        return False

B
Bin Li 已提交
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
    def rearrange_batch_to_space(self):
        if not self._option.quantize:
            return False

        # Put b2s after biasadd and relu
        for conv_op in self._model.op:
            if conv_op.type in [MaceOp.Conv2D.name,
                                MaceOp.DepthwiseConv2d.name] \
                    and self.consumer_count(conv_op.output[0]) == 1:
                b2s_op = self._consumers[conv_op.output[0]][0]
                if b2s_op.type == MaceOp.BatchToSpaceND.name \
                        and self.consumer_count(b2s_op.output[0]) == 1:
                    biasadd_or_act_op = self._consumers[b2s_op.output[0]][0]
                    if biasadd_or_act_op.type == MaceOp.BiasAdd.name:
                        biasadd_op = biasadd_or_act_op
                        if self.consumer_count(biasadd_op.output[0]) == 1 \
                                and self._consumers[biasadd_op.output[0]][0].type == MaceOp.Activation.name:  # noqa
                            act_op = self._consumers[biasadd_op.output[0]][0]
                            biasadd_op.input[0] = conv_op.output[0]
                            b2s_op.input[0] = act_op.output[0]
                            for op in self._consumers[act_op.output[0]]:
                                self.replace(op.input,
                                             act_op.output[0],
                                             b2s_op.output[0])
                        else:
                            biasadd_op.input[0] = conv_op.output[0]
                            b2s_op.input[0] = biasadd_op.output[0]
                            for op in self._consumers[biasadd_op.output[0]]:
                                self.replace(op.input,
                                             biasadd_op.output[0],
                                             b2s_op.output[0])

                        print("Rearrange batch to space: %s(%s)"
                              % (b2s_op.name, b2s_op.type))
                        return True
                    elif biasadd_or_act_op.type == MaceOp.Activation.name:
                        act_op = biasadd_or_act_op
                        act_op.input[0] = conv_op.output[0]
                        b2s_op.input[0] = act_op.output[0]
                        for op in self._consumers[act_op.output[0]]:
                            self.replace(op.input,
                                         act_op.output[0],
                                         b2s_op.output[0])

                        print("Rearrange batch to space: %s(%s)"
                              % (b2s_op.name, b2s_op.type))
                        return True

        return False

李寅 已提交
1840 1841 1842 1843
    def add_quantize_tensor_range(self):
        # Quantize info from range statistics
        range_file = self._option.quantize_range_file
        if range_file:
李寅 已提交
1844
            print("Add quantize tensor range")
李寅 已提交
1845 1846
            with open(range_file) as f:
                for line in f:
1847
                    tensor_name, minmax = line.split("@@")[:2]
李寅 已提交
1848 1849
                    min_val, max_val = [float(i) for i in
                                        minmax.strip().split(",")]
B
Bin Li 已提交
1850
                    scale, zero, min_val, max_val = \
B
Bin Li 已提交
1851 1852 1853
                        quantize_util.adjust_range(min_val, max_val,
                                                   self._option.device,
                                                   non_zero=False)
李寅 已提交
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
                    activation_info = mace_pb2.QuantizeActivationInfo()
                    activation_info.minval = min_val
                    activation_info.maxval = max_val
                    activation_info.scale = scale
                    activation_info.zero_point = zero
                    self._quantize_activation_info[tensor_name] = activation_info  # noqa

            for op in self._model.op:
                if op.name.find(MaceKeyword.mace_output_node_name) >= 0:
                    continue
                for output in op.output:
                    mace_check(output in self._quantize_activation_info,
                               "%s does not have quantize activation info"
                               % op)
B
Bin Li 已提交
1868 1869
                    op.quantize_info.extend([
                        self._quantize_activation_info[output]])
李寅 已提交
1870

1871 1872
        if not self._option.quantize:
            return False
B
Bin Li 已提交
1873 1874

        print("Add default quantize info for input")
B
Bin Li 已提交
1875
        for i, input_node in enumerate(self._option.input_nodes.values()):
B
Bin Li 已提交
1876 1877 1878 1879 1880 1881 1882
            if input_node.name not in self._quantize_activation_info:
                print("Input range %s: %s" % (input_node.name,
                                              str(input_node.range)))
                new_input_name = self.input_name_map[input_node.name]
                scale, zero, minval, maxval = \
                    quantize_util.adjust_range(input_node.range[0],
                                               input_node.range[1],
B
Bin Li 已提交
1883
                                               self._option.device,
B
Bin Li 已提交
1884
                                               non_zero=False)
1885 1886
                quantize_info = \
                    mace_pb2.QuantizeActivationInfo()
B
Bin Li 已提交
1887 1888 1889 1890 1891
                quantize_info.minval = minval
                quantize_info.maxval = maxval
                quantize_info.scale = scale
                quantize_info.zero_point = zero
                self._quantize_activation_info[new_input_name] = quantize_info
B
Bin Li 已提交
1892 1893
                input_op = self._producer[input_node.name]
                input_op.quantize_info.extend([quantize_info])
B
Bin Li 已提交
1894

1895
        print("Add default quantize info for ops like Pooling, Softmax")
李寅 已提交
1896
        for op in self._model.op:
B
Bin Li 已提交
1897 1898 1899
            if op.type in [MaceOp.ExpandDims.name,
                           MaceOp.Pad.name,
                           MaceOp.Pooling.name,
B
Bin Li 已提交
1900
                           MaceOp.Reduce.name,
B
Bin Li 已提交
1901
                           MaceOp.Reshape.name,
李寅 已提交
1902
                           MaceOp.ResizeBilinear.name,
B
Bin Li 已提交
1903 1904
                           MaceOp.Squeeze.name,
                           MaceOp.StridedSlice.name,
李寅 已提交
1905
                           MaceOp.BatchToSpaceND.name,
1906 1907 1908
                           MaceOp.SpaceToBatchND.name,
                           MaceOp.SpaceToDepth.name,
                           MaceOp.DepthToSpace.name]:
李寅 已提交
1909 1910
                del op.quantize_info[:]
                producer_op = self._producer[op.input[0]]
B
Bin Li 已提交
1911 1912 1913 1914 1915
                if producer_op.output[0] in self._option.input_nodes:
                    new_input_name = self.input_name_map[producer_op.output[0]]
                    self.copy_quantize_info(
                        op, self._quantize_activation_info[new_input_name])
                else:
1916 1917
                    self.copy_quantize_info(op,
                                            producer_op.quantize_info[0])
1918 1919
                self._quantize_activation_info[op.output[0]] = \
                    op.quantize_info[0]
1920 1921 1922
            elif (op.type == MaceOp.Concat.name
                  and (not op.quantize_info
                       or self._option.change_concat_ranges)):
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
                if op.quantize_info:
                    maxval = op.quantize_info[0].maxval
                    minval = op.quantize_info[0].minval
                    del op.quantize_info[:]
                else:
                    maxval = float("-inf")
                    minval = float("inf")
                for i in range(len(op.input)):
                    minval = min(minval, self._producer[op.input[i]].quantize_info[0].minval)  # noqa
                    maxval = max(maxval, self._producer[op.input[i]].quantize_info[0].maxval)  # noqa
                quantize_info = \
                    self.add_quantize_info(op, minval, maxval)
李寅 已提交
1935
                self._quantize_activation_info[op.output[0]] = quantize_info
1936 1937 1938 1939 1940 1941 1942
                if self._option.change_concat_ranges:
                    for i in range(len(op.input)):
                        producer_op = self._producer[op.input[i]]
                        del producer_op.quantize_info[:]
                        self.copy_quantize_info(producer_op, quantize_info)
                        self._quantize_activation_info[producer_op.output[0]] \
                            = producer_op.quantize_info[0]
B
Bin Li 已提交
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
            elif op.type == MaceOp.Activation.name:
                act_type = ConverterUtil.get_arg(
                    op, MaceKeyword.mace_activation_type_str).s.decode()
                if act_type not in [ActivationType.TANH.name,
                                    ActivationType.SIGMOID.name]:
                    continue
                del op.quantize_info[:]
                if act_type == ActivationType.TANH.name:
                    quantize_info = self.add_quantize_info(op, -1.0, 1.0)
                else:
                    quantize_info = self.add_quantize_info(op, 0.0, 1.0)
                self._quantize_activation_info[op.output[0]] = quantize_info
李寅 已提交
1955 1956 1957 1958 1959
            elif op.type == MaceOp.Softmax.name:
                del op.quantize_info[:]
                quantize_info = \
                    self.add_quantize_info(op, 0.0, 1.0)
                self._quantize_activation_info[op.output[0]] = quantize_info
B
Bin Li 已提交
1960 1961 1962
            elif (op.type == MaceOp.Eltwise.name
                  and not op.quantize_info
                  and len(op.input) == 2
B
Bin Li 已提交
1963 1964
                  and op.input[0] not in self._consts
                  and op.input[1] not in self._consts):
B
Bin Li 已提交
1965 1966
                producer_op0 = self._producer[op.input[0]]
                producer_op1 = self._producer[op.input[1]]
B
Bin Li 已提交
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
                if ConverterUtil.get_arg(
                        op, MaceKeyword.mace_element_type_str).i \
                        == EltwiseType.SUM.value:
                    minval = producer_op0.quantize_info[0].minval \
                        + producer_op1.quantize_info[0].minval
                    maxval = producer_op0.quantize_info[0].maxval \
                        + producer_op1.quantize_info[0].maxval
                elif ConverterUtil.get_arg(
                        op, MaceKeyword.mace_element_type_str).i \
                        == EltwiseType.SUB.value:
                    minval = producer_op0.quantize_info[0].minval \
                        - producer_op1.quantize_info[0].maxval
                    maxval = producer_op0.quantize_info[0].maxval \
                        - producer_op1.quantize_info[0].minval
                else:
                    mace_check(False, "Quantized Elementwise only support:"
B
Bin Li 已提交
1983
                                      " SUM and SUB without ranges now.")
1984 1985
                quantize_info = \
                    self.add_quantize_info(op, minval, maxval)
B
Bin Li 已提交
1986
                self._quantize_activation_info[op.output[0]] = quantize_info
李寅 已提交
1987 1988 1989 1990 1991 1992 1993

        return False

    def check_quantize_info(self):
        if not self._option.quantize:
            return False

B
Bin Li 已提交
1994
        print("Check quantize info")
李寅 已提交
1995 1996 1997 1998 1999 2000 2001
        for op in self._model.op:
            if (op.name.find(MaceKeyword.mace_input_node_name) == -1
                and op.name.find(MaceKeyword.mace_output_node_name) == -1
                and op.type != MaceOp.Quantize.name
                and op.type != MaceOp.Dequantize.name):  # noqa
                mace_check(len(op.output) == len(op.quantize_info),
                           "missing quantize info: %s" % op)
2002 2003 2004 2005 2006
            for i in six.moves.range(len(op.quantize_info)):
                print("Op output %s range: [%f, %f]" % (
                    op.output[i],
                    op.quantize_info[i].minval,
                    op.quantize_info[i].maxval))
2007

Y
yulianfei 已提交
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
    def fp16_gather_weight(self):
        for op in self._model.op:
            if op.type != MaceOp.Gather.name:
                continue
            if op.input[0] not in self._consts:
                raise KeyError("Not in const tensor: " + str(op.input[0]))

            const_tensor = self._consts[op.input[0]]
            if const_tensor.data_type == mace_pb2.DT_FLOAT16:
                print(str(const_tensor.name) + " is alreay float16")
                continue

            print("FP16 Embedding Lookup Weights: %s" % const_tensor.name)

            op_outputs = [x for x in op.output]
            new_gather_name = op.name + '_fp16'
            new_gather_output_name = new_gather_name + ":0"
            dehalve_name = op.name

            # fp16 weights
            const_tensor.data_type = mace_pb2.DT_FLOAT16

            # change gather
            op.name = new_gather_name
            op.output[:] = [new_gather_output_name]
            # op.output.extend([new_gather_output_name])
            data_type_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_op_data_type_str)  # noqa
            if data_type_arg is None:
                data_type_arg = op.arg.add()
                data_type_arg.name = MaceKeyword.mace_op_data_type_str
            data_type_arg.i = mace_pb2.DT_FLOAT16

            # add dehalve
            dehalve_op = self._model.op.add()
            dehalve_op.name = dehalve_name
            dehalve_op.type = MaceOp.Cast.name
            dehalve_op.input.extend([new_gather_output_name])
            dehalve_op.output.extend(op_outputs)
            dehalve_op.output_shape.extend(op.output_shape)
            dehalve_op.output_type.extend([mace_pb2.DT_FLOAT])
            data_type_arg = dehalve_op.arg.add()
            data_type_arg.name = MaceKeyword.mace_op_data_type_str
            data_type_arg.i = mace_pb2.DT_FLOAT16

2052 2053 2054 2055
    def fp16_matmul_weight(self):
        if self._option.device != DeviceType.CPU.value:
            return

L
liukai6 已提交
2056
        print('Convert matmul weights to fp16 for specific matmul: activation + weights')  # noqa
2057 2058 2059 2060

        for op in self._model.op:
            if op.type != MaceOp.MatMul.name:
                continue
L
liukai6 已提交
2061
            if op.input[0] not in self._consts and op.input[1] not in self._consts:  # noqa
2062
                continue
L
liukai6 已提交
2063
            if op.input[0] in self._consts and op.input[1] in self._consts:
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
                continue

            # Matmul fp16 Op only support fp32[1,k] x fp16[w,k]T or fp16[w,k] x fp32[k,1] now!  # noqa

            transpose_a_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_transpose_a_str)  # noqa
            transpose_b_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_transpose_b_str)  # noqa
            transpose_a = transpose_a_arg is not None and transpose_a_arg.i == 1  # noqa
            transpose_b = transpose_b_arg is not None and transpose_b_arg.i == 1  # noqa

            left_tensor = op.input[0]
            right_tensor = op.input[1]
            left_shape = self.get_tensor_shape(left_tensor)
            right_shape = self.get_tensor_shape(right_tensor)

            height = left_shape[-1] if transpose_a else left_shape[-2]
            width = right_shape[-2] if transpose_b else right_shape[-1]
            batch = reduce(lambda x, y: x * y, left_shape[: -2], 1)

            if batch != 1:
                continue

            if left_tensor in self._consts:
                if width != 1 or transpose_a:
                    continue
                const_tensor = self._consts[left_tensor]
            else:
                if height != 1 or not transpose_b:
                    continue
                const_tensor = self._consts[right_tensor]

            print('Convert Matmul Weights to fp16: %s' % op.name)

            const_tensor.data_type = mace_pb2.DT_FLOAT16
            data_type_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_op_data_type_str)  # noqa
            if data_type_arg is None:
                data_type_arg = op.arg.add()
                data_type_arg.name = MaceKeyword.mace_op_data_type_str
            data_type_arg.i = mace_pb2.DT_FLOAT16
            op.output_type.extend([mace_pb2.DT_FLOAT])

2104 2105 2106 2107 2108 2109 2110 2111 2112
    def add_opencl_informations(self):
        print("Add OpenCL informations")

        net = self._model

        arg = net.arg.add()
        arg.name = MaceKeyword.mace_opencl_mem_type
        arg.i = mace_pb2.GPU_IMAGE if self._option.cl_mem_type == "image"\
            else mace_pb2.GPU_BUFFER
B
Bin Li 已提交
2113

L
luxuhui 已提交
2114
    def transform_reshape_and_flatten(self):
L
lichao18 已提交
2115 2116
        net = self._model
        for op in net.op:
L
luxuhui 已提交
2117 2118 2119 2120 2121
            if op.type != MaceOp.Reshape.name:
                continue
            dim_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_dim_str)
            shape_tensor = None
            if len(op.input) == 1:
L
lichao18 已提交
2122
                print("Transform Caffe Reshape")
L
lichao18 已提交
2123 2124 2125 2126 2127
                dims = []
                axis_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_axis_str)
                # transform caffe reshape op
                if dim_arg:
                    dims = dim_arg.ints
L
lichao18 已提交
2128 2129 2130 2131
                    shape_tensor = net.tensors.add()
                    shape_tensor.name = op.name + '_shape'
                    shape_tensor.dims.append(len(op.output_shape[0].dims))
                    shape_tensor.data_type = mace_pb2.DT_INT32
L
lichao18 已提交
2132 2133 2134
                # transform caffe flatten op
                elif axis_arg is not None:
                    axis = axis_arg.i
L
lichao18 已提交
2135
                    for i in range(0, axis):
L
lichao18 已提交
2136 2137 2138 2139
                        dims.append(0)
                    dims.append(-1)
                    for i in range(axis + 1, len(op.output_shape[0].dims)):
                        dims.append(0)
L
lichao18 已提交
2140 2141 2142 2143
                    shape_tensor = net.tensors.add()
                    shape_tensor.name = op.name + '_shape'
                    shape_tensor.dims.append(len(dims))
                    shape_tensor.data_type = mace_pb2.DT_INT32
L
lichao18 已提交
2144 2145 2146 2147
                else:
                    mace_check(False, "Only support reshape and flatten")
                shape_tensor.int32_data.extend(dims)
                op.input.append(shape_tensor.name)
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160

    def transform_shape_tensor_to_param(self):
        kOpTypeInputIdxMap = {
            MaceOp.ResizeNearestNeighbor.name: 1,
            MaceOp.Deconv2D.name: 2,
            MaceOp.Reshape.name: 1,
        }
        net = self._model
        for op in net.op:
            if op.type not in kOpTypeInputIdxMap:
                continue
            shape_idx = kOpTypeInputIdxMap[op.type]
            dim_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_dim_str)
L
luxuhui 已提交
2161 2162
            if len(op.input) > shape_idx and dim_arg is None and \
                    op.input[shape_idx] in self._consts:
2163
                shape_tensor = self._consts[op.input[shape_idx]]
L
luxuhui 已提交
2164 2165 2166
                dim_arg = op.arg.add()
                dim_arg.name = MaceKeyword.mace_dim_str
                dim_arg.ints.extend(shape_tensor.int32_data)
Y
yejianwu 已提交
2167 2168 2169 2170 2171

    def fold_fc_reshape(self):
        net = self._model
        for op in net.op:
            # whether to reshape fc output(default 4D)
L
liutuo 已提交
2172 2173
            if op.type == MaceOp.FullyConnected.name and\
                    op.output[0] in self._consumers:
Y
yejianwu 已提交
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
                consumers = self._consumers[op.output[0]]
                op_output_shape = op.output_shape[0].dims[:]
                for consumer in consumers:
                    if consumer.type == MaceOp.Reshape.name and \
                            consumer.input[1] in self._consts and \
                            self._consts[consumer.input[1]].int32_data[:] == \
                            [op_output_shape[0], 1, 1, op_output_shape[1]]:
                        # work for tensorflow
                        net.tensors.remove(self._consts[consumer.input[1]])
                        del consumer.input[1]
                        self.safe_remove_node(consumer, None)
                        return True
        return False
李寅 已提交
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228

    def transform_channel_shuffle(self):
        net = self._model
        for op in net.op:
            if op.type == MaceOp.Transpose.name and \
                    len(op.output_shape[0].dims) == 5:
                perm = ConverterUtil.get_arg(op,
                                             MaceKeyword.mace_dims_str).ints
                if [0, 1, 2, 4, 3] == list(perm):
                    # Remove the following Reshape op
                    reshape_op = self._consumers.get(op.output[0], None)
                    if (reshape_op and
                            len(reshape_op) == 1 and
                            reshape_op[0].type == MaceOp.Reshape.name and
                            len(reshape_op[0].output_shape[0].dims) == 4):
                        print("Transform channel shuffle")
                        output_shape = reshape_op[0].output_shape[0].dims
                        self.safe_remove_node(reshape_op[0], op,
                                              remove_input_tensor=True)
                    else:
                        return False

                    # Change Transpose op to ChannelShuffle
                    op.type = MaceOp.ChannelShuffle.name
                    del op.arg[:]
                    group_arg = op.arg.add()
                    group_arg.name = MaceKeyword.mace_group_str
                    group_arg.i = op.output_shape[0].dims[4]
                    op.output_shape[0].dims[:] = output_shape

                    # Remove previous Reshape op
                    producer_op = self._producer.get(op.input[0], None)
                    if producer_op:
                        if producer_op.type == MaceOp.Reshape.name:
                            self.safe_remove_node(producer_op, None)
                        elif producer_op.type == MaceOp.Stack.name:
                            print("Change channel shuffle stack to concat")
                            # Change previous Stack op to Concat if any
                            producer_op.type = MaceOp.Concat.name
                            producer_op.output_shape[0].dims[:] = output_shape

                    return True
2229

2230
    def quantize_specific_ops_only(self):
2231 2232 2233 2234
        """
        This transform rule is only used internally, we are not gonna make
        things too complex for users
        """
2235 2236 2237 2238 2239
        to_quantize_ops_output_type = {
            MaceOp.MatMul.name: mace_pb2.DT_INT32,
            MaceOp.Gather.name: mace_pb2.DT_UINT8,
        }

2240
        for op in self._model.op:
2241 2242
            if (op.type not in to_quantize_ops_output_type
                    or len(op.output) > 1
2243 2244 2245 2246 2247 2248
                    or ConverterUtil.get_arg(op,
                                             MaceKeyword.mace_op_data_type_str).i != mace_pb2.DT_FLOAT):  # noqa
                # only support single output
                continue

            quantized_inputs_names = []
L
liyin 已提交
2249

2250
            should_quantize = False
L
liyin 已提交
2251 2252 2253 2254 2255 2256 2257 2258
            has_const = False
            for idx, input_tensor in enumerate(op.input):
                if input_tensor in self._consts:
                    has_const = True
                    break
            if not has_const:
                continue

2259 2260
            for idx, input_tensor in enumerate(op.input):
                if self.get_tensor_data_type(input_tensor) \
2261 2262
                        == mace_pb2.DT_FLOAT:
                    should_quantize = True
2263 2264 2265
                    break
            if not should_quantize:
                continue
2266 2267
            else:
                print("Quantize op %s (%s)" % (op.name, op.type))
2268

L
liyin 已提交
2269 2270
            non_zero = self._option.device == DeviceType.CPU.value \
                and op.type == MaceOp.MatMul.name
2271 2272 2273 2274

            for idx, input_tensor in enumerate(op.input):
                quantized_inputs_names.append(input_tensor)

2275 2276 2277 2278
                if self.get_tensor_data_type(input_tensor) \
                        != mace_pb2.DT_FLOAT:
                    continue

2279 2280 2281
                if input_tensor in self._consts:
                    const_tensor = self._consts[input_tensor]
                    quantized_tensor = quantize_util.quantize(
B
Bin Li 已提交
2282
                        const_tensor.float_data, self._option.device, non_zero)
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
                    del const_tensor.float_data[:]
                    const_tensor.int32_data.extend(quantized_tensor.data)
                    const_tensor.data_type = mace_pb2.DT_UINT8
                    const_tensor.scale = quantized_tensor.scale
                    const_tensor.zero_point = quantized_tensor.zero
                    const_tensor.minval = quantized_tensor.minval
                    const_tensor.maxval = quantized_tensor.maxval
                    const_tensor.quantized = True
                else:
                    input_shape = self.get_tensor_shape(input_tensor)
                    quantize_op = self._model.op.add()
                    quantize_op.name = self.normalize_op_name(
                        input_tensor) + "_quant"
                    quantize_op.type = MaceOp.Quantize.name
                    quantize_op.input.extend([input_tensor])
                    quantize_output_name = quantize_op.name + '_0'
                    quantize_op.output.extend([quantize_output_name])
                    output_shape = quantize_op.output_shape.add()
                    output_shape.dims.extend(input_shape)
                    quantize_op.output_type.extend([mace_pb2.DT_UINT8])
                    data_type_arg = quantize_op.arg.add()
                    data_type_arg.name = MaceKeyword.mace_op_data_type_str
                    data_type_arg.i = mace_pb2.DT_UINT8
2306 2307 2308
                    ConverterUtil.add_data_format_arg(
                        quantize_op,
                        self.get_tensor_data_format(input_tensor))
2309 2310 2311

                    data_type_arg = quantize_op.arg.add()
                    data_type_arg.name = MaceKeyword.mace_non_zero
L
liyin 已提交
2312
                    data_type_arg.i = 0
2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323

                    find_range_arg = quantize_op.arg.add()
                    find_range_arg.name = \
                        MaceKeyword.mace_find_range_every_time
                    find_range_arg.i = 1

                    quantized_inputs_names[-1] = quantize_output_name

            del op.input[:]
            op.input.extend(quantized_inputs_names)

2324 2325
            original_output_name = op.output[0]
            op.output[0] = original_output_name + "_quant"
2326
            op.output_type.extend([to_quantize_ops_output_type[op.type]])
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337
            data_type_arg = ConverterUtil.get_arg(op,
                                                  MaceKeyword.mace_op_data_type_str)  # noqa
            if data_type_arg is None:
                data_type_arg = op.arg.add()
                data_type_arg.name = MaceKeyword.mace_op_data_type_str
            data_type_arg.i = mace_pb2.DT_UINT8

            dequantize_op = self._model.op.add()
            dequantize_op.name = op.name + "_dequant"
            dequantize_op.type = MaceOp.Dequantize.name
            dequantize_op.input.extend([op.output[0]])
2338
            dequantize_op.output.extend([original_output_name])
2339 2340 2341 2342
            dequantize_op.output_shape.extend(op.output_shape)
            dequantize_op.output_type.extend([mace_pb2.DT_FLOAT])
            data_type_arg = dequantize_op.arg.add()
            data_type_arg.name = MaceKeyword.mace_op_data_type_str
2343
            data_type_arg.i = to_quantize_ops_output_type[op.type]
2344 2345 2346
            ConverterUtil.add_data_format_arg(
                dequantize_op,
                self.get_tensor_data_format(original_output_name))
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356
            quantize_flag_arg = ConverterUtil.get_arg(self._model,
                                                      MaceKeyword.mace_quantize_flag_arg_str)  # noqa
            if quantize_flag_arg is None:
                quantize_flag_arg = self._model.arg.add()
                quantize_flag_arg.name = MaceKeyword.mace_quantize_flag_arg_str
                quantize_flag_arg.i = 1

            return True

        return False