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

L
liyin 已提交
15 16 17 18
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

L
liyin 已提交
19

B
Bin Li 已提交
20 21
import copy
import numpy as np
B
Bin Li 已提交
22
from enum import Enum
B
Bin Li 已提交
23
from operator import mul
L
liyin 已提交
24
from functools import reduce
B
Bin Li 已提交
25

L
liyin 已提交
26 27
from py_proto import mace_pb2
from transform import base_converter
B
Bin Li 已提交
28
from transform.base_converter import ActivationType
L
liyin 已提交
29 30 31 32 33 34
from transform.base_converter import ConverterUtil
from transform.base_converter import DeviceType
from transform.base_converter import EltwiseType
from transform.base_converter import MaceKeyword
from transform.base_converter import MaceOp
from transform.base_converter import PaddingMode
B
Bin Li 已提交
35
from transform.base_converter import PadType
L
liyin 已提交
36 37 38
from transform.base_converter import PoolingType
from transform.base_converter import ReduceType
from utils.util import mace_check
B
Bin Li 已提交
39 40


B
Bin Li 已提交
41 42
HexagonSupportedOps = [
    'BatchToSpaceND_8',
B
Bin Li 已提交
43
    'DepthToSpace_8',
B
Bin Li 已提交
44 45
    'DepthwiseSupernode_8x8p32to8',
    'DequantizeOUTPUT_8tof',
B
Bin Li 已提交
46 47
    'INPUT',
    'OUTPUT',
B
Bin Li 已提交
48 49 50 51
    'QuantizedAdd_8p8to8',
    'QuantizedAvgPool_8',
    'QuantizedConcat_8',
    'QuantizedMaxPool_8',
B
Bin Li 已提交
52 53
    'QuantizedMaximum_8',
    'QuantizedMinimum_8',
B
Bin Li 已提交
54
    'QuantizedMul_8x8to8',
B
Bin Li 已提交
55 56 57 58
    'QuantizedPad_8',
    'QuantizedRelu_8',
    'QuantizedReluX_8',
    'QuantizedReshape',
B
Bin Li 已提交
59
    'QuantizedResizeBilinear_8',
B
Bin Li 已提交
60
    'QuantizedSigmoid_8',
B
Bin Li 已提交
61
    'QuantizedSoftmax_8',
B
Bin Li 已提交
62
    'QuantizedStridedSlice_8',
B
Bin Li 已提交
63
    'QuantizedSub_8p8to8',
B
Bin Li 已提交
64
    'QuantizedTanh_8',
65
    'QuantizedTransposeConv2d_8x8p32to8',
B
Bin Li 已提交
66 67
    'QuantizeINPUT_f_to_8',
    'SpaceToBatchND_8',
B
Bin Li 已提交
68
    'SpaceToDepth_8',
B
Bin Li 已提交
69 70 71 72 73 74 75
    'Supernode_8x8p32to8',
    'Nop',
]

HexagonOp = Enum('HexagonOp', [(op, op) for op in HexagonSupportedOps],
                 type=str)

B
Bin Li 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
padding_mode = {
    PaddingMode.NA: 0,
    PaddingMode.SAME: 1,
    PaddingMode.VALID: 2
}


def get_tensor_name_from_op(op_name, port):
    return op_name + ':' + str(port)


def get_op_and_port_from_tensor(tensor_name):
    if ':' in tensor_name:
        op, port = tensor_name.split(':')
        port = int(port)
    else:
        op = tensor_name
        port = 0
    return op, port


B
Bin Li 已提交
97 98 99 100 101 102 103 104 105 106 107
def get_input_shape(tensor_name, model):
    producer_op_name, _ = get_op_and_port_from_tensor(tensor_name)
    input_shape = None
    for producer_op in model.op:
        if producer_op.name == producer_op_name:
            input_shape = producer_op.output_shape[0].dims
            break
    mace_check(input_shape is not None, "Missing input shape.")
    return input_shape


B
Bin Li 已提交
108 109 110 111
def normalize_name(name):
    return name.replace(':', '_')


B
Bin Li 已提交
112
class HexagonConverter(base_converter.ConverterInterface):
B
Bin Li 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    activation_type = {
        ActivationType.RELU.name: HexagonOp.QuantizedRelu_8.name,
        ActivationType.RELUX.name: HexagonOp.QuantizedReluX_8.name,
        ActivationType.TANH.name: HexagonOp.QuantizedTanh_8.name,
        ActivationType.SIGMOID.name: HexagonOp.QuantizedSigmoid_8.name,
    }

    eltwise_type = {
        EltwiseType.SUM.value: HexagonOp.QuantizedAdd_8p8to8.name,
        EltwiseType.SUB.value: HexagonOp.QuantizedSub_8p8to8.name,
        EltwiseType.PROD.value: HexagonOp.QuantizedMul_8x8to8.name,
        EltwiseType.MIN.value: HexagonOp.QuantizedMinimum_8.name,
        EltwiseType.MAX.value: HexagonOp.QuantizedMaximum_8.name,
    }

B
Bin Li 已提交
128 129 130 131 132
    def __init__(self, option, model, quantize_activation_info):
        self._option = option
        self._model = model
        self._consts = {}
        self._quantize_activation_info = quantize_activation_info
B
Bin Li 已提交
133
        self._op_converters = {
B
Bin Li 已提交
134
            MaceOp.Activation.name: self.convert_activation,
B
Bin Li 已提交
135 136 137
            MaceOp.BatchToSpaceND.name: self.convert_batchspace,
            MaceOp.Concat.name: self.convert_concat,
            MaceOp.Conv2D.name: self.convert_conv2d,
138
            MaceOp.Deconv2D.name: self.convert_deconv2d,
B
Bin Li 已提交
139 140 141 142
            MaceOp.DepthToSpace.name: self.convert_depthspace,
            MaceOp.DepthwiseConv2d.name: self.convert_conv2d,
            MaceOp.Dequantize.name: self.convert_dequantize,
            MaceOp.Eltwise.name: self.convert_elementwise,
B
Bin Li 已提交
143 144
            MaceOp.ExpandDims.name: self.convert_expanddims,
            MaceOp.Pad.name: self.convert_pad,
B
Bin Li 已提交
145 146 147 148 149
            MaceOp.Pooling.name: self.convert_pooling,
            MaceOp.Quantize.name: self.convert_quantize,
            MaceOp.Reduce.name: self.convert_reduce,
            MaceOp.ResizeBilinear.name: self.convert_resizebilinear,
            MaceOp.Softmax.name: self.convert_softmax,
B
Bin Li 已提交
150
            MaceOp.StridedSlice.name: self.convert_stridedslice,
B
Bin Li 已提交
151 152 153
            MaceOp.SpaceToBatchND.name: self.convert_batchspace,
            MaceOp.SpaceToDepth.name: self.convert_depthspace,
        }
B
Bin Li 已提交
154 155 156 157 158 159 160 161

    def run(self):
        for tensor in self._model.tensors:
            self._consts[tensor.name] = tensor

        # convert op node
        self.convert_ops()

B
Bin Li 已提交
162
        model_inputs = self.convert_input_output_node()
B
Bin Li 已提交
163

B
Bin Li 已提交
164
        self.add_node_id(model_inputs)
B
Bin Li 已提交
165 166 167

        return self._model

L
liyin 已提交
168 169 170 171 172 173 174 175 176
    def add_port_for_tensors(self,  tensors):
        for i in range(len(tensors)):
            if ':' not in tensors[i]:
                node_name = tensors[i]
                tensors[i] += ':0'
                if node_name in self._quantize_activation_info:
                    self._quantize_activation_info[tensors[i]] = \
                        self._quantize_activation_info[node_name]

B
Bin Li 已提交
177 178 179 180
    def add_scalar_const_node(self, name, val, op=None):
        if op is not None:
            name = op.name + name
            op.input.append(name)
B
Bin Li 已提交
181 182 183 184 185 186 187 188
        if name not in self._consts:
            tensor = self._model.tensors.add()
            self._consts[name] = tensor
            tensor.name = name
            tensor.data_type = mace_pb2.DT_FLOAT
            tensor.dims.extend([1])
            tensor.float_data.extend([val])

B
Bin Li 已提交
189 190 191 192 193 194 195 196 197 198 199 200
    def add_arg_const_node(self, op, name, dims, data=None, insert_index=None):
        arg_tensor = self._model.tensors.add()
        arg_tensor.name = op.name + name
        arg_tensor.data_type = mace_pb2.DT_INT32
        arg_tensor.dims.extend(dims)
        if data:
            arg_tensor.int32_data.extend(data)
        if insert_index is None:
            op.input.append(arg_tensor.name)
        else:
            op.input.insert(insert_index, arg_tensor.name)

B
Bin Li 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    def add_min_max_const_node(
            self, this_op, tensor_name, add_min=True, add_max=True,
            diff_port=True):
        op, port = get_op_and_port_from_tensor(tensor_name)
        mace_check(port == 0, 'port should be 0 to add min max tensor then.')
        if tensor_name in self._quantize_activation_info:
            quantize_info = self._quantize_activation_info[tensor_name]
            minval = quantize_info.minval
            maxval = quantize_info.maxval
            is_activation = True
        elif tensor_name in self._consts:
            tensor = self._consts[tensor_name]
            minval = tensor.minval
            maxval = tensor.maxval
            is_activation = False
        else:
            raise Exception('Quantize info not found: ', tensor_name)

        if add_min:
            if is_activation and diff_port:
                min_tensor_name = op + ':1'
            else:
                min_tensor_name = op + '_min:0'
224
                self.add_scalar_const_node(min_tensor_name, minval)
B
Bin Li 已提交
225 226 227 228 229 230
            this_op.input.extend([min_tensor_name])
        if add_max:
            if is_activation and diff_port:
                max_tensor_name = op + ':2'
            else:
                max_tensor_name = op + '_max:0'
231
                self.add_scalar_const_node(max_tensor_name, maxval)
B
Bin Li 已提交
232 233
            this_op.input.extend([max_tensor_name])

B
Bin Li 已提交
234 235 236 237 238 239
    def add_constant_min_max_for_first_op(self, op):
        minval = self._quantize_activation_info[op.input[0]].minval
        maxval = self._quantize_activation_info[op.input[0]].maxval
        input_op, _ = get_op_and_port_from_tensor(op.input[0])
        input_min = input_op + '_min:0'
        input_max = input_op + '_max:0'
240 241
        self.add_scalar_const_node(input_min, minval)
        self.add_scalar_const_node(input_max, maxval)
B
Bin Li 已提交
242 243 244 245 246
        for i in range(len(op.input)):
            if op.input[i] == input_op + ':1':
                op.input[i] = input_min
            elif op.input[i] == input_op + ':2':
                op.input[i] = input_max
B
Bin Li 已提交
247

248 249
    def convert_input_output_node(self):
        quantize_input_op = self._model.op[0]
B
Bin Li 已提交
250
        mace_check(
251
            quantize_input_op.type == HexagonOp.QuantizeINPUT_f_to_8.name,
B
Bin Li 已提交
252
            "Not started with Quantize op.")
B
Bin Li 已提交
253
        first_quantize_input_op = copy.deepcopy(quantize_input_op)
B
Bin Li 已提交
254
        del quantize_input_op.input[:]
B
Bin Li 已提交
255 256 257 258
        del quantize_input_op.output[:]
        del quantize_input_op.output_shape[:]
        del quantize_input_op.output_type[:]
        del quantize_input_op.out_max_byte_size[:]
B
Bin Li 已提交
259 260

        dequantize_output_op = self._model.op[-1]
261 262 263
        mace_check(dequantize_output_op.type
                   == HexagonOp.DequantizeOUTPUT_8tof.name,
                   "Not ended with Dequantize op.")
B
Bin Li 已提交
264
        last_dequantize_output_op = copy.deepcopy(dequantize_output_op)
265
        del dequantize_output_op.input[:]
B
Bin Li 已提交
266
        del dequantize_output_op.output[:]
B
Bin Li 已提交
267 268 269 270
        del dequantize_output_op.output_shape[:]
        del dequantize_output_op.output_type[:]
        del dequantize_output_op.out_max_byte_size[:]

B
Bin Li 已提交
271 272 273 274 275 276 277 278
        # Combine multiple inputs/outputs to one hexagon input/output node,
        # in input_info/output_info order
        ops = {}
        for op in self._model.op:
            ops[op.name] = op
        for input_node in self._option.input_nodes.values():
            op_name = normalize_name(
                MaceKeyword.mace_input_node_name + '_' + input_node.name)
B
Bin Li 已提交
279 280 281 282 283
            if op_name == first_quantize_input_op.name:
                op = first_quantize_input_op
                quantize_input_op.name = MaceKeyword.mace_input_node_name
            else:
                op = ops[op_name]
B
Bin Li 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
            mace_check(op.type == HexagonOp.QuantizeINPUT_f_to_8.name,
                       "input node type is: %s" % op.type)
            quantize_input_op.output.extend(op.output)
            quantize_input_op.output_shape.extend(op.output_shape)
            quantize_input_op.output_type.extend(op.output_type)
            quantize_input_op.out_max_byte_size.extend(
                op.out_max_byte_size)
        for output_node in self._option.check_nodes.values():
            op_name = normalize_name(output_node.name)
            op = last_dequantize_output_op \
                if op_name == last_dequantize_output_op.name else ops[op_name]
            mace_check(op.type == HexagonOp.DequantizeOUTPUT_8tof.name,
                       "output node type is: %s" % op.type)
            dequantize_output_op.input.extend(op.input)

        # Delete redundant inputs/outputs nodes
300 301 302
        index = 1
        while index < len(self._model.op) - 1:
            op = self._model.op[index]
B
Bin Li 已提交
303 304
            if op.type == HexagonOp.QuantizeINPUT_f_to_8.name \
                    or op.type == HexagonOp.DequantizeOUTPUT_8tof.name:
305
                del self._model.op[index]
B
Bin Li 已提交
306 307
            else:
                index += 1
308

B
Bin Li 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322
        if self._option.device == DeviceType.HTA.value:
            # replace QuantizeINPUT_f_to_8 with INPUT
            quantize_input_op.type = HexagonOp.INPUT.name
            del quantize_input_op.output_shape[1:]
            del quantize_input_op.output_type[1:]
            del quantize_input_op.out_max_byte_size[1:]

            # replace first op's input min max with constant
            self.add_constant_min_max_for_first_op(self._model.op[1])

            # replace DequantizeOUTPUT_8tof with OUTPUT
            dequantize_output_op.type = HexagonOp.OUTPUT.name
            del dequantize_output_op.input[1:]

B
Bin Li 已提交
323 324 325
        return quantize_input_op.output

    def add_node_id(self, model_inputs):
B
Bin Li 已提交
326 327 328 329 330
        node_id_counter = 0
        node_id_map = {}
        for tensor in self._model.tensors:
            tensor.node_id = node_id_counter
            node_id_counter += 1
L
liyin 已提交
331
            node_id_map[tensor.name] = tensor.node_id
B
Bin Li 已提交
332

B
Bin Li 已提交
333
        print("Hexagon op:")
B
Bin Li 已提交
334
        index = 0
B
Bin Li 已提交
335 336
        for op in self._model.op:
            op.node_id = node_id_counter
L
liyin 已提交
337 338 339
            node_id_counter += 1
            for output in op.output:
                node_id_map[output] = op.node_id
B
Bin Li 已提交
340 341 342 343 344 345 346 347
            if op.type not in [HexagonOp.QuantizeINPUT_f_to_8,
                               HexagonOp.DequantizeOUTPUT_8tof.name]:
                index_str = str(index)
                index += 1
            else:
                index_str = ''
            print('Op: %s (%s, node_id:%d, index:%s)' %
                  (op.name, op.type, op.node_id, index_str))
B
Bin Li 已提交
348 349
            for ipt in op.input:
                op_name, port = get_op_and_port_from_tensor(ipt)
L
liyin 已提交
350 351
                tensor_name = ipt if port == 0 else op_name + ':0'
                node_id = node_id_map[tensor_name]
B
Bin Li 已提交
352 353
                node_input = op.node_input.add()
                node_input.node_id = node_id
B
Bin Li 已提交
354 355 356 357 358
                if tensor_name in model_inputs:
                    for i in range(len(model_inputs)):
                        if model_inputs[i] == tensor_name:
                            port += i * 3
                node_input.output_port = port
B
Bin Li 已提交
359 360 361 362 363 364 365 366 367 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

    def convert_ops(self):
        print("Convert mace graph to hexagon.")
        for op in self._model.op:
            mace_check(op.type in self._op_converters,
                       "Mace Hexagon does not support op type %s yet"
                       % op.type)
            self.pre_convert(op)
            self._op_converters[op.type](op)
            self.post_convert(op)

    def pre_convert(self, op):
        self.add_port_for_tensors(op.input)
        self.add_port_for_tensors(op.output)

    def post_convert(self, op):
        if op.type != MaceOp.Dequantize.name:
            min_output_shape = op.output_shape.add()
            min_output_shape.dims.extend([1])
            max_output_shape = op.output_shape.add()
            max_output_shape.dims.extend([1])
            op.output_type.extend(
                [mace_pb2.DT_UINT8, mace_pb2.DT_FLOAT, mace_pb2.DT_FLOAT])
        for i in range(len(op.output_shape)):
            out_max_byte_size = reduce(mul, op.output_shape[i].dims)
            if op.output_type[i] == mace_pb2.DT_FLOAT:
                out_max_byte_size *= 4
            op.out_max_byte_size.extend([out_max_byte_size])

        op.padding = padding_mode[PaddingMode.NA]
        arg = ConverterUtil.get_arg(op, MaceKeyword.mace_padding_str)
        if arg is not None:
            op.padding = padding_mode[PaddingMode(arg.i)]

B
Bin Li 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
    def convert_activation(self, op):
        self.add_min_max_const_node(op, op.input[0])

        act_type = ConverterUtil.get_arg(
            op, MaceKeyword.mace_activation_type_str).s.decode()
        if act_type == ActivationType.RELUX.name:
            x = ConverterUtil.get_arg(
                op, MaceKeyword.mace_activation_max_limit_str).f
            self.add_scalar_const_node("/x:0", x, op)
        try:
            op.type = self.activation_type[act_type]
        except KeyError:
            mace_check(False,
                       "Hexagon does not support activation %s" % act_type)

B
Bin Li 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
    def convert_batchspace(self, op):
        strides_arg = ConverterUtil.get_arg(
            op, MaceKeyword.mace_space_batch_block_shape_str)
        self.add_arg_const_node(
            op, '/strides:0', [1, 1, 1, len(strides_arg.ints)],
            strides_arg.ints)

        if op.type == MaceOp.BatchToSpaceND.name:
            pad_arg = ConverterUtil.get_arg(
                op, MaceKeyword.mace_batch_to_space_crops_str)
        else:
            pad_arg = ConverterUtil.get_arg(
                op, MaceKeyword.mace_paddings_str)
        self.add_arg_const_node(
            op, '/pad:0', [1, 1, len(pad_arg.ints) // 2, 2], pad_arg.ints)

        self.add_min_max_const_node(op, op.input[0])

        if op.type == MaceOp.BatchToSpaceND.name:
            op.type = HexagonOp.BatchToSpaceND_8.name
        else:
            op.type = HexagonOp.SpaceToBatchND_8.name

    def convert_concat(self, op):
        inputs = copy.deepcopy(op.input)
        for ipt in inputs:
            self.add_min_max_const_node(op, ipt, True, False)
        for ipt in inputs:
            self.add_min_max_const_node(op, ipt, False, True)

        dim_arg = ConverterUtil.get_arg(
            op, MaceKeyword.mace_axis_str)
        self.add_arg_const_node(op, '/dim:0', [1], [dim_arg.i], 0)

        op.type = HexagonOp.QuantizedConcat_8.name

    def convert_conv2d(self, op):
        channels = op.output_shape[0].dims[3]
        if len(op.input) < 3:
            print('Supernode requires biasadd, we add it.')
            bias_data = np.zeros(channels, dtype=int)
            bias_tensor = self._model.tensors.add()
            bias_tensor.data_type = mace_pb2.DT_INT32
            bias_tensor.dims.extend([channels])
            bias_tensor.int32_data.extend(bias_data)
            bias_tensor.minval = 0
            bias_tensor.maxval = 0
            bias_tensor.name = op.name + "/bias:0"
            bias = bias_tensor.name
            self._consts[bias] = bias_tensor
        else:
            bias = op.input.pop()

        self.add_min_max_const_node(op, op.input[0])
        self.add_min_max_const_node(op, op.input[1])

        strides_arg = ConverterUtil.get_arg(op, 'strides')
        mace_check(strides_arg is not None,
                   "Missing strides of Conv or Depthwise Conv.")
        self.add_arg_const_node(
            op, '/strides:0', [1, strides_arg.ints[0], strides_arg.ints[1], 1])

        op.input.append(bias)
        self.add_min_max_const_node(op, bias)
        self.add_min_max_const_node(
            op, op.output[0], True, True, False)

        if op.type == MaceOp.DepthwiseConv2d.name:
            op.type = HexagonOp.DepthwiseSupernode_8x8p32to8.name
        else:
            op.type = HexagonOp.Supernode_8x8p32to8.name

480 481 482 483 484
    def add_deconv_pad_node(self, op):
        padding_type_arg = \
            ConverterUtil.get_arg(op, MaceKeyword.mace_padding_type_str)
        mace_check(padding_type_arg is not None, "Missing padding of Deconv.")
        padding_type = PaddingMode(padding_type_arg.i)
B
Bin Li 已提交
485 486 487 488 489 490 491 492
        strides_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_strides_str)
        mace_check(strides_arg is not None, "Missing strides of Deconv.")
        stride_h = strides_arg.ints[0]
        stride_w = strides_arg.ints[1]

        input_shape = get_input_shape(op.input[0], self._model)
        input_h = input_shape[1]
        input_w = input_shape[2]
493
        filter_tensor = self._consts[op.input[1]]
B
Bin Li 已提交
494 495 496 497
        filter_h = filter_tensor.dims[1]
        filter_w = filter_tensor.dims[2]
        output_h = op.output_shape[0].dims[1]
        output_w = op.output_shape[0].dims[2]
498 499

        if padding_type == PaddingMode.VALID:
B
Bin Li 已提交
500 501
            expected_input_h = (output_h - filter_h + stride_h) // stride_h
            expected_input_w = (output_w - filter_w + stride_w) // stride_w
502
        elif padding_type == PaddingMode.SAME:
B
Bin Li 已提交
503 504
            expected_input_h = (output_h + stride_h - 1) // stride_h
            expected_input_w = (output_w + stride_w - 1) // stride_w
505 506 507
        else:
            raise Exception('Hexagon deconv does not support padding type: ',
                            padding_type)
B
Bin Li 已提交
508 509
        mace_check(expected_input_h == input_h, "Wrong input/output height")
        mace_check(expected_input_w == input_w, "Wrong input/output width")
510

B
Bin Li 已提交
511 512 513 514 515
        pad_h = (input_h - 1) * stride_h + filter_h - output_h
        pad_w = (input_w - 1) * stride_w + filter_w - output_w
        pad_h, pad_w = max(pad_h, 0), max(pad_w, 0)
        paddings = [pad_h, pad_h, pad_w, pad_w]
        self.add_arg_const_node(op, "/paddings:0", [1, 1, 2, 2], paddings)
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

    def convert_deconv2d(self, op):
        channels = op.output_shape[0].dims[3]
        if len(op.input) < 4:
            print('Hexagon deconv requires biasadd, we add it.')
            bias_data = np.zeros(channels, dtype=int)
            bias_tensor = self._model.tensors.add()
            bias_tensor.data_type = mace_pb2.DT_INT32
            bias_tensor.dims.extend([channels])
            bias_tensor.int32_data.extend(bias_data)
            bias_tensor.minval = 0
            bias_tensor.maxval = 0
            bias_tensor.name = op.name + "/bias:0"
            bias = bias_tensor.name
            self._consts[bias] = bias_tensor
        else:
            bias = op.input.pop()
        op.input.pop()  # output shape

        self.add_min_max_const_node(op, op.input[0])
        self.add_min_max_const_node(op, op.input[1])

        self.add_deconv_pad_node(op)

        strides_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_strides_str)
        mace_check(strides_arg is not None, "Missing strides of Deconv.")
        self.add_arg_const_node(
            op, '/strides:0', [1, strides_arg.ints[0], strides_arg.ints[1], 1])

        op.input.append(bias)
        self.add_min_max_const_node(op, bias)
        self.add_min_max_const_node(
            op, op.output[0], True, True, False)

        op.type = HexagonOp.QuantizedTransposeConv2d_8x8p32to8.name

B
Bin Li 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    def convert_depthspace(self, op):
        size_arg = ConverterUtil.get_arg(
            op, MaceKeyword.mace_space_depth_block_size_str)
        self.add_arg_const_node(op, '/block_size:0', [1], [size_arg.i])

        self.add_min_max_const_node(op, op.input[0])

        if op.type == MaceOp.DepthToSpace.name:
            op.type = HexagonOp.DepthToSpace_8.name
        else:
            op.type = HexagonOp.SpaceToDepth_8.name

    def convert_dequantize(self, op):
        self.add_min_max_const_node(op, op.input[0])

        op.type = HexagonOp.DequantizeOUTPUT_8tof.name

    def convert_elementwise(self, op):
B
Bin Li 已提交
570 571 572
        if len(op.input) == 1:
            scalar_input_arg = ConverterUtil.get_arg(
                op, MaceKeyword.mace_scalar_input_str)
B
Bin Li 已提交
573
            self.add_scalar_const_node("/b:0", scalar_input_arg.f, op)
B
Bin Li 已提交
574 575 576
        self.add_min_max_const_node(op, op.input[0])
        self.add_min_max_const_node(op, op.input[1])

B
Bin Li 已提交
577 578 579 580 581 582
        element_type = ConverterUtil.get_arg(
            op, MaceKeyword.mace_element_type_str).i
        if element_type in [EltwiseType.SUM.value,
                            EltwiseType.SUB.value,
                            EltwiseType.MIN.value,
                            EltwiseType.MAX.value]:
B
Bin Li 已提交
583 584
            self.add_min_max_const_node(
                op, op.output[0], True, True, False)
B
Bin Li 已提交
585 586 587
        try:
            op.type = self.eltwise_type[element_type]
        except KeyError:
B
Bin Li 已提交
588 589 590 591
            mace_check(False,
                       "Hexagon does not support elementwise %s"
                       % EltwiseType(element_type).name)

B
Bin Li 已提交
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
    def convert_expanddims(self, op):
        shape = op.output_shape[0].dims
        self.add_arg_const_node(op, '/shape:0', [len(shape)], shape)

        self.add_min_max_const_node(op, op.input[0])
        # Convert to reshape as hexagon does not support quantized expanddims
        op.type = HexagonOp.QuantizedReshape.name

    def convert_pad(self, op):
        self.add_min_max_const_node(op, op.input[0])

        paddings = ConverterUtil.get_arg(
            op, MaceKeyword.mace_paddings_str).ints
        self.add_arg_const_node(
            op, '/paddings:0', [1, 1, len(paddings) // 2, 2], paddings)

        pad_type = ConverterUtil.get_arg(op, MaceKeyword.mace_pad_type_str).i
        mace_check(pad_type == PadType.CONSTANT.value,
                   "Hexagon only supports constant pad")
        constant_value = ConverterUtil.get_arg(
            op, MaceKeyword.mace_constant_value_str).f
        self.add_scalar_const_node('/constant_value:0', constant_value, op)

        op.type = HexagonOp.QuantizedPad_8.name

B
Bin Li 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    def convert_pooling(self, op):
        self.add_min_max_const_node(op, op.input[0])

        window_arg = ConverterUtil.get_arg(
            op, MaceKeyword.mace_kernel_str)
        self.add_arg_const_node(
            op, '/window:0', [1, window_arg.ints[0], window_arg.ints[1], 1])
        strides_arg = ConverterUtil.get_arg(
            op, MaceKeyword.mace_strides_str)
        self.add_arg_const_node(
            op, '/strides:0', [1, strides_arg.ints[0], strides_arg.ints[1], 1])

        pooling_type_arg = ConverterUtil.get_arg(
            op, MaceKeyword.mace_pooling_type_str)
        if PoolingType(pooling_type_arg.i) == PoolingType.AVG:
            op.type = HexagonOp.QuantizedAvgPool_8.name
        else:
            op.type = HexagonOp.QuantizedMaxPool_8.name

    def convert_quantize(self, op):
        op.type = HexagonOp.QuantizeINPUT_f_to_8.name

    def convert_reduce(self, op):
        self.add_min_max_const_node(op, op.input[0])
        reduce_type_arg = ConverterUtil.get_arg(
            op, MaceKeyword.mace_reduce_type_str)
        mace_check(reduce_type_arg.i == ReduceType.MEAN.value,
                   "Hexagon Reduce only supports Mean now.")
        keep_dims_arg = ConverterUtil.get_arg(
            op, MaceKeyword.mace_keepdims_str)
        mace_check(keep_dims_arg.i == 1,
                   "Hexagon Reduce Mean only supports keep dims now.")
        axis_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_axis_str)
        mace_check(1 <= len(axis_arg.ints) <= 2,
                   "Hexagon Reduce Mean only supports spatial now.")
        for i in axis_arg.ints:
            mace_check(1 <= i <= 2,
                       "Hexagon Reduce Mean only supports spatial now")
B
Bin Li 已提交
655
        input_shape = get_input_shape(op.input[0], self._model)
B
Bin Li 已提交
656
        if len(axis_arg.ints) == 1:
B
Bin Li 已提交
657 658
            dim1, dim2 = (input_shape[1], 1) \
                if axis_arg.ints[0] == 1 else (1, input_shape[2])
B
Bin Li 已提交
659
        else:
B
Bin Li 已提交
660
            dim1, dim2 = input_shape[1], input_shape[2]
B
Bin Li 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
        self.add_arg_const_node(op, '/window:0', [1, dim1, dim2, 1])
        self.add_arg_const_node(op, '/strides:0', [1, dim1, dim2, 1])

        op.type = HexagonOp.QuantizedAvgPool_8.name

    def convert_resizebilinear(self, op):
        newdim_arg = ConverterUtil.get_arg(
            op, MaceKeyword.mace_resize_size_str)
        self.add_arg_const_node(
            op, '/newdim:0', [len(newdim_arg.ints)], newdim_arg.ints)

        self.add_min_max_const_node(op, op.input[0])

        align_corners_arg = ConverterUtil.get_arg(
            op, MaceKeyword.mace_align_corners_str)
        self.add_arg_const_node(
            op, '/align_corners:0', [1], [align_corners_arg.i])

        op.type = HexagonOp.QuantizedResizeBilinear_8.name

    def convert_softmax(self, op):
        self.add_min_max_const_node(op, op.input[0])

        op.type = HexagonOp.QuantizedSoftmax_8.name
B
Bin Li 已提交
685 686

    def convert_stridedslice(self, op):
B
Bin Li 已提交
687
        begin_mask = ConverterUtil.get_arg(
B
Bin Li 已提交
688 689 690 691 692
            op, MaceKeyword.mace_begin_mask_str).i
        end_mask = ConverterUtil.get_arg(
            op, MaceKeyword.mace_end_mask_str).i
        shrink_mask = ConverterUtil.get_arg(
            op, MaceKeyword.mace_shrink_axis_mask_str).i
B
Bin Li 已提交
693 694 695
        self.add_arg_const_node(op, "/begin_mask:0", [1], [begin_mask])
        self.add_arg_const_node(op, "/end_mask:0", [1], [end_mask])
        self.add_arg_const_node(op, "/shrink_mask:0", [1], [shrink_mask])
B
Bin Li 已提交
696 697 698
        self.add_min_max_const_node(op, op.input[0])

        op.type = HexagonOp.QuantizedStridedSlice_8.name